intel_display.c 485 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385
  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 "intel_dsi.h"
  40. #include "i915_trace.h"
  41. #include <drm/drm_atomic.h>
  42. #include <drm/drm_atomic_helper.h>
  43. #include <drm/drm_dp_helper.h>
  44. #include <drm/drm_crtc_helper.h>
  45. #include <drm/drm_plane_helper.h>
  46. #include <drm/drm_rect.h>
  47. #include <linux/dma_remapping.h>
  48. #include <linux/reservation.h>
  49. static bool is_mmio_work(struct intel_flip_work *work)
  50. {
  51. return work->mmio_work.func;
  52. }
  53. /* Primary plane formats for gen <= 3 */
  54. static const uint32_t i8xx_primary_formats[] = {
  55. DRM_FORMAT_C8,
  56. DRM_FORMAT_RGB565,
  57. DRM_FORMAT_XRGB1555,
  58. DRM_FORMAT_XRGB8888,
  59. };
  60. /* Primary plane formats for gen >= 4 */
  61. static const uint32_t i965_primary_formats[] = {
  62. DRM_FORMAT_C8,
  63. DRM_FORMAT_RGB565,
  64. DRM_FORMAT_XRGB8888,
  65. DRM_FORMAT_XBGR8888,
  66. DRM_FORMAT_XRGB2101010,
  67. DRM_FORMAT_XBGR2101010,
  68. };
  69. static const uint32_t skl_primary_formats[] = {
  70. DRM_FORMAT_C8,
  71. DRM_FORMAT_RGB565,
  72. DRM_FORMAT_XRGB8888,
  73. DRM_FORMAT_XBGR8888,
  74. DRM_FORMAT_ARGB8888,
  75. DRM_FORMAT_ABGR8888,
  76. DRM_FORMAT_XRGB2101010,
  77. DRM_FORMAT_XBGR2101010,
  78. DRM_FORMAT_YUYV,
  79. DRM_FORMAT_YVYU,
  80. DRM_FORMAT_UYVY,
  81. DRM_FORMAT_VYUY,
  82. };
  83. /* Cursor formats */
  84. static const uint32_t intel_cursor_formats[] = {
  85. DRM_FORMAT_ARGB8888,
  86. };
  87. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  88. struct intel_crtc_state *pipe_config);
  89. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  90. struct intel_crtc_state *pipe_config);
  91. static int intel_framebuffer_init(struct drm_device *dev,
  92. struct intel_framebuffer *ifb,
  93. struct drm_mode_fb_cmd2 *mode_cmd,
  94. struct drm_i915_gem_object *obj);
  95. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  96. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  97. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
  98. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  99. struct intel_link_m_n *m_n,
  100. struct intel_link_m_n *m2_n2);
  101. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  102. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  103. static void haswell_set_pipemisc(struct drm_crtc *crtc);
  104. static void vlv_prepare_pll(struct intel_crtc *crtc,
  105. const struct intel_crtc_state *pipe_config);
  106. static void chv_prepare_pll(struct intel_crtc *crtc,
  107. const struct intel_crtc_state *pipe_config);
  108. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  109. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  110. static void skl_init_scalers(struct drm_i915_private *dev_priv,
  111. struct intel_crtc *crtc,
  112. struct intel_crtc_state *crtc_state);
  113. static void skylake_pfit_enable(struct intel_crtc *crtc);
  114. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
  115. static void ironlake_pfit_enable(struct intel_crtc *crtc);
  116. static void intel_modeset_setup_hw_state(struct drm_device *dev);
  117. static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
  118. static int ilk_max_pixel_rate(struct drm_atomic_state *state);
  119. static int bxt_calc_cdclk(int max_pixclk);
  120. struct intel_limit {
  121. struct {
  122. int min, max;
  123. } dot, vco, n, m, m1, m2, p, p1;
  124. struct {
  125. int dot_limit;
  126. int p2_slow, p2_fast;
  127. } p2;
  128. };
  129. /* returns HPLL frequency in kHz */
  130. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  131. {
  132. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  133. /* Obtain SKU information */
  134. mutex_lock(&dev_priv->sb_lock);
  135. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  136. CCK_FUSE_HPLL_FREQ_MASK;
  137. mutex_unlock(&dev_priv->sb_lock);
  138. return vco_freq[hpll_freq] * 1000;
  139. }
  140. int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
  141. const char *name, u32 reg, int ref_freq)
  142. {
  143. u32 val;
  144. int divider;
  145. mutex_lock(&dev_priv->sb_lock);
  146. val = vlv_cck_read(dev_priv, reg);
  147. mutex_unlock(&dev_priv->sb_lock);
  148. divider = val & CCK_FREQUENCY_VALUES;
  149. WARN((val & CCK_FREQUENCY_STATUS) !=
  150. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  151. "%s change in progress\n", name);
  152. return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
  153. }
  154. static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  155. const char *name, u32 reg)
  156. {
  157. if (dev_priv->hpll_freq == 0)
  158. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  159. return vlv_get_cck_clock(dev_priv, name, reg,
  160. dev_priv->hpll_freq);
  161. }
  162. static int
  163. intel_pch_rawclk(struct drm_i915_private *dev_priv)
  164. {
  165. return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
  166. }
  167. static int
  168. intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
  169. {
  170. /* RAWCLK_FREQ_VLV register updated from power well code */
  171. return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
  172. CCK_DISPLAY_REF_CLOCK_CONTROL);
  173. }
  174. static int
  175. intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
  176. {
  177. uint32_t clkcfg;
  178. /* hrawclock is 1/4 the FSB frequency */
  179. clkcfg = I915_READ(CLKCFG);
  180. switch (clkcfg & CLKCFG_FSB_MASK) {
  181. case CLKCFG_FSB_400:
  182. return 100000;
  183. case CLKCFG_FSB_533:
  184. return 133333;
  185. case CLKCFG_FSB_667:
  186. return 166667;
  187. case CLKCFG_FSB_800:
  188. return 200000;
  189. case CLKCFG_FSB_1067:
  190. return 266667;
  191. case CLKCFG_FSB_1333:
  192. return 333333;
  193. /* these two are just a guess; one of them might be right */
  194. case CLKCFG_FSB_1600:
  195. case CLKCFG_FSB_1600_ALT:
  196. return 400000;
  197. default:
  198. return 133333;
  199. }
  200. }
  201. void intel_update_rawclk(struct drm_i915_private *dev_priv)
  202. {
  203. if (HAS_PCH_SPLIT(dev_priv))
  204. dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
  205. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  206. dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
  207. else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
  208. dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
  209. else
  210. return; /* no rawclk on other platforms, or no need to know it */
  211. DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
  212. }
  213. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  214. {
  215. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  216. return;
  217. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  218. CCK_CZ_CLOCK_CONTROL);
  219. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  220. }
  221. static inline u32 /* units of 100MHz */
  222. intel_fdi_link_freq(struct drm_i915_private *dev_priv,
  223. const struct intel_crtc_state *pipe_config)
  224. {
  225. if (HAS_DDI(dev_priv))
  226. return pipe_config->port_clock; /* SPLL */
  227. else if (IS_GEN5(dev_priv))
  228. return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
  229. else
  230. return 270000;
  231. }
  232. static const struct intel_limit intel_limits_i8xx_dac = {
  233. .dot = { .min = 25000, .max = 350000 },
  234. .vco = { .min = 908000, .max = 1512000 },
  235. .n = { .min = 2, .max = 16 },
  236. .m = { .min = 96, .max = 140 },
  237. .m1 = { .min = 18, .max = 26 },
  238. .m2 = { .min = 6, .max = 16 },
  239. .p = { .min = 4, .max = 128 },
  240. .p1 = { .min = 2, .max = 33 },
  241. .p2 = { .dot_limit = 165000,
  242. .p2_slow = 4, .p2_fast = 2 },
  243. };
  244. static const struct intel_limit intel_limits_i8xx_dvo = {
  245. .dot = { .min = 25000, .max = 350000 },
  246. .vco = { .min = 908000, .max = 1512000 },
  247. .n = { .min = 2, .max = 16 },
  248. .m = { .min = 96, .max = 140 },
  249. .m1 = { .min = 18, .max = 26 },
  250. .m2 = { .min = 6, .max = 16 },
  251. .p = { .min = 4, .max = 128 },
  252. .p1 = { .min = 2, .max = 33 },
  253. .p2 = { .dot_limit = 165000,
  254. .p2_slow = 4, .p2_fast = 4 },
  255. };
  256. static const struct intel_limit intel_limits_i8xx_lvds = {
  257. .dot = { .min = 25000, .max = 350000 },
  258. .vco = { .min = 908000, .max = 1512000 },
  259. .n = { .min = 2, .max = 16 },
  260. .m = { .min = 96, .max = 140 },
  261. .m1 = { .min = 18, .max = 26 },
  262. .m2 = { .min = 6, .max = 16 },
  263. .p = { .min = 4, .max = 128 },
  264. .p1 = { .min = 1, .max = 6 },
  265. .p2 = { .dot_limit = 165000,
  266. .p2_slow = 14, .p2_fast = 7 },
  267. };
  268. static const struct intel_limit intel_limits_i9xx_sdvo = {
  269. .dot = { .min = 20000, .max = 400000 },
  270. .vco = { .min = 1400000, .max = 2800000 },
  271. .n = { .min = 1, .max = 6 },
  272. .m = { .min = 70, .max = 120 },
  273. .m1 = { .min = 8, .max = 18 },
  274. .m2 = { .min = 3, .max = 7 },
  275. .p = { .min = 5, .max = 80 },
  276. .p1 = { .min = 1, .max = 8 },
  277. .p2 = { .dot_limit = 200000,
  278. .p2_slow = 10, .p2_fast = 5 },
  279. };
  280. static const struct intel_limit intel_limits_i9xx_lvds = {
  281. .dot = { .min = 20000, .max = 400000 },
  282. .vco = { .min = 1400000, .max = 2800000 },
  283. .n = { .min = 1, .max = 6 },
  284. .m = { .min = 70, .max = 120 },
  285. .m1 = { .min = 8, .max = 18 },
  286. .m2 = { .min = 3, .max = 7 },
  287. .p = { .min = 7, .max = 98 },
  288. .p1 = { .min = 1, .max = 8 },
  289. .p2 = { .dot_limit = 112000,
  290. .p2_slow = 14, .p2_fast = 7 },
  291. };
  292. static const struct intel_limit intel_limits_g4x_sdvo = {
  293. .dot = { .min = 25000, .max = 270000 },
  294. .vco = { .min = 1750000, .max = 3500000},
  295. .n = { .min = 1, .max = 4 },
  296. .m = { .min = 104, .max = 138 },
  297. .m1 = { .min = 17, .max = 23 },
  298. .m2 = { .min = 5, .max = 11 },
  299. .p = { .min = 10, .max = 30 },
  300. .p1 = { .min = 1, .max = 3},
  301. .p2 = { .dot_limit = 270000,
  302. .p2_slow = 10,
  303. .p2_fast = 10
  304. },
  305. };
  306. static const struct intel_limit intel_limits_g4x_hdmi = {
  307. .dot = { .min = 22000, .max = 400000 },
  308. .vco = { .min = 1750000, .max = 3500000},
  309. .n = { .min = 1, .max = 4 },
  310. .m = { .min = 104, .max = 138 },
  311. .m1 = { .min = 16, .max = 23 },
  312. .m2 = { .min = 5, .max = 11 },
  313. .p = { .min = 5, .max = 80 },
  314. .p1 = { .min = 1, .max = 8},
  315. .p2 = { .dot_limit = 165000,
  316. .p2_slow = 10, .p2_fast = 5 },
  317. };
  318. static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
  319. .dot = { .min = 20000, .max = 115000 },
  320. .vco = { .min = 1750000, .max = 3500000 },
  321. .n = { .min = 1, .max = 3 },
  322. .m = { .min = 104, .max = 138 },
  323. .m1 = { .min = 17, .max = 23 },
  324. .m2 = { .min = 5, .max = 11 },
  325. .p = { .min = 28, .max = 112 },
  326. .p1 = { .min = 2, .max = 8 },
  327. .p2 = { .dot_limit = 0,
  328. .p2_slow = 14, .p2_fast = 14
  329. },
  330. };
  331. static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
  332. .dot = { .min = 80000, .max = 224000 },
  333. .vco = { .min = 1750000, .max = 3500000 },
  334. .n = { .min = 1, .max = 3 },
  335. .m = { .min = 104, .max = 138 },
  336. .m1 = { .min = 17, .max = 23 },
  337. .m2 = { .min = 5, .max = 11 },
  338. .p = { .min = 14, .max = 42 },
  339. .p1 = { .min = 2, .max = 6 },
  340. .p2 = { .dot_limit = 0,
  341. .p2_slow = 7, .p2_fast = 7
  342. },
  343. };
  344. static const struct intel_limit intel_limits_pineview_sdvo = {
  345. .dot = { .min = 20000, .max = 400000},
  346. .vco = { .min = 1700000, .max = 3500000 },
  347. /* Pineview's Ncounter is a ring counter */
  348. .n = { .min = 3, .max = 6 },
  349. .m = { .min = 2, .max = 256 },
  350. /* Pineview only has one combined m divider, which we treat as m2. */
  351. .m1 = { .min = 0, .max = 0 },
  352. .m2 = { .min = 0, .max = 254 },
  353. .p = { .min = 5, .max = 80 },
  354. .p1 = { .min = 1, .max = 8 },
  355. .p2 = { .dot_limit = 200000,
  356. .p2_slow = 10, .p2_fast = 5 },
  357. };
  358. static const struct intel_limit intel_limits_pineview_lvds = {
  359. .dot = { .min = 20000, .max = 400000 },
  360. .vco = { .min = 1700000, .max = 3500000 },
  361. .n = { .min = 3, .max = 6 },
  362. .m = { .min = 2, .max = 256 },
  363. .m1 = { .min = 0, .max = 0 },
  364. .m2 = { .min = 0, .max = 254 },
  365. .p = { .min = 7, .max = 112 },
  366. .p1 = { .min = 1, .max = 8 },
  367. .p2 = { .dot_limit = 112000,
  368. .p2_slow = 14, .p2_fast = 14 },
  369. };
  370. /* Ironlake / Sandybridge
  371. *
  372. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  373. * the range value for them is (actual_value - 2).
  374. */
  375. static const struct intel_limit intel_limits_ironlake_dac = {
  376. .dot = { .min = 25000, .max = 350000 },
  377. .vco = { .min = 1760000, .max = 3510000 },
  378. .n = { .min = 1, .max = 5 },
  379. .m = { .min = 79, .max = 127 },
  380. .m1 = { .min = 12, .max = 22 },
  381. .m2 = { .min = 5, .max = 9 },
  382. .p = { .min = 5, .max = 80 },
  383. .p1 = { .min = 1, .max = 8 },
  384. .p2 = { .dot_limit = 225000,
  385. .p2_slow = 10, .p2_fast = 5 },
  386. };
  387. static const struct intel_limit intel_limits_ironlake_single_lvds = {
  388. .dot = { .min = 25000, .max = 350000 },
  389. .vco = { .min = 1760000, .max = 3510000 },
  390. .n = { .min = 1, .max = 3 },
  391. .m = { .min = 79, .max = 118 },
  392. .m1 = { .min = 12, .max = 22 },
  393. .m2 = { .min = 5, .max = 9 },
  394. .p = { .min = 28, .max = 112 },
  395. .p1 = { .min = 2, .max = 8 },
  396. .p2 = { .dot_limit = 225000,
  397. .p2_slow = 14, .p2_fast = 14 },
  398. };
  399. static const struct intel_limit intel_limits_ironlake_dual_lvds = {
  400. .dot = { .min = 25000, .max = 350000 },
  401. .vco = { .min = 1760000, .max = 3510000 },
  402. .n = { .min = 1, .max = 3 },
  403. .m = { .min = 79, .max = 127 },
  404. .m1 = { .min = 12, .max = 22 },
  405. .m2 = { .min = 5, .max = 9 },
  406. .p = { .min = 14, .max = 56 },
  407. .p1 = { .min = 2, .max = 8 },
  408. .p2 = { .dot_limit = 225000,
  409. .p2_slow = 7, .p2_fast = 7 },
  410. };
  411. /* LVDS 100mhz refclk limits. */
  412. static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
  413. .dot = { .min = 25000, .max = 350000 },
  414. .vco = { .min = 1760000, .max = 3510000 },
  415. .n = { .min = 1, .max = 2 },
  416. .m = { .min = 79, .max = 126 },
  417. .m1 = { .min = 12, .max = 22 },
  418. .m2 = { .min = 5, .max = 9 },
  419. .p = { .min = 28, .max = 112 },
  420. .p1 = { .min = 2, .max = 8 },
  421. .p2 = { .dot_limit = 225000,
  422. .p2_slow = 14, .p2_fast = 14 },
  423. };
  424. static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
  425. .dot = { .min = 25000, .max = 350000 },
  426. .vco = { .min = 1760000, .max = 3510000 },
  427. .n = { .min = 1, .max = 3 },
  428. .m = { .min = 79, .max = 126 },
  429. .m1 = { .min = 12, .max = 22 },
  430. .m2 = { .min = 5, .max = 9 },
  431. .p = { .min = 14, .max = 42 },
  432. .p1 = { .min = 2, .max = 6 },
  433. .p2 = { .dot_limit = 225000,
  434. .p2_slow = 7, .p2_fast = 7 },
  435. };
  436. static const struct intel_limit intel_limits_vlv = {
  437. /*
  438. * These are the data rate limits (measured in fast clocks)
  439. * since those are the strictest limits we have. The fast
  440. * clock and actual rate limits are more relaxed, so checking
  441. * them would make no difference.
  442. */
  443. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  444. .vco = { .min = 4000000, .max = 6000000 },
  445. .n = { .min = 1, .max = 7 },
  446. .m1 = { .min = 2, .max = 3 },
  447. .m2 = { .min = 11, .max = 156 },
  448. .p1 = { .min = 2, .max = 3 },
  449. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  450. };
  451. static const struct intel_limit intel_limits_chv = {
  452. /*
  453. * These are the data rate limits (measured in fast clocks)
  454. * since those are the strictest limits we have. The fast
  455. * clock and actual rate limits are more relaxed, so checking
  456. * them would make no difference.
  457. */
  458. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  459. .vco = { .min = 4800000, .max = 6480000 },
  460. .n = { .min = 1, .max = 1 },
  461. .m1 = { .min = 2, .max = 2 },
  462. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  463. .p1 = { .min = 2, .max = 4 },
  464. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  465. };
  466. static const struct intel_limit intel_limits_bxt = {
  467. /* FIXME: find real dot limits */
  468. .dot = { .min = 0, .max = INT_MAX },
  469. .vco = { .min = 4800000, .max = 6700000 },
  470. .n = { .min = 1, .max = 1 },
  471. .m1 = { .min = 2, .max = 2 },
  472. /* FIXME: find real m2 limits */
  473. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  474. .p1 = { .min = 2, .max = 4 },
  475. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  476. };
  477. static bool
  478. needs_modeset(struct drm_crtc_state *state)
  479. {
  480. return drm_atomic_crtc_needs_modeset(state);
  481. }
  482. /*
  483. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  484. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  485. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  486. * The helpers' return value is the rate of the clock that is fed to the
  487. * display engine's pipe which can be the above fast dot clock rate or a
  488. * divided-down version of it.
  489. */
  490. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  491. static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
  492. {
  493. clock->m = clock->m2 + 2;
  494. clock->p = clock->p1 * clock->p2;
  495. if (WARN_ON(clock->n == 0 || clock->p == 0))
  496. return 0;
  497. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  498. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  499. return clock->dot;
  500. }
  501. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  502. {
  503. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  504. }
  505. static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
  506. {
  507. clock->m = i9xx_dpll_compute_m(clock);
  508. clock->p = clock->p1 * clock->p2;
  509. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  510. return 0;
  511. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  512. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  513. return clock->dot;
  514. }
  515. static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
  516. {
  517. clock->m = clock->m1 * clock->m2;
  518. clock->p = clock->p1 * clock->p2;
  519. if (WARN_ON(clock->n == 0 || clock->p == 0))
  520. return 0;
  521. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  522. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  523. return clock->dot / 5;
  524. }
  525. int chv_calc_dpll_params(int refclk, struct dpll *clock)
  526. {
  527. clock->m = clock->m1 * clock->m2;
  528. clock->p = clock->p1 * clock->p2;
  529. if (WARN_ON(clock->n == 0 || clock->p == 0))
  530. return 0;
  531. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  532. clock->n << 22);
  533. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  534. return clock->dot / 5;
  535. }
  536. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  537. /**
  538. * Returns whether the given set of divisors are valid for a given refclk with
  539. * the given connectors.
  540. */
  541. static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
  542. const struct intel_limit *limit,
  543. const struct dpll *clock)
  544. {
  545. if (clock->n < limit->n.min || limit->n.max < clock->n)
  546. INTELPllInvalid("n out of range\n");
  547. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  548. INTELPllInvalid("p1 out of range\n");
  549. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  550. INTELPllInvalid("m2 out of range\n");
  551. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  552. INTELPllInvalid("m1 out of range\n");
  553. if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
  554. !IS_CHERRYVIEW(dev_priv) && !IS_BROXTON(dev_priv))
  555. if (clock->m1 <= clock->m2)
  556. INTELPllInvalid("m1 <= m2\n");
  557. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  558. !IS_BROXTON(dev_priv)) {
  559. if (clock->p < limit->p.min || limit->p.max < clock->p)
  560. INTELPllInvalid("p out of range\n");
  561. if (clock->m < limit->m.min || limit->m.max < clock->m)
  562. INTELPllInvalid("m out of range\n");
  563. }
  564. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  565. INTELPllInvalid("vco out of range\n");
  566. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  567. * connector, etc., rather than just a single range.
  568. */
  569. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  570. INTELPllInvalid("dot out of range\n");
  571. return true;
  572. }
  573. static int
  574. i9xx_select_p2_div(const struct intel_limit *limit,
  575. const struct intel_crtc_state *crtc_state,
  576. int target)
  577. {
  578. struct drm_device *dev = crtc_state->base.crtc->dev;
  579. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  580. /*
  581. * For LVDS just rely on its current settings for dual-channel.
  582. * We haven't figured out how to reliably set up different
  583. * single/dual channel state, if we even can.
  584. */
  585. if (intel_is_dual_link_lvds(dev))
  586. return limit->p2.p2_fast;
  587. else
  588. return limit->p2.p2_slow;
  589. } else {
  590. if (target < limit->p2.dot_limit)
  591. return limit->p2.p2_slow;
  592. else
  593. return limit->p2.p2_fast;
  594. }
  595. }
  596. /*
  597. * Returns a set of divisors for the desired target clock with the given
  598. * refclk, or FALSE. The returned values represent the clock equation:
  599. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  600. *
  601. * Target and reference clocks are specified in kHz.
  602. *
  603. * If match_clock is provided, then best_clock P divider must match the P
  604. * divider from @match_clock used for LVDS downclocking.
  605. */
  606. static bool
  607. i9xx_find_best_dpll(const struct intel_limit *limit,
  608. struct intel_crtc_state *crtc_state,
  609. int target, int refclk, struct dpll *match_clock,
  610. struct dpll *best_clock)
  611. {
  612. struct drm_device *dev = crtc_state->base.crtc->dev;
  613. struct dpll clock;
  614. int err = target;
  615. memset(best_clock, 0, sizeof(*best_clock));
  616. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  617. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  618. clock.m1++) {
  619. for (clock.m2 = limit->m2.min;
  620. clock.m2 <= limit->m2.max; clock.m2++) {
  621. if (clock.m2 >= clock.m1)
  622. break;
  623. for (clock.n = limit->n.min;
  624. clock.n <= limit->n.max; clock.n++) {
  625. for (clock.p1 = limit->p1.min;
  626. clock.p1 <= limit->p1.max; clock.p1++) {
  627. int this_err;
  628. i9xx_calc_dpll_params(refclk, &clock);
  629. if (!intel_PLL_is_valid(to_i915(dev),
  630. limit,
  631. &clock))
  632. continue;
  633. if (match_clock &&
  634. clock.p != match_clock->p)
  635. continue;
  636. this_err = abs(clock.dot - target);
  637. if (this_err < err) {
  638. *best_clock = clock;
  639. err = this_err;
  640. }
  641. }
  642. }
  643. }
  644. }
  645. return (err != target);
  646. }
  647. /*
  648. * Returns a set of divisors for the desired target clock with the given
  649. * refclk, or FALSE. The returned values represent the clock equation:
  650. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  651. *
  652. * Target and reference clocks are specified in kHz.
  653. *
  654. * If match_clock is provided, then best_clock P divider must match the P
  655. * divider from @match_clock used for LVDS downclocking.
  656. */
  657. static bool
  658. pnv_find_best_dpll(const struct intel_limit *limit,
  659. struct intel_crtc_state *crtc_state,
  660. int target, int refclk, struct dpll *match_clock,
  661. struct dpll *best_clock)
  662. {
  663. struct drm_device *dev = crtc_state->base.crtc->dev;
  664. struct dpll clock;
  665. int err = target;
  666. memset(best_clock, 0, sizeof(*best_clock));
  667. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  668. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  669. clock.m1++) {
  670. for (clock.m2 = limit->m2.min;
  671. clock.m2 <= limit->m2.max; clock.m2++) {
  672. for (clock.n = limit->n.min;
  673. clock.n <= limit->n.max; clock.n++) {
  674. for (clock.p1 = limit->p1.min;
  675. clock.p1 <= limit->p1.max; clock.p1++) {
  676. int this_err;
  677. pnv_calc_dpll_params(refclk, &clock);
  678. if (!intel_PLL_is_valid(to_i915(dev),
  679. limit,
  680. &clock))
  681. continue;
  682. if (match_clock &&
  683. clock.p != match_clock->p)
  684. continue;
  685. this_err = abs(clock.dot - target);
  686. if (this_err < err) {
  687. *best_clock = clock;
  688. err = this_err;
  689. }
  690. }
  691. }
  692. }
  693. }
  694. return (err != target);
  695. }
  696. /*
  697. * Returns a set of divisors for the desired target clock with the given
  698. * refclk, or FALSE. The returned values represent the clock equation:
  699. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  700. *
  701. * Target and reference clocks are specified in kHz.
  702. *
  703. * If match_clock is provided, then best_clock P divider must match the P
  704. * divider from @match_clock used for LVDS downclocking.
  705. */
  706. static bool
  707. g4x_find_best_dpll(const struct intel_limit *limit,
  708. struct intel_crtc_state *crtc_state,
  709. int target, int refclk, struct dpll *match_clock,
  710. struct dpll *best_clock)
  711. {
  712. struct drm_device *dev = crtc_state->base.crtc->dev;
  713. struct dpll clock;
  714. int max_n;
  715. bool found = false;
  716. /* approximately equals target * 0.00585 */
  717. int err_most = (target >> 8) + (target >> 9);
  718. memset(best_clock, 0, sizeof(*best_clock));
  719. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  720. max_n = limit->n.max;
  721. /* based on hardware requirement, prefer smaller n to precision */
  722. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  723. /* based on hardware requirement, prefere larger m1,m2 */
  724. for (clock.m1 = limit->m1.max;
  725. clock.m1 >= limit->m1.min; clock.m1--) {
  726. for (clock.m2 = limit->m2.max;
  727. clock.m2 >= limit->m2.min; clock.m2--) {
  728. for (clock.p1 = limit->p1.max;
  729. clock.p1 >= limit->p1.min; clock.p1--) {
  730. int this_err;
  731. i9xx_calc_dpll_params(refclk, &clock);
  732. if (!intel_PLL_is_valid(to_i915(dev),
  733. limit,
  734. &clock))
  735. continue;
  736. this_err = abs(clock.dot - target);
  737. if (this_err < err_most) {
  738. *best_clock = clock;
  739. err_most = this_err;
  740. max_n = clock.n;
  741. found = true;
  742. }
  743. }
  744. }
  745. }
  746. }
  747. return found;
  748. }
  749. /*
  750. * Check if the calculated PLL configuration is more optimal compared to the
  751. * best configuration and error found so far. Return the calculated error.
  752. */
  753. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  754. const struct dpll *calculated_clock,
  755. const struct dpll *best_clock,
  756. unsigned int best_error_ppm,
  757. unsigned int *error_ppm)
  758. {
  759. /*
  760. * For CHV ignore the error and consider only the P value.
  761. * Prefer a bigger P value based on HW requirements.
  762. */
  763. if (IS_CHERRYVIEW(to_i915(dev))) {
  764. *error_ppm = 0;
  765. return calculated_clock->p > best_clock->p;
  766. }
  767. if (WARN_ON_ONCE(!target_freq))
  768. return false;
  769. *error_ppm = div_u64(1000000ULL *
  770. abs(target_freq - calculated_clock->dot),
  771. target_freq);
  772. /*
  773. * Prefer a better P value over a better (smaller) error if the error
  774. * is small. Ensure this preference for future configurations too by
  775. * setting the error to 0.
  776. */
  777. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  778. *error_ppm = 0;
  779. return true;
  780. }
  781. return *error_ppm + 10 < best_error_ppm;
  782. }
  783. /*
  784. * Returns a set of divisors for the desired target clock with the given
  785. * refclk, or FALSE. The returned values represent the clock equation:
  786. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  787. */
  788. static bool
  789. vlv_find_best_dpll(const struct intel_limit *limit,
  790. struct intel_crtc_state *crtc_state,
  791. int target, int refclk, struct dpll *match_clock,
  792. struct dpll *best_clock)
  793. {
  794. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  795. struct drm_device *dev = crtc->base.dev;
  796. struct dpll clock;
  797. unsigned int bestppm = 1000000;
  798. /* min update 19.2 MHz */
  799. int max_n = min(limit->n.max, refclk / 19200);
  800. bool found = false;
  801. target *= 5; /* fast clock */
  802. memset(best_clock, 0, sizeof(*best_clock));
  803. /* based on hardware requirement, prefer smaller n to precision */
  804. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  805. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  806. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  807. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  808. clock.p = clock.p1 * clock.p2;
  809. /* based on hardware requirement, prefer bigger m1,m2 values */
  810. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  811. unsigned int ppm;
  812. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  813. refclk * clock.m1);
  814. vlv_calc_dpll_params(refclk, &clock);
  815. if (!intel_PLL_is_valid(to_i915(dev),
  816. limit,
  817. &clock))
  818. continue;
  819. if (!vlv_PLL_is_optimal(dev, target,
  820. &clock,
  821. best_clock,
  822. bestppm, &ppm))
  823. continue;
  824. *best_clock = clock;
  825. bestppm = ppm;
  826. found = true;
  827. }
  828. }
  829. }
  830. }
  831. return found;
  832. }
  833. /*
  834. * Returns a set of divisors for the desired target clock with the given
  835. * refclk, or FALSE. The returned values represent the clock equation:
  836. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  837. */
  838. static bool
  839. chv_find_best_dpll(const struct intel_limit *limit,
  840. struct intel_crtc_state *crtc_state,
  841. int target, int refclk, struct dpll *match_clock,
  842. struct dpll *best_clock)
  843. {
  844. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  845. struct drm_device *dev = crtc->base.dev;
  846. unsigned int best_error_ppm;
  847. struct dpll clock;
  848. uint64_t m2;
  849. int found = false;
  850. memset(best_clock, 0, sizeof(*best_clock));
  851. best_error_ppm = 1000000;
  852. /*
  853. * Based on hardware doc, the n always set to 1, and m1 always
  854. * set to 2. If requires to support 200Mhz refclk, we need to
  855. * revisit this because n may not 1 anymore.
  856. */
  857. clock.n = 1, clock.m1 = 2;
  858. target *= 5; /* fast clock */
  859. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  860. for (clock.p2 = limit->p2.p2_fast;
  861. clock.p2 >= limit->p2.p2_slow;
  862. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  863. unsigned int error_ppm;
  864. clock.p = clock.p1 * clock.p2;
  865. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  866. clock.n) << 22, refclk * clock.m1);
  867. if (m2 > INT_MAX/clock.m1)
  868. continue;
  869. clock.m2 = m2;
  870. chv_calc_dpll_params(refclk, &clock);
  871. if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
  872. continue;
  873. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  874. best_error_ppm, &error_ppm))
  875. continue;
  876. *best_clock = clock;
  877. best_error_ppm = error_ppm;
  878. found = true;
  879. }
  880. }
  881. return found;
  882. }
  883. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  884. struct dpll *best_clock)
  885. {
  886. int refclk = 100000;
  887. const struct intel_limit *limit = &intel_limits_bxt;
  888. return chv_find_best_dpll(limit, crtc_state,
  889. target_clock, refclk, NULL, best_clock);
  890. }
  891. bool intel_crtc_active(struct intel_crtc *crtc)
  892. {
  893. /* Be paranoid as we can arrive here with only partial
  894. * state retrieved from the hardware during setup.
  895. *
  896. * We can ditch the adjusted_mode.crtc_clock check as soon
  897. * as Haswell has gained clock readout/fastboot support.
  898. *
  899. * We can ditch the crtc->primary->fb check as soon as we can
  900. * properly reconstruct framebuffers.
  901. *
  902. * FIXME: The intel_crtc->active here should be switched to
  903. * crtc->state->active once we have proper CRTC states wired up
  904. * for atomic.
  905. */
  906. return crtc->active && crtc->base.primary->state->fb &&
  907. crtc->config->base.adjusted_mode.crtc_clock;
  908. }
  909. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  910. enum pipe pipe)
  911. {
  912. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  913. return crtc->config->cpu_transcoder;
  914. }
  915. static bool pipe_dsl_stopped(struct drm_i915_private *dev_priv, enum pipe pipe)
  916. {
  917. i915_reg_t reg = PIPEDSL(pipe);
  918. u32 line1, line2;
  919. u32 line_mask;
  920. if (IS_GEN2(dev_priv))
  921. line_mask = DSL_LINEMASK_GEN2;
  922. else
  923. line_mask = DSL_LINEMASK_GEN3;
  924. line1 = I915_READ(reg) & line_mask;
  925. msleep(5);
  926. line2 = I915_READ(reg) & line_mask;
  927. return line1 == line2;
  928. }
  929. /*
  930. * intel_wait_for_pipe_off - wait for pipe to turn off
  931. * @crtc: crtc whose pipe to wait for
  932. *
  933. * After disabling a pipe, we can't wait for vblank in the usual way,
  934. * spinning on the vblank interrupt status bit, since we won't actually
  935. * see an interrupt when the pipe is disabled.
  936. *
  937. * On Gen4 and above:
  938. * wait for the pipe register state bit to turn off
  939. *
  940. * Otherwise:
  941. * wait for the display line value to settle (it usually
  942. * ends up stopping at the start of the next frame).
  943. *
  944. */
  945. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  946. {
  947. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  948. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  949. enum pipe pipe = crtc->pipe;
  950. if (INTEL_GEN(dev_priv) >= 4) {
  951. i915_reg_t reg = PIPECONF(cpu_transcoder);
  952. /* Wait for the Pipe State to go off */
  953. if (intel_wait_for_register(dev_priv,
  954. reg, I965_PIPECONF_ACTIVE, 0,
  955. 100))
  956. WARN(1, "pipe_off wait timed out\n");
  957. } else {
  958. /* Wait for the display line to settle */
  959. if (wait_for(pipe_dsl_stopped(dev_priv, pipe), 100))
  960. WARN(1, "pipe_off wait timed out\n");
  961. }
  962. }
  963. /* Only for pre-ILK configs */
  964. void assert_pll(struct drm_i915_private *dev_priv,
  965. enum pipe pipe, bool state)
  966. {
  967. u32 val;
  968. bool cur_state;
  969. val = I915_READ(DPLL(pipe));
  970. cur_state = !!(val & DPLL_VCO_ENABLE);
  971. I915_STATE_WARN(cur_state != state,
  972. "PLL state assertion failure (expected %s, current %s)\n",
  973. onoff(state), onoff(cur_state));
  974. }
  975. /* XXX: the dsi pll is shared between MIPI DSI ports */
  976. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  977. {
  978. u32 val;
  979. bool cur_state;
  980. mutex_lock(&dev_priv->sb_lock);
  981. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  982. mutex_unlock(&dev_priv->sb_lock);
  983. cur_state = val & DSI_PLL_VCO_EN;
  984. I915_STATE_WARN(cur_state != state,
  985. "DSI PLL state assertion failure (expected %s, current %s)\n",
  986. onoff(state), onoff(cur_state));
  987. }
  988. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  989. enum pipe pipe, bool state)
  990. {
  991. bool cur_state;
  992. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  993. pipe);
  994. if (HAS_DDI(dev_priv)) {
  995. /* DDI does not have a specific FDI_TX register */
  996. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  997. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  998. } else {
  999. u32 val = I915_READ(FDI_TX_CTL(pipe));
  1000. cur_state = !!(val & FDI_TX_ENABLE);
  1001. }
  1002. I915_STATE_WARN(cur_state != state,
  1003. "FDI TX state assertion failure (expected %s, current %s)\n",
  1004. onoff(state), onoff(cur_state));
  1005. }
  1006. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1007. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1008. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1009. enum pipe pipe, bool state)
  1010. {
  1011. u32 val;
  1012. bool cur_state;
  1013. val = I915_READ(FDI_RX_CTL(pipe));
  1014. cur_state = !!(val & FDI_RX_ENABLE);
  1015. I915_STATE_WARN(cur_state != state,
  1016. "FDI RX state assertion failure (expected %s, current %s)\n",
  1017. onoff(state), onoff(cur_state));
  1018. }
  1019. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1020. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1021. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1022. enum pipe pipe)
  1023. {
  1024. u32 val;
  1025. /* ILK FDI PLL is always enabled */
  1026. if (IS_GEN5(dev_priv))
  1027. return;
  1028. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1029. if (HAS_DDI(dev_priv))
  1030. return;
  1031. val = I915_READ(FDI_TX_CTL(pipe));
  1032. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1033. }
  1034. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1035. enum pipe pipe, bool state)
  1036. {
  1037. u32 val;
  1038. bool cur_state;
  1039. val = I915_READ(FDI_RX_CTL(pipe));
  1040. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1041. I915_STATE_WARN(cur_state != state,
  1042. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1043. onoff(state), onoff(cur_state));
  1044. }
  1045. void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
  1046. {
  1047. i915_reg_t pp_reg;
  1048. u32 val;
  1049. enum pipe panel_pipe = PIPE_A;
  1050. bool locked = true;
  1051. if (WARN_ON(HAS_DDI(dev_priv)))
  1052. return;
  1053. if (HAS_PCH_SPLIT(dev_priv)) {
  1054. u32 port_sel;
  1055. pp_reg = PP_CONTROL(0);
  1056. port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
  1057. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1058. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1059. panel_pipe = PIPE_B;
  1060. /* XXX: else fix for eDP */
  1061. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1062. /* presumably write lock depends on pipe, not port select */
  1063. pp_reg = PP_CONTROL(pipe);
  1064. panel_pipe = pipe;
  1065. } else {
  1066. pp_reg = PP_CONTROL(0);
  1067. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1068. panel_pipe = PIPE_B;
  1069. }
  1070. val = I915_READ(pp_reg);
  1071. if (!(val & PANEL_POWER_ON) ||
  1072. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1073. locked = false;
  1074. I915_STATE_WARN(panel_pipe == pipe && locked,
  1075. "panel assertion failure, pipe %c regs locked\n",
  1076. pipe_name(pipe));
  1077. }
  1078. static void assert_cursor(struct drm_i915_private *dev_priv,
  1079. enum pipe pipe, bool state)
  1080. {
  1081. bool cur_state;
  1082. if (IS_845G(dev_priv) || IS_I865G(dev_priv))
  1083. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1084. else
  1085. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1086. I915_STATE_WARN(cur_state != state,
  1087. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1088. pipe_name(pipe), onoff(state), onoff(cur_state));
  1089. }
  1090. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1091. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1092. void assert_pipe(struct drm_i915_private *dev_priv,
  1093. enum pipe pipe, bool state)
  1094. {
  1095. bool cur_state;
  1096. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1097. pipe);
  1098. enum intel_display_power_domain power_domain;
  1099. /* if we need the pipe quirk it must be always on */
  1100. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1101. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1102. state = true;
  1103. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1104. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1105. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1106. cur_state = !!(val & PIPECONF_ENABLE);
  1107. intel_display_power_put(dev_priv, power_domain);
  1108. } else {
  1109. cur_state = false;
  1110. }
  1111. I915_STATE_WARN(cur_state != state,
  1112. "pipe %c assertion failure (expected %s, current %s)\n",
  1113. pipe_name(pipe), onoff(state), onoff(cur_state));
  1114. }
  1115. static void assert_plane(struct drm_i915_private *dev_priv,
  1116. enum plane plane, bool state)
  1117. {
  1118. u32 val;
  1119. bool cur_state;
  1120. val = I915_READ(DSPCNTR(plane));
  1121. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1122. I915_STATE_WARN(cur_state != state,
  1123. "plane %c assertion failure (expected %s, current %s)\n",
  1124. plane_name(plane), onoff(state), onoff(cur_state));
  1125. }
  1126. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1127. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1128. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1129. enum pipe pipe)
  1130. {
  1131. int i;
  1132. /* Primary planes are fixed to pipes on gen4+ */
  1133. if (INTEL_GEN(dev_priv) >= 4) {
  1134. u32 val = I915_READ(DSPCNTR(pipe));
  1135. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1136. "plane %c assertion failure, should be disabled but not\n",
  1137. plane_name(pipe));
  1138. return;
  1139. }
  1140. /* Need to check both planes against the pipe */
  1141. for_each_pipe(dev_priv, i) {
  1142. u32 val = I915_READ(DSPCNTR(i));
  1143. enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1144. DISPPLANE_SEL_PIPE_SHIFT;
  1145. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1146. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1147. plane_name(i), pipe_name(pipe));
  1148. }
  1149. }
  1150. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1151. enum pipe pipe)
  1152. {
  1153. int sprite;
  1154. if (INTEL_GEN(dev_priv) >= 9) {
  1155. for_each_sprite(dev_priv, pipe, sprite) {
  1156. u32 val = I915_READ(PLANE_CTL(pipe, sprite));
  1157. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1158. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1159. sprite, pipe_name(pipe));
  1160. }
  1161. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1162. for_each_sprite(dev_priv, pipe, sprite) {
  1163. u32 val = I915_READ(SPCNTR(pipe, sprite));
  1164. I915_STATE_WARN(val & SP_ENABLE,
  1165. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1166. sprite_name(pipe, sprite), pipe_name(pipe));
  1167. }
  1168. } else if (INTEL_GEN(dev_priv) >= 7) {
  1169. u32 val = I915_READ(SPRCTL(pipe));
  1170. I915_STATE_WARN(val & SPRITE_ENABLE,
  1171. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1172. plane_name(pipe), pipe_name(pipe));
  1173. } else if (INTEL_GEN(dev_priv) >= 5) {
  1174. u32 val = I915_READ(DVSCNTR(pipe));
  1175. I915_STATE_WARN(val & DVS_ENABLE,
  1176. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1177. plane_name(pipe), pipe_name(pipe));
  1178. }
  1179. }
  1180. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1181. {
  1182. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1183. drm_crtc_vblank_put(crtc);
  1184. }
  1185. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1186. enum pipe pipe)
  1187. {
  1188. u32 val;
  1189. bool enabled;
  1190. val = I915_READ(PCH_TRANSCONF(pipe));
  1191. enabled = !!(val & TRANS_ENABLE);
  1192. I915_STATE_WARN(enabled,
  1193. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1194. pipe_name(pipe));
  1195. }
  1196. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1197. enum pipe pipe, u32 port_sel, u32 val)
  1198. {
  1199. if ((val & DP_PORT_EN) == 0)
  1200. return false;
  1201. if (HAS_PCH_CPT(dev_priv)) {
  1202. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1203. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1204. return false;
  1205. } else if (IS_CHERRYVIEW(dev_priv)) {
  1206. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1207. return false;
  1208. } else {
  1209. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1210. return false;
  1211. }
  1212. return true;
  1213. }
  1214. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1215. enum pipe pipe, u32 val)
  1216. {
  1217. if ((val & SDVO_ENABLE) == 0)
  1218. return false;
  1219. if (HAS_PCH_CPT(dev_priv)) {
  1220. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1221. return false;
  1222. } else if (IS_CHERRYVIEW(dev_priv)) {
  1223. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1224. return false;
  1225. } else {
  1226. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1227. return false;
  1228. }
  1229. return true;
  1230. }
  1231. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1232. enum pipe pipe, u32 val)
  1233. {
  1234. if ((val & LVDS_PORT_EN) == 0)
  1235. return false;
  1236. if (HAS_PCH_CPT(dev_priv)) {
  1237. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1238. return false;
  1239. } else {
  1240. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1241. return false;
  1242. }
  1243. return true;
  1244. }
  1245. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1246. enum pipe pipe, u32 val)
  1247. {
  1248. if ((val & ADPA_DAC_ENABLE) == 0)
  1249. return false;
  1250. if (HAS_PCH_CPT(dev_priv)) {
  1251. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1252. return false;
  1253. } else {
  1254. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1255. return false;
  1256. }
  1257. return true;
  1258. }
  1259. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1260. enum pipe pipe, i915_reg_t reg,
  1261. u32 port_sel)
  1262. {
  1263. u32 val = I915_READ(reg);
  1264. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1265. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1266. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1267. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
  1268. && (val & DP_PIPEB_SELECT),
  1269. "IBX PCH dp port still using transcoder B\n");
  1270. }
  1271. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1272. enum pipe pipe, i915_reg_t reg)
  1273. {
  1274. u32 val = I915_READ(reg);
  1275. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1276. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1277. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1278. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
  1279. && (val & SDVO_PIPE_B_SELECT),
  1280. "IBX PCH hdmi port still using transcoder B\n");
  1281. }
  1282. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1283. enum pipe pipe)
  1284. {
  1285. u32 val;
  1286. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1287. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1288. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1289. val = I915_READ(PCH_ADPA);
  1290. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1291. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1292. pipe_name(pipe));
  1293. val = I915_READ(PCH_LVDS);
  1294. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1295. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1296. pipe_name(pipe));
  1297. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1298. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1299. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1300. }
  1301. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1302. const struct intel_crtc_state *pipe_config)
  1303. {
  1304. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1305. enum pipe pipe = crtc->pipe;
  1306. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1307. POSTING_READ(DPLL(pipe));
  1308. udelay(150);
  1309. if (intel_wait_for_register(dev_priv,
  1310. DPLL(pipe),
  1311. DPLL_LOCK_VLV,
  1312. DPLL_LOCK_VLV,
  1313. 1))
  1314. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1315. }
  1316. static void vlv_enable_pll(struct intel_crtc *crtc,
  1317. const struct intel_crtc_state *pipe_config)
  1318. {
  1319. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1320. enum pipe pipe = crtc->pipe;
  1321. assert_pipe_disabled(dev_priv, pipe);
  1322. /* PLL is protected by panel, make sure we can write it */
  1323. assert_panel_unlocked(dev_priv, pipe);
  1324. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1325. _vlv_enable_pll(crtc, pipe_config);
  1326. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1327. POSTING_READ(DPLL_MD(pipe));
  1328. }
  1329. static void _chv_enable_pll(struct intel_crtc *crtc,
  1330. const struct intel_crtc_state *pipe_config)
  1331. {
  1332. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1333. enum pipe pipe = crtc->pipe;
  1334. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1335. u32 tmp;
  1336. mutex_lock(&dev_priv->sb_lock);
  1337. /* Enable back the 10bit clock to display controller */
  1338. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1339. tmp |= DPIO_DCLKP_EN;
  1340. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1341. mutex_unlock(&dev_priv->sb_lock);
  1342. /*
  1343. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1344. */
  1345. udelay(1);
  1346. /* Enable PLL */
  1347. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1348. /* Check PLL is locked */
  1349. if (intel_wait_for_register(dev_priv,
  1350. DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
  1351. 1))
  1352. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1353. }
  1354. static void chv_enable_pll(struct intel_crtc *crtc,
  1355. const struct intel_crtc_state *pipe_config)
  1356. {
  1357. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1358. enum pipe pipe = crtc->pipe;
  1359. assert_pipe_disabled(dev_priv, pipe);
  1360. /* PLL is protected by panel, make sure we can write it */
  1361. assert_panel_unlocked(dev_priv, pipe);
  1362. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1363. _chv_enable_pll(crtc, pipe_config);
  1364. if (pipe != PIPE_A) {
  1365. /*
  1366. * WaPixelRepeatModeFixForC0:chv
  1367. *
  1368. * DPLLCMD is AWOL. Use chicken bits to propagate
  1369. * the value from DPLLBMD to either pipe B or C.
  1370. */
  1371. I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
  1372. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1373. I915_WRITE(CBR4_VLV, 0);
  1374. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1375. /*
  1376. * DPLLB VGA mode also seems to cause problems.
  1377. * We should always have it disabled.
  1378. */
  1379. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1380. } else {
  1381. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1382. POSTING_READ(DPLL_MD(pipe));
  1383. }
  1384. }
  1385. static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
  1386. {
  1387. struct intel_crtc *crtc;
  1388. int count = 0;
  1389. for_each_intel_crtc(&dev_priv->drm, crtc) {
  1390. count += crtc->base.state->active &&
  1391. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
  1392. }
  1393. return count;
  1394. }
  1395. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1396. {
  1397. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1398. i915_reg_t reg = DPLL(crtc->pipe);
  1399. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1400. assert_pipe_disabled(dev_priv, crtc->pipe);
  1401. /* PLL is protected by panel, make sure we can write it */
  1402. if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
  1403. assert_panel_unlocked(dev_priv, crtc->pipe);
  1404. /* Enable DVO 2x clock on both PLLs if necessary */
  1405. if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
  1406. /*
  1407. * It appears to be important that we don't enable this
  1408. * for the current pipe before otherwise configuring the
  1409. * PLL. No idea how this should be handled if multiple
  1410. * DVO outputs are enabled simultaneosly.
  1411. */
  1412. dpll |= DPLL_DVO_2X_MODE;
  1413. I915_WRITE(DPLL(!crtc->pipe),
  1414. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1415. }
  1416. /*
  1417. * Apparently we need to have VGA mode enabled prior to changing
  1418. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1419. * dividers, even though the register value does change.
  1420. */
  1421. I915_WRITE(reg, 0);
  1422. I915_WRITE(reg, dpll);
  1423. /* Wait for the clocks to stabilize. */
  1424. POSTING_READ(reg);
  1425. udelay(150);
  1426. if (INTEL_GEN(dev_priv) >= 4) {
  1427. I915_WRITE(DPLL_MD(crtc->pipe),
  1428. crtc->config->dpll_hw_state.dpll_md);
  1429. } else {
  1430. /* The pixel multiplier can only be updated once the
  1431. * DPLL is enabled and the clocks are stable.
  1432. *
  1433. * So write it again.
  1434. */
  1435. I915_WRITE(reg, dpll);
  1436. }
  1437. /* We do this three times for luck */
  1438. I915_WRITE(reg, dpll);
  1439. POSTING_READ(reg);
  1440. udelay(150); /* wait for warmup */
  1441. I915_WRITE(reg, dpll);
  1442. POSTING_READ(reg);
  1443. udelay(150); /* wait for warmup */
  1444. I915_WRITE(reg, dpll);
  1445. POSTING_READ(reg);
  1446. udelay(150); /* wait for warmup */
  1447. }
  1448. /**
  1449. * i9xx_disable_pll - disable a PLL
  1450. * @dev_priv: i915 private structure
  1451. * @pipe: pipe PLL to disable
  1452. *
  1453. * Disable the PLL for @pipe, making sure the pipe is off first.
  1454. *
  1455. * Note! This is for pre-ILK only.
  1456. */
  1457. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1458. {
  1459. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1460. enum pipe pipe = crtc->pipe;
  1461. /* Disable DVO 2x clock on both PLLs if necessary */
  1462. if (IS_I830(dev_priv) &&
  1463. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
  1464. !intel_num_dvo_pipes(dev_priv)) {
  1465. I915_WRITE(DPLL(PIPE_B),
  1466. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1467. I915_WRITE(DPLL(PIPE_A),
  1468. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1469. }
  1470. /* Don't disable pipe or pipe PLLs if needed */
  1471. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1472. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1473. return;
  1474. /* Make sure the pipe isn't still relying on us */
  1475. assert_pipe_disabled(dev_priv, pipe);
  1476. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1477. POSTING_READ(DPLL(pipe));
  1478. }
  1479. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1480. {
  1481. u32 val;
  1482. /* Make sure the pipe isn't still relying on us */
  1483. assert_pipe_disabled(dev_priv, pipe);
  1484. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1485. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1486. if (pipe != PIPE_A)
  1487. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1488. I915_WRITE(DPLL(pipe), val);
  1489. POSTING_READ(DPLL(pipe));
  1490. }
  1491. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1492. {
  1493. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1494. u32 val;
  1495. /* Make sure the pipe isn't still relying on us */
  1496. assert_pipe_disabled(dev_priv, pipe);
  1497. val = DPLL_SSC_REF_CLK_CHV |
  1498. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1499. if (pipe != PIPE_A)
  1500. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1501. I915_WRITE(DPLL(pipe), val);
  1502. POSTING_READ(DPLL(pipe));
  1503. mutex_lock(&dev_priv->sb_lock);
  1504. /* Disable 10bit clock to display controller */
  1505. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1506. val &= ~DPIO_DCLKP_EN;
  1507. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1508. mutex_unlock(&dev_priv->sb_lock);
  1509. }
  1510. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1511. struct intel_digital_port *dport,
  1512. unsigned int expected_mask)
  1513. {
  1514. u32 port_mask;
  1515. i915_reg_t dpll_reg;
  1516. switch (dport->port) {
  1517. case PORT_B:
  1518. port_mask = DPLL_PORTB_READY_MASK;
  1519. dpll_reg = DPLL(0);
  1520. break;
  1521. case PORT_C:
  1522. port_mask = DPLL_PORTC_READY_MASK;
  1523. dpll_reg = DPLL(0);
  1524. expected_mask <<= 4;
  1525. break;
  1526. case PORT_D:
  1527. port_mask = DPLL_PORTD_READY_MASK;
  1528. dpll_reg = DPIO_PHY_STATUS;
  1529. break;
  1530. default:
  1531. BUG();
  1532. }
  1533. if (intel_wait_for_register(dev_priv,
  1534. dpll_reg, port_mask, expected_mask,
  1535. 1000))
  1536. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1537. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1538. }
  1539. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1540. enum pipe pipe)
  1541. {
  1542. struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
  1543. pipe);
  1544. i915_reg_t reg;
  1545. uint32_t val, pipeconf_val;
  1546. /* Make sure PCH DPLL is enabled */
  1547. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1548. /* FDI must be feeding us bits for PCH ports */
  1549. assert_fdi_tx_enabled(dev_priv, pipe);
  1550. assert_fdi_rx_enabled(dev_priv, pipe);
  1551. if (HAS_PCH_CPT(dev_priv)) {
  1552. /* Workaround: Set the timing override bit before enabling the
  1553. * pch transcoder. */
  1554. reg = TRANS_CHICKEN2(pipe);
  1555. val = I915_READ(reg);
  1556. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1557. I915_WRITE(reg, val);
  1558. }
  1559. reg = PCH_TRANSCONF(pipe);
  1560. val = I915_READ(reg);
  1561. pipeconf_val = I915_READ(PIPECONF(pipe));
  1562. if (HAS_PCH_IBX(dev_priv)) {
  1563. /*
  1564. * Make the BPC in transcoder be consistent with
  1565. * that in pipeconf reg. For HDMI we must use 8bpc
  1566. * here for both 8bpc and 12bpc.
  1567. */
  1568. val &= ~PIPECONF_BPC_MASK;
  1569. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
  1570. val |= PIPECONF_8BPC;
  1571. else
  1572. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1573. }
  1574. val &= ~TRANS_INTERLACE_MASK;
  1575. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1576. if (HAS_PCH_IBX(dev_priv) &&
  1577. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  1578. val |= TRANS_LEGACY_INTERLACED_ILK;
  1579. else
  1580. val |= TRANS_INTERLACED;
  1581. else
  1582. val |= TRANS_PROGRESSIVE;
  1583. I915_WRITE(reg, val | TRANS_ENABLE);
  1584. if (intel_wait_for_register(dev_priv,
  1585. reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
  1586. 100))
  1587. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1588. }
  1589. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1590. enum transcoder cpu_transcoder)
  1591. {
  1592. u32 val, pipeconf_val;
  1593. /* FDI must be feeding us bits for PCH ports */
  1594. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1595. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1596. /* Workaround: set timing override bit. */
  1597. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1598. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1599. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1600. val = TRANS_ENABLE;
  1601. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1602. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1603. PIPECONF_INTERLACED_ILK)
  1604. val |= TRANS_INTERLACED;
  1605. else
  1606. val |= TRANS_PROGRESSIVE;
  1607. I915_WRITE(LPT_TRANSCONF, val);
  1608. if (intel_wait_for_register(dev_priv,
  1609. LPT_TRANSCONF,
  1610. TRANS_STATE_ENABLE,
  1611. TRANS_STATE_ENABLE,
  1612. 100))
  1613. DRM_ERROR("Failed to enable PCH transcoder\n");
  1614. }
  1615. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1616. enum pipe pipe)
  1617. {
  1618. i915_reg_t reg;
  1619. uint32_t val;
  1620. /* FDI relies on the transcoder */
  1621. assert_fdi_tx_disabled(dev_priv, pipe);
  1622. assert_fdi_rx_disabled(dev_priv, pipe);
  1623. /* Ports must be off as well */
  1624. assert_pch_ports_disabled(dev_priv, pipe);
  1625. reg = PCH_TRANSCONF(pipe);
  1626. val = I915_READ(reg);
  1627. val &= ~TRANS_ENABLE;
  1628. I915_WRITE(reg, val);
  1629. /* wait for PCH transcoder off, transcoder state */
  1630. if (intel_wait_for_register(dev_priv,
  1631. reg, TRANS_STATE_ENABLE, 0,
  1632. 50))
  1633. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1634. if (HAS_PCH_CPT(dev_priv)) {
  1635. /* Workaround: Clear the timing override chicken bit again. */
  1636. reg = TRANS_CHICKEN2(pipe);
  1637. val = I915_READ(reg);
  1638. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1639. I915_WRITE(reg, val);
  1640. }
  1641. }
  1642. void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1643. {
  1644. u32 val;
  1645. val = I915_READ(LPT_TRANSCONF);
  1646. val &= ~TRANS_ENABLE;
  1647. I915_WRITE(LPT_TRANSCONF, val);
  1648. /* wait for PCH transcoder off, transcoder state */
  1649. if (intel_wait_for_register(dev_priv,
  1650. LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
  1651. 50))
  1652. DRM_ERROR("Failed to disable PCH transcoder\n");
  1653. /* Workaround: clear timing override bit. */
  1654. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1655. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1656. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1657. }
  1658. enum transcoder intel_crtc_pch_transcoder(struct intel_crtc *crtc)
  1659. {
  1660. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1661. WARN_ON(!crtc->config->has_pch_encoder);
  1662. if (HAS_PCH_LPT(dev_priv))
  1663. return TRANSCODER_A;
  1664. else
  1665. return (enum transcoder) crtc->pipe;
  1666. }
  1667. /**
  1668. * intel_enable_pipe - enable a pipe, asserting requirements
  1669. * @crtc: crtc responsible for the pipe
  1670. *
  1671. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1672. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1673. */
  1674. static void intel_enable_pipe(struct intel_crtc *crtc)
  1675. {
  1676. struct drm_device *dev = crtc->base.dev;
  1677. struct drm_i915_private *dev_priv = to_i915(dev);
  1678. enum pipe pipe = crtc->pipe;
  1679. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1680. i915_reg_t reg;
  1681. u32 val;
  1682. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1683. assert_planes_disabled(dev_priv, pipe);
  1684. assert_cursor_disabled(dev_priv, pipe);
  1685. assert_sprites_disabled(dev_priv, pipe);
  1686. /*
  1687. * A pipe without a PLL won't actually be able to drive bits from
  1688. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1689. * need the check.
  1690. */
  1691. if (HAS_GMCH_DISPLAY(dev_priv)) {
  1692. if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
  1693. assert_dsi_pll_enabled(dev_priv);
  1694. else
  1695. assert_pll_enabled(dev_priv, pipe);
  1696. } else {
  1697. if (crtc->config->has_pch_encoder) {
  1698. /* if driving the PCH, we need FDI enabled */
  1699. assert_fdi_rx_pll_enabled(dev_priv,
  1700. (enum pipe) intel_crtc_pch_transcoder(crtc));
  1701. assert_fdi_tx_pll_enabled(dev_priv,
  1702. (enum pipe) cpu_transcoder);
  1703. }
  1704. /* FIXME: assert CPU port conditions for SNB+ */
  1705. }
  1706. reg = PIPECONF(cpu_transcoder);
  1707. val = I915_READ(reg);
  1708. if (val & PIPECONF_ENABLE) {
  1709. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1710. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1711. return;
  1712. }
  1713. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1714. POSTING_READ(reg);
  1715. /*
  1716. * Until the pipe starts DSL will read as 0, which would cause
  1717. * an apparent vblank timestamp jump, which messes up also the
  1718. * frame count when it's derived from the timestamps. So let's
  1719. * wait for the pipe to start properly before we call
  1720. * drm_crtc_vblank_on()
  1721. */
  1722. if (dev->max_vblank_count == 0 &&
  1723. wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
  1724. DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
  1725. }
  1726. /**
  1727. * intel_disable_pipe - disable a pipe, asserting requirements
  1728. * @crtc: crtc whose pipes is to be disabled
  1729. *
  1730. * Disable the pipe of @crtc, making sure that various hardware
  1731. * specific requirements are met, if applicable, e.g. plane
  1732. * disabled, panel fitter off, etc.
  1733. *
  1734. * Will wait until the pipe has shut down before returning.
  1735. */
  1736. static void intel_disable_pipe(struct intel_crtc *crtc)
  1737. {
  1738. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1739. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1740. enum pipe pipe = crtc->pipe;
  1741. i915_reg_t reg;
  1742. u32 val;
  1743. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1744. /*
  1745. * Make sure planes won't keep trying to pump pixels to us,
  1746. * or we might hang the display.
  1747. */
  1748. assert_planes_disabled(dev_priv, pipe);
  1749. assert_cursor_disabled(dev_priv, pipe);
  1750. assert_sprites_disabled(dev_priv, pipe);
  1751. reg = PIPECONF(cpu_transcoder);
  1752. val = I915_READ(reg);
  1753. if ((val & PIPECONF_ENABLE) == 0)
  1754. return;
  1755. /*
  1756. * Double wide has implications for planes
  1757. * so best keep it disabled when not needed.
  1758. */
  1759. if (crtc->config->double_wide)
  1760. val &= ~PIPECONF_DOUBLE_WIDE;
  1761. /* Don't disable pipe or pipe PLLs if needed */
  1762. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1763. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1764. val &= ~PIPECONF_ENABLE;
  1765. I915_WRITE(reg, val);
  1766. if ((val & PIPECONF_ENABLE) == 0)
  1767. intel_wait_for_pipe_off(crtc);
  1768. }
  1769. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1770. {
  1771. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1772. }
  1773. static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
  1774. uint64_t fb_modifier, unsigned int cpp)
  1775. {
  1776. switch (fb_modifier) {
  1777. case DRM_FORMAT_MOD_NONE:
  1778. return cpp;
  1779. case I915_FORMAT_MOD_X_TILED:
  1780. if (IS_GEN2(dev_priv))
  1781. return 128;
  1782. else
  1783. return 512;
  1784. case I915_FORMAT_MOD_Y_TILED:
  1785. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1786. return 128;
  1787. else
  1788. return 512;
  1789. case I915_FORMAT_MOD_Yf_TILED:
  1790. switch (cpp) {
  1791. case 1:
  1792. return 64;
  1793. case 2:
  1794. case 4:
  1795. return 128;
  1796. case 8:
  1797. case 16:
  1798. return 256;
  1799. default:
  1800. MISSING_CASE(cpp);
  1801. return cpp;
  1802. }
  1803. break;
  1804. default:
  1805. MISSING_CASE(fb_modifier);
  1806. return cpp;
  1807. }
  1808. }
  1809. unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
  1810. uint64_t fb_modifier, unsigned int cpp)
  1811. {
  1812. if (fb_modifier == DRM_FORMAT_MOD_NONE)
  1813. return 1;
  1814. else
  1815. return intel_tile_size(dev_priv) /
  1816. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1817. }
  1818. /* Return the tile dimensions in pixel units */
  1819. static void intel_tile_dims(const struct drm_i915_private *dev_priv,
  1820. unsigned int *tile_width,
  1821. unsigned int *tile_height,
  1822. uint64_t fb_modifier,
  1823. unsigned int cpp)
  1824. {
  1825. unsigned int tile_width_bytes =
  1826. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1827. *tile_width = tile_width_bytes / cpp;
  1828. *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
  1829. }
  1830. unsigned int
  1831. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1832. uint32_t pixel_format, uint64_t fb_modifier)
  1833. {
  1834. unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
  1835. unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
  1836. return ALIGN(height, tile_height);
  1837. }
  1838. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1839. {
  1840. unsigned int size = 0;
  1841. int i;
  1842. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1843. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1844. return size;
  1845. }
  1846. static void
  1847. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1848. const struct drm_framebuffer *fb,
  1849. unsigned int rotation)
  1850. {
  1851. if (drm_rotation_90_or_270(rotation)) {
  1852. *view = i915_ggtt_view_rotated;
  1853. view->params.rotated = to_intel_framebuffer(fb)->rot_info;
  1854. } else {
  1855. *view = i915_ggtt_view_normal;
  1856. }
  1857. }
  1858. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1859. {
  1860. if (INTEL_INFO(dev_priv)->gen >= 9)
  1861. return 256 * 1024;
  1862. else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
  1863. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1864. return 128 * 1024;
  1865. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1866. return 4 * 1024;
  1867. else
  1868. return 0;
  1869. }
  1870. static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
  1871. uint64_t fb_modifier)
  1872. {
  1873. switch (fb_modifier) {
  1874. case DRM_FORMAT_MOD_NONE:
  1875. return intel_linear_alignment(dev_priv);
  1876. case I915_FORMAT_MOD_X_TILED:
  1877. if (INTEL_INFO(dev_priv)->gen >= 9)
  1878. return 256 * 1024;
  1879. return 0;
  1880. case I915_FORMAT_MOD_Y_TILED:
  1881. case I915_FORMAT_MOD_Yf_TILED:
  1882. return 1 * 1024 * 1024;
  1883. default:
  1884. MISSING_CASE(fb_modifier);
  1885. return 0;
  1886. }
  1887. }
  1888. struct i915_vma *
  1889. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1890. {
  1891. struct drm_device *dev = fb->dev;
  1892. struct drm_i915_private *dev_priv = to_i915(dev);
  1893. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1894. struct i915_ggtt_view view;
  1895. struct i915_vma *vma;
  1896. u32 alignment;
  1897. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1898. alignment = intel_surf_alignment(dev_priv, fb->modifier);
  1899. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1900. /* Note that the w/a also requires 64 PTE of padding following the
  1901. * bo. We currently fill all unused PTE with the shadow page and so
  1902. * we should always have valid PTE following the scanout preventing
  1903. * the VT-d warning.
  1904. */
  1905. if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
  1906. alignment = 256 * 1024;
  1907. /*
  1908. * Global gtt pte registers are special registers which actually forward
  1909. * writes to a chunk of system memory. Which means that there is no risk
  1910. * that the register values disappear as soon as we call
  1911. * intel_runtime_pm_put(), so it is correct to wrap only the
  1912. * pin/unpin/fence and not more.
  1913. */
  1914. intel_runtime_pm_get(dev_priv);
  1915. vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
  1916. if (IS_ERR(vma))
  1917. goto err;
  1918. if (i915_vma_is_map_and_fenceable(vma)) {
  1919. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1920. * fence, whereas 965+ only requires a fence if using
  1921. * framebuffer compression. For simplicity, we always, when
  1922. * possible, install a fence as the cost is not that onerous.
  1923. *
  1924. * If we fail to fence the tiled scanout, then either the
  1925. * modeset will reject the change (which is highly unlikely as
  1926. * the affected systems, all but one, do not have unmappable
  1927. * space) or we will not be able to enable full powersaving
  1928. * techniques (also likely not to apply due to various limits
  1929. * FBC and the like impose on the size of the buffer, which
  1930. * presumably we violated anyway with this unmappable buffer).
  1931. * Anyway, it is presumably better to stumble onwards with
  1932. * something and try to run the system in a "less than optimal"
  1933. * mode that matches the user configuration.
  1934. */
  1935. if (i915_vma_get_fence(vma) == 0)
  1936. i915_vma_pin_fence(vma);
  1937. }
  1938. err:
  1939. intel_runtime_pm_put(dev_priv);
  1940. return vma;
  1941. }
  1942. void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1943. {
  1944. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1945. struct i915_ggtt_view view;
  1946. struct i915_vma *vma;
  1947. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  1948. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1949. vma = i915_gem_object_to_ggtt(obj, &view);
  1950. i915_vma_unpin_fence(vma);
  1951. i915_gem_object_unpin_from_display_plane(vma);
  1952. }
  1953. static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
  1954. unsigned int rotation)
  1955. {
  1956. if (drm_rotation_90_or_270(rotation))
  1957. return to_intel_framebuffer(fb)->rotated[plane].pitch;
  1958. else
  1959. return fb->pitches[plane];
  1960. }
  1961. /*
  1962. * Convert the x/y offsets into a linear offset.
  1963. * Only valid with 0/180 degree rotation, which is fine since linear
  1964. * offset is only used with linear buffers on pre-hsw and tiled buffers
  1965. * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
  1966. */
  1967. u32 intel_fb_xy_to_linear(int x, int y,
  1968. const struct intel_plane_state *state,
  1969. int plane)
  1970. {
  1971. const struct drm_framebuffer *fb = state->base.fb;
  1972. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  1973. unsigned int pitch = fb->pitches[plane];
  1974. return y * pitch + x * cpp;
  1975. }
  1976. /*
  1977. * Add the x/y offsets derived from fb->offsets[] to the user
  1978. * specified plane src x/y offsets. The resulting x/y offsets
  1979. * specify the start of scanout from the beginning of the gtt mapping.
  1980. */
  1981. void intel_add_fb_offsets(int *x, int *y,
  1982. const struct intel_plane_state *state,
  1983. int plane)
  1984. {
  1985. const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
  1986. unsigned int rotation = state->base.rotation;
  1987. if (drm_rotation_90_or_270(rotation)) {
  1988. *x += intel_fb->rotated[plane].x;
  1989. *y += intel_fb->rotated[plane].y;
  1990. } else {
  1991. *x += intel_fb->normal[plane].x;
  1992. *y += intel_fb->normal[plane].y;
  1993. }
  1994. }
  1995. /*
  1996. * Input tile dimensions and pitch must already be
  1997. * rotated to match x and y, and in pixel units.
  1998. */
  1999. static u32 _intel_adjust_tile_offset(int *x, int *y,
  2000. unsigned int tile_width,
  2001. unsigned int tile_height,
  2002. unsigned int tile_size,
  2003. unsigned int pitch_tiles,
  2004. u32 old_offset,
  2005. u32 new_offset)
  2006. {
  2007. unsigned int pitch_pixels = pitch_tiles * tile_width;
  2008. unsigned int tiles;
  2009. WARN_ON(old_offset & (tile_size - 1));
  2010. WARN_ON(new_offset & (tile_size - 1));
  2011. WARN_ON(new_offset > old_offset);
  2012. tiles = (old_offset - new_offset) / tile_size;
  2013. *y += tiles / pitch_tiles * tile_height;
  2014. *x += tiles % pitch_tiles * tile_width;
  2015. /* minimize x in case it got needlessly big */
  2016. *y += *x / pitch_pixels * tile_height;
  2017. *x %= pitch_pixels;
  2018. return new_offset;
  2019. }
  2020. /*
  2021. * Adjust the tile offset by moving the difference into
  2022. * the x/y offsets.
  2023. */
  2024. static u32 intel_adjust_tile_offset(int *x, int *y,
  2025. const struct intel_plane_state *state, int plane,
  2026. u32 old_offset, u32 new_offset)
  2027. {
  2028. const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
  2029. const struct drm_framebuffer *fb = state->base.fb;
  2030. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2031. unsigned int rotation = state->base.rotation;
  2032. unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
  2033. WARN_ON(new_offset > old_offset);
  2034. if (fb->modifier != DRM_FORMAT_MOD_NONE) {
  2035. unsigned int tile_size, tile_width, tile_height;
  2036. unsigned int pitch_tiles;
  2037. tile_size = intel_tile_size(dev_priv);
  2038. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2039. fb->modifier, cpp);
  2040. if (drm_rotation_90_or_270(rotation)) {
  2041. pitch_tiles = pitch / tile_height;
  2042. swap(tile_width, tile_height);
  2043. } else {
  2044. pitch_tiles = pitch / (tile_width * cpp);
  2045. }
  2046. _intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2047. tile_size, pitch_tiles,
  2048. old_offset, new_offset);
  2049. } else {
  2050. old_offset += *y * pitch + *x * cpp;
  2051. *y = (old_offset - new_offset) / pitch;
  2052. *x = ((old_offset - new_offset) - *y * pitch) / cpp;
  2053. }
  2054. return new_offset;
  2055. }
  2056. /*
  2057. * Computes the linear offset to the base tile and adjusts
  2058. * x, y. bytes per pixel is assumed to be a power-of-two.
  2059. *
  2060. * In the 90/270 rotated case, x and y are assumed
  2061. * to be already rotated to match the rotated GTT view, and
  2062. * pitch is the tile_height aligned framebuffer height.
  2063. *
  2064. * This function is used when computing the derived information
  2065. * under intel_framebuffer, so using any of that information
  2066. * here is not allowed. Anything under drm_framebuffer can be
  2067. * used. This is why the user has to pass in the pitch since it
  2068. * is specified in the rotated orientation.
  2069. */
  2070. static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
  2071. int *x, int *y,
  2072. const struct drm_framebuffer *fb, int plane,
  2073. unsigned int pitch,
  2074. unsigned int rotation,
  2075. u32 alignment)
  2076. {
  2077. uint64_t fb_modifier = fb->modifier;
  2078. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2079. u32 offset, offset_aligned;
  2080. if (alignment)
  2081. alignment--;
  2082. if (fb_modifier != DRM_FORMAT_MOD_NONE) {
  2083. unsigned int tile_size, tile_width, tile_height;
  2084. unsigned int tile_rows, tiles, pitch_tiles;
  2085. tile_size = intel_tile_size(dev_priv);
  2086. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2087. fb_modifier, cpp);
  2088. if (drm_rotation_90_or_270(rotation)) {
  2089. pitch_tiles = pitch / tile_height;
  2090. swap(tile_width, tile_height);
  2091. } else {
  2092. pitch_tiles = pitch / (tile_width * cpp);
  2093. }
  2094. tile_rows = *y / tile_height;
  2095. *y %= tile_height;
  2096. tiles = *x / tile_width;
  2097. *x %= tile_width;
  2098. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  2099. offset_aligned = offset & ~alignment;
  2100. _intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2101. tile_size, pitch_tiles,
  2102. offset, offset_aligned);
  2103. } else {
  2104. offset = *y * pitch + *x * cpp;
  2105. offset_aligned = offset & ~alignment;
  2106. *y = (offset & alignment) / pitch;
  2107. *x = ((offset & alignment) - *y * pitch) / cpp;
  2108. }
  2109. return offset_aligned;
  2110. }
  2111. u32 intel_compute_tile_offset(int *x, int *y,
  2112. const struct intel_plane_state *state,
  2113. int plane)
  2114. {
  2115. const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
  2116. const struct drm_framebuffer *fb = state->base.fb;
  2117. unsigned int rotation = state->base.rotation;
  2118. int pitch = intel_fb_pitch(fb, plane, rotation);
  2119. u32 alignment;
  2120. /* AUX_DIST needs only 4K alignment */
  2121. if (fb->pixel_format == DRM_FORMAT_NV12 && plane == 1)
  2122. alignment = 4096;
  2123. else
  2124. alignment = intel_surf_alignment(dev_priv, fb->modifier);
  2125. return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
  2126. rotation, alignment);
  2127. }
  2128. /* Convert the fb->offset[] linear offset into x/y offsets */
  2129. static void intel_fb_offset_to_xy(int *x, int *y,
  2130. const struct drm_framebuffer *fb, int plane)
  2131. {
  2132. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2133. unsigned int pitch = fb->pitches[plane];
  2134. u32 linear_offset = fb->offsets[plane];
  2135. *y = linear_offset / pitch;
  2136. *x = linear_offset % pitch / cpp;
  2137. }
  2138. static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
  2139. {
  2140. switch (fb_modifier) {
  2141. case I915_FORMAT_MOD_X_TILED:
  2142. return I915_TILING_X;
  2143. case I915_FORMAT_MOD_Y_TILED:
  2144. return I915_TILING_Y;
  2145. default:
  2146. return I915_TILING_NONE;
  2147. }
  2148. }
  2149. static int
  2150. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  2151. struct drm_framebuffer *fb)
  2152. {
  2153. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  2154. struct intel_rotation_info *rot_info = &intel_fb->rot_info;
  2155. u32 gtt_offset_rotated = 0;
  2156. unsigned int max_size = 0;
  2157. uint32_t format = fb->pixel_format;
  2158. int i, num_planes = drm_format_num_planes(format);
  2159. unsigned int tile_size = intel_tile_size(dev_priv);
  2160. for (i = 0; i < num_planes; i++) {
  2161. unsigned int width, height;
  2162. unsigned int cpp, size;
  2163. u32 offset;
  2164. int x, y;
  2165. cpp = drm_format_plane_cpp(format, i);
  2166. width = drm_format_plane_width(fb->width, format, i);
  2167. height = drm_format_plane_height(fb->height, format, i);
  2168. intel_fb_offset_to_xy(&x, &y, fb, i);
  2169. /*
  2170. * The fence (if used) is aligned to the start of the object
  2171. * so having the framebuffer wrap around across the edge of the
  2172. * fenced region doesn't really work. We have no API to configure
  2173. * the fence start offset within the object (nor could we probably
  2174. * on gen2/3). So it's just easier if we just require that the
  2175. * fb layout agrees with the fence layout. We already check that the
  2176. * fb stride matches the fence stride elsewhere.
  2177. */
  2178. if (i915_gem_object_is_tiled(intel_fb->obj) &&
  2179. (x + width) * cpp > fb->pitches[i]) {
  2180. DRM_DEBUG("bad fb plane %d offset: 0x%x\n",
  2181. i, fb->offsets[i]);
  2182. return -EINVAL;
  2183. }
  2184. /*
  2185. * First pixel of the framebuffer from
  2186. * the start of the normal gtt mapping.
  2187. */
  2188. intel_fb->normal[i].x = x;
  2189. intel_fb->normal[i].y = y;
  2190. offset = _intel_compute_tile_offset(dev_priv, &x, &y,
  2191. fb, 0, fb->pitches[i],
  2192. DRM_ROTATE_0, tile_size);
  2193. offset /= tile_size;
  2194. if (fb->modifier != DRM_FORMAT_MOD_NONE) {
  2195. unsigned int tile_width, tile_height;
  2196. unsigned int pitch_tiles;
  2197. struct drm_rect r;
  2198. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2199. fb->modifier, cpp);
  2200. rot_info->plane[i].offset = offset;
  2201. rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
  2202. rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
  2203. rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
  2204. intel_fb->rotated[i].pitch =
  2205. rot_info->plane[i].height * tile_height;
  2206. /* how many tiles does this plane need */
  2207. size = rot_info->plane[i].stride * rot_info->plane[i].height;
  2208. /*
  2209. * If the plane isn't horizontally tile aligned,
  2210. * we need one more tile.
  2211. */
  2212. if (x != 0)
  2213. size++;
  2214. /* rotate the x/y offsets to match the GTT view */
  2215. r.x1 = x;
  2216. r.y1 = y;
  2217. r.x2 = x + width;
  2218. r.y2 = y + height;
  2219. drm_rect_rotate(&r,
  2220. rot_info->plane[i].width * tile_width,
  2221. rot_info->plane[i].height * tile_height,
  2222. DRM_ROTATE_270);
  2223. x = r.x1;
  2224. y = r.y1;
  2225. /* rotate the tile dimensions to match the GTT view */
  2226. pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
  2227. swap(tile_width, tile_height);
  2228. /*
  2229. * We only keep the x/y offsets, so push all of the
  2230. * gtt offset into the x/y offsets.
  2231. */
  2232. _intel_adjust_tile_offset(&x, &y,
  2233. tile_width, tile_height,
  2234. tile_size, pitch_tiles,
  2235. gtt_offset_rotated * tile_size, 0);
  2236. gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
  2237. /*
  2238. * First pixel of the framebuffer from
  2239. * the start of the rotated gtt mapping.
  2240. */
  2241. intel_fb->rotated[i].x = x;
  2242. intel_fb->rotated[i].y = y;
  2243. } else {
  2244. size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
  2245. x * cpp, tile_size);
  2246. }
  2247. /* how many tiles in total needed in the bo */
  2248. max_size = max(max_size, offset + size);
  2249. }
  2250. if (max_size * tile_size > to_intel_framebuffer(fb)->obj->base.size) {
  2251. DRM_DEBUG("fb too big for bo (need %u bytes, have %zu bytes)\n",
  2252. max_size * tile_size, to_intel_framebuffer(fb)->obj->base.size);
  2253. return -EINVAL;
  2254. }
  2255. return 0;
  2256. }
  2257. static int i9xx_format_to_fourcc(int format)
  2258. {
  2259. switch (format) {
  2260. case DISPPLANE_8BPP:
  2261. return DRM_FORMAT_C8;
  2262. case DISPPLANE_BGRX555:
  2263. return DRM_FORMAT_XRGB1555;
  2264. case DISPPLANE_BGRX565:
  2265. return DRM_FORMAT_RGB565;
  2266. default:
  2267. case DISPPLANE_BGRX888:
  2268. return DRM_FORMAT_XRGB8888;
  2269. case DISPPLANE_RGBX888:
  2270. return DRM_FORMAT_XBGR8888;
  2271. case DISPPLANE_BGRX101010:
  2272. return DRM_FORMAT_XRGB2101010;
  2273. case DISPPLANE_RGBX101010:
  2274. return DRM_FORMAT_XBGR2101010;
  2275. }
  2276. }
  2277. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2278. {
  2279. switch (format) {
  2280. case PLANE_CTL_FORMAT_RGB_565:
  2281. return DRM_FORMAT_RGB565;
  2282. default:
  2283. case PLANE_CTL_FORMAT_XRGB_8888:
  2284. if (rgb_order) {
  2285. if (alpha)
  2286. return DRM_FORMAT_ABGR8888;
  2287. else
  2288. return DRM_FORMAT_XBGR8888;
  2289. } else {
  2290. if (alpha)
  2291. return DRM_FORMAT_ARGB8888;
  2292. else
  2293. return DRM_FORMAT_XRGB8888;
  2294. }
  2295. case PLANE_CTL_FORMAT_XRGB_2101010:
  2296. if (rgb_order)
  2297. return DRM_FORMAT_XBGR2101010;
  2298. else
  2299. return DRM_FORMAT_XRGB2101010;
  2300. }
  2301. }
  2302. static bool
  2303. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2304. struct intel_initial_plane_config *plane_config)
  2305. {
  2306. struct drm_device *dev = crtc->base.dev;
  2307. struct drm_i915_private *dev_priv = to_i915(dev);
  2308. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2309. struct drm_i915_gem_object *obj = NULL;
  2310. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2311. struct drm_framebuffer *fb = &plane_config->fb->base;
  2312. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2313. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2314. PAGE_SIZE);
  2315. size_aligned -= base_aligned;
  2316. if (plane_config->size == 0)
  2317. return false;
  2318. /* If the FB is too big, just don't use it since fbdev is not very
  2319. * important and we should probably use that space with FBC or other
  2320. * features. */
  2321. if (size_aligned * 2 > ggtt->stolen_usable_size)
  2322. return false;
  2323. mutex_lock(&dev->struct_mutex);
  2324. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2325. base_aligned,
  2326. base_aligned,
  2327. size_aligned);
  2328. if (!obj) {
  2329. mutex_unlock(&dev->struct_mutex);
  2330. return false;
  2331. }
  2332. if (plane_config->tiling == I915_TILING_X)
  2333. obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
  2334. mode_cmd.pixel_format = fb->pixel_format;
  2335. mode_cmd.width = fb->width;
  2336. mode_cmd.height = fb->height;
  2337. mode_cmd.pitches[0] = fb->pitches[0];
  2338. mode_cmd.modifier[0] = fb->modifier;
  2339. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2340. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2341. &mode_cmd, obj)) {
  2342. DRM_DEBUG_KMS("intel fb init failed\n");
  2343. goto out_unref_obj;
  2344. }
  2345. mutex_unlock(&dev->struct_mutex);
  2346. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2347. return true;
  2348. out_unref_obj:
  2349. i915_gem_object_put(obj);
  2350. mutex_unlock(&dev->struct_mutex);
  2351. return false;
  2352. }
  2353. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2354. static void
  2355. update_state_fb(struct drm_plane *plane)
  2356. {
  2357. if (plane->fb == plane->state->fb)
  2358. return;
  2359. if (plane->state->fb)
  2360. drm_framebuffer_unreference(plane->state->fb);
  2361. plane->state->fb = plane->fb;
  2362. if (plane->state->fb)
  2363. drm_framebuffer_reference(plane->state->fb);
  2364. }
  2365. static void
  2366. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2367. struct intel_initial_plane_config *plane_config)
  2368. {
  2369. struct drm_device *dev = intel_crtc->base.dev;
  2370. struct drm_i915_private *dev_priv = to_i915(dev);
  2371. struct drm_crtc *c;
  2372. struct intel_crtc *i;
  2373. struct drm_i915_gem_object *obj;
  2374. struct drm_plane *primary = intel_crtc->base.primary;
  2375. struct drm_plane_state *plane_state = primary->state;
  2376. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2377. struct intel_plane *intel_plane = to_intel_plane(primary);
  2378. struct intel_plane_state *intel_state =
  2379. to_intel_plane_state(plane_state);
  2380. struct drm_framebuffer *fb;
  2381. if (!plane_config->fb)
  2382. return;
  2383. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2384. fb = &plane_config->fb->base;
  2385. goto valid_fb;
  2386. }
  2387. kfree(plane_config->fb);
  2388. /*
  2389. * Failed to alloc the obj, check to see if we should share
  2390. * an fb with another CRTC instead
  2391. */
  2392. for_each_crtc(dev, c) {
  2393. i = to_intel_crtc(c);
  2394. if (c == &intel_crtc->base)
  2395. continue;
  2396. if (!i->active)
  2397. continue;
  2398. fb = c->primary->fb;
  2399. if (!fb)
  2400. continue;
  2401. obj = intel_fb_obj(fb);
  2402. if (i915_gem_object_ggtt_offset(obj, NULL) == plane_config->base) {
  2403. drm_framebuffer_reference(fb);
  2404. goto valid_fb;
  2405. }
  2406. }
  2407. /*
  2408. * We've failed to reconstruct the BIOS FB. Current display state
  2409. * indicates that the primary plane is visible, but has a NULL FB,
  2410. * which will lead to problems later if we don't fix it up. The
  2411. * simplest solution is to just disable the primary plane now and
  2412. * pretend the BIOS never had it enabled.
  2413. */
  2414. to_intel_plane_state(plane_state)->base.visible = false;
  2415. crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
  2416. intel_pre_disable_primary_noatomic(&intel_crtc->base);
  2417. intel_plane->disable_plane(primary, &intel_crtc->base);
  2418. return;
  2419. valid_fb:
  2420. plane_state->src_x = 0;
  2421. plane_state->src_y = 0;
  2422. plane_state->src_w = fb->width << 16;
  2423. plane_state->src_h = fb->height << 16;
  2424. plane_state->crtc_x = 0;
  2425. plane_state->crtc_y = 0;
  2426. plane_state->crtc_w = fb->width;
  2427. plane_state->crtc_h = fb->height;
  2428. intel_state->base.src = drm_plane_state_src(plane_state);
  2429. intel_state->base.dst = drm_plane_state_dest(plane_state);
  2430. obj = intel_fb_obj(fb);
  2431. if (i915_gem_object_is_tiled(obj))
  2432. dev_priv->preserve_bios_swizzle = true;
  2433. drm_framebuffer_reference(fb);
  2434. primary->fb = primary->state->fb = fb;
  2435. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2436. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2437. atomic_or(to_intel_plane(primary)->frontbuffer_bit,
  2438. &obj->frontbuffer_bits);
  2439. }
  2440. static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
  2441. unsigned int rotation)
  2442. {
  2443. int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2444. switch (fb->modifier) {
  2445. case DRM_FORMAT_MOD_NONE:
  2446. case I915_FORMAT_MOD_X_TILED:
  2447. switch (cpp) {
  2448. case 8:
  2449. return 4096;
  2450. case 4:
  2451. case 2:
  2452. case 1:
  2453. return 8192;
  2454. default:
  2455. MISSING_CASE(cpp);
  2456. break;
  2457. }
  2458. break;
  2459. case I915_FORMAT_MOD_Y_TILED:
  2460. case I915_FORMAT_MOD_Yf_TILED:
  2461. switch (cpp) {
  2462. case 8:
  2463. return 2048;
  2464. case 4:
  2465. return 4096;
  2466. case 2:
  2467. case 1:
  2468. return 8192;
  2469. default:
  2470. MISSING_CASE(cpp);
  2471. break;
  2472. }
  2473. break;
  2474. default:
  2475. MISSING_CASE(fb->modifier);
  2476. }
  2477. return 2048;
  2478. }
  2479. static int skl_check_main_surface(struct intel_plane_state *plane_state)
  2480. {
  2481. const struct drm_i915_private *dev_priv = to_i915(plane_state->base.plane->dev);
  2482. const struct drm_framebuffer *fb = plane_state->base.fb;
  2483. unsigned int rotation = plane_state->base.rotation;
  2484. int x = plane_state->base.src.x1 >> 16;
  2485. int y = plane_state->base.src.y1 >> 16;
  2486. int w = drm_rect_width(&plane_state->base.src) >> 16;
  2487. int h = drm_rect_height(&plane_state->base.src) >> 16;
  2488. int max_width = skl_max_plane_width(fb, 0, rotation);
  2489. int max_height = 4096;
  2490. u32 alignment, offset, aux_offset = plane_state->aux.offset;
  2491. if (w > max_width || h > max_height) {
  2492. DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
  2493. w, h, max_width, max_height);
  2494. return -EINVAL;
  2495. }
  2496. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2497. offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
  2498. alignment = intel_surf_alignment(dev_priv, fb->modifier);
  2499. /*
  2500. * AUX surface offset is specified as the distance from the
  2501. * main surface offset, and it must be non-negative. Make
  2502. * sure that is what we will get.
  2503. */
  2504. if (offset > aux_offset)
  2505. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2506. offset, aux_offset & ~(alignment - 1));
  2507. /*
  2508. * When using an X-tiled surface, the plane blows up
  2509. * if the x offset + width exceed the stride.
  2510. *
  2511. * TODO: linear and Y-tiled seem fine, Yf untested,
  2512. */
  2513. if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
  2514. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2515. while ((x + w) * cpp > fb->pitches[0]) {
  2516. if (offset == 0) {
  2517. DRM_DEBUG_KMS("Unable to find suitable display surface offset\n");
  2518. return -EINVAL;
  2519. }
  2520. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2521. offset, offset - alignment);
  2522. }
  2523. }
  2524. plane_state->main.offset = offset;
  2525. plane_state->main.x = x;
  2526. plane_state->main.y = y;
  2527. return 0;
  2528. }
  2529. static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
  2530. {
  2531. const struct drm_framebuffer *fb = plane_state->base.fb;
  2532. unsigned int rotation = plane_state->base.rotation;
  2533. int max_width = skl_max_plane_width(fb, 1, rotation);
  2534. int max_height = 4096;
  2535. int x = plane_state->base.src.x1 >> 17;
  2536. int y = plane_state->base.src.y1 >> 17;
  2537. int w = drm_rect_width(&plane_state->base.src) >> 17;
  2538. int h = drm_rect_height(&plane_state->base.src) >> 17;
  2539. u32 offset;
  2540. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2541. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2542. /* FIXME not quite sure how/if these apply to the chroma plane */
  2543. if (w > max_width || h > max_height) {
  2544. DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
  2545. w, h, max_width, max_height);
  2546. return -EINVAL;
  2547. }
  2548. plane_state->aux.offset = offset;
  2549. plane_state->aux.x = x;
  2550. plane_state->aux.y = y;
  2551. return 0;
  2552. }
  2553. int skl_check_plane_surface(struct intel_plane_state *plane_state)
  2554. {
  2555. const struct drm_framebuffer *fb = plane_state->base.fb;
  2556. unsigned int rotation = plane_state->base.rotation;
  2557. int ret;
  2558. if (!plane_state->base.visible)
  2559. return 0;
  2560. /* Rotate src coordinates to match rotated GTT view */
  2561. if (drm_rotation_90_or_270(rotation))
  2562. drm_rect_rotate(&plane_state->base.src,
  2563. fb->width << 16, fb->height << 16,
  2564. DRM_ROTATE_270);
  2565. /*
  2566. * Handle the AUX surface first since
  2567. * the main surface setup depends on it.
  2568. */
  2569. if (fb->pixel_format == DRM_FORMAT_NV12) {
  2570. ret = skl_check_nv12_aux_surface(plane_state);
  2571. if (ret)
  2572. return ret;
  2573. } else {
  2574. plane_state->aux.offset = ~0xfff;
  2575. plane_state->aux.x = 0;
  2576. plane_state->aux.y = 0;
  2577. }
  2578. ret = skl_check_main_surface(plane_state);
  2579. if (ret)
  2580. return ret;
  2581. return 0;
  2582. }
  2583. static void i9xx_update_primary_plane(struct drm_plane *primary,
  2584. const struct intel_crtc_state *crtc_state,
  2585. const struct intel_plane_state *plane_state)
  2586. {
  2587. struct drm_i915_private *dev_priv = to_i915(primary->dev);
  2588. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2589. struct drm_framebuffer *fb = plane_state->base.fb;
  2590. int plane = intel_crtc->plane;
  2591. u32 linear_offset;
  2592. u32 dspcntr;
  2593. i915_reg_t reg = DSPCNTR(plane);
  2594. unsigned int rotation = plane_state->base.rotation;
  2595. int x = plane_state->base.src.x1 >> 16;
  2596. int y = plane_state->base.src.y1 >> 16;
  2597. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2598. dspcntr |= DISPLAY_PLANE_ENABLE;
  2599. if (INTEL_GEN(dev_priv) < 4) {
  2600. if (intel_crtc->pipe == PIPE_B)
  2601. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2602. /* pipesrc and dspsize control the size that is scaled from,
  2603. * which should always be the user's requested size.
  2604. */
  2605. I915_WRITE(DSPSIZE(plane),
  2606. ((crtc_state->pipe_src_h - 1) << 16) |
  2607. (crtc_state->pipe_src_w - 1));
  2608. I915_WRITE(DSPPOS(plane), 0);
  2609. } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
  2610. I915_WRITE(PRIMSIZE(plane),
  2611. ((crtc_state->pipe_src_h - 1) << 16) |
  2612. (crtc_state->pipe_src_w - 1));
  2613. I915_WRITE(PRIMPOS(plane), 0);
  2614. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2615. }
  2616. switch (fb->pixel_format) {
  2617. case DRM_FORMAT_C8:
  2618. dspcntr |= DISPPLANE_8BPP;
  2619. break;
  2620. case DRM_FORMAT_XRGB1555:
  2621. dspcntr |= DISPPLANE_BGRX555;
  2622. break;
  2623. case DRM_FORMAT_RGB565:
  2624. dspcntr |= DISPPLANE_BGRX565;
  2625. break;
  2626. case DRM_FORMAT_XRGB8888:
  2627. dspcntr |= DISPPLANE_BGRX888;
  2628. break;
  2629. case DRM_FORMAT_XBGR8888:
  2630. dspcntr |= DISPPLANE_RGBX888;
  2631. break;
  2632. case DRM_FORMAT_XRGB2101010:
  2633. dspcntr |= DISPPLANE_BGRX101010;
  2634. break;
  2635. case DRM_FORMAT_XBGR2101010:
  2636. dspcntr |= DISPPLANE_RGBX101010;
  2637. break;
  2638. default:
  2639. BUG();
  2640. }
  2641. if (INTEL_GEN(dev_priv) >= 4 &&
  2642. fb->modifier == I915_FORMAT_MOD_X_TILED)
  2643. dspcntr |= DISPPLANE_TILED;
  2644. if (rotation & DRM_ROTATE_180)
  2645. dspcntr |= DISPPLANE_ROTATE_180;
  2646. if (rotation & DRM_REFLECT_X)
  2647. dspcntr |= DISPPLANE_MIRROR;
  2648. if (IS_G4X(dev_priv))
  2649. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2650. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2651. if (INTEL_GEN(dev_priv) >= 4)
  2652. intel_crtc->dspaddr_offset =
  2653. intel_compute_tile_offset(&x, &y, plane_state, 0);
  2654. if (rotation & DRM_ROTATE_180) {
  2655. x += crtc_state->pipe_src_w - 1;
  2656. y += crtc_state->pipe_src_h - 1;
  2657. } else if (rotation & DRM_REFLECT_X) {
  2658. x += crtc_state->pipe_src_w - 1;
  2659. }
  2660. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2661. if (INTEL_GEN(dev_priv) < 4)
  2662. intel_crtc->dspaddr_offset = linear_offset;
  2663. intel_crtc->adjusted_x = x;
  2664. intel_crtc->adjusted_y = y;
  2665. I915_WRITE(reg, dspcntr);
  2666. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2667. if (INTEL_GEN(dev_priv) >= 4) {
  2668. I915_WRITE(DSPSURF(plane),
  2669. intel_fb_gtt_offset(fb, rotation) +
  2670. intel_crtc->dspaddr_offset);
  2671. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2672. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2673. } else {
  2674. I915_WRITE(DSPADDR(plane),
  2675. intel_fb_gtt_offset(fb, rotation) +
  2676. intel_crtc->dspaddr_offset);
  2677. }
  2678. POSTING_READ(reg);
  2679. }
  2680. static void i9xx_disable_primary_plane(struct drm_plane *primary,
  2681. struct drm_crtc *crtc)
  2682. {
  2683. struct drm_device *dev = crtc->dev;
  2684. struct drm_i915_private *dev_priv = to_i915(dev);
  2685. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2686. int plane = intel_crtc->plane;
  2687. I915_WRITE(DSPCNTR(plane), 0);
  2688. if (INTEL_INFO(dev_priv)->gen >= 4)
  2689. I915_WRITE(DSPSURF(plane), 0);
  2690. else
  2691. I915_WRITE(DSPADDR(plane), 0);
  2692. POSTING_READ(DSPCNTR(plane));
  2693. }
  2694. static void ironlake_update_primary_plane(struct drm_plane *primary,
  2695. const struct intel_crtc_state *crtc_state,
  2696. const struct intel_plane_state *plane_state)
  2697. {
  2698. struct drm_device *dev = primary->dev;
  2699. struct drm_i915_private *dev_priv = to_i915(dev);
  2700. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2701. struct drm_framebuffer *fb = plane_state->base.fb;
  2702. int plane = intel_crtc->plane;
  2703. u32 linear_offset;
  2704. u32 dspcntr;
  2705. i915_reg_t reg = DSPCNTR(plane);
  2706. unsigned int rotation = plane_state->base.rotation;
  2707. int x = plane_state->base.src.x1 >> 16;
  2708. int y = plane_state->base.src.y1 >> 16;
  2709. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2710. dspcntr |= DISPLAY_PLANE_ENABLE;
  2711. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  2712. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2713. switch (fb->pixel_format) {
  2714. case DRM_FORMAT_C8:
  2715. dspcntr |= DISPPLANE_8BPP;
  2716. break;
  2717. case DRM_FORMAT_RGB565:
  2718. dspcntr |= DISPPLANE_BGRX565;
  2719. break;
  2720. case DRM_FORMAT_XRGB8888:
  2721. dspcntr |= DISPPLANE_BGRX888;
  2722. break;
  2723. case DRM_FORMAT_XBGR8888:
  2724. dspcntr |= DISPPLANE_RGBX888;
  2725. break;
  2726. case DRM_FORMAT_XRGB2101010:
  2727. dspcntr |= DISPPLANE_BGRX101010;
  2728. break;
  2729. case DRM_FORMAT_XBGR2101010:
  2730. dspcntr |= DISPPLANE_RGBX101010;
  2731. break;
  2732. default:
  2733. BUG();
  2734. }
  2735. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  2736. dspcntr |= DISPPLANE_TILED;
  2737. if (rotation & DRM_ROTATE_180)
  2738. dspcntr |= DISPPLANE_ROTATE_180;
  2739. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv))
  2740. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2741. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2742. intel_crtc->dspaddr_offset =
  2743. intel_compute_tile_offset(&x, &y, plane_state, 0);
  2744. /* HSW+ does this automagically in hardware */
  2745. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv) &&
  2746. rotation & DRM_ROTATE_180) {
  2747. x += crtc_state->pipe_src_w - 1;
  2748. y += crtc_state->pipe_src_h - 1;
  2749. }
  2750. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2751. intel_crtc->adjusted_x = x;
  2752. intel_crtc->adjusted_y = y;
  2753. I915_WRITE(reg, dspcntr);
  2754. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2755. I915_WRITE(DSPSURF(plane),
  2756. intel_fb_gtt_offset(fb, rotation) +
  2757. intel_crtc->dspaddr_offset);
  2758. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  2759. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2760. } else {
  2761. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2762. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2763. }
  2764. POSTING_READ(reg);
  2765. }
  2766. u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
  2767. uint64_t fb_modifier, uint32_t pixel_format)
  2768. {
  2769. if (fb_modifier == DRM_FORMAT_MOD_NONE) {
  2770. return 64;
  2771. } else {
  2772. int cpp = drm_format_plane_cpp(pixel_format, 0);
  2773. return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  2774. }
  2775. }
  2776. u32 intel_fb_gtt_offset(struct drm_framebuffer *fb,
  2777. unsigned int rotation)
  2778. {
  2779. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2780. struct i915_ggtt_view view;
  2781. struct i915_vma *vma;
  2782. intel_fill_fb_ggtt_view(&view, fb, rotation);
  2783. vma = i915_gem_object_to_ggtt(obj, &view);
  2784. if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
  2785. view.type))
  2786. return -1;
  2787. return i915_ggtt_offset(vma);
  2788. }
  2789. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2790. {
  2791. struct drm_device *dev = intel_crtc->base.dev;
  2792. struct drm_i915_private *dev_priv = to_i915(dev);
  2793. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2794. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2795. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2796. }
  2797. /*
  2798. * This function detaches (aka. unbinds) unused scalers in hardware
  2799. */
  2800. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2801. {
  2802. struct intel_crtc_scaler_state *scaler_state;
  2803. int i;
  2804. scaler_state = &intel_crtc->config->scaler_state;
  2805. /* loop through and disable scalers that aren't in use */
  2806. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2807. if (!scaler_state->scalers[i].in_use)
  2808. skl_detach_scaler(intel_crtc, i);
  2809. }
  2810. }
  2811. u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
  2812. unsigned int rotation)
  2813. {
  2814. const struct drm_i915_private *dev_priv = to_i915(fb->dev);
  2815. u32 stride = intel_fb_pitch(fb, plane, rotation);
  2816. /*
  2817. * The stride is either expressed as a multiple of 64 bytes chunks for
  2818. * linear buffers or in number of tiles for tiled buffers.
  2819. */
  2820. if (drm_rotation_90_or_270(rotation)) {
  2821. int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2822. stride /= intel_tile_height(dev_priv, fb->modifier, cpp);
  2823. } else {
  2824. stride /= intel_fb_stride_alignment(dev_priv, fb->modifier,
  2825. fb->pixel_format);
  2826. }
  2827. return stride;
  2828. }
  2829. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2830. {
  2831. switch (pixel_format) {
  2832. case DRM_FORMAT_C8:
  2833. return PLANE_CTL_FORMAT_INDEXED;
  2834. case DRM_FORMAT_RGB565:
  2835. return PLANE_CTL_FORMAT_RGB_565;
  2836. case DRM_FORMAT_XBGR8888:
  2837. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2838. case DRM_FORMAT_XRGB8888:
  2839. return PLANE_CTL_FORMAT_XRGB_8888;
  2840. /*
  2841. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2842. * to be already pre-multiplied. We need to add a knob (or a different
  2843. * DRM_FORMAT) for user-space to configure that.
  2844. */
  2845. case DRM_FORMAT_ABGR8888:
  2846. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2847. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2848. case DRM_FORMAT_ARGB8888:
  2849. return PLANE_CTL_FORMAT_XRGB_8888 |
  2850. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2851. case DRM_FORMAT_XRGB2101010:
  2852. return PLANE_CTL_FORMAT_XRGB_2101010;
  2853. case DRM_FORMAT_XBGR2101010:
  2854. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2855. case DRM_FORMAT_YUYV:
  2856. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2857. case DRM_FORMAT_YVYU:
  2858. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2859. case DRM_FORMAT_UYVY:
  2860. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2861. case DRM_FORMAT_VYUY:
  2862. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2863. default:
  2864. MISSING_CASE(pixel_format);
  2865. }
  2866. return 0;
  2867. }
  2868. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2869. {
  2870. switch (fb_modifier) {
  2871. case DRM_FORMAT_MOD_NONE:
  2872. break;
  2873. case I915_FORMAT_MOD_X_TILED:
  2874. return PLANE_CTL_TILED_X;
  2875. case I915_FORMAT_MOD_Y_TILED:
  2876. return PLANE_CTL_TILED_Y;
  2877. case I915_FORMAT_MOD_Yf_TILED:
  2878. return PLANE_CTL_TILED_YF;
  2879. default:
  2880. MISSING_CASE(fb_modifier);
  2881. }
  2882. return 0;
  2883. }
  2884. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2885. {
  2886. switch (rotation) {
  2887. case DRM_ROTATE_0:
  2888. break;
  2889. /*
  2890. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2891. * while i915 HW rotation is clockwise, thats why this swapping.
  2892. */
  2893. case DRM_ROTATE_90:
  2894. return PLANE_CTL_ROTATE_270;
  2895. case DRM_ROTATE_180:
  2896. return PLANE_CTL_ROTATE_180;
  2897. case DRM_ROTATE_270:
  2898. return PLANE_CTL_ROTATE_90;
  2899. default:
  2900. MISSING_CASE(rotation);
  2901. }
  2902. return 0;
  2903. }
  2904. static void skylake_update_primary_plane(struct drm_plane *plane,
  2905. const struct intel_crtc_state *crtc_state,
  2906. const struct intel_plane_state *plane_state)
  2907. {
  2908. struct drm_device *dev = plane->dev;
  2909. struct drm_i915_private *dev_priv = to_i915(dev);
  2910. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2911. struct drm_framebuffer *fb = plane_state->base.fb;
  2912. int pipe = intel_crtc->pipe;
  2913. u32 plane_ctl;
  2914. unsigned int rotation = plane_state->base.rotation;
  2915. u32 stride = skl_plane_stride(fb, 0, rotation);
  2916. u32 surf_addr = plane_state->main.offset;
  2917. int scaler_id = plane_state->scaler_id;
  2918. int src_x = plane_state->main.x;
  2919. int src_y = plane_state->main.y;
  2920. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  2921. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  2922. int dst_x = plane_state->base.dst.x1;
  2923. int dst_y = plane_state->base.dst.y1;
  2924. int dst_w = drm_rect_width(&plane_state->base.dst);
  2925. int dst_h = drm_rect_height(&plane_state->base.dst);
  2926. plane_ctl = PLANE_CTL_ENABLE |
  2927. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2928. PLANE_CTL_PIPE_CSC_ENABLE;
  2929. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2930. plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
  2931. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2932. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2933. /* Sizes are 0 based */
  2934. src_w--;
  2935. src_h--;
  2936. dst_w--;
  2937. dst_h--;
  2938. intel_crtc->dspaddr_offset = surf_addr;
  2939. intel_crtc->adjusted_x = src_x;
  2940. intel_crtc->adjusted_y = src_y;
  2941. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2942. I915_WRITE(PLANE_OFFSET(pipe, 0), (src_y << 16) | src_x);
  2943. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2944. I915_WRITE(PLANE_SIZE(pipe, 0), (src_h << 16) | src_w);
  2945. if (scaler_id >= 0) {
  2946. uint32_t ps_ctrl = 0;
  2947. WARN_ON(!dst_w || !dst_h);
  2948. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2949. crtc_state->scaler_state.scalers[scaler_id].mode;
  2950. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2951. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2952. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2953. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2954. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2955. } else {
  2956. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2957. }
  2958. I915_WRITE(PLANE_SURF(pipe, 0),
  2959. intel_fb_gtt_offset(fb, rotation) + surf_addr);
  2960. POSTING_READ(PLANE_SURF(pipe, 0));
  2961. }
  2962. static void skylake_disable_primary_plane(struct drm_plane *primary,
  2963. struct drm_crtc *crtc)
  2964. {
  2965. struct drm_device *dev = crtc->dev;
  2966. struct drm_i915_private *dev_priv = to_i915(dev);
  2967. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2968. int pipe = intel_crtc->pipe;
  2969. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2970. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2971. POSTING_READ(PLANE_SURF(pipe, 0));
  2972. }
  2973. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2974. static int
  2975. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2976. int x, int y, enum mode_set_atomic state)
  2977. {
  2978. /* Support for kgdboc is disabled, this needs a major rework. */
  2979. DRM_ERROR("legacy panic handler not supported any more.\n");
  2980. return -ENODEV;
  2981. }
  2982. static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
  2983. {
  2984. struct intel_crtc *crtc;
  2985. for_each_intel_crtc(&dev_priv->drm, crtc)
  2986. intel_finish_page_flip_cs(dev_priv, crtc->pipe);
  2987. }
  2988. static void intel_update_primary_planes(struct drm_device *dev)
  2989. {
  2990. struct drm_crtc *crtc;
  2991. for_each_crtc(dev, crtc) {
  2992. struct intel_plane *plane = to_intel_plane(crtc->primary);
  2993. struct intel_plane_state *plane_state =
  2994. to_intel_plane_state(plane->base.state);
  2995. if (plane_state->base.visible)
  2996. plane->update_plane(&plane->base,
  2997. to_intel_crtc_state(crtc->state),
  2998. plane_state);
  2999. }
  3000. }
  3001. static int
  3002. __intel_display_resume(struct drm_device *dev,
  3003. struct drm_atomic_state *state)
  3004. {
  3005. struct drm_crtc_state *crtc_state;
  3006. struct drm_crtc *crtc;
  3007. int i, ret;
  3008. intel_modeset_setup_hw_state(dev);
  3009. i915_redisable_vga(to_i915(dev));
  3010. if (!state)
  3011. return 0;
  3012. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  3013. /*
  3014. * Force recalculation even if we restore
  3015. * current state. With fast modeset this may not result
  3016. * in a modeset when the state is compatible.
  3017. */
  3018. crtc_state->mode_changed = true;
  3019. }
  3020. /* ignore any reset values/BIOS leftovers in the WM registers */
  3021. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  3022. ret = drm_atomic_commit(state);
  3023. WARN_ON(ret == -EDEADLK);
  3024. return ret;
  3025. }
  3026. static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
  3027. {
  3028. return intel_has_gpu_reset(dev_priv) &&
  3029. INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
  3030. }
  3031. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  3032. {
  3033. struct drm_device *dev = &dev_priv->drm;
  3034. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3035. struct drm_atomic_state *state;
  3036. int ret;
  3037. /*
  3038. * Need mode_config.mutex so that we don't
  3039. * trample ongoing ->detect() and whatnot.
  3040. */
  3041. mutex_lock(&dev->mode_config.mutex);
  3042. drm_modeset_acquire_init(ctx, 0);
  3043. while (1) {
  3044. ret = drm_modeset_lock_all_ctx(dev, ctx);
  3045. if (ret != -EDEADLK)
  3046. break;
  3047. drm_modeset_backoff(ctx);
  3048. }
  3049. /* reset doesn't touch the display, but flips might get nuked anyway, */
  3050. if (!i915.force_reset_modeset_test &&
  3051. !gpu_reset_clobbers_display(dev_priv))
  3052. return;
  3053. /*
  3054. * Disabling the crtcs gracefully seems nicer. Also the
  3055. * g33 docs say we should at least disable all the planes.
  3056. */
  3057. state = drm_atomic_helper_duplicate_state(dev, ctx);
  3058. if (IS_ERR(state)) {
  3059. ret = PTR_ERR(state);
  3060. state = NULL;
  3061. DRM_ERROR("Duplicating state failed with %i\n", ret);
  3062. goto err;
  3063. }
  3064. ret = drm_atomic_helper_disable_all(dev, ctx);
  3065. if (ret) {
  3066. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  3067. goto err;
  3068. }
  3069. dev_priv->modeset_restore_state = state;
  3070. state->acquire_ctx = ctx;
  3071. return;
  3072. err:
  3073. drm_atomic_state_put(state);
  3074. }
  3075. void intel_finish_reset(struct drm_i915_private *dev_priv)
  3076. {
  3077. struct drm_device *dev = &dev_priv->drm;
  3078. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3079. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  3080. int ret;
  3081. /*
  3082. * Flips in the rings will be nuked by the reset,
  3083. * so complete all pending flips so that user space
  3084. * will get its events and not get stuck.
  3085. */
  3086. intel_complete_page_flips(dev_priv);
  3087. dev_priv->modeset_restore_state = NULL;
  3088. /* reset doesn't touch the display */
  3089. if (!gpu_reset_clobbers_display(dev_priv)) {
  3090. if (!state) {
  3091. /*
  3092. * Flips in the rings have been nuked by the reset,
  3093. * so update the base address of all primary
  3094. * planes to the the last fb to make sure we're
  3095. * showing the correct fb after a reset.
  3096. *
  3097. * FIXME: Atomic will make this obsolete since we won't schedule
  3098. * CS-based flips (which might get lost in gpu resets) any more.
  3099. */
  3100. intel_update_primary_planes(dev);
  3101. } else {
  3102. ret = __intel_display_resume(dev, state);
  3103. if (ret)
  3104. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3105. }
  3106. } else {
  3107. /*
  3108. * The display has been reset as well,
  3109. * so need a full re-initialization.
  3110. */
  3111. intel_runtime_pm_disable_interrupts(dev_priv);
  3112. intel_runtime_pm_enable_interrupts(dev_priv);
  3113. intel_pps_unlock_regs_wa(dev_priv);
  3114. intel_modeset_init_hw(dev);
  3115. spin_lock_irq(&dev_priv->irq_lock);
  3116. if (dev_priv->display.hpd_irq_setup)
  3117. dev_priv->display.hpd_irq_setup(dev_priv);
  3118. spin_unlock_irq(&dev_priv->irq_lock);
  3119. ret = __intel_display_resume(dev, state);
  3120. if (ret)
  3121. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3122. intel_hpd_init(dev_priv);
  3123. }
  3124. if (state)
  3125. drm_atomic_state_put(state);
  3126. drm_modeset_drop_locks(ctx);
  3127. drm_modeset_acquire_fini(ctx);
  3128. mutex_unlock(&dev->mode_config.mutex);
  3129. }
  3130. static bool abort_flip_on_reset(struct intel_crtc *crtc)
  3131. {
  3132. struct i915_gpu_error *error = &to_i915(crtc->base.dev)->gpu_error;
  3133. if (i915_reset_in_progress(error))
  3134. return true;
  3135. if (crtc->reset_count != i915_reset_count(error))
  3136. return true;
  3137. return false;
  3138. }
  3139. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  3140. {
  3141. struct drm_device *dev = crtc->dev;
  3142. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3143. bool pending;
  3144. if (abort_flip_on_reset(intel_crtc))
  3145. return false;
  3146. spin_lock_irq(&dev->event_lock);
  3147. pending = to_intel_crtc(crtc)->flip_work != NULL;
  3148. spin_unlock_irq(&dev->event_lock);
  3149. return pending;
  3150. }
  3151. static void intel_update_pipe_config(struct intel_crtc *crtc,
  3152. struct intel_crtc_state *old_crtc_state)
  3153. {
  3154. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3155. struct intel_crtc_state *pipe_config =
  3156. to_intel_crtc_state(crtc->base.state);
  3157. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  3158. crtc->base.mode = crtc->base.state->mode;
  3159. DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
  3160. old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
  3161. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  3162. /*
  3163. * Update pipe size and adjust fitter if needed: the reason for this is
  3164. * that in compute_mode_changes we check the native mode (not the pfit
  3165. * mode) to see if we can flip rather than do a full mode set. In the
  3166. * fastboot case, we'll flip, but if we don't update the pipesrc and
  3167. * pfit state, we'll end up with a big fb scanned out into the wrong
  3168. * sized surface.
  3169. */
  3170. I915_WRITE(PIPESRC(crtc->pipe),
  3171. ((pipe_config->pipe_src_w - 1) << 16) |
  3172. (pipe_config->pipe_src_h - 1));
  3173. /* on skylake this is done by detaching scalers */
  3174. if (INTEL_GEN(dev_priv) >= 9) {
  3175. skl_detach_scalers(crtc);
  3176. if (pipe_config->pch_pfit.enabled)
  3177. skylake_pfit_enable(crtc);
  3178. } else if (HAS_PCH_SPLIT(dev_priv)) {
  3179. if (pipe_config->pch_pfit.enabled)
  3180. ironlake_pfit_enable(crtc);
  3181. else if (old_crtc_state->pch_pfit.enabled)
  3182. ironlake_pfit_disable(crtc, true);
  3183. }
  3184. }
  3185. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  3186. {
  3187. struct drm_device *dev = crtc->dev;
  3188. struct drm_i915_private *dev_priv = to_i915(dev);
  3189. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3190. int pipe = intel_crtc->pipe;
  3191. i915_reg_t reg;
  3192. u32 temp;
  3193. /* enable normal train */
  3194. reg = FDI_TX_CTL(pipe);
  3195. temp = I915_READ(reg);
  3196. if (IS_IVYBRIDGE(dev_priv)) {
  3197. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3198. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  3199. } else {
  3200. temp &= ~FDI_LINK_TRAIN_NONE;
  3201. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  3202. }
  3203. I915_WRITE(reg, temp);
  3204. reg = FDI_RX_CTL(pipe);
  3205. temp = I915_READ(reg);
  3206. if (HAS_PCH_CPT(dev_priv)) {
  3207. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3208. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  3209. } else {
  3210. temp &= ~FDI_LINK_TRAIN_NONE;
  3211. temp |= FDI_LINK_TRAIN_NONE;
  3212. }
  3213. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  3214. /* wait one idle pattern time */
  3215. POSTING_READ(reg);
  3216. udelay(1000);
  3217. /* IVB wants error correction enabled */
  3218. if (IS_IVYBRIDGE(dev_priv))
  3219. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  3220. FDI_FE_ERRC_ENABLE);
  3221. }
  3222. /* The FDI link training functions for ILK/Ibexpeak. */
  3223. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  3224. {
  3225. struct drm_device *dev = crtc->dev;
  3226. struct drm_i915_private *dev_priv = to_i915(dev);
  3227. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3228. int pipe = intel_crtc->pipe;
  3229. i915_reg_t reg;
  3230. u32 temp, tries;
  3231. /* FDI needs bits from pipe first */
  3232. assert_pipe_enabled(dev_priv, pipe);
  3233. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3234. for train result */
  3235. reg = FDI_RX_IMR(pipe);
  3236. temp = I915_READ(reg);
  3237. temp &= ~FDI_RX_SYMBOL_LOCK;
  3238. temp &= ~FDI_RX_BIT_LOCK;
  3239. I915_WRITE(reg, temp);
  3240. I915_READ(reg);
  3241. udelay(150);
  3242. /* enable CPU FDI TX and PCH FDI RX */
  3243. reg = FDI_TX_CTL(pipe);
  3244. temp = I915_READ(reg);
  3245. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3246. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3247. temp &= ~FDI_LINK_TRAIN_NONE;
  3248. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3249. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3250. reg = FDI_RX_CTL(pipe);
  3251. temp = I915_READ(reg);
  3252. temp &= ~FDI_LINK_TRAIN_NONE;
  3253. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3254. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3255. POSTING_READ(reg);
  3256. udelay(150);
  3257. /* Ironlake workaround, enable clock pointer after FDI enable*/
  3258. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3259. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  3260. FDI_RX_PHASE_SYNC_POINTER_EN);
  3261. reg = FDI_RX_IIR(pipe);
  3262. for (tries = 0; tries < 5; tries++) {
  3263. temp = I915_READ(reg);
  3264. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3265. if ((temp & FDI_RX_BIT_LOCK)) {
  3266. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3267. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3268. break;
  3269. }
  3270. }
  3271. if (tries == 5)
  3272. DRM_ERROR("FDI train 1 fail!\n");
  3273. /* Train 2 */
  3274. reg = FDI_TX_CTL(pipe);
  3275. temp = I915_READ(reg);
  3276. temp &= ~FDI_LINK_TRAIN_NONE;
  3277. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3278. I915_WRITE(reg, temp);
  3279. reg = FDI_RX_CTL(pipe);
  3280. temp = I915_READ(reg);
  3281. temp &= ~FDI_LINK_TRAIN_NONE;
  3282. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3283. I915_WRITE(reg, temp);
  3284. POSTING_READ(reg);
  3285. udelay(150);
  3286. reg = FDI_RX_IIR(pipe);
  3287. for (tries = 0; tries < 5; tries++) {
  3288. temp = I915_READ(reg);
  3289. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3290. if (temp & FDI_RX_SYMBOL_LOCK) {
  3291. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3292. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3293. break;
  3294. }
  3295. }
  3296. if (tries == 5)
  3297. DRM_ERROR("FDI train 2 fail!\n");
  3298. DRM_DEBUG_KMS("FDI train done\n");
  3299. }
  3300. static const int snb_b_fdi_train_param[] = {
  3301. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3302. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3303. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3304. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3305. };
  3306. /* The FDI link training functions for SNB/Cougarpoint. */
  3307. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  3308. {
  3309. struct drm_device *dev = crtc->dev;
  3310. struct drm_i915_private *dev_priv = to_i915(dev);
  3311. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3312. int pipe = intel_crtc->pipe;
  3313. i915_reg_t reg;
  3314. u32 temp, i, retry;
  3315. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3316. for train result */
  3317. reg = FDI_RX_IMR(pipe);
  3318. temp = I915_READ(reg);
  3319. temp &= ~FDI_RX_SYMBOL_LOCK;
  3320. temp &= ~FDI_RX_BIT_LOCK;
  3321. I915_WRITE(reg, temp);
  3322. POSTING_READ(reg);
  3323. udelay(150);
  3324. /* enable CPU FDI TX and PCH FDI RX */
  3325. reg = FDI_TX_CTL(pipe);
  3326. temp = I915_READ(reg);
  3327. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3328. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3329. temp &= ~FDI_LINK_TRAIN_NONE;
  3330. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3331. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3332. /* SNB-B */
  3333. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3334. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3335. I915_WRITE(FDI_RX_MISC(pipe),
  3336. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3337. reg = FDI_RX_CTL(pipe);
  3338. temp = I915_READ(reg);
  3339. if (HAS_PCH_CPT(dev_priv)) {
  3340. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3341. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3342. } else {
  3343. temp &= ~FDI_LINK_TRAIN_NONE;
  3344. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3345. }
  3346. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3347. POSTING_READ(reg);
  3348. udelay(150);
  3349. for (i = 0; i < 4; i++) {
  3350. reg = FDI_TX_CTL(pipe);
  3351. temp = I915_READ(reg);
  3352. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3353. temp |= snb_b_fdi_train_param[i];
  3354. I915_WRITE(reg, temp);
  3355. POSTING_READ(reg);
  3356. udelay(500);
  3357. for (retry = 0; retry < 5; retry++) {
  3358. reg = FDI_RX_IIR(pipe);
  3359. temp = I915_READ(reg);
  3360. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3361. if (temp & FDI_RX_BIT_LOCK) {
  3362. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3363. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3364. break;
  3365. }
  3366. udelay(50);
  3367. }
  3368. if (retry < 5)
  3369. break;
  3370. }
  3371. if (i == 4)
  3372. DRM_ERROR("FDI train 1 fail!\n");
  3373. /* Train 2 */
  3374. reg = FDI_TX_CTL(pipe);
  3375. temp = I915_READ(reg);
  3376. temp &= ~FDI_LINK_TRAIN_NONE;
  3377. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3378. if (IS_GEN6(dev_priv)) {
  3379. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3380. /* SNB-B */
  3381. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3382. }
  3383. I915_WRITE(reg, temp);
  3384. reg = FDI_RX_CTL(pipe);
  3385. temp = I915_READ(reg);
  3386. if (HAS_PCH_CPT(dev_priv)) {
  3387. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3388. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3389. } else {
  3390. temp &= ~FDI_LINK_TRAIN_NONE;
  3391. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3392. }
  3393. I915_WRITE(reg, temp);
  3394. POSTING_READ(reg);
  3395. udelay(150);
  3396. for (i = 0; i < 4; i++) {
  3397. reg = FDI_TX_CTL(pipe);
  3398. temp = I915_READ(reg);
  3399. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3400. temp |= snb_b_fdi_train_param[i];
  3401. I915_WRITE(reg, temp);
  3402. POSTING_READ(reg);
  3403. udelay(500);
  3404. for (retry = 0; retry < 5; retry++) {
  3405. reg = FDI_RX_IIR(pipe);
  3406. temp = I915_READ(reg);
  3407. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3408. if (temp & FDI_RX_SYMBOL_LOCK) {
  3409. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3410. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3411. break;
  3412. }
  3413. udelay(50);
  3414. }
  3415. if (retry < 5)
  3416. break;
  3417. }
  3418. if (i == 4)
  3419. DRM_ERROR("FDI train 2 fail!\n");
  3420. DRM_DEBUG_KMS("FDI train done.\n");
  3421. }
  3422. /* Manual link training for Ivy Bridge A0 parts */
  3423. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3424. {
  3425. struct drm_device *dev = crtc->dev;
  3426. struct drm_i915_private *dev_priv = to_i915(dev);
  3427. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3428. int pipe = intel_crtc->pipe;
  3429. i915_reg_t reg;
  3430. u32 temp, i, j;
  3431. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3432. for train result */
  3433. reg = FDI_RX_IMR(pipe);
  3434. temp = I915_READ(reg);
  3435. temp &= ~FDI_RX_SYMBOL_LOCK;
  3436. temp &= ~FDI_RX_BIT_LOCK;
  3437. I915_WRITE(reg, temp);
  3438. POSTING_READ(reg);
  3439. udelay(150);
  3440. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3441. I915_READ(FDI_RX_IIR(pipe)));
  3442. /* Try each vswing and preemphasis setting twice before moving on */
  3443. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3444. /* disable first in case we need to retry */
  3445. reg = FDI_TX_CTL(pipe);
  3446. temp = I915_READ(reg);
  3447. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3448. temp &= ~FDI_TX_ENABLE;
  3449. I915_WRITE(reg, temp);
  3450. reg = FDI_RX_CTL(pipe);
  3451. temp = I915_READ(reg);
  3452. temp &= ~FDI_LINK_TRAIN_AUTO;
  3453. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3454. temp &= ~FDI_RX_ENABLE;
  3455. I915_WRITE(reg, temp);
  3456. /* enable CPU FDI TX and PCH FDI RX */
  3457. reg = FDI_TX_CTL(pipe);
  3458. temp = I915_READ(reg);
  3459. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3460. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3461. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3462. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3463. temp |= snb_b_fdi_train_param[j/2];
  3464. temp |= FDI_COMPOSITE_SYNC;
  3465. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3466. I915_WRITE(FDI_RX_MISC(pipe),
  3467. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3468. reg = FDI_RX_CTL(pipe);
  3469. temp = I915_READ(reg);
  3470. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3471. temp |= FDI_COMPOSITE_SYNC;
  3472. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3473. POSTING_READ(reg);
  3474. udelay(1); /* should be 0.5us */
  3475. for (i = 0; i < 4; i++) {
  3476. reg = FDI_RX_IIR(pipe);
  3477. temp = I915_READ(reg);
  3478. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3479. if (temp & FDI_RX_BIT_LOCK ||
  3480. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3481. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3482. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3483. i);
  3484. break;
  3485. }
  3486. udelay(1); /* should be 0.5us */
  3487. }
  3488. if (i == 4) {
  3489. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3490. continue;
  3491. }
  3492. /* Train 2 */
  3493. reg = FDI_TX_CTL(pipe);
  3494. temp = I915_READ(reg);
  3495. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3496. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3497. I915_WRITE(reg, temp);
  3498. reg = FDI_RX_CTL(pipe);
  3499. temp = I915_READ(reg);
  3500. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3501. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3502. I915_WRITE(reg, temp);
  3503. POSTING_READ(reg);
  3504. udelay(2); /* should be 1.5us */
  3505. for (i = 0; i < 4; i++) {
  3506. reg = FDI_RX_IIR(pipe);
  3507. temp = I915_READ(reg);
  3508. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3509. if (temp & FDI_RX_SYMBOL_LOCK ||
  3510. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3511. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3512. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3513. i);
  3514. goto train_done;
  3515. }
  3516. udelay(2); /* should be 1.5us */
  3517. }
  3518. if (i == 4)
  3519. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3520. }
  3521. train_done:
  3522. DRM_DEBUG_KMS("FDI train done.\n");
  3523. }
  3524. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3525. {
  3526. struct drm_device *dev = intel_crtc->base.dev;
  3527. struct drm_i915_private *dev_priv = to_i915(dev);
  3528. int pipe = intel_crtc->pipe;
  3529. i915_reg_t reg;
  3530. u32 temp;
  3531. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3532. reg = FDI_RX_CTL(pipe);
  3533. temp = I915_READ(reg);
  3534. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3535. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3536. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3537. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3538. POSTING_READ(reg);
  3539. udelay(200);
  3540. /* Switch from Rawclk to PCDclk */
  3541. temp = I915_READ(reg);
  3542. I915_WRITE(reg, temp | FDI_PCDCLK);
  3543. POSTING_READ(reg);
  3544. udelay(200);
  3545. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3546. reg = FDI_TX_CTL(pipe);
  3547. temp = I915_READ(reg);
  3548. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3549. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3550. POSTING_READ(reg);
  3551. udelay(100);
  3552. }
  3553. }
  3554. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3555. {
  3556. struct drm_device *dev = intel_crtc->base.dev;
  3557. struct drm_i915_private *dev_priv = to_i915(dev);
  3558. int pipe = intel_crtc->pipe;
  3559. i915_reg_t reg;
  3560. u32 temp;
  3561. /* Switch from PCDclk to Rawclk */
  3562. reg = FDI_RX_CTL(pipe);
  3563. temp = I915_READ(reg);
  3564. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3565. /* Disable CPU FDI TX PLL */
  3566. reg = FDI_TX_CTL(pipe);
  3567. temp = I915_READ(reg);
  3568. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3569. POSTING_READ(reg);
  3570. udelay(100);
  3571. reg = FDI_RX_CTL(pipe);
  3572. temp = I915_READ(reg);
  3573. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3574. /* Wait for the clocks to turn off. */
  3575. POSTING_READ(reg);
  3576. udelay(100);
  3577. }
  3578. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3579. {
  3580. struct drm_device *dev = crtc->dev;
  3581. struct drm_i915_private *dev_priv = to_i915(dev);
  3582. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3583. int pipe = intel_crtc->pipe;
  3584. i915_reg_t reg;
  3585. u32 temp;
  3586. /* disable CPU FDI tx and PCH FDI rx */
  3587. reg = FDI_TX_CTL(pipe);
  3588. temp = I915_READ(reg);
  3589. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3590. POSTING_READ(reg);
  3591. reg = FDI_RX_CTL(pipe);
  3592. temp = I915_READ(reg);
  3593. temp &= ~(0x7 << 16);
  3594. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3595. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3596. POSTING_READ(reg);
  3597. udelay(100);
  3598. /* Ironlake workaround, disable clock pointer after downing FDI */
  3599. if (HAS_PCH_IBX(dev_priv))
  3600. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3601. /* still set train pattern 1 */
  3602. reg = FDI_TX_CTL(pipe);
  3603. temp = I915_READ(reg);
  3604. temp &= ~FDI_LINK_TRAIN_NONE;
  3605. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3606. I915_WRITE(reg, temp);
  3607. reg = FDI_RX_CTL(pipe);
  3608. temp = I915_READ(reg);
  3609. if (HAS_PCH_CPT(dev_priv)) {
  3610. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3611. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3612. } else {
  3613. temp &= ~FDI_LINK_TRAIN_NONE;
  3614. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3615. }
  3616. /* BPC in FDI rx is consistent with that in PIPECONF */
  3617. temp &= ~(0x07 << 16);
  3618. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3619. I915_WRITE(reg, temp);
  3620. POSTING_READ(reg);
  3621. udelay(100);
  3622. }
  3623. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3624. {
  3625. struct drm_i915_private *dev_priv = to_i915(dev);
  3626. struct intel_crtc *crtc;
  3627. /* Note that we don't need to be called with mode_config.lock here
  3628. * as our list of CRTC objects is static for the lifetime of the
  3629. * device and so cannot disappear as we iterate. Similarly, we can
  3630. * happily treat the predicates as racy, atomic checks as userspace
  3631. * cannot claim and pin a new fb without at least acquring the
  3632. * struct_mutex and so serialising with us.
  3633. */
  3634. for_each_intel_crtc(dev, crtc) {
  3635. if (atomic_read(&crtc->unpin_work_count) == 0)
  3636. continue;
  3637. if (crtc->flip_work)
  3638. intel_wait_for_vblank(dev_priv, crtc->pipe);
  3639. return true;
  3640. }
  3641. return false;
  3642. }
  3643. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3644. {
  3645. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3646. struct intel_flip_work *work = intel_crtc->flip_work;
  3647. intel_crtc->flip_work = NULL;
  3648. if (work->event)
  3649. drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
  3650. drm_crtc_vblank_put(&intel_crtc->base);
  3651. wake_up_all(&dev_priv->pending_flip_queue);
  3652. queue_work(dev_priv->wq, &work->unpin_work);
  3653. trace_i915_flip_complete(intel_crtc->plane,
  3654. work->pending_flip_obj);
  3655. }
  3656. static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3657. {
  3658. struct drm_device *dev = crtc->dev;
  3659. struct drm_i915_private *dev_priv = to_i915(dev);
  3660. long ret;
  3661. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3662. ret = wait_event_interruptible_timeout(
  3663. dev_priv->pending_flip_queue,
  3664. !intel_crtc_has_pending_flip(crtc),
  3665. 60*HZ);
  3666. if (ret < 0)
  3667. return ret;
  3668. if (ret == 0) {
  3669. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3670. struct intel_flip_work *work;
  3671. spin_lock_irq(&dev->event_lock);
  3672. work = intel_crtc->flip_work;
  3673. if (work && !is_mmio_work(work)) {
  3674. WARN_ONCE(1, "Removing stuck page flip\n");
  3675. page_flip_completed(intel_crtc);
  3676. }
  3677. spin_unlock_irq(&dev->event_lock);
  3678. }
  3679. return 0;
  3680. }
  3681. void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3682. {
  3683. u32 temp;
  3684. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3685. mutex_lock(&dev_priv->sb_lock);
  3686. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3687. temp |= SBI_SSCCTL_DISABLE;
  3688. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3689. mutex_unlock(&dev_priv->sb_lock);
  3690. }
  3691. /* Program iCLKIP clock to the desired frequency */
  3692. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3693. {
  3694. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  3695. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3696. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3697. u32 temp;
  3698. lpt_disable_iclkip(dev_priv);
  3699. /* The iCLK virtual clock root frequency is in MHz,
  3700. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3701. * divisors, it is necessary to divide one by another, so we
  3702. * convert the virtual clock precision to KHz here for higher
  3703. * precision.
  3704. */
  3705. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3706. u32 iclk_virtual_root_freq = 172800 * 1000;
  3707. u32 iclk_pi_range = 64;
  3708. u32 desired_divisor;
  3709. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3710. clock << auxdiv);
  3711. divsel = (desired_divisor / iclk_pi_range) - 2;
  3712. phaseinc = desired_divisor % iclk_pi_range;
  3713. /*
  3714. * Near 20MHz is a corner case which is
  3715. * out of range for the 7-bit divisor
  3716. */
  3717. if (divsel <= 0x7f)
  3718. break;
  3719. }
  3720. /* This should not happen with any sane values */
  3721. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3722. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3723. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3724. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3725. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3726. clock,
  3727. auxdiv,
  3728. divsel,
  3729. phasedir,
  3730. phaseinc);
  3731. mutex_lock(&dev_priv->sb_lock);
  3732. /* Program SSCDIVINTPHASE6 */
  3733. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3734. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3735. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3736. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3737. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3738. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3739. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3740. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3741. /* Program SSCAUXDIV */
  3742. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3743. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3744. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3745. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3746. /* Enable modulator and associated divider */
  3747. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3748. temp &= ~SBI_SSCCTL_DISABLE;
  3749. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3750. mutex_unlock(&dev_priv->sb_lock);
  3751. /* Wait for initialization time */
  3752. udelay(24);
  3753. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3754. }
  3755. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3756. {
  3757. u32 divsel, phaseinc, auxdiv;
  3758. u32 iclk_virtual_root_freq = 172800 * 1000;
  3759. u32 iclk_pi_range = 64;
  3760. u32 desired_divisor;
  3761. u32 temp;
  3762. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3763. return 0;
  3764. mutex_lock(&dev_priv->sb_lock);
  3765. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3766. if (temp & SBI_SSCCTL_DISABLE) {
  3767. mutex_unlock(&dev_priv->sb_lock);
  3768. return 0;
  3769. }
  3770. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3771. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3772. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3773. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3774. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3775. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3776. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3777. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3778. mutex_unlock(&dev_priv->sb_lock);
  3779. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3780. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3781. desired_divisor << auxdiv);
  3782. }
  3783. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3784. enum pipe pch_transcoder)
  3785. {
  3786. struct drm_device *dev = crtc->base.dev;
  3787. struct drm_i915_private *dev_priv = to_i915(dev);
  3788. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3789. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3790. I915_READ(HTOTAL(cpu_transcoder)));
  3791. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3792. I915_READ(HBLANK(cpu_transcoder)));
  3793. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3794. I915_READ(HSYNC(cpu_transcoder)));
  3795. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3796. I915_READ(VTOTAL(cpu_transcoder)));
  3797. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3798. I915_READ(VBLANK(cpu_transcoder)));
  3799. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3800. I915_READ(VSYNC(cpu_transcoder)));
  3801. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3802. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3803. }
  3804. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3805. {
  3806. struct drm_i915_private *dev_priv = to_i915(dev);
  3807. uint32_t temp;
  3808. temp = I915_READ(SOUTH_CHICKEN1);
  3809. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3810. return;
  3811. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3812. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3813. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3814. if (enable)
  3815. temp |= FDI_BC_BIFURCATION_SELECT;
  3816. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3817. I915_WRITE(SOUTH_CHICKEN1, temp);
  3818. POSTING_READ(SOUTH_CHICKEN1);
  3819. }
  3820. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3821. {
  3822. struct drm_device *dev = intel_crtc->base.dev;
  3823. switch (intel_crtc->pipe) {
  3824. case PIPE_A:
  3825. break;
  3826. case PIPE_B:
  3827. if (intel_crtc->config->fdi_lanes > 2)
  3828. cpt_set_fdi_bc_bifurcation(dev, false);
  3829. else
  3830. cpt_set_fdi_bc_bifurcation(dev, true);
  3831. break;
  3832. case PIPE_C:
  3833. cpt_set_fdi_bc_bifurcation(dev, true);
  3834. break;
  3835. default:
  3836. BUG();
  3837. }
  3838. }
  3839. /* Return which DP Port should be selected for Transcoder DP control */
  3840. static enum port
  3841. intel_trans_dp_port_sel(struct drm_crtc *crtc)
  3842. {
  3843. struct drm_device *dev = crtc->dev;
  3844. struct intel_encoder *encoder;
  3845. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3846. if (encoder->type == INTEL_OUTPUT_DP ||
  3847. encoder->type == INTEL_OUTPUT_EDP)
  3848. return enc_to_dig_port(&encoder->base)->port;
  3849. }
  3850. return -1;
  3851. }
  3852. /*
  3853. * Enable PCH resources required for PCH ports:
  3854. * - PCH PLLs
  3855. * - FDI training & RX/TX
  3856. * - update transcoder timings
  3857. * - DP transcoding bits
  3858. * - transcoder
  3859. */
  3860. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3861. {
  3862. struct drm_device *dev = crtc->dev;
  3863. struct drm_i915_private *dev_priv = to_i915(dev);
  3864. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3865. int pipe = intel_crtc->pipe;
  3866. u32 temp;
  3867. assert_pch_transcoder_disabled(dev_priv, pipe);
  3868. if (IS_IVYBRIDGE(dev_priv))
  3869. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3870. /* Write the TU size bits before fdi link training, so that error
  3871. * detection works. */
  3872. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3873. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3874. /* For PCH output, training FDI link */
  3875. dev_priv->display.fdi_link_train(crtc);
  3876. /* We need to program the right clock selection before writing the pixel
  3877. * mutliplier into the DPLL. */
  3878. if (HAS_PCH_CPT(dev_priv)) {
  3879. u32 sel;
  3880. temp = I915_READ(PCH_DPLL_SEL);
  3881. temp |= TRANS_DPLL_ENABLE(pipe);
  3882. sel = TRANS_DPLLB_SEL(pipe);
  3883. if (intel_crtc->config->shared_dpll ==
  3884. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3885. temp |= sel;
  3886. else
  3887. temp &= ~sel;
  3888. I915_WRITE(PCH_DPLL_SEL, temp);
  3889. }
  3890. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3891. * transcoder, and we actually should do this to not upset any PCH
  3892. * transcoder that already use the clock when we share it.
  3893. *
  3894. * Note that enable_shared_dpll tries to do the right thing, but
  3895. * get_shared_dpll unconditionally resets the pll - we need that to have
  3896. * the right LVDS enable sequence. */
  3897. intel_enable_shared_dpll(intel_crtc);
  3898. /* set transcoder timing, panel must allow it */
  3899. assert_panel_unlocked(dev_priv, pipe);
  3900. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3901. intel_fdi_normal_train(crtc);
  3902. /* For PCH DP, enable TRANS_DP_CTL */
  3903. if (HAS_PCH_CPT(dev_priv) &&
  3904. intel_crtc_has_dp_encoder(intel_crtc->config)) {
  3905. const struct drm_display_mode *adjusted_mode =
  3906. &intel_crtc->config->base.adjusted_mode;
  3907. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3908. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3909. temp = I915_READ(reg);
  3910. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3911. TRANS_DP_SYNC_MASK |
  3912. TRANS_DP_BPC_MASK);
  3913. temp |= TRANS_DP_OUTPUT_ENABLE;
  3914. temp |= bpc << 9; /* same format but at 11:9 */
  3915. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3916. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3917. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3918. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3919. switch (intel_trans_dp_port_sel(crtc)) {
  3920. case PORT_B:
  3921. temp |= TRANS_DP_PORT_SEL_B;
  3922. break;
  3923. case PORT_C:
  3924. temp |= TRANS_DP_PORT_SEL_C;
  3925. break;
  3926. case PORT_D:
  3927. temp |= TRANS_DP_PORT_SEL_D;
  3928. break;
  3929. default:
  3930. BUG();
  3931. }
  3932. I915_WRITE(reg, temp);
  3933. }
  3934. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3935. }
  3936. static void lpt_pch_enable(struct drm_crtc *crtc)
  3937. {
  3938. struct drm_device *dev = crtc->dev;
  3939. struct drm_i915_private *dev_priv = to_i915(dev);
  3940. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3941. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3942. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3943. lpt_program_iclkip(crtc);
  3944. /* Set transcoder timing. */
  3945. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3946. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3947. }
  3948. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3949. {
  3950. struct drm_i915_private *dev_priv = to_i915(dev);
  3951. i915_reg_t dslreg = PIPEDSL(pipe);
  3952. u32 temp;
  3953. temp = I915_READ(dslreg);
  3954. udelay(500);
  3955. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3956. if (wait_for(I915_READ(dslreg) != temp, 5))
  3957. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3958. }
  3959. }
  3960. static int
  3961. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3962. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3963. int src_w, int src_h, int dst_w, int dst_h)
  3964. {
  3965. struct intel_crtc_scaler_state *scaler_state =
  3966. &crtc_state->scaler_state;
  3967. struct intel_crtc *intel_crtc =
  3968. to_intel_crtc(crtc_state->base.crtc);
  3969. int need_scaling;
  3970. need_scaling = drm_rotation_90_or_270(rotation) ?
  3971. (src_h != dst_w || src_w != dst_h):
  3972. (src_w != dst_w || src_h != dst_h);
  3973. /*
  3974. * if plane is being disabled or scaler is no more required or force detach
  3975. * - free scaler binded to this plane/crtc
  3976. * - in order to do this, update crtc->scaler_usage
  3977. *
  3978. * Here scaler state in crtc_state is set free so that
  3979. * scaler can be assigned to other user. Actual register
  3980. * update to free the scaler is done in plane/panel-fit programming.
  3981. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3982. */
  3983. if (force_detach || !need_scaling) {
  3984. if (*scaler_id >= 0) {
  3985. scaler_state->scaler_users &= ~(1 << scaler_user);
  3986. scaler_state->scalers[*scaler_id].in_use = 0;
  3987. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3988. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3989. intel_crtc->pipe, scaler_user, *scaler_id,
  3990. scaler_state->scaler_users);
  3991. *scaler_id = -1;
  3992. }
  3993. return 0;
  3994. }
  3995. /* range checks */
  3996. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3997. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3998. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3999. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  4000. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  4001. "size is out of scaler range\n",
  4002. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  4003. return -EINVAL;
  4004. }
  4005. /* mark this plane as a scaler user in crtc_state */
  4006. scaler_state->scaler_users |= (1 << scaler_user);
  4007. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  4008. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  4009. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  4010. scaler_state->scaler_users);
  4011. return 0;
  4012. }
  4013. /**
  4014. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  4015. *
  4016. * @state: crtc's scaler state
  4017. *
  4018. * Return
  4019. * 0 - scaler_usage updated successfully
  4020. * error - requested scaling cannot be supported or other error condition
  4021. */
  4022. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  4023. {
  4024. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  4025. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  4026. &state->scaler_state.scaler_id, DRM_ROTATE_0,
  4027. state->pipe_src_w, state->pipe_src_h,
  4028. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  4029. }
  4030. /**
  4031. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  4032. *
  4033. * @state: crtc's scaler state
  4034. * @plane_state: atomic plane state to update
  4035. *
  4036. * Return
  4037. * 0 - scaler_usage updated successfully
  4038. * error - requested scaling cannot be supported or other error condition
  4039. */
  4040. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  4041. struct intel_plane_state *plane_state)
  4042. {
  4043. struct intel_plane *intel_plane =
  4044. to_intel_plane(plane_state->base.plane);
  4045. struct drm_framebuffer *fb = plane_state->base.fb;
  4046. int ret;
  4047. bool force_detach = !fb || !plane_state->base.visible;
  4048. ret = skl_update_scaler(crtc_state, force_detach,
  4049. drm_plane_index(&intel_plane->base),
  4050. &plane_state->scaler_id,
  4051. plane_state->base.rotation,
  4052. drm_rect_width(&plane_state->base.src) >> 16,
  4053. drm_rect_height(&plane_state->base.src) >> 16,
  4054. drm_rect_width(&plane_state->base.dst),
  4055. drm_rect_height(&plane_state->base.dst));
  4056. if (ret || plane_state->scaler_id < 0)
  4057. return ret;
  4058. /* check colorkey */
  4059. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  4060. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  4061. intel_plane->base.base.id,
  4062. intel_plane->base.name);
  4063. return -EINVAL;
  4064. }
  4065. /* Check src format */
  4066. switch (fb->pixel_format) {
  4067. case DRM_FORMAT_RGB565:
  4068. case DRM_FORMAT_XBGR8888:
  4069. case DRM_FORMAT_XRGB8888:
  4070. case DRM_FORMAT_ABGR8888:
  4071. case DRM_FORMAT_ARGB8888:
  4072. case DRM_FORMAT_XRGB2101010:
  4073. case DRM_FORMAT_XBGR2101010:
  4074. case DRM_FORMAT_YUYV:
  4075. case DRM_FORMAT_YVYU:
  4076. case DRM_FORMAT_UYVY:
  4077. case DRM_FORMAT_VYUY:
  4078. break;
  4079. default:
  4080. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  4081. intel_plane->base.base.id, intel_plane->base.name,
  4082. fb->base.id, fb->pixel_format);
  4083. return -EINVAL;
  4084. }
  4085. return 0;
  4086. }
  4087. static void skylake_scaler_disable(struct intel_crtc *crtc)
  4088. {
  4089. int i;
  4090. for (i = 0; i < crtc->num_scalers; i++)
  4091. skl_detach_scaler(crtc, i);
  4092. }
  4093. static void skylake_pfit_enable(struct intel_crtc *crtc)
  4094. {
  4095. struct drm_device *dev = crtc->base.dev;
  4096. struct drm_i915_private *dev_priv = to_i915(dev);
  4097. int pipe = crtc->pipe;
  4098. struct intel_crtc_scaler_state *scaler_state =
  4099. &crtc->config->scaler_state;
  4100. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  4101. if (crtc->config->pch_pfit.enabled) {
  4102. int id;
  4103. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  4104. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  4105. return;
  4106. }
  4107. id = scaler_state->scaler_id;
  4108. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  4109. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  4110. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  4111. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  4112. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  4113. }
  4114. }
  4115. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  4116. {
  4117. struct drm_device *dev = crtc->base.dev;
  4118. struct drm_i915_private *dev_priv = to_i915(dev);
  4119. int pipe = crtc->pipe;
  4120. if (crtc->config->pch_pfit.enabled) {
  4121. /* Force use of hard-coded filter coefficients
  4122. * as some pre-programmed values are broken,
  4123. * e.g. x201.
  4124. */
  4125. if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
  4126. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  4127. PF_PIPE_SEL_IVB(pipe));
  4128. else
  4129. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  4130. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  4131. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  4132. }
  4133. }
  4134. void hsw_enable_ips(struct intel_crtc *crtc)
  4135. {
  4136. struct drm_device *dev = crtc->base.dev;
  4137. struct drm_i915_private *dev_priv = to_i915(dev);
  4138. if (!crtc->config->ips_enabled)
  4139. return;
  4140. /*
  4141. * We can only enable IPS after we enable a plane and wait for a vblank
  4142. * This function is called from post_plane_update, which is run after
  4143. * a vblank wait.
  4144. */
  4145. assert_plane_enabled(dev_priv, crtc->plane);
  4146. if (IS_BROADWELL(dev_priv)) {
  4147. mutex_lock(&dev_priv->rps.hw_lock);
  4148. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  4149. mutex_unlock(&dev_priv->rps.hw_lock);
  4150. /* Quoting Art Runyan: "its not safe to expect any particular
  4151. * value in IPS_CTL bit 31 after enabling IPS through the
  4152. * mailbox." Moreover, the mailbox may return a bogus state,
  4153. * so we need to just enable it and continue on.
  4154. */
  4155. } else {
  4156. I915_WRITE(IPS_CTL, IPS_ENABLE);
  4157. /* The bit only becomes 1 in the next vblank, so this wait here
  4158. * is essentially intel_wait_for_vblank. If we don't have this
  4159. * and don't wait for vblanks until the end of crtc_enable, then
  4160. * the HW state readout code will complain that the expected
  4161. * IPS_CTL value is not the one we read. */
  4162. if (intel_wait_for_register(dev_priv,
  4163. IPS_CTL, IPS_ENABLE, IPS_ENABLE,
  4164. 50))
  4165. DRM_ERROR("Timed out waiting for IPS enable\n");
  4166. }
  4167. }
  4168. void hsw_disable_ips(struct intel_crtc *crtc)
  4169. {
  4170. struct drm_device *dev = crtc->base.dev;
  4171. struct drm_i915_private *dev_priv = to_i915(dev);
  4172. if (!crtc->config->ips_enabled)
  4173. return;
  4174. assert_plane_enabled(dev_priv, crtc->plane);
  4175. if (IS_BROADWELL(dev_priv)) {
  4176. mutex_lock(&dev_priv->rps.hw_lock);
  4177. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  4178. mutex_unlock(&dev_priv->rps.hw_lock);
  4179. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  4180. if (intel_wait_for_register(dev_priv,
  4181. IPS_CTL, IPS_ENABLE, 0,
  4182. 42))
  4183. DRM_ERROR("Timed out waiting for IPS disable\n");
  4184. } else {
  4185. I915_WRITE(IPS_CTL, 0);
  4186. POSTING_READ(IPS_CTL);
  4187. }
  4188. /* We need to wait for a vblank before we can disable the plane. */
  4189. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4190. }
  4191. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  4192. {
  4193. if (intel_crtc->overlay) {
  4194. struct drm_device *dev = intel_crtc->base.dev;
  4195. struct drm_i915_private *dev_priv = to_i915(dev);
  4196. mutex_lock(&dev->struct_mutex);
  4197. dev_priv->mm.interruptible = false;
  4198. (void) intel_overlay_switch_off(intel_crtc->overlay);
  4199. dev_priv->mm.interruptible = true;
  4200. mutex_unlock(&dev->struct_mutex);
  4201. }
  4202. /* Let userspace switch the overlay on again. In most cases userspace
  4203. * has to recompute where to put it anyway.
  4204. */
  4205. }
  4206. /**
  4207. * intel_post_enable_primary - Perform operations after enabling primary plane
  4208. * @crtc: the CRTC whose primary plane was just enabled
  4209. *
  4210. * Performs potentially sleeping operations that must be done after the primary
  4211. * plane is enabled, such as updating FBC and IPS. Note that this may be
  4212. * called due to an explicit primary plane update, or due to an implicit
  4213. * re-enable that is caused when a sprite plane is updated to no longer
  4214. * completely hide the primary plane.
  4215. */
  4216. static void
  4217. intel_post_enable_primary(struct drm_crtc *crtc)
  4218. {
  4219. struct drm_device *dev = crtc->dev;
  4220. struct drm_i915_private *dev_priv = to_i915(dev);
  4221. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4222. int pipe = intel_crtc->pipe;
  4223. /*
  4224. * FIXME IPS should be fine as long as one plane is
  4225. * enabled, but in practice it seems to have problems
  4226. * when going from primary only to sprite only and vice
  4227. * versa.
  4228. */
  4229. hsw_enable_ips(intel_crtc);
  4230. /*
  4231. * Gen2 reports pipe underruns whenever all planes are disabled.
  4232. * So don't enable underrun reporting before at least some planes
  4233. * are enabled.
  4234. * FIXME: Need to fix the logic to work when we turn off all planes
  4235. * but leave the pipe running.
  4236. */
  4237. if (IS_GEN2(dev_priv))
  4238. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4239. /* Underruns don't always raise interrupts, so check manually. */
  4240. intel_check_cpu_fifo_underruns(dev_priv);
  4241. intel_check_pch_fifo_underruns(dev_priv);
  4242. }
  4243. /* FIXME move all this to pre_plane_update() with proper state tracking */
  4244. static void
  4245. intel_pre_disable_primary(struct drm_crtc *crtc)
  4246. {
  4247. struct drm_device *dev = crtc->dev;
  4248. struct drm_i915_private *dev_priv = to_i915(dev);
  4249. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4250. int pipe = intel_crtc->pipe;
  4251. /*
  4252. * Gen2 reports pipe underruns whenever all planes are disabled.
  4253. * So diasble underrun reporting before all the planes get disabled.
  4254. * FIXME: Need to fix the logic to work when we turn off all planes
  4255. * but leave the pipe running.
  4256. */
  4257. if (IS_GEN2(dev_priv))
  4258. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4259. /*
  4260. * FIXME IPS should be fine as long as one plane is
  4261. * enabled, but in practice it seems to have problems
  4262. * when going from primary only to sprite only and vice
  4263. * versa.
  4264. */
  4265. hsw_disable_ips(intel_crtc);
  4266. }
  4267. /* FIXME get rid of this and use pre_plane_update */
  4268. static void
  4269. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  4270. {
  4271. struct drm_device *dev = crtc->dev;
  4272. struct drm_i915_private *dev_priv = to_i915(dev);
  4273. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4274. int pipe = intel_crtc->pipe;
  4275. intel_pre_disable_primary(crtc);
  4276. /*
  4277. * Vblank time updates from the shadow to live plane control register
  4278. * are blocked if the memory self-refresh mode is active at that
  4279. * moment. So to make sure the plane gets truly disabled, disable
  4280. * first the self-refresh mode. The self-refresh enable bit in turn
  4281. * will be checked/applied by the HW only at the next frame start
  4282. * event which is after the vblank start event, so we need to have a
  4283. * wait-for-vblank between disabling the plane and the pipe.
  4284. */
  4285. if (HAS_GMCH_DISPLAY(dev_priv)) {
  4286. intel_set_memory_cxsr(dev_priv, false);
  4287. dev_priv->wm.vlv.cxsr = false;
  4288. intel_wait_for_vblank(dev_priv, pipe);
  4289. }
  4290. }
  4291. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  4292. {
  4293. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4294. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4295. struct intel_crtc_state *pipe_config =
  4296. to_intel_crtc_state(crtc->base.state);
  4297. struct drm_plane *primary = crtc->base.primary;
  4298. struct drm_plane_state *old_pri_state =
  4299. drm_atomic_get_existing_plane_state(old_state, primary);
  4300. intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
  4301. crtc->wm.cxsr_allowed = true;
  4302. if (pipe_config->update_wm_post && pipe_config->base.active)
  4303. intel_update_watermarks(crtc);
  4304. if (old_pri_state) {
  4305. struct intel_plane_state *primary_state =
  4306. to_intel_plane_state(primary->state);
  4307. struct intel_plane_state *old_primary_state =
  4308. to_intel_plane_state(old_pri_state);
  4309. intel_fbc_post_update(crtc);
  4310. if (primary_state->base.visible &&
  4311. (needs_modeset(&pipe_config->base) ||
  4312. !old_primary_state->base.visible))
  4313. intel_post_enable_primary(&crtc->base);
  4314. }
  4315. }
  4316. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
  4317. {
  4318. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4319. struct drm_device *dev = crtc->base.dev;
  4320. struct drm_i915_private *dev_priv = to_i915(dev);
  4321. struct intel_crtc_state *pipe_config =
  4322. to_intel_crtc_state(crtc->base.state);
  4323. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4324. struct drm_plane *primary = crtc->base.primary;
  4325. struct drm_plane_state *old_pri_state =
  4326. drm_atomic_get_existing_plane_state(old_state, primary);
  4327. bool modeset = needs_modeset(&pipe_config->base);
  4328. struct intel_atomic_state *old_intel_state =
  4329. to_intel_atomic_state(old_state);
  4330. if (old_pri_state) {
  4331. struct intel_plane_state *primary_state =
  4332. to_intel_plane_state(primary->state);
  4333. struct intel_plane_state *old_primary_state =
  4334. to_intel_plane_state(old_pri_state);
  4335. intel_fbc_pre_update(crtc, pipe_config, primary_state);
  4336. if (old_primary_state->base.visible &&
  4337. (modeset || !primary_state->base.visible))
  4338. intel_pre_disable_primary(&crtc->base);
  4339. }
  4340. if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev_priv)) {
  4341. crtc->wm.cxsr_allowed = false;
  4342. /*
  4343. * Vblank time updates from the shadow to live plane control register
  4344. * are blocked if the memory self-refresh mode is active at that
  4345. * moment. So to make sure the plane gets truly disabled, disable
  4346. * first the self-refresh mode. The self-refresh enable bit in turn
  4347. * will be checked/applied by the HW only at the next frame start
  4348. * event which is after the vblank start event, so we need to have a
  4349. * wait-for-vblank between disabling the plane and the pipe.
  4350. */
  4351. if (old_crtc_state->base.active) {
  4352. intel_set_memory_cxsr(dev_priv, false);
  4353. dev_priv->wm.vlv.cxsr = false;
  4354. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4355. }
  4356. }
  4357. /*
  4358. * IVB workaround: must disable low power watermarks for at least
  4359. * one frame before enabling scaling. LP watermarks can be re-enabled
  4360. * when scaling is disabled.
  4361. *
  4362. * WaCxSRDisabledForSpriteScaling:ivb
  4363. */
  4364. if (pipe_config->disable_lp_wm) {
  4365. ilk_disable_lp_wm(dev);
  4366. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4367. }
  4368. /*
  4369. * If we're doing a modeset, we're done. No need to do any pre-vblank
  4370. * watermark programming here.
  4371. */
  4372. if (needs_modeset(&pipe_config->base))
  4373. return;
  4374. /*
  4375. * For platforms that support atomic watermarks, program the
  4376. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  4377. * will be the intermediate values that are safe for both pre- and
  4378. * post- vblank; when vblank happens, the 'active' values will be set
  4379. * to the final 'target' values and we'll do this again to get the
  4380. * optimal watermarks. For gen9+ platforms, the values we program here
  4381. * will be the final target values which will get automatically latched
  4382. * at vblank time; no further programming will be necessary.
  4383. *
  4384. * If a platform hasn't been transitioned to atomic watermarks yet,
  4385. * we'll continue to update watermarks the old way, if flags tell
  4386. * us to.
  4387. */
  4388. if (dev_priv->display.initial_watermarks != NULL)
  4389. dev_priv->display.initial_watermarks(old_intel_state,
  4390. pipe_config);
  4391. else if (pipe_config->update_wm_pre)
  4392. intel_update_watermarks(crtc);
  4393. }
  4394. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4395. {
  4396. struct drm_device *dev = crtc->dev;
  4397. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4398. struct drm_plane *p;
  4399. int pipe = intel_crtc->pipe;
  4400. intel_crtc_dpms_overlay_disable(intel_crtc);
  4401. drm_for_each_plane_mask(p, dev, plane_mask)
  4402. to_intel_plane(p)->disable_plane(p, crtc);
  4403. /*
  4404. * FIXME: Once we grow proper nuclear flip support out of this we need
  4405. * to compute the mask of flip planes precisely. For the time being
  4406. * consider this a flip to a NULL plane.
  4407. */
  4408. intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4409. }
  4410. static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
  4411. struct intel_crtc_state *crtc_state,
  4412. struct drm_atomic_state *old_state)
  4413. {
  4414. struct drm_connector_state *old_conn_state;
  4415. struct drm_connector *conn;
  4416. int i;
  4417. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4418. struct drm_connector_state *conn_state = conn->state;
  4419. struct intel_encoder *encoder =
  4420. to_intel_encoder(conn_state->best_encoder);
  4421. if (conn_state->crtc != crtc)
  4422. continue;
  4423. if (encoder->pre_pll_enable)
  4424. encoder->pre_pll_enable(encoder, crtc_state, conn_state);
  4425. }
  4426. }
  4427. static void intel_encoders_pre_enable(struct drm_crtc *crtc,
  4428. struct intel_crtc_state *crtc_state,
  4429. struct drm_atomic_state *old_state)
  4430. {
  4431. struct drm_connector_state *old_conn_state;
  4432. struct drm_connector *conn;
  4433. int i;
  4434. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4435. struct drm_connector_state *conn_state = conn->state;
  4436. struct intel_encoder *encoder =
  4437. to_intel_encoder(conn_state->best_encoder);
  4438. if (conn_state->crtc != crtc)
  4439. continue;
  4440. if (encoder->pre_enable)
  4441. encoder->pre_enable(encoder, crtc_state, conn_state);
  4442. }
  4443. }
  4444. static void intel_encoders_enable(struct drm_crtc *crtc,
  4445. struct intel_crtc_state *crtc_state,
  4446. struct drm_atomic_state *old_state)
  4447. {
  4448. struct drm_connector_state *old_conn_state;
  4449. struct drm_connector *conn;
  4450. int i;
  4451. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4452. struct drm_connector_state *conn_state = conn->state;
  4453. struct intel_encoder *encoder =
  4454. to_intel_encoder(conn_state->best_encoder);
  4455. if (conn_state->crtc != crtc)
  4456. continue;
  4457. encoder->enable(encoder, crtc_state, conn_state);
  4458. intel_opregion_notify_encoder(encoder, true);
  4459. }
  4460. }
  4461. static void intel_encoders_disable(struct drm_crtc *crtc,
  4462. struct intel_crtc_state *old_crtc_state,
  4463. struct drm_atomic_state *old_state)
  4464. {
  4465. struct drm_connector_state *old_conn_state;
  4466. struct drm_connector *conn;
  4467. int i;
  4468. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4469. struct intel_encoder *encoder =
  4470. to_intel_encoder(old_conn_state->best_encoder);
  4471. if (old_conn_state->crtc != crtc)
  4472. continue;
  4473. intel_opregion_notify_encoder(encoder, false);
  4474. encoder->disable(encoder, old_crtc_state, old_conn_state);
  4475. }
  4476. }
  4477. static void intel_encoders_post_disable(struct drm_crtc *crtc,
  4478. struct intel_crtc_state *old_crtc_state,
  4479. struct drm_atomic_state *old_state)
  4480. {
  4481. struct drm_connector_state *old_conn_state;
  4482. struct drm_connector *conn;
  4483. int i;
  4484. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4485. struct intel_encoder *encoder =
  4486. to_intel_encoder(old_conn_state->best_encoder);
  4487. if (old_conn_state->crtc != crtc)
  4488. continue;
  4489. if (encoder->post_disable)
  4490. encoder->post_disable(encoder, old_crtc_state, old_conn_state);
  4491. }
  4492. }
  4493. static void intel_encoders_post_pll_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_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. if (encoder->post_pll_disable)
  4506. encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
  4507. }
  4508. }
  4509. static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
  4510. struct drm_atomic_state *old_state)
  4511. {
  4512. struct drm_crtc *crtc = pipe_config->base.crtc;
  4513. struct drm_device *dev = crtc->dev;
  4514. struct drm_i915_private *dev_priv = to_i915(dev);
  4515. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4516. int pipe = intel_crtc->pipe;
  4517. struct intel_atomic_state *old_intel_state =
  4518. to_intel_atomic_state(old_state);
  4519. if (WARN_ON(intel_crtc->active))
  4520. return;
  4521. /*
  4522. * Sometimes spurious CPU pipe underruns happen during FDI
  4523. * training, at least with VGA+HDMI cloning. Suppress them.
  4524. *
  4525. * On ILK we get an occasional spurious CPU pipe underruns
  4526. * between eDP port A enable and vdd enable. Also PCH port
  4527. * enable seems to result in the occasional CPU pipe underrun.
  4528. *
  4529. * Spurious PCH underruns also occur during PCH enabling.
  4530. */
  4531. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4532. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4533. if (intel_crtc->config->has_pch_encoder)
  4534. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4535. if (intel_crtc->config->has_pch_encoder)
  4536. intel_prepare_shared_dpll(intel_crtc);
  4537. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4538. intel_dp_set_m_n(intel_crtc, M1_N1);
  4539. intel_set_pipe_timings(intel_crtc);
  4540. intel_set_pipe_src_size(intel_crtc);
  4541. if (intel_crtc->config->has_pch_encoder) {
  4542. intel_cpu_transcoder_set_m_n(intel_crtc,
  4543. &intel_crtc->config->fdi_m_n, NULL);
  4544. }
  4545. ironlake_set_pipeconf(crtc);
  4546. intel_crtc->active = true;
  4547. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4548. if (intel_crtc->config->has_pch_encoder) {
  4549. /* Note: FDI PLL enabling _must_ be done before we enable the
  4550. * cpu pipes, hence this is separate from all the other fdi/pch
  4551. * enabling. */
  4552. ironlake_fdi_pll_enable(intel_crtc);
  4553. } else {
  4554. assert_fdi_tx_disabled(dev_priv, pipe);
  4555. assert_fdi_rx_disabled(dev_priv, pipe);
  4556. }
  4557. ironlake_pfit_enable(intel_crtc);
  4558. /*
  4559. * On ILK+ LUT must be loaded before the pipe is running but with
  4560. * clocks enabled
  4561. */
  4562. intel_color_load_luts(&pipe_config->base);
  4563. if (dev_priv->display.initial_watermarks != NULL)
  4564. dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
  4565. intel_enable_pipe(intel_crtc);
  4566. if (intel_crtc->config->has_pch_encoder)
  4567. ironlake_pch_enable(crtc);
  4568. assert_vblank_disabled(crtc);
  4569. drm_crtc_vblank_on(crtc);
  4570. intel_encoders_enable(crtc, pipe_config, old_state);
  4571. if (HAS_PCH_CPT(dev_priv))
  4572. cpt_verify_modeset(dev, intel_crtc->pipe);
  4573. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4574. if (intel_crtc->config->has_pch_encoder)
  4575. intel_wait_for_vblank(dev_priv, pipe);
  4576. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4577. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4578. }
  4579. /* IPS only exists on ULT machines and is tied to pipe A. */
  4580. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4581. {
  4582. return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
  4583. }
  4584. static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
  4585. struct drm_atomic_state *old_state)
  4586. {
  4587. struct drm_crtc *crtc = pipe_config->base.crtc;
  4588. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4589. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4590. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4591. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4592. struct intel_atomic_state *old_intel_state =
  4593. to_intel_atomic_state(old_state);
  4594. if (WARN_ON(intel_crtc->active))
  4595. return;
  4596. if (intel_crtc->config->has_pch_encoder)
  4597. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4598. false);
  4599. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4600. if (intel_crtc->config->shared_dpll)
  4601. intel_enable_shared_dpll(intel_crtc);
  4602. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4603. intel_dp_set_m_n(intel_crtc, M1_N1);
  4604. if (!transcoder_is_dsi(cpu_transcoder))
  4605. intel_set_pipe_timings(intel_crtc);
  4606. intel_set_pipe_src_size(intel_crtc);
  4607. if (cpu_transcoder != TRANSCODER_EDP &&
  4608. !transcoder_is_dsi(cpu_transcoder)) {
  4609. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4610. intel_crtc->config->pixel_multiplier - 1);
  4611. }
  4612. if (intel_crtc->config->has_pch_encoder) {
  4613. intel_cpu_transcoder_set_m_n(intel_crtc,
  4614. &intel_crtc->config->fdi_m_n, NULL);
  4615. }
  4616. if (!transcoder_is_dsi(cpu_transcoder))
  4617. haswell_set_pipeconf(crtc);
  4618. haswell_set_pipemisc(crtc);
  4619. intel_color_set_csc(&pipe_config->base);
  4620. intel_crtc->active = true;
  4621. if (intel_crtc->config->has_pch_encoder)
  4622. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4623. else
  4624. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4625. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4626. if (intel_crtc->config->has_pch_encoder)
  4627. dev_priv->display.fdi_link_train(crtc);
  4628. if (!transcoder_is_dsi(cpu_transcoder))
  4629. intel_ddi_enable_pipe_clock(intel_crtc);
  4630. if (INTEL_GEN(dev_priv) >= 9)
  4631. skylake_pfit_enable(intel_crtc);
  4632. else
  4633. ironlake_pfit_enable(intel_crtc);
  4634. /*
  4635. * On ILK+ LUT must be loaded before the pipe is running but with
  4636. * clocks enabled
  4637. */
  4638. intel_color_load_luts(&pipe_config->base);
  4639. intel_ddi_set_pipe_settings(crtc);
  4640. if (!transcoder_is_dsi(cpu_transcoder))
  4641. intel_ddi_enable_transcoder_func(crtc);
  4642. if (dev_priv->display.initial_watermarks != NULL)
  4643. dev_priv->display.initial_watermarks(old_intel_state,
  4644. pipe_config);
  4645. else
  4646. intel_update_watermarks(intel_crtc);
  4647. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4648. if (!transcoder_is_dsi(cpu_transcoder))
  4649. intel_enable_pipe(intel_crtc);
  4650. if (intel_crtc->config->has_pch_encoder)
  4651. lpt_pch_enable(crtc);
  4652. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4653. intel_ddi_set_vc_payload_alloc(crtc, true);
  4654. assert_vblank_disabled(crtc);
  4655. drm_crtc_vblank_on(crtc);
  4656. intel_encoders_enable(crtc, pipe_config, old_state);
  4657. if (intel_crtc->config->has_pch_encoder) {
  4658. intel_wait_for_vblank(dev_priv, pipe);
  4659. intel_wait_for_vblank(dev_priv, pipe);
  4660. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4661. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4662. true);
  4663. }
  4664. /* If we change the relative order between pipe/planes enabling, we need
  4665. * to change the workaround. */
  4666. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4667. if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
  4668. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4669. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4670. }
  4671. }
  4672. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4673. {
  4674. struct drm_device *dev = crtc->base.dev;
  4675. struct drm_i915_private *dev_priv = to_i915(dev);
  4676. int pipe = crtc->pipe;
  4677. /* To avoid upsetting the power well on haswell only disable the pfit if
  4678. * it's in use. The hw state code will make sure we get this right. */
  4679. if (force || crtc->config->pch_pfit.enabled) {
  4680. I915_WRITE(PF_CTL(pipe), 0);
  4681. I915_WRITE(PF_WIN_POS(pipe), 0);
  4682. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4683. }
  4684. }
  4685. static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4686. struct drm_atomic_state *old_state)
  4687. {
  4688. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4689. struct drm_device *dev = crtc->dev;
  4690. struct drm_i915_private *dev_priv = to_i915(dev);
  4691. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4692. int pipe = intel_crtc->pipe;
  4693. /*
  4694. * Sometimes spurious CPU pipe underruns happen when the
  4695. * pipe is already disabled, but FDI RX/TX is still enabled.
  4696. * Happens at least with VGA+HDMI cloning. Suppress them.
  4697. */
  4698. if (intel_crtc->config->has_pch_encoder) {
  4699. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4700. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4701. }
  4702. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4703. drm_crtc_vblank_off(crtc);
  4704. assert_vblank_disabled(crtc);
  4705. intel_disable_pipe(intel_crtc);
  4706. ironlake_pfit_disable(intel_crtc, false);
  4707. if (intel_crtc->config->has_pch_encoder)
  4708. ironlake_fdi_disable(crtc);
  4709. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4710. if (intel_crtc->config->has_pch_encoder) {
  4711. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4712. if (HAS_PCH_CPT(dev_priv)) {
  4713. i915_reg_t reg;
  4714. u32 temp;
  4715. /* disable TRANS_DP_CTL */
  4716. reg = TRANS_DP_CTL(pipe);
  4717. temp = I915_READ(reg);
  4718. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4719. TRANS_DP_PORT_SEL_MASK);
  4720. temp |= TRANS_DP_PORT_SEL_NONE;
  4721. I915_WRITE(reg, temp);
  4722. /* disable DPLL_SEL */
  4723. temp = I915_READ(PCH_DPLL_SEL);
  4724. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4725. I915_WRITE(PCH_DPLL_SEL, temp);
  4726. }
  4727. ironlake_fdi_pll_disable(intel_crtc);
  4728. }
  4729. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4730. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4731. }
  4732. static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4733. struct drm_atomic_state *old_state)
  4734. {
  4735. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4736. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4737. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4738. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4739. if (intel_crtc->config->has_pch_encoder)
  4740. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4741. false);
  4742. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4743. drm_crtc_vblank_off(crtc);
  4744. assert_vblank_disabled(crtc);
  4745. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4746. if (!transcoder_is_dsi(cpu_transcoder))
  4747. intel_disable_pipe(intel_crtc);
  4748. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4749. intel_ddi_set_vc_payload_alloc(crtc, false);
  4750. if (!transcoder_is_dsi(cpu_transcoder))
  4751. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4752. if (INTEL_GEN(dev_priv) >= 9)
  4753. skylake_scaler_disable(intel_crtc);
  4754. else
  4755. ironlake_pfit_disable(intel_crtc, false);
  4756. if (!transcoder_is_dsi(cpu_transcoder))
  4757. intel_ddi_disable_pipe_clock(intel_crtc);
  4758. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4759. if (old_crtc_state->has_pch_encoder)
  4760. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4761. true);
  4762. }
  4763. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4764. {
  4765. struct drm_device *dev = crtc->base.dev;
  4766. struct drm_i915_private *dev_priv = to_i915(dev);
  4767. struct intel_crtc_state *pipe_config = crtc->config;
  4768. if (!pipe_config->gmch_pfit.control)
  4769. return;
  4770. /*
  4771. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4772. * according to register description and PRM.
  4773. */
  4774. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4775. assert_pipe_disabled(dev_priv, crtc->pipe);
  4776. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4777. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4778. /* Border color in case we don't scale up to the full screen. Black by
  4779. * default, change to something else for debugging. */
  4780. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4781. }
  4782. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4783. {
  4784. switch (port) {
  4785. case PORT_A:
  4786. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4787. case PORT_B:
  4788. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4789. case PORT_C:
  4790. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4791. case PORT_D:
  4792. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4793. case PORT_E:
  4794. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4795. default:
  4796. MISSING_CASE(port);
  4797. return POWER_DOMAIN_PORT_OTHER;
  4798. }
  4799. }
  4800. static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
  4801. {
  4802. switch (port) {
  4803. case PORT_A:
  4804. return POWER_DOMAIN_AUX_A;
  4805. case PORT_B:
  4806. return POWER_DOMAIN_AUX_B;
  4807. case PORT_C:
  4808. return POWER_DOMAIN_AUX_C;
  4809. case PORT_D:
  4810. return POWER_DOMAIN_AUX_D;
  4811. case PORT_E:
  4812. /* FIXME: Check VBT for actual wiring of PORT E */
  4813. return POWER_DOMAIN_AUX_D;
  4814. default:
  4815. MISSING_CASE(port);
  4816. return POWER_DOMAIN_AUX_A;
  4817. }
  4818. }
  4819. enum intel_display_power_domain
  4820. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4821. {
  4822. struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
  4823. struct intel_digital_port *intel_dig_port;
  4824. switch (intel_encoder->type) {
  4825. case INTEL_OUTPUT_UNKNOWN:
  4826. /* Only DDI platforms should ever use this output type */
  4827. WARN_ON_ONCE(!HAS_DDI(dev_priv));
  4828. case INTEL_OUTPUT_DP:
  4829. case INTEL_OUTPUT_HDMI:
  4830. case INTEL_OUTPUT_EDP:
  4831. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4832. return port_to_power_domain(intel_dig_port->port);
  4833. case INTEL_OUTPUT_DP_MST:
  4834. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4835. return port_to_power_domain(intel_dig_port->port);
  4836. case INTEL_OUTPUT_ANALOG:
  4837. return POWER_DOMAIN_PORT_CRT;
  4838. case INTEL_OUTPUT_DSI:
  4839. return POWER_DOMAIN_PORT_DSI;
  4840. default:
  4841. return POWER_DOMAIN_PORT_OTHER;
  4842. }
  4843. }
  4844. enum intel_display_power_domain
  4845. intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
  4846. {
  4847. struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
  4848. struct intel_digital_port *intel_dig_port;
  4849. switch (intel_encoder->type) {
  4850. case INTEL_OUTPUT_UNKNOWN:
  4851. case INTEL_OUTPUT_HDMI:
  4852. /*
  4853. * Only DDI platforms should ever use these output types.
  4854. * We can get here after the HDMI detect code has already set
  4855. * the type of the shared encoder. Since we can't be sure
  4856. * what's the status of the given connectors, play safe and
  4857. * run the DP detection too.
  4858. */
  4859. WARN_ON_ONCE(!HAS_DDI(dev_priv));
  4860. case INTEL_OUTPUT_DP:
  4861. case INTEL_OUTPUT_EDP:
  4862. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4863. return port_to_aux_power_domain(intel_dig_port->port);
  4864. case INTEL_OUTPUT_DP_MST:
  4865. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4866. return port_to_aux_power_domain(intel_dig_port->port);
  4867. default:
  4868. MISSING_CASE(intel_encoder->type);
  4869. return POWER_DOMAIN_AUX_A;
  4870. }
  4871. }
  4872. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
  4873. struct intel_crtc_state *crtc_state)
  4874. {
  4875. struct drm_device *dev = crtc->dev;
  4876. struct drm_encoder *encoder;
  4877. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4878. enum pipe pipe = intel_crtc->pipe;
  4879. unsigned long mask;
  4880. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4881. if (!crtc_state->base.active)
  4882. return 0;
  4883. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4884. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4885. if (crtc_state->pch_pfit.enabled ||
  4886. crtc_state->pch_pfit.force_thru)
  4887. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4888. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4889. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4890. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4891. }
  4892. if (crtc_state->shared_dpll)
  4893. mask |= BIT(POWER_DOMAIN_PLLS);
  4894. return mask;
  4895. }
  4896. static unsigned long
  4897. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4898. struct intel_crtc_state *crtc_state)
  4899. {
  4900. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4901. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4902. enum intel_display_power_domain domain;
  4903. unsigned long domains, new_domains, old_domains;
  4904. old_domains = intel_crtc->enabled_power_domains;
  4905. intel_crtc->enabled_power_domains = new_domains =
  4906. get_crtc_power_domains(crtc, crtc_state);
  4907. domains = new_domains & ~old_domains;
  4908. for_each_power_domain(domain, domains)
  4909. intel_display_power_get(dev_priv, domain);
  4910. return old_domains & ~new_domains;
  4911. }
  4912. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4913. unsigned long domains)
  4914. {
  4915. enum intel_display_power_domain domain;
  4916. for_each_power_domain(domain, domains)
  4917. intel_display_power_put(dev_priv, domain);
  4918. }
  4919. static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
  4920. {
  4921. int max_cdclk_freq = dev_priv->max_cdclk_freq;
  4922. if (INTEL_INFO(dev_priv)->gen >= 9 ||
  4923. IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  4924. return max_cdclk_freq;
  4925. else if (IS_CHERRYVIEW(dev_priv))
  4926. return max_cdclk_freq*95/100;
  4927. else if (INTEL_INFO(dev_priv)->gen < 4)
  4928. return 2*max_cdclk_freq*90/100;
  4929. else
  4930. return max_cdclk_freq*90/100;
  4931. }
  4932. static int skl_calc_cdclk(int max_pixclk, int vco);
  4933. static void intel_update_max_cdclk(struct drm_i915_private *dev_priv)
  4934. {
  4935. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
  4936. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4937. int max_cdclk, vco;
  4938. vco = dev_priv->skl_preferred_vco_freq;
  4939. WARN_ON(vco != 8100000 && vco != 8640000);
  4940. /*
  4941. * Use the lower (vco 8640) cdclk values as a
  4942. * first guess. skl_calc_cdclk() will correct it
  4943. * if the preferred vco is 8100 instead.
  4944. */
  4945. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4946. max_cdclk = 617143;
  4947. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4948. max_cdclk = 540000;
  4949. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4950. max_cdclk = 432000;
  4951. else
  4952. max_cdclk = 308571;
  4953. dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
  4954. } else if (IS_BROXTON(dev_priv)) {
  4955. dev_priv->max_cdclk_freq = 624000;
  4956. } else if (IS_BROADWELL(dev_priv)) {
  4957. /*
  4958. * FIXME with extra cooling we can allow
  4959. * 540 MHz for ULX and 675 Mhz for ULT.
  4960. * How can we know if extra cooling is
  4961. * available? PCI ID, VTB, something else?
  4962. */
  4963. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4964. dev_priv->max_cdclk_freq = 450000;
  4965. else if (IS_BDW_ULX(dev_priv))
  4966. dev_priv->max_cdclk_freq = 450000;
  4967. else if (IS_BDW_ULT(dev_priv))
  4968. dev_priv->max_cdclk_freq = 540000;
  4969. else
  4970. dev_priv->max_cdclk_freq = 675000;
  4971. } else if (IS_CHERRYVIEW(dev_priv)) {
  4972. dev_priv->max_cdclk_freq = 320000;
  4973. } else if (IS_VALLEYVIEW(dev_priv)) {
  4974. dev_priv->max_cdclk_freq = 400000;
  4975. } else {
  4976. /* otherwise assume cdclk is fixed */
  4977. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4978. }
  4979. dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
  4980. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4981. dev_priv->max_cdclk_freq);
  4982. DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
  4983. dev_priv->max_dotclk_freq);
  4984. }
  4985. static void intel_update_cdclk(struct drm_i915_private *dev_priv)
  4986. {
  4987. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev_priv);
  4988. if (INTEL_GEN(dev_priv) >= 9)
  4989. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
  4990. dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
  4991. dev_priv->cdclk_pll.ref);
  4992. else
  4993. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4994. dev_priv->cdclk_freq);
  4995. /*
  4996. * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
  4997. * Programmng [sic] note: bit[9:2] should be programmed to the number
  4998. * of cdclk that generates 4MHz reference clock freq which is used to
  4999. * generate GMBus clock. This will vary with the cdclk freq.
  5000. */
  5001. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  5002. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  5003. }
  5004. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  5005. static int skl_cdclk_decimal(int cdclk)
  5006. {
  5007. return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
  5008. }
  5009. static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
  5010. {
  5011. int ratio;
  5012. if (cdclk == dev_priv->cdclk_pll.ref)
  5013. return 0;
  5014. switch (cdclk) {
  5015. default:
  5016. MISSING_CASE(cdclk);
  5017. case 144000:
  5018. case 288000:
  5019. case 384000:
  5020. case 576000:
  5021. ratio = 60;
  5022. break;
  5023. case 624000:
  5024. ratio = 65;
  5025. break;
  5026. }
  5027. return dev_priv->cdclk_pll.ref * ratio;
  5028. }
  5029. static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
  5030. {
  5031. I915_WRITE(BXT_DE_PLL_ENABLE, 0);
  5032. /* Timeout 200us */
  5033. if (intel_wait_for_register(dev_priv,
  5034. BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
  5035. 1))
  5036. DRM_ERROR("timeout waiting for DE PLL unlock\n");
  5037. dev_priv->cdclk_pll.vco = 0;
  5038. }
  5039. static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
  5040. {
  5041. int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
  5042. u32 val;
  5043. val = I915_READ(BXT_DE_PLL_CTL);
  5044. val &= ~BXT_DE_PLL_RATIO_MASK;
  5045. val |= BXT_DE_PLL_RATIO(ratio);
  5046. I915_WRITE(BXT_DE_PLL_CTL, val);
  5047. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  5048. /* Timeout 200us */
  5049. if (intel_wait_for_register(dev_priv,
  5050. BXT_DE_PLL_ENABLE,
  5051. BXT_DE_PLL_LOCK,
  5052. BXT_DE_PLL_LOCK,
  5053. 1))
  5054. DRM_ERROR("timeout waiting for DE PLL lock\n");
  5055. dev_priv->cdclk_pll.vco = vco;
  5056. }
  5057. static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
  5058. {
  5059. u32 val, divider;
  5060. int vco, ret;
  5061. vco = bxt_de_pll_vco(dev_priv, cdclk);
  5062. DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
  5063. /* cdclk = vco / 2 / div{1,1.5,2,4} */
  5064. switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
  5065. case 8:
  5066. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  5067. break;
  5068. case 4:
  5069. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  5070. break;
  5071. case 3:
  5072. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  5073. break;
  5074. case 2:
  5075. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  5076. break;
  5077. default:
  5078. WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
  5079. WARN_ON(vco != 0);
  5080. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  5081. break;
  5082. }
  5083. /* Inform power controller of upcoming frequency change */
  5084. mutex_lock(&dev_priv->rps.hw_lock);
  5085. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  5086. 0x80000000);
  5087. mutex_unlock(&dev_priv->rps.hw_lock);
  5088. if (ret) {
  5089. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  5090. ret, cdclk);
  5091. return;
  5092. }
  5093. if (dev_priv->cdclk_pll.vco != 0 &&
  5094. dev_priv->cdclk_pll.vco != vco)
  5095. bxt_de_pll_disable(dev_priv);
  5096. if (dev_priv->cdclk_pll.vco != vco)
  5097. bxt_de_pll_enable(dev_priv, vco);
  5098. val = divider | skl_cdclk_decimal(cdclk);
  5099. /*
  5100. * FIXME if only the cd2x divider needs changing, it could be done
  5101. * without shutting off the pipe (if only one pipe is active).
  5102. */
  5103. val |= BXT_CDCLK_CD2X_PIPE_NONE;
  5104. /*
  5105. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  5106. * enable otherwise.
  5107. */
  5108. if (cdclk >= 500000)
  5109. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  5110. I915_WRITE(CDCLK_CTL, val);
  5111. mutex_lock(&dev_priv->rps.hw_lock);
  5112. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  5113. DIV_ROUND_UP(cdclk, 25000));
  5114. mutex_unlock(&dev_priv->rps.hw_lock);
  5115. if (ret) {
  5116. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  5117. ret, cdclk);
  5118. return;
  5119. }
  5120. intel_update_cdclk(dev_priv);
  5121. }
  5122. static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
  5123. {
  5124. u32 cdctl, expected;
  5125. intel_update_cdclk(dev_priv);
  5126. if (dev_priv->cdclk_pll.vco == 0 ||
  5127. dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
  5128. goto sanitize;
  5129. /* DPLL okay; verify the cdclock
  5130. *
  5131. * Some BIOS versions leave an incorrect decimal frequency value and
  5132. * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
  5133. * so sanitize this register.
  5134. */
  5135. cdctl = I915_READ(CDCLK_CTL);
  5136. /*
  5137. * Let's ignore the pipe field, since BIOS could have configured the
  5138. * dividers both synching to an active pipe, or asynchronously
  5139. * (PIPE_NONE).
  5140. */
  5141. cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
  5142. expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
  5143. skl_cdclk_decimal(dev_priv->cdclk_freq);
  5144. /*
  5145. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  5146. * enable otherwise.
  5147. */
  5148. if (dev_priv->cdclk_freq >= 500000)
  5149. expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  5150. if (cdctl == expected)
  5151. /* All well; nothing to sanitize */
  5152. return;
  5153. sanitize:
  5154. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  5155. /* force cdclk programming */
  5156. dev_priv->cdclk_freq = 0;
  5157. /* force full PLL disable + enable */
  5158. dev_priv->cdclk_pll.vco = -1;
  5159. }
  5160. void bxt_init_cdclk(struct drm_i915_private *dev_priv)
  5161. {
  5162. bxt_sanitize_cdclk(dev_priv);
  5163. if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
  5164. return;
  5165. /*
  5166. * FIXME:
  5167. * - The initial CDCLK needs to be read from VBT.
  5168. * Need to make this change after VBT has changes for BXT.
  5169. */
  5170. bxt_set_cdclk(dev_priv, bxt_calc_cdclk(0));
  5171. }
  5172. void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
  5173. {
  5174. bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
  5175. }
  5176. static int skl_calc_cdclk(int max_pixclk, int vco)
  5177. {
  5178. if (vco == 8640000) {
  5179. if (max_pixclk > 540000)
  5180. return 617143;
  5181. else if (max_pixclk > 432000)
  5182. return 540000;
  5183. else if (max_pixclk > 308571)
  5184. return 432000;
  5185. else
  5186. return 308571;
  5187. } else {
  5188. if (max_pixclk > 540000)
  5189. return 675000;
  5190. else if (max_pixclk > 450000)
  5191. return 540000;
  5192. else if (max_pixclk > 337500)
  5193. return 450000;
  5194. else
  5195. return 337500;
  5196. }
  5197. }
  5198. static void
  5199. skl_dpll0_update(struct drm_i915_private *dev_priv)
  5200. {
  5201. u32 val;
  5202. dev_priv->cdclk_pll.ref = 24000;
  5203. dev_priv->cdclk_pll.vco = 0;
  5204. val = I915_READ(LCPLL1_CTL);
  5205. if ((val & LCPLL_PLL_ENABLE) == 0)
  5206. return;
  5207. if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
  5208. return;
  5209. val = I915_READ(DPLL_CTRL1);
  5210. if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
  5211. DPLL_CTRL1_SSC(SKL_DPLL0) |
  5212. DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
  5213. DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
  5214. return;
  5215. switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
  5216. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
  5217. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
  5218. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
  5219. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
  5220. dev_priv->cdclk_pll.vco = 8100000;
  5221. break;
  5222. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
  5223. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
  5224. dev_priv->cdclk_pll.vco = 8640000;
  5225. break;
  5226. default:
  5227. MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  5228. break;
  5229. }
  5230. }
  5231. void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
  5232. {
  5233. bool changed = dev_priv->skl_preferred_vco_freq != vco;
  5234. dev_priv->skl_preferred_vco_freq = vco;
  5235. if (changed)
  5236. intel_update_max_cdclk(dev_priv);
  5237. }
  5238. static void
  5239. skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
  5240. {
  5241. int min_cdclk = skl_calc_cdclk(0, vco);
  5242. u32 val;
  5243. WARN_ON(vco != 8100000 && vco != 8640000);
  5244. /* select the minimum CDCLK before enabling DPLL 0 */
  5245. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
  5246. I915_WRITE(CDCLK_CTL, val);
  5247. POSTING_READ(CDCLK_CTL);
  5248. /*
  5249. * We always enable DPLL0 with the lowest link rate possible, but still
  5250. * taking into account the VCO required to operate the eDP panel at the
  5251. * desired frequency. The usual DP link rates operate with a VCO of
  5252. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  5253. * The modeset code is responsible for the selection of the exact link
  5254. * rate later on, with the constraint of choosing a frequency that
  5255. * works with vco.
  5256. */
  5257. val = I915_READ(DPLL_CTRL1);
  5258. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  5259. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  5260. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  5261. if (vco == 8640000)
  5262. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  5263. SKL_DPLL0);
  5264. else
  5265. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  5266. SKL_DPLL0);
  5267. I915_WRITE(DPLL_CTRL1, val);
  5268. POSTING_READ(DPLL_CTRL1);
  5269. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  5270. if (intel_wait_for_register(dev_priv,
  5271. LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  5272. 5))
  5273. DRM_ERROR("DPLL0 not locked\n");
  5274. dev_priv->cdclk_pll.vco = vco;
  5275. /* We'll want to keep using the current vco from now on. */
  5276. skl_set_preferred_cdclk_vco(dev_priv, vco);
  5277. }
  5278. static void
  5279. skl_dpll0_disable(struct drm_i915_private *dev_priv)
  5280. {
  5281. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  5282. if (intel_wait_for_register(dev_priv,
  5283. LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
  5284. 1))
  5285. DRM_ERROR("Couldn't disable DPLL0\n");
  5286. dev_priv->cdclk_pll.vco = 0;
  5287. }
  5288. static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
  5289. {
  5290. u32 freq_select, pcu_ack;
  5291. int ret;
  5292. WARN_ON((cdclk == 24000) != (vco == 0));
  5293. DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
  5294. mutex_lock(&dev_priv->rps.hw_lock);
  5295. ret = skl_pcode_request(dev_priv, SKL_PCODE_CDCLK_CONTROL,
  5296. SKL_CDCLK_PREPARE_FOR_CHANGE,
  5297. SKL_CDCLK_READY_FOR_CHANGE,
  5298. SKL_CDCLK_READY_FOR_CHANGE, 3);
  5299. mutex_unlock(&dev_priv->rps.hw_lock);
  5300. if (ret) {
  5301. DRM_ERROR("Failed to inform PCU about cdclk change (%d)\n",
  5302. ret);
  5303. return;
  5304. }
  5305. /* set CDCLK_CTL */
  5306. switch (cdclk) {
  5307. case 450000:
  5308. case 432000:
  5309. freq_select = CDCLK_FREQ_450_432;
  5310. pcu_ack = 1;
  5311. break;
  5312. case 540000:
  5313. freq_select = CDCLK_FREQ_540;
  5314. pcu_ack = 2;
  5315. break;
  5316. case 308571:
  5317. case 337500:
  5318. default:
  5319. freq_select = CDCLK_FREQ_337_308;
  5320. pcu_ack = 0;
  5321. break;
  5322. case 617143:
  5323. case 675000:
  5324. freq_select = CDCLK_FREQ_675_617;
  5325. pcu_ack = 3;
  5326. break;
  5327. }
  5328. if (dev_priv->cdclk_pll.vco != 0 &&
  5329. dev_priv->cdclk_pll.vco != vco)
  5330. skl_dpll0_disable(dev_priv);
  5331. if (dev_priv->cdclk_pll.vco != vco)
  5332. skl_dpll0_enable(dev_priv, vco);
  5333. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
  5334. POSTING_READ(CDCLK_CTL);
  5335. /* inform PCU of the change */
  5336. mutex_lock(&dev_priv->rps.hw_lock);
  5337. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  5338. mutex_unlock(&dev_priv->rps.hw_lock);
  5339. intel_update_cdclk(dev_priv);
  5340. }
  5341. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
  5342. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  5343. {
  5344. skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
  5345. }
  5346. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  5347. {
  5348. int cdclk, vco;
  5349. skl_sanitize_cdclk(dev_priv);
  5350. if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
  5351. /*
  5352. * Use the current vco as our initial
  5353. * guess as to what the preferred vco is.
  5354. */
  5355. if (dev_priv->skl_preferred_vco_freq == 0)
  5356. skl_set_preferred_cdclk_vco(dev_priv,
  5357. dev_priv->cdclk_pll.vco);
  5358. return;
  5359. }
  5360. vco = dev_priv->skl_preferred_vco_freq;
  5361. if (vco == 0)
  5362. vco = 8100000;
  5363. cdclk = skl_calc_cdclk(0, vco);
  5364. skl_set_cdclk(dev_priv, cdclk, vco);
  5365. }
  5366. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
  5367. {
  5368. uint32_t cdctl, expected;
  5369. /*
  5370. * check if the pre-os intialized the display
  5371. * There is SWF18 scratchpad register defined which is set by the
  5372. * pre-os which can be used by the OS drivers to check the status
  5373. */
  5374. if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
  5375. goto sanitize;
  5376. intel_update_cdclk(dev_priv);
  5377. /* Is PLL enabled and locked ? */
  5378. if (dev_priv->cdclk_pll.vco == 0 ||
  5379. dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
  5380. goto sanitize;
  5381. /* DPLL okay; verify the cdclock
  5382. *
  5383. * Noticed in some instances that the freq selection is correct but
  5384. * decimal part is programmed wrong from BIOS where pre-os does not
  5385. * enable display. Verify the same as well.
  5386. */
  5387. cdctl = I915_READ(CDCLK_CTL);
  5388. expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
  5389. skl_cdclk_decimal(dev_priv->cdclk_freq);
  5390. if (cdctl == expected)
  5391. /* All well; nothing to sanitize */
  5392. return;
  5393. sanitize:
  5394. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  5395. /* force cdclk programming */
  5396. dev_priv->cdclk_freq = 0;
  5397. /* force full PLL disable + enable */
  5398. dev_priv->cdclk_pll.vco = -1;
  5399. }
  5400. /* Adjust CDclk dividers to allow high res or save power if possible */
  5401. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  5402. {
  5403. struct drm_i915_private *dev_priv = to_i915(dev);
  5404. u32 val, cmd;
  5405. WARN_ON(dev_priv->display.get_display_clock_speed(dev_priv)
  5406. != dev_priv->cdclk_freq);
  5407. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  5408. cmd = 2;
  5409. else if (cdclk == 266667)
  5410. cmd = 1;
  5411. else
  5412. cmd = 0;
  5413. mutex_lock(&dev_priv->rps.hw_lock);
  5414. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  5415. val &= ~DSPFREQGUAR_MASK;
  5416. val |= (cmd << DSPFREQGUAR_SHIFT);
  5417. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  5418. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  5419. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  5420. 50)) {
  5421. DRM_ERROR("timed out waiting for CDclk change\n");
  5422. }
  5423. mutex_unlock(&dev_priv->rps.hw_lock);
  5424. mutex_lock(&dev_priv->sb_lock);
  5425. if (cdclk == 400000) {
  5426. u32 divider;
  5427. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  5428. /* adjust cdclk divider */
  5429. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  5430. val &= ~CCK_FREQUENCY_VALUES;
  5431. val |= divider;
  5432. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  5433. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  5434. CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
  5435. 50))
  5436. DRM_ERROR("timed out waiting for CDclk change\n");
  5437. }
  5438. /* adjust self-refresh exit latency value */
  5439. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  5440. val &= ~0x7f;
  5441. /*
  5442. * For high bandwidth configs, we set a higher latency in the bunit
  5443. * so that the core display fetch happens in time to avoid underruns.
  5444. */
  5445. if (cdclk == 400000)
  5446. val |= 4500 / 250; /* 4.5 usec */
  5447. else
  5448. val |= 3000 / 250; /* 3.0 usec */
  5449. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  5450. mutex_unlock(&dev_priv->sb_lock);
  5451. intel_update_cdclk(dev_priv);
  5452. }
  5453. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  5454. {
  5455. struct drm_i915_private *dev_priv = to_i915(dev);
  5456. u32 val, cmd;
  5457. WARN_ON(dev_priv->display.get_display_clock_speed(dev_priv)
  5458. != dev_priv->cdclk_freq);
  5459. switch (cdclk) {
  5460. case 333333:
  5461. case 320000:
  5462. case 266667:
  5463. case 200000:
  5464. break;
  5465. default:
  5466. MISSING_CASE(cdclk);
  5467. return;
  5468. }
  5469. /*
  5470. * Specs are full of misinformation, but testing on actual
  5471. * hardware has shown that we just need to write the desired
  5472. * CCK divider into the Punit register.
  5473. */
  5474. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  5475. mutex_lock(&dev_priv->rps.hw_lock);
  5476. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  5477. val &= ~DSPFREQGUAR_MASK_CHV;
  5478. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  5479. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  5480. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  5481. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  5482. 50)) {
  5483. DRM_ERROR("timed out waiting for CDclk change\n");
  5484. }
  5485. mutex_unlock(&dev_priv->rps.hw_lock);
  5486. intel_update_cdclk(dev_priv);
  5487. }
  5488. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  5489. int max_pixclk)
  5490. {
  5491. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  5492. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  5493. /*
  5494. * Really only a few cases to deal with, as only 4 CDclks are supported:
  5495. * 200MHz
  5496. * 267MHz
  5497. * 320/333MHz (depends on HPLL freq)
  5498. * 400MHz (VLV only)
  5499. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  5500. * of the lower bin and adjust if needed.
  5501. *
  5502. * We seem to get an unstable or solid color picture at 200MHz.
  5503. * Not sure what's wrong. For now use 200MHz only when all pipes
  5504. * are off.
  5505. */
  5506. if (!IS_CHERRYVIEW(dev_priv) &&
  5507. max_pixclk > freq_320*limit/100)
  5508. return 400000;
  5509. else if (max_pixclk > 266667*limit/100)
  5510. return freq_320;
  5511. else if (max_pixclk > 0)
  5512. return 266667;
  5513. else
  5514. return 200000;
  5515. }
  5516. static int bxt_calc_cdclk(int max_pixclk)
  5517. {
  5518. if (max_pixclk > 576000)
  5519. return 624000;
  5520. else if (max_pixclk > 384000)
  5521. return 576000;
  5522. else if (max_pixclk > 288000)
  5523. return 384000;
  5524. else if (max_pixclk > 144000)
  5525. return 288000;
  5526. else
  5527. return 144000;
  5528. }
  5529. /* Compute the max pixel clock for new configuration. */
  5530. static int intel_mode_max_pixclk(struct drm_device *dev,
  5531. struct drm_atomic_state *state)
  5532. {
  5533. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  5534. struct drm_i915_private *dev_priv = to_i915(dev);
  5535. struct drm_crtc *crtc;
  5536. struct drm_crtc_state *crtc_state;
  5537. unsigned max_pixclk = 0, i;
  5538. enum pipe pipe;
  5539. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  5540. sizeof(intel_state->min_pixclk));
  5541. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  5542. int pixclk = 0;
  5543. if (crtc_state->enable)
  5544. pixclk = crtc_state->adjusted_mode.crtc_clock;
  5545. intel_state->min_pixclk[i] = pixclk;
  5546. }
  5547. for_each_pipe(dev_priv, pipe)
  5548. max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
  5549. return max_pixclk;
  5550. }
  5551. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  5552. {
  5553. struct drm_device *dev = state->dev;
  5554. struct drm_i915_private *dev_priv = to_i915(dev);
  5555. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5556. struct intel_atomic_state *intel_state =
  5557. to_intel_atomic_state(state);
  5558. intel_state->cdclk = intel_state->dev_cdclk =
  5559. valleyview_calc_cdclk(dev_priv, max_pixclk);
  5560. if (!intel_state->active_crtcs)
  5561. intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
  5562. return 0;
  5563. }
  5564. static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
  5565. {
  5566. int max_pixclk = ilk_max_pixel_rate(state);
  5567. struct intel_atomic_state *intel_state =
  5568. to_intel_atomic_state(state);
  5569. intel_state->cdclk = intel_state->dev_cdclk =
  5570. bxt_calc_cdclk(max_pixclk);
  5571. if (!intel_state->active_crtcs)
  5572. intel_state->dev_cdclk = bxt_calc_cdclk(0);
  5573. return 0;
  5574. }
  5575. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5576. {
  5577. unsigned int credits, default_credits;
  5578. if (IS_CHERRYVIEW(dev_priv))
  5579. default_credits = PFI_CREDIT(12);
  5580. else
  5581. default_credits = PFI_CREDIT(8);
  5582. if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
  5583. /* CHV suggested value is 31 or 63 */
  5584. if (IS_CHERRYVIEW(dev_priv))
  5585. credits = PFI_CREDIT_63;
  5586. else
  5587. credits = PFI_CREDIT(15);
  5588. } else {
  5589. credits = default_credits;
  5590. }
  5591. /*
  5592. * WA - write default credits before re-programming
  5593. * FIXME: should we also set the resend bit here?
  5594. */
  5595. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5596. default_credits);
  5597. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5598. credits | PFI_CREDIT_RESEND);
  5599. /*
  5600. * FIXME is this guaranteed to clear
  5601. * immediately or should we poll for it?
  5602. */
  5603. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5604. }
  5605. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5606. {
  5607. struct drm_device *dev = old_state->dev;
  5608. struct drm_i915_private *dev_priv = to_i915(dev);
  5609. struct intel_atomic_state *old_intel_state =
  5610. to_intel_atomic_state(old_state);
  5611. unsigned req_cdclk = old_intel_state->dev_cdclk;
  5612. /*
  5613. * FIXME: We can end up here with all power domains off, yet
  5614. * with a CDCLK frequency other than the minimum. To account
  5615. * for this take the PIPE-A power domain, which covers the HW
  5616. * blocks needed for the following programming. This can be
  5617. * removed once it's guaranteed that we get here either with
  5618. * the minimum CDCLK set, or the required power domains
  5619. * enabled.
  5620. */
  5621. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5622. if (IS_CHERRYVIEW(dev_priv))
  5623. cherryview_set_cdclk(dev, req_cdclk);
  5624. else
  5625. valleyview_set_cdclk(dev, req_cdclk);
  5626. vlv_program_pfi_credits(dev_priv);
  5627. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5628. }
  5629. static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
  5630. struct drm_atomic_state *old_state)
  5631. {
  5632. struct drm_crtc *crtc = pipe_config->base.crtc;
  5633. struct drm_device *dev = crtc->dev;
  5634. struct drm_i915_private *dev_priv = to_i915(dev);
  5635. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5636. int pipe = intel_crtc->pipe;
  5637. if (WARN_ON(intel_crtc->active))
  5638. return;
  5639. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5640. intel_dp_set_m_n(intel_crtc, M1_N1);
  5641. intel_set_pipe_timings(intel_crtc);
  5642. intel_set_pipe_src_size(intel_crtc);
  5643. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  5644. struct drm_i915_private *dev_priv = to_i915(dev);
  5645. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5646. I915_WRITE(CHV_CANVAS(pipe), 0);
  5647. }
  5648. i9xx_set_pipeconf(intel_crtc);
  5649. intel_crtc->active = true;
  5650. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5651. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  5652. if (IS_CHERRYVIEW(dev_priv)) {
  5653. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5654. chv_enable_pll(intel_crtc, intel_crtc->config);
  5655. } else {
  5656. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5657. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5658. }
  5659. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  5660. i9xx_pfit_enable(intel_crtc);
  5661. intel_color_load_luts(&pipe_config->base);
  5662. intel_update_watermarks(intel_crtc);
  5663. intel_enable_pipe(intel_crtc);
  5664. assert_vblank_disabled(crtc);
  5665. drm_crtc_vblank_on(crtc);
  5666. intel_encoders_enable(crtc, pipe_config, old_state);
  5667. }
  5668. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5669. {
  5670. struct drm_device *dev = crtc->base.dev;
  5671. struct drm_i915_private *dev_priv = to_i915(dev);
  5672. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5673. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5674. }
  5675. static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
  5676. struct drm_atomic_state *old_state)
  5677. {
  5678. struct drm_crtc *crtc = pipe_config->base.crtc;
  5679. struct drm_device *dev = crtc->dev;
  5680. struct drm_i915_private *dev_priv = to_i915(dev);
  5681. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5682. enum pipe pipe = intel_crtc->pipe;
  5683. if (WARN_ON(intel_crtc->active))
  5684. return;
  5685. i9xx_set_pll_dividers(intel_crtc);
  5686. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5687. intel_dp_set_m_n(intel_crtc, M1_N1);
  5688. intel_set_pipe_timings(intel_crtc);
  5689. intel_set_pipe_src_size(intel_crtc);
  5690. i9xx_set_pipeconf(intel_crtc);
  5691. intel_crtc->active = true;
  5692. if (!IS_GEN2(dev_priv))
  5693. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5694. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  5695. i9xx_enable_pll(intel_crtc);
  5696. i9xx_pfit_enable(intel_crtc);
  5697. intel_color_load_luts(&pipe_config->base);
  5698. intel_update_watermarks(intel_crtc);
  5699. intel_enable_pipe(intel_crtc);
  5700. assert_vblank_disabled(crtc);
  5701. drm_crtc_vblank_on(crtc);
  5702. intel_encoders_enable(crtc, pipe_config, old_state);
  5703. }
  5704. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5705. {
  5706. struct drm_device *dev = crtc->base.dev;
  5707. struct drm_i915_private *dev_priv = to_i915(dev);
  5708. if (!crtc->config->gmch_pfit.control)
  5709. return;
  5710. assert_pipe_disabled(dev_priv, crtc->pipe);
  5711. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5712. I915_READ(PFIT_CONTROL));
  5713. I915_WRITE(PFIT_CONTROL, 0);
  5714. }
  5715. static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
  5716. struct drm_atomic_state *old_state)
  5717. {
  5718. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  5719. struct drm_device *dev = crtc->dev;
  5720. struct drm_i915_private *dev_priv = to_i915(dev);
  5721. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5722. int pipe = intel_crtc->pipe;
  5723. /*
  5724. * On gen2 planes are double buffered but the pipe isn't, so we must
  5725. * wait for planes to fully turn off before disabling the pipe.
  5726. */
  5727. if (IS_GEN2(dev_priv))
  5728. intel_wait_for_vblank(dev_priv, pipe);
  5729. intel_encoders_disable(crtc, old_crtc_state, old_state);
  5730. drm_crtc_vblank_off(crtc);
  5731. assert_vblank_disabled(crtc);
  5732. intel_disable_pipe(intel_crtc);
  5733. i9xx_pfit_disable(intel_crtc);
  5734. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  5735. if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
  5736. if (IS_CHERRYVIEW(dev_priv))
  5737. chv_disable_pll(dev_priv, pipe);
  5738. else if (IS_VALLEYVIEW(dev_priv))
  5739. vlv_disable_pll(dev_priv, pipe);
  5740. else
  5741. i9xx_disable_pll(intel_crtc);
  5742. }
  5743. intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
  5744. if (!IS_GEN2(dev_priv))
  5745. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5746. }
  5747. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5748. {
  5749. struct intel_encoder *encoder;
  5750. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5751. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5752. enum intel_display_power_domain domain;
  5753. unsigned long domains;
  5754. struct drm_atomic_state *state;
  5755. struct intel_crtc_state *crtc_state;
  5756. int ret;
  5757. if (!intel_crtc->active)
  5758. return;
  5759. if (to_intel_plane_state(crtc->primary->state)->base.visible) {
  5760. WARN_ON(intel_crtc->flip_work);
  5761. intel_pre_disable_primary_noatomic(crtc);
  5762. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  5763. to_intel_plane_state(crtc->primary->state)->base.visible = false;
  5764. }
  5765. state = drm_atomic_state_alloc(crtc->dev);
  5766. if (!state) {
  5767. DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
  5768. crtc->base.id, crtc->name);
  5769. return;
  5770. }
  5771. state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
  5772. /* Everything's already locked, -EDEADLK can't happen. */
  5773. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  5774. ret = drm_atomic_add_affected_connectors(state, crtc);
  5775. WARN_ON(IS_ERR(crtc_state) || ret);
  5776. dev_priv->display.crtc_disable(crtc_state, state);
  5777. drm_atomic_state_put(state);
  5778. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  5779. crtc->base.id, crtc->name);
  5780. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  5781. crtc->state->active = false;
  5782. intel_crtc->active = false;
  5783. crtc->enabled = false;
  5784. crtc->state->connector_mask = 0;
  5785. crtc->state->encoder_mask = 0;
  5786. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  5787. encoder->base.crtc = NULL;
  5788. intel_fbc_disable(intel_crtc);
  5789. intel_update_watermarks(intel_crtc);
  5790. intel_disable_shared_dpll(intel_crtc);
  5791. domains = intel_crtc->enabled_power_domains;
  5792. for_each_power_domain(domain, domains)
  5793. intel_display_power_put(dev_priv, domain);
  5794. intel_crtc->enabled_power_domains = 0;
  5795. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5796. dev_priv->min_pixclk[intel_crtc->pipe] = 0;
  5797. }
  5798. /*
  5799. * turn all crtc's off, but do not adjust state
  5800. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5801. */
  5802. int intel_display_suspend(struct drm_device *dev)
  5803. {
  5804. struct drm_i915_private *dev_priv = to_i915(dev);
  5805. struct drm_atomic_state *state;
  5806. int ret;
  5807. state = drm_atomic_helper_suspend(dev);
  5808. ret = PTR_ERR_OR_ZERO(state);
  5809. if (ret)
  5810. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5811. else
  5812. dev_priv->modeset_restore_state = state;
  5813. return ret;
  5814. }
  5815. void intel_encoder_destroy(struct drm_encoder *encoder)
  5816. {
  5817. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5818. drm_encoder_cleanup(encoder);
  5819. kfree(intel_encoder);
  5820. }
  5821. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5822. * internal consistency). */
  5823. static void intel_connector_verify_state(struct intel_connector *connector)
  5824. {
  5825. struct drm_crtc *crtc = connector->base.state->crtc;
  5826. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5827. connector->base.base.id,
  5828. connector->base.name);
  5829. if (connector->get_hw_state(connector)) {
  5830. struct intel_encoder *encoder = connector->encoder;
  5831. struct drm_connector_state *conn_state = connector->base.state;
  5832. I915_STATE_WARN(!crtc,
  5833. "connector enabled without attached crtc\n");
  5834. if (!crtc)
  5835. return;
  5836. I915_STATE_WARN(!crtc->state->active,
  5837. "connector is active, but attached crtc isn't\n");
  5838. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5839. return;
  5840. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5841. "atomic encoder doesn't match attached encoder\n");
  5842. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5843. "attached encoder crtc differs from connector crtc\n");
  5844. } else {
  5845. I915_STATE_WARN(crtc && crtc->state->active,
  5846. "attached crtc is active, but connector isn't\n");
  5847. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5848. "best encoder set without crtc!\n");
  5849. }
  5850. }
  5851. int intel_connector_init(struct intel_connector *connector)
  5852. {
  5853. drm_atomic_helper_connector_reset(&connector->base);
  5854. if (!connector->base.state)
  5855. return -ENOMEM;
  5856. return 0;
  5857. }
  5858. struct intel_connector *intel_connector_alloc(void)
  5859. {
  5860. struct intel_connector *connector;
  5861. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5862. if (!connector)
  5863. return NULL;
  5864. if (intel_connector_init(connector) < 0) {
  5865. kfree(connector);
  5866. return NULL;
  5867. }
  5868. return connector;
  5869. }
  5870. /* Simple connector->get_hw_state implementation for encoders that support only
  5871. * one connector and no cloning and hence the encoder state determines the state
  5872. * of the connector. */
  5873. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5874. {
  5875. enum pipe pipe = 0;
  5876. struct intel_encoder *encoder = connector->encoder;
  5877. return encoder->get_hw_state(encoder, &pipe);
  5878. }
  5879. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5880. {
  5881. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5882. return crtc_state->fdi_lanes;
  5883. return 0;
  5884. }
  5885. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5886. struct intel_crtc_state *pipe_config)
  5887. {
  5888. struct drm_i915_private *dev_priv = to_i915(dev);
  5889. struct drm_atomic_state *state = pipe_config->base.state;
  5890. struct intel_crtc *other_crtc;
  5891. struct intel_crtc_state *other_crtc_state;
  5892. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5893. pipe_name(pipe), pipe_config->fdi_lanes);
  5894. if (pipe_config->fdi_lanes > 4) {
  5895. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5896. pipe_name(pipe), pipe_config->fdi_lanes);
  5897. return -EINVAL;
  5898. }
  5899. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  5900. if (pipe_config->fdi_lanes > 2) {
  5901. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5902. pipe_config->fdi_lanes);
  5903. return -EINVAL;
  5904. } else {
  5905. return 0;
  5906. }
  5907. }
  5908. if (INTEL_INFO(dev_priv)->num_pipes == 2)
  5909. return 0;
  5910. /* Ivybridge 3 pipe is really complicated */
  5911. switch (pipe) {
  5912. case PIPE_A:
  5913. return 0;
  5914. case PIPE_B:
  5915. if (pipe_config->fdi_lanes <= 2)
  5916. return 0;
  5917. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
  5918. other_crtc_state =
  5919. intel_atomic_get_crtc_state(state, other_crtc);
  5920. if (IS_ERR(other_crtc_state))
  5921. return PTR_ERR(other_crtc_state);
  5922. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5923. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5924. pipe_name(pipe), pipe_config->fdi_lanes);
  5925. return -EINVAL;
  5926. }
  5927. return 0;
  5928. case PIPE_C:
  5929. if (pipe_config->fdi_lanes > 2) {
  5930. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5931. pipe_name(pipe), pipe_config->fdi_lanes);
  5932. return -EINVAL;
  5933. }
  5934. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
  5935. other_crtc_state =
  5936. intel_atomic_get_crtc_state(state, other_crtc);
  5937. if (IS_ERR(other_crtc_state))
  5938. return PTR_ERR(other_crtc_state);
  5939. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5940. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5941. return -EINVAL;
  5942. }
  5943. return 0;
  5944. default:
  5945. BUG();
  5946. }
  5947. }
  5948. #define RETRY 1
  5949. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5950. struct intel_crtc_state *pipe_config)
  5951. {
  5952. struct drm_device *dev = intel_crtc->base.dev;
  5953. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5954. int lane, link_bw, fdi_dotclock, ret;
  5955. bool needs_recompute = false;
  5956. retry:
  5957. /* FDI is a binary signal running at ~2.7GHz, encoding
  5958. * each output octet as 10 bits. The actual frequency
  5959. * is stored as a divider into a 100MHz clock, and the
  5960. * mode pixel clock is stored in units of 1KHz.
  5961. * Hence the bw of each lane in terms of the mode signal
  5962. * is:
  5963. */
  5964. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5965. fdi_dotclock = adjusted_mode->crtc_clock;
  5966. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5967. pipe_config->pipe_bpp);
  5968. pipe_config->fdi_lanes = lane;
  5969. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5970. link_bw, &pipe_config->fdi_m_n);
  5971. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5972. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5973. pipe_config->pipe_bpp -= 2*3;
  5974. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5975. pipe_config->pipe_bpp);
  5976. needs_recompute = true;
  5977. pipe_config->bw_constrained = true;
  5978. goto retry;
  5979. }
  5980. if (needs_recompute)
  5981. return RETRY;
  5982. return ret;
  5983. }
  5984. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5985. struct intel_crtc_state *pipe_config)
  5986. {
  5987. if (pipe_config->pipe_bpp > 24)
  5988. return false;
  5989. /* HSW can handle pixel rate up to cdclk? */
  5990. if (IS_HASWELL(dev_priv))
  5991. return true;
  5992. /*
  5993. * We compare against max which means we must take
  5994. * the increased cdclk requirement into account when
  5995. * calculating the new cdclk.
  5996. *
  5997. * Should measure whether using a lower cdclk w/o IPS
  5998. */
  5999. return ilk_pipe_pixel_rate(pipe_config) <=
  6000. dev_priv->max_cdclk_freq * 95 / 100;
  6001. }
  6002. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  6003. struct intel_crtc_state *pipe_config)
  6004. {
  6005. struct drm_device *dev = crtc->base.dev;
  6006. struct drm_i915_private *dev_priv = to_i915(dev);
  6007. pipe_config->ips_enabled = i915.enable_ips &&
  6008. hsw_crtc_supports_ips(crtc) &&
  6009. pipe_config_supports_ips(dev_priv, pipe_config);
  6010. }
  6011. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  6012. {
  6013. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6014. /* GDG double wide on either pipe, otherwise pipe A only */
  6015. return INTEL_INFO(dev_priv)->gen < 4 &&
  6016. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  6017. }
  6018. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  6019. struct intel_crtc_state *pipe_config)
  6020. {
  6021. struct drm_device *dev = crtc->base.dev;
  6022. struct drm_i915_private *dev_priv = to_i915(dev);
  6023. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  6024. int clock_limit = dev_priv->max_dotclk_freq;
  6025. if (INTEL_GEN(dev_priv) < 4) {
  6026. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  6027. /*
  6028. * Enable double wide mode when the dot clock
  6029. * is > 90% of the (display) core speed.
  6030. */
  6031. if (intel_crtc_supports_double_wide(crtc) &&
  6032. adjusted_mode->crtc_clock > clock_limit) {
  6033. clock_limit = dev_priv->max_dotclk_freq;
  6034. pipe_config->double_wide = true;
  6035. }
  6036. }
  6037. if (adjusted_mode->crtc_clock > clock_limit) {
  6038. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  6039. adjusted_mode->crtc_clock, clock_limit,
  6040. yesno(pipe_config->double_wide));
  6041. return -EINVAL;
  6042. }
  6043. /*
  6044. * Pipe horizontal size must be even in:
  6045. * - DVO ganged mode
  6046. * - LVDS dual channel mode
  6047. * - Double wide pipe
  6048. */
  6049. if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  6050. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  6051. pipe_config->pipe_src_w &= ~1;
  6052. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  6053. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  6054. */
  6055. if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
  6056. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  6057. return -EINVAL;
  6058. if (HAS_IPS(dev_priv))
  6059. hsw_compute_ips_config(crtc, pipe_config);
  6060. if (pipe_config->has_pch_encoder)
  6061. return ironlake_fdi_compute_config(crtc, pipe_config);
  6062. return 0;
  6063. }
  6064. static int skylake_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6065. {
  6066. u32 cdctl;
  6067. skl_dpll0_update(dev_priv);
  6068. if (dev_priv->cdclk_pll.vco == 0)
  6069. return dev_priv->cdclk_pll.ref;
  6070. cdctl = I915_READ(CDCLK_CTL);
  6071. if (dev_priv->cdclk_pll.vco == 8640000) {
  6072. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  6073. case CDCLK_FREQ_450_432:
  6074. return 432000;
  6075. case CDCLK_FREQ_337_308:
  6076. return 308571;
  6077. case CDCLK_FREQ_540:
  6078. return 540000;
  6079. case CDCLK_FREQ_675_617:
  6080. return 617143;
  6081. default:
  6082. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  6083. }
  6084. } else {
  6085. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  6086. case CDCLK_FREQ_450_432:
  6087. return 450000;
  6088. case CDCLK_FREQ_337_308:
  6089. return 337500;
  6090. case CDCLK_FREQ_540:
  6091. return 540000;
  6092. case CDCLK_FREQ_675_617:
  6093. return 675000;
  6094. default:
  6095. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  6096. }
  6097. }
  6098. return dev_priv->cdclk_pll.ref;
  6099. }
  6100. static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
  6101. {
  6102. u32 val;
  6103. dev_priv->cdclk_pll.ref = 19200;
  6104. dev_priv->cdclk_pll.vco = 0;
  6105. val = I915_READ(BXT_DE_PLL_ENABLE);
  6106. if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
  6107. return;
  6108. if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
  6109. return;
  6110. val = I915_READ(BXT_DE_PLL_CTL);
  6111. dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
  6112. dev_priv->cdclk_pll.ref;
  6113. }
  6114. static int broxton_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6115. {
  6116. u32 divider;
  6117. int div, vco;
  6118. bxt_de_pll_update(dev_priv);
  6119. vco = dev_priv->cdclk_pll.vco;
  6120. if (vco == 0)
  6121. return dev_priv->cdclk_pll.ref;
  6122. divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
  6123. switch (divider) {
  6124. case BXT_CDCLK_CD2X_DIV_SEL_1:
  6125. div = 2;
  6126. break;
  6127. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  6128. div = 3;
  6129. break;
  6130. case BXT_CDCLK_CD2X_DIV_SEL_2:
  6131. div = 4;
  6132. break;
  6133. case BXT_CDCLK_CD2X_DIV_SEL_4:
  6134. div = 8;
  6135. break;
  6136. default:
  6137. MISSING_CASE(divider);
  6138. return dev_priv->cdclk_pll.ref;
  6139. }
  6140. return DIV_ROUND_CLOSEST(vco, div);
  6141. }
  6142. static int broadwell_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6143. {
  6144. uint32_t lcpll = I915_READ(LCPLL_CTL);
  6145. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  6146. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  6147. return 800000;
  6148. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  6149. return 450000;
  6150. else if (freq == LCPLL_CLK_FREQ_450)
  6151. return 450000;
  6152. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  6153. return 540000;
  6154. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  6155. return 337500;
  6156. else
  6157. return 675000;
  6158. }
  6159. static int haswell_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6160. {
  6161. uint32_t lcpll = I915_READ(LCPLL_CTL);
  6162. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  6163. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  6164. return 800000;
  6165. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  6166. return 450000;
  6167. else if (freq == LCPLL_CLK_FREQ_450)
  6168. return 450000;
  6169. else if (IS_HSW_ULT(dev_priv))
  6170. return 337500;
  6171. else
  6172. return 540000;
  6173. }
  6174. static int valleyview_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6175. {
  6176. return vlv_get_cck_clock_hpll(dev_priv, "cdclk",
  6177. CCK_DISPLAY_CLOCK_CONTROL);
  6178. }
  6179. static int ilk_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6180. {
  6181. return 450000;
  6182. }
  6183. static int i945_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6184. {
  6185. return 400000;
  6186. }
  6187. static int i915_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6188. {
  6189. return 333333;
  6190. }
  6191. static int i9xx_misc_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6192. {
  6193. return 200000;
  6194. }
  6195. static int pnv_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6196. {
  6197. struct pci_dev *pdev = dev_priv->drm.pdev;
  6198. u16 gcfgc = 0;
  6199. pci_read_config_word(pdev, GCFGC, &gcfgc);
  6200. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  6201. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  6202. return 266667;
  6203. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  6204. return 333333;
  6205. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  6206. return 444444;
  6207. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  6208. return 200000;
  6209. default:
  6210. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  6211. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  6212. return 133333;
  6213. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  6214. return 166667;
  6215. }
  6216. }
  6217. static int i915gm_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6218. {
  6219. struct pci_dev *pdev = dev_priv->drm.pdev;
  6220. u16 gcfgc = 0;
  6221. pci_read_config_word(pdev, GCFGC, &gcfgc);
  6222. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  6223. return 133333;
  6224. else {
  6225. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  6226. case GC_DISPLAY_CLOCK_333_MHZ:
  6227. return 333333;
  6228. default:
  6229. case GC_DISPLAY_CLOCK_190_200_MHZ:
  6230. return 190000;
  6231. }
  6232. }
  6233. }
  6234. static int i865_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6235. {
  6236. return 266667;
  6237. }
  6238. static int i85x_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6239. {
  6240. struct pci_dev *pdev = dev_priv->drm.pdev;
  6241. u16 hpllcc = 0;
  6242. /*
  6243. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  6244. * encoding is different :(
  6245. * FIXME is this the right way to detect 852GM/852GMV?
  6246. */
  6247. if (pdev->revision == 0x1)
  6248. return 133333;
  6249. pci_bus_read_config_word(pdev->bus,
  6250. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  6251. /* Assume that the hardware is in the high speed state. This
  6252. * should be the default.
  6253. */
  6254. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  6255. case GC_CLOCK_133_200:
  6256. case GC_CLOCK_133_200_2:
  6257. case GC_CLOCK_100_200:
  6258. return 200000;
  6259. case GC_CLOCK_166_250:
  6260. return 250000;
  6261. case GC_CLOCK_100_133:
  6262. return 133333;
  6263. case GC_CLOCK_133_266:
  6264. case GC_CLOCK_133_266_2:
  6265. case GC_CLOCK_166_266:
  6266. return 266667;
  6267. }
  6268. /* Shouldn't happen */
  6269. return 0;
  6270. }
  6271. static int i830_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6272. {
  6273. return 133333;
  6274. }
  6275. static unsigned int intel_hpll_vco(struct drm_i915_private *dev_priv)
  6276. {
  6277. static const unsigned int blb_vco[8] = {
  6278. [0] = 3200000,
  6279. [1] = 4000000,
  6280. [2] = 5333333,
  6281. [3] = 4800000,
  6282. [4] = 6400000,
  6283. };
  6284. static const unsigned int pnv_vco[8] = {
  6285. [0] = 3200000,
  6286. [1] = 4000000,
  6287. [2] = 5333333,
  6288. [3] = 4800000,
  6289. [4] = 2666667,
  6290. };
  6291. static const unsigned int cl_vco[8] = {
  6292. [0] = 3200000,
  6293. [1] = 4000000,
  6294. [2] = 5333333,
  6295. [3] = 6400000,
  6296. [4] = 3333333,
  6297. [5] = 3566667,
  6298. [6] = 4266667,
  6299. };
  6300. static const unsigned int elk_vco[8] = {
  6301. [0] = 3200000,
  6302. [1] = 4000000,
  6303. [2] = 5333333,
  6304. [3] = 4800000,
  6305. };
  6306. static const unsigned int ctg_vco[8] = {
  6307. [0] = 3200000,
  6308. [1] = 4000000,
  6309. [2] = 5333333,
  6310. [3] = 6400000,
  6311. [4] = 2666667,
  6312. [5] = 4266667,
  6313. };
  6314. const unsigned int *vco_table;
  6315. unsigned int vco;
  6316. uint8_t tmp = 0;
  6317. /* FIXME other chipsets? */
  6318. if (IS_GM45(dev_priv))
  6319. vco_table = ctg_vco;
  6320. else if (IS_G4X(dev_priv))
  6321. vco_table = elk_vco;
  6322. else if (IS_CRESTLINE(dev_priv))
  6323. vco_table = cl_vco;
  6324. else if (IS_PINEVIEW(dev_priv))
  6325. vco_table = pnv_vco;
  6326. else if (IS_G33(dev_priv))
  6327. vco_table = blb_vco;
  6328. else
  6329. return 0;
  6330. tmp = I915_READ(IS_MOBILE(dev_priv) ? HPLLVCO_MOBILE : HPLLVCO);
  6331. vco = vco_table[tmp & 0x7];
  6332. if (vco == 0)
  6333. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  6334. else
  6335. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  6336. return vco;
  6337. }
  6338. static int gm45_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6339. {
  6340. struct pci_dev *pdev = dev_priv->drm.pdev;
  6341. unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
  6342. uint16_t tmp = 0;
  6343. pci_read_config_word(pdev, GCFGC, &tmp);
  6344. cdclk_sel = (tmp >> 12) & 0x1;
  6345. switch (vco) {
  6346. case 2666667:
  6347. case 4000000:
  6348. case 5333333:
  6349. return cdclk_sel ? 333333 : 222222;
  6350. case 3200000:
  6351. return cdclk_sel ? 320000 : 228571;
  6352. default:
  6353. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  6354. return 222222;
  6355. }
  6356. }
  6357. static int i965gm_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6358. {
  6359. struct pci_dev *pdev = dev_priv->drm.pdev;
  6360. static const uint8_t div_3200[] = { 16, 10, 8 };
  6361. static const uint8_t div_4000[] = { 20, 12, 10 };
  6362. static const uint8_t div_5333[] = { 24, 16, 14 };
  6363. const uint8_t *div_table;
  6364. unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
  6365. uint16_t tmp = 0;
  6366. pci_read_config_word(pdev, GCFGC, &tmp);
  6367. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  6368. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  6369. goto fail;
  6370. switch (vco) {
  6371. case 3200000:
  6372. div_table = div_3200;
  6373. break;
  6374. case 4000000:
  6375. div_table = div_4000;
  6376. break;
  6377. case 5333333:
  6378. div_table = div_5333;
  6379. break;
  6380. default:
  6381. goto fail;
  6382. }
  6383. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  6384. fail:
  6385. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  6386. return 200000;
  6387. }
  6388. static int g33_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6389. {
  6390. struct pci_dev *pdev = dev_priv->drm.pdev;
  6391. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  6392. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  6393. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  6394. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  6395. const uint8_t *div_table;
  6396. unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
  6397. uint16_t tmp = 0;
  6398. pci_read_config_word(pdev, GCFGC, &tmp);
  6399. cdclk_sel = (tmp >> 4) & 0x7;
  6400. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  6401. goto fail;
  6402. switch (vco) {
  6403. case 3200000:
  6404. div_table = div_3200;
  6405. break;
  6406. case 4000000:
  6407. div_table = div_4000;
  6408. break;
  6409. case 4800000:
  6410. div_table = div_4800;
  6411. break;
  6412. case 5333333:
  6413. div_table = div_5333;
  6414. break;
  6415. default:
  6416. goto fail;
  6417. }
  6418. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  6419. fail:
  6420. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  6421. return 190476;
  6422. }
  6423. static void
  6424. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  6425. {
  6426. while (*num > DATA_LINK_M_N_MASK ||
  6427. *den > DATA_LINK_M_N_MASK) {
  6428. *num >>= 1;
  6429. *den >>= 1;
  6430. }
  6431. }
  6432. static void compute_m_n(unsigned int m, unsigned int n,
  6433. uint32_t *ret_m, uint32_t *ret_n)
  6434. {
  6435. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  6436. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  6437. intel_reduce_m_n_ratio(ret_m, ret_n);
  6438. }
  6439. void
  6440. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  6441. int pixel_clock, int link_clock,
  6442. struct intel_link_m_n *m_n)
  6443. {
  6444. m_n->tu = 64;
  6445. compute_m_n(bits_per_pixel * pixel_clock,
  6446. link_clock * nlanes * 8,
  6447. &m_n->gmch_m, &m_n->gmch_n);
  6448. compute_m_n(pixel_clock, link_clock,
  6449. &m_n->link_m, &m_n->link_n);
  6450. }
  6451. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  6452. {
  6453. if (i915.panel_use_ssc >= 0)
  6454. return i915.panel_use_ssc != 0;
  6455. return dev_priv->vbt.lvds_use_ssc
  6456. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  6457. }
  6458. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  6459. {
  6460. return (1 << dpll->n) << 16 | dpll->m2;
  6461. }
  6462. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  6463. {
  6464. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  6465. }
  6466. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  6467. struct intel_crtc_state *crtc_state,
  6468. struct dpll *reduced_clock)
  6469. {
  6470. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6471. u32 fp, fp2 = 0;
  6472. if (IS_PINEVIEW(dev_priv)) {
  6473. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  6474. if (reduced_clock)
  6475. fp2 = pnv_dpll_compute_fp(reduced_clock);
  6476. } else {
  6477. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6478. if (reduced_clock)
  6479. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6480. }
  6481. crtc_state->dpll_hw_state.fp0 = fp;
  6482. crtc->lowfreq_avail = false;
  6483. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6484. reduced_clock) {
  6485. crtc_state->dpll_hw_state.fp1 = fp2;
  6486. crtc->lowfreq_avail = true;
  6487. } else {
  6488. crtc_state->dpll_hw_state.fp1 = fp;
  6489. }
  6490. }
  6491. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  6492. pipe)
  6493. {
  6494. u32 reg_val;
  6495. /*
  6496. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  6497. * and set it to a reasonable value instead.
  6498. */
  6499. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6500. reg_val &= 0xffffff00;
  6501. reg_val |= 0x00000030;
  6502. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6503. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6504. reg_val &= 0x8cffffff;
  6505. reg_val = 0x8c000000;
  6506. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6507. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6508. reg_val &= 0xffffff00;
  6509. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6510. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6511. reg_val &= 0x00ffffff;
  6512. reg_val |= 0xb0000000;
  6513. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6514. }
  6515. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  6516. struct intel_link_m_n *m_n)
  6517. {
  6518. struct drm_device *dev = crtc->base.dev;
  6519. struct drm_i915_private *dev_priv = to_i915(dev);
  6520. int pipe = crtc->pipe;
  6521. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6522. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  6523. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  6524. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  6525. }
  6526. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6527. struct intel_link_m_n *m_n,
  6528. struct intel_link_m_n *m2_n2)
  6529. {
  6530. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6531. int pipe = crtc->pipe;
  6532. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6533. if (INTEL_GEN(dev_priv) >= 5) {
  6534. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6535. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6536. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6537. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6538. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6539. * for gen < 8) and if DRRS is supported (to make sure the
  6540. * registers are not unnecessarily accessed).
  6541. */
  6542. if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
  6543. INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
  6544. I915_WRITE(PIPE_DATA_M2(transcoder),
  6545. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6546. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6547. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6548. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6549. }
  6550. } else {
  6551. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6552. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6553. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6554. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6555. }
  6556. }
  6557. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6558. {
  6559. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6560. if (m_n == M1_N1) {
  6561. dp_m_n = &crtc->config->dp_m_n;
  6562. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6563. } else if (m_n == M2_N2) {
  6564. /*
  6565. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6566. * needs to be programmed into M1_N1.
  6567. */
  6568. dp_m_n = &crtc->config->dp_m2_n2;
  6569. } else {
  6570. DRM_ERROR("Unsupported divider value\n");
  6571. return;
  6572. }
  6573. if (crtc->config->has_pch_encoder)
  6574. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6575. else
  6576. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6577. }
  6578. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6579. struct intel_crtc_state *pipe_config)
  6580. {
  6581. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  6582. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6583. if (crtc->pipe != PIPE_A)
  6584. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6585. /* DPLL not used with DSI, but still need the rest set up */
  6586. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  6587. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  6588. DPLL_EXT_BUFFER_ENABLE_VLV;
  6589. pipe_config->dpll_hw_state.dpll_md =
  6590. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6591. }
  6592. static void chv_compute_dpll(struct intel_crtc *crtc,
  6593. struct intel_crtc_state *pipe_config)
  6594. {
  6595. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  6596. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6597. if (crtc->pipe != PIPE_A)
  6598. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6599. /* DPLL not used with DSI, but still need the rest set up */
  6600. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  6601. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  6602. pipe_config->dpll_hw_state.dpll_md =
  6603. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6604. }
  6605. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6606. const struct intel_crtc_state *pipe_config)
  6607. {
  6608. struct drm_device *dev = crtc->base.dev;
  6609. struct drm_i915_private *dev_priv = to_i915(dev);
  6610. enum pipe pipe = crtc->pipe;
  6611. u32 mdiv;
  6612. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6613. u32 coreclk, reg_val;
  6614. /* Enable Refclk */
  6615. I915_WRITE(DPLL(pipe),
  6616. pipe_config->dpll_hw_state.dpll &
  6617. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  6618. /* No need to actually set up the DPLL with DSI */
  6619. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6620. return;
  6621. mutex_lock(&dev_priv->sb_lock);
  6622. bestn = pipe_config->dpll.n;
  6623. bestm1 = pipe_config->dpll.m1;
  6624. bestm2 = pipe_config->dpll.m2;
  6625. bestp1 = pipe_config->dpll.p1;
  6626. bestp2 = pipe_config->dpll.p2;
  6627. /* See eDP HDMI DPIO driver vbios notes doc */
  6628. /* PLL B needs special handling */
  6629. if (pipe == PIPE_B)
  6630. vlv_pllb_recal_opamp(dev_priv, pipe);
  6631. /* Set up Tx target for periodic Rcomp update */
  6632. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6633. /* Disable target IRef on PLL */
  6634. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6635. reg_val &= 0x00ffffff;
  6636. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6637. /* Disable fast lock */
  6638. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6639. /* Set idtafcrecal before PLL is enabled */
  6640. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6641. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6642. mdiv |= ((bestn << DPIO_N_SHIFT));
  6643. mdiv |= (1 << DPIO_K_SHIFT);
  6644. /*
  6645. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6646. * but we don't support that).
  6647. * Note: don't use the DAC post divider as it seems unstable.
  6648. */
  6649. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6650. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6651. mdiv |= DPIO_ENABLE_CALIBRATION;
  6652. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6653. /* Set HBR and RBR LPF coefficients */
  6654. if (pipe_config->port_clock == 162000 ||
  6655. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
  6656. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
  6657. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6658. 0x009f0003);
  6659. else
  6660. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6661. 0x00d0000f);
  6662. if (intel_crtc_has_dp_encoder(pipe_config)) {
  6663. /* Use SSC source */
  6664. if (pipe == PIPE_A)
  6665. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6666. 0x0df40000);
  6667. else
  6668. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6669. 0x0df70000);
  6670. } else { /* HDMI or VGA */
  6671. /* Use bend source */
  6672. if (pipe == PIPE_A)
  6673. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6674. 0x0df70000);
  6675. else
  6676. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6677. 0x0df40000);
  6678. }
  6679. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6680. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6681. if (intel_crtc_has_dp_encoder(crtc->config))
  6682. coreclk |= 0x01000000;
  6683. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6684. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6685. mutex_unlock(&dev_priv->sb_lock);
  6686. }
  6687. static void chv_prepare_pll(struct intel_crtc *crtc,
  6688. const struct intel_crtc_state *pipe_config)
  6689. {
  6690. struct drm_device *dev = crtc->base.dev;
  6691. struct drm_i915_private *dev_priv = to_i915(dev);
  6692. enum pipe pipe = crtc->pipe;
  6693. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6694. u32 loopfilter, tribuf_calcntr;
  6695. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6696. u32 dpio_val;
  6697. int vco;
  6698. /* Enable Refclk and SSC */
  6699. I915_WRITE(DPLL(pipe),
  6700. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6701. /* No need to actually set up the DPLL with DSI */
  6702. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6703. return;
  6704. bestn = pipe_config->dpll.n;
  6705. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6706. bestm1 = pipe_config->dpll.m1;
  6707. bestm2 = pipe_config->dpll.m2 >> 22;
  6708. bestp1 = pipe_config->dpll.p1;
  6709. bestp2 = pipe_config->dpll.p2;
  6710. vco = pipe_config->dpll.vco;
  6711. dpio_val = 0;
  6712. loopfilter = 0;
  6713. mutex_lock(&dev_priv->sb_lock);
  6714. /* p1 and p2 divider */
  6715. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6716. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6717. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6718. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6719. 1 << DPIO_CHV_K_DIV_SHIFT);
  6720. /* Feedback post-divider - m2 */
  6721. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6722. /* Feedback refclk divider - n and m1 */
  6723. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6724. DPIO_CHV_M1_DIV_BY_2 |
  6725. 1 << DPIO_CHV_N_DIV_SHIFT);
  6726. /* M2 fraction division */
  6727. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6728. /* M2 fraction division enable */
  6729. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6730. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6731. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6732. if (bestm2_frac)
  6733. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6734. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6735. /* Program digital lock detect threshold */
  6736. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6737. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6738. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6739. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6740. if (!bestm2_frac)
  6741. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6742. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6743. /* Loop filter */
  6744. if (vco == 5400000) {
  6745. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6746. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6747. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6748. tribuf_calcntr = 0x9;
  6749. } else if (vco <= 6200000) {
  6750. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6751. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6752. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6753. tribuf_calcntr = 0x9;
  6754. } else if (vco <= 6480000) {
  6755. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6756. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6757. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6758. tribuf_calcntr = 0x8;
  6759. } else {
  6760. /* Not supported. Apply the same limits as in the max case */
  6761. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6762. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6763. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6764. tribuf_calcntr = 0;
  6765. }
  6766. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6767. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6768. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6769. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6770. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6771. /* AFC Recal */
  6772. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6773. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6774. DPIO_AFC_RECAL);
  6775. mutex_unlock(&dev_priv->sb_lock);
  6776. }
  6777. /**
  6778. * vlv_force_pll_on - forcibly enable just the PLL
  6779. * @dev_priv: i915 private structure
  6780. * @pipe: pipe PLL to enable
  6781. * @dpll: PLL configuration
  6782. *
  6783. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6784. * in cases where we need the PLL enabled even when @pipe is not going to
  6785. * be enabled.
  6786. */
  6787. int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
  6788. const struct dpll *dpll)
  6789. {
  6790. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  6791. struct intel_crtc_state *pipe_config;
  6792. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  6793. if (!pipe_config)
  6794. return -ENOMEM;
  6795. pipe_config->base.crtc = &crtc->base;
  6796. pipe_config->pixel_multiplier = 1;
  6797. pipe_config->dpll = *dpll;
  6798. if (IS_CHERRYVIEW(dev_priv)) {
  6799. chv_compute_dpll(crtc, pipe_config);
  6800. chv_prepare_pll(crtc, pipe_config);
  6801. chv_enable_pll(crtc, pipe_config);
  6802. } else {
  6803. vlv_compute_dpll(crtc, pipe_config);
  6804. vlv_prepare_pll(crtc, pipe_config);
  6805. vlv_enable_pll(crtc, pipe_config);
  6806. }
  6807. kfree(pipe_config);
  6808. return 0;
  6809. }
  6810. /**
  6811. * vlv_force_pll_off - forcibly disable just the PLL
  6812. * @dev_priv: i915 private structure
  6813. * @pipe: pipe PLL to disable
  6814. *
  6815. * Disable the PLL for @pipe. To be used in cases where we need
  6816. * the PLL enabled even when @pipe is not going to be enabled.
  6817. */
  6818. void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
  6819. {
  6820. if (IS_CHERRYVIEW(dev_priv))
  6821. chv_disable_pll(dev_priv, pipe);
  6822. else
  6823. vlv_disable_pll(dev_priv, pipe);
  6824. }
  6825. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6826. struct intel_crtc_state *crtc_state,
  6827. struct dpll *reduced_clock)
  6828. {
  6829. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6830. u32 dpll;
  6831. struct dpll *clock = &crtc_state->dpll;
  6832. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6833. dpll = DPLL_VGA_MODE_DIS;
  6834. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  6835. dpll |= DPLLB_MODE_LVDS;
  6836. else
  6837. dpll |= DPLLB_MODE_DAC_SERIAL;
  6838. if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) || IS_G33(dev_priv)) {
  6839. dpll |= (crtc_state->pixel_multiplier - 1)
  6840. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6841. }
  6842. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6843. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  6844. dpll |= DPLL_SDVO_HIGH_SPEED;
  6845. if (intel_crtc_has_dp_encoder(crtc_state))
  6846. dpll |= DPLL_SDVO_HIGH_SPEED;
  6847. /* compute bitmask from p1 value */
  6848. if (IS_PINEVIEW(dev_priv))
  6849. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6850. else {
  6851. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6852. if (IS_G4X(dev_priv) && reduced_clock)
  6853. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6854. }
  6855. switch (clock->p2) {
  6856. case 5:
  6857. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6858. break;
  6859. case 7:
  6860. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6861. break;
  6862. case 10:
  6863. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6864. break;
  6865. case 14:
  6866. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6867. break;
  6868. }
  6869. if (INTEL_GEN(dev_priv) >= 4)
  6870. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6871. if (crtc_state->sdvo_tv_clock)
  6872. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6873. else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6874. intel_panel_use_ssc(dev_priv))
  6875. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6876. else
  6877. dpll |= PLL_REF_INPUT_DREFCLK;
  6878. dpll |= DPLL_VCO_ENABLE;
  6879. crtc_state->dpll_hw_state.dpll = dpll;
  6880. if (INTEL_GEN(dev_priv) >= 4) {
  6881. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6882. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6883. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6884. }
  6885. }
  6886. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6887. struct intel_crtc_state *crtc_state,
  6888. struct dpll *reduced_clock)
  6889. {
  6890. struct drm_device *dev = crtc->base.dev;
  6891. struct drm_i915_private *dev_priv = to_i915(dev);
  6892. u32 dpll;
  6893. struct dpll *clock = &crtc_state->dpll;
  6894. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6895. dpll = DPLL_VGA_MODE_DIS;
  6896. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6897. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6898. } else {
  6899. if (clock->p1 == 2)
  6900. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6901. else
  6902. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6903. if (clock->p2 == 4)
  6904. dpll |= PLL_P2_DIVIDE_BY_4;
  6905. }
  6906. if (!IS_I830(dev_priv) &&
  6907. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
  6908. dpll |= DPLL_DVO_2X_MODE;
  6909. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6910. intel_panel_use_ssc(dev_priv))
  6911. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6912. else
  6913. dpll |= PLL_REF_INPUT_DREFCLK;
  6914. dpll |= DPLL_VCO_ENABLE;
  6915. crtc_state->dpll_hw_state.dpll = dpll;
  6916. }
  6917. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6918. {
  6919. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  6920. enum pipe pipe = intel_crtc->pipe;
  6921. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6922. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  6923. uint32_t crtc_vtotal, crtc_vblank_end;
  6924. int vsyncshift = 0;
  6925. /* We need to be careful not to changed the adjusted mode, for otherwise
  6926. * the hw state checker will get angry at the mismatch. */
  6927. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6928. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6929. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6930. /* the chip adds 2 halflines automatically */
  6931. crtc_vtotal -= 1;
  6932. crtc_vblank_end -= 1;
  6933. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6934. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6935. else
  6936. vsyncshift = adjusted_mode->crtc_hsync_start -
  6937. adjusted_mode->crtc_htotal / 2;
  6938. if (vsyncshift < 0)
  6939. vsyncshift += adjusted_mode->crtc_htotal;
  6940. }
  6941. if (INTEL_GEN(dev_priv) > 3)
  6942. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6943. I915_WRITE(HTOTAL(cpu_transcoder),
  6944. (adjusted_mode->crtc_hdisplay - 1) |
  6945. ((adjusted_mode->crtc_htotal - 1) << 16));
  6946. I915_WRITE(HBLANK(cpu_transcoder),
  6947. (adjusted_mode->crtc_hblank_start - 1) |
  6948. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6949. I915_WRITE(HSYNC(cpu_transcoder),
  6950. (adjusted_mode->crtc_hsync_start - 1) |
  6951. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6952. I915_WRITE(VTOTAL(cpu_transcoder),
  6953. (adjusted_mode->crtc_vdisplay - 1) |
  6954. ((crtc_vtotal - 1) << 16));
  6955. I915_WRITE(VBLANK(cpu_transcoder),
  6956. (adjusted_mode->crtc_vblank_start - 1) |
  6957. ((crtc_vblank_end - 1) << 16));
  6958. I915_WRITE(VSYNC(cpu_transcoder),
  6959. (adjusted_mode->crtc_vsync_start - 1) |
  6960. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6961. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6962. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6963. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6964. * bits. */
  6965. if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
  6966. (pipe == PIPE_B || pipe == PIPE_C))
  6967. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6968. }
  6969. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  6970. {
  6971. struct drm_device *dev = intel_crtc->base.dev;
  6972. struct drm_i915_private *dev_priv = to_i915(dev);
  6973. enum pipe pipe = intel_crtc->pipe;
  6974. /* pipesrc controls the size that is scaled from, which should
  6975. * always be the user's requested size.
  6976. */
  6977. I915_WRITE(PIPESRC(pipe),
  6978. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  6979. (intel_crtc->config->pipe_src_h - 1));
  6980. }
  6981. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  6982. struct intel_crtc_state *pipe_config)
  6983. {
  6984. struct drm_device *dev = crtc->base.dev;
  6985. struct drm_i915_private *dev_priv = to_i915(dev);
  6986. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  6987. uint32_t tmp;
  6988. tmp = I915_READ(HTOTAL(cpu_transcoder));
  6989. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  6990. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  6991. tmp = I915_READ(HBLANK(cpu_transcoder));
  6992. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  6993. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  6994. tmp = I915_READ(HSYNC(cpu_transcoder));
  6995. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  6996. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  6997. tmp = I915_READ(VTOTAL(cpu_transcoder));
  6998. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  6999. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  7000. tmp = I915_READ(VBLANK(cpu_transcoder));
  7001. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  7002. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  7003. tmp = I915_READ(VSYNC(cpu_transcoder));
  7004. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  7005. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  7006. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  7007. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  7008. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  7009. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  7010. }
  7011. }
  7012. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  7013. struct intel_crtc_state *pipe_config)
  7014. {
  7015. struct drm_device *dev = crtc->base.dev;
  7016. struct drm_i915_private *dev_priv = to_i915(dev);
  7017. u32 tmp;
  7018. tmp = I915_READ(PIPESRC(crtc->pipe));
  7019. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  7020. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  7021. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  7022. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  7023. }
  7024. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  7025. struct intel_crtc_state *pipe_config)
  7026. {
  7027. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  7028. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  7029. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  7030. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  7031. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  7032. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  7033. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  7034. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  7035. mode->flags = pipe_config->base.adjusted_mode.flags;
  7036. mode->type = DRM_MODE_TYPE_DRIVER;
  7037. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  7038. mode->flags |= pipe_config->base.adjusted_mode.flags;
  7039. mode->hsync = drm_mode_hsync(mode);
  7040. mode->vrefresh = drm_mode_vrefresh(mode);
  7041. drm_mode_set_name(mode);
  7042. }
  7043. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  7044. {
  7045. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  7046. uint32_t pipeconf;
  7047. pipeconf = 0;
  7048. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  7049. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  7050. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  7051. if (intel_crtc->config->double_wide)
  7052. pipeconf |= PIPECONF_DOUBLE_WIDE;
  7053. /* only g4x and later have fancy bpc/dither controls */
  7054. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  7055. IS_CHERRYVIEW(dev_priv)) {
  7056. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  7057. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  7058. pipeconf |= PIPECONF_DITHER_EN |
  7059. PIPECONF_DITHER_TYPE_SP;
  7060. switch (intel_crtc->config->pipe_bpp) {
  7061. case 18:
  7062. pipeconf |= PIPECONF_6BPC;
  7063. break;
  7064. case 24:
  7065. pipeconf |= PIPECONF_8BPC;
  7066. break;
  7067. case 30:
  7068. pipeconf |= PIPECONF_10BPC;
  7069. break;
  7070. default:
  7071. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7072. BUG();
  7073. }
  7074. }
  7075. if (HAS_PIPE_CXSR(dev_priv)) {
  7076. if (intel_crtc->lowfreq_avail) {
  7077. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  7078. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  7079. } else {
  7080. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  7081. }
  7082. }
  7083. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  7084. if (INTEL_GEN(dev_priv) < 4 ||
  7085. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  7086. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  7087. else
  7088. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  7089. } else
  7090. pipeconf |= PIPECONF_PROGRESSIVE;
  7091. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  7092. intel_crtc->config->limited_color_range)
  7093. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  7094. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  7095. POSTING_READ(PIPECONF(intel_crtc->pipe));
  7096. }
  7097. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  7098. struct intel_crtc_state *crtc_state)
  7099. {
  7100. struct drm_device *dev = crtc->base.dev;
  7101. struct drm_i915_private *dev_priv = to_i915(dev);
  7102. const struct intel_limit *limit;
  7103. int refclk = 48000;
  7104. memset(&crtc_state->dpll_hw_state, 0,
  7105. sizeof(crtc_state->dpll_hw_state));
  7106. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7107. if (intel_panel_use_ssc(dev_priv)) {
  7108. refclk = dev_priv->vbt.lvds_ssc_freq;
  7109. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7110. }
  7111. limit = &intel_limits_i8xx_lvds;
  7112. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
  7113. limit = &intel_limits_i8xx_dvo;
  7114. } else {
  7115. limit = &intel_limits_i8xx_dac;
  7116. }
  7117. if (!crtc_state->clock_set &&
  7118. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7119. refclk, NULL, &crtc_state->dpll)) {
  7120. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7121. return -EINVAL;
  7122. }
  7123. i8xx_compute_dpll(crtc, crtc_state, NULL);
  7124. return 0;
  7125. }
  7126. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  7127. struct intel_crtc_state *crtc_state)
  7128. {
  7129. struct drm_device *dev = crtc->base.dev;
  7130. struct drm_i915_private *dev_priv = to_i915(dev);
  7131. const struct intel_limit *limit;
  7132. int refclk = 96000;
  7133. memset(&crtc_state->dpll_hw_state, 0,
  7134. sizeof(crtc_state->dpll_hw_state));
  7135. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7136. if (intel_panel_use_ssc(dev_priv)) {
  7137. refclk = dev_priv->vbt.lvds_ssc_freq;
  7138. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7139. }
  7140. if (intel_is_dual_link_lvds(dev))
  7141. limit = &intel_limits_g4x_dual_channel_lvds;
  7142. else
  7143. limit = &intel_limits_g4x_single_channel_lvds;
  7144. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  7145. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  7146. limit = &intel_limits_g4x_hdmi;
  7147. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  7148. limit = &intel_limits_g4x_sdvo;
  7149. } else {
  7150. /* The option is for other outputs */
  7151. limit = &intel_limits_i9xx_sdvo;
  7152. }
  7153. if (!crtc_state->clock_set &&
  7154. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7155. refclk, NULL, &crtc_state->dpll)) {
  7156. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7157. return -EINVAL;
  7158. }
  7159. i9xx_compute_dpll(crtc, crtc_state, NULL);
  7160. return 0;
  7161. }
  7162. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  7163. struct intel_crtc_state *crtc_state)
  7164. {
  7165. struct drm_device *dev = crtc->base.dev;
  7166. struct drm_i915_private *dev_priv = to_i915(dev);
  7167. const struct intel_limit *limit;
  7168. int refclk = 96000;
  7169. memset(&crtc_state->dpll_hw_state, 0,
  7170. sizeof(crtc_state->dpll_hw_state));
  7171. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7172. if (intel_panel_use_ssc(dev_priv)) {
  7173. refclk = dev_priv->vbt.lvds_ssc_freq;
  7174. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7175. }
  7176. limit = &intel_limits_pineview_lvds;
  7177. } else {
  7178. limit = &intel_limits_pineview_sdvo;
  7179. }
  7180. if (!crtc_state->clock_set &&
  7181. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7182. refclk, NULL, &crtc_state->dpll)) {
  7183. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7184. return -EINVAL;
  7185. }
  7186. i9xx_compute_dpll(crtc, crtc_state, NULL);
  7187. return 0;
  7188. }
  7189. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  7190. struct intel_crtc_state *crtc_state)
  7191. {
  7192. struct drm_device *dev = crtc->base.dev;
  7193. struct drm_i915_private *dev_priv = to_i915(dev);
  7194. const struct intel_limit *limit;
  7195. int refclk = 96000;
  7196. memset(&crtc_state->dpll_hw_state, 0,
  7197. sizeof(crtc_state->dpll_hw_state));
  7198. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7199. if (intel_panel_use_ssc(dev_priv)) {
  7200. refclk = dev_priv->vbt.lvds_ssc_freq;
  7201. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7202. }
  7203. limit = &intel_limits_i9xx_lvds;
  7204. } else {
  7205. limit = &intel_limits_i9xx_sdvo;
  7206. }
  7207. if (!crtc_state->clock_set &&
  7208. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7209. refclk, NULL, &crtc_state->dpll)) {
  7210. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7211. return -EINVAL;
  7212. }
  7213. i9xx_compute_dpll(crtc, crtc_state, NULL);
  7214. return 0;
  7215. }
  7216. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  7217. struct intel_crtc_state *crtc_state)
  7218. {
  7219. int refclk = 100000;
  7220. const struct intel_limit *limit = &intel_limits_chv;
  7221. memset(&crtc_state->dpll_hw_state, 0,
  7222. sizeof(crtc_state->dpll_hw_state));
  7223. if (!crtc_state->clock_set &&
  7224. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7225. refclk, NULL, &crtc_state->dpll)) {
  7226. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7227. return -EINVAL;
  7228. }
  7229. chv_compute_dpll(crtc, crtc_state);
  7230. return 0;
  7231. }
  7232. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  7233. struct intel_crtc_state *crtc_state)
  7234. {
  7235. int refclk = 100000;
  7236. const struct intel_limit *limit = &intel_limits_vlv;
  7237. memset(&crtc_state->dpll_hw_state, 0,
  7238. sizeof(crtc_state->dpll_hw_state));
  7239. if (!crtc_state->clock_set &&
  7240. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7241. refclk, NULL, &crtc_state->dpll)) {
  7242. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7243. return -EINVAL;
  7244. }
  7245. vlv_compute_dpll(crtc, crtc_state);
  7246. return 0;
  7247. }
  7248. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  7249. struct intel_crtc_state *pipe_config)
  7250. {
  7251. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7252. uint32_t tmp;
  7253. if (INTEL_GEN(dev_priv) <= 3 &&
  7254. (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
  7255. return;
  7256. tmp = I915_READ(PFIT_CONTROL);
  7257. if (!(tmp & PFIT_ENABLE))
  7258. return;
  7259. /* Check whether the pfit is attached to our pipe. */
  7260. if (INTEL_GEN(dev_priv) < 4) {
  7261. if (crtc->pipe != PIPE_B)
  7262. return;
  7263. } else {
  7264. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  7265. return;
  7266. }
  7267. pipe_config->gmch_pfit.control = tmp;
  7268. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  7269. }
  7270. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  7271. struct intel_crtc_state *pipe_config)
  7272. {
  7273. struct drm_device *dev = crtc->base.dev;
  7274. struct drm_i915_private *dev_priv = to_i915(dev);
  7275. int pipe = pipe_config->cpu_transcoder;
  7276. struct dpll clock;
  7277. u32 mdiv;
  7278. int refclk = 100000;
  7279. /* In case of DSI, DPLL will not be used */
  7280. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  7281. return;
  7282. mutex_lock(&dev_priv->sb_lock);
  7283. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  7284. mutex_unlock(&dev_priv->sb_lock);
  7285. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  7286. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  7287. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  7288. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  7289. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  7290. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  7291. }
  7292. static void
  7293. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  7294. struct intel_initial_plane_config *plane_config)
  7295. {
  7296. struct drm_device *dev = crtc->base.dev;
  7297. struct drm_i915_private *dev_priv = to_i915(dev);
  7298. u32 val, base, offset;
  7299. int pipe = crtc->pipe, plane = crtc->plane;
  7300. int fourcc, pixel_format;
  7301. unsigned int aligned_height;
  7302. struct drm_framebuffer *fb;
  7303. struct intel_framebuffer *intel_fb;
  7304. val = I915_READ(DSPCNTR(plane));
  7305. if (!(val & DISPLAY_PLANE_ENABLE))
  7306. return;
  7307. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7308. if (!intel_fb) {
  7309. DRM_DEBUG_KMS("failed to alloc fb\n");
  7310. return;
  7311. }
  7312. fb = &intel_fb->base;
  7313. if (INTEL_GEN(dev_priv) >= 4) {
  7314. if (val & DISPPLANE_TILED) {
  7315. plane_config->tiling = I915_TILING_X;
  7316. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7317. }
  7318. }
  7319. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7320. fourcc = i9xx_format_to_fourcc(pixel_format);
  7321. fb->pixel_format = fourcc;
  7322. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7323. if (INTEL_GEN(dev_priv) >= 4) {
  7324. if (plane_config->tiling)
  7325. offset = I915_READ(DSPTILEOFF(plane));
  7326. else
  7327. offset = I915_READ(DSPLINOFF(plane));
  7328. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  7329. } else {
  7330. base = I915_READ(DSPADDR(plane));
  7331. }
  7332. plane_config->base = base;
  7333. val = I915_READ(PIPESRC(pipe));
  7334. fb->width = ((val >> 16) & 0xfff) + 1;
  7335. fb->height = ((val >> 0) & 0xfff) + 1;
  7336. val = I915_READ(DSPSTRIDE(pipe));
  7337. fb->pitches[0] = val & 0xffffffc0;
  7338. aligned_height = intel_fb_align_height(dev, fb->height,
  7339. fb->pixel_format,
  7340. fb->modifier);
  7341. plane_config->size = fb->pitches[0] * aligned_height;
  7342. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7343. pipe_name(pipe), plane, fb->width, fb->height,
  7344. fb->bits_per_pixel, base, fb->pitches[0],
  7345. plane_config->size);
  7346. plane_config->fb = intel_fb;
  7347. }
  7348. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  7349. struct intel_crtc_state *pipe_config)
  7350. {
  7351. struct drm_device *dev = crtc->base.dev;
  7352. struct drm_i915_private *dev_priv = to_i915(dev);
  7353. int pipe = pipe_config->cpu_transcoder;
  7354. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  7355. struct dpll clock;
  7356. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  7357. int refclk = 100000;
  7358. /* In case of DSI, DPLL will not be used */
  7359. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  7360. return;
  7361. mutex_lock(&dev_priv->sb_lock);
  7362. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  7363. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  7364. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  7365. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  7366. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  7367. mutex_unlock(&dev_priv->sb_lock);
  7368. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  7369. clock.m2 = (pll_dw0 & 0xff) << 22;
  7370. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  7371. clock.m2 |= pll_dw2 & 0x3fffff;
  7372. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  7373. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  7374. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  7375. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  7376. }
  7377. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  7378. struct intel_crtc_state *pipe_config)
  7379. {
  7380. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7381. enum intel_display_power_domain power_domain;
  7382. uint32_t tmp;
  7383. bool ret;
  7384. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7385. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7386. return false;
  7387. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7388. pipe_config->shared_dpll = NULL;
  7389. ret = false;
  7390. tmp = I915_READ(PIPECONF(crtc->pipe));
  7391. if (!(tmp & PIPECONF_ENABLE))
  7392. goto out;
  7393. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  7394. IS_CHERRYVIEW(dev_priv)) {
  7395. switch (tmp & PIPECONF_BPC_MASK) {
  7396. case PIPECONF_6BPC:
  7397. pipe_config->pipe_bpp = 18;
  7398. break;
  7399. case PIPECONF_8BPC:
  7400. pipe_config->pipe_bpp = 24;
  7401. break;
  7402. case PIPECONF_10BPC:
  7403. pipe_config->pipe_bpp = 30;
  7404. break;
  7405. default:
  7406. break;
  7407. }
  7408. }
  7409. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  7410. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  7411. pipe_config->limited_color_range = true;
  7412. if (INTEL_GEN(dev_priv) < 4)
  7413. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  7414. intel_get_pipe_timings(crtc, pipe_config);
  7415. intel_get_pipe_src_size(crtc, pipe_config);
  7416. i9xx_get_pfit_config(crtc, pipe_config);
  7417. if (INTEL_GEN(dev_priv) >= 4) {
  7418. /* No way to read it out on pipes B and C */
  7419. if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
  7420. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  7421. else
  7422. tmp = I915_READ(DPLL_MD(crtc->pipe));
  7423. pipe_config->pixel_multiplier =
  7424. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  7425. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  7426. pipe_config->dpll_hw_state.dpll_md = tmp;
  7427. } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  7428. IS_G33(dev_priv)) {
  7429. tmp = I915_READ(DPLL(crtc->pipe));
  7430. pipe_config->pixel_multiplier =
  7431. ((tmp & SDVO_MULTIPLIER_MASK)
  7432. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  7433. } else {
  7434. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  7435. * port and will be fixed up in the encoder->get_config
  7436. * function. */
  7437. pipe_config->pixel_multiplier = 1;
  7438. }
  7439. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  7440. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  7441. /*
  7442. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  7443. * on 830. Filter it out here so that we don't
  7444. * report errors due to that.
  7445. */
  7446. if (IS_I830(dev_priv))
  7447. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  7448. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  7449. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  7450. } else {
  7451. /* Mask out read-only status bits. */
  7452. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  7453. DPLL_PORTC_READY_MASK |
  7454. DPLL_PORTB_READY_MASK);
  7455. }
  7456. if (IS_CHERRYVIEW(dev_priv))
  7457. chv_crtc_clock_get(crtc, pipe_config);
  7458. else if (IS_VALLEYVIEW(dev_priv))
  7459. vlv_crtc_clock_get(crtc, pipe_config);
  7460. else
  7461. i9xx_crtc_clock_get(crtc, pipe_config);
  7462. /*
  7463. * Normally the dotclock is filled in by the encoder .get_config()
  7464. * but in case the pipe is enabled w/o any ports we need a sane
  7465. * default.
  7466. */
  7467. pipe_config->base.adjusted_mode.crtc_clock =
  7468. pipe_config->port_clock / pipe_config->pixel_multiplier;
  7469. ret = true;
  7470. out:
  7471. intel_display_power_put(dev_priv, power_domain);
  7472. return ret;
  7473. }
  7474. static void ironlake_init_pch_refclk(struct drm_device *dev)
  7475. {
  7476. struct drm_i915_private *dev_priv = to_i915(dev);
  7477. struct intel_encoder *encoder;
  7478. int i;
  7479. u32 val, final;
  7480. bool has_lvds = false;
  7481. bool has_cpu_edp = false;
  7482. bool has_panel = false;
  7483. bool has_ck505 = false;
  7484. bool can_ssc = false;
  7485. bool using_ssc_source = false;
  7486. /* We need to take the global config into account */
  7487. for_each_intel_encoder(dev, encoder) {
  7488. switch (encoder->type) {
  7489. case INTEL_OUTPUT_LVDS:
  7490. has_panel = true;
  7491. has_lvds = true;
  7492. break;
  7493. case INTEL_OUTPUT_EDP:
  7494. has_panel = true;
  7495. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  7496. has_cpu_edp = true;
  7497. break;
  7498. default:
  7499. break;
  7500. }
  7501. }
  7502. if (HAS_PCH_IBX(dev_priv)) {
  7503. has_ck505 = dev_priv->vbt.display_clock_mode;
  7504. can_ssc = has_ck505;
  7505. } else {
  7506. has_ck505 = false;
  7507. can_ssc = true;
  7508. }
  7509. /* Check if any DPLLs are using the SSC source */
  7510. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  7511. u32 temp = I915_READ(PCH_DPLL(i));
  7512. if (!(temp & DPLL_VCO_ENABLE))
  7513. continue;
  7514. if ((temp & PLL_REF_INPUT_MASK) ==
  7515. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  7516. using_ssc_source = true;
  7517. break;
  7518. }
  7519. }
  7520. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  7521. has_panel, has_lvds, has_ck505, using_ssc_source);
  7522. /* Ironlake: try to setup display ref clock before DPLL
  7523. * enabling. This is only under driver's control after
  7524. * PCH B stepping, previous chipset stepping should be
  7525. * ignoring this setting.
  7526. */
  7527. val = I915_READ(PCH_DREF_CONTROL);
  7528. /* As we must carefully and slowly disable/enable each source in turn,
  7529. * compute the final state we want first and check if we need to
  7530. * make any changes at all.
  7531. */
  7532. final = val;
  7533. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  7534. if (has_ck505)
  7535. final |= DREF_NONSPREAD_CK505_ENABLE;
  7536. else
  7537. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  7538. final &= ~DREF_SSC_SOURCE_MASK;
  7539. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7540. final &= ~DREF_SSC1_ENABLE;
  7541. if (has_panel) {
  7542. final |= DREF_SSC_SOURCE_ENABLE;
  7543. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7544. final |= DREF_SSC1_ENABLE;
  7545. if (has_cpu_edp) {
  7546. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7547. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7548. else
  7549. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7550. } else
  7551. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7552. } else if (using_ssc_source) {
  7553. final |= DREF_SSC_SOURCE_ENABLE;
  7554. final |= DREF_SSC1_ENABLE;
  7555. }
  7556. if (final == val)
  7557. return;
  7558. /* Always enable nonspread source */
  7559. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  7560. if (has_ck505)
  7561. val |= DREF_NONSPREAD_CK505_ENABLE;
  7562. else
  7563. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  7564. if (has_panel) {
  7565. val &= ~DREF_SSC_SOURCE_MASK;
  7566. val |= DREF_SSC_SOURCE_ENABLE;
  7567. /* SSC must be turned on before enabling the CPU output */
  7568. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7569. DRM_DEBUG_KMS("Using SSC on panel\n");
  7570. val |= DREF_SSC1_ENABLE;
  7571. } else
  7572. val &= ~DREF_SSC1_ENABLE;
  7573. /* Get SSC going before enabling the outputs */
  7574. I915_WRITE(PCH_DREF_CONTROL, val);
  7575. POSTING_READ(PCH_DREF_CONTROL);
  7576. udelay(200);
  7577. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7578. /* Enable CPU source on CPU attached eDP */
  7579. if (has_cpu_edp) {
  7580. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7581. DRM_DEBUG_KMS("Using SSC on eDP\n");
  7582. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7583. } else
  7584. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7585. } else
  7586. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7587. I915_WRITE(PCH_DREF_CONTROL, val);
  7588. POSTING_READ(PCH_DREF_CONTROL);
  7589. udelay(200);
  7590. } else {
  7591. DRM_DEBUG_KMS("Disabling CPU source output\n");
  7592. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7593. /* Turn off CPU output */
  7594. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7595. I915_WRITE(PCH_DREF_CONTROL, val);
  7596. POSTING_READ(PCH_DREF_CONTROL);
  7597. udelay(200);
  7598. if (!using_ssc_source) {
  7599. DRM_DEBUG_KMS("Disabling SSC source\n");
  7600. /* Turn off the SSC source */
  7601. val &= ~DREF_SSC_SOURCE_MASK;
  7602. val |= DREF_SSC_SOURCE_DISABLE;
  7603. /* Turn off SSC1 */
  7604. val &= ~DREF_SSC1_ENABLE;
  7605. I915_WRITE(PCH_DREF_CONTROL, val);
  7606. POSTING_READ(PCH_DREF_CONTROL);
  7607. udelay(200);
  7608. }
  7609. }
  7610. BUG_ON(val != final);
  7611. }
  7612. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  7613. {
  7614. uint32_t tmp;
  7615. tmp = I915_READ(SOUTH_CHICKEN2);
  7616. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  7617. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7618. if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
  7619. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  7620. DRM_ERROR("FDI mPHY reset assert timeout\n");
  7621. tmp = I915_READ(SOUTH_CHICKEN2);
  7622. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  7623. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7624. if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
  7625. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  7626. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  7627. }
  7628. /* WaMPhyProgramming:hsw */
  7629. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  7630. {
  7631. uint32_t tmp;
  7632. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  7633. tmp &= ~(0xFF << 24);
  7634. tmp |= (0x12 << 24);
  7635. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  7636. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  7637. tmp |= (1 << 11);
  7638. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  7639. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  7640. tmp |= (1 << 11);
  7641. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  7642. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  7643. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7644. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7645. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7646. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7647. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7648. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7649. tmp &= ~(7 << 13);
  7650. tmp |= (5 << 13);
  7651. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7652. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7653. tmp &= ~(7 << 13);
  7654. tmp |= (5 << 13);
  7655. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7656. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7657. tmp &= ~0xFF;
  7658. tmp |= 0x1C;
  7659. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7660. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7661. tmp &= ~0xFF;
  7662. tmp |= 0x1C;
  7663. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7664. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7665. tmp &= ~(0xFF << 16);
  7666. tmp |= (0x1C << 16);
  7667. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7668. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7669. tmp &= ~(0xFF << 16);
  7670. tmp |= (0x1C << 16);
  7671. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7672. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7673. tmp |= (1 << 27);
  7674. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7675. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7676. tmp |= (1 << 27);
  7677. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7678. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7679. tmp &= ~(0xF << 28);
  7680. tmp |= (4 << 28);
  7681. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7682. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7683. tmp &= ~(0xF << 28);
  7684. tmp |= (4 << 28);
  7685. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7686. }
  7687. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7688. * Programming" based on the parameters passed:
  7689. * - Sequence to enable CLKOUT_DP
  7690. * - Sequence to enable CLKOUT_DP without spread
  7691. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7692. */
  7693. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7694. bool with_fdi)
  7695. {
  7696. struct drm_i915_private *dev_priv = to_i915(dev);
  7697. uint32_t reg, tmp;
  7698. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7699. with_spread = true;
  7700. if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
  7701. with_fdi, "LP PCH doesn't have FDI\n"))
  7702. with_fdi = false;
  7703. mutex_lock(&dev_priv->sb_lock);
  7704. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7705. tmp &= ~SBI_SSCCTL_DISABLE;
  7706. tmp |= SBI_SSCCTL_PATHALT;
  7707. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7708. udelay(24);
  7709. if (with_spread) {
  7710. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7711. tmp &= ~SBI_SSCCTL_PATHALT;
  7712. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7713. if (with_fdi) {
  7714. lpt_reset_fdi_mphy(dev_priv);
  7715. lpt_program_fdi_mphy(dev_priv);
  7716. }
  7717. }
  7718. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  7719. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7720. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7721. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7722. mutex_unlock(&dev_priv->sb_lock);
  7723. }
  7724. /* Sequence to disable CLKOUT_DP */
  7725. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7726. {
  7727. struct drm_i915_private *dev_priv = to_i915(dev);
  7728. uint32_t reg, tmp;
  7729. mutex_lock(&dev_priv->sb_lock);
  7730. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  7731. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7732. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7733. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7734. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7735. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7736. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7737. tmp |= SBI_SSCCTL_PATHALT;
  7738. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7739. udelay(32);
  7740. }
  7741. tmp |= SBI_SSCCTL_DISABLE;
  7742. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7743. }
  7744. mutex_unlock(&dev_priv->sb_lock);
  7745. }
  7746. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  7747. static const uint16_t sscdivintphase[] = {
  7748. [BEND_IDX( 50)] = 0x3B23,
  7749. [BEND_IDX( 45)] = 0x3B23,
  7750. [BEND_IDX( 40)] = 0x3C23,
  7751. [BEND_IDX( 35)] = 0x3C23,
  7752. [BEND_IDX( 30)] = 0x3D23,
  7753. [BEND_IDX( 25)] = 0x3D23,
  7754. [BEND_IDX( 20)] = 0x3E23,
  7755. [BEND_IDX( 15)] = 0x3E23,
  7756. [BEND_IDX( 10)] = 0x3F23,
  7757. [BEND_IDX( 5)] = 0x3F23,
  7758. [BEND_IDX( 0)] = 0x0025,
  7759. [BEND_IDX( -5)] = 0x0025,
  7760. [BEND_IDX(-10)] = 0x0125,
  7761. [BEND_IDX(-15)] = 0x0125,
  7762. [BEND_IDX(-20)] = 0x0225,
  7763. [BEND_IDX(-25)] = 0x0225,
  7764. [BEND_IDX(-30)] = 0x0325,
  7765. [BEND_IDX(-35)] = 0x0325,
  7766. [BEND_IDX(-40)] = 0x0425,
  7767. [BEND_IDX(-45)] = 0x0425,
  7768. [BEND_IDX(-50)] = 0x0525,
  7769. };
  7770. /*
  7771. * Bend CLKOUT_DP
  7772. * steps -50 to 50 inclusive, in steps of 5
  7773. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  7774. * change in clock period = -(steps / 10) * 5.787 ps
  7775. */
  7776. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  7777. {
  7778. uint32_t tmp;
  7779. int idx = BEND_IDX(steps);
  7780. if (WARN_ON(steps % 5 != 0))
  7781. return;
  7782. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  7783. return;
  7784. mutex_lock(&dev_priv->sb_lock);
  7785. if (steps % 10 != 0)
  7786. tmp = 0xAAAAAAAB;
  7787. else
  7788. tmp = 0x00000000;
  7789. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  7790. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  7791. tmp &= 0xffff0000;
  7792. tmp |= sscdivintphase[idx];
  7793. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  7794. mutex_unlock(&dev_priv->sb_lock);
  7795. }
  7796. #undef BEND_IDX
  7797. static void lpt_init_pch_refclk(struct drm_device *dev)
  7798. {
  7799. struct intel_encoder *encoder;
  7800. bool has_vga = false;
  7801. for_each_intel_encoder(dev, encoder) {
  7802. switch (encoder->type) {
  7803. case INTEL_OUTPUT_ANALOG:
  7804. has_vga = true;
  7805. break;
  7806. default:
  7807. break;
  7808. }
  7809. }
  7810. if (has_vga) {
  7811. lpt_bend_clkout_dp(to_i915(dev), 0);
  7812. lpt_enable_clkout_dp(dev, true, true);
  7813. } else {
  7814. lpt_disable_clkout_dp(dev);
  7815. }
  7816. }
  7817. /*
  7818. * Initialize reference clocks when the driver loads
  7819. */
  7820. void intel_init_pch_refclk(struct drm_device *dev)
  7821. {
  7822. struct drm_i915_private *dev_priv = to_i915(dev);
  7823. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
  7824. ironlake_init_pch_refclk(dev);
  7825. else if (HAS_PCH_LPT(dev_priv))
  7826. lpt_init_pch_refclk(dev);
  7827. }
  7828. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7829. {
  7830. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7831. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7832. int pipe = intel_crtc->pipe;
  7833. uint32_t val;
  7834. val = 0;
  7835. switch (intel_crtc->config->pipe_bpp) {
  7836. case 18:
  7837. val |= PIPECONF_6BPC;
  7838. break;
  7839. case 24:
  7840. val |= PIPECONF_8BPC;
  7841. break;
  7842. case 30:
  7843. val |= PIPECONF_10BPC;
  7844. break;
  7845. case 36:
  7846. val |= PIPECONF_12BPC;
  7847. break;
  7848. default:
  7849. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7850. BUG();
  7851. }
  7852. if (intel_crtc->config->dither)
  7853. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7854. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7855. val |= PIPECONF_INTERLACED_ILK;
  7856. else
  7857. val |= PIPECONF_PROGRESSIVE;
  7858. if (intel_crtc->config->limited_color_range)
  7859. val |= PIPECONF_COLOR_RANGE_SELECT;
  7860. I915_WRITE(PIPECONF(pipe), val);
  7861. POSTING_READ(PIPECONF(pipe));
  7862. }
  7863. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7864. {
  7865. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7866. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7867. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7868. u32 val = 0;
  7869. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  7870. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7871. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7872. val |= PIPECONF_INTERLACED_ILK;
  7873. else
  7874. val |= PIPECONF_PROGRESSIVE;
  7875. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7876. POSTING_READ(PIPECONF(cpu_transcoder));
  7877. }
  7878. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  7879. {
  7880. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7881. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7882. if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
  7883. u32 val = 0;
  7884. switch (intel_crtc->config->pipe_bpp) {
  7885. case 18:
  7886. val |= PIPEMISC_DITHER_6_BPC;
  7887. break;
  7888. case 24:
  7889. val |= PIPEMISC_DITHER_8_BPC;
  7890. break;
  7891. case 30:
  7892. val |= PIPEMISC_DITHER_10_BPC;
  7893. break;
  7894. case 36:
  7895. val |= PIPEMISC_DITHER_12_BPC;
  7896. break;
  7897. default:
  7898. /* Case prevented by pipe_config_set_bpp. */
  7899. BUG();
  7900. }
  7901. if (intel_crtc->config->dither)
  7902. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7903. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  7904. }
  7905. }
  7906. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7907. {
  7908. /*
  7909. * Account for spread spectrum to avoid
  7910. * oversubscribing the link. Max center spread
  7911. * is 2.5%; use 5% for safety's sake.
  7912. */
  7913. u32 bps = target_clock * bpp * 21 / 20;
  7914. return DIV_ROUND_UP(bps, link_bw * 8);
  7915. }
  7916. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7917. {
  7918. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7919. }
  7920. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7921. struct intel_crtc_state *crtc_state,
  7922. struct dpll *reduced_clock)
  7923. {
  7924. struct drm_crtc *crtc = &intel_crtc->base;
  7925. struct drm_device *dev = crtc->dev;
  7926. struct drm_i915_private *dev_priv = to_i915(dev);
  7927. u32 dpll, fp, fp2;
  7928. int factor;
  7929. /* Enable autotuning of the PLL clock (if permissible) */
  7930. factor = 21;
  7931. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7932. if ((intel_panel_use_ssc(dev_priv) &&
  7933. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7934. (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
  7935. factor = 25;
  7936. } else if (crtc_state->sdvo_tv_clock)
  7937. factor = 20;
  7938. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7939. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7940. fp |= FP_CB_TUNE;
  7941. if (reduced_clock) {
  7942. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  7943. if (reduced_clock->m < factor * reduced_clock->n)
  7944. fp2 |= FP_CB_TUNE;
  7945. } else {
  7946. fp2 = fp;
  7947. }
  7948. dpll = 0;
  7949. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  7950. dpll |= DPLLB_MODE_LVDS;
  7951. else
  7952. dpll |= DPLLB_MODE_DAC_SERIAL;
  7953. dpll |= (crtc_state->pixel_multiplier - 1)
  7954. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7955. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  7956. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  7957. dpll |= DPLL_SDVO_HIGH_SPEED;
  7958. if (intel_crtc_has_dp_encoder(crtc_state))
  7959. dpll |= DPLL_SDVO_HIGH_SPEED;
  7960. /*
  7961. * The high speed IO clock is only really required for
  7962. * SDVO/HDMI/DP, but we also enable it for CRT to make it
  7963. * possible to share the DPLL between CRT and HDMI. Enabling
  7964. * the clock needlessly does no real harm, except use up a
  7965. * bit of power potentially.
  7966. *
  7967. * We'll limit this to IVB with 3 pipes, since it has only two
  7968. * DPLLs and so DPLL sharing is the only way to get three pipes
  7969. * driving PCH ports at the same time. On SNB we could do this,
  7970. * and potentially avoid enabling the second DPLL, but it's not
  7971. * clear if it''s a win or loss power wise. No point in doing
  7972. * this on ILK at all since it has a fixed DPLL<->pipe mapping.
  7973. */
  7974. if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
  7975. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
  7976. dpll |= DPLL_SDVO_HIGH_SPEED;
  7977. /* compute bitmask from p1 value */
  7978. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7979. /* also FPA1 */
  7980. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7981. switch (crtc_state->dpll.p2) {
  7982. case 5:
  7983. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  7984. break;
  7985. case 7:
  7986. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  7987. break;
  7988. case 10:
  7989. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  7990. break;
  7991. case 14:
  7992. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  7993. break;
  7994. }
  7995. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  7996. intel_panel_use_ssc(dev_priv))
  7997. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  7998. else
  7999. dpll |= PLL_REF_INPUT_DREFCLK;
  8000. dpll |= DPLL_VCO_ENABLE;
  8001. crtc_state->dpll_hw_state.dpll = dpll;
  8002. crtc_state->dpll_hw_state.fp0 = fp;
  8003. crtc_state->dpll_hw_state.fp1 = fp2;
  8004. }
  8005. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  8006. struct intel_crtc_state *crtc_state)
  8007. {
  8008. struct drm_device *dev = crtc->base.dev;
  8009. struct drm_i915_private *dev_priv = to_i915(dev);
  8010. struct dpll reduced_clock;
  8011. bool has_reduced_clock = false;
  8012. struct intel_shared_dpll *pll;
  8013. const struct intel_limit *limit;
  8014. int refclk = 120000;
  8015. memset(&crtc_state->dpll_hw_state, 0,
  8016. sizeof(crtc_state->dpll_hw_state));
  8017. crtc->lowfreq_avail = false;
  8018. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  8019. if (!crtc_state->has_pch_encoder)
  8020. return 0;
  8021. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  8022. if (intel_panel_use_ssc(dev_priv)) {
  8023. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  8024. dev_priv->vbt.lvds_ssc_freq);
  8025. refclk = dev_priv->vbt.lvds_ssc_freq;
  8026. }
  8027. if (intel_is_dual_link_lvds(dev)) {
  8028. if (refclk == 100000)
  8029. limit = &intel_limits_ironlake_dual_lvds_100m;
  8030. else
  8031. limit = &intel_limits_ironlake_dual_lvds;
  8032. } else {
  8033. if (refclk == 100000)
  8034. limit = &intel_limits_ironlake_single_lvds_100m;
  8035. else
  8036. limit = &intel_limits_ironlake_single_lvds;
  8037. }
  8038. } else {
  8039. limit = &intel_limits_ironlake_dac;
  8040. }
  8041. if (!crtc_state->clock_set &&
  8042. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  8043. refclk, NULL, &crtc_state->dpll)) {
  8044. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  8045. return -EINVAL;
  8046. }
  8047. ironlake_compute_dpll(crtc, crtc_state,
  8048. has_reduced_clock ? &reduced_clock : NULL);
  8049. pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
  8050. if (pll == NULL) {
  8051. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  8052. pipe_name(crtc->pipe));
  8053. return -EINVAL;
  8054. }
  8055. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  8056. has_reduced_clock)
  8057. crtc->lowfreq_avail = true;
  8058. return 0;
  8059. }
  8060. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  8061. struct intel_link_m_n *m_n)
  8062. {
  8063. struct drm_device *dev = crtc->base.dev;
  8064. struct drm_i915_private *dev_priv = to_i915(dev);
  8065. enum pipe pipe = crtc->pipe;
  8066. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  8067. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  8068. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  8069. & ~TU_SIZE_MASK;
  8070. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  8071. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  8072. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8073. }
  8074. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  8075. enum transcoder transcoder,
  8076. struct intel_link_m_n *m_n,
  8077. struct intel_link_m_n *m2_n2)
  8078. {
  8079. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8080. enum pipe pipe = crtc->pipe;
  8081. if (INTEL_GEN(dev_priv) >= 5) {
  8082. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  8083. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  8084. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  8085. & ~TU_SIZE_MASK;
  8086. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  8087. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  8088. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8089. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  8090. * gen < 8) and if DRRS is supported (to make sure the
  8091. * registers are not unnecessarily read).
  8092. */
  8093. if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
  8094. crtc->config->has_drrs) {
  8095. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  8096. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  8097. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  8098. & ~TU_SIZE_MASK;
  8099. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  8100. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  8101. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8102. }
  8103. } else {
  8104. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  8105. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  8106. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  8107. & ~TU_SIZE_MASK;
  8108. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  8109. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  8110. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8111. }
  8112. }
  8113. void intel_dp_get_m_n(struct intel_crtc *crtc,
  8114. struct intel_crtc_state *pipe_config)
  8115. {
  8116. if (pipe_config->has_pch_encoder)
  8117. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  8118. else
  8119. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  8120. &pipe_config->dp_m_n,
  8121. &pipe_config->dp_m2_n2);
  8122. }
  8123. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  8124. struct intel_crtc_state *pipe_config)
  8125. {
  8126. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  8127. &pipe_config->fdi_m_n, NULL);
  8128. }
  8129. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  8130. struct intel_crtc_state *pipe_config)
  8131. {
  8132. struct drm_device *dev = crtc->base.dev;
  8133. struct drm_i915_private *dev_priv = to_i915(dev);
  8134. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  8135. uint32_t ps_ctrl = 0;
  8136. int id = -1;
  8137. int i;
  8138. /* find scaler attached to this pipe */
  8139. for (i = 0; i < crtc->num_scalers; i++) {
  8140. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  8141. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  8142. id = i;
  8143. pipe_config->pch_pfit.enabled = true;
  8144. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  8145. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  8146. break;
  8147. }
  8148. }
  8149. scaler_state->scaler_id = id;
  8150. if (id >= 0) {
  8151. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  8152. } else {
  8153. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8154. }
  8155. }
  8156. static void
  8157. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  8158. struct intel_initial_plane_config *plane_config)
  8159. {
  8160. struct drm_device *dev = crtc->base.dev;
  8161. struct drm_i915_private *dev_priv = to_i915(dev);
  8162. u32 val, base, offset, stride_mult, tiling;
  8163. int pipe = crtc->pipe;
  8164. int fourcc, pixel_format;
  8165. unsigned int aligned_height;
  8166. struct drm_framebuffer *fb;
  8167. struct intel_framebuffer *intel_fb;
  8168. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8169. if (!intel_fb) {
  8170. DRM_DEBUG_KMS("failed to alloc fb\n");
  8171. return;
  8172. }
  8173. fb = &intel_fb->base;
  8174. val = I915_READ(PLANE_CTL(pipe, 0));
  8175. if (!(val & PLANE_CTL_ENABLE))
  8176. goto error;
  8177. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  8178. fourcc = skl_format_to_fourcc(pixel_format,
  8179. val & PLANE_CTL_ORDER_RGBX,
  8180. val & PLANE_CTL_ALPHA_MASK);
  8181. fb->pixel_format = fourcc;
  8182. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  8183. tiling = val & PLANE_CTL_TILED_MASK;
  8184. switch (tiling) {
  8185. case PLANE_CTL_TILED_LINEAR:
  8186. fb->modifier = DRM_FORMAT_MOD_NONE;
  8187. break;
  8188. case PLANE_CTL_TILED_X:
  8189. plane_config->tiling = I915_TILING_X;
  8190. fb->modifier = I915_FORMAT_MOD_X_TILED;
  8191. break;
  8192. case PLANE_CTL_TILED_Y:
  8193. fb->modifier = I915_FORMAT_MOD_Y_TILED;
  8194. break;
  8195. case PLANE_CTL_TILED_YF:
  8196. fb->modifier = I915_FORMAT_MOD_Yf_TILED;
  8197. break;
  8198. default:
  8199. MISSING_CASE(tiling);
  8200. goto error;
  8201. }
  8202. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  8203. plane_config->base = base;
  8204. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  8205. val = I915_READ(PLANE_SIZE(pipe, 0));
  8206. fb->height = ((val >> 16) & 0xfff) + 1;
  8207. fb->width = ((val >> 0) & 0x1fff) + 1;
  8208. val = I915_READ(PLANE_STRIDE(pipe, 0));
  8209. stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier,
  8210. fb->pixel_format);
  8211. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  8212. aligned_height = intel_fb_align_height(dev, fb->height,
  8213. fb->pixel_format,
  8214. fb->modifier);
  8215. plane_config->size = fb->pitches[0] * aligned_height;
  8216. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  8217. pipe_name(pipe), fb->width, fb->height,
  8218. fb->bits_per_pixel, base, fb->pitches[0],
  8219. plane_config->size);
  8220. plane_config->fb = intel_fb;
  8221. return;
  8222. error:
  8223. kfree(intel_fb);
  8224. }
  8225. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  8226. struct intel_crtc_state *pipe_config)
  8227. {
  8228. struct drm_device *dev = crtc->base.dev;
  8229. struct drm_i915_private *dev_priv = to_i915(dev);
  8230. uint32_t tmp;
  8231. tmp = I915_READ(PF_CTL(crtc->pipe));
  8232. if (tmp & PF_ENABLE) {
  8233. pipe_config->pch_pfit.enabled = true;
  8234. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  8235. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  8236. /* We currently do not free assignements of panel fitters on
  8237. * ivb/hsw (since we don't use the higher upscaling modes which
  8238. * differentiates them) so just WARN about this case for now. */
  8239. if (IS_GEN7(dev_priv)) {
  8240. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  8241. PF_PIPE_SEL_IVB(crtc->pipe));
  8242. }
  8243. }
  8244. }
  8245. static void
  8246. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  8247. struct intel_initial_plane_config *plane_config)
  8248. {
  8249. struct drm_device *dev = crtc->base.dev;
  8250. struct drm_i915_private *dev_priv = to_i915(dev);
  8251. u32 val, base, offset;
  8252. int pipe = crtc->pipe;
  8253. int fourcc, pixel_format;
  8254. unsigned int aligned_height;
  8255. struct drm_framebuffer *fb;
  8256. struct intel_framebuffer *intel_fb;
  8257. val = I915_READ(DSPCNTR(pipe));
  8258. if (!(val & DISPLAY_PLANE_ENABLE))
  8259. return;
  8260. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8261. if (!intel_fb) {
  8262. DRM_DEBUG_KMS("failed to alloc fb\n");
  8263. return;
  8264. }
  8265. fb = &intel_fb->base;
  8266. if (INTEL_GEN(dev_priv) >= 4) {
  8267. if (val & DISPPLANE_TILED) {
  8268. plane_config->tiling = I915_TILING_X;
  8269. fb->modifier = I915_FORMAT_MOD_X_TILED;
  8270. }
  8271. }
  8272. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  8273. fourcc = i9xx_format_to_fourcc(pixel_format);
  8274. fb->pixel_format = fourcc;
  8275. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  8276. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  8277. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  8278. offset = I915_READ(DSPOFFSET(pipe));
  8279. } else {
  8280. if (plane_config->tiling)
  8281. offset = I915_READ(DSPTILEOFF(pipe));
  8282. else
  8283. offset = I915_READ(DSPLINOFF(pipe));
  8284. }
  8285. plane_config->base = base;
  8286. val = I915_READ(PIPESRC(pipe));
  8287. fb->width = ((val >> 16) & 0xfff) + 1;
  8288. fb->height = ((val >> 0) & 0xfff) + 1;
  8289. val = I915_READ(DSPSTRIDE(pipe));
  8290. fb->pitches[0] = val & 0xffffffc0;
  8291. aligned_height = intel_fb_align_height(dev, fb->height,
  8292. fb->pixel_format,
  8293. fb->modifier);
  8294. plane_config->size = fb->pitches[0] * aligned_height;
  8295. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  8296. pipe_name(pipe), fb->width, fb->height,
  8297. fb->bits_per_pixel, base, fb->pitches[0],
  8298. plane_config->size);
  8299. plane_config->fb = intel_fb;
  8300. }
  8301. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  8302. struct intel_crtc_state *pipe_config)
  8303. {
  8304. struct drm_device *dev = crtc->base.dev;
  8305. struct drm_i915_private *dev_priv = to_i915(dev);
  8306. enum intel_display_power_domain power_domain;
  8307. uint32_t tmp;
  8308. bool ret;
  8309. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  8310. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8311. return false;
  8312. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8313. pipe_config->shared_dpll = NULL;
  8314. ret = false;
  8315. tmp = I915_READ(PIPECONF(crtc->pipe));
  8316. if (!(tmp & PIPECONF_ENABLE))
  8317. goto out;
  8318. switch (tmp & PIPECONF_BPC_MASK) {
  8319. case PIPECONF_6BPC:
  8320. pipe_config->pipe_bpp = 18;
  8321. break;
  8322. case PIPECONF_8BPC:
  8323. pipe_config->pipe_bpp = 24;
  8324. break;
  8325. case PIPECONF_10BPC:
  8326. pipe_config->pipe_bpp = 30;
  8327. break;
  8328. case PIPECONF_12BPC:
  8329. pipe_config->pipe_bpp = 36;
  8330. break;
  8331. default:
  8332. break;
  8333. }
  8334. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  8335. pipe_config->limited_color_range = true;
  8336. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  8337. struct intel_shared_dpll *pll;
  8338. enum intel_dpll_id pll_id;
  8339. pipe_config->has_pch_encoder = true;
  8340. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  8341. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8342. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8343. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8344. if (HAS_PCH_IBX(dev_priv)) {
  8345. /*
  8346. * The pipe->pch transcoder and pch transcoder->pll
  8347. * mapping is fixed.
  8348. */
  8349. pll_id = (enum intel_dpll_id) crtc->pipe;
  8350. } else {
  8351. tmp = I915_READ(PCH_DPLL_SEL);
  8352. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  8353. pll_id = DPLL_ID_PCH_PLL_B;
  8354. else
  8355. pll_id= DPLL_ID_PCH_PLL_A;
  8356. }
  8357. pipe_config->shared_dpll =
  8358. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  8359. pll = pipe_config->shared_dpll;
  8360. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  8361. &pipe_config->dpll_hw_state));
  8362. tmp = pipe_config->dpll_hw_state.dpll;
  8363. pipe_config->pixel_multiplier =
  8364. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  8365. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  8366. ironlake_pch_clock_get(crtc, pipe_config);
  8367. } else {
  8368. pipe_config->pixel_multiplier = 1;
  8369. }
  8370. intel_get_pipe_timings(crtc, pipe_config);
  8371. intel_get_pipe_src_size(crtc, pipe_config);
  8372. ironlake_get_pfit_config(crtc, pipe_config);
  8373. ret = true;
  8374. out:
  8375. intel_display_power_put(dev_priv, power_domain);
  8376. return ret;
  8377. }
  8378. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  8379. {
  8380. struct drm_device *dev = &dev_priv->drm;
  8381. struct intel_crtc *crtc;
  8382. for_each_intel_crtc(dev, crtc)
  8383. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  8384. pipe_name(crtc->pipe));
  8385. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  8386. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  8387. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  8388. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  8389. I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
  8390. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  8391. "CPU PWM1 enabled\n");
  8392. if (IS_HASWELL(dev_priv))
  8393. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  8394. "CPU PWM2 enabled\n");
  8395. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  8396. "PCH PWM1 enabled\n");
  8397. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  8398. "Utility pin enabled\n");
  8399. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  8400. /*
  8401. * In theory we can still leave IRQs enabled, as long as only the HPD
  8402. * interrupts remain enabled. We used to check for that, but since it's
  8403. * gen-specific and since we only disable LCPLL after we fully disable
  8404. * the interrupts, the check below should be enough.
  8405. */
  8406. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  8407. }
  8408. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  8409. {
  8410. if (IS_HASWELL(dev_priv))
  8411. return I915_READ(D_COMP_HSW);
  8412. else
  8413. return I915_READ(D_COMP_BDW);
  8414. }
  8415. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  8416. {
  8417. if (IS_HASWELL(dev_priv)) {
  8418. mutex_lock(&dev_priv->rps.hw_lock);
  8419. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  8420. val))
  8421. DRM_DEBUG_KMS("Failed to write to D_COMP\n");
  8422. mutex_unlock(&dev_priv->rps.hw_lock);
  8423. } else {
  8424. I915_WRITE(D_COMP_BDW, val);
  8425. POSTING_READ(D_COMP_BDW);
  8426. }
  8427. }
  8428. /*
  8429. * This function implements pieces of two sequences from BSpec:
  8430. * - Sequence for display software to disable LCPLL
  8431. * - Sequence for display software to allow package C8+
  8432. * The steps implemented here are just the steps that actually touch the LCPLL
  8433. * register. Callers should take care of disabling all the display engine
  8434. * functions, doing the mode unset, fixing interrupts, etc.
  8435. */
  8436. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  8437. bool switch_to_fclk, bool allow_power_down)
  8438. {
  8439. uint32_t val;
  8440. assert_can_disable_lcpll(dev_priv);
  8441. val = I915_READ(LCPLL_CTL);
  8442. if (switch_to_fclk) {
  8443. val |= LCPLL_CD_SOURCE_FCLK;
  8444. I915_WRITE(LCPLL_CTL, val);
  8445. if (wait_for_us(I915_READ(LCPLL_CTL) &
  8446. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8447. DRM_ERROR("Switching to FCLK failed\n");
  8448. val = I915_READ(LCPLL_CTL);
  8449. }
  8450. val |= LCPLL_PLL_DISABLE;
  8451. I915_WRITE(LCPLL_CTL, val);
  8452. POSTING_READ(LCPLL_CTL);
  8453. if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
  8454. DRM_ERROR("LCPLL still locked\n");
  8455. val = hsw_read_dcomp(dev_priv);
  8456. val |= D_COMP_COMP_DISABLE;
  8457. hsw_write_dcomp(dev_priv, val);
  8458. ndelay(100);
  8459. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  8460. 1))
  8461. DRM_ERROR("D_COMP RCOMP still in progress\n");
  8462. if (allow_power_down) {
  8463. val = I915_READ(LCPLL_CTL);
  8464. val |= LCPLL_POWER_DOWN_ALLOW;
  8465. I915_WRITE(LCPLL_CTL, val);
  8466. POSTING_READ(LCPLL_CTL);
  8467. }
  8468. }
  8469. /*
  8470. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  8471. * source.
  8472. */
  8473. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  8474. {
  8475. uint32_t val;
  8476. val = I915_READ(LCPLL_CTL);
  8477. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  8478. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  8479. return;
  8480. /*
  8481. * Make sure we're not on PC8 state before disabling PC8, otherwise
  8482. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  8483. */
  8484. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  8485. if (val & LCPLL_POWER_DOWN_ALLOW) {
  8486. val &= ~LCPLL_POWER_DOWN_ALLOW;
  8487. I915_WRITE(LCPLL_CTL, val);
  8488. POSTING_READ(LCPLL_CTL);
  8489. }
  8490. val = hsw_read_dcomp(dev_priv);
  8491. val |= D_COMP_COMP_FORCE;
  8492. val &= ~D_COMP_COMP_DISABLE;
  8493. hsw_write_dcomp(dev_priv, val);
  8494. val = I915_READ(LCPLL_CTL);
  8495. val &= ~LCPLL_PLL_DISABLE;
  8496. I915_WRITE(LCPLL_CTL, val);
  8497. if (intel_wait_for_register(dev_priv,
  8498. LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  8499. 5))
  8500. DRM_ERROR("LCPLL not locked yet\n");
  8501. if (val & LCPLL_CD_SOURCE_FCLK) {
  8502. val = I915_READ(LCPLL_CTL);
  8503. val &= ~LCPLL_CD_SOURCE_FCLK;
  8504. I915_WRITE(LCPLL_CTL, val);
  8505. if (wait_for_us((I915_READ(LCPLL_CTL) &
  8506. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8507. DRM_ERROR("Switching back to LCPLL failed\n");
  8508. }
  8509. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  8510. intel_update_cdclk(dev_priv);
  8511. }
  8512. /*
  8513. * Package states C8 and deeper are really deep PC states that can only be
  8514. * reached when all the devices on the system allow it, so even if the graphics
  8515. * device allows PC8+, it doesn't mean the system will actually get to these
  8516. * states. Our driver only allows PC8+ when going into runtime PM.
  8517. *
  8518. * The requirements for PC8+ are that all the outputs are disabled, the power
  8519. * well is disabled and most interrupts are disabled, and these are also
  8520. * requirements for runtime PM. When these conditions are met, we manually do
  8521. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  8522. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  8523. * hang the machine.
  8524. *
  8525. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  8526. * the state of some registers, so when we come back from PC8+ we need to
  8527. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  8528. * need to take care of the registers kept by RC6. Notice that this happens even
  8529. * if we don't put the device in PCI D3 state (which is what currently happens
  8530. * because of the runtime PM support).
  8531. *
  8532. * For more, read "Display Sequences for Package C8" on the hardware
  8533. * documentation.
  8534. */
  8535. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  8536. {
  8537. struct drm_device *dev = &dev_priv->drm;
  8538. uint32_t val;
  8539. DRM_DEBUG_KMS("Enabling package C8+\n");
  8540. if (HAS_PCH_LPT_LP(dev_priv)) {
  8541. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8542. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  8543. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8544. }
  8545. lpt_disable_clkout_dp(dev);
  8546. hsw_disable_lcpll(dev_priv, true, true);
  8547. }
  8548. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  8549. {
  8550. struct drm_device *dev = &dev_priv->drm;
  8551. uint32_t val;
  8552. DRM_DEBUG_KMS("Disabling package C8+\n");
  8553. hsw_restore_lcpll(dev_priv);
  8554. lpt_init_pch_refclk(dev);
  8555. if (HAS_PCH_LPT_LP(dev_priv)) {
  8556. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8557. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  8558. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8559. }
  8560. }
  8561. static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8562. {
  8563. struct drm_device *dev = old_state->dev;
  8564. struct intel_atomic_state *old_intel_state =
  8565. to_intel_atomic_state(old_state);
  8566. unsigned int req_cdclk = old_intel_state->dev_cdclk;
  8567. bxt_set_cdclk(to_i915(dev), req_cdclk);
  8568. }
  8569. static int bdw_adjust_min_pipe_pixel_rate(struct intel_crtc_state *crtc_state,
  8570. int pixel_rate)
  8571. {
  8572. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  8573. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  8574. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  8575. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  8576. /* BSpec says "Do not use DisplayPort with CDCLK less than
  8577. * 432 MHz, audio enabled, port width x4, and link rate
  8578. * HBR2 (5.4 GHz), or else there may be audio corruption or
  8579. * screen corruption."
  8580. */
  8581. if (intel_crtc_has_dp_encoder(crtc_state) &&
  8582. crtc_state->has_audio &&
  8583. crtc_state->port_clock >= 540000 &&
  8584. crtc_state->lane_count == 4)
  8585. pixel_rate = max(432000, pixel_rate);
  8586. return pixel_rate;
  8587. }
  8588. /* compute the max rate for new configuration */
  8589. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  8590. {
  8591. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8592. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8593. struct drm_crtc *crtc;
  8594. struct drm_crtc_state *cstate;
  8595. struct intel_crtc_state *crtc_state;
  8596. unsigned max_pixel_rate = 0, i;
  8597. enum pipe pipe;
  8598. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  8599. sizeof(intel_state->min_pixclk));
  8600. for_each_crtc_in_state(state, crtc, cstate, i) {
  8601. int pixel_rate;
  8602. crtc_state = to_intel_crtc_state(cstate);
  8603. if (!crtc_state->base.enable) {
  8604. intel_state->min_pixclk[i] = 0;
  8605. continue;
  8606. }
  8607. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  8608. if (IS_BROADWELL(dev_priv) || IS_GEN9(dev_priv))
  8609. pixel_rate = bdw_adjust_min_pipe_pixel_rate(crtc_state,
  8610. pixel_rate);
  8611. intel_state->min_pixclk[i] = pixel_rate;
  8612. }
  8613. for_each_pipe(dev_priv, pipe)
  8614. max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
  8615. return max_pixel_rate;
  8616. }
  8617. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  8618. {
  8619. struct drm_i915_private *dev_priv = to_i915(dev);
  8620. uint32_t val, data;
  8621. int ret;
  8622. if (WARN((I915_READ(LCPLL_CTL) &
  8623. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  8624. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  8625. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  8626. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  8627. "trying to change cdclk frequency with cdclk not enabled\n"))
  8628. return;
  8629. mutex_lock(&dev_priv->rps.hw_lock);
  8630. ret = sandybridge_pcode_write(dev_priv,
  8631. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  8632. mutex_unlock(&dev_priv->rps.hw_lock);
  8633. if (ret) {
  8634. DRM_ERROR("failed to inform pcode about cdclk change\n");
  8635. return;
  8636. }
  8637. val = I915_READ(LCPLL_CTL);
  8638. val |= LCPLL_CD_SOURCE_FCLK;
  8639. I915_WRITE(LCPLL_CTL, val);
  8640. if (wait_for_us(I915_READ(LCPLL_CTL) &
  8641. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8642. DRM_ERROR("Switching to FCLK failed\n");
  8643. val = I915_READ(LCPLL_CTL);
  8644. val &= ~LCPLL_CLK_FREQ_MASK;
  8645. switch (cdclk) {
  8646. case 450000:
  8647. val |= LCPLL_CLK_FREQ_450;
  8648. data = 0;
  8649. break;
  8650. case 540000:
  8651. val |= LCPLL_CLK_FREQ_54O_BDW;
  8652. data = 1;
  8653. break;
  8654. case 337500:
  8655. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8656. data = 2;
  8657. break;
  8658. case 675000:
  8659. val |= LCPLL_CLK_FREQ_675_BDW;
  8660. data = 3;
  8661. break;
  8662. default:
  8663. WARN(1, "invalid cdclk frequency\n");
  8664. return;
  8665. }
  8666. I915_WRITE(LCPLL_CTL, val);
  8667. val = I915_READ(LCPLL_CTL);
  8668. val &= ~LCPLL_CD_SOURCE_FCLK;
  8669. I915_WRITE(LCPLL_CTL, val);
  8670. if (wait_for_us((I915_READ(LCPLL_CTL) &
  8671. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8672. DRM_ERROR("Switching back to LCPLL failed\n");
  8673. mutex_lock(&dev_priv->rps.hw_lock);
  8674. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8675. mutex_unlock(&dev_priv->rps.hw_lock);
  8676. I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
  8677. intel_update_cdclk(dev_priv);
  8678. WARN(cdclk != dev_priv->cdclk_freq,
  8679. "cdclk requested %d kHz but got %d kHz\n",
  8680. cdclk, dev_priv->cdclk_freq);
  8681. }
  8682. static int broadwell_calc_cdclk(int max_pixclk)
  8683. {
  8684. if (max_pixclk > 540000)
  8685. return 675000;
  8686. else if (max_pixclk > 450000)
  8687. return 540000;
  8688. else if (max_pixclk > 337500)
  8689. return 450000;
  8690. else
  8691. return 337500;
  8692. }
  8693. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8694. {
  8695. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8696. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8697. int max_pixclk = ilk_max_pixel_rate(state);
  8698. int cdclk;
  8699. /*
  8700. * FIXME should also account for plane ratio
  8701. * once 64bpp pixel formats are supported.
  8702. */
  8703. cdclk = broadwell_calc_cdclk(max_pixclk);
  8704. if (cdclk > dev_priv->max_cdclk_freq) {
  8705. DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8706. cdclk, dev_priv->max_cdclk_freq);
  8707. return -EINVAL;
  8708. }
  8709. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8710. if (!intel_state->active_crtcs)
  8711. intel_state->dev_cdclk = broadwell_calc_cdclk(0);
  8712. return 0;
  8713. }
  8714. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8715. {
  8716. struct drm_device *dev = old_state->dev;
  8717. struct intel_atomic_state *old_intel_state =
  8718. to_intel_atomic_state(old_state);
  8719. unsigned req_cdclk = old_intel_state->dev_cdclk;
  8720. broadwell_set_cdclk(dev, req_cdclk);
  8721. }
  8722. static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
  8723. {
  8724. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8725. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8726. const int max_pixclk = ilk_max_pixel_rate(state);
  8727. int vco = intel_state->cdclk_pll_vco;
  8728. int cdclk;
  8729. /*
  8730. * FIXME should also account for plane ratio
  8731. * once 64bpp pixel formats are supported.
  8732. */
  8733. cdclk = skl_calc_cdclk(max_pixclk, vco);
  8734. /*
  8735. * FIXME move the cdclk caclulation to
  8736. * compute_config() so we can fail gracegully.
  8737. */
  8738. if (cdclk > dev_priv->max_cdclk_freq) {
  8739. DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8740. cdclk, dev_priv->max_cdclk_freq);
  8741. cdclk = dev_priv->max_cdclk_freq;
  8742. }
  8743. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8744. if (!intel_state->active_crtcs)
  8745. intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
  8746. return 0;
  8747. }
  8748. static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8749. {
  8750. struct drm_i915_private *dev_priv = to_i915(old_state->dev);
  8751. struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
  8752. unsigned int req_cdclk = intel_state->dev_cdclk;
  8753. unsigned int req_vco = intel_state->cdclk_pll_vco;
  8754. skl_set_cdclk(dev_priv, req_cdclk, req_vco);
  8755. }
  8756. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8757. struct intel_crtc_state *crtc_state)
  8758. {
  8759. if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
  8760. if (!intel_ddi_pll_select(crtc, crtc_state))
  8761. return -EINVAL;
  8762. }
  8763. crtc->lowfreq_avail = false;
  8764. return 0;
  8765. }
  8766. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8767. enum port port,
  8768. struct intel_crtc_state *pipe_config)
  8769. {
  8770. enum intel_dpll_id id;
  8771. switch (port) {
  8772. case PORT_A:
  8773. id = DPLL_ID_SKL_DPLL0;
  8774. break;
  8775. case PORT_B:
  8776. id = DPLL_ID_SKL_DPLL1;
  8777. break;
  8778. case PORT_C:
  8779. id = DPLL_ID_SKL_DPLL2;
  8780. break;
  8781. default:
  8782. DRM_ERROR("Incorrect port type\n");
  8783. return;
  8784. }
  8785. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8786. }
  8787. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8788. enum port port,
  8789. struct intel_crtc_state *pipe_config)
  8790. {
  8791. enum intel_dpll_id id;
  8792. u32 temp;
  8793. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8794. id = temp >> (port * 3 + 1);
  8795. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
  8796. return;
  8797. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8798. }
  8799. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8800. enum port port,
  8801. struct intel_crtc_state *pipe_config)
  8802. {
  8803. enum intel_dpll_id id;
  8804. uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8805. switch (ddi_pll_sel) {
  8806. case PORT_CLK_SEL_WRPLL1:
  8807. id = DPLL_ID_WRPLL1;
  8808. break;
  8809. case PORT_CLK_SEL_WRPLL2:
  8810. id = DPLL_ID_WRPLL2;
  8811. break;
  8812. case PORT_CLK_SEL_SPLL:
  8813. id = DPLL_ID_SPLL;
  8814. break;
  8815. case PORT_CLK_SEL_LCPLL_810:
  8816. id = DPLL_ID_LCPLL_810;
  8817. break;
  8818. case PORT_CLK_SEL_LCPLL_1350:
  8819. id = DPLL_ID_LCPLL_1350;
  8820. break;
  8821. case PORT_CLK_SEL_LCPLL_2700:
  8822. id = DPLL_ID_LCPLL_2700;
  8823. break;
  8824. default:
  8825. MISSING_CASE(ddi_pll_sel);
  8826. /* fall through */
  8827. case PORT_CLK_SEL_NONE:
  8828. return;
  8829. }
  8830. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8831. }
  8832. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  8833. struct intel_crtc_state *pipe_config,
  8834. unsigned long *power_domain_mask)
  8835. {
  8836. struct drm_device *dev = crtc->base.dev;
  8837. struct drm_i915_private *dev_priv = to_i915(dev);
  8838. enum intel_display_power_domain power_domain;
  8839. u32 tmp;
  8840. /*
  8841. * The pipe->transcoder mapping is fixed with the exception of the eDP
  8842. * transcoder handled below.
  8843. */
  8844. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8845. /*
  8846. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  8847. * consistency and less surprising code; it's in always on power).
  8848. */
  8849. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8850. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8851. enum pipe trans_edp_pipe;
  8852. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8853. default:
  8854. WARN(1, "unknown pipe linked to edp transcoder\n");
  8855. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8856. case TRANS_DDI_EDP_INPUT_A_ON:
  8857. trans_edp_pipe = PIPE_A;
  8858. break;
  8859. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8860. trans_edp_pipe = PIPE_B;
  8861. break;
  8862. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8863. trans_edp_pipe = PIPE_C;
  8864. break;
  8865. }
  8866. if (trans_edp_pipe == crtc->pipe)
  8867. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8868. }
  8869. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  8870. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8871. return false;
  8872. *power_domain_mask |= BIT(power_domain);
  8873. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8874. return tmp & PIPECONF_ENABLE;
  8875. }
  8876. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  8877. struct intel_crtc_state *pipe_config,
  8878. unsigned long *power_domain_mask)
  8879. {
  8880. struct drm_device *dev = crtc->base.dev;
  8881. struct drm_i915_private *dev_priv = to_i915(dev);
  8882. enum intel_display_power_domain power_domain;
  8883. enum port port;
  8884. enum transcoder cpu_transcoder;
  8885. u32 tmp;
  8886. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  8887. if (port == PORT_A)
  8888. cpu_transcoder = TRANSCODER_DSI_A;
  8889. else
  8890. cpu_transcoder = TRANSCODER_DSI_C;
  8891. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  8892. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8893. continue;
  8894. *power_domain_mask |= BIT(power_domain);
  8895. /*
  8896. * The PLL needs to be enabled with a valid divider
  8897. * configuration, otherwise accessing DSI registers will hang
  8898. * the machine. See BSpec North Display Engine
  8899. * registers/MIPI[BXT]. We can break out here early, since we
  8900. * need the same DSI PLL to be enabled for both DSI ports.
  8901. */
  8902. if (!intel_dsi_pll_is_enabled(dev_priv))
  8903. break;
  8904. /* XXX: this works for video mode only */
  8905. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  8906. if (!(tmp & DPI_ENABLE))
  8907. continue;
  8908. tmp = I915_READ(MIPI_CTRL(port));
  8909. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  8910. continue;
  8911. pipe_config->cpu_transcoder = cpu_transcoder;
  8912. break;
  8913. }
  8914. return transcoder_is_dsi(pipe_config->cpu_transcoder);
  8915. }
  8916. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8917. struct intel_crtc_state *pipe_config)
  8918. {
  8919. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8920. struct intel_shared_dpll *pll;
  8921. enum port port;
  8922. uint32_t tmp;
  8923. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8924. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8925. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  8926. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8927. else if (IS_BROXTON(dev_priv))
  8928. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8929. else
  8930. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8931. pll = pipe_config->shared_dpll;
  8932. if (pll) {
  8933. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  8934. &pipe_config->dpll_hw_state));
  8935. }
  8936. /*
  8937. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8938. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8939. * the PCH transcoder is on.
  8940. */
  8941. if (INTEL_GEN(dev_priv) < 9 &&
  8942. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8943. pipe_config->has_pch_encoder = true;
  8944. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8945. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8946. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8947. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8948. }
  8949. }
  8950. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8951. struct intel_crtc_state *pipe_config)
  8952. {
  8953. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8954. enum intel_display_power_domain power_domain;
  8955. unsigned long power_domain_mask;
  8956. bool active;
  8957. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  8958. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8959. return false;
  8960. power_domain_mask = BIT(power_domain);
  8961. pipe_config->shared_dpll = NULL;
  8962. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  8963. if (IS_BROXTON(dev_priv) &&
  8964. bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
  8965. WARN_ON(active);
  8966. active = true;
  8967. }
  8968. if (!active)
  8969. goto out;
  8970. if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  8971. haswell_get_ddi_port_state(crtc, pipe_config);
  8972. intel_get_pipe_timings(crtc, pipe_config);
  8973. }
  8974. intel_get_pipe_src_size(crtc, pipe_config);
  8975. pipe_config->gamma_mode =
  8976. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  8977. if (INTEL_GEN(dev_priv) >= 9) {
  8978. skl_init_scalers(dev_priv, crtc, pipe_config);
  8979. pipe_config->scaler_state.scaler_id = -1;
  8980. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8981. }
  8982. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8983. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  8984. power_domain_mask |= BIT(power_domain);
  8985. if (INTEL_GEN(dev_priv) >= 9)
  8986. skylake_get_pfit_config(crtc, pipe_config);
  8987. else
  8988. ironlake_get_pfit_config(crtc, pipe_config);
  8989. }
  8990. if (IS_HASWELL(dev_priv))
  8991. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  8992. (I915_READ(IPS_CTL) & IPS_ENABLE);
  8993. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  8994. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  8995. pipe_config->pixel_multiplier =
  8996. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  8997. } else {
  8998. pipe_config->pixel_multiplier = 1;
  8999. }
  9000. out:
  9001. for_each_power_domain(power_domain, power_domain_mask)
  9002. intel_display_power_put(dev_priv, power_domain);
  9003. return active;
  9004. }
  9005. static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
  9006. const struct intel_plane_state *plane_state)
  9007. {
  9008. struct drm_device *dev = crtc->dev;
  9009. struct drm_i915_private *dev_priv = to_i915(dev);
  9010. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9011. uint32_t cntl = 0, size = 0;
  9012. if (plane_state && plane_state->base.visible) {
  9013. unsigned int width = plane_state->base.crtc_w;
  9014. unsigned int height = plane_state->base.crtc_h;
  9015. unsigned int stride = roundup_pow_of_two(width) * 4;
  9016. switch (stride) {
  9017. default:
  9018. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  9019. width, stride);
  9020. stride = 256;
  9021. /* fallthrough */
  9022. case 256:
  9023. case 512:
  9024. case 1024:
  9025. case 2048:
  9026. break;
  9027. }
  9028. cntl |= CURSOR_ENABLE |
  9029. CURSOR_GAMMA_ENABLE |
  9030. CURSOR_FORMAT_ARGB |
  9031. CURSOR_STRIDE(stride);
  9032. size = (height << 12) | width;
  9033. }
  9034. if (intel_crtc->cursor_cntl != 0 &&
  9035. (intel_crtc->cursor_base != base ||
  9036. intel_crtc->cursor_size != size ||
  9037. intel_crtc->cursor_cntl != cntl)) {
  9038. /* On these chipsets we can only modify the base/size/stride
  9039. * whilst the cursor is disabled.
  9040. */
  9041. I915_WRITE(CURCNTR(PIPE_A), 0);
  9042. POSTING_READ(CURCNTR(PIPE_A));
  9043. intel_crtc->cursor_cntl = 0;
  9044. }
  9045. if (intel_crtc->cursor_base != base) {
  9046. I915_WRITE(CURBASE(PIPE_A), base);
  9047. intel_crtc->cursor_base = base;
  9048. }
  9049. if (intel_crtc->cursor_size != size) {
  9050. I915_WRITE(CURSIZE, size);
  9051. intel_crtc->cursor_size = size;
  9052. }
  9053. if (intel_crtc->cursor_cntl != cntl) {
  9054. I915_WRITE(CURCNTR(PIPE_A), cntl);
  9055. POSTING_READ(CURCNTR(PIPE_A));
  9056. intel_crtc->cursor_cntl = cntl;
  9057. }
  9058. }
  9059. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
  9060. const struct intel_plane_state *plane_state)
  9061. {
  9062. struct drm_device *dev = crtc->dev;
  9063. struct drm_i915_private *dev_priv = to_i915(dev);
  9064. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9065. int pipe = intel_crtc->pipe;
  9066. uint32_t cntl = 0;
  9067. if (plane_state && plane_state->base.visible) {
  9068. cntl = MCURSOR_GAMMA_ENABLE;
  9069. switch (plane_state->base.crtc_w) {
  9070. case 64:
  9071. cntl |= CURSOR_MODE_64_ARGB_AX;
  9072. break;
  9073. case 128:
  9074. cntl |= CURSOR_MODE_128_ARGB_AX;
  9075. break;
  9076. case 256:
  9077. cntl |= CURSOR_MODE_256_ARGB_AX;
  9078. break;
  9079. default:
  9080. MISSING_CASE(plane_state->base.crtc_w);
  9081. return;
  9082. }
  9083. cntl |= pipe << 28; /* Connect to correct pipe */
  9084. if (HAS_DDI(dev_priv))
  9085. cntl |= CURSOR_PIPE_CSC_ENABLE;
  9086. if (plane_state->base.rotation & DRM_ROTATE_180)
  9087. cntl |= CURSOR_ROTATE_180;
  9088. }
  9089. if (intel_crtc->cursor_cntl != cntl) {
  9090. I915_WRITE(CURCNTR(pipe), cntl);
  9091. POSTING_READ(CURCNTR(pipe));
  9092. intel_crtc->cursor_cntl = cntl;
  9093. }
  9094. /* and commit changes on next vblank */
  9095. I915_WRITE(CURBASE(pipe), base);
  9096. POSTING_READ(CURBASE(pipe));
  9097. intel_crtc->cursor_base = base;
  9098. }
  9099. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  9100. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  9101. const struct intel_plane_state *plane_state)
  9102. {
  9103. struct drm_device *dev = crtc->dev;
  9104. struct drm_i915_private *dev_priv = to_i915(dev);
  9105. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9106. int pipe = intel_crtc->pipe;
  9107. u32 base = intel_crtc->cursor_addr;
  9108. u32 pos = 0;
  9109. if (plane_state) {
  9110. int x = plane_state->base.crtc_x;
  9111. int y = plane_state->base.crtc_y;
  9112. if (x < 0) {
  9113. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  9114. x = -x;
  9115. }
  9116. pos |= x << CURSOR_X_SHIFT;
  9117. if (y < 0) {
  9118. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  9119. y = -y;
  9120. }
  9121. pos |= y << CURSOR_Y_SHIFT;
  9122. /* ILK+ do this automagically */
  9123. if (HAS_GMCH_DISPLAY(dev_priv) &&
  9124. plane_state->base.rotation & DRM_ROTATE_180) {
  9125. base += (plane_state->base.crtc_h *
  9126. plane_state->base.crtc_w - 1) * 4;
  9127. }
  9128. }
  9129. I915_WRITE(CURPOS(pipe), pos);
  9130. if (IS_845G(dev_priv) || IS_I865G(dev_priv))
  9131. i845_update_cursor(crtc, base, plane_state);
  9132. else
  9133. i9xx_update_cursor(crtc, base, plane_state);
  9134. }
  9135. static bool cursor_size_ok(struct drm_i915_private *dev_priv,
  9136. uint32_t width, uint32_t height)
  9137. {
  9138. if (width == 0 || height == 0)
  9139. return false;
  9140. /*
  9141. * 845g/865g are special in that they are only limited by
  9142. * the width of their cursors, the height is arbitrary up to
  9143. * the precision of the register. Everything else requires
  9144. * square cursors, limited to a few power-of-two sizes.
  9145. */
  9146. if (IS_845G(dev_priv) || IS_I865G(dev_priv)) {
  9147. if ((width & 63) != 0)
  9148. return false;
  9149. if (width > (IS_845G(dev_priv) ? 64 : 512))
  9150. return false;
  9151. if (height > 1023)
  9152. return false;
  9153. } else {
  9154. switch (width | height) {
  9155. case 256:
  9156. case 128:
  9157. if (IS_GEN2(dev_priv))
  9158. return false;
  9159. case 64:
  9160. break;
  9161. default:
  9162. return false;
  9163. }
  9164. }
  9165. return true;
  9166. }
  9167. /* VESA 640x480x72Hz mode to set on the pipe */
  9168. static struct drm_display_mode load_detect_mode = {
  9169. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  9170. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  9171. };
  9172. struct drm_framebuffer *
  9173. __intel_framebuffer_create(struct drm_device *dev,
  9174. struct drm_mode_fb_cmd2 *mode_cmd,
  9175. struct drm_i915_gem_object *obj)
  9176. {
  9177. struct intel_framebuffer *intel_fb;
  9178. int ret;
  9179. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  9180. if (!intel_fb)
  9181. return ERR_PTR(-ENOMEM);
  9182. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  9183. if (ret)
  9184. goto err;
  9185. return &intel_fb->base;
  9186. err:
  9187. kfree(intel_fb);
  9188. return ERR_PTR(ret);
  9189. }
  9190. static struct drm_framebuffer *
  9191. intel_framebuffer_create(struct drm_device *dev,
  9192. struct drm_mode_fb_cmd2 *mode_cmd,
  9193. struct drm_i915_gem_object *obj)
  9194. {
  9195. struct drm_framebuffer *fb;
  9196. int ret;
  9197. ret = i915_mutex_lock_interruptible(dev);
  9198. if (ret)
  9199. return ERR_PTR(ret);
  9200. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  9201. mutex_unlock(&dev->struct_mutex);
  9202. return fb;
  9203. }
  9204. static u32
  9205. intel_framebuffer_pitch_for_width(int width, int bpp)
  9206. {
  9207. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  9208. return ALIGN(pitch, 64);
  9209. }
  9210. static u32
  9211. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  9212. {
  9213. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  9214. return PAGE_ALIGN(pitch * mode->vdisplay);
  9215. }
  9216. static struct drm_framebuffer *
  9217. intel_framebuffer_create_for_mode(struct drm_device *dev,
  9218. struct drm_display_mode *mode,
  9219. int depth, int bpp)
  9220. {
  9221. struct drm_framebuffer *fb;
  9222. struct drm_i915_gem_object *obj;
  9223. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  9224. obj = i915_gem_object_create(dev,
  9225. intel_framebuffer_size_for_mode(mode, bpp));
  9226. if (IS_ERR(obj))
  9227. return ERR_CAST(obj);
  9228. mode_cmd.width = mode->hdisplay;
  9229. mode_cmd.height = mode->vdisplay;
  9230. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  9231. bpp);
  9232. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  9233. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  9234. if (IS_ERR(fb))
  9235. i915_gem_object_put(obj);
  9236. return fb;
  9237. }
  9238. static struct drm_framebuffer *
  9239. mode_fits_in_fbdev(struct drm_device *dev,
  9240. struct drm_display_mode *mode)
  9241. {
  9242. #ifdef CONFIG_DRM_FBDEV_EMULATION
  9243. struct drm_i915_private *dev_priv = to_i915(dev);
  9244. struct drm_i915_gem_object *obj;
  9245. struct drm_framebuffer *fb;
  9246. if (!dev_priv->fbdev)
  9247. return NULL;
  9248. if (!dev_priv->fbdev->fb)
  9249. return NULL;
  9250. obj = dev_priv->fbdev->fb->obj;
  9251. BUG_ON(!obj);
  9252. fb = &dev_priv->fbdev->fb->base;
  9253. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  9254. fb->bits_per_pixel))
  9255. return NULL;
  9256. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  9257. return NULL;
  9258. drm_framebuffer_reference(fb);
  9259. return fb;
  9260. #else
  9261. return NULL;
  9262. #endif
  9263. }
  9264. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  9265. struct drm_crtc *crtc,
  9266. struct drm_display_mode *mode,
  9267. struct drm_framebuffer *fb,
  9268. int x, int y)
  9269. {
  9270. struct drm_plane_state *plane_state;
  9271. int hdisplay, vdisplay;
  9272. int ret;
  9273. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  9274. if (IS_ERR(plane_state))
  9275. return PTR_ERR(plane_state);
  9276. if (mode)
  9277. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  9278. else
  9279. hdisplay = vdisplay = 0;
  9280. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  9281. if (ret)
  9282. return ret;
  9283. drm_atomic_set_fb_for_plane(plane_state, fb);
  9284. plane_state->crtc_x = 0;
  9285. plane_state->crtc_y = 0;
  9286. plane_state->crtc_w = hdisplay;
  9287. plane_state->crtc_h = vdisplay;
  9288. plane_state->src_x = x << 16;
  9289. plane_state->src_y = y << 16;
  9290. plane_state->src_w = hdisplay << 16;
  9291. plane_state->src_h = vdisplay << 16;
  9292. return 0;
  9293. }
  9294. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  9295. struct drm_display_mode *mode,
  9296. struct intel_load_detect_pipe *old,
  9297. struct drm_modeset_acquire_ctx *ctx)
  9298. {
  9299. struct intel_crtc *intel_crtc;
  9300. struct intel_encoder *intel_encoder =
  9301. intel_attached_encoder(connector);
  9302. struct drm_crtc *possible_crtc;
  9303. struct drm_encoder *encoder = &intel_encoder->base;
  9304. struct drm_crtc *crtc = NULL;
  9305. struct drm_device *dev = encoder->dev;
  9306. struct drm_i915_private *dev_priv = to_i915(dev);
  9307. struct drm_framebuffer *fb;
  9308. struct drm_mode_config *config = &dev->mode_config;
  9309. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  9310. struct drm_connector_state *connector_state;
  9311. struct intel_crtc_state *crtc_state;
  9312. int ret, i = -1;
  9313. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  9314. connector->base.id, connector->name,
  9315. encoder->base.id, encoder->name);
  9316. old->restore_state = NULL;
  9317. retry:
  9318. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  9319. if (ret)
  9320. goto fail;
  9321. /*
  9322. * Algorithm gets a little messy:
  9323. *
  9324. * - if the connector already has an assigned crtc, use it (but make
  9325. * sure it's on first)
  9326. *
  9327. * - try to find the first unused crtc that can drive this connector,
  9328. * and use that if we find one
  9329. */
  9330. /* See if we already have a CRTC for this connector */
  9331. if (connector->state->crtc) {
  9332. crtc = connector->state->crtc;
  9333. ret = drm_modeset_lock(&crtc->mutex, ctx);
  9334. if (ret)
  9335. goto fail;
  9336. /* Make sure the crtc and connector are running */
  9337. goto found;
  9338. }
  9339. /* Find an unused one (if possible) */
  9340. for_each_crtc(dev, possible_crtc) {
  9341. i++;
  9342. if (!(encoder->possible_crtcs & (1 << i)))
  9343. continue;
  9344. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  9345. if (ret)
  9346. goto fail;
  9347. if (possible_crtc->state->enable) {
  9348. drm_modeset_unlock(&possible_crtc->mutex);
  9349. continue;
  9350. }
  9351. crtc = possible_crtc;
  9352. break;
  9353. }
  9354. /*
  9355. * If we didn't find an unused CRTC, don't use any.
  9356. */
  9357. if (!crtc) {
  9358. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  9359. goto fail;
  9360. }
  9361. found:
  9362. intel_crtc = to_intel_crtc(crtc);
  9363. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  9364. if (ret)
  9365. goto fail;
  9366. state = drm_atomic_state_alloc(dev);
  9367. restore_state = drm_atomic_state_alloc(dev);
  9368. if (!state || !restore_state) {
  9369. ret = -ENOMEM;
  9370. goto fail;
  9371. }
  9372. state->acquire_ctx = ctx;
  9373. restore_state->acquire_ctx = ctx;
  9374. connector_state = drm_atomic_get_connector_state(state, connector);
  9375. if (IS_ERR(connector_state)) {
  9376. ret = PTR_ERR(connector_state);
  9377. goto fail;
  9378. }
  9379. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  9380. if (ret)
  9381. goto fail;
  9382. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  9383. if (IS_ERR(crtc_state)) {
  9384. ret = PTR_ERR(crtc_state);
  9385. goto fail;
  9386. }
  9387. crtc_state->base.active = crtc_state->base.enable = true;
  9388. if (!mode)
  9389. mode = &load_detect_mode;
  9390. /* We need a framebuffer large enough to accommodate all accesses
  9391. * that the plane may generate whilst we perform load detection.
  9392. * We can not rely on the fbcon either being present (we get called
  9393. * during its initialisation to detect all boot displays, or it may
  9394. * not even exist) or that it is large enough to satisfy the
  9395. * requested mode.
  9396. */
  9397. fb = mode_fits_in_fbdev(dev, mode);
  9398. if (fb == NULL) {
  9399. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  9400. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  9401. } else
  9402. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  9403. if (IS_ERR(fb)) {
  9404. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  9405. goto fail;
  9406. }
  9407. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  9408. if (ret)
  9409. goto fail;
  9410. drm_framebuffer_unreference(fb);
  9411. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  9412. if (ret)
  9413. goto fail;
  9414. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  9415. if (!ret)
  9416. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  9417. if (!ret)
  9418. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
  9419. if (ret) {
  9420. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  9421. goto fail;
  9422. }
  9423. ret = drm_atomic_commit(state);
  9424. if (ret) {
  9425. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  9426. goto fail;
  9427. }
  9428. old->restore_state = restore_state;
  9429. drm_atomic_state_put(state);
  9430. /* let the connector get through one full cycle before testing */
  9431. intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
  9432. return true;
  9433. fail:
  9434. if (state) {
  9435. drm_atomic_state_put(state);
  9436. state = NULL;
  9437. }
  9438. if (restore_state) {
  9439. drm_atomic_state_put(restore_state);
  9440. restore_state = NULL;
  9441. }
  9442. if (ret == -EDEADLK) {
  9443. drm_modeset_backoff(ctx);
  9444. goto retry;
  9445. }
  9446. return false;
  9447. }
  9448. void intel_release_load_detect_pipe(struct drm_connector *connector,
  9449. struct intel_load_detect_pipe *old,
  9450. struct drm_modeset_acquire_ctx *ctx)
  9451. {
  9452. struct intel_encoder *intel_encoder =
  9453. intel_attached_encoder(connector);
  9454. struct drm_encoder *encoder = &intel_encoder->base;
  9455. struct drm_atomic_state *state = old->restore_state;
  9456. int ret;
  9457. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  9458. connector->base.id, connector->name,
  9459. encoder->base.id, encoder->name);
  9460. if (!state)
  9461. return;
  9462. ret = drm_atomic_commit(state);
  9463. if (ret)
  9464. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  9465. drm_atomic_state_put(state);
  9466. }
  9467. static int i9xx_pll_refclk(struct drm_device *dev,
  9468. const struct intel_crtc_state *pipe_config)
  9469. {
  9470. struct drm_i915_private *dev_priv = to_i915(dev);
  9471. u32 dpll = pipe_config->dpll_hw_state.dpll;
  9472. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  9473. return dev_priv->vbt.lvds_ssc_freq;
  9474. else if (HAS_PCH_SPLIT(dev_priv))
  9475. return 120000;
  9476. else if (!IS_GEN2(dev_priv))
  9477. return 96000;
  9478. else
  9479. return 48000;
  9480. }
  9481. /* Returns the clock of the currently programmed mode of the given pipe. */
  9482. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  9483. struct intel_crtc_state *pipe_config)
  9484. {
  9485. struct drm_device *dev = crtc->base.dev;
  9486. struct drm_i915_private *dev_priv = to_i915(dev);
  9487. int pipe = pipe_config->cpu_transcoder;
  9488. u32 dpll = pipe_config->dpll_hw_state.dpll;
  9489. u32 fp;
  9490. struct dpll clock;
  9491. int port_clock;
  9492. int refclk = i9xx_pll_refclk(dev, pipe_config);
  9493. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  9494. fp = pipe_config->dpll_hw_state.fp0;
  9495. else
  9496. fp = pipe_config->dpll_hw_state.fp1;
  9497. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  9498. if (IS_PINEVIEW(dev_priv)) {
  9499. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  9500. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  9501. } else {
  9502. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  9503. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  9504. }
  9505. if (!IS_GEN2(dev_priv)) {
  9506. if (IS_PINEVIEW(dev_priv))
  9507. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  9508. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  9509. else
  9510. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  9511. DPLL_FPA01_P1_POST_DIV_SHIFT);
  9512. switch (dpll & DPLL_MODE_MASK) {
  9513. case DPLLB_MODE_DAC_SERIAL:
  9514. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  9515. 5 : 10;
  9516. break;
  9517. case DPLLB_MODE_LVDS:
  9518. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  9519. 7 : 14;
  9520. break;
  9521. default:
  9522. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  9523. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  9524. return;
  9525. }
  9526. if (IS_PINEVIEW(dev_priv))
  9527. port_clock = pnv_calc_dpll_params(refclk, &clock);
  9528. else
  9529. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  9530. } else {
  9531. u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
  9532. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  9533. if (is_lvds) {
  9534. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  9535. DPLL_FPA01_P1_POST_DIV_SHIFT);
  9536. if (lvds & LVDS_CLKB_POWER_UP)
  9537. clock.p2 = 7;
  9538. else
  9539. clock.p2 = 14;
  9540. } else {
  9541. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  9542. clock.p1 = 2;
  9543. else {
  9544. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  9545. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  9546. }
  9547. if (dpll & PLL_P2_DIVIDE_BY_4)
  9548. clock.p2 = 4;
  9549. else
  9550. clock.p2 = 2;
  9551. }
  9552. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  9553. }
  9554. /*
  9555. * This value includes pixel_multiplier. We will use
  9556. * port_clock to compute adjusted_mode.crtc_clock in the
  9557. * encoder's get_config() function.
  9558. */
  9559. pipe_config->port_clock = port_clock;
  9560. }
  9561. int intel_dotclock_calculate(int link_freq,
  9562. const struct intel_link_m_n *m_n)
  9563. {
  9564. /*
  9565. * The calculation for the data clock is:
  9566. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  9567. * But we want to avoid losing precison if possible, so:
  9568. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  9569. *
  9570. * and the link clock is simpler:
  9571. * link_clock = (m * link_clock) / n
  9572. */
  9573. if (!m_n->link_n)
  9574. return 0;
  9575. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  9576. }
  9577. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  9578. struct intel_crtc_state *pipe_config)
  9579. {
  9580. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9581. /* read out port_clock from the DPLL */
  9582. i9xx_crtc_clock_get(crtc, pipe_config);
  9583. /*
  9584. * In case there is an active pipe without active ports,
  9585. * we may need some idea for the dotclock anyway.
  9586. * Calculate one based on the FDI configuration.
  9587. */
  9588. pipe_config->base.adjusted_mode.crtc_clock =
  9589. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9590. &pipe_config->fdi_m_n);
  9591. }
  9592. /** Returns the currently programmed mode of the given pipe. */
  9593. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  9594. struct drm_crtc *crtc)
  9595. {
  9596. struct drm_i915_private *dev_priv = to_i915(dev);
  9597. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9598. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  9599. struct drm_display_mode *mode;
  9600. struct intel_crtc_state *pipe_config;
  9601. int htot = I915_READ(HTOTAL(cpu_transcoder));
  9602. int hsync = I915_READ(HSYNC(cpu_transcoder));
  9603. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  9604. int vsync = I915_READ(VSYNC(cpu_transcoder));
  9605. enum pipe pipe = intel_crtc->pipe;
  9606. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  9607. if (!mode)
  9608. return NULL;
  9609. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  9610. if (!pipe_config) {
  9611. kfree(mode);
  9612. return NULL;
  9613. }
  9614. /*
  9615. * Construct a pipe_config sufficient for getting the clock info
  9616. * back out of crtc_clock_get.
  9617. *
  9618. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  9619. * to use a real value here instead.
  9620. */
  9621. pipe_config->cpu_transcoder = (enum transcoder) pipe;
  9622. pipe_config->pixel_multiplier = 1;
  9623. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  9624. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  9625. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  9626. i9xx_crtc_clock_get(intel_crtc, pipe_config);
  9627. mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
  9628. mode->hdisplay = (htot & 0xffff) + 1;
  9629. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  9630. mode->hsync_start = (hsync & 0xffff) + 1;
  9631. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  9632. mode->vdisplay = (vtot & 0xffff) + 1;
  9633. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  9634. mode->vsync_start = (vsync & 0xffff) + 1;
  9635. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  9636. drm_mode_set_name(mode);
  9637. kfree(pipe_config);
  9638. return mode;
  9639. }
  9640. static void intel_crtc_destroy(struct drm_crtc *crtc)
  9641. {
  9642. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9643. struct drm_device *dev = crtc->dev;
  9644. struct intel_flip_work *work;
  9645. spin_lock_irq(&dev->event_lock);
  9646. work = intel_crtc->flip_work;
  9647. intel_crtc->flip_work = NULL;
  9648. spin_unlock_irq(&dev->event_lock);
  9649. if (work) {
  9650. cancel_work_sync(&work->mmio_work);
  9651. cancel_work_sync(&work->unpin_work);
  9652. kfree(work);
  9653. }
  9654. drm_crtc_cleanup(crtc);
  9655. kfree(intel_crtc);
  9656. }
  9657. static void intel_unpin_work_fn(struct work_struct *__work)
  9658. {
  9659. struct intel_flip_work *work =
  9660. container_of(__work, struct intel_flip_work, unpin_work);
  9661. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  9662. struct drm_device *dev = crtc->base.dev;
  9663. struct drm_plane *primary = crtc->base.primary;
  9664. if (is_mmio_work(work))
  9665. flush_work(&work->mmio_work);
  9666. mutex_lock(&dev->struct_mutex);
  9667. intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
  9668. i915_gem_object_put(work->pending_flip_obj);
  9669. mutex_unlock(&dev->struct_mutex);
  9670. i915_gem_request_put(work->flip_queued_req);
  9671. intel_frontbuffer_flip_complete(to_i915(dev),
  9672. to_intel_plane(primary)->frontbuffer_bit);
  9673. intel_fbc_post_update(crtc);
  9674. drm_framebuffer_unreference(work->old_fb);
  9675. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  9676. atomic_dec(&crtc->unpin_work_count);
  9677. kfree(work);
  9678. }
  9679. /* Is 'a' after or equal to 'b'? */
  9680. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9681. {
  9682. return !((a - b) & 0x80000000);
  9683. }
  9684. static bool __pageflip_finished_cs(struct intel_crtc *crtc,
  9685. struct intel_flip_work *work)
  9686. {
  9687. struct drm_device *dev = crtc->base.dev;
  9688. struct drm_i915_private *dev_priv = to_i915(dev);
  9689. if (abort_flip_on_reset(crtc))
  9690. return true;
  9691. /*
  9692. * The relevant registers doen't exist on pre-ctg.
  9693. * As the flip done interrupt doesn't trigger for mmio
  9694. * flips on gmch platforms, a flip count check isn't
  9695. * really needed there. But since ctg has the registers,
  9696. * include it in the check anyway.
  9697. */
  9698. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  9699. return true;
  9700. /*
  9701. * BDW signals flip done immediately if the plane
  9702. * is disabled, even if the plane enable is already
  9703. * armed to occur at the next vblank :(
  9704. */
  9705. /*
  9706. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9707. * used the same base address. In that case the mmio flip might
  9708. * have completed, but the CS hasn't even executed the flip yet.
  9709. *
  9710. * A flip count check isn't enough as the CS might have updated
  9711. * the base address just after start of vblank, but before we
  9712. * managed to process the interrupt. This means we'd complete the
  9713. * CS flip too soon.
  9714. *
  9715. * Combining both checks should get us a good enough result. It may
  9716. * still happen that the CS flip has been executed, but has not
  9717. * yet actually completed. But in case the base address is the same
  9718. * anyway, we don't really care.
  9719. */
  9720. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9721. crtc->flip_work->gtt_offset &&
  9722. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
  9723. crtc->flip_work->flip_count);
  9724. }
  9725. static bool
  9726. __pageflip_finished_mmio(struct intel_crtc *crtc,
  9727. struct intel_flip_work *work)
  9728. {
  9729. /*
  9730. * MMIO work completes when vblank is different from
  9731. * flip_queued_vblank.
  9732. *
  9733. * Reset counter value doesn't matter, this is handled by
  9734. * i915_wait_request finishing early, so no need to handle
  9735. * reset here.
  9736. */
  9737. return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
  9738. }
  9739. static bool pageflip_finished(struct intel_crtc *crtc,
  9740. struct intel_flip_work *work)
  9741. {
  9742. if (!atomic_read(&work->pending))
  9743. return false;
  9744. smp_rmb();
  9745. if (is_mmio_work(work))
  9746. return __pageflip_finished_mmio(crtc, work);
  9747. else
  9748. return __pageflip_finished_cs(crtc, work);
  9749. }
  9750. void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
  9751. {
  9752. struct drm_device *dev = &dev_priv->drm;
  9753. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  9754. struct intel_flip_work *work;
  9755. unsigned long flags;
  9756. /* Ignore early vblank irqs */
  9757. if (!crtc)
  9758. return;
  9759. /*
  9760. * This is called both by irq handlers and the reset code (to complete
  9761. * lost pageflips) so needs the full irqsave spinlocks.
  9762. */
  9763. spin_lock_irqsave(&dev->event_lock, flags);
  9764. work = crtc->flip_work;
  9765. if (work != NULL &&
  9766. !is_mmio_work(work) &&
  9767. pageflip_finished(crtc, work))
  9768. page_flip_completed(crtc);
  9769. spin_unlock_irqrestore(&dev->event_lock, flags);
  9770. }
  9771. void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
  9772. {
  9773. struct drm_device *dev = &dev_priv->drm;
  9774. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  9775. struct intel_flip_work *work;
  9776. unsigned long flags;
  9777. /* Ignore early vblank irqs */
  9778. if (!crtc)
  9779. return;
  9780. /*
  9781. * This is called both by irq handlers and the reset code (to complete
  9782. * lost pageflips) so needs the full irqsave spinlocks.
  9783. */
  9784. spin_lock_irqsave(&dev->event_lock, flags);
  9785. work = crtc->flip_work;
  9786. if (work != NULL &&
  9787. is_mmio_work(work) &&
  9788. pageflip_finished(crtc, work))
  9789. page_flip_completed(crtc);
  9790. spin_unlock_irqrestore(&dev->event_lock, flags);
  9791. }
  9792. static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
  9793. struct intel_flip_work *work)
  9794. {
  9795. work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
  9796. /* Ensure that the work item is consistent when activating it ... */
  9797. smp_mb__before_atomic();
  9798. atomic_set(&work->pending, 1);
  9799. }
  9800. static int intel_gen2_queue_flip(struct drm_device *dev,
  9801. struct drm_crtc *crtc,
  9802. struct drm_framebuffer *fb,
  9803. struct drm_i915_gem_object *obj,
  9804. struct drm_i915_gem_request *req,
  9805. uint32_t flags)
  9806. {
  9807. struct intel_ring *ring = req->ring;
  9808. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9809. u32 flip_mask;
  9810. int ret;
  9811. ret = intel_ring_begin(req, 6);
  9812. if (ret)
  9813. return ret;
  9814. /* Can't queue multiple flips, so wait for the previous
  9815. * one to finish before executing the next.
  9816. */
  9817. if (intel_crtc->plane)
  9818. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9819. else
  9820. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9821. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9822. intel_ring_emit(ring, MI_NOOP);
  9823. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9824. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9825. intel_ring_emit(ring, fb->pitches[0]);
  9826. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  9827. intel_ring_emit(ring, 0); /* aux display base address, unused */
  9828. return 0;
  9829. }
  9830. static int intel_gen3_queue_flip(struct drm_device *dev,
  9831. struct drm_crtc *crtc,
  9832. struct drm_framebuffer *fb,
  9833. struct drm_i915_gem_object *obj,
  9834. struct drm_i915_gem_request *req,
  9835. uint32_t flags)
  9836. {
  9837. struct intel_ring *ring = req->ring;
  9838. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9839. u32 flip_mask;
  9840. int ret;
  9841. ret = intel_ring_begin(req, 6);
  9842. if (ret)
  9843. return ret;
  9844. if (intel_crtc->plane)
  9845. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9846. else
  9847. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9848. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9849. intel_ring_emit(ring, MI_NOOP);
  9850. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  9851. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9852. intel_ring_emit(ring, fb->pitches[0]);
  9853. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  9854. intel_ring_emit(ring, MI_NOOP);
  9855. return 0;
  9856. }
  9857. static int intel_gen4_queue_flip(struct drm_device *dev,
  9858. struct drm_crtc *crtc,
  9859. struct drm_framebuffer *fb,
  9860. struct drm_i915_gem_object *obj,
  9861. struct drm_i915_gem_request *req,
  9862. uint32_t flags)
  9863. {
  9864. struct intel_ring *ring = req->ring;
  9865. struct drm_i915_private *dev_priv = to_i915(dev);
  9866. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9867. uint32_t pf, pipesrc;
  9868. int ret;
  9869. ret = intel_ring_begin(req, 4);
  9870. if (ret)
  9871. return ret;
  9872. /* i965+ uses the linear or tiled offsets from the
  9873. * Display Registers (which do not change across a page-flip)
  9874. * so we need only reprogram the base address.
  9875. */
  9876. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9877. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9878. intel_ring_emit(ring, fb->pitches[0]);
  9879. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset |
  9880. intel_fb_modifier_to_tiling(fb->modifier));
  9881. /* XXX Enabling the panel-fitter across page-flip is so far
  9882. * untested on non-native modes, so ignore it for now.
  9883. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9884. */
  9885. pf = 0;
  9886. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9887. intel_ring_emit(ring, pf | pipesrc);
  9888. return 0;
  9889. }
  9890. static int intel_gen6_queue_flip(struct drm_device *dev,
  9891. struct drm_crtc *crtc,
  9892. struct drm_framebuffer *fb,
  9893. struct drm_i915_gem_object *obj,
  9894. struct drm_i915_gem_request *req,
  9895. uint32_t flags)
  9896. {
  9897. struct intel_ring *ring = req->ring;
  9898. struct drm_i915_private *dev_priv = to_i915(dev);
  9899. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9900. uint32_t pf, pipesrc;
  9901. int ret;
  9902. ret = intel_ring_begin(req, 4);
  9903. if (ret)
  9904. return ret;
  9905. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9906. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9907. intel_ring_emit(ring, fb->pitches[0] |
  9908. intel_fb_modifier_to_tiling(fb->modifier));
  9909. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  9910. /* Contrary to the suggestions in the documentation,
  9911. * "Enable Panel Fitter" does not seem to be required when page
  9912. * flipping with a non-native mode, and worse causes a normal
  9913. * modeset to fail.
  9914. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9915. */
  9916. pf = 0;
  9917. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9918. intel_ring_emit(ring, pf | pipesrc);
  9919. return 0;
  9920. }
  9921. static int intel_gen7_queue_flip(struct drm_device *dev,
  9922. struct drm_crtc *crtc,
  9923. struct drm_framebuffer *fb,
  9924. struct drm_i915_gem_object *obj,
  9925. struct drm_i915_gem_request *req,
  9926. uint32_t flags)
  9927. {
  9928. struct drm_i915_private *dev_priv = to_i915(dev);
  9929. struct intel_ring *ring = req->ring;
  9930. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9931. uint32_t plane_bit = 0;
  9932. int len, ret;
  9933. switch (intel_crtc->plane) {
  9934. case PLANE_A:
  9935. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9936. break;
  9937. case PLANE_B:
  9938. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9939. break;
  9940. case PLANE_C:
  9941. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9942. break;
  9943. default:
  9944. WARN_ONCE(1, "unknown plane in flip command\n");
  9945. return -ENODEV;
  9946. }
  9947. len = 4;
  9948. if (req->engine->id == RCS) {
  9949. len += 6;
  9950. /*
  9951. * On Gen 8, SRM is now taking an extra dword to accommodate
  9952. * 48bits addresses, and we need a NOOP for the batch size to
  9953. * stay even.
  9954. */
  9955. if (IS_GEN8(dev_priv))
  9956. len += 2;
  9957. }
  9958. /*
  9959. * BSpec MI_DISPLAY_FLIP for IVB:
  9960. * "The full packet must be contained within the same cache line."
  9961. *
  9962. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9963. * cacheline, if we ever start emitting more commands before
  9964. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9965. * then do the cacheline alignment, and finally emit the
  9966. * MI_DISPLAY_FLIP.
  9967. */
  9968. ret = intel_ring_cacheline_align(req);
  9969. if (ret)
  9970. return ret;
  9971. ret = intel_ring_begin(req, len);
  9972. if (ret)
  9973. return ret;
  9974. /* Unmask the flip-done completion message. Note that the bspec says that
  9975. * we should do this for both the BCS and RCS, and that we must not unmask
  9976. * more than one flip event at any time (or ensure that one flip message
  9977. * can be sent by waiting for flip-done prior to queueing new flips).
  9978. * Experimentation says that BCS works despite DERRMR masking all
  9979. * flip-done completion events and that unmasking all planes at once
  9980. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9981. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9982. */
  9983. if (req->engine->id == RCS) {
  9984. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  9985. intel_ring_emit_reg(ring, DERRMR);
  9986. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  9987. DERRMR_PIPEB_PRI_FLIP_DONE |
  9988. DERRMR_PIPEC_PRI_FLIP_DONE));
  9989. if (IS_GEN8(dev_priv))
  9990. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
  9991. MI_SRM_LRM_GLOBAL_GTT);
  9992. else
  9993. intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
  9994. MI_SRM_LRM_GLOBAL_GTT);
  9995. intel_ring_emit_reg(ring, DERRMR);
  9996. intel_ring_emit(ring,
  9997. i915_ggtt_offset(req->engine->scratch) + 256);
  9998. if (IS_GEN8(dev_priv)) {
  9999. intel_ring_emit(ring, 0);
  10000. intel_ring_emit(ring, MI_NOOP);
  10001. }
  10002. }
  10003. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  10004. intel_ring_emit(ring, fb->pitches[0] |
  10005. intel_fb_modifier_to_tiling(fb->modifier));
  10006. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  10007. intel_ring_emit(ring, (MI_NOOP));
  10008. return 0;
  10009. }
  10010. static bool use_mmio_flip(struct intel_engine_cs *engine,
  10011. struct drm_i915_gem_object *obj)
  10012. {
  10013. /*
  10014. * This is not being used for older platforms, because
  10015. * non-availability of flip done interrupt forces us to use
  10016. * CS flips. Older platforms derive flip done using some clever
  10017. * tricks involving the flip_pending status bits and vblank irqs.
  10018. * So using MMIO flips there would disrupt this mechanism.
  10019. */
  10020. if (engine == NULL)
  10021. return true;
  10022. if (INTEL_GEN(engine->i915) < 5)
  10023. return false;
  10024. if (i915.use_mmio_flip < 0)
  10025. return false;
  10026. else if (i915.use_mmio_flip > 0)
  10027. return true;
  10028. else if (i915.enable_execlists)
  10029. return true;
  10030. return engine != i915_gem_object_last_write_engine(obj);
  10031. }
  10032. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
  10033. unsigned int rotation,
  10034. struct intel_flip_work *work)
  10035. {
  10036. struct drm_device *dev = intel_crtc->base.dev;
  10037. struct drm_i915_private *dev_priv = to_i915(dev);
  10038. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  10039. const enum pipe pipe = intel_crtc->pipe;
  10040. u32 ctl, stride = skl_plane_stride(fb, 0, rotation);
  10041. ctl = I915_READ(PLANE_CTL(pipe, 0));
  10042. ctl &= ~PLANE_CTL_TILED_MASK;
  10043. switch (fb->modifier) {
  10044. case DRM_FORMAT_MOD_NONE:
  10045. break;
  10046. case I915_FORMAT_MOD_X_TILED:
  10047. ctl |= PLANE_CTL_TILED_X;
  10048. break;
  10049. case I915_FORMAT_MOD_Y_TILED:
  10050. ctl |= PLANE_CTL_TILED_Y;
  10051. break;
  10052. case I915_FORMAT_MOD_Yf_TILED:
  10053. ctl |= PLANE_CTL_TILED_YF;
  10054. break;
  10055. default:
  10056. MISSING_CASE(fb->modifier);
  10057. }
  10058. /*
  10059. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  10060. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  10061. */
  10062. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  10063. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  10064. I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
  10065. POSTING_READ(PLANE_SURF(pipe, 0));
  10066. }
  10067. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
  10068. struct intel_flip_work *work)
  10069. {
  10070. struct drm_device *dev = intel_crtc->base.dev;
  10071. struct drm_i915_private *dev_priv = to_i915(dev);
  10072. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  10073. i915_reg_t reg = DSPCNTR(intel_crtc->plane);
  10074. u32 dspcntr;
  10075. dspcntr = I915_READ(reg);
  10076. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  10077. dspcntr |= DISPPLANE_TILED;
  10078. else
  10079. dspcntr &= ~DISPPLANE_TILED;
  10080. I915_WRITE(reg, dspcntr);
  10081. I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
  10082. POSTING_READ(DSPSURF(intel_crtc->plane));
  10083. }
  10084. static void intel_mmio_flip_work_func(struct work_struct *w)
  10085. {
  10086. struct intel_flip_work *work =
  10087. container_of(w, struct intel_flip_work, mmio_work);
  10088. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  10089. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  10090. struct intel_framebuffer *intel_fb =
  10091. to_intel_framebuffer(crtc->base.primary->fb);
  10092. struct drm_i915_gem_object *obj = intel_fb->obj;
  10093. WARN_ON(i915_gem_object_wait(obj, 0, MAX_SCHEDULE_TIMEOUT, NULL) < 0);
  10094. intel_pipe_update_start(crtc);
  10095. if (INTEL_GEN(dev_priv) >= 9)
  10096. skl_do_mmio_flip(crtc, work->rotation, work);
  10097. else
  10098. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  10099. ilk_do_mmio_flip(crtc, work);
  10100. intel_pipe_update_end(crtc, work);
  10101. }
  10102. static int intel_default_queue_flip(struct drm_device *dev,
  10103. struct drm_crtc *crtc,
  10104. struct drm_framebuffer *fb,
  10105. struct drm_i915_gem_object *obj,
  10106. struct drm_i915_gem_request *req,
  10107. uint32_t flags)
  10108. {
  10109. return -ENODEV;
  10110. }
  10111. static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
  10112. struct intel_crtc *intel_crtc,
  10113. struct intel_flip_work *work)
  10114. {
  10115. u32 addr, vblank;
  10116. if (!atomic_read(&work->pending))
  10117. return false;
  10118. smp_rmb();
  10119. vblank = intel_crtc_get_vblank_counter(intel_crtc);
  10120. if (work->flip_ready_vblank == 0) {
  10121. if (work->flip_queued_req &&
  10122. !i915_gem_request_completed(work->flip_queued_req))
  10123. return false;
  10124. work->flip_ready_vblank = vblank;
  10125. }
  10126. if (vblank - work->flip_ready_vblank < 3)
  10127. return false;
  10128. /* Potential stall - if we see that the flip has happened,
  10129. * assume a missed interrupt. */
  10130. if (INTEL_GEN(dev_priv) >= 4)
  10131. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  10132. else
  10133. addr = I915_READ(DSPADDR(intel_crtc->plane));
  10134. /* There is a potential issue here with a false positive after a flip
  10135. * to the same address. We could address this by checking for a
  10136. * non-incrementing frame counter.
  10137. */
  10138. return addr == work->gtt_offset;
  10139. }
  10140. void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
  10141. {
  10142. struct drm_device *dev = &dev_priv->drm;
  10143. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  10144. struct intel_flip_work *work;
  10145. WARN_ON(!in_interrupt());
  10146. if (crtc == NULL)
  10147. return;
  10148. spin_lock(&dev->event_lock);
  10149. work = crtc->flip_work;
  10150. if (work != NULL && !is_mmio_work(work) &&
  10151. __pageflip_stall_check_cs(dev_priv, crtc, work)) {
  10152. WARN_ONCE(1,
  10153. "Kicking stuck page flip: queued at %d, now %d\n",
  10154. work->flip_queued_vblank, intel_crtc_get_vblank_counter(crtc));
  10155. page_flip_completed(crtc);
  10156. work = NULL;
  10157. }
  10158. if (work != NULL && !is_mmio_work(work) &&
  10159. intel_crtc_get_vblank_counter(crtc) - work->flip_queued_vblank > 1)
  10160. intel_queue_rps_boost_for_request(work->flip_queued_req);
  10161. spin_unlock(&dev->event_lock);
  10162. }
  10163. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  10164. struct drm_framebuffer *fb,
  10165. struct drm_pending_vblank_event *event,
  10166. uint32_t page_flip_flags)
  10167. {
  10168. struct drm_device *dev = crtc->dev;
  10169. struct drm_i915_private *dev_priv = to_i915(dev);
  10170. struct drm_framebuffer *old_fb = crtc->primary->fb;
  10171. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10172. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10173. struct drm_plane *primary = crtc->primary;
  10174. enum pipe pipe = intel_crtc->pipe;
  10175. struct intel_flip_work *work;
  10176. struct intel_engine_cs *engine;
  10177. bool mmio_flip;
  10178. struct drm_i915_gem_request *request;
  10179. struct i915_vma *vma;
  10180. int ret;
  10181. /*
  10182. * drm_mode_page_flip_ioctl() should already catch this, but double
  10183. * check to be safe. In the future we may enable pageflipping from
  10184. * a disabled primary plane.
  10185. */
  10186. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  10187. return -EBUSY;
  10188. /* Can't change pixel format via MI display flips. */
  10189. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  10190. return -EINVAL;
  10191. /*
  10192. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  10193. * Note that pitch changes could also affect these register.
  10194. */
  10195. if (INTEL_GEN(dev_priv) > 3 &&
  10196. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  10197. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  10198. return -EINVAL;
  10199. if (i915_terminally_wedged(&dev_priv->gpu_error))
  10200. goto out_hang;
  10201. work = kzalloc(sizeof(*work), GFP_KERNEL);
  10202. if (work == NULL)
  10203. return -ENOMEM;
  10204. work->event = event;
  10205. work->crtc = crtc;
  10206. work->old_fb = old_fb;
  10207. INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
  10208. ret = drm_crtc_vblank_get(crtc);
  10209. if (ret)
  10210. goto free_work;
  10211. /* We borrow the event spin lock for protecting flip_work */
  10212. spin_lock_irq(&dev->event_lock);
  10213. if (intel_crtc->flip_work) {
  10214. /* Before declaring the flip queue wedged, check if
  10215. * the hardware completed the operation behind our backs.
  10216. */
  10217. if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
  10218. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  10219. page_flip_completed(intel_crtc);
  10220. } else {
  10221. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  10222. spin_unlock_irq(&dev->event_lock);
  10223. drm_crtc_vblank_put(crtc);
  10224. kfree(work);
  10225. return -EBUSY;
  10226. }
  10227. }
  10228. intel_crtc->flip_work = work;
  10229. spin_unlock_irq(&dev->event_lock);
  10230. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  10231. flush_workqueue(dev_priv->wq);
  10232. /* Reference the objects for the scheduled work. */
  10233. drm_framebuffer_reference(work->old_fb);
  10234. crtc->primary->fb = fb;
  10235. update_state_fb(crtc->primary);
  10236. work->pending_flip_obj = i915_gem_object_get(obj);
  10237. ret = i915_mutex_lock_interruptible(dev);
  10238. if (ret)
  10239. goto cleanup;
  10240. intel_crtc->reset_count = i915_reset_count(&dev_priv->gpu_error);
  10241. if (i915_reset_in_progress_or_wedged(&dev_priv->gpu_error)) {
  10242. ret = -EIO;
  10243. goto unlock;
  10244. }
  10245. atomic_inc(&intel_crtc->unpin_work_count);
  10246. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  10247. work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
  10248. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  10249. engine = dev_priv->engine[BCS];
  10250. if (fb->modifier != old_fb->modifier)
  10251. /* vlv: DISPLAY_FLIP fails to change tiling */
  10252. engine = NULL;
  10253. } else if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
  10254. engine = dev_priv->engine[BCS];
  10255. } else if (INTEL_GEN(dev_priv) >= 7) {
  10256. engine = i915_gem_object_last_write_engine(obj);
  10257. if (engine == NULL || engine->id != RCS)
  10258. engine = dev_priv->engine[BCS];
  10259. } else {
  10260. engine = dev_priv->engine[RCS];
  10261. }
  10262. mmio_flip = use_mmio_flip(engine, obj);
  10263. vma = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  10264. if (IS_ERR(vma)) {
  10265. ret = PTR_ERR(vma);
  10266. goto cleanup_pending;
  10267. }
  10268. work->gtt_offset = intel_fb_gtt_offset(fb, primary->state->rotation);
  10269. work->gtt_offset += intel_crtc->dspaddr_offset;
  10270. work->rotation = crtc->primary->state->rotation;
  10271. /*
  10272. * There's the potential that the next frame will not be compatible with
  10273. * FBC, so we want to call pre_update() before the actual page flip.
  10274. * The problem is that pre_update() caches some information about the fb
  10275. * object, so we want to do this only after the object is pinned. Let's
  10276. * be on the safe side and do this immediately before scheduling the
  10277. * flip.
  10278. */
  10279. intel_fbc_pre_update(intel_crtc, intel_crtc->config,
  10280. to_intel_plane_state(primary->state));
  10281. if (mmio_flip) {
  10282. INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
  10283. queue_work(system_unbound_wq, &work->mmio_work);
  10284. } else {
  10285. request = i915_gem_request_alloc(engine, engine->last_context);
  10286. if (IS_ERR(request)) {
  10287. ret = PTR_ERR(request);
  10288. goto cleanup_unpin;
  10289. }
  10290. ret = i915_gem_request_await_object(request, obj, false);
  10291. if (ret)
  10292. goto cleanup_request;
  10293. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  10294. page_flip_flags);
  10295. if (ret)
  10296. goto cleanup_request;
  10297. intel_mark_page_flip_active(intel_crtc, work);
  10298. work->flip_queued_req = i915_gem_request_get(request);
  10299. i915_add_request_no_flush(request);
  10300. }
  10301. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  10302. i915_gem_track_fb(intel_fb_obj(old_fb), obj,
  10303. to_intel_plane(primary)->frontbuffer_bit);
  10304. mutex_unlock(&dev->struct_mutex);
  10305. intel_frontbuffer_flip_prepare(to_i915(dev),
  10306. to_intel_plane(primary)->frontbuffer_bit);
  10307. trace_i915_flip_request(intel_crtc->plane, obj);
  10308. return 0;
  10309. cleanup_request:
  10310. i915_add_request_no_flush(request);
  10311. cleanup_unpin:
  10312. intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
  10313. cleanup_pending:
  10314. atomic_dec(&intel_crtc->unpin_work_count);
  10315. unlock:
  10316. mutex_unlock(&dev->struct_mutex);
  10317. cleanup:
  10318. crtc->primary->fb = old_fb;
  10319. update_state_fb(crtc->primary);
  10320. i915_gem_object_put(obj);
  10321. drm_framebuffer_unreference(work->old_fb);
  10322. spin_lock_irq(&dev->event_lock);
  10323. intel_crtc->flip_work = NULL;
  10324. spin_unlock_irq(&dev->event_lock);
  10325. drm_crtc_vblank_put(crtc);
  10326. free_work:
  10327. kfree(work);
  10328. if (ret == -EIO) {
  10329. struct drm_atomic_state *state;
  10330. struct drm_plane_state *plane_state;
  10331. out_hang:
  10332. state = drm_atomic_state_alloc(dev);
  10333. if (!state)
  10334. return -ENOMEM;
  10335. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  10336. retry:
  10337. plane_state = drm_atomic_get_plane_state(state, primary);
  10338. ret = PTR_ERR_OR_ZERO(plane_state);
  10339. if (!ret) {
  10340. drm_atomic_set_fb_for_plane(plane_state, fb);
  10341. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  10342. if (!ret)
  10343. ret = drm_atomic_commit(state);
  10344. }
  10345. if (ret == -EDEADLK) {
  10346. drm_modeset_backoff(state->acquire_ctx);
  10347. drm_atomic_state_clear(state);
  10348. goto retry;
  10349. }
  10350. drm_atomic_state_put(state);
  10351. if (ret == 0 && event) {
  10352. spin_lock_irq(&dev->event_lock);
  10353. drm_crtc_send_vblank_event(crtc, event);
  10354. spin_unlock_irq(&dev->event_lock);
  10355. }
  10356. }
  10357. return ret;
  10358. }
  10359. /**
  10360. * intel_wm_need_update - Check whether watermarks need updating
  10361. * @plane: drm plane
  10362. * @state: new plane state
  10363. *
  10364. * Check current plane state versus the new one to determine whether
  10365. * watermarks need to be recalculated.
  10366. *
  10367. * Returns true or false.
  10368. */
  10369. static bool intel_wm_need_update(struct drm_plane *plane,
  10370. struct drm_plane_state *state)
  10371. {
  10372. struct intel_plane_state *new = to_intel_plane_state(state);
  10373. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  10374. /* Update watermarks on tiling or size changes. */
  10375. if (new->base.visible != cur->base.visible)
  10376. return true;
  10377. if (!cur->base.fb || !new->base.fb)
  10378. return false;
  10379. if (cur->base.fb->modifier != new->base.fb->modifier ||
  10380. cur->base.rotation != new->base.rotation ||
  10381. drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
  10382. drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
  10383. drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
  10384. drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
  10385. return true;
  10386. return false;
  10387. }
  10388. static bool needs_scaling(struct intel_plane_state *state)
  10389. {
  10390. int src_w = drm_rect_width(&state->base.src) >> 16;
  10391. int src_h = drm_rect_height(&state->base.src) >> 16;
  10392. int dst_w = drm_rect_width(&state->base.dst);
  10393. int dst_h = drm_rect_height(&state->base.dst);
  10394. return (src_w != dst_w || src_h != dst_h);
  10395. }
  10396. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  10397. struct drm_plane_state *plane_state)
  10398. {
  10399. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  10400. struct drm_crtc *crtc = crtc_state->crtc;
  10401. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10402. struct drm_plane *plane = plane_state->plane;
  10403. struct drm_device *dev = crtc->dev;
  10404. struct drm_i915_private *dev_priv = to_i915(dev);
  10405. struct intel_plane_state *old_plane_state =
  10406. to_intel_plane_state(plane->state);
  10407. bool mode_changed = needs_modeset(crtc_state);
  10408. bool was_crtc_enabled = crtc->state->active;
  10409. bool is_crtc_enabled = crtc_state->active;
  10410. bool turn_off, turn_on, visible, was_visible;
  10411. struct drm_framebuffer *fb = plane_state->fb;
  10412. int ret;
  10413. if (INTEL_GEN(dev_priv) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
  10414. ret = skl_update_scaler_plane(
  10415. to_intel_crtc_state(crtc_state),
  10416. to_intel_plane_state(plane_state));
  10417. if (ret)
  10418. return ret;
  10419. }
  10420. was_visible = old_plane_state->base.visible;
  10421. visible = to_intel_plane_state(plane_state)->base.visible;
  10422. if (!was_crtc_enabled && WARN_ON(was_visible))
  10423. was_visible = false;
  10424. /*
  10425. * Visibility is calculated as if the crtc was on, but
  10426. * after scaler setup everything depends on it being off
  10427. * when the crtc isn't active.
  10428. *
  10429. * FIXME this is wrong for watermarks. Watermarks should also
  10430. * be computed as if the pipe would be active. Perhaps move
  10431. * per-plane wm computation to the .check_plane() hook, and
  10432. * only combine the results from all planes in the current place?
  10433. */
  10434. if (!is_crtc_enabled)
  10435. to_intel_plane_state(plane_state)->base.visible = visible = false;
  10436. if (!was_visible && !visible)
  10437. return 0;
  10438. if (fb != old_plane_state->base.fb)
  10439. pipe_config->fb_changed = true;
  10440. turn_off = was_visible && (!visible || mode_changed);
  10441. turn_on = visible && (!was_visible || mode_changed);
  10442. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  10443. intel_crtc->base.base.id,
  10444. intel_crtc->base.name,
  10445. plane->base.id, plane->name,
  10446. fb ? fb->base.id : -1);
  10447. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  10448. plane->base.id, plane->name,
  10449. was_visible, visible,
  10450. turn_off, turn_on, mode_changed);
  10451. if (turn_on) {
  10452. pipe_config->update_wm_pre = true;
  10453. /* must disable cxsr around plane enable/disable */
  10454. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  10455. pipe_config->disable_cxsr = true;
  10456. } else if (turn_off) {
  10457. pipe_config->update_wm_post = true;
  10458. /* must disable cxsr around plane enable/disable */
  10459. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  10460. pipe_config->disable_cxsr = true;
  10461. } else if (intel_wm_need_update(plane, plane_state)) {
  10462. /* FIXME bollocks */
  10463. pipe_config->update_wm_pre = true;
  10464. pipe_config->update_wm_post = true;
  10465. }
  10466. /* Pre-gen9 platforms need two-step watermark updates */
  10467. if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
  10468. INTEL_GEN(dev_priv) < 9 && dev_priv->display.optimize_watermarks)
  10469. to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
  10470. if (visible || was_visible)
  10471. pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
  10472. /*
  10473. * WaCxSRDisabledForSpriteScaling:ivb
  10474. *
  10475. * cstate->update_wm was already set above, so this flag will
  10476. * take effect when we commit and program watermarks.
  10477. */
  10478. if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev_priv) &&
  10479. needs_scaling(to_intel_plane_state(plane_state)) &&
  10480. !needs_scaling(old_plane_state))
  10481. pipe_config->disable_lp_wm = true;
  10482. return 0;
  10483. }
  10484. static bool encoders_cloneable(const struct intel_encoder *a,
  10485. const struct intel_encoder *b)
  10486. {
  10487. /* masks could be asymmetric, so check both ways */
  10488. return a == b || (a->cloneable & (1 << b->type) &&
  10489. b->cloneable & (1 << a->type));
  10490. }
  10491. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  10492. struct intel_crtc *crtc,
  10493. struct intel_encoder *encoder)
  10494. {
  10495. struct intel_encoder *source_encoder;
  10496. struct drm_connector *connector;
  10497. struct drm_connector_state *connector_state;
  10498. int i;
  10499. for_each_connector_in_state(state, connector, connector_state, i) {
  10500. if (connector_state->crtc != &crtc->base)
  10501. continue;
  10502. source_encoder =
  10503. to_intel_encoder(connector_state->best_encoder);
  10504. if (!encoders_cloneable(encoder, source_encoder))
  10505. return false;
  10506. }
  10507. return true;
  10508. }
  10509. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  10510. struct drm_crtc_state *crtc_state)
  10511. {
  10512. struct drm_device *dev = crtc->dev;
  10513. struct drm_i915_private *dev_priv = to_i915(dev);
  10514. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10515. struct intel_crtc_state *pipe_config =
  10516. to_intel_crtc_state(crtc_state);
  10517. struct drm_atomic_state *state = crtc_state->state;
  10518. int ret;
  10519. bool mode_changed = needs_modeset(crtc_state);
  10520. if (mode_changed && !crtc_state->active)
  10521. pipe_config->update_wm_post = true;
  10522. if (mode_changed && crtc_state->enable &&
  10523. dev_priv->display.crtc_compute_clock &&
  10524. !WARN_ON(pipe_config->shared_dpll)) {
  10525. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  10526. pipe_config);
  10527. if (ret)
  10528. return ret;
  10529. }
  10530. if (crtc_state->color_mgmt_changed) {
  10531. ret = intel_color_check(crtc, crtc_state);
  10532. if (ret)
  10533. return ret;
  10534. /*
  10535. * Changing color management on Intel hardware is
  10536. * handled as part of planes update.
  10537. */
  10538. crtc_state->planes_changed = true;
  10539. }
  10540. ret = 0;
  10541. if (dev_priv->display.compute_pipe_wm) {
  10542. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  10543. if (ret) {
  10544. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  10545. return ret;
  10546. }
  10547. }
  10548. if (dev_priv->display.compute_intermediate_wm &&
  10549. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  10550. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  10551. return 0;
  10552. /*
  10553. * Calculate 'intermediate' watermarks that satisfy both the
  10554. * old state and the new state. We can program these
  10555. * immediately.
  10556. */
  10557. ret = dev_priv->display.compute_intermediate_wm(dev,
  10558. intel_crtc,
  10559. pipe_config);
  10560. if (ret) {
  10561. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  10562. return ret;
  10563. }
  10564. } else if (dev_priv->display.compute_intermediate_wm) {
  10565. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  10566. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  10567. }
  10568. if (INTEL_GEN(dev_priv) >= 9) {
  10569. if (mode_changed)
  10570. ret = skl_update_scaler_crtc(pipe_config);
  10571. if (!ret)
  10572. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  10573. pipe_config);
  10574. }
  10575. return ret;
  10576. }
  10577. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  10578. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  10579. .atomic_begin = intel_begin_crtc_commit,
  10580. .atomic_flush = intel_finish_crtc_commit,
  10581. .atomic_check = intel_crtc_atomic_check,
  10582. };
  10583. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  10584. {
  10585. struct intel_connector *connector;
  10586. for_each_intel_connector(dev, connector) {
  10587. if (connector->base.state->crtc)
  10588. drm_connector_unreference(&connector->base);
  10589. if (connector->base.encoder) {
  10590. connector->base.state->best_encoder =
  10591. connector->base.encoder;
  10592. connector->base.state->crtc =
  10593. connector->base.encoder->crtc;
  10594. drm_connector_reference(&connector->base);
  10595. } else {
  10596. connector->base.state->best_encoder = NULL;
  10597. connector->base.state->crtc = NULL;
  10598. }
  10599. }
  10600. }
  10601. static void
  10602. connected_sink_compute_bpp(struct intel_connector *connector,
  10603. struct intel_crtc_state *pipe_config)
  10604. {
  10605. const struct drm_display_info *info = &connector->base.display_info;
  10606. int bpp = pipe_config->pipe_bpp;
  10607. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  10608. connector->base.base.id,
  10609. connector->base.name);
  10610. /* Don't use an invalid EDID bpc value */
  10611. if (info->bpc != 0 && info->bpc * 3 < bpp) {
  10612. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  10613. bpp, info->bpc * 3);
  10614. pipe_config->pipe_bpp = info->bpc * 3;
  10615. }
  10616. /* Clamp bpp to 8 on screens without EDID 1.4 */
  10617. if (info->bpc == 0 && bpp > 24) {
  10618. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  10619. bpp);
  10620. pipe_config->pipe_bpp = 24;
  10621. }
  10622. }
  10623. static int
  10624. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  10625. struct intel_crtc_state *pipe_config)
  10626. {
  10627. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  10628. struct drm_atomic_state *state;
  10629. struct drm_connector *connector;
  10630. struct drm_connector_state *connector_state;
  10631. int bpp, i;
  10632. if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  10633. IS_CHERRYVIEW(dev_priv)))
  10634. bpp = 10*3;
  10635. else if (INTEL_GEN(dev_priv) >= 5)
  10636. bpp = 12*3;
  10637. else
  10638. bpp = 8*3;
  10639. pipe_config->pipe_bpp = bpp;
  10640. state = pipe_config->base.state;
  10641. /* Clamp display bpp to EDID value */
  10642. for_each_connector_in_state(state, connector, connector_state, i) {
  10643. if (connector_state->crtc != &crtc->base)
  10644. continue;
  10645. connected_sink_compute_bpp(to_intel_connector(connector),
  10646. pipe_config);
  10647. }
  10648. return bpp;
  10649. }
  10650. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  10651. {
  10652. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  10653. "type: 0x%x flags: 0x%x\n",
  10654. mode->crtc_clock,
  10655. mode->crtc_hdisplay, mode->crtc_hsync_start,
  10656. mode->crtc_hsync_end, mode->crtc_htotal,
  10657. mode->crtc_vdisplay, mode->crtc_vsync_start,
  10658. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  10659. }
  10660. static inline void
  10661. intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
  10662. unsigned int lane_count, struct intel_link_m_n *m_n)
  10663. {
  10664. DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10665. id, lane_count,
  10666. m_n->gmch_m, m_n->gmch_n,
  10667. m_n->link_m, m_n->link_n, m_n->tu);
  10668. }
  10669. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  10670. struct intel_crtc_state *pipe_config,
  10671. const char *context)
  10672. {
  10673. struct drm_device *dev = crtc->base.dev;
  10674. struct drm_i915_private *dev_priv = to_i915(dev);
  10675. struct drm_plane *plane;
  10676. struct intel_plane *intel_plane;
  10677. struct intel_plane_state *state;
  10678. struct drm_framebuffer *fb;
  10679. DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
  10680. crtc->base.base.id, crtc->base.name, context);
  10681. DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
  10682. transcoder_name(pipe_config->cpu_transcoder),
  10683. pipe_config->pipe_bpp, pipe_config->dither);
  10684. if (pipe_config->has_pch_encoder)
  10685. intel_dump_m_n_config(pipe_config, "fdi",
  10686. pipe_config->fdi_lanes,
  10687. &pipe_config->fdi_m_n);
  10688. if (intel_crtc_has_dp_encoder(pipe_config)) {
  10689. intel_dump_m_n_config(pipe_config, "dp m_n",
  10690. pipe_config->lane_count, &pipe_config->dp_m_n);
  10691. if (pipe_config->has_drrs)
  10692. intel_dump_m_n_config(pipe_config, "dp m2_n2",
  10693. pipe_config->lane_count,
  10694. &pipe_config->dp_m2_n2);
  10695. }
  10696. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  10697. pipe_config->has_audio, pipe_config->has_infoframe);
  10698. DRM_DEBUG_KMS("requested mode:\n");
  10699. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  10700. DRM_DEBUG_KMS("adjusted mode:\n");
  10701. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  10702. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  10703. DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d\n",
  10704. pipe_config->port_clock,
  10705. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  10706. if (INTEL_GEN(dev_priv) >= 9)
  10707. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  10708. crtc->num_scalers,
  10709. pipe_config->scaler_state.scaler_users,
  10710. pipe_config->scaler_state.scaler_id);
  10711. if (HAS_GMCH_DISPLAY(dev_priv))
  10712. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10713. pipe_config->gmch_pfit.control,
  10714. pipe_config->gmch_pfit.pgm_ratios,
  10715. pipe_config->gmch_pfit.lvds_border_bits);
  10716. else
  10717. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10718. pipe_config->pch_pfit.pos,
  10719. pipe_config->pch_pfit.size,
  10720. enableddisabled(pipe_config->pch_pfit.enabled));
  10721. DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
  10722. pipe_config->ips_enabled, pipe_config->double_wide);
  10723. if (IS_BROXTON(dev_priv)) {
  10724. DRM_DEBUG_KMS("dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
  10725. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10726. "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
  10727. pipe_config->dpll_hw_state.ebb0,
  10728. pipe_config->dpll_hw_state.ebb4,
  10729. pipe_config->dpll_hw_state.pll0,
  10730. pipe_config->dpll_hw_state.pll1,
  10731. pipe_config->dpll_hw_state.pll2,
  10732. pipe_config->dpll_hw_state.pll3,
  10733. pipe_config->dpll_hw_state.pll6,
  10734. pipe_config->dpll_hw_state.pll8,
  10735. pipe_config->dpll_hw_state.pll9,
  10736. pipe_config->dpll_hw_state.pll10,
  10737. pipe_config->dpll_hw_state.pcsdw12);
  10738. } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
  10739. DRM_DEBUG_KMS("dpll_hw_state: "
  10740. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10741. pipe_config->dpll_hw_state.ctrl1,
  10742. pipe_config->dpll_hw_state.cfgcr1,
  10743. pipe_config->dpll_hw_state.cfgcr2);
  10744. } else if (HAS_DDI(dev_priv)) {
  10745. DRM_DEBUG_KMS("dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
  10746. pipe_config->dpll_hw_state.wrpll,
  10747. pipe_config->dpll_hw_state.spll);
  10748. } else {
  10749. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10750. "fp0: 0x%x, fp1: 0x%x\n",
  10751. pipe_config->dpll_hw_state.dpll,
  10752. pipe_config->dpll_hw_state.dpll_md,
  10753. pipe_config->dpll_hw_state.fp0,
  10754. pipe_config->dpll_hw_state.fp1);
  10755. }
  10756. DRM_DEBUG_KMS("planes on this crtc\n");
  10757. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10758. struct drm_format_name_buf format_name;
  10759. intel_plane = to_intel_plane(plane);
  10760. if (intel_plane->pipe != crtc->pipe)
  10761. continue;
  10762. state = to_intel_plane_state(plane->state);
  10763. fb = state->base.fb;
  10764. if (!fb) {
  10765. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  10766. plane->base.id, plane->name, state->scaler_id);
  10767. continue;
  10768. }
  10769. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
  10770. plane->base.id, plane->name,
  10771. fb->base.id, fb->width, fb->height,
  10772. drm_get_format_name(fb->pixel_format, &format_name));
  10773. if (INTEL_GEN(dev_priv) >= 9)
  10774. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  10775. state->scaler_id,
  10776. state->base.src.x1 >> 16,
  10777. state->base.src.y1 >> 16,
  10778. drm_rect_width(&state->base.src) >> 16,
  10779. drm_rect_height(&state->base.src) >> 16,
  10780. state->base.dst.x1, state->base.dst.y1,
  10781. drm_rect_width(&state->base.dst),
  10782. drm_rect_height(&state->base.dst));
  10783. }
  10784. }
  10785. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10786. {
  10787. struct drm_device *dev = state->dev;
  10788. struct drm_connector *connector;
  10789. unsigned int used_ports = 0;
  10790. unsigned int used_mst_ports = 0;
  10791. /*
  10792. * Walk the connector list instead of the encoder
  10793. * list to detect the problem on ddi platforms
  10794. * where there's just one encoder per digital port.
  10795. */
  10796. drm_for_each_connector(connector, dev) {
  10797. struct drm_connector_state *connector_state;
  10798. struct intel_encoder *encoder;
  10799. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  10800. if (!connector_state)
  10801. connector_state = connector->state;
  10802. if (!connector_state->best_encoder)
  10803. continue;
  10804. encoder = to_intel_encoder(connector_state->best_encoder);
  10805. WARN_ON(!connector_state->crtc);
  10806. switch (encoder->type) {
  10807. unsigned int port_mask;
  10808. case INTEL_OUTPUT_UNKNOWN:
  10809. if (WARN_ON(!HAS_DDI(to_i915(dev))))
  10810. break;
  10811. case INTEL_OUTPUT_DP:
  10812. case INTEL_OUTPUT_HDMI:
  10813. case INTEL_OUTPUT_EDP:
  10814. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10815. /* the same port mustn't appear more than once */
  10816. if (used_ports & port_mask)
  10817. return false;
  10818. used_ports |= port_mask;
  10819. break;
  10820. case INTEL_OUTPUT_DP_MST:
  10821. used_mst_ports |=
  10822. 1 << enc_to_mst(&encoder->base)->primary->port;
  10823. break;
  10824. default:
  10825. break;
  10826. }
  10827. }
  10828. /* can't mix MST and SST/HDMI on the same port */
  10829. if (used_ports & used_mst_ports)
  10830. return false;
  10831. return true;
  10832. }
  10833. static void
  10834. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10835. {
  10836. struct drm_crtc_state tmp_state;
  10837. struct intel_crtc_scaler_state scaler_state;
  10838. struct intel_dpll_hw_state dpll_hw_state;
  10839. struct intel_shared_dpll *shared_dpll;
  10840. bool force_thru;
  10841. /* FIXME: before the switch to atomic started, a new pipe_config was
  10842. * kzalloc'd. Code that depends on any field being zero should be
  10843. * fixed, so that the crtc_state can be safely duplicated. For now,
  10844. * only fields that are know to not cause problems are preserved. */
  10845. tmp_state = crtc_state->base;
  10846. scaler_state = crtc_state->scaler_state;
  10847. shared_dpll = crtc_state->shared_dpll;
  10848. dpll_hw_state = crtc_state->dpll_hw_state;
  10849. force_thru = crtc_state->pch_pfit.force_thru;
  10850. memset(crtc_state, 0, sizeof *crtc_state);
  10851. crtc_state->base = tmp_state;
  10852. crtc_state->scaler_state = scaler_state;
  10853. crtc_state->shared_dpll = shared_dpll;
  10854. crtc_state->dpll_hw_state = dpll_hw_state;
  10855. crtc_state->pch_pfit.force_thru = force_thru;
  10856. }
  10857. static int
  10858. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10859. struct intel_crtc_state *pipe_config)
  10860. {
  10861. struct drm_atomic_state *state = pipe_config->base.state;
  10862. struct intel_encoder *encoder;
  10863. struct drm_connector *connector;
  10864. struct drm_connector_state *connector_state;
  10865. int base_bpp, ret = -EINVAL;
  10866. int i;
  10867. bool retry = true;
  10868. clear_intel_crtc_state(pipe_config);
  10869. pipe_config->cpu_transcoder =
  10870. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10871. /*
  10872. * Sanitize sync polarity flags based on requested ones. If neither
  10873. * positive or negative polarity is requested, treat this as meaning
  10874. * negative polarity.
  10875. */
  10876. if (!(pipe_config->base.adjusted_mode.flags &
  10877. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10878. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10879. if (!(pipe_config->base.adjusted_mode.flags &
  10880. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10881. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10882. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10883. pipe_config);
  10884. if (base_bpp < 0)
  10885. goto fail;
  10886. /*
  10887. * Determine the real pipe dimensions. Note that stereo modes can
  10888. * increase the actual pipe size due to the frame doubling and
  10889. * insertion of additional space for blanks between the frame. This
  10890. * is stored in the crtc timings. We use the requested mode to do this
  10891. * computation to clearly distinguish it from the adjusted mode, which
  10892. * can be changed by the connectors in the below retry loop.
  10893. */
  10894. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10895. &pipe_config->pipe_src_w,
  10896. &pipe_config->pipe_src_h);
  10897. for_each_connector_in_state(state, connector, connector_state, i) {
  10898. if (connector_state->crtc != crtc)
  10899. continue;
  10900. encoder = to_intel_encoder(connector_state->best_encoder);
  10901. if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
  10902. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  10903. goto fail;
  10904. }
  10905. /*
  10906. * Determine output_types before calling the .compute_config()
  10907. * hooks so that the hooks can use this information safely.
  10908. */
  10909. pipe_config->output_types |= 1 << encoder->type;
  10910. }
  10911. encoder_retry:
  10912. /* Ensure the port clock defaults are reset when retrying. */
  10913. pipe_config->port_clock = 0;
  10914. pipe_config->pixel_multiplier = 1;
  10915. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10916. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10917. CRTC_STEREO_DOUBLE);
  10918. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10919. * adjust it according to limitations or connector properties, and also
  10920. * a chance to reject the mode entirely.
  10921. */
  10922. for_each_connector_in_state(state, connector, connector_state, i) {
  10923. if (connector_state->crtc != crtc)
  10924. continue;
  10925. encoder = to_intel_encoder(connector_state->best_encoder);
  10926. if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
  10927. DRM_DEBUG_KMS("Encoder config failure\n");
  10928. goto fail;
  10929. }
  10930. }
  10931. /* Set default port clock if not overwritten by the encoder. Needs to be
  10932. * done afterwards in case the encoder adjusts the mode. */
  10933. if (!pipe_config->port_clock)
  10934. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10935. * pipe_config->pixel_multiplier;
  10936. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10937. if (ret < 0) {
  10938. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10939. goto fail;
  10940. }
  10941. if (ret == RETRY) {
  10942. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10943. ret = -EINVAL;
  10944. goto fail;
  10945. }
  10946. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10947. retry = false;
  10948. goto encoder_retry;
  10949. }
  10950. /* Dithering seems to not pass-through bits correctly when it should, so
  10951. * only enable it on 6bpc panels. */
  10952. pipe_config->dither = pipe_config->pipe_bpp == 6*3;
  10953. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  10954. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10955. fail:
  10956. return ret;
  10957. }
  10958. static void
  10959. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  10960. {
  10961. struct drm_crtc *crtc;
  10962. struct drm_crtc_state *crtc_state;
  10963. int i;
  10964. /* Double check state. */
  10965. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10966. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10967. /* Update hwmode for vblank functions */
  10968. if (crtc->state->active)
  10969. crtc->hwmode = crtc->state->adjusted_mode;
  10970. else
  10971. crtc->hwmode.crtc_clock = 0;
  10972. /*
  10973. * Update legacy state to satisfy fbc code. This can
  10974. * be removed when fbc uses the atomic state.
  10975. */
  10976. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10977. struct drm_plane_state *plane_state = crtc->primary->state;
  10978. crtc->primary->fb = plane_state->fb;
  10979. crtc->x = plane_state->src_x >> 16;
  10980. crtc->y = plane_state->src_y >> 16;
  10981. }
  10982. }
  10983. }
  10984. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10985. {
  10986. int diff;
  10987. if (clock1 == clock2)
  10988. return true;
  10989. if (!clock1 || !clock2)
  10990. return false;
  10991. diff = abs(clock1 - clock2);
  10992. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10993. return true;
  10994. return false;
  10995. }
  10996. static bool
  10997. intel_compare_m_n(unsigned int m, unsigned int n,
  10998. unsigned int m2, unsigned int n2,
  10999. bool exact)
  11000. {
  11001. if (m == m2 && n == n2)
  11002. return true;
  11003. if (exact || !m || !n || !m2 || !n2)
  11004. return false;
  11005. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  11006. if (n > n2) {
  11007. while (n > n2) {
  11008. m2 <<= 1;
  11009. n2 <<= 1;
  11010. }
  11011. } else if (n < n2) {
  11012. while (n < n2) {
  11013. m <<= 1;
  11014. n <<= 1;
  11015. }
  11016. }
  11017. if (n != n2)
  11018. return false;
  11019. return intel_fuzzy_clock_check(m, m2);
  11020. }
  11021. static bool
  11022. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  11023. struct intel_link_m_n *m2_n2,
  11024. bool adjust)
  11025. {
  11026. if (m_n->tu == m2_n2->tu &&
  11027. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  11028. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  11029. intel_compare_m_n(m_n->link_m, m_n->link_n,
  11030. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  11031. if (adjust)
  11032. *m2_n2 = *m_n;
  11033. return true;
  11034. }
  11035. return false;
  11036. }
  11037. static bool
  11038. intel_pipe_config_compare(struct drm_i915_private *dev_priv,
  11039. struct intel_crtc_state *current_config,
  11040. struct intel_crtc_state *pipe_config,
  11041. bool adjust)
  11042. {
  11043. bool ret = true;
  11044. #define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
  11045. do { \
  11046. if (!adjust) \
  11047. DRM_ERROR(fmt, ##__VA_ARGS__); \
  11048. else \
  11049. DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
  11050. } while (0)
  11051. #define PIPE_CONF_CHECK_X(name) \
  11052. if (current_config->name != pipe_config->name) { \
  11053. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  11054. "(expected 0x%08x, found 0x%08x)\n", \
  11055. current_config->name, \
  11056. pipe_config->name); \
  11057. ret = false; \
  11058. }
  11059. #define PIPE_CONF_CHECK_I(name) \
  11060. if (current_config->name != pipe_config->name) { \
  11061. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  11062. "(expected %i, found %i)\n", \
  11063. current_config->name, \
  11064. pipe_config->name); \
  11065. ret = false; \
  11066. }
  11067. #define PIPE_CONF_CHECK_P(name) \
  11068. if (current_config->name != pipe_config->name) { \
  11069. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  11070. "(expected %p, found %p)\n", \
  11071. current_config->name, \
  11072. pipe_config->name); \
  11073. ret = false; \
  11074. }
  11075. #define PIPE_CONF_CHECK_M_N(name) \
  11076. if (!intel_compare_link_m_n(&current_config->name, \
  11077. &pipe_config->name,\
  11078. adjust)) { \
  11079. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  11080. "(expected tu %i gmch %i/%i link %i/%i, " \
  11081. "found tu %i, gmch %i/%i link %i/%i)\n", \
  11082. current_config->name.tu, \
  11083. current_config->name.gmch_m, \
  11084. current_config->name.gmch_n, \
  11085. current_config->name.link_m, \
  11086. current_config->name.link_n, \
  11087. pipe_config->name.tu, \
  11088. pipe_config->name.gmch_m, \
  11089. pipe_config->name.gmch_n, \
  11090. pipe_config->name.link_m, \
  11091. pipe_config->name.link_n); \
  11092. ret = false; \
  11093. }
  11094. /* This is required for BDW+ where there is only one set of registers for
  11095. * switching between high and low RR.
  11096. * This macro can be used whenever a comparison has to be made between one
  11097. * hw state and multiple sw state variables.
  11098. */
  11099. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  11100. if (!intel_compare_link_m_n(&current_config->name, \
  11101. &pipe_config->name, adjust) && \
  11102. !intel_compare_link_m_n(&current_config->alt_name, \
  11103. &pipe_config->name, adjust)) { \
  11104. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  11105. "(expected tu %i gmch %i/%i link %i/%i, " \
  11106. "or tu %i gmch %i/%i link %i/%i, " \
  11107. "found tu %i, gmch %i/%i link %i/%i)\n", \
  11108. current_config->name.tu, \
  11109. current_config->name.gmch_m, \
  11110. current_config->name.gmch_n, \
  11111. current_config->name.link_m, \
  11112. current_config->name.link_n, \
  11113. current_config->alt_name.tu, \
  11114. current_config->alt_name.gmch_m, \
  11115. current_config->alt_name.gmch_n, \
  11116. current_config->alt_name.link_m, \
  11117. current_config->alt_name.link_n, \
  11118. pipe_config->name.tu, \
  11119. pipe_config->name.gmch_m, \
  11120. pipe_config->name.gmch_n, \
  11121. pipe_config->name.link_m, \
  11122. pipe_config->name.link_n); \
  11123. ret = false; \
  11124. }
  11125. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  11126. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  11127. INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
  11128. "(expected %i, found %i)\n", \
  11129. current_config->name & (mask), \
  11130. pipe_config->name & (mask)); \
  11131. ret = false; \
  11132. }
  11133. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  11134. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  11135. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  11136. "(expected %i, found %i)\n", \
  11137. current_config->name, \
  11138. pipe_config->name); \
  11139. ret = false; \
  11140. }
  11141. #define PIPE_CONF_QUIRK(quirk) \
  11142. ((current_config->quirks | pipe_config->quirks) & (quirk))
  11143. PIPE_CONF_CHECK_I(cpu_transcoder);
  11144. PIPE_CONF_CHECK_I(has_pch_encoder);
  11145. PIPE_CONF_CHECK_I(fdi_lanes);
  11146. PIPE_CONF_CHECK_M_N(fdi_m_n);
  11147. PIPE_CONF_CHECK_I(lane_count);
  11148. PIPE_CONF_CHECK_X(lane_lat_optim_mask);
  11149. if (INTEL_GEN(dev_priv) < 8) {
  11150. PIPE_CONF_CHECK_M_N(dp_m_n);
  11151. if (current_config->has_drrs)
  11152. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  11153. } else
  11154. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  11155. PIPE_CONF_CHECK_X(output_types);
  11156. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  11157. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  11158. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  11159. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  11160. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  11161. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  11162. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  11163. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  11164. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  11165. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  11166. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  11167. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  11168. PIPE_CONF_CHECK_I(pixel_multiplier);
  11169. PIPE_CONF_CHECK_I(has_hdmi_sink);
  11170. if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
  11171. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11172. PIPE_CONF_CHECK_I(limited_color_range);
  11173. PIPE_CONF_CHECK_I(has_infoframe);
  11174. PIPE_CONF_CHECK_I(has_audio);
  11175. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11176. DRM_MODE_FLAG_INTERLACE);
  11177. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  11178. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11179. DRM_MODE_FLAG_PHSYNC);
  11180. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11181. DRM_MODE_FLAG_NHSYNC);
  11182. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11183. DRM_MODE_FLAG_PVSYNC);
  11184. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11185. DRM_MODE_FLAG_NVSYNC);
  11186. }
  11187. PIPE_CONF_CHECK_X(gmch_pfit.control);
  11188. /* pfit ratios are autocomputed by the hw on gen4+ */
  11189. if (INTEL_GEN(dev_priv) < 4)
  11190. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  11191. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  11192. if (!adjust) {
  11193. PIPE_CONF_CHECK_I(pipe_src_w);
  11194. PIPE_CONF_CHECK_I(pipe_src_h);
  11195. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  11196. if (current_config->pch_pfit.enabled) {
  11197. PIPE_CONF_CHECK_X(pch_pfit.pos);
  11198. PIPE_CONF_CHECK_X(pch_pfit.size);
  11199. }
  11200. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  11201. }
  11202. /* BDW+ don't expose a synchronous way to read the state */
  11203. if (IS_HASWELL(dev_priv))
  11204. PIPE_CONF_CHECK_I(ips_enabled);
  11205. PIPE_CONF_CHECK_I(double_wide);
  11206. PIPE_CONF_CHECK_P(shared_dpll);
  11207. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  11208. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  11209. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  11210. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  11211. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  11212. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  11213. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  11214. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  11215. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  11216. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  11217. PIPE_CONF_CHECK_X(dsi_pll.div);
  11218. if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
  11219. PIPE_CONF_CHECK_I(pipe_bpp);
  11220. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  11221. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  11222. #undef PIPE_CONF_CHECK_X
  11223. #undef PIPE_CONF_CHECK_I
  11224. #undef PIPE_CONF_CHECK_P
  11225. #undef PIPE_CONF_CHECK_FLAGS
  11226. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  11227. #undef PIPE_CONF_QUIRK
  11228. #undef INTEL_ERR_OR_DBG_KMS
  11229. return ret;
  11230. }
  11231. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  11232. const struct intel_crtc_state *pipe_config)
  11233. {
  11234. if (pipe_config->has_pch_encoder) {
  11235. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  11236. &pipe_config->fdi_m_n);
  11237. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  11238. /*
  11239. * FDI already provided one idea for the dotclock.
  11240. * Yell if the encoder disagrees.
  11241. */
  11242. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  11243. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  11244. fdi_dotclock, dotclock);
  11245. }
  11246. }
  11247. static void verify_wm_state(struct drm_crtc *crtc,
  11248. struct drm_crtc_state *new_state)
  11249. {
  11250. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  11251. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  11252. struct skl_pipe_wm hw_wm, *sw_wm;
  11253. struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
  11254. struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
  11255. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11256. const enum pipe pipe = intel_crtc->pipe;
  11257. int plane, level, max_level = ilk_wm_max_level(dev_priv);
  11258. if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
  11259. return;
  11260. skl_pipe_wm_get_hw_state(crtc, &hw_wm);
  11261. sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
  11262. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  11263. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  11264. /* planes */
  11265. for_each_universal_plane(dev_priv, pipe, plane) {
  11266. hw_plane_wm = &hw_wm.planes[plane];
  11267. sw_plane_wm = &sw_wm->planes[plane];
  11268. /* Watermarks */
  11269. for (level = 0; level <= max_level; level++) {
  11270. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  11271. &sw_plane_wm->wm[level]))
  11272. continue;
  11273. 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",
  11274. pipe_name(pipe), plane + 1, level,
  11275. sw_plane_wm->wm[level].plane_en,
  11276. sw_plane_wm->wm[level].plane_res_b,
  11277. sw_plane_wm->wm[level].plane_res_l,
  11278. hw_plane_wm->wm[level].plane_en,
  11279. hw_plane_wm->wm[level].plane_res_b,
  11280. hw_plane_wm->wm[level].plane_res_l);
  11281. }
  11282. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  11283. &sw_plane_wm->trans_wm)) {
  11284. 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",
  11285. pipe_name(pipe), plane + 1,
  11286. sw_plane_wm->trans_wm.plane_en,
  11287. sw_plane_wm->trans_wm.plane_res_b,
  11288. sw_plane_wm->trans_wm.plane_res_l,
  11289. hw_plane_wm->trans_wm.plane_en,
  11290. hw_plane_wm->trans_wm.plane_res_b,
  11291. hw_plane_wm->trans_wm.plane_res_l);
  11292. }
  11293. /* DDB */
  11294. hw_ddb_entry = &hw_ddb.plane[pipe][plane];
  11295. sw_ddb_entry = &sw_ddb->plane[pipe][plane];
  11296. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  11297. DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
  11298. pipe_name(pipe), plane + 1,
  11299. sw_ddb_entry->start, sw_ddb_entry->end,
  11300. hw_ddb_entry->start, hw_ddb_entry->end);
  11301. }
  11302. }
  11303. /*
  11304. * cursor
  11305. * If the cursor plane isn't active, we may not have updated it's ddb
  11306. * allocation. In that case since the ddb allocation will be updated
  11307. * once the plane becomes visible, we can skip this check
  11308. */
  11309. if (intel_crtc->cursor_addr) {
  11310. hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
  11311. sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
  11312. /* Watermarks */
  11313. for (level = 0; level <= max_level; level++) {
  11314. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  11315. &sw_plane_wm->wm[level]))
  11316. continue;
  11317. 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",
  11318. pipe_name(pipe), level,
  11319. sw_plane_wm->wm[level].plane_en,
  11320. sw_plane_wm->wm[level].plane_res_b,
  11321. sw_plane_wm->wm[level].plane_res_l,
  11322. hw_plane_wm->wm[level].plane_en,
  11323. hw_plane_wm->wm[level].plane_res_b,
  11324. hw_plane_wm->wm[level].plane_res_l);
  11325. }
  11326. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  11327. &sw_plane_wm->trans_wm)) {
  11328. DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  11329. pipe_name(pipe),
  11330. sw_plane_wm->trans_wm.plane_en,
  11331. sw_plane_wm->trans_wm.plane_res_b,
  11332. sw_plane_wm->trans_wm.plane_res_l,
  11333. hw_plane_wm->trans_wm.plane_en,
  11334. hw_plane_wm->trans_wm.plane_res_b,
  11335. hw_plane_wm->trans_wm.plane_res_l);
  11336. }
  11337. /* DDB */
  11338. hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  11339. sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  11340. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  11341. DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
  11342. pipe_name(pipe),
  11343. sw_ddb_entry->start, sw_ddb_entry->end,
  11344. hw_ddb_entry->start, hw_ddb_entry->end);
  11345. }
  11346. }
  11347. }
  11348. static void
  11349. verify_connector_state(struct drm_device *dev,
  11350. struct drm_atomic_state *state,
  11351. struct drm_crtc *crtc)
  11352. {
  11353. struct drm_connector *connector;
  11354. struct drm_connector_state *old_conn_state;
  11355. int i;
  11356. for_each_connector_in_state(state, connector, old_conn_state, i) {
  11357. struct drm_encoder *encoder = connector->encoder;
  11358. struct drm_connector_state *state = connector->state;
  11359. if (state->crtc != crtc)
  11360. continue;
  11361. intel_connector_verify_state(to_intel_connector(connector));
  11362. I915_STATE_WARN(state->best_encoder != encoder,
  11363. "connector's atomic encoder doesn't match legacy encoder\n");
  11364. }
  11365. }
  11366. static void
  11367. verify_encoder_state(struct drm_device *dev)
  11368. {
  11369. struct intel_encoder *encoder;
  11370. struct intel_connector *connector;
  11371. for_each_intel_encoder(dev, encoder) {
  11372. bool enabled = false;
  11373. enum pipe pipe;
  11374. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  11375. encoder->base.base.id,
  11376. encoder->base.name);
  11377. for_each_intel_connector(dev, connector) {
  11378. if (connector->base.state->best_encoder != &encoder->base)
  11379. continue;
  11380. enabled = true;
  11381. I915_STATE_WARN(connector->base.state->crtc !=
  11382. encoder->base.crtc,
  11383. "connector's crtc doesn't match encoder crtc\n");
  11384. }
  11385. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  11386. "encoder's enabled state mismatch "
  11387. "(expected %i, found %i)\n",
  11388. !!encoder->base.crtc, enabled);
  11389. if (!encoder->base.crtc) {
  11390. bool active;
  11391. active = encoder->get_hw_state(encoder, &pipe);
  11392. I915_STATE_WARN(active,
  11393. "encoder detached but still enabled on pipe %c.\n",
  11394. pipe_name(pipe));
  11395. }
  11396. }
  11397. }
  11398. static void
  11399. verify_crtc_state(struct drm_crtc *crtc,
  11400. struct drm_crtc_state *old_crtc_state,
  11401. struct drm_crtc_state *new_crtc_state)
  11402. {
  11403. struct drm_device *dev = crtc->dev;
  11404. struct drm_i915_private *dev_priv = to_i915(dev);
  11405. struct intel_encoder *encoder;
  11406. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11407. struct intel_crtc_state *pipe_config, *sw_config;
  11408. struct drm_atomic_state *old_state;
  11409. bool active;
  11410. old_state = old_crtc_state->state;
  11411. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  11412. pipe_config = to_intel_crtc_state(old_crtc_state);
  11413. memset(pipe_config, 0, sizeof(*pipe_config));
  11414. pipe_config->base.crtc = crtc;
  11415. pipe_config->base.state = old_state;
  11416. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  11417. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  11418. /* hw state is inconsistent with the pipe quirk */
  11419. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  11420. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  11421. active = new_crtc_state->active;
  11422. I915_STATE_WARN(new_crtc_state->active != active,
  11423. "crtc active state doesn't match with hw state "
  11424. "(expected %i, found %i)\n", new_crtc_state->active, active);
  11425. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  11426. "transitional active state does not match atomic hw state "
  11427. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  11428. for_each_encoder_on_crtc(dev, crtc, encoder) {
  11429. enum pipe pipe;
  11430. active = encoder->get_hw_state(encoder, &pipe);
  11431. I915_STATE_WARN(active != new_crtc_state->active,
  11432. "[ENCODER:%i] active %i with crtc active %i\n",
  11433. encoder->base.base.id, active, new_crtc_state->active);
  11434. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  11435. "Encoder connected to wrong pipe %c\n",
  11436. pipe_name(pipe));
  11437. if (active) {
  11438. pipe_config->output_types |= 1 << encoder->type;
  11439. encoder->get_config(encoder, pipe_config);
  11440. }
  11441. }
  11442. if (!new_crtc_state->active)
  11443. return;
  11444. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  11445. sw_config = to_intel_crtc_state(crtc->state);
  11446. if (!intel_pipe_config_compare(dev_priv, sw_config,
  11447. pipe_config, false)) {
  11448. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  11449. intel_dump_pipe_config(intel_crtc, pipe_config,
  11450. "[hw state]");
  11451. intel_dump_pipe_config(intel_crtc, sw_config,
  11452. "[sw state]");
  11453. }
  11454. }
  11455. static void
  11456. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  11457. struct intel_shared_dpll *pll,
  11458. struct drm_crtc *crtc,
  11459. struct drm_crtc_state *new_state)
  11460. {
  11461. struct intel_dpll_hw_state dpll_hw_state;
  11462. unsigned crtc_mask;
  11463. bool active;
  11464. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  11465. DRM_DEBUG_KMS("%s\n", pll->name);
  11466. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  11467. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  11468. I915_STATE_WARN(!pll->on && pll->active_mask,
  11469. "pll in active use but not on in sw tracking\n");
  11470. I915_STATE_WARN(pll->on && !pll->active_mask,
  11471. "pll is on but not used by any active crtc\n");
  11472. I915_STATE_WARN(pll->on != active,
  11473. "pll on state mismatch (expected %i, found %i)\n",
  11474. pll->on, active);
  11475. }
  11476. if (!crtc) {
  11477. I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
  11478. "more active pll users than references: %x vs %x\n",
  11479. pll->active_mask, pll->config.crtc_mask);
  11480. return;
  11481. }
  11482. crtc_mask = 1 << drm_crtc_index(crtc);
  11483. if (new_state->active)
  11484. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  11485. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  11486. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  11487. else
  11488. I915_STATE_WARN(pll->active_mask & crtc_mask,
  11489. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  11490. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  11491. I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
  11492. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  11493. crtc_mask, pll->config.crtc_mask);
  11494. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
  11495. &dpll_hw_state,
  11496. sizeof(dpll_hw_state)),
  11497. "pll hw state mismatch\n");
  11498. }
  11499. static void
  11500. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  11501. struct drm_crtc_state *old_crtc_state,
  11502. struct drm_crtc_state *new_crtc_state)
  11503. {
  11504. struct drm_i915_private *dev_priv = to_i915(dev);
  11505. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  11506. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  11507. if (new_state->shared_dpll)
  11508. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  11509. if (old_state->shared_dpll &&
  11510. old_state->shared_dpll != new_state->shared_dpll) {
  11511. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  11512. struct intel_shared_dpll *pll = old_state->shared_dpll;
  11513. I915_STATE_WARN(pll->active_mask & crtc_mask,
  11514. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  11515. pipe_name(drm_crtc_index(crtc)));
  11516. I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
  11517. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  11518. pipe_name(drm_crtc_index(crtc)));
  11519. }
  11520. }
  11521. static void
  11522. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  11523. struct drm_atomic_state *state,
  11524. struct drm_crtc_state *old_state,
  11525. struct drm_crtc_state *new_state)
  11526. {
  11527. if (!needs_modeset(new_state) &&
  11528. !to_intel_crtc_state(new_state)->update_pipe)
  11529. return;
  11530. verify_wm_state(crtc, new_state);
  11531. verify_connector_state(crtc->dev, state, crtc);
  11532. verify_crtc_state(crtc, old_state, new_state);
  11533. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  11534. }
  11535. static void
  11536. verify_disabled_dpll_state(struct drm_device *dev)
  11537. {
  11538. struct drm_i915_private *dev_priv = to_i915(dev);
  11539. int i;
  11540. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  11541. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  11542. }
  11543. static void
  11544. intel_modeset_verify_disabled(struct drm_device *dev,
  11545. struct drm_atomic_state *state)
  11546. {
  11547. verify_encoder_state(dev);
  11548. verify_connector_state(dev, state, NULL);
  11549. verify_disabled_dpll_state(dev);
  11550. }
  11551. static void update_scanline_offset(struct intel_crtc *crtc)
  11552. {
  11553. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  11554. /*
  11555. * The scanline counter increments at the leading edge of hsync.
  11556. *
  11557. * On most platforms it starts counting from vtotal-1 on the
  11558. * first active line. That means the scanline counter value is
  11559. * always one less than what we would expect. Ie. just after
  11560. * start of vblank, which also occurs at start of hsync (on the
  11561. * last active line), the scanline counter will read vblank_start-1.
  11562. *
  11563. * On gen2 the scanline counter starts counting from 1 instead
  11564. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  11565. * to keep the value positive), instead of adding one.
  11566. *
  11567. * On HSW+ the behaviour of the scanline counter depends on the output
  11568. * type. For DP ports it behaves like most other platforms, but on HDMI
  11569. * there's an extra 1 line difference. So we need to add two instead of
  11570. * one to the value.
  11571. */
  11572. if (IS_GEN2(dev_priv)) {
  11573. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  11574. int vtotal;
  11575. vtotal = adjusted_mode->crtc_vtotal;
  11576. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  11577. vtotal /= 2;
  11578. crtc->scanline_offset = vtotal - 1;
  11579. } else if (HAS_DDI(dev_priv) &&
  11580. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
  11581. crtc->scanline_offset = 2;
  11582. } else
  11583. crtc->scanline_offset = 1;
  11584. }
  11585. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  11586. {
  11587. struct drm_device *dev = state->dev;
  11588. struct drm_i915_private *dev_priv = to_i915(dev);
  11589. struct intel_shared_dpll_config *shared_dpll = NULL;
  11590. struct drm_crtc *crtc;
  11591. struct drm_crtc_state *crtc_state;
  11592. int i;
  11593. if (!dev_priv->display.crtc_compute_clock)
  11594. return;
  11595. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11596. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11597. struct intel_shared_dpll *old_dpll =
  11598. to_intel_crtc_state(crtc->state)->shared_dpll;
  11599. if (!needs_modeset(crtc_state))
  11600. continue;
  11601. to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
  11602. if (!old_dpll)
  11603. continue;
  11604. if (!shared_dpll)
  11605. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  11606. intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
  11607. }
  11608. }
  11609. /*
  11610. * This implements the workaround described in the "notes" section of the mode
  11611. * set sequence documentation. When going from no pipes or single pipe to
  11612. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  11613. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  11614. */
  11615. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  11616. {
  11617. struct drm_crtc_state *crtc_state;
  11618. struct intel_crtc *intel_crtc;
  11619. struct drm_crtc *crtc;
  11620. struct intel_crtc_state *first_crtc_state = NULL;
  11621. struct intel_crtc_state *other_crtc_state = NULL;
  11622. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  11623. int i;
  11624. /* look at all crtc's that are going to be enabled in during modeset */
  11625. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11626. intel_crtc = to_intel_crtc(crtc);
  11627. if (!crtc_state->active || !needs_modeset(crtc_state))
  11628. continue;
  11629. if (first_crtc_state) {
  11630. other_crtc_state = to_intel_crtc_state(crtc_state);
  11631. break;
  11632. } else {
  11633. first_crtc_state = to_intel_crtc_state(crtc_state);
  11634. first_pipe = intel_crtc->pipe;
  11635. }
  11636. }
  11637. /* No workaround needed? */
  11638. if (!first_crtc_state)
  11639. return 0;
  11640. /* w/a possibly needed, check how many crtc's are already enabled. */
  11641. for_each_intel_crtc(state->dev, intel_crtc) {
  11642. struct intel_crtc_state *pipe_config;
  11643. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  11644. if (IS_ERR(pipe_config))
  11645. return PTR_ERR(pipe_config);
  11646. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  11647. if (!pipe_config->base.active ||
  11648. needs_modeset(&pipe_config->base))
  11649. continue;
  11650. /* 2 or more enabled crtcs means no need for w/a */
  11651. if (enabled_pipe != INVALID_PIPE)
  11652. return 0;
  11653. enabled_pipe = intel_crtc->pipe;
  11654. }
  11655. if (enabled_pipe != INVALID_PIPE)
  11656. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  11657. else if (other_crtc_state)
  11658. other_crtc_state->hsw_workaround_pipe = first_pipe;
  11659. return 0;
  11660. }
  11661. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  11662. {
  11663. struct drm_crtc *crtc;
  11664. struct drm_crtc_state *crtc_state;
  11665. int ret = 0;
  11666. /* add all active pipes to the state */
  11667. for_each_crtc(state->dev, crtc) {
  11668. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11669. if (IS_ERR(crtc_state))
  11670. return PTR_ERR(crtc_state);
  11671. if (!crtc_state->active || needs_modeset(crtc_state))
  11672. continue;
  11673. crtc_state->mode_changed = true;
  11674. ret = drm_atomic_add_affected_connectors(state, crtc);
  11675. if (ret)
  11676. break;
  11677. ret = drm_atomic_add_affected_planes(state, crtc);
  11678. if (ret)
  11679. break;
  11680. }
  11681. return ret;
  11682. }
  11683. static int intel_modeset_checks(struct drm_atomic_state *state)
  11684. {
  11685. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11686. struct drm_i915_private *dev_priv = to_i915(state->dev);
  11687. struct drm_crtc *crtc;
  11688. struct drm_crtc_state *crtc_state;
  11689. int ret = 0, i;
  11690. if (!check_digital_port_conflicts(state)) {
  11691. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  11692. return -EINVAL;
  11693. }
  11694. intel_state->modeset = true;
  11695. intel_state->active_crtcs = dev_priv->active_crtcs;
  11696. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11697. if (crtc_state->active)
  11698. intel_state->active_crtcs |= 1 << i;
  11699. else
  11700. intel_state->active_crtcs &= ~(1 << i);
  11701. if (crtc_state->active != crtc->state->active)
  11702. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  11703. }
  11704. /*
  11705. * See if the config requires any additional preparation, e.g.
  11706. * to adjust global state with pipes off. We need to do this
  11707. * here so we can get the modeset_pipe updated config for the new
  11708. * mode set on this crtc. For other crtcs we need to use the
  11709. * adjusted_mode bits in the crtc directly.
  11710. */
  11711. if (dev_priv->display.modeset_calc_cdclk) {
  11712. if (!intel_state->cdclk_pll_vco)
  11713. intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
  11714. if (!intel_state->cdclk_pll_vco)
  11715. intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
  11716. ret = dev_priv->display.modeset_calc_cdclk(state);
  11717. if (ret < 0)
  11718. return ret;
  11719. if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
  11720. intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
  11721. ret = intel_modeset_all_pipes(state);
  11722. if (ret < 0)
  11723. return ret;
  11724. DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
  11725. intel_state->cdclk, intel_state->dev_cdclk);
  11726. } else {
  11727. to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
  11728. }
  11729. intel_modeset_clear_plls(state);
  11730. if (IS_HASWELL(dev_priv))
  11731. return haswell_mode_set_planes_workaround(state);
  11732. return 0;
  11733. }
  11734. /*
  11735. * Handle calculation of various watermark data at the end of the atomic check
  11736. * phase. The code here should be run after the per-crtc and per-plane 'check'
  11737. * handlers to ensure that all derived state has been updated.
  11738. */
  11739. static int calc_watermark_data(struct drm_atomic_state *state)
  11740. {
  11741. struct drm_device *dev = state->dev;
  11742. struct drm_i915_private *dev_priv = to_i915(dev);
  11743. /* Is there platform-specific watermark information to calculate? */
  11744. if (dev_priv->display.compute_global_watermarks)
  11745. return dev_priv->display.compute_global_watermarks(state);
  11746. return 0;
  11747. }
  11748. /**
  11749. * intel_atomic_check - validate state object
  11750. * @dev: drm device
  11751. * @state: state to validate
  11752. */
  11753. static int intel_atomic_check(struct drm_device *dev,
  11754. struct drm_atomic_state *state)
  11755. {
  11756. struct drm_i915_private *dev_priv = to_i915(dev);
  11757. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11758. struct drm_crtc *crtc;
  11759. struct drm_crtc_state *crtc_state;
  11760. int ret, i;
  11761. bool any_ms = false;
  11762. ret = drm_atomic_helper_check_modeset(dev, state);
  11763. if (ret)
  11764. return ret;
  11765. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11766. struct intel_crtc_state *pipe_config =
  11767. to_intel_crtc_state(crtc_state);
  11768. /* Catch I915_MODE_FLAG_INHERITED */
  11769. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  11770. crtc_state->mode_changed = true;
  11771. if (!needs_modeset(crtc_state))
  11772. continue;
  11773. if (!crtc_state->enable) {
  11774. any_ms = true;
  11775. continue;
  11776. }
  11777. /* FIXME: For only active_changed we shouldn't need to do any
  11778. * state recomputation at all. */
  11779. ret = drm_atomic_add_affected_connectors(state, crtc);
  11780. if (ret)
  11781. return ret;
  11782. ret = intel_modeset_pipe_config(crtc, pipe_config);
  11783. if (ret) {
  11784. intel_dump_pipe_config(to_intel_crtc(crtc),
  11785. pipe_config, "[failed]");
  11786. return ret;
  11787. }
  11788. if (i915.fastboot &&
  11789. intel_pipe_config_compare(dev_priv,
  11790. to_intel_crtc_state(crtc->state),
  11791. pipe_config, true)) {
  11792. crtc_state->mode_changed = false;
  11793. to_intel_crtc_state(crtc_state)->update_pipe = true;
  11794. }
  11795. if (needs_modeset(crtc_state))
  11796. any_ms = true;
  11797. ret = drm_atomic_add_affected_planes(state, crtc);
  11798. if (ret)
  11799. return ret;
  11800. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  11801. needs_modeset(crtc_state) ?
  11802. "[modeset]" : "[fastset]");
  11803. }
  11804. if (any_ms) {
  11805. ret = intel_modeset_checks(state);
  11806. if (ret)
  11807. return ret;
  11808. } else {
  11809. intel_state->cdclk = dev_priv->atomic_cdclk_freq;
  11810. }
  11811. ret = drm_atomic_helper_check_planes(dev, state);
  11812. if (ret)
  11813. return ret;
  11814. intel_fbc_choose_crtc(dev_priv, state);
  11815. return calc_watermark_data(state);
  11816. }
  11817. static int intel_atomic_prepare_commit(struct drm_device *dev,
  11818. struct drm_atomic_state *state)
  11819. {
  11820. struct drm_i915_private *dev_priv = to_i915(dev);
  11821. struct drm_crtc_state *crtc_state;
  11822. struct drm_crtc *crtc;
  11823. int i, ret;
  11824. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11825. if (state->legacy_cursor_update)
  11826. continue;
  11827. ret = intel_crtc_wait_for_pending_flips(crtc);
  11828. if (ret)
  11829. return ret;
  11830. if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
  11831. flush_workqueue(dev_priv->wq);
  11832. }
  11833. ret = mutex_lock_interruptible(&dev->struct_mutex);
  11834. if (ret)
  11835. return ret;
  11836. ret = drm_atomic_helper_prepare_planes(dev, state);
  11837. mutex_unlock(&dev->struct_mutex);
  11838. return ret;
  11839. }
  11840. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  11841. {
  11842. struct drm_device *dev = crtc->base.dev;
  11843. if (!dev->max_vblank_count)
  11844. return drm_accurate_vblank_count(&crtc->base);
  11845. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  11846. }
  11847. static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
  11848. struct drm_i915_private *dev_priv,
  11849. unsigned crtc_mask)
  11850. {
  11851. unsigned last_vblank_count[I915_MAX_PIPES];
  11852. enum pipe pipe;
  11853. int ret;
  11854. if (!crtc_mask)
  11855. return;
  11856. for_each_pipe(dev_priv, pipe) {
  11857. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  11858. pipe);
  11859. if (!((1 << pipe) & crtc_mask))
  11860. continue;
  11861. ret = drm_crtc_vblank_get(&crtc->base);
  11862. if (WARN_ON(ret != 0)) {
  11863. crtc_mask &= ~(1 << pipe);
  11864. continue;
  11865. }
  11866. last_vblank_count[pipe] = drm_crtc_vblank_count(&crtc->base);
  11867. }
  11868. for_each_pipe(dev_priv, pipe) {
  11869. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  11870. pipe);
  11871. long lret;
  11872. if (!((1 << pipe) & crtc_mask))
  11873. continue;
  11874. lret = wait_event_timeout(dev->vblank[pipe].queue,
  11875. last_vblank_count[pipe] !=
  11876. drm_crtc_vblank_count(&crtc->base),
  11877. msecs_to_jiffies(50));
  11878. WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
  11879. drm_crtc_vblank_put(&crtc->base);
  11880. }
  11881. }
  11882. static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
  11883. {
  11884. /* fb updated, need to unpin old fb */
  11885. if (crtc_state->fb_changed)
  11886. return true;
  11887. /* wm changes, need vblank before final wm's */
  11888. if (crtc_state->update_wm_post)
  11889. return true;
  11890. /*
  11891. * cxsr is re-enabled after vblank.
  11892. * This is already handled by crtc_state->update_wm_post,
  11893. * but added for clarity.
  11894. */
  11895. if (crtc_state->disable_cxsr)
  11896. return true;
  11897. return false;
  11898. }
  11899. static void intel_update_crtc(struct drm_crtc *crtc,
  11900. struct drm_atomic_state *state,
  11901. struct drm_crtc_state *old_crtc_state,
  11902. unsigned int *crtc_vblank_mask)
  11903. {
  11904. struct drm_device *dev = crtc->dev;
  11905. struct drm_i915_private *dev_priv = to_i915(dev);
  11906. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11907. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc->state);
  11908. bool modeset = needs_modeset(crtc->state);
  11909. if (modeset) {
  11910. update_scanline_offset(intel_crtc);
  11911. dev_priv->display.crtc_enable(pipe_config, state);
  11912. } else {
  11913. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  11914. }
  11915. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  11916. intel_fbc_enable(
  11917. intel_crtc, pipe_config,
  11918. to_intel_plane_state(crtc->primary->state));
  11919. }
  11920. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  11921. if (needs_vblank_wait(pipe_config))
  11922. *crtc_vblank_mask |= drm_crtc_mask(crtc);
  11923. }
  11924. static void intel_update_crtcs(struct drm_atomic_state *state,
  11925. unsigned int *crtc_vblank_mask)
  11926. {
  11927. struct drm_crtc *crtc;
  11928. struct drm_crtc_state *old_crtc_state;
  11929. int i;
  11930. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11931. if (!crtc->state->active)
  11932. continue;
  11933. intel_update_crtc(crtc, state, old_crtc_state,
  11934. crtc_vblank_mask);
  11935. }
  11936. }
  11937. static void skl_update_crtcs(struct drm_atomic_state *state,
  11938. unsigned int *crtc_vblank_mask)
  11939. {
  11940. struct drm_i915_private *dev_priv = to_i915(state->dev);
  11941. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11942. struct drm_crtc *crtc;
  11943. struct intel_crtc *intel_crtc;
  11944. struct drm_crtc_state *old_crtc_state;
  11945. struct intel_crtc_state *cstate;
  11946. unsigned int updated = 0;
  11947. bool progress;
  11948. enum pipe pipe;
  11949. int i;
  11950. const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
  11951. for_each_crtc_in_state(state, crtc, old_crtc_state, i)
  11952. /* ignore allocations for crtc's that have been turned off. */
  11953. if (crtc->state->active)
  11954. entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
  11955. /*
  11956. * Whenever the number of active pipes changes, we need to make sure we
  11957. * update the pipes in the right order so that their ddb allocations
  11958. * never overlap with eachother inbetween CRTC updates. Otherwise we'll
  11959. * cause pipe underruns and other bad stuff.
  11960. */
  11961. do {
  11962. progress = false;
  11963. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11964. bool vbl_wait = false;
  11965. unsigned int cmask = drm_crtc_mask(crtc);
  11966. intel_crtc = to_intel_crtc(crtc);
  11967. cstate = to_intel_crtc_state(crtc->state);
  11968. pipe = intel_crtc->pipe;
  11969. if (updated & cmask || !cstate->base.active)
  11970. continue;
  11971. if (skl_ddb_allocation_overlaps(entries, &cstate->wm.skl.ddb, i))
  11972. continue;
  11973. updated |= cmask;
  11974. entries[i] = &cstate->wm.skl.ddb;
  11975. /*
  11976. * If this is an already active pipe, it's DDB changed,
  11977. * and this isn't the last pipe that needs updating
  11978. * then we need to wait for a vblank to pass for the
  11979. * new ddb allocation to take effect.
  11980. */
  11981. if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
  11982. &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
  11983. !crtc->state->active_changed &&
  11984. intel_state->wm_results.dirty_pipes != updated)
  11985. vbl_wait = true;
  11986. intel_update_crtc(crtc, state, old_crtc_state,
  11987. crtc_vblank_mask);
  11988. if (vbl_wait)
  11989. intel_wait_for_vblank(dev_priv, pipe);
  11990. progress = true;
  11991. }
  11992. } while (progress);
  11993. }
  11994. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  11995. {
  11996. struct drm_device *dev = state->dev;
  11997. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11998. struct drm_i915_private *dev_priv = to_i915(dev);
  11999. struct drm_crtc_state *old_crtc_state;
  12000. struct drm_crtc *crtc;
  12001. struct intel_crtc_state *intel_cstate;
  12002. bool hw_check = intel_state->modeset;
  12003. unsigned long put_domains[I915_MAX_PIPES] = {};
  12004. unsigned crtc_vblank_mask = 0;
  12005. int i;
  12006. drm_atomic_helper_wait_for_dependencies(state);
  12007. if (intel_state->modeset)
  12008. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  12009. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12010. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12011. if (needs_modeset(crtc->state) ||
  12012. to_intel_crtc_state(crtc->state)->update_pipe) {
  12013. hw_check = true;
  12014. put_domains[to_intel_crtc(crtc)->pipe] =
  12015. modeset_get_crtc_power_domains(crtc,
  12016. to_intel_crtc_state(crtc->state));
  12017. }
  12018. if (!needs_modeset(crtc->state))
  12019. continue;
  12020. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  12021. if (old_crtc_state->active) {
  12022. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  12023. dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
  12024. intel_crtc->active = false;
  12025. intel_fbc_disable(intel_crtc);
  12026. intel_disable_shared_dpll(intel_crtc);
  12027. /*
  12028. * Underruns don't always raise
  12029. * interrupts, so check manually.
  12030. */
  12031. intel_check_cpu_fifo_underruns(dev_priv);
  12032. intel_check_pch_fifo_underruns(dev_priv);
  12033. if (!crtc->state->active) {
  12034. /*
  12035. * Make sure we don't call initial_watermarks
  12036. * for ILK-style watermark updates.
  12037. */
  12038. if (dev_priv->display.atomic_update_watermarks)
  12039. dev_priv->display.initial_watermarks(intel_state,
  12040. to_intel_crtc_state(crtc->state));
  12041. else
  12042. intel_update_watermarks(intel_crtc);
  12043. }
  12044. }
  12045. }
  12046. /* Only after disabling all output pipelines that will be changed can we
  12047. * update the the output configuration. */
  12048. intel_modeset_update_crtc_state(state);
  12049. if (intel_state->modeset) {
  12050. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  12051. if (dev_priv->display.modeset_commit_cdclk &&
  12052. (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
  12053. intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
  12054. dev_priv->display.modeset_commit_cdclk(state);
  12055. /*
  12056. * SKL workaround: bspec recommends we disable the SAGV when we
  12057. * have more then one pipe enabled
  12058. */
  12059. if (!intel_can_enable_sagv(state))
  12060. intel_disable_sagv(dev_priv);
  12061. intel_modeset_verify_disabled(dev, state);
  12062. }
  12063. /* Complete the events for pipes that have now been disabled */
  12064. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12065. bool modeset = needs_modeset(crtc->state);
  12066. /* Complete events for now disable pipes here. */
  12067. if (modeset && !crtc->state->active && crtc->state->event) {
  12068. spin_lock_irq(&dev->event_lock);
  12069. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  12070. spin_unlock_irq(&dev->event_lock);
  12071. crtc->state->event = NULL;
  12072. }
  12073. }
  12074. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  12075. dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
  12076. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  12077. * already, but still need the state for the delayed optimization. To
  12078. * fix this:
  12079. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  12080. * - schedule that vblank worker _before_ calling hw_done
  12081. * - at the start of commit_tail, cancel it _synchrously
  12082. * - switch over to the vblank wait helper in the core after that since
  12083. * we don't need out special handling any more.
  12084. */
  12085. if (!state->legacy_cursor_update)
  12086. intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
  12087. /*
  12088. * Now that the vblank has passed, we can go ahead and program the
  12089. * optimal watermarks on platforms that need two-step watermark
  12090. * programming.
  12091. *
  12092. * TODO: Move this (and other cleanup) to an async worker eventually.
  12093. */
  12094. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12095. intel_cstate = to_intel_crtc_state(crtc->state);
  12096. if (dev_priv->display.optimize_watermarks)
  12097. dev_priv->display.optimize_watermarks(intel_state,
  12098. intel_cstate);
  12099. }
  12100. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12101. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  12102. if (put_domains[i])
  12103. modeset_put_power_domains(dev_priv, put_domains[i]);
  12104. intel_modeset_verify_crtc(crtc, state, old_crtc_state, crtc->state);
  12105. }
  12106. if (intel_state->modeset && intel_can_enable_sagv(state))
  12107. intel_enable_sagv(dev_priv);
  12108. drm_atomic_helper_commit_hw_done(state);
  12109. if (intel_state->modeset)
  12110. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  12111. mutex_lock(&dev->struct_mutex);
  12112. drm_atomic_helper_cleanup_planes(dev, state);
  12113. mutex_unlock(&dev->struct_mutex);
  12114. drm_atomic_helper_commit_cleanup_done(state);
  12115. drm_atomic_state_put(state);
  12116. /* As one of the primary mmio accessors, KMS has a high likelihood
  12117. * of triggering bugs in unclaimed access. After we finish
  12118. * modesetting, see if an error has been flagged, and if so
  12119. * enable debugging for the next modeset - and hope we catch
  12120. * the culprit.
  12121. *
  12122. * XXX note that we assume display power is on at this point.
  12123. * This might hold true now but we need to add pm helper to check
  12124. * unclaimed only when the hardware is on, as atomic commits
  12125. * can happen also when the device is completely off.
  12126. */
  12127. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  12128. }
  12129. static void intel_atomic_commit_work(struct work_struct *work)
  12130. {
  12131. struct drm_atomic_state *state =
  12132. container_of(work, struct drm_atomic_state, commit_work);
  12133. intel_atomic_commit_tail(state);
  12134. }
  12135. static int __i915_sw_fence_call
  12136. intel_atomic_commit_ready(struct i915_sw_fence *fence,
  12137. enum i915_sw_fence_notify notify)
  12138. {
  12139. struct intel_atomic_state *state =
  12140. container_of(fence, struct intel_atomic_state, commit_ready);
  12141. switch (notify) {
  12142. case FENCE_COMPLETE:
  12143. if (state->base.commit_work.func)
  12144. queue_work(system_unbound_wq, &state->base.commit_work);
  12145. break;
  12146. case FENCE_FREE:
  12147. {
  12148. struct intel_atomic_helper *helper =
  12149. &to_i915(state->base.dev)->atomic_helper;
  12150. if (llist_add(&state->freed, &helper->free_list))
  12151. schedule_work(&helper->free_work);
  12152. break;
  12153. }
  12154. }
  12155. return NOTIFY_DONE;
  12156. }
  12157. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  12158. {
  12159. struct drm_plane_state *old_plane_state;
  12160. struct drm_plane *plane;
  12161. int i;
  12162. for_each_plane_in_state(state, plane, old_plane_state, i)
  12163. i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
  12164. intel_fb_obj(plane->state->fb),
  12165. to_intel_plane(plane)->frontbuffer_bit);
  12166. }
  12167. /**
  12168. * intel_atomic_commit - commit validated state object
  12169. * @dev: DRM device
  12170. * @state: the top-level driver state object
  12171. * @nonblock: nonblocking commit
  12172. *
  12173. * This function commits a top-level state object that has been validated
  12174. * with drm_atomic_helper_check().
  12175. *
  12176. * RETURNS
  12177. * Zero for success or -errno.
  12178. */
  12179. static int intel_atomic_commit(struct drm_device *dev,
  12180. struct drm_atomic_state *state,
  12181. bool nonblock)
  12182. {
  12183. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  12184. struct drm_i915_private *dev_priv = to_i915(dev);
  12185. int ret = 0;
  12186. ret = drm_atomic_helper_setup_commit(state, nonblock);
  12187. if (ret)
  12188. return ret;
  12189. drm_atomic_state_get(state);
  12190. i915_sw_fence_init(&intel_state->commit_ready,
  12191. intel_atomic_commit_ready);
  12192. ret = intel_atomic_prepare_commit(dev, state);
  12193. if (ret) {
  12194. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  12195. i915_sw_fence_commit(&intel_state->commit_ready);
  12196. return ret;
  12197. }
  12198. drm_atomic_helper_swap_state(state, true);
  12199. dev_priv->wm.distrust_bios_wm = false;
  12200. intel_shared_dpll_commit(state);
  12201. intel_atomic_track_fbs(state);
  12202. if (intel_state->modeset) {
  12203. memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
  12204. sizeof(intel_state->min_pixclk));
  12205. dev_priv->active_crtcs = intel_state->active_crtcs;
  12206. dev_priv->atomic_cdclk_freq = intel_state->cdclk;
  12207. }
  12208. drm_atomic_state_get(state);
  12209. INIT_WORK(&state->commit_work,
  12210. nonblock ? intel_atomic_commit_work : NULL);
  12211. i915_sw_fence_commit(&intel_state->commit_ready);
  12212. if (!nonblock) {
  12213. i915_sw_fence_wait(&intel_state->commit_ready);
  12214. intel_atomic_commit_tail(state);
  12215. }
  12216. return 0;
  12217. }
  12218. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  12219. {
  12220. struct drm_device *dev = crtc->dev;
  12221. struct drm_atomic_state *state;
  12222. struct drm_crtc_state *crtc_state;
  12223. int ret;
  12224. state = drm_atomic_state_alloc(dev);
  12225. if (!state) {
  12226. DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
  12227. crtc->base.id, crtc->name);
  12228. return;
  12229. }
  12230. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  12231. retry:
  12232. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  12233. ret = PTR_ERR_OR_ZERO(crtc_state);
  12234. if (!ret) {
  12235. if (!crtc_state->active)
  12236. goto out;
  12237. crtc_state->mode_changed = true;
  12238. ret = drm_atomic_commit(state);
  12239. }
  12240. if (ret == -EDEADLK) {
  12241. drm_atomic_state_clear(state);
  12242. drm_modeset_backoff(state->acquire_ctx);
  12243. goto retry;
  12244. }
  12245. out:
  12246. drm_atomic_state_put(state);
  12247. }
  12248. /*
  12249. * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
  12250. * drm_atomic_helper_legacy_gamma_set() directly.
  12251. */
  12252. static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
  12253. u16 *red, u16 *green, u16 *blue,
  12254. uint32_t size)
  12255. {
  12256. struct drm_device *dev = crtc->dev;
  12257. struct drm_mode_config *config = &dev->mode_config;
  12258. struct drm_crtc_state *state;
  12259. int ret;
  12260. ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
  12261. if (ret)
  12262. return ret;
  12263. /*
  12264. * Make sure we update the legacy properties so this works when
  12265. * atomic is not enabled.
  12266. */
  12267. state = crtc->state;
  12268. drm_object_property_set_value(&crtc->base,
  12269. config->degamma_lut_property,
  12270. (state->degamma_lut) ?
  12271. state->degamma_lut->base.id : 0);
  12272. drm_object_property_set_value(&crtc->base,
  12273. config->ctm_property,
  12274. (state->ctm) ?
  12275. state->ctm->base.id : 0);
  12276. drm_object_property_set_value(&crtc->base,
  12277. config->gamma_lut_property,
  12278. (state->gamma_lut) ?
  12279. state->gamma_lut->base.id : 0);
  12280. return 0;
  12281. }
  12282. static const struct drm_crtc_funcs intel_crtc_funcs = {
  12283. .gamma_set = intel_atomic_legacy_gamma_set,
  12284. .set_config = drm_atomic_helper_set_config,
  12285. .set_property = drm_atomic_helper_crtc_set_property,
  12286. .destroy = intel_crtc_destroy,
  12287. .page_flip = intel_crtc_page_flip,
  12288. .atomic_duplicate_state = intel_crtc_duplicate_state,
  12289. .atomic_destroy_state = intel_crtc_destroy_state,
  12290. };
  12291. /**
  12292. * intel_prepare_plane_fb - Prepare fb for usage on plane
  12293. * @plane: drm plane to prepare for
  12294. * @fb: framebuffer to prepare for presentation
  12295. *
  12296. * Prepares a framebuffer for usage on a display plane. Generally this
  12297. * involves pinning the underlying object and updating the frontbuffer tracking
  12298. * bits. Some older platforms need special physical address handling for
  12299. * cursor planes.
  12300. *
  12301. * Must be called with struct_mutex held.
  12302. *
  12303. * Returns 0 on success, negative error code on failure.
  12304. */
  12305. int
  12306. intel_prepare_plane_fb(struct drm_plane *plane,
  12307. struct drm_plane_state *new_state)
  12308. {
  12309. struct intel_atomic_state *intel_state =
  12310. to_intel_atomic_state(new_state->state);
  12311. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  12312. struct drm_framebuffer *fb = new_state->fb;
  12313. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  12314. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  12315. int ret;
  12316. if (!obj && !old_obj)
  12317. return 0;
  12318. if (old_obj) {
  12319. struct drm_crtc_state *crtc_state =
  12320. drm_atomic_get_existing_crtc_state(new_state->state,
  12321. plane->state->crtc);
  12322. /* Big Hammer, we also need to ensure that any pending
  12323. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  12324. * current scanout is retired before unpinning the old
  12325. * framebuffer. Note that we rely on userspace rendering
  12326. * into the buffer attached to the pipe they are waiting
  12327. * on. If not, userspace generates a GPU hang with IPEHR
  12328. * point to the MI_WAIT_FOR_EVENT.
  12329. *
  12330. * This should only fail upon a hung GPU, in which case we
  12331. * can safely continue.
  12332. */
  12333. if (needs_modeset(crtc_state)) {
  12334. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  12335. old_obj->resv, NULL,
  12336. false, 0,
  12337. GFP_KERNEL);
  12338. if (ret < 0)
  12339. return ret;
  12340. }
  12341. }
  12342. if (new_state->fence) { /* explicit fencing */
  12343. ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
  12344. new_state->fence,
  12345. I915_FENCE_TIMEOUT,
  12346. GFP_KERNEL);
  12347. if (ret < 0)
  12348. return ret;
  12349. }
  12350. if (!obj)
  12351. return 0;
  12352. if (!new_state->fence) { /* implicit fencing */
  12353. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  12354. obj->resv, NULL,
  12355. false, I915_FENCE_TIMEOUT,
  12356. GFP_KERNEL);
  12357. if (ret < 0)
  12358. return ret;
  12359. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  12360. }
  12361. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  12362. INTEL_INFO(dev_priv)->cursor_needs_physical) {
  12363. int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
  12364. ret = i915_gem_object_attach_phys(obj, align);
  12365. if (ret) {
  12366. DRM_DEBUG_KMS("failed to attach phys object\n");
  12367. return ret;
  12368. }
  12369. } else {
  12370. struct i915_vma *vma;
  12371. vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  12372. if (IS_ERR(vma)) {
  12373. DRM_DEBUG_KMS("failed to pin object\n");
  12374. return PTR_ERR(vma);
  12375. }
  12376. }
  12377. return 0;
  12378. }
  12379. /**
  12380. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  12381. * @plane: drm plane to clean up for
  12382. * @fb: old framebuffer that was on plane
  12383. *
  12384. * Cleans up a framebuffer that has just been removed from a plane.
  12385. *
  12386. * Must be called with struct_mutex held.
  12387. */
  12388. void
  12389. intel_cleanup_plane_fb(struct drm_plane *plane,
  12390. struct drm_plane_state *old_state)
  12391. {
  12392. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  12393. struct intel_plane_state *old_intel_state;
  12394. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
  12395. struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
  12396. old_intel_state = to_intel_plane_state(old_state);
  12397. if (!obj && !old_obj)
  12398. return;
  12399. if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
  12400. !INTEL_INFO(dev_priv)->cursor_needs_physical))
  12401. intel_unpin_fb_obj(old_state->fb, old_state->rotation);
  12402. }
  12403. int
  12404. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  12405. {
  12406. int max_scale;
  12407. int crtc_clock, cdclk;
  12408. if (!intel_crtc || !crtc_state->base.enable)
  12409. return DRM_PLANE_HELPER_NO_SCALING;
  12410. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  12411. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  12412. if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
  12413. return DRM_PLANE_HELPER_NO_SCALING;
  12414. /*
  12415. * skl max scale is lower of:
  12416. * close to 3 but not 3, -1 is for that purpose
  12417. * or
  12418. * cdclk/crtc_clock
  12419. */
  12420. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  12421. return max_scale;
  12422. }
  12423. static int
  12424. intel_check_primary_plane(struct drm_plane *plane,
  12425. struct intel_crtc_state *crtc_state,
  12426. struct intel_plane_state *state)
  12427. {
  12428. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  12429. struct drm_crtc *crtc = state->base.crtc;
  12430. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  12431. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  12432. bool can_position = false;
  12433. int ret;
  12434. if (INTEL_GEN(dev_priv) >= 9) {
  12435. /* use scaler when colorkey is not required */
  12436. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  12437. min_scale = 1;
  12438. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  12439. }
  12440. can_position = true;
  12441. }
  12442. ret = drm_plane_helper_check_state(&state->base,
  12443. &state->clip,
  12444. min_scale, max_scale,
  12445. can_position, true);
  12446. if (ret)
  12447. return ret;
  12448. if (!state->base.fb)
  12449. return 0;
  12450. if (INTEL_GEN(dev_priv) >= 9) {
  12451. ret = skl_check_plane_surface(state);
  12452. if (ret)
  12453. return ret;
  12454. }
  12455. return 0;
  12456. }
  12457. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  12458. struct drm_crtc_state *old_crtc_state)
  12459. {
  12460. struct drm_device *dev = crtc->dev;
  12461. struct drm_i915_private *dev_priv = to_i915(dev);
  12462. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12463. struct intel_crtc_state *intel_cstate =
  12464. to_intel_crtc_state(crtc->state);
  12465. struct intel_crtc_state *old_intel_cstate =
  12466. to_intel_crtc_state(old_crtc_state);
  12467. struct intel_atomic_state *old_intel_state =
  12468. to_intel_atomic_state(old_crtc_state->state);
  12469. bool modeset = needs_modeset(crtc->state);
  12470. /* Perform vblank evasion around commit operation */
  12471. intel_pipe_update_start(intel_crtc);
  12472. if (modeset)
  12473. goto out;
  12474. if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
  12475. intel_color_set_csc(crtc->state);
  12476. intel_color_load_luts(crtc->state);
  12477. }
  12478. if (intel_cstate->update_pipe)
  12479. intel_update_pipe_config(intel_crtc, old_intel_cstate);
  12480. else if (INTEL_GEN(dev_priv) >= 9)
  12481. skl_detach_scalers(intel_crtc);
  12482. out:
  12483. if (dev_priv->display.atomic_update_watermarks)
  12484. dev_priv->display.atomic_update_watermarks(old_intel_state,
  12485. intel_cstate);
  12486. }
  12487. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  12488. struct drm_crtc_state *old_crtc_state)
  12489. {
  12490. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12491. intel_pipe_update_end(intel_crtc, NULL);
  12492. }
  12493. /**
  12494. * intel_plane_destroy - destroy a plane
  12495. * @plane: plane to destroy
  12496. *
  12497. * Common destruction function for all types of planes (primary, cursor,
  12498. * sprite).
  12499. */
  12500. void intel_plane_destroy(struct drm_plane *plane)
  12501. {
  12502. drm_plane_cleanup(plane);
  12503. kfree(to_intel_plane(plane));
  12504. }
  12505. const struct drm_plane_funcs intel_plane_funcs = {
  12506. .update_plane = drm_atomic_helper_update_plane,
  12507. .disable_plane = drm_atomic_helper_disable_plane,
  12508. .destroy = intel_plane_destroy,
  12509. .set_property = drm_atomic_helper_plane_set_property,
  12510. .atomic_get_property = intel_plane_atomic_get_property,
  12511. .atomic_set_property = intel_plane_atomic_set_property,
  12512. .atomic_duplicate_state = intel_plane_duplicate_state,
  12513. .atomic_destroy_state = intel_plane_destroy_state,
  12514. };
  12515. static struct intel_plane *
  12516. intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  12517. {
  12518. struct intel_plane *primary = NULL;
  12519. struct intel_plane_state *state = NULL;
  12520. const uint32_t *intel_primary_formats;
  12521. unsigned int supported_rotations;
  12522. unsigned int num_formats;
  12523. int ret;
  12524. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  12525. if (!primary) {
  12526. ret = -ENOMEM;
  12527. goto fail;
  12528. }
  12529. state = intel_create_plane_state(&primary->base);
  12530. if (!state) {
  12531. ret = -ENOMEM;
  12532. goto fail;
  12533. }
  12534. primary->base.state = &state->base;
  12535. primary->can_scale = false;
  12536. primary->max_downscale = 1;
  12537. if (INTEL_GEN(dev_priv) >= 9) {
  12538. primary->can_scale = true;
  12539. state->scaler_id = -1;
  12540. }
  12541. primary->pipe = pipe;
  12542. /*
  12543. * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
  12544. * port is hooked to pipe B. Hence we want plane A feeding pipe B.
  12545. */
  12546. if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
  12547. primary->plane = (enum plane) !pipe;
  12548. else
  12549. primary->plane = (enum plane) pipe;
  12550. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  12551. primary->check_plane = intel_check_primary_plane;
  12552. if (INTEL_GEN(dev_priv) >= 9) {
  12553. intel_primary_formats = skl_primary_formats;
  12554. num_formats = ARRAY_SIZE(skl_primary_formats);
  12555. primary->update_plane = skylake_update_primary_plane;
  12556. primary->disable_plane = skylake_disable_primary_plane;
  12557. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12558. intel_primary_formats = i965_primary_formats;
  12559. num_formats = ARRAY_SIZE(i965_primary_formats);
  12560. primary->update_plane = ironlake_update_primary_plane;
  12561. primary->disable_plane = i9xx_disable_primary_plane;
  12562. } else if (INTEL_GEN(dev_priv) >= 4) {
  12563. intel_primary_formats = i965_primary_formats;
  12564. num_formats = ARRAY_SIZE(i965_primary_formats);
  12565. primary->update_plane = i9xx_update_primary_plane;
  12566. primary->disable_plane = i9xx_disable_primary_plane;
  12567. } else {
  12568. intel_primary_formats = i8xx_primary_formats;
  12569. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  12570. primary->update_plane = i9xx_update_primary_plane;
  12571. primary->disable_plane = i9xx_disable_primary_plane;
  12572. }
  12573. if (INTEL_GEN(dev_priv) >= 9)
  12574. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  12575. 0, &intel_plane_funcs,
  12576. intel_primary_formats, num_formats,
  12577. DRM_PLANE_TYPE_PRIMARY,
  12578. "plane 1%c", pipe_name(pipe));
  12579. else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  12580. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  12581. 0, &intel_plane_funcs,
  12582. intel_primary_formats, num_formats,
  12583. DRM_PLANE_TYPE_PRIMARY,
  12584. "primary %c", pipe_name(pipe));
  12585. else
  12586. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  12587. 0, &intel_plane_funcs,
  12588. intel_primary_formats, num_formats,
  12589. DRM_PLANE_TYPE_PRIMARY,
  12590. "plane %c", plane_name(primary->plane));
  12591. if (ret)
  12592. goto fail;
  12593. if (INTEL_GEN(dev_priv) >= 9) {
  12594. supported_rotations =
  12595. DRM_ROTATE_0 | DRM_ROTATE_90 |
  12596. DRM_ROTATE_180 | DRM_ROTATE_270;
  12597. } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  12598. supported_rotations =
  12599. DRM_ROTATE_0 | DRM_ROTATE_180 |
  12600. DRM_REFLECT_X;
  12601. } else if (INTEL_GEN(dev_priv) >= 4) {
  12602. supported_rotations =
  12603. DRM_ROTATE_0 | DRM_ROTATE_180;
  12604. } else {
  12605. supported_rotations = DRM_ROTATE_0;
  12606. }
  12607. if (INTEL_GEN(dev_priv) >= 4)
  12608. drm_plane_create_rotation_property(&primary->base,
  12609. DRM_ROTATE_0,
  12610. supported_rotations);
  12611. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  12612. return primary;
  12613. fail:
  12614. kfree(state);
  12615. kfree(primary);
  12616. return ERR_PTR(ret);
  12617. }
  12618. static int
  12619. intel_check_cursor_plane(struct drm_plane *plane,
  12620. struct intel_crtc_state *crtc_state,
  12621. struct intel_plane_state *state)
  12622. {
  12623. struct drm_framebuffer *fb = state->base.fb;
  12624. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  12625. enum pipe pipe = to_intel_plane(plane)->pipe;
  12626. unsigned stride;
  12627. int ret;
  12628. ret = drm_plane_helper_check_state(&state->base,
  12629. &state->clip,
  12630. DRM_PLANE_HELPER_NO_SCALING,
  12631. DRM_PLANE_HELPER_NO_SCALING,
  12632. true, true);
  12633. if (ret)
  12634. return ret;
  12635. /* if we want to turn off the cursor ignore width and height */
  12636. if (!obj)
  12637. return 0;
  12638. /* Check for which cursor types we support */
  12639. if (!cursor_size_ok(to_i915(plane->dev), state->base.crtc_w,
  12640. state->base.crtc_h)) {
  12641. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  12642. state->base.crtc_w, state->base.crtc_h);
  12643. return -EINVAL;
  12644. }
  12645. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  12646. if (obj->base.size < stride * state->base.crtc_h) {
  12647. DRM_DEBUG_KMS("buffer is too small\n");
  12648. return -ENOMEM;
  12649. }
  12650. if (fb->modifier != DRM_FORMAT_MOD_NONE) {
  12651. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  12652. return -EINVAL;
  12653. }
  12654. /*
  12655. * There's something wrong with the cursor on CHV pipe C.
  12656. * If it straddles the left edge of the screen then
  12657. * moving it away from the edge or disabling it often
  12658. * results in a pipe underrun, and often that can lead to
  12659. * dead pipe (constant underrun reported, and it scans
  12660. * out just a solid color). To recover from that, the
  12661. * display power well must be turned off and on again.
  12662. * Refuse the put the cursor into that compromised position.
  12663. */
  12664. if (IS_CHERRYVIEW(to_i915(plane->dev)) && pipe == PIPE_C &&
  12665. state->base.visible && state->base.crtc_x < 0) {
  12666. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  12667. return -EINVAL;
  12668. }
  12669. return 0;
  12670. }
  12671. static void
  12672. intel_disable_cursor_plane(struct drm_plane *plane,
  12673. struct drm_crtc *crtc)
  12674. {
  12675. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12676. intel_crtc->cursor_addr = 0;
  12677. intel_crtc_update_cursor(crtc, NULL);
  12678. }
  12679. static void
  12680. intel_update_cursor_plane(struct drm_plane *plane,
  12681. const struct intel_crtc_state *crtc_state,
  12682. const struct intel_plane_state *state)
  12683. {
  12684. struct drm_crtc *crtc = crtc_state->base.crtc;
  12685. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12686. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  12687. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  12688. uint32_t addr;
  12689. if (!obj)
  12690. addr = 0;
  12691. else if (!INTEL_INFO(dev_priv)->cursor_needs_physical)
  12692. addr = i915_gem_object_ggtt_offset(obj, NULL);
  12693. else
  12694. addr = obj->phys_handle->busaddr;
  12695. intel_crtc->cursor_addr = addr;
  12696. intel_crtc_update_cursor(crtc, state);
  12697. }
  12698. static struct intel_plane *
  12699. intel_cursor_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  12700. {
  12701. struct intel_plane *cursor = NULL;
  12702. struct intel_plane_state *state = NULL;
  12703. int ret;
  12704. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  12705. if (!cursor) {
  12706. ret = -ENOMEM;
  12707. goto fail;
  12708. }
  12709. state = intel_create_plane_state(&cursor->base);
  12710. if (!state) {
  12711. ret = -ENOMEM;
  12712. goto fail;
  12713. }
  12714. cursor->base.state = &state->base;
  12715. cursor->can_scale = false;
  12716. cursor->max_downscale = 1;
  12717. cursor->pipe = pipe;
  12718. cursor->plane = pipe;
  12719. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  12720. cursor->check_plane = intel_check_cursor_plane;
  12721. cursor->update_plane = intel_update_cursor_plane;
  12722. cursor->disable_plane = intel_disable_cursor_plane;
  12723. ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
  12724. 0, &intel_plane_funcs,
  12725. intel_cursor_formats,
  12726. ARRAY_SIZE(intel_cursor_formats),
  12727. DRM_PLANE_TYPE_CURSOR,
  12728. "cursor %c", pipe_name(pipe));
  12729. if (ret)
  12730. goto fail;
  12731. if (INTEL_GEN(dev_priv) >= 4)
  12732. drm_plane_create_rotation_property(&cursor->base,
  12733. DRM_ROTATE_0,
  12734. DRM_ROTATE_0 |
  12735. DRM_ROTATE_180);
  12736. if (INTEL_GEN(dev_priv) >= 9)
  12737. state->scaler_id = -1;
  12738. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  12739. return cursor;
  12740. fail:
  12741. kfree(state);
  12742. kfree(cursor);
  12743. return ERR_PTR(ret);
  12744. }
  12745. static void skl_init_scalers(struct drm_i915_private *dev_priv,
  12746. struct intel_crtc *crtc,
  12747. struct intel_crtc_state *crtc_state)
  12748. {
  12749. struct intel_crtc_scaler_state *scaler_state =
  12750. &crtc_state->scaler_state;
  12751. int i;
  12752. for (i = 0; i < crtc->num_scalers; i++) {
  12753. struct intel_scaler *scaler = &scaler_state->scalers[i];
  12754. scaler->in_use = 0;
  12755. scaler->mode = PS_SCALER_MODE_DYN;
  12756. }
  12757. scaler_state->scaler_id = -1;
  12758. }
  12759. static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
  12760. {
  12761. struct intel_crtc *intel_crtc;
  12762. struct intel_crtc_state *crtc_state = NULL;
  12763. struct intel_plane *primary = NULL;
  12764. struct intel_plane *cursor = NULL;
  12765. int sprite, ret;
  12766. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  12767. if (!intel_crtc)
  12768. return -ENOMEM;
  12769. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  12770. if (!crtc_state) {
  12771. ret = -ENOMEM;
  12772. goto fail;
  12773. }
  12774. intel_crtc->config = crtc_state;
  12775. intel_crtc->base.state = &crtc_state->base;
  12776. crtc_state->base.crtc = &intel_crtc->base;
  12777. /* initialize shared scalers */
  12778. if (INTEL_GEN(dev_priv) >= 9) {
  12779. if (pipe == PIPE_C)
  12780. intel_crtc->num_scalers = 1;
  12781. else
  12782. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  12783. skl_init_scalers(dev_priv, intel_crtc, crtc_state);
  12784. }
  12785. primary = intel_primary_plane_create(dev_priv, pipe);
  12786. if (IS_ERR(primary)) {
  12787. ret = PTR_ERR(primary);
  12788. goto fail;
  12789. }
  12790. for_each_sprite(dev_priv, pipe, sprite) {
  12791. struct intel_plane *plane;
  12792. plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
  12793. if (IS_ERR(plane)) {
  12794. ret = PTR_ERR(plane);
  12795. goto fail;
  12796. }
  12797. }
  12798. cursor = intel_cursor_plane_create(dev_priv, pipe);
  12799. if (IS_ERR(cursor)) {
  12800. ret = PTR_ERR(cursor);
  12801. goto fail;
  12802. }
  12803. ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
  12804. &primary->base, &cursor->base,
  12805. &intel_crtc_funcs,
  12806. "pipe %c", pipe_name(pipe));
  12807. if (ret)
  12808. goto fail;
  12809. intel_crtc->pipe = pipe;
  12810. intel_crtc->plane = primary->plane;
  12811. intel_crtc->cursor_base = ~0;
  12812. intel_crtc->cursor_cntl = ~0;
  12813. intel_crtc->cursor_size = ~0;
  12814. intel_crtc->wm.cxsr_allowed = true;
  12815. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  12816. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  12817. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
  12818. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
  12819. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  12820. intel_color_init(&intel_crtc->base);
  12821. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  12822. return 0;
  12823. fail:
  12824. /*
  12825. * drm_mode_config_cleanup() will free up any
  12826. * crtcs/planes already initialized.
  12827. */
  12828. kfree(crtc_state);
  12829. kfree(intel_crtc);
  12830. return ret;
  12831. }
  12832. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  12833. {
  12834. struct drm_encoder *encoder = connector->base.encoder;
  12835. struct drm_device *dev = connector->base.dev;
  12836. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  12837. if (!encoder || WARN_ON(!encoder->crtc))
  12838. return INVALID_PIPE;
  12839. return to_intel_crtc(encoder->crtc)->pipe;
  12840. }
  12841. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  12842. struct drm_file *file)
  12843. {
  12844. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  12845. struct drm_crtc *drmmode_crtc;
  12846. struct intel_crtc *crtc;
  12847. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  12848. if (!drmmode_crtc)
  12849. return -ENOENT;
  12850. crtc = to_intel_crtc(drmmode_crtc);
  12851. pipe_from_crtc_id->pipe = crtc->pipe;
  12852. return 0;
  12853. }
  12854. static int intel_encoder_clones(struct intel_encoder *encoder)
  12855. {
  12856. struct drm_device *dev = encoder->base.dev;
  12857. struct intel_encoder *source_encoder;
  12858. int index_mask = 0;
  12859. int entry = 0;
  12860. for_each_intel_encoder(dev, source_encoder) {
  12861. if (encoders_cloneable(encoder, source_encoder))
  12862. index_mask |= (1 << entry);
  12863. entry++;
  12864. }
  12865. return index_mask;
  12866. }
  12867. static bool has_edp_a(struct drm_i915_private *dev_priv)
  12868. {
  12869. if (!IS_MOBILE(dev_priv))
  12870. return false;
  12871. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  12872. return false;
  12873. if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  12874. return false;
  12875. return true;
  12876. }
  12877. static bool intel_crt_present(struct drm_i915_private *dev_priv)
  12878. {
  12879. if (INTEL_GEN(dev_priv) >= 9)
  12880. return false;
  12881. if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
  12882. return false;
  12883. if (IS_CHERRYVIEW(dev_priv))
  12884. return false;
  12885. if (HAS_PCH_LPT_H(dev_priv) &&
  12886. I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  12887. return false;
  12888. /* DDI E can't be used if DDI A requires 4 lanes */
  12889. if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  12890. return false;
  12891. if (!dev_priv->vbt.int_crt_support)
  12892. return false;
  12893. return true;
  12894. }
  12895. void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
  12896. {
  12897. int pps_num;
  12898. int pps_idx;
  12899. if (HAS_DDI(dev_priv))
  12900. return;
  12901. /*
  12902. * This w/a is needed at least on CPT/PPT, but to be sure apply it
  12903. * everywhere where registers can be write protected.
  12904. */
  12905. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12906. pps_num = 2;
  12907. else
  12908. pps_num = 1;
  12909. for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
  12910. u32 val = I915_READ(PP_CONTROL(pps_idx));
  12911. val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
  12912. I915_WRITE(PP_CONTROL(pps_idx), val);
  12913. }
  12914. }
  12915. static void intel_pps_init(struct drm_i915_private *dev_priv)
  12916. {
  12917. if (HAS_PCH_SPLIT(dev_priv) || IS_BROXTON(dev_priv))
  12918. dev_priv->pps_mmio_base = PCH_PPS_BASE;
  12919. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12920. dev_priv->pps_mmio_base = VLV_PPS_BASE;
  12921. else
  12922. dev_priv->pps_mmio_base = PPS_BASE;
  12923. intel_pps_unlock_regs_wa(dev_priv);
  12924. }
  12925. static void intel_setup_outputs(struct drm_device *dev)
  12926. {
  12927. struct drm_i915_private *dev_priv = to_i915(dev);
  12928. struct intel_encoder *encoder;
  12929. bool dpd_is_edp = false;
  12930. intel_pps_init(dev_priv);
  12931. /*
  12932. * intel_edp_init_connector() depends on this completing first, to
  12933. * prevent the registeration of both eDP and LVDS and the incorrect
  12934. * sharing of the PPS.
  12935. */
  12936. intel_lvds_init(dev);
  12937. if (intel_crt_present(dev_priv))
  12938. intel_crt_init(dev);
  12939. if (IS_BROXTON(dev_priv)) {
  12940. /*
  12941. * FIXME: Broxton doesn't support port detection via the
  12942. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  12943. * detect the ports.
  12944. */
  12945. intel_ddi_init(dev, PORT_A);
  12946. intel_ddi_init(dev, PORT_B);
  12947. intel_ddi_init(dev, PORT_C);
  12948. intel_dsi_init(dev);
  12949. } else if (HAS_DDI(dev_priv)) {
  12950. int found;
  12951. /*
  12952. * Haswell uses DDI functions to detect digital outputs.
  12953. * On SKL pre-D0 the strap isn't connected, so we assume
  12954. * it's there.
  12955. */
  12956. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  12957. /* WaIgnoreDDIAStrap: skl */
  12958. if (found || IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  12959. intel_ddi_init(dev, PORT_A);
  12960. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  12961. * register */
  12962. found = I915_READ(SFUSE_STRAP);
  12963. if (found & SFUSE_STRAP_DDIB_DETECTED)
  12964. intel_ddi_init(dev, PORT_B);
  12965. if (found & SFUSE_STRAP_DDIC_DETECTED)
  12966. intel_ddi_init(dev, PORT_C);
  12967. if (found & SFUSE_STRAP_DDID_DETECTED)
  12968. intel_ddi_init(dev, PORT_D);
  12969. /*
  12970. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  12971. */
  12972. if ((IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) &&
  12973. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  12974. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  12975. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  12976. intel_ddi_init(dev, PORT_E);
  12977. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12978. int found;
  12979. dpd_is_edp = intel_dp_is_edp(dev_priv, PORT_D);
  12980. if (has_edp_a(dev_priv))
  12981. intel_dp_init(dev, DP_A, PORT_A);
  12982. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  12983. /* PCH SDVOB multiplex with HDMIB */
  12984. found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
  12985. if (!found)
  12986. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  12987. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  12988. intel_dp_init(dev, PCH_DP_B, PORT_B);
  12989. }
  12990. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  12991. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  12992. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  12993. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  12994. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  12995. intel_dp_init(dev, PCH_DP_C, PORT_C);
  12996. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  12997. intel_dp_init(dev, PCH_DP_D, PORT_D);
  12998. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  12999. bool has_edp, has_port;
  13000. /*
  13001. * The DP_DETECTED bit is the latched state of the DDC
  13002. * SDA pin at boot. However since eDP doesn't require DDC
  13003. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  13004. * eDP ports may have been muxed to an alternate function.
  13005. * Thus we can't rely on the DP_DETECTED bit alone to detect
  13006. * eDP ports. Consult the VBT as well as DP_DETECTED to
  13007. * detect eDP ports.
  13008. *
  13009. * Sadly the straps seem to be missing sometimes even for HDMI
  13010. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  13011. * and VBT for the presence of the port. Additionally we can't
  13012. * trust the port type the VBT declares as we've seen at least
  13013. * HDMI ports that the VBT claim are DP or eDP.
  13014. */
  13015. has_edp = intel_dp_is_edp(dev_priv, PORT_B);
  13016. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  13017. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  13018. has_edp &= intel_dp_init(dev, VLV_DP_B, PORT_B);
  13019. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  13020. intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
  13021. has_edp = intel_dp_is_edp(dev_priv, PORT_C);
  13022. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  13023. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  13024. has_edp &= intel_dp_init(dev, VLV_DP_C, PORT_C);
  13025. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  13026. intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
  13027. if (IS_CHERRYVIEW(dev_priv)) {
  13028. /*
  13029. * eDP not supported on port D,
  13030. * so no need to worry about it
  13031. */
  13032. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  13033. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  13034. intel_dp_init(dev, CHV_DP_D, PORT_D);
  13035. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  13036. intel_hdmi_init(dev, CHV_HDMID, PORT_D);
  13037. }
  13038. intel_dsi_init(dev);
  13039. } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
  13040. bool found = false;
  13041. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  13042. DRM_DEBUG_KMS("probing SDVOB\n");
  13043. found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
  13044. if (!found && IS_G4X(dev_priv)) {
  13045. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  13046. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  13047. }
  13048. if (!found && IS_G4X(dev_priv))
  13049. intel_dp_init(dev, DP_B, PORT_B);
  13050. }
  13051. /* Before G4X SDVOC doesn't have its own detect register */
  13052. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  13053. DRM_DEBUG_KMS("probing SDVOC\n");
  13054. found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
  13055. }
  13056. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  13057. if (IS_G4X(dev_priv)) {
  13058. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  13059. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  13060. }
  13061. if (IS_G4X(dev_priv))
  13062. intel_dp_init(dev, DP_C, PORT_C);
  13063. }
  13064. if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
  13065. intel_dp_init(dev, DP_D, PORT_D);
  13066. } else if (IS_GEN2(dev_priv))
  13067. intel_dvo_init(dev);
  13068. if (SUPPORTS_TV(dev_priv))
  13069. intel_tv_init(dev);
  13070. intel_psr_init(dev);
  13071. for_each_intel_encoder(dev, encoder) {
  13072. encoder->base.possible_crtcs = encoder->crtc_mask;
  13073. encoder->base.possible_clones =
  13074. intel_encoder_clones(encoder);
  13075. }
  13076. intel_init_pch_refclk(dev);
  13077. drm_helper_move_panel_connectors_to_head(dev);
  13078. }
  13079. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  13080. {
  13081. struct drm_device *dev = fb->dev;
  13082. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  13083. drm_framebuffer_cleanup(fb);
  13084. mutex_lock(&dev->struct_mutex);
  13085. WARN_ON(!intel_fb->obj->framebuffer_references--);
  13086. i915_gem_object_put(intel_fb->obj);
  13087. mutex_unlock(&dev->struct_mutex);
  13088. kfree(intel_fb);
  13089. }
  13090. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  13091. struct drm_file *file,
  13092. unsigned int *handle)
  13093. {
  13094. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  13095. struct drm_i915_gem_object *obj = intel_fb->obj;
  13096. if (obj->userptr.mm) {
  13097. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  13098. return -EINVAL;
  13099. }
  13100. return drm_gem_handle_create(file, &obj->base, handle);
  13101. }
  13102. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  13103. struct drm_file *file,
  13104. unsigned flags, unsigned color,
  13105. struct drm_clip_rect *clips,
  13106. unsigned num_clips)
  13107. {
  13108. struct drm_device *dev = fb->dev;
  13109. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  13110. struct drm_i915_gem_object *obj = intel_fb->obj;
  13111. mutex_lock(&dev->struct_mutex);
  13112. if (obj->pin_display && obj->cache_dirty)
  13113. i915_gem_clflush_object(obj, true);
  13114. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  13115. mutex_unlock(&dev->struct_mutex);
  13116. return 0;
  13117. }
  13118. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  13119. .destroy = intel_user_framebuffer_destroy,
  13120. .create_handle = intel_user_framebuffer_create_handle,
  13121. .dirty = intel_user_framebuffer_dirty,
  13122. };
  13123. static
  13124. u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
  13125. uint64_t fb_modifier, uint32_t pixel_format)
  13126. {
  13127. u32 gen = INTEL_INFO(dev_priv)->gen;
  13128. if (gen >= 9) {
  13129. int cpp = drm_format_plane_cpp(pixel_format, 0);
  13130. /* "The stride in bytes must not exceed the of the size of 8K
  13131. * pixels and 32K bytes."
  13132. */
  13133. return min(8192 * cpp, 32768);
  13134. } else if (gen >= 5 && !IS_VALLEYVIEW(dev_priv) &&
  13135. !IS_CHERRYVIEW(dev_priv)) {
  13136. return 32*1024;
  13137. } else if (gen >= 4) {
  13138. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  13139. return 16*1024;
  13140. else
  13141. return 32*1024;
  13142. } else if (gen >= 3) {
  13143. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  13144. return 8*1024;
  13145. else
  13146. return 16*1024;
  13147. } else {
  13148. /* XXX DSPC is limited to 4k tiled */
  13149. return 8*1024;
  13150. }
  13151. }
  13152. static int intel_framebuffer_init(struct drm_device *dev,
  13153. struct intel_framebuffer *intel_fb,
  13154. struct drm_mode_fb_cmd2 *mode_cmd,
  13155. struct drm_i915_gem_object *obj)
  13156. {
  13157. struct drm_i915_private *dev_priv = to_i915(dev);
  13158. unsigned int tiling = i915_gem_object_get_tiling(obj);
  13159. int ret;
  13160. u32 pitch_limit, stride_alignment;
  13161. struct drm_format_name_buf format_name;
  13162. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  13163. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  13164. /*
  13165. * If there's a fence, enforce that
  13166. * the fb modifier and tiling mode match.
  13167. */
  13168. if (tiling != I915_TILING_NONE &&
  13169. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  13170. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  13171. return -EINVAL;
  13172. }
  13173. } else {
  13174. if (tiling == I915_TILING_X) {
  13175. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  13176. } else if (tiling == I915_TILING_Y) {
  13177. DRM_DEBUG("No Y tiling for legacy addfb\n");
  13178. return -EINVAL;
  13179. }
  13180. }
  13181. /* Passed in modifier sanity checking. */
  13182. switch (mode_cmd->modifier[0]) {
  13183. case I915_FORMAT_MOD_Y_TILED:
  13184. case I915_FORMAT_MOD_Yf_TILED:
  13185. if (INTEL_GEN(dev_priv) < 9) {
  13186. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  13187. mode_cmd->modifier[0]);
  13188. return -EINVAL;
  13189. }
  13190. case DRM_FORMAT_MOD_NONE:
  13191. case I915_FORMAT_MOD_X_TILED:
  13192. break;
  13193. default:
  13194. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  13195. mode_cmd->modifier[0]);
  13196. return -EINVAL;
  13197. }
  13198. /*
  13199. * gen2/3 display engine uses the fence if present,
  13200. * so the tiling mode must match the fb modifier exactly.
  13201. */
  13202. if (INTEL_INFO(dev_priv)->gen < 4 &&
  13203. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  13204. DRM_DEBUG("tiling_mode must match fb modifier exactly on gen2/3\n");
  13205. return -EINVAL;
  13206. }
  13207. stride_alignment = intel_fb_stride_alignment(dev_priv,
  13208. mode_cmd->modifier[0],
  13209. mode_cmd->pixel_format);
  13210. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  13211. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  13212. mode_cmd->pitches[0], stride_alignment);
  13213. return -EINVAL;
  13214. }
  13215. pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
  13216. mode_cmd->pixel_format);
  13217. if (mode_cmd->pitches[0] > pitch_limit) {
  13218. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  13219. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  13220. "tiled" : "linear",
  13221. mode_cmd->pitches[0], pitch_limit);
  13222. return -EINVAL;
  13223. }
  13224. /*
  13225. * If there's a fence, enforce that
  13226. * the fb pitch and fence stride match.
  13227. */
  13228. if (tiling != I915_TILING_NONE &&
  13229. mode_cmd->pitches[0] != i915_gem_object_get_stride(obj)) {
  13230. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  13231. mode_cmd->pitches[0],
  13232. i915_gem_object_get_stride(obj));
  13233. return -EINVAL;
  13234. }
  13235. /* Reject formats not supported by any plane early. */
  13236. switch (mode_cmd->pixel_format) {
  13237. case DRM_FORMAT_C8:
  13238. case DRM_FORMAT_RGB565:
  13239. case DRM_FORMAT_XRGB8888:
  13240. case DRM_FORMAT_ARGB8888:
  13241. break;
  13242. case DRM_FORMAT_XRGB1555:
  13243. if (INTEL_GEN(dev_priv) > 3) {
  13244. DRM_DEBUG("unsupported pixel format: %s\n",
  13245. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  13246. return -EINVAL;
  13247. }
  13248. break;
  13249. case DRM_FORMAT_ABGR8888:
  13250. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  13251. INTEL_GEN(dev_priv) < 9) {
  13252. DRM_DEBUG("unsupported pixel format: %s\n",
  13253. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  13254. return -EINVAL;
  13255. }
  13256. break;
  13257. case DRM_FORMAT_XBGR8888:
  13258. case DRM_FORMAT_XRGB2101010:
  13259. case DRM_FORMAT_XBGR2101010:
  13260. if (INTEL_GEN(dev_priv) < 4) {
  13261. DRM_DEBUG("unsupported pixel format: %s\n",
  13262. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  13263. return -EINVAL;
  13264. }
  13265. break;
  13266. case DRM_FORMAT_ABGR2101010:
  13267. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  13268. DRM_DEBUG("unsupported pixel format: %s\n",
  13269. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  13270. return -EINVAL;
  13271. }
  13272. break;
  13273. case DRM_FORMAT_YUYV:
  13274. case DRM_FORMAT_UYVY:
  13275. case DRM_FORMAT_YVYU:
  13276. case DRM_FORMAT_VYUY:
  13277. if (INTEL_GEN(dev_priv) < 5) {
  13278. DRM_DEBUG("unsupported pixel format: %s\n",
  13279. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  13280. return -EINVAL;
  13281. }
  13282. break;
  13283. default:
  13284. DRM_DEBUG("unsupported pixel format: %s\n",
  13285. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  13286. return -EINVAL;
  13287. }
  13288. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  13289. if (mode_cmd->offsets[0] != 0)
  13290. return -EINVAL;
  13291. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  13292. intel_fb->obj = obj;
  13293. ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
  13294. if (ret)
  13295. return ret;
  13296. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  13297. if (ret) {
  13298. DRM_ERROR("framebuffer init failed %d\n", ret);
  13299. return ret;
  13300. }
  13301. intel_fb->obj->framebuffer_references++;
  13302. return 0;
  13303. }
  13304. static struct drm_framebuffer *
  13305. intel_user_framebuffer_create(struct drm_device *dev,
  13306. struct drm_file *filp,
  13307. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  13308. {
  13309. struct drm_framebuffer *fb;
  13310. struct drm_i915_gem_object *obj;
  13311. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  13312. obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
  13313. if (!obj)
  13314. return ERR_PTR(-ENOENT);
  13315. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  13316. if (IS_ERR(fb))
  13317. i915_gem_object_put(obj);
  13318. return fb;
  13319. }
  13320. static const struct drm_mode_config_funcs intel_mode_funcs = {
  13321. .fb_create = intel_user_framebuffer_create,
  13322. .output_poll_changed = intel_fbdev_output_poll_changed,
  13323. .atomic_check = intel_atomic_check,
  13324. .atomic_commit = intel_atomic_commit,
  13325. .atomic_state_alloc = intel_atomic_state_alloc,
  13326. .atomic_state_clear = intel_atomic_state_clear,
  13327. };
  13328. /**
  13329. * intel_init_display_hooks - initialize the display modesetting hooks
  13330. * @dev_priv: device private
  13331. */
  13332. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  13333. {
  13334. if (INTEL_INFO(dev_priv)->gen >= 9) {
  13335. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  13336. dev_priv->display.get_initial_plane_config =
  13337. skylake_get_initial_plane_config;
  13338. dev_priv->display.crtc_compute_clock =
  13339. haswell_crtc_compute_clock;
  13340. dev_priv->display.crtc_enable = haswell_crtc_enable;
  13341. dev_priv->display.crtc_disable = haswell_crtc_disable;
  13342. } else if (HAS_DDI(dev_priv)) {
  13343. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  13344. dev_priv->display.get_initial_plane_config =
  13345. ironlake_get_initial_plane_config;
  13346. dev_priv->display.crtc_compute_clock =
  13347. haswell_crtc_compute_clock;
  13348. dev_priv->display.crtc_enable = haswell_crtc_enable;
  13349. dev_priv->display.crtc_disable = haswell_crtc_disable;
  13350. } else if (HAS_PCH_SPLIT(dev_priv)) {
  13351. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  13352. dev_priv->display.get_initial_plane_config =
  13353. ironlake_get_initial_plane_config;
  13354. dev_priv->display.crtc_compute_clock =
  13355. ironlake_crtc_compute_clock;
  13356. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  13357. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  13358. } else if (IS_CHERRYVIEW(dev_priv)) {
  13359. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13360. dev_priv->display.get_initial_plane_config =
  13361. i9xx_get_initial_plane_config;
  13362. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  13363. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  13364. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13365. } else if (IS_VALLEYVIEW(dev_priv)) {
  13366. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13367. dev_priv->display.get_initial_plane_config =
  13368. i9xx_get_initial_plane_config;
  13369. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  13370. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  13371. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13372. } else if (IS_G4X(dev_priv)) {
  13373. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13374. dev_priv->display.get_initial_plane_config =
  13375. i9xx_get_initial_plane_config;
  13376. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  13377. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13378. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13379. } else if (IS_PINEVIEW(dev_priv)) {
  13380. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13381. dev_priv->display.get_initial_plane_config =
  13382. i9xx_get_initial_plane_config;
  13383. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  13384. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13385. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13386. } else if (!IS_GEN2(dev_priv)) {
  13387. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13388. dev_priv->display.get_initial_plane_config =
  13389. i9xx_get_initial_plane_config;
  13390. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  13391. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13392. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13393. } else {
  13394. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13395. dev_priv->display.get_initial_plane_config =
  13396. i9xx_get_initial_plane_config;
  13397. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  13398. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13399. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13400. }
  13401. /* Returns the core display clock speed */
  13402. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  13403. dev_priv->display.get_display_clock_speed =
  13404. skylake_get_display_clock_speed;
  13405. else if (IS_BROXTON(dev_priv))
  13406. dev_priv->display.get_display_clock_speed =
  13407. broxton_get_display_clock_speed;
  13408. else if (IS_BROADWELL(dev_priv))
  13409. dev_priv->display.get_display_clock_speed =
  13410. broadwell_get_display_clock_speed;
  13411. else if (IS_HASWELL(dev_priv))
  13412. dev_priv->display.get_display_clock_speed =
  13413. haswell_get_display_clock_speed;
  13414. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  13415. dev_priv->display.get_display_clock_speed =
  13416. valleyview_get_display_clock_speed;
  13417. else if (IS_GEN5(dev_priv))
  13418. dev_priv->display.get_display_clock_speed =
  13419. ilk_get_display_clock_speed;
  13420. else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
  13421. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  13422. dev_priv->display.get_display_clock_speed =
  13423. i945_get_display_clock_speed;
  13424. else if (IS_GM45(dev_priv))
  13425. dev_priv->display.get_display_clock_speed =
  13426. gm45_get_display_clock_speed;
  13427. else if (IS_CRESTLINE(dev_priv))
  13428. dev_priv->display.get_display_clock_speed =
  13429. i965gm_get_display_clock_speed;
  13430. else if (IS_PINEVIEW(dev_priv))
  13431. dev_priv->display.get_display_clock_speed =
  13432. pnv_get_display_clock_speed;
  13433. else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
  13434. dev_priv->display.get_display_clock_speed =
  13435. g33_get_display_clock_speed;
  13436. else if (IS_I915G(dev_priv))
  13437. dev_priv->display.get_display_clock_speed =
  13438. i915_get_display_clock_speed;
  13439. else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
  13440. dev_priv->display.get_display_clock_speed =
  13441. i9xx_misc_get_display_clock_speed;
  13442. else if (IS_I915GM(dev_priv))
  13443. dev_priv->display.get_display_clock_speed =
  13444. i915gm_get_display_clock_speed;
  13445. else if (IS_I865G(dev_priv))
  13446. dev_priv->display.get_display_clock_speed =
  13447. i865_get_display_clock_speed;
  13448. else if (IS_I85X(dev_priv))
  13449. dev_priv->display.get_display_clock_speed =
  13450. i85x_get_display_clock_speed;
  13451. else { /* 830 */
  13452. WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
  13453. dev_priv->display.get_display_clock_speed =
  13454. i830_get_display_clock_speed;
  13455. }
  13456. if (IS_GEN5(dev_priv)) {
  13457. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  13458. } else if (IS_GEN6(dev_priv)) {
  13459. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  13460. } else if (IS_IVYBRIDGE(dev_priv)) {
  13461. /* FIXME: detect B0+ stepping and use auto training */
  13462. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  13463. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  13464. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  13465. }
  13466. if (IS_BROADWELL(dev_priv)) {
  13467. dev_priv->display.modeset_commit_cdclk =
  13468. broadwell_modeset_commit_cdclk;
  13469. dev_priv->display.modeset_calc_cdclk =
  13470. broadwell_modeset_calc_cdclk;
  13471. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  13472. dev_priv->display.modeset_commit_cdclk =
  13473. valleyview_modeset_commit_cdclk;
  13474. dev_priv->display.modeset_calc_cdclk =
  13475. valleyview_modeset_calc_cdclk;
  13476. } else if (IS_BROXTON(dev_priv)) {
  13477. dev_priv->display.modeset_commit_cdclk =
  13478. bxt_modeset_commit_cdclk;
  13479. dev_priv->display.modeset_calc_cdclk =
  13480. bxt_modeset_calc_cdclk;
  13481. } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
  13482. dev_priv->display.modeset_commit_cdclk =
  13483. skl_modeset_commit_cdclk;
  13484. dev_priv->display.modeset_calc_cdclk =
  13485. skl_modeset_calc_cdclk;
  13486. }
  13487. if (dev_priv->info.gen >= 9)
  13488. dev_priv->display.update_crtcs = skl_update_crtcs;
  13489. else
  13490. dev_priv->display.update_crtcs = intel_update_crtcs;
  13491. switch (INTEL_INFO(dev_priv)->gen) {
  13492. case 2:
  13493. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  13494. break;
  13495. case 3:
  13496. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  13497. break;
  13498. case 4:
  13499. case 5:
  13500. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  13501. break;
  13502. case 6:
  13503. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  13504. break;
  13505. case 7:
  13506. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  13507. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  13508. break;
  13509. case 9:
  13510. /* Drop through - unsupported since execlist only. */
  13511. default:
  13512. /* Default just returns -ENODEV to indicate unsupported */
  13513. dev_priv->display.queue_flip = intel_default_queue_flip;
  13514. }
  13515. }
  13516. /*
  13517. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  13518. * resume, or other times. This quirk makes sure that's the case for
  13519. * affected systems.
  13520. */
  13521. static void quirk_pipea_force(struct drm_device *dev)
  13522. {
  13523. struct drm_i915_private *dev_priv = to_i915(dev);
  13524. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  13525. DRM_INFO("applying pipe a force quirk\n");
  13526. }
  13527. static void quirk_pipeb_force(struct drm_device *dev)
  13528. {
  13529. struct drm_i915_private *dev_priv = to_i915(dev);
  13530. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  13531. DRM_INFO("applying pipe b force quirk\n");
  13532. }
  13533. /*
  13534. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  13535. */
  13536. static void quirk_ssc_force_disable(struct drm_device *dev)
  13537. {
  13538. struct drm_i915_private *dev_priv = to_i915(dev);
  13539. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  13540. DRM_INFO("applying lvds SSC disable quirk\n");
  13541. }
  13542. /*
  13543. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  13544. * brightness value
  13545. */
  13546. static void quirk_invert_brightness(struct drm_device *dev)
  13547. {
  13548. struct drm_i915_private *dev_priv = to_i915(dev);
  13549. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  13550. DRM_INFO("applying inverted panel brightness quirk\n");
  13551. }
  13552. /* Some VBT's incorrectly indicate no backlight is present */
  13553. static void quirk_backlight_present(struct drm_device *dev)
  13554. {
  13555. struct drm_i915_private *dev_priv = to_i915(dev);
  13556. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  13557. DRM_INFO("applying backlight present quirk\n");
  13558. }
  13559. struct intel_quirk {
  13560. int device;
  13561. int subsystem_vendor;
  13562. int subsystem_device;
  13563. void (*hook)(struct drm_device *dev);
  13564. };
  13565. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  13566. struct intel_dmi_quirk {
  13567. void (*hook)(struct drm_device *dev);
  13568. const struct dmi_system_id (*dmi_id_list)[];
  13569. };
  13570. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  13571. {
  13572. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  13573. return 1;
  13574. }
  13575. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  13576. {
  13577. .dmi_id_list = &(const struct dmi_system_id[]) {
  13578. {
  13579. .callback = intel_dmi_reverse_brightness,
  13580. .ident = "NCR Corporation",
  13581. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  13582. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  13583. },
  13584. },
  13585. { } /* terminating entry */
  13586. },
  13587. .hook = quirk_invert_brightness,
  13588. },
  13589. };
  13590. static struct intel_quirk intel_quirks[] = {
  13591. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  13592. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  13593. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  13594. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  13595. /* 830 needs to leave pipe A & dpll A up */
  13596. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  13597. /* 830 needs to leave pipe B & dpll B up */
  13598. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  13599. /* Lenovo U160 cannot use SSC on LVDS */
  13600. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  13601. /* Sony Vaio Y cannot use SSC on LVDS */
  13602. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  13603. /* Acer Aspire 5734Z must invert backlight brightness */
  13604. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  13605. /* Acer/eMachines G725 */
  13606. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  13607. /* Acer/eMachines e725 */
  13608. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  13609. /* Acer/Packard Bell NCL20 */
  13610. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  13611. /* Acer Aspire 4736Z */
  13612. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  13613. /* Acer Aspire 5336 */
  13614. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  13615. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  13616. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  13617. /* Acer C720 Chromebook (Core i3 4005U) */
  13618. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  13619. /* Apple Macbook 2,1 (Core 2 T7400) */
  13620. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  13621. /* Apple Macbook 4,1 */
  13622. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  13623. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  13624. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  13625. /* HP Chromebook 14 (Celeron 2955U) */
  13626. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  13627. /* Dell Chromebook 11 */
  13628. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  13629. /* Dell Chromebook 11 (2015 version) */
  13630. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  13631. };
  13632. static void intel_init_quirks(struct drm_device *dev)
  13633. {
  13634. struct pci_dev *d = dev->pdev;
  13635. int i;
  13636. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  13637. struct intel_quirk *q = &intel_quirks[i];
  13638. if (d->device == q->device &&
  13639. (d->subsystem_vendor == q->subsystem_vendor ||
  13640. q->subsystem_vendor == PCI_ANY_ID) &&
  13641. (d->subsystem_device == q->subsystem_device ||
  13642. q->subsystem_device == PCI_ANY_ID))
  13643. q->hook(dev);
  13644. }
  13645. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  13646. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  13647. intel_dmi_quirks[i].hook(dev);
  13648. }
  13649. }
  13650. /* Disable the VGA plane that we never use */
  13651. static void i915_disable_vga(struct drm_i915_private *dev_priv)
  13652. {
  13653. struct pci_dev *pdev = dev_priv->drm.pdev;
  13654. u8 sr1;
  13655. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  13656. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  13657. vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
  13658. outb(SR01, VGA_SR_INDEX);
  13659. sr1 = inb(VGA_SR_DATA);
  13660. outb(sr1 | 1<<5, VGA_SR_DATA);
  13661. vga_put(pdev, VGA_RSRC_LEGACY_IO);
  13662. udelay(300);
  13663. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  13664. POSTING_READ(vga_reg);
  13665. }
  13666. void intel_modeset_init_hw(struct drm_device *dev)
  13667. {
  13668. struct drm_i915_private *dev_priv = to_i915(dev);
  13669. intel_update_cdclk(dev_priv);
  13670. dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
  13671. intel_init_clock_gating(dev_priv);
  13672. }
  13673. /*
  13674. * Calculate what we think the watermarks should be for the state we've read
  13675. * out of the hardware and then immediately program those watermarks so that
  13676. * we ensure the hardware settings match our internal state.
  13677. *
  13678. * We can calculate what we think WM's should be by creating a duplicate of the
  13679. * current state (which was constructed during hardware readout) and running it
  13680. * through the atomic check code to calculate new watermark values in the
  13681. * state object.
  13682. */
  13683. static void sanitize_watermarks(struct drm_device *dev)
  13684. {
  13685. struct drm_i915_private *dev_priv = to_i915(dev);
  13686. struct drm_atomic_state *state;
  13687. struct intel_atomic_state *intel_state;
  13688. struct drm_crtc *crtc;
  13689. struct drm_crtc_state *cstate;
  13690. struct drm_modeset_acquire_ctx ctx;
  13691. int ret;
  13692. int i;
  13693. /* Only supported on platforms that use atomic watermark design */
  13694. if (!dev_priv->display.optimize_watermarks)
  13695. return;
  13696. /*
  13697. * We need to hold connection_mutex before calling duplicate_state so
  13698. * that the connector loop is protected.
  13699. */
  13700. drm_modeset_acquire_init(&ctx, 0);
  13701. retry:
  13702. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  13703. if (ret == -EDEADLK) {
  13704. drm_modeset_backoff(&ctx);
  13705. goto retry;
  13706. } else if (WARN_ON(ret)) {
  13707. goto fail;
  13708. }
  13709. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  13710. if (WARN_ON(IS_ERR(state)))
  13711. goto fail;
  13712. intel_state = to_intel_atomic_state(state);
  13713. /*
  13714. * Hardware readout is the only time we don't want to calculate
  13715. * intermediate watermarks (since we don't trust the current
  13716. * watermarks).
  13717. */
  13718. intel_state->skip_intermediate_wm = true;
  13719. ret = intel_atomic_check(dev, state);
  13720. if (ret) {
  13721. /*
  13722. * If we fail here, it means that the hardware appears to be
  13723. * programmed in a way that shouldn't be possible, given our
  13724. * understanding of watermark requirements. This might mean a
  13725. * mistake in the hardware readout code or a mistake in the
  13726. * watermark calculations for a given platform. Raise a WARN
  13727. * so that this is noticeable.
  13728. *
  13729. * If this actually happens, we'll have to just leave the
  13730. * BIOS-programmed watermarks untouched and hope for the best.
  13731. */
  13732. WARN(true, "Could not determine valid watermarks for inherited state\n");
  13733. goto put_state;
  13734. }
  13735. /* Write calculated watermark values back */
  13736. for_each_crtc_in_state(state, crtc, cstate, i) {
  13737. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  13738. cs->wm.need_postvbl_update = true;
  13739. dev_priv->display.optimize_watermarks(intel_state, cs);
  13740. }
  13741. put_state:
  13742. drm_atomic_state_put(state);
  13743. fail:
  13744. drm_modeset_drop_locks(&ctx);
  13745. drm_modeset_acquire_fini(&ctx);
  13746. }
  13747. static void intel_atomic_helper_free_state(struct work_struct *work)
  13748. {
  13749. struct drm_i915_private *dev_priv =
  13750. container_of(work, typeof(*dev_priv), atomic_helper.free_work);
  13751. struct intel_atomic_state *state, *next;
  13752. struct llist_node *freed;
  13753. freed = llist_del_all(&dev_priv->atomic_helper.free_list);
  13754. llist_for_each_entry_safe(state, next, freed, freed)
  13755. drm_atomic_state_put(&state->base);
  13756. }
  13757. int intel_modeset_init(struct drm_device *dev)
  13758. {
  13759. struct drm_i915_private *dev_priv = to_i915(dev);
  13760. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  13761. enum pipe pipe;
  13762. struct intel_crtc *crtc;
  13763. drm_mode_config_init(dev);
  13764. dev->mode_config.min_width = 0;
  13765. dev->mode_config.min_height = 0;
  13766. dev->mode_config.preferred_depth = 24;
  13767. dev->mode_config.prefer_shadow = 1;
  13768. dev->mode_config.allow_fb_modifiers = true;
  13769. dev->mode_config.funcs = &intel_mode_funcs;
  13770. INIT_WORK(&dev_priv->atomic_helper.free_work,
  13771. intel_atomic_helper_free_state);
  13772. intel_init_quirks(dev);
  13773. intel_init_pm(dev_priv);
  13774. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  13775. return 0;
  13776. /*
  13777. * There may be no VBT; and if the BIOS enabled SSC we can
  13778. * just keep using it to avoid unnecessary flicker. Whereas if the
  13779. * BIOS isn't using it, don't assume it will work even if the VBT
  13780. * indicates as much.
  13781. */
  13782. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
  13783. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  13784. DREF_SSC1_ENABLE);
  13785. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  13786. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  13787. bios_lvds_use_ssc ? "en" : "dis",
  13788. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  13789. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  13790. }
  13791. }
  13792. if (IS_GEN2(dev_priv)) {
  13793. dev->mode_config.max_width = 2048;
  13794. dev->mode_config.max_height = 2048;
  13795. } else if (IS_GEN3(dev_priv)) {
  13796. dev->mode_config.max_width = 4096;
  13797. dev->mode_config.max_height = 4096;
  13798. } else {
  13799. dev->mode_config.max_width = 8192;
  13800. dev->mode_config.max_height = 8192;
  13801. }
  13802. if (IS_845G(dev_priv) || IS_I865G(dev_priv)) {
  13803. dev->mode_config.cursor_width = IS_845G(dev_priv) ? 64 : 512;
  13804. dev->mode_config.cursor_height = 1023;
  13805. } else if (IS_GEN2(dev_priv)) {
  13806. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  13807. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  13808. } else {
  13809. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  13810. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  13811. }
  13812. dev->mode_config.fb_base = ggtt->mappable_base;
  13813. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  13814. INTEL_INFO(dev_priv)->num_pipes,
  13815. INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
  13816. for_each_pipe(dev_priv, pipe) {
  13817. int ret;
  13818. ret = intel_crtc_init(dev_priv, pipe);
  13819. if (ret) {
  13820. drm_mode_config_cleanup(dev);
  13821. return ret;
  13822. }
  13823. }
  13824. intel_update_czclk(dev_priv);
  13825. intel_update_cdclk(dev_priv);
  13826. dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
  13827. intel_shared_dpll_init(dev);
  13828. if (dev_priv->max_cdclk_freq == 0)
  13829. intel_update_max_cdclk(dev_priv);
  13830. /* Just disable it once at startup */
  13831. i915_disable_vga(dev_priv);
  13832. intel_setup_outputs(dev);
  13833. drm_modeset_lock_all(dev);
  13834. intel_modeset_setup_hw_state(dev);
  13835. drm_modeset_unlock_all(dev);
  13836. for_each_intel_crtc(dev, crtc) {
  13837. struct intel_initial_plane_config plane_config = {};
  13838. if (!crtc->active)
  13839. continue;
  13840. /*
  13841. * Note that reserving the BIOS fb up front prevents us
  13842. * from stuffing other stolen allocations like the ring
  13843. * on top. This prevents some ugliness at boot time, and
  13844. * can even allow for smooth boot transitions if the BIOS
  13845. * fb is large enough for the active pipe configuration.
  13846. */
  13847. dev_priv->display.get_initial_plane_config(crtc,
  13848. &plane_config);
  13849. /*
  13850. * If the fb is shared between multiple heads, we'll
  13851. * just get the first one.
  13852. */
  13853. intel_find_initial_plane_obj(crtc, &plane_config);
  13854. }
  13855. /*
  13856. * Make sure hardware watermarks really match the state we read out.
  13857. * Note that we need to do this after reconstructing the BIOS fb's
  13858. * since the watermark calculation done here will use pstate->fb.
  13859. */
  13860. sanitize_watermarks(dev);
  13861. return 0;
  13862. }
  13863. static void intel_enable_pipe_a(struct drm_device *dev)
  13864. {
  13865. struct intel_connector *connector;
  13866. struct drm_connector *crt = NULL;
  13867. struct intel_load_detect_pipe load_detect_temp;
  13868. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  13869. /* We can't just switch on the pipe A, we need to set things up with a
  13870. * proper mode and output configuration. As a gross hack, enable pipe A
  13871. * by enabling the load detect pipe once. */
  13872. for_each_intel_connector(dev, connector) {
  13873. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  13874. crt = &connector->base;
  13875. break;
  13876. }
  13877. }
  13878. if (!crt)
  13879. return;
  13880. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  13881. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  13882. }
  13883. static bool
  13884. intel_check_plane_mapping(struct intel_crtc *crtc)
  13885. {
  13886. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  13887. u32 val;
  13888. if (INTEL_INFO(dev_priv)->num_pipes == 1)
  13889. return true;
  13890. val = I915_READ(DSPCNTR(!crtc->plane));
  13891. if ((val & DISPLAY_PLANE_ENABLE) &&
  13892. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  13893. return false;
  13894. return true;
  13895. }
  13896. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  13897. {
  13898. struct drm_device *dev = crtc->base.dev;
  13899. struct intel_encoder *encoder;
  13900. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  13901. return true;
  13902. return false;
  13903. }
  13904. static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
  13905. {
  13906. struct drm_device *dev = encoder->base.dev;
  13907. struct intel_connector *connector;
  13908. for_each_connector_on_encoder(dev, &encoder->base, connector)
  13909. return connector;
  13910. return NULL;
  13911. }
  13912. static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
  13913. enum transcoder pch_transcoder)
  13914. {
  13915. return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
  13916. (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
  13917. }
  13918. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  13919. {
  13920. struct drm_device *dev = crtc->base.dev;
  13921. struct drm_i915_private *dev_priv = to_i915(dev);
  13922. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  13923. /* Clear any frame start delays used for debugging left by the BIOS */
  13924. if (!transcoder_is_dsi(cpu_transcoder)) {
  13925. i915_reg_t reg = PIPECONF(cpu_transcoder);
  13926. I915_WRITE(reg,
  13927. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  13928. }
  13929. /* restore vblank interrupts to correct state */
  13930. drm_crtc_vblank_reset(&crtc->base);
  13931. if (crtc->active) {
  13932. struct intel_plane *plane;
  13933. drm_crtc_vblank_on(&crtc->base);
  13934. /* Disable everything but the primary plane */
  13935. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  13936. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  13937. continue;
  13938. plane->disable_plane(&plane->base, &crtc->base);
  13939. }
  13940. }
  13941. /* We need to sanitize the plane -> pipe mapping first because this will
  13942. * disable the crtc (and hence change the state) if it is wrong. Note
  13943. * that gen4+ has a fixed plane -> pipe mapping. */
  13944. if (INTEL_GEN(dev_priv) < 4 && !intel_check_plane_mapping(crtc)) {
  13945. bool plane;
  13946. DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
  13947. crtc->base.base.id, crtc->base.name);
  13948. /* Pipe has the wrong plane attached and the plane is active.
  13949. * Temporarily change the plane mapping and disable everything
  13950. * ... */
  13951. plane = crtc->plane;
  13952. to_intel_plane_state(crtc->base.primary->state)->base.visible = true;
  13953. crtc->plane = !plane;
  13954. intel_crtc_disable_noatomic(&crtc->base);
  13955. crtc->plane = plane;
  13956. }
  13957. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  13958. crtc->pipe == PIPE_A && !crtc->active) {
  13959. /* BIOS forgot to enable pipe A, this mostly happens after
  13960. * resume. Force-enable the pipe to fix this, the update_dpms
  13961. * call below we restore the pipe to the right state, but leave
  13962. * the required bits on. */
  13963. intel_enable_pipe_a(dev);
  13964. }
  13965. /* Adjust the state of the output pipe according to whether we
  13966. * have active connectors/encoders. */
  13967. if (crtc->active && !intel_crtc_has_encoders(crtc))
  13968. intel_crtc_disable_noatomic(&crtc->base);
  13969. if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
  13970. /*
  13971. * We start out with underrun reporting disabled to avoid races.
  13972. * For correct bookkeeping mark this on active crtcs.
  13973. *
  13974. * Also on gmch platforms we dont have any hardware bits to
  13975. * disable the underrun reporting. Which means we need to start
  13976. * out with underrun reporting disabled also on inactive pipes,
  13977. * since otherwise we'll complain about the garbage we read when
  13978. * e.g. coming up after runtime pm.
  13979. *
  13980. * No protection against concurrent access is required - at
  13981. * worst a fifo underrun happens which also sets this to false.
  13982. */
  13983. crtc->cpu_fifo_underrun_disabled = true;
  13984. /*
  13985. * We track the PCH trancoder underrun reporting state
  13986. * within the crtc. With crtc for pipe A housing the underrun
  13987. * reporting state for PCH transcoder A, crtc for pipe B housing
  13988. * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
  13989. * and marking underrun reporting as disabled for the non-existing
  13990. * PCH transcoders B and C would prevent enabling the south
  13991. * error interrupt (see cpt_can_enable_serr_int()).
  13992. */
  13993. if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
  13994. crtc->pch_fifo_underrun_disabled = true;
  13995. }
  13996. }
  13997. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  13998. {
  13999. struct intel_connector *connector;
  14000. /* We need to check both for a crtc link (meaning that the
  14001. * encoder is active and trying to read from a pipe) and the
  14002. * pipe itself being active. */
  14003. bool has_active_crtc = encoder->base.crtc &&
  14004. to_intel_crtc(encoder->base.crtc)->active;
  14005. connector = intel_encoder_find_connector(encoder);
  14006. if (connector && !has_active_crtc) {
  14007. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  14008. encoder->base.base.id,
  14009. encoder->base.name);
  14010. /* Connector is active, but has no active pipe. This is
  14011. * fallout from our resume register restoring. Disable
  14012. * the encoder manually again. */
  14013. if (encoder->base.crtc) {
  14014. struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
  14015. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  14016. encoder->base.base.id,
  14017. encoder->base.name);
  14018. encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  14019. if (encoder->post_disable)
  14020. encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  14021. }
  14022. encoder->base.crtc = NULL;
  14023. /* Inconsistent output/port/pipe state happens presumably due to
  14024. * a bug in one of the get_hw_state functions. Or someplace else
  14025. * in our code, like the register restore mess on resume. Clamp
  14026. * things to off as a safer default. */
  14027. connector->base.dpms = DRM_MODE_DPMS_OFF;
  14028. connector->base.encoder = NULL;
  14029. }
  14030. /* Enabled encoders without active connectors will be fixed in
  14031. * the crtc fixup. */
  14032. }
  14033. void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
  14034. {
  14035. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  14036. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  14037. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  14038. i915_disable_vga(dev_priv);
  14039. }
  14040. }
  14041. void i915_redisable_vga(struct drm_i915_private *dev_priv)
  14042. {
  14043. /* This function can be called both from intel_modeset_setup_hw_state or
  14044. * at a very early point in our resume sequence, where the power well
  14045. * structures are not yet restored. Since this function is at a very
  14046. * paranoid "someone might have enabled VGA while we were not looking"
  14047. * level, just check if the power well is enabled instead of trying to
  14048. * follow the "don't touch the power well if we don't need it" policy
  14049. * the rest of the driver uses. */
  14050. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  14051. return;
  14052. i915_redisable_vga_power_on(dev_priv);
  14053. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  14054. }
  14055. static bool primary_get_hw_state(struct intel_plane *plane)
  14056. {
  14057. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  14058. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  14059. }
  14060. /* FIXME read out full plane state for all planes */
  14061. static void readout_plane_state(struct intel_crtc *crtc)
  14062. {
  14063. struct drm_plane *primary = crtc->base.primary;
  14064. struct intel_plane_state *plane_state =
  14065. to_intel_plane_state(primary->state);
  14066. plane_state->base.visible = crtc->active &&
  14067. primary_get_hw_state(to_intel_plane(primary));
  14068. if (plane_state->base.visible)
  14069. crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
  14070. }
  14071. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  14072. {
  14073. struct drm_i915_private *dev_priv = to_i915(dev);
  14074. enum pipe pipe;
  14075. struct intel_crtc *crtc;
  14076. struct intel_encoder *encoder;
  14077. struct intel_connector *connector;
  14078. int i;
  14079. dev_priv->active_crtcs = 0;
  14080. for_each_intel_crtc(dev, crtc) {
  14081. struct intel_crtc_state *crtc_state = crtc->config;
  14082. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  14083. memset(crtc_state, 0, sizeof(*crtc_state));
  14084. crtc_state->base.crtc = &crtc->base;
  14085. crtc_state->base.active = crtc_state->base.enable =
  14086. dev_priv->display.get_pipe_config(crtc, crtc_state);
  14087. crtc->base.enabled = crtc_state->base.enable;
  14088. crtc->active = crtc_state->base.active;
  14089. if (crtc_state->base.active)
  14090. dev_priv->active_crtcs |= 1 << crtc->pipe;
  14091. readout_plane_state(crtc);
  14092. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  14093. crtc->base.base.id, crtc->base.name,
  14094. enableddisabled(crtc->active));
  14095. }
  14096. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  14097. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  14098. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  14099. &pll->config.hw_state);
  14100. pll->config.crtc_mask = 0;
  14101. for_each_intel_crtc(dev, crtc) {
  14102. if (crtc->active && crtc->config->shared_dpll == pll)
  14103. pll->config.crtc_mask |= 1 << crtc->pipe;
  14104. }
  14105. pll->active_mask = pll->config.crtc_mask;
  14106. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  14107. pll->name, pll->config.crtc_mask, pll->on);
  14108. }
  14109. for_each_intel_encoder(dev, encoder) {
  14110. pipe = 0;
  14111. if (encoder->get_hw_state(encoder, &pipe)) {
  14112. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  14113. encoder->base.crtc = &crtc->base;
  14114. crtc->config->output_types |= 1 << encoder->type;
  14115. encoder->get_config(encoder, crtc->config);
  14116. } else {
  14117. encoder->base.crtc = NULL;
  14118. }
  14119. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  14120. encoder->base.base.id, encoder->base.name,
  14121. enableddisabled(encoder->base.crtc),
  14122. pipe_name(pipe));
  14123. }
  14124. for_each_intel_connector(dev, connector) {
  14125. if (connector->get_hw_state(connector)) {
  14126. connector->base.dpms = DRM_MODE_DPMS_ON;
  14127. encoder = connector->encoder;
  14128. connector->base.encoder = &encoder->base;
  14129. if (encoder->base.crtc &&
  14130. encoder->base.crtc->state->active) {
  14131. /*
  14132. * This has to be done during hardware readout
  14133. * because anything calling .crtc_disable may
  14134. * rely on the connector_mask being accurate.
  14135. */
  14136. encoder->base.crtc->state->connector_mask |=
  14137. 1 << drm_connector_index(&connector->base);
  14138. encoder->base.crtc->state->encoder_mask |=
  14139. 1 << drm_encoder_index(&encoder->base);
  14140. }
  14141. } else {
  14142. connector->base.dpms = DRM_MODE_DPMS_OFF;
  14143. connector->base.encoder = NULL;
  14144. }
  14145. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  14146. connector->base.base.id, connector->base.name,
  14147. enableddisabled(connector->base.encoder));
  14148. }
  14149. for_each_intel_crtc(dev, crtc) {
  14150. int pixclk = 0;
  14151. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  14152. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  14153. if (crtc->base.state->active) {
  14154. intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
  14155. intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
  14156. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  14157. /*
  14158. * The initial mode needs to be set in order to keep
  14159. * the atomic core happy. It wants a valid mode if the
  14160. * crtc's enabled, so we do the above call.
  14161. *
  14162. * At this point some state updated by the connectors
  14163. * in their ->detect() callback has not run yet, so
  14164. * no recalculation can be done yet.
  14165. *
  14166. * Even if we could do a recalculation and modeset
  14167. * right now it would cause a double modeset if
  14168. * fbdev or userspace chooses a different initial mode.
  14169. *
  14170. * If that happens, someone indicated they wanted a
  14171. * mode change, which means it's safe to do a full
  14172. * recalculation.
  14173. */
  14174. crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
  14175. if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
  14176. pixclk = ilk_pipe_pixel_rate(crtc->config);
  14177. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  14178. pixclk = crtc->config->base.adjusted_mode.crtc_clock;
  14179. else
  14180. WARN_ON(dev_priv->display.modeset_calc_cdclk);
  14181. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  14182. if (IS_BROADWELL(dev_priv) && crtc->config->ips_enabled)
  14183. pixclk = DIV_ROUND_UP(pixclk * 100, 95);
  14184. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  14185. update_scanline_offset(crtc);
  14186. }
  14187. dev_priv->min_pixclk[crtc->pipe] = pixclk;
  14188. intel_pipe_config_sanity_check(dev_priv, crtc->config);
  14189. }
  14190. }
  14191. /* Scan out the current hw modeset state,
  14192. * and sanitizes it to the current state
  14193. */
  14194. static void
  14195. intel_modeset_setup_hw_state(struct drm_device *dev)
  14196. {
  14197. struct drm_i915_private *dev_priv = to_i915(dev);
  14198. enum pipe pipe;
  14199. struct intel_crtc *crtc;
  14200. struct intel_encoder *encoder;
  14201. int i;
  14202. intel_modeset_readout_hw_state(dev);
  14203. /* HW state is read out, now we need to sanitize this mess. */
  14204. for_each_intel_encoder(dev, encoder) {
  14205. intel_sanitize_encoder(encoder);
  14206. }
  14207. for_each_pipe(dev_priv, pipe) {
  14208. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  14209. intel_sanitize_crtc(crtc);
  14210. intel_dump_pipe_config(crtc, crtc->config,
  14211. "[setup_hw_state]");
  14212. }
  14213. intel_modeset_update_connector_atomic_state(dev);
  14214. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  14215. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  14216. if (!pll->on || pll->active_mask)
  14217. continue;
  14218. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  14219. pll->funcs.disable(dev_priv, pll);
  14220. pll->on = false;
  14221. }
  14222. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  14223. vlv_wm_get_hw_state(dev);
  14224. else if (IS_GEN9(dev_priv))
  14225. skl_wm_get_hw_state(dev);
  14226. else if (HAS_PCH_SPLIT(dev_priv))
  14227. ilk_wm_get_hw_state(dev);
  14228. for_each_intel_crtc(dev, crtc) {
  14229. unsigned long put_domains;
  14230. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  14231. if (WARN_ON(put_domains))
  14232. modeset_put_power_domains(dev_priv, put_domains);
  14233. }
  14234. intel_display_set_init_power(dev_priv, false);
  14235. intel_fbc_init_pipe_state(dev_priv);
  14236. }
  14237. void intel_display_resume(struct drm_device *dev)
  14238. {
  14239. struct drm_i915_private *dev_priv = to_i915(dev);
  14240. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  14241. struct drm_modeset_acquire_ctx ctx;
  14242. int ret;
  14243. dev_priv->modeset_restore_state = NULL;
  14244. if (state)
  14245. state->acquire_ctx = &ctx;
  14246. /*
  14247. * This is a cludge because with real atomic modeset mode_config.mutex
  14248. * won't be taken. Unfortunately some probed state like
  14249. * audio_codec_enable is still protected by mode_config.mutex, so lock
  14250. * it here for now.
  14251. */
  14252. mutex_lock(&dev->mode_config.mutex);
  14253. drm_modeset_acquire_init(&ctx, 0);
  14254. while (1) {
  14255. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  14256. if (ret != -EDEADLK)
  14257. break;
  14258. drm_modeset_backoff(&ctx);
  14259. }
  14260. if (!ret)
  14261. ret = __intel_display_resume(dev, state);
  14262. drm_modeset_drop_locks(&ctx);
  14263. drm_modeset_acquire_fini(&ctx);
  14264. mutex_unlock(&dev->mode_config.mutex);
  14265. if (ret)
  14266. DRM_ERROR("Restoring old state failed with %i\n", ret);
  14267. if (state)
  14268. drm_atomic_state_put(state);
  14269. }
  14270. void intel_modeset_gem_init(struct drm_device *dev)
  14271. {
  14272. struct drm_i915_private *dev_priv = to_i915(dev);
  14273. struct drm_crtc *c;
  14274. struct drm_i915_gem_object *obj;
  14275. intel_init_gt_powersave(dev_priv);
  14276. intel_modeset_init_hw(dev);
  14277. intel_setup_overlay(dev_priv);
  14278. /*
  14279. * Make sure any fbs we allocated at startup are properly
  14280. * pinned & fenced. When we do the allocation it's too early
  14281. * for this.
  14282. */
  14283. for_each_crtc(dev, c) {
  14284. struct i915_vma *vma;
  14285. obj = intel_fb_obj(c->primary->fb);
  14286. if (obj == NULL)
  14287. continue;
  14288. mutex_lock(&dev->struct_mutex);
  14289. vma = intel_pin_and_fence_fb_obj(c->primary->fb,
  14290. c->primary->state->rotation);
  14291. mutex_unlock(&dev->struct_mutex);
  14292. if (IS_ERR(vma)) {
  14293. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  14294. to_intel_crtc(c)->pipe);
  14295. drm_framebuffer_unreference(c->primary->fb);
  14296. c->primary->fb = NULL;
  14297. c->primary->crtc = c->primary->state->crtc = NULL;
  14298. update_state_fb(c->primary);
  14299. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  14300. }
  14301. }
  14302. }
  14303. int intel_connector_register(struct drm_connector *connector)
  14304. {
  14305. struct intel_connector *intel_connector = to_intel_connector(connector);
  14306. int ret;
  14307. ret = intel_backlight_device_register(intel_connector);
  14308. if (ret)
  14309. goto err;
  14310. return 0;
  14311. err:
  14312. return ret;
  14313. }
  14314. void intel_connector_unregister(struct drm_connector *connector)
  14315. {
  14316. struct intel_connector *intel_connector = to_intel_connector(connector);
  14317. intel_backlight_device_unregister(intel_connector);
  14318. intel_panel_destroy_backlight(connector);
  14319. }
  14320. void intel_modeset_cleanup(struct drm_device *dev)
  14321. {
  14322. struct drm_i915_private *dev_priv = to_i915(dev);
  14323. flush_work(&dev_priv->atomic_helper.free_work);
  14324. WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
  14325. intel_disable_gt_powersave(dev_priv);
  14326. /*
  14327. * Interrupts and polling as the first thing to avoid creating havoc.
  14328. * Too much stuff here (turning of connectors, ...) would
  14329. * experience fancy races otherwise.
  14330. */
  14331. intel_irq_uninstall(dev_priv);
  14332. /*
  14333. * Due to the hpd irq storm handling the hotplug work can re-arm the
  14334. * poll handlers. Hence disable polling after hpd handling is shut down.
  14335. */
  14336. drm_kms_helper_poll_fini(dev);
  14337. intel_unregister_dsm_handler();
  14338. intel_fbc_global_disable(dev_priv);
  14339. /* flush any delayed tasks or pending work */
  14340. flush_scheduled_work();
  14341. drm_mode_config_cleanup(dev);
  14342. intel_cleanup_overlay(dev_priv);
  14343. intel_cleanup_gt_powersave(dev_priv);
  14344. intel_teardown_gmbus(dev);
  14345. }
  14346. void intel_connector_attach_encoder(struct intel_connector *connector,
  14347. struct intel_encoder *encoder)
  14348. {
  14349. connector->encoder = encoder;
  14350. drm_mode_connector_attach_encoder(&connector->base,
  14351. &encoder->base);
  14352. }
  14353. /*
  14354. * set vga decode state - true == enable VGA decode
  14355. */
  14356. int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
  14357. {
  14358. unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  14359. u16 gmch_ctrl;
  14360. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  14361. DRM_ERROR("failed to read control word\n");
  14362. return -EIO;
  14363. }
  14364. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  14365. return 0;
  14366. if (state)
  14367. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  14368. else
  14369. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  14370. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  14371. DRM_ERROR("failed to write control word\n");
  14372. return -EIO;
  14373. }
  14374. return 0;
  14375. }
  14376. #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
  14377. struct intel_display_error_state {
  14378. u32 power_well_driver;
  14379. int num_transcoders;
  14380. struct intel_cursor_error_state {
  14381. u32 control;
  14382. u32 position;
  14383. u32 base;
  14384. u32 size;
  14385. } cursor[I915_MAX_PIPES];
  14386. struct intel_pipe_error_state {
  14387. bool power_domain_on;
  14388. u32 source;
  14389. u32 stat;
  14390. } pipe[I915_MAX_PIPES];
  14391. struct intel_plane_error_state {
  14392. u32 control;
  14393. u32 stride;
  14394. u32 size;
  14395. u32 pos;
  14396. u32 addr;
  14397. u32 surface;
  14398. u32 tile_offset;
  14399. } plane[I915_MAX_PIPES];
  14400. struct intel_transcoder_error_state {
  14401. bool power_domain_on;
  14402. enum transcoder cpu_transcoder;
  14403. u32 conf;
  14404. u32 htotal;
  14405. u32 hblank;
  14406. u32 hsync;
  14407. u32 vtotal;
  14408. u32 vblank;
  14409. u32 vsync;
  14410. } transcoder[4];
  14411. };
  14412. struct intel_display_error_state *
  14413. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  14414. {
  14415. struct intel_display_error_state *error;
  14416. int transcoders[] = {
  14417. TRANSCODER_A,
  14418. TRANSCODER_B,
  14419. TRANSCODER_C,
  14420. TRANSCODER_EDP,
  14421. };
  14422. int i;
  14423. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  14424. return NULL;
  14425. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  14426. if (error == NULL)
  14427. return NULL;
  14428. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  14429. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  14430. for_each_pipe(dev_priv, i) {
  14431. error->pipe[i].power_domain_on =
  14432. __intel_display_power_is_enabled(dev_priv,
  14433. POWER_DOMAIN_PIPE(i));
  14434. if (!error->pipe[i].power_domain_on)
  14435. continue;
  14436. error->cursor[i].control = I915_READ(CURCNTR(i));
  14437. error->cursor[i].position = I915_READ(CURPOS(i));
  14438. error->cursor[i].base = I915_READ(CURBASE(i));
  14439. error->plane[i].control = I915_READ(DSPCNTR(i));
  14440. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  14441. if (INTEL_GEN(dev_priv) <= 3) {
  14442. error->plane[i].size = I915_READ(DSPSIZE(i));
  14443. error->plane[i].pos = I915_READ(DSPPOS(i));
  14444. }
  14445. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  14446. error->plane[i].addr = I915_READ(DSPADDR(i));
  14447. if (INTEL_GEN(dev_priv) >= 4) {
  14448. error->plane[i].surface = I915_READ(DSPSURF(i));
  14449. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  14450. }
  14451. error->pipe[i].source = I915_READ(PIPESRC(i));
  14452. if (HAS_GMCH_DISPLAY(dev_priv))
  14453. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  14454. }
  14455. /* Note: this does not include DSI transcoders. */
  14456. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  14457. if (HAS_DDI(dev_priv))
  14458. error->num_transcoders++; /* Account for eDP. */
  14459. for (i = 0; i < error->num_transcoders; i++) {
  14460. enum transcoder cpu_transcoder = transcoders[i];
  14461. error->transcoder[i].power_domain_on =
  14462. __intel_display_power_is_enabled(dev_priv,
  14463. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  14464. if (!error->transcoder[i].power_domain_on)
  14465. continue;
  14466. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  14467. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  14468. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  14469. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  14470. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  14471. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  14472. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  14473. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  14474. }
  14475. return error;
  14476. }
  14477. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  14478. void
  14479. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  14480. struct drm_i915_private *dev_priv,
  14481. struct intel_display_error_state *error)
  14482. {
  14483. int i;
  14484. if (!error)
  14485. return;
  14486. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
  14487. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  14488. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  14489. error->power_well_driver);
  14490. for_each_pipe(dev_priv, i) {
  14491. err_printf(m, "Pipe [%d]:\n", i);
  14492. err_printf(m, " Power: %s\n",
  14493. onoff(error->pipe[i].power_domain_on));
  14494. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  14495. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  14496. err_printf(m, "Plane [%d]:\n", i);
  14497. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  14498. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  14499. if (INTEL_GEN(dev_priv) <= 3) {
  14500. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  14501. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  14502. }
  14503. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  14504. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  14505. if (INTEL_GEN(dev_priv) >= 4) {
  14506. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  14507. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  14508. }
  14509. err_printf(m, "Cursor [%d]:\n", i);
  14510. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  14511. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  14512. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  14513. }
  14514. for (i = 0; i < error->num_transcoders; i++) {
  14515. err_printf(m, "CPU transcoder: %s\n",
  14516. transcoder_name(error->transcoder[i].cpu_transcoder));
  14517. err_printf(m, " Power: %s\n",
  14518. onoff(error->transcoder[i].power_domain_on));
  14519. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  14520. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  14521. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  14522. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  14523. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  14524. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  14525. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  14526. }
  14527. }
  14528. #endif