intel_display.c 445 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include "intel_frontbuffer.h"
  37. #include <drm/i915_drm.h>
  38. #include "i915_drv.h"
  39. #include "i915_gem_clflush.h"
  40. #include "intel_dsi.h"
  41. #include "i915_trace.h"
  42. #include <drm/drm_atomic.h>
  43. #include <drm/drm_atomic_helper.h>
  44. #include <drm/drm_dp_helper.h>
  45. #include <drm/drm_crtc_helper.h>
  46. #include <drm/drm_plane_helper.h>
  47. #include <drm/drm_rect.h>
  48. #include <linux/dma_remapping.h>
  49. #include <linux/reservation.h>
  50. static bool is_mmio_work(struct intel_flip_work *work)
  51. {
  52. return work->mmio_work.func;
  53. }
  54. /* Primary plane formats for gen <= 3 */
  55. static const uint32_t i8xx_primary_formats[] = {
  56. DRM_FORMAT_C8,
  57. DRM_FORMAT_RGB565,
  58. DRM_FORMAT_XRGB1555,
  59. DRM_FORMAT_XRGB8888,
  60. };
  61. /* Primary plane formats for gen >= 4 */
  62. static const uint32_t i965_primary_formats[] = {
  63. DRM_FORMAT_C8,
  64. DRM_FORMAT_RGB565,
  65. DRM_FORMAT_XRGB8888,
  66. DRM_FORMAT_XBGR8888,
  67. DRM_FORMAT_XRGB2101010,
  68. DRM_FORMAT_XBGR2101010,
  69. };
  70. static const uint32_t skl_primary_formats[] = {
  71. DRM_FORMAT_C8,
  72. DRM_FORMAT_RGB565,
  73. DRM_FORMAT_XRGB8888,
  74. DRM_FORMAT_XBGR8888,
  75. DRM_FORMAT_ARGB8888,
  76. DRM_FORMAT_ABGR8888,
  77. DRM_FORMAT_XRGB2101010,
  78. DRM_FORMAT_XBGR2101010,
  79. DRM_FORMAT_YUYV,
  80. DRM_FORMAT_YVYU,
  81. DRM_FORMAT_UYVY,
  82. DRM_FORMAT_VYUY,
  83. };
  84. /* Cursor formats */
  85. static const uint32_t intel_cursor_formats[] = {
  86. DRM_FORMAT_ARGB8888,
  87. };
  88. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  89. struct intel_crtc_state *pipe_config);
  90. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  91. struct intel_crtc_state *pipe_config);
  92. static int intel_framebuffer_init(struct intel_framebuffer *ifb,
  93. struct drm_i915_gem_object *obj,
  94. struct drm_mode_fb_cmd2 *mode_cmd);
  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 intel_crtc_init_scalers(struct intel_crtc *crtc,
  111. struct intel_crtc_state *crtc_state);
  112. static void skylake_pfit_enable(struct intel_crtc *crtc);
  113. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
  114. static void ironlake_pfit_enable(struct intel_crtc *crtc);
  115. static void intel_modeset_setup_hw_state(struct drm_device *dev);
  116. static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
  117. struct intel_limit {
  118. struct {
  119. int min, max;
  120. } dot, vco, n, m, m1, m2, p, p1;
  121. struct {
  122. int dot_limit;
  123. int p2_slow, p2_fast;
  124. } p2;
  125. };
  126. /* returns HPLL frequency in kHz */
  127. int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
  128. {
  129. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  130. /* Obtain SKU information */
  131. mutex_lock(&dev_priv->sb_lock);
  132. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  133. CCK_FUSE_HPLL_FREQ_MASK;
  134. mutex_unlock(&dev_priv->sb_lock);
  135. return vco_freq[hpll_freq] * 1000;
  136. }
  137. int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
  138. const char *name, u32 reg, int ref_freq)
  139. {
  140. u32 val;
  141. int divider;
  142. mutex_lock(&dev_priv->sb_lock);
  143. val = vlv_cck_read(dev_priv, reg);
  144. mutex_unlock(&dev_priv->sb_lock);
  145. divider = val & CCK_FREQUENCY_VALUES;
  146. WARN((val & CCK_FREQUENCY_STATUS) !=
  147. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  148. "%s change in progress\n", name);
  149. return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
  150. }
  151. int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  152. const char *name, u32 reg)
  153. {
  154. if (dev_priv->hpll_freq == 0)
  155. dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
  156. return vlv_get_cck_clock(dev_priv, name, reg,
  157. dev_priv->hpll_freq);
  158. }
  159. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  160. {
  161. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  162. return;
  163. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  164. CCK_CZ_CLOCK_CONTROL);
  165. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  166. }
  167. static inline u32 /* units of 100MHz */
  168. intel_fdi_link_freq(struct drm_i915_private *dev_priv,
  169. const struct intel_crtc_state *pipe_config)
  170. {
  171. if (HAS_DDI(dev_priv))
  172. return pipe_config->port_clock; /* SPLL */
  173. else if (IS_GEN5(dev_priv))
  174. return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
  175. else
  176. return 270000;
  177. }
  178. static const struct intel_limit intel_limits_i8xx_dac = {
  179. .dot = { .min = 25000, .max = 350000 },
  180. .vco = { .min = 908000, .max = 1512000 },
  181. .n = { .min = 2, .max = 16 },
  182. .m = { .min = 96, .max = 140 },
  183. .m1 = { .min = 18, .max = 26 },
  184. .m2 = { .min = 6, .max = 16 },
  185. .p = { .min = 4, .max = 128 },
  186. .p1 = { .min = 2, .max = 33 },
  187. .p2 = { .dot_limit = 165000,
  188. .p2_slow = 4, .p2_fast = 2 },
  189. };
  190. static const struct intel_limit intel_limits_i8xx_dvo = {
  191. .dot = { .min = 25000, .max = 350000 },
  192. .vco = { .min = 908000, .max = 1512000 },
  193. .n = { .min = 2, .max = 16 },
  194. .m = { .min = 96, .max = 140 },
  195. .m1 = { .min = 18, .max = 26 },
  196. .m2 = { .min = 6, .max = 16 },
  197. .p = { .min = 4, .max = 128 },
  198. .p1 = { .min = 2, .max = 33 },
  199. .p2 = { .dot_limit = 165000,
  200. .p2_slow = 4, .p2_fast = 4 },
  201. };
  202. static const struct intel_limit intel_limits_i8xx_lvds = {
  203. .dot = { .min = 25000, .max = 350000 },
  204. .vco = { .min = 908000, .max = 1512000 },
  205. .n = { .min = 2, .max = 16 },
  206. .m = { .min = 96, .max = 140 },
  207. .m1 = { .min = 18, .max = 26 },
  208. .m2 = { .min = 6, .max = 16 },
  209. .p = { .min = 4, .max = 128 },
  210. .p1 = { .min = 1, .max = 6 },
  211. .p2 = { .dot_limit = 165000,
  212. .p2_slow = 14, .p2_fast = 7 },
  213. };
  214. static const struct intel_limit intel_limits_i9xx_sdvo = {
  215. .dot = { .min = 20000, .max = 400000 },
  216. .vco = { .min = 1400000, .max = 2800000 },
  217. .n = { .min = 1, .max = 6 },
  218. .m = { .min = 70, .max = 120 },
  219. .m1 = { .min = 8, .max = 18 },
  220. .m2 = { .min = 3, .max = 7 },
  221. .p = { .min = 5, .max = 80 },
  222. .p1 = { .min = 1, .max = 8 },
  223. .p2 = { .dot_limit = 200000,
  224. .p2_slow = 10, .p2_fast = 5 },
  225. };
  226. static const struct intel_limit intel_limits_i9xx_lvds = {
  227. .dot = { .min = 20000, .max = 400000 },
  228. .vco = { .min = 1400000, .max = 2800000 },
  229. .n = { .min = 1, .max = 6 },
  230. .m = { .min = 70, .max = 120 },
  231. .m1 = { .min = 8, .max = 18 },
  232. .m2 = { .min = 3, .max = 7 },
  233. .p = { .min = 7, .max = 98 },
  234. .p1 = { .min = 1, .max = 8 },
  235. .p2 = { .dot_limit = 112000,
  236. .p2_slow = 14, .p2_fast = 7 },
  237. };
  238. static const struct intel_limit intel_limits_g4x_sdvo = {
  239. .dot = { .min = 25000, .max = 270000 },
  240. .vco = { .min = 1750000, .max = 3500000},
  241. .n = { .min = 1, .max = 4 },
  242. .m = { .min = 104, .max = 138 },
  243. .m1 = { .min = 17, .max = 23 },
  244. .m2 = { .min = 5, .max = 11 },
  245. .p = { .min = 10, .max = 30 },
  246. .p1 = { .min = 1, .max = 3},
  247. .p2 = { .dot_limit = 270000,
  248. .p2_slow = 10,
  249. .p2_fast = 10
  250. },
  251. };
  252. static const struct intel_limit intel_limits_g4x_hdmi = {
  253. .dot = { .min = 22000, .max = 400000 },
  254. .vco = { .min = 1750000, .max = 3500000},
  255. .n = { .min = 1, .max = 4 },
  256. .m = { .min = 104, .max = 138 },
  257. .m1 = { .min = 16, .max = 23 },
  258. .m2 = { .min = 5, .max = 11 },
  259. .p = { .min = 5, .max = 80 },
  260. .p1 = { .min = 1, .max = 8},
  261. .p2 = { .dot_limit = 165000,
  262. .p2_slow = 10, .p2_fast = 5 },
  263. };
  264. static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
  265. .dot = { .min = 20000, .max = 115000 },
  266. .vco = { .min = 1750000, .max = 3500000 },
  267. .n = { .min = 1, .max = 3 },
  268. .m = { .min = 104, .max = 138 },
  269. .m1 = { .min = 17, .max = 23 },
  270. .m2 = { .min = 5, .max = 11 },
  271. .p = { .min = 28, .max = 112 },
  272. .p1 = { .min = 2, .max = 8 },
  273. .p2 = { .dot_limit = 0,
  274. .p2_slow = 14, .p2_fast = 14
  275. },
  276. };
  277. static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
  278. .dot = { .min = 80000, .max = 224000 },
  279. .vco = { .min = 1750000, .max = 3500000 },
  280. .n = { .min = 1, .max = 3 },
  281. .m = { .min = 104, .max = 138 },
  282. .m1 = { .min = 17, .max = 23 },
  283. .m2 = { .min = 5, .max = 11 },
  284. .p = { .min = 14, .max = 42 },
  285. .p1 = { .min = 2, .max = 6 },
  286. .p2 = { .dot_limit = 0,
  287. .p2_slow = 7, .p2_fast = 7
  288. },
  289. };
  290. static const struct intel_limit intel_limits_pineview_sdvo = {
  291. .dot = { .min = 20000, .max = 400000},
  292. .vco = { .min = 1700000, .max = 3500000 },
  293. /* Pineview's Ncounter is a ring counter */
  294. .n = { .min = 3, .max = 6 },
  295. .m = { .min = 2, .max = 256 },
  296. /* Pineview only has one combined m divider, which we treat as m2. */
  297. .m1 = { .min = 0, .max = 0 },
  298. .m2 = { .min = 0, .max = 254 },
  299. .p = { .min = 5, .max = 80 },
  300. .p1 = { .min = 1, .max = 8 },
  301. .p2 = { .dot_limit = 200000,
  302. .p2_slow = 10, .p2_fast = 5 },
  303. };
  304. static const struct intel_limit intel_limits_pineview_lvds = {
  305. .dot = { .min = 20000, .max = 400000 },
  306. .vco = { .min = 1700000, .max = 3500000 },
  307. .n = { .min = 3, .max = 6 },
  308. .m = { .min = 2, .max = 256 },
  309. .m1 = { .min = 0, .max = 0 },
  310. .m2 = { .min = 0, .max = 254 },
  311. .p = { .min = 7, .max = 112 },
  312. .p1 = { .min = 1, .max = 8 },
  313. .p2 = { .dot_limit = 112000,
  314. .p2_slow = 14, .p2_fast = 14 },
  315. };
  316. /* Ironlake / Sandybridge
  317. *
  318. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  319. * the range value for them is (actual_value - 2).
  320. */
  321. static const struct intel_limit intel_limits_ironlake_dac = {
  322. .dot = { .min = 25000, .max = 350000 },
  323. .vco = { .min = 1760000, .max = 3510000 },
  324. .n = { .min = 1, .max = 5 },
  325. .m = { .min = 79, .max = 127 },
  326. .m1 = { .min = 12, .max = 22 },
  327. .m2 = { .min = 5, .max = 9 },
  328. .p = { .min = 5, .max = 80 },
  329. .p1 = { .min = 1, .max = 8 },
  330. .p2 = { .dot_limit = 225000,
  331. .p2_slow = 10, .p2_fast = 5 },
  332. };
  333. static const struct intel_limit intel_limits_ironlake_single_lvds = {
  334. .dot = { .min = 25000, .max = 350000 },
  335. .vco = { .min = 1760000, .max = 3510000 },
  336. .n = { .min = 1, .max = 3 },
  337. .m = { .min = 79, .max = 118 },
  338. .m1 = { .min = 12, .max = 22 },
  339. .m2 = { .min = 5, .max = 9 },
  340. .p = { .min = 28, .max = 112 },
  341. .p1 = { .min = 2, .max = 8 },
  342. .p2 = { .dot_limit = 225000,
  343. .p2_slow = 14, .p2_fast = 14 },
  344. };
  345. static const struct intel_limit intel_limits_ironlake_dual_lvds = {
  346. .dot = { .min = 25000, .max = 350000 },
  347. .vco = { .min = 1760000, .max = 3510000 },
  348. .n = { .min = 1, .max = 3 },
  349. .m = { .min = 79, .max = 127 },
  350. .m1 = { .min = 12, .max = 22 },
  351. .m2 = { .min = 5, .max = 9 },
  352. .p = { .min = 14, .max = 56 },
  353. .p1 = { .min = 2, .max = 8 },
  354. .p2 = { .dot_limit = 225000,
  355. .p2_slow = 7, .p2_fast = 7 },
  356. };
  357. /* LVDS 100mhz refclk limits. */
  358. static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
  359. .dot = { .min = 25000, .max = 350000 },
  360. .vco = { .min = 1760000, .max = 3510000 },
  361. .n = { .min = 1, .max = 2 },
  362. .m = { .min = 79, .max = 126 },
  363. .m1 = { .min = 12, .max = 22 },
  364. .m2 = { .min = 5, .max = 9 },
  365. .p = { .min = 28, .max = 112 },
  366. .p1 = { .min = 2, .max = 8 },
  367. .p2 = { .dot_limit = 225000,
  368. .p2_slow = 14, .p2_fast = 14 },
  369. };
  370. static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
  371. .dot = { .min = 25000, .max = 350000 },
  372. .vco = { .min = 1760000, .max = 3510000 },
  373. .n = { .min = 1, .max = 3 },
  374. .m = { .min = 79, .max = 126 },
  375. .m1 = { .min = 12, .max = 22 },
  376. .m2 = { .min = 5, .max = 9 },
  377. .p = { .min = 14, .max = 42 },
  378. .p1 = { .min = 2, .max = 6 },
  379. .p2 = { .dot_limit = 225000,
  380. .p2_slow = 7, .p2_fast = 7 },
  381. };
  382. static const struct intel_limit intel_limits_vlv = {
  383. /*
  384. * These are the data rate limits (measured in fast clocks)
  385. * since those are the strictest limits we have. The fast
  386. * clock and actual rate limits are more relaxed, so checking
  387. * them would make no difference.
  388. */
  389. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  390. .vco = { .min = 4000000, .max = 6000000 },
  391. .n = { .min = 1, .max = 7 },
  392. .m1 = { .min = 2, .max = 3 },
  393. .m2 = { .min = 11, .max = 156 },
  394. .p1 = { .min = 2, .max = 3 },
  395. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  396. };
  397. static const struct intel_limit intel_limits_chv = {
  398. /*
  399. * These are the data rate limits (measured in fast clocks)
  400. * since those are the strictest limits we have. The fast
  401. * clock and actual rate limits are more relaxed, so checking
  402. * them would make no difference.
  403. */
  404. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  405. .vco = { .min = 4800000, .max = 6480000 },
  406. .n = { .min = 1, .max = 1 },
  407. .m1 = { .min = 2, .max = 2 },
  408. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  409. .p1 = { .min = 2, .max = 4 },
  410. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  411. };
  412. static const struct intel_limit intel_limits_bxt = {
  413. /* FIXME: find real dot limits */
  414. .dot = { .min = 0, .max = INT_MAX },
  415. .vco = { .min = 4800000, .max = 6700000 },
  416. .n = { .min = 1, .max = 1 },
  417. .m1 = { .min = 2, .max = 2 },
  418. /* FIXME: find real m2 limits */
  419. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  420. .p1 = { .min = 2, .max = 4 },
  421. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  422. };
  423. static bool
  424. needs_modeset(struct drm_crtc_state *state)
  425. {
  426. return drm_atomic_crtc_needs_modeset(state);
  427. }
  428. /*
  429. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  430. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  431. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  432. * The helpers' return value is the rate of the clock that is fed to the
  433. * display engine's pipe which can be the above fast dot clock rate or a
  434. * divided-down version of it.
  435. */
  436. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  437. static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
  438. {
  439. clock->m = clock->m2 + 2;
  440. clock->p = clock->p1 * clock->p2;
  441. if (WARN_ON(clock->n == 0 || clock->p == 0))
  442. return 0;
  443. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  444. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  445. return clock->dot;
  446. }
  447. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  448. {
  449. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  450. }
  451. static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
  452. {
  453. clock->m = i9xx_dpll_compute_m(clock);
  454. clock->p = clock->p1 * clock->p2;
  455. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  456. return 0;
  457. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  458. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  459. return clock->dot;
  460. }
  461. static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
  462. {
  463. clock->m = clock->m1 * clock->m2;
  464. clock->p = clock->p1 * clock->p2;
  465. if (WARN_ON(clock->n == 0 || clock->p == 0))
  466. return 0;
  467. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  468. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  469. return clock->dot / 5;
  470. }
  471. int chv_calc_dpll_params(int refclk, struct dpll *clock)
  472. {
  473. clock->m = clock->m1 * clock->m2;
  474. clock->p = clock->p1 * clock->p2;
  475. if (WARN_ON(clock->n == 0 || clock->p == 0))
  476. return 0;
  477. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  478. clock->n << 22);
  479. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  480. return clock->dot / 5;
  481. }
  482. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  483. /**
  484. * Returns whether the given set of divisors are valid for a given refclk with
  485. * the given connectors.
  486. */
  487. static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
  488. const struct intel_limit *limit,
  489. const struct dpll *clock)
  490. {
  491. if (clock->n < limit->n.min || limit->n.max < clock->n)
  492. INTELPllInvalid("n out of range\n");
  493. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  494. INTELPllInvalid("p1 out of range\n");
  495. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  496. INTELPllInvalid("m2 out of range\n");
  497. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  498. INTELPllInvalid("m1 out of range\n");
  499. if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
  500. !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
  501. if (clock->m1 <= clock->m2)
  502. INTELPllInvalid("m1 <= m2\n");
  503. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  504. !IS_GEN9_LP(dev_priv)) {
  505. if (clock->p < limit->p.min || limit->p.max < clock->p)
  506. INTELPllInvalid("p out of range\n");
  507. if (clock->m < limit->m.min || limit->m.max < clock->m)
  508. INTELPllInvalid("m out of range\n");
  509. }
  510. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  511. INTELPllInvalid("vco out of range\n");
  512. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  513. * connector, etc., rather than just a single range.
  514. */
  515. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  516. INTELPllInvalid("dot out of range\n");
  517. return true;
  518. }
  519. static int
  520. i9xx_select_p2_div(const struct intel_limit *limit,
  521. const struct intel_crtc_state *crtc_state,
  522. int target)
  523. {
  524. struct drm_device *dev = crtc_state->base.crtc->dev;
  525. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  526. /*
  527. * For LVDS just rely on its current settings for dual-channel.
  528. * We haven't figured out how to reliably set up different
  529. * single/dual channel state, if we even can.
  530. */
  531. if (intel_is_dual_link_lvds(dev))
  532. return limit->p2.p2_fast;
  533. else
  534. return limit->p2.p2_slow;
  535. } else {
  536. if (target < limit->p2.dot_limit)
  537. return limit->p2.p2_slow;
  538. else
  539. return limit->p2.p2_fast;
  540. }
  541. }
  542. /*
  543. * Returns a set of divisors for the desired target clock with the given
  544. * refclk, or FALSE. The returned values represent the clock equation:
  545. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  546. *
  547. * Target and reference clocks are specified in kHz.
  548. *
  549. * If match_clock is provided, then best_clock P divider must match the P
  550. * divider from @match_clock used for LVDS downclocking.
  551. */
  552. static bool
  553. i9xx_find_best_dpll(const struct intel_limit *limit,
  554. struct intel_crtc_state *crtc_state,
  555. int target, int refclk, struct dpll *match_clock,
  556. struct dpll *best_clock)
  557. {
  558. struct drm_device *dev = crtc_state->base.crtc->dev;
  559. struct dpll clock;
  560. int err = target;
  561. memset(best_clock, 0, sizeof(*best_clock));
  562. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  563. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  564. clock.m1++) {
  565. for (clock.m2 = limit->m2.min;
  566. clock.m2 <= limit->m2.max; clock.m2++) {
  567. if (clock.m2 >= clock.m1)
  568. break;
  569. for (clock.n = limit->n.min;
  570. clock.n <= limit->n.max; clock.n++) {
  571. for (clock.p1 = limit->p1.min;
  572. clock.p1 <= limit->p1.max; clock.p1++) {
  573. int this_err;
  574. i9xx_calc_dpll_params(refclk, &clock);
  575. if (!intel_PLL_is_valid(to_i915(dev),
  576. limit,
  577. &clock))
  578. continue;
  579. if (match_clock &&
  580. clock.p != match_clock->p)
  581. continue;
  582. this_err = abs(clock.dot - target);
  583. if (this_err < err) {
  584. *best_clock = clock;
  585. err = this_err;
  586. }
  587. }
  588. }
  589. }
  590. }
  591. return (err != target);
  592. }
  593. /*
  594. * Returns a set of divisors for the desired target clock with the given
  595. * refclk, or FALSE. The returned values represent the clock equation:
  596. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  597. *
  598. * Target and reference clocks are specified in kHz.
  599. *
  600. * If match_clock is provided, then best_clock P divider must match the P
  601. * divider from @match_clock used for LVDS downclocking.
  602. */
  603. static bool
  604. pnv_find_best_dpll(const struct intel_limit *limit,
  605. struct intel_crtc_state *crtc_state,
  606. int target, int refclk, struct dpll *match_clock,
  607. struct dpll *best_clock)
  608. {
  609. struct drm_device *dev = crtc_state->base.crtc->dev;
  610. struct dpll clock;
  611. int err = target;
  612. memset(best_clock, 0, sizeof(*best_clock));
  613. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  614. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  615. clock.m1++) {
  616. for (clock.m2 = limit->m2.min;
  617. clock.m2 <= limit->m2.max; clock.m2++) {
  618. for (clock.n = limit->n.min;
  619. clock.n <= limit->n.max; clock.n++) {
  620. for (clock.p1 = limit->p1.min;
  621. clock.p1 <= limit->p1.max; clock.p1++) {
  622. int this_err;
  623. pnv_calc_dpll_params(refclk, &clock);
  624. if (!intel_PLL_is_valid(to_i915(dev),
  625. limit,
  626. &clock))
  627. continue;
  628. if (match_clock &&
  629. clock.p != match_clock->p)
  630. continue;
  631. this_err = abs(clock.dot - target);
  632. if (this_err < err) {
  633. *best_clock = clock;
  634. err = this_err;
  635. }
  636. }
  637. }
  638. }
  639. }
  640. return (err != target);
  641. }
  642. /*
  643. * Returns a set of divisors for the desired target clock with the given
  644. * refclk, or FALSE. The returned values represent the clock equation:
  645. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  646. *
  647. * Target and reference clocks are specified in kHz.
  648. *
  649. * If match_clock is provided, then best_clock P divider must match the P
  650. * divider from @match_clock used for LVDS downclocking.
  651. */
  652. static bool
  653. g4x_find_best_dpll(const struct intel_limit *limit,
  654. struct intel_crtc_state *crtc_state,
  655. int target, int refclk, struct dpll *match_clock,
  656. struct dpll *best_clock)
  657. {
  658. struct drm_device *dev = crtc_state->base.crtc->dev;
  659. struct dpll clock;
  660. int max_n;
  661. bool found = false;
  662. /* approximately equals target * 0.00585 */
  663. int err_most = (target >> 8) + (target >> 9);
  664. memset(best_clock, 0, sizeof(*best_clock));
  665. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  666. max_n = limit->n.max;
  667. /* based on hardware requirement, prefer smaller n to precision */
  668. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  669. /* based on hardware requirement, prefere larger m1,m2 */
  670. for (clock.m1 = limit->m1.max;
  671. clock.m1 >= limit->m1.min; clock.m1--) {
  672. for (clock.m2 = limit->m2.max;
  673. clock.m2 >= limit->m2.min; clock.m2--) {
  674. for (clock.p1 = limit->p1.max;
  675. clock.p1 >= limit->p1.min; clock.p1--) {
  676. int this_err;
  677. i9xx_calc_dpll_params(refclk, &clock);
  678. if (!intel_PLL_is_valid(to_i915(dev),
  679. limit,
  680. &clock))
  681. continue;
  682. this_err = abs(clock.dot - target);
  683. if (this_err < err_most) {
  684. *best_clock = clock;
  685. err_most = this_err;
  686. max_n = clock.n;
  687. found = true;
  688. }
  689. }
  690. }
  691. }
  692. }
  693. return found;
  694. }
  695. /*
  696. * Check if the calculated PLL configuration is more optimal compared to the
  697. * best configuration and error found so far. Return the calculated error.
  698. */
  699. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  700. const struct dpll *calculated_clock,
  701. const struct dpll *best_clock,
  702. unsigned int best_error_ppm,
  703. unsigned int *error_ppm)
  704. {
  705. /*
  706. * For CHV ignore the error and consider only the P value.
  707. * Prefer a bigger P value based on HW requirements.
  708. */
  709. if (IS_CHERRYVIEW(to_i915(dev))) {
  710. *error_ppm = 0;
  711. return calculated_clock->p > best_clock->p;
  712. }
  713. if (WARN_ON_ONCE(!target_freq))
  714. return false;
  715. *error_ppm = div_u64(1000000ULL *
  716. abs(target_freq - calculated_clock->dot),
  717. target_freq);
  718. /*
  719. * Prefer a better P value over a better (smaller) error if the error
  720. * is small. Ensure this preference for future configurations too by
  721. * setting the error to 0.
  722. */
  723. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  724. *error_ppm = 0;
  725. return true;
  726. }
  727. return *error_ppm + 10 < best_error_ppm;
  728. }
  729. /*
  730. * Returns a set of divisors for the desired target clock with the given
  731. * refclk, or FALSE. The returned values represent the clock equation:
  732. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  733. */
  734. static bool
  735. vlv_find_best_dpll(const struct intel_limit *limit,
  736. struct intel_crtc_state *crtc_state,
  737. int target, int refclk, struct dpll *match_clock,
  738. struct dpll *best_clock)
  739. {
  740. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  741. struct drm_device *dev = crtc->base.dev;
  742. struct dpll clock;
  743. unsigned int bestppm = 1000000;
  744. /* min update 19.2 MHz */
  745. int max_n = min(limit->n.max, refclk / 19200);
  746. bool found = false;
  747. target *= 5; /* fast clock */
  748. memset(best_clock, 0, sizeof(*best_clock));
  749. /* based on hardware requirement, prefer smaller n to precision */
  750. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  751. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  752. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  753. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  754. clock.p = clock.p1 * clock.p2;
  755. /* based on hardware requirement, prefer bigger m1,m2 values */
  756. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  757. unsigned int ppm;
  758. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  759. refclk * clock.m1);
  760. vlv_calc_dpll_params(refclk, &clock);
  761. if (!intel_PLL_is_valid(to_i915(dev),
  762. limit,
  763. &clock))
  764. continue;
  765. if (!vlv_PLL_is_optimal(dev, target,
  766. &clock,
  767. best_clock,
  768. bestppm, &ppm))
  769. continue;
  770. *best_clock = clock;
  771. bestppm = ppm;
  772. found = true;
  773. }
  774. }
  775. }
  776. }
  777. return found;
  778. }
  779. /*
  780. * Returns a set of divisors for the desired target clock with the given
  781. * refclk, or FALSE. The returned values represent the clock equation:
  782. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  783. */
  784. static bool
  785. chv_find_best_dpll(const struct intel_limit *limit,
  786. struct intel_crtc_state *crtc_state,
  787. int target, int refclk, struct dpll *match_clock,
  788. struct dpll *best_clock)
  789. {
  790. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  791. struct drm_device *dev = crtc->base.dev;
  792. unsigned int best_error_ppm;
  793. struct dpll clock;
  794. uint64_t m2;
  795. int found = false;
  796. memset(best_clock, 0, sizeof(*best_clock));
  797. best_error_ppm = 1000000;
  798. /*
  799. * Based on hardware doc, the n always set to 1, and m1 always
  800. * set to 2. If requires to support 200Mhz refclk, we need to
  801. * revisit this because n may not 1 anymore.
  802. */
  803. clock.n = 1, clock.m1 = 2;
  804. target *= 5; /* fast clock */
  805. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  806. for (clock.p2 = limit->p2.p2_fast;
  807. clock.p2 >= limit->p2.p2_slow;
  808. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  809. unsigned int error_ppm;
  810. clock.p = clock.p1 * clock.p2;
  811. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  812. clock.n) << 22, refclk * clock.m1);
  813. if (m2 > INT_MAX/clock.m1)
  814. continue;
  815. clock.m2 = m2;
  816. chv_calc_dpll_params(refclk, &clock);
  817. if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
  818. continue;
  819. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  820. best_error_ppm, &error_ppm))
  821. continue;
  822. *best_clock = clock;
  823. best_error_ppm = error_ppm;
  824. found = true;
  825. }
  826. }
  827. return found;
  828. }
  829. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  830. struct dpll *best_clock)
  831. {
  832. int refclk = 100000;
  833. const struct intel_limit *limit = &intel_limits_bxt;
  834. return chv_find_best_dpll(limit, crtc_state,
  835. target_clock, refclk, NULL, best_clock);
  836. }
  837. bool intel_crtc_active(struct intel_crtc *crtc)
  838. {
  839. /* Be paranoid as we can arrive here with only partial
  840. * state retrieved from the hardware during setup.
  841. *
  842. * We can ditch the adjusted_mode.crtc_clock check as soon
  843. * as Haswell has gained clock readout/fastboot support.
  844. *
  845. * We can ditch the crtc->primary->fb check as soon as we can
  846. * properly reconstruct framebuffers.
  847. *
  848. * FIXME: The intel_crtc->active here should be switched to
  849. * crtc->state->active once we have proper CRTC states wired up
  850. * for atomic.
  851. */
  852. return crtc->active && crtc->base.primary->state->fb &&
  853. crtc->config->base.adjusted_mode.crtc_clock;
  854. }
  855. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  856. enum pipe pipe)
  857. {
  858. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  859. return crtc->config->cpu_transcoder;
  860. }
  861. static bool pipe_dsl_stopped(struct drm_i915_private *dev_priv, enum pipe pipe)
  862. {
  863. i915_reg_t reg = PIPEDSL(pipe);
  864. u32 line1, line2;
  865. u32 line_mask;
  866. if (IS_GEN2(dev_priv))
  867. line_mask = DSL_LINEMASK_GEN2;
  868. else
  869. line_mask = DSL_LINEMASK_GEN3;
  870. line1 = I915_READ(reg) & line_mask;
  871. msleep(5);
  872. line2 = I915_READ(reg) & line_mask;
  873. return line1 == line2;
  874. }
  875. /*
  876. * intel_wait_for_pipe_off - wait for pipe to turn off
  877. * @crtc: crtc whose pipe to wait for
  878. *
  879. * After disabling a pipe, we can't wait for vblank in the usual way,
  880. * spinning on the vblank interrupt status bit, since we won't actually
  881. * see an interrupt when the pipe is disabled.
  882. *
  883. * On Gen4 and above:
  884. * wait for the pipe register state bit to turn off
  885. *
  886. * Otherwise:
  887. * wait for the display line value to settle (it usually
  888. * ends up stopping at the start of the next frame).
  889. *
  890. */
  891. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  892. {
  893. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  894. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  895. enum pipe pipe = crtc->pipe;
  896. if (INTEL_GEN(dev_priv) >= 4) {
  897. i915_reg_t reg = PIPECONF(cpu_transcoder);
  898. /* Wait for the Pipe State to go off */
  899. if (intel_wait_for_register(dev_priv,
  900. reg, I965_PIPECONF_ACTIVE, 0,
  901. 100))
  902. WARN(1, "pipe_off wait timed out\n");
  903. } else {
  904. /* Wait for the display line to settle */
  905. if (wait_for(pipe_dsl_stopped(dev_priv, pipe), 100))
  906. WARN(1, "pipe_off wait timed out\n");
  907. }
  908. }
  909. /* Only for pre-ILK configs */
  910. void assert_pll(struct drm_i915_private *dev_priv,
  911. enum pipe pipe, bool state)
  912. {
  913. u32 val;
  914. bool cur_state;
  915. val = I915_READ(DPLL(pipe));
  916. cur_state = !!(val & DPLL_VCO_ENABLE);
  917. I915_STATE_WARN(cur_state != state,
  918. "PLL state assertion failure (expected %s, current %s)\n",
  919. onoff(state), onoff(cur_state));
  920. }
  921. /* XXX: the dsi pll is shared between MIPI DSI ports */
  922. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  923. {
  924. u32 val;
  925. bool cur_state;
  926. mutex_lock(&dev_priv->sb_lock);
  927. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  928. mutex_unlock(&dev_priv->sb_lock);
  929. cur_state = val & DSI_PLL_VCO_EN;
  930. I915_STATE_WARN(cur_state != state,
  931. "DSI PLL state assertion failure (expected %s, current %s)\n",
  932. onoff(state), onoff(cur_state));
  933. }
  934. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  935. enum pipe pipe, bool state)
  936. {
  937. bool cur_state;
  938. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  939. pipe);
  940. if (HAS_DDI(dev_priv)) {
  941. /* DDI does not have a specific FDI_TX register */
  942. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  943. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  944. } else {
  945. u32 val = I915_READ(FDI_TX_CTL(pipe));
  946. cur_state = !!(val & FDI_TX_ENABLE);
  947. }
  948. I915_STATE_WARN(cur_state != state,
  949. "FDI TX state assertion failure (expected %s, current %s)\n",
  950. onoff(state), onoff(cur_state));
  951. }
  952. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  953. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  954. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  955. enum pipe pipe, bool state)
  956. {
  957. u32 val;
  958. bool cur_state;
  959. val = I915_READ(FDI_RX_CTL(pipe));
  960. cur_state = !!(val & FDI_RX_ENABLE);
  961. I915_STATE_WARN(cur_state != state,
  962. "FDI RX state assertion failure (expected %s, current %s)\n",
  963. onoff(state), onoff(cur_state));
  964. }
  965. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  966. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  967. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  968. enum pipe pipe)
  969. {
  970. u32 val;
  971. /* ILK FDI PLL is always enabled */
  972. if (IS_GEN5(dev_priv))
  973. return;
  974. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  975. if (HAS_DDI(dev_priv))
  976. return;
  977. val = I915_READ(FDI_TX_CTL(pipe));
  978. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  979. }
  980. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  981. enum pipe pipe, bool state)
  982. {
  983. u32 val;
  984. bool cur_state;
  985. val = I915_READ(FDI_RX_CTL(pipe));
  986. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  987. I915_STATE_WARN(cur_state != state,
  988. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  989. onoff(state), onoff(cur_state));
  990. }
  991. void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
  992. {
  993. i915_reg_t pp_reg;
  994. u32 val;
  995. enum pipe panel_pipe = PIPE_A;
  996. bool locked = true;
  997. if (WARN_ON(HAS_DDI(dev_priv)))
  998. return;
  999. if (HAS_PCH_SPLIT(dev_priv)) {
  1000. u32 port_sel;
  1001. pp_reg = PP_CONTROL(0);
  1002. port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
  1003. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1004. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1005. panel_pipe = PIPE_B;
  1006. /* XXX: else fix for eDP */
  1007. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1008. /* presumably write lock depends on pipe, not port select */
  1009. pp_reg = PP_CONTROL(pipe);
  1010. panel_pipe = pipe;
  1011. } else {
  1012. pp_reg = PP_CONTROL(0);
  1013. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1014. panel_pipe = PIPE_B;
  1015. }
  1016. val = I915_READ(pp_reg);
  1017. if (!(val & PANEL_POWER_ON) ||
  1018. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1019. locked = false;
  1020. I915_STATE_WARN(panel_pipe == pipe && locked,
  1021. "panel assertion failure, pipe %c regs locked\n",
  1022. pipe_name(pipe));
  1023. }
  1024. static void assert_cursor(struct drm_i915_private *dev_priv,
  1025. enum pipe pipe, bool state)
  1026. {
  1027. bool cur_state;
  1028. if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  1029. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1030. else
  1031. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1032. I915_STATE_WARN(cur_state != state,
  1033. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1034. pipe_name(pipe), onoff(state), onoff(cur_state));
  1035. }
  1036. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1037. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1038. void assert_pipe(struct drm_i915_private *dev_priv,
  1039. enum pipe pipe, bool state)
  1040. {
  1041. bool cur_state;
  1042. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1043. pipe);
  1044. enum intel_display_power_domain power_domain;
  1045. /* if we need the pipe quirk it must be always on */
  1046. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1047. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1048. state = true;
  1049. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1050. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1051. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1052. cur_state = !!(val & PIPECONF_ENABLE);
  1053. intel_display_power_put(dev_priv, power_domain);
  1054. } else {
  1055. cur_state = false;
  1056. }
  1057. I915_STATE_WARN(cur_state != state,
  1058. "pipe %c assertion failure (expected %s, current %s)\n",
  1059. pipe_name(pipe), onoff(state), onoff(cur_state));
  1060. }
  1061. static void assert_plane(struct drm_i915_private *dev_priv,
  1062. enum plane plane, bool state)
  1063. {
  1064. u32 val;
  1065. bool cur_state;
  1066. val = I915_READ(DSPCNTR(plane));
  1067. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1068. I915_STATE_WARN(cur_state != state,
  1069. "plane %c assertion failure (expected %s, current %s)\n",
  1070. plane_name(plane), onoff(state), onoff(cur_state));
  1071. }
  1072. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1073. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1074. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1075. enum pipe pipe)
  1076. {
  1077. int i;
  1078. /* Primary planes are fixed to pipes on gen4+ */
  1079. if (INTEL_GEN(dev_priv) >= 4) {
  1080. u32 val = I915_READ(DSPCNTR(pipe));
  1081. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1082. "plane %c assertion failure, should be disabled but not\n",
  1083. plane_name(pipe));
  1084. return;
  1085. }
  1086. /* Need to check both planes against the pipe */
  1087. for_each_pipe(dev_priv, i) {
  1088. u32 val = I915_READ(DSPCNTR(i));
  1089. enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1090. DISPPLANE_SEL_PIPE_SHIFT;
  1091. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1092. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1093. plane_name(i), pipe_name(pipe));
  1094. }
  1095. }
  1096. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1097. enum pipe pipe)
  1098. {
  1099. int sprite;
  1100. if (INTEL_GEN(dev_priv) >= 9) {
  1101. for_each_sprite(dev_priv, pipe, sprite) {
  1102. u32 val = I915_READ(PLANE_CTL(pipe, sprite));
  1103. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1104. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1105. sprite, pipe_name(pipe));
  1106. }
  1107. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1108. for_each_sprite(dev_priv, pipe, sprite) {
  1109. u32 val = I915_READ(SPCNTR(pipe, PLANE_SPRITE0 + sprite));
  1110. I915_STATE_WARN(val & SP_ENABLE,
  1111. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1112. sprite_name(pipe, sprite), pipe_name(pipe));
  1113. }
  1114. } else if (INTEL_GEN(dev_priv) >= 7) {
  1115. u32 val = I915_READ(SPRCTL(pipe));
  1116. I915_STATE_WARN(val & SPRITE_ENABLE,
  1117. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1118. plane_name(pipe), pipe_name(pipe));
  1119. } else if (INTEL_GEN(dev_priv) >= 5) {
  1120. u32 val = I915_READ(DVSCNTR(pipe));
  1121. I915_STATE_WARN(val & DVS_ENABLE,
  1122. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1123. plane_name(pipe), pipe_name(pipe));
  1124. }
  1125. }
  1126. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1127. {
  1128. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1129. drm_crtc_vblank_put(crtc);
  1130. }
  1131. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1132. enum pipe pipe)
  1133. {
  1134. u32 val;
  1135. bool enabled;
  1136. val = I915_READ(PCH_TRANSCONF(pipe));
  1137. enabled = !!(val & TRANS_ENABLE);
  1138. I915_STATE_WARN(enabled,
  1139. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1140. pipe_name(pipe));
  1141. }
  1142. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1143. enum pipe pipe, u32 port_sel, u32 val)
  1144. {
  1145. if ((val & DP_PORT_EN) == 0)
  1146. return false;
  1147. if (HAS_PCH_CPT(dev_priv)) {
  1148. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1149. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1150. return false;
  1151. } else if (IS_CHERRYVIEW(dev_priv)) {
  1152. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1153. return false;
  1154. } else {
  1155. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1156. return false;
  1157. }
  1158. return true;
  1159. }
  1160. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1161. enum pipe pipe, u32 val)
  1162. {
  1163. if ((val & SDVO_ENABLE) == 0)
  1164. return false;
  1165. if (HAS_PCH_CPT(dev_priv)) {
  1166. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1167. return false;
  1168. } else if (IS_CHERRYVIEW(dev_priv)) {
  1169. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1170. return false;
  1171. } else {
  1172. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1173. return false;
  1174. }
  1175. return true;
  1176. }
  1177. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1178. enum pipe pipe, u32 val)
  1179. {
  1180. if ((val & LVDS_PORT_EN) == 0)
  1181. return false;
  1182. if (HAS_PCH_CPT(dev_priv)) {
  1183. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1184. return false;
  1185. } else {
  1186. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1187. return false;
  1188. }
  1189. return true;
  1190. }
  1191. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1192. enum pipe pipe, u32 val)
  1193. {
  1194. if ((val & ADPA_DAC_ENABLE) == 0)
  1195. return false;
  1196. if (HAS_PCH_CPT(dev_priv)) {
  1197. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1198. return false;
  1199. } else {
  1200. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1201. return false;
  1202. }
  1203. return true;
  1204. }
  1205. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1206. enum pipe pipe, i915_reg_t reg,
  1207. u32 port_sel)
  1208. {
  1209. u32 val = I915_READ(reg);
  1210. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1211. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1212. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1213. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
  1214. && (val & DP_PIPEB_SELECT),
  1215. "IBX PCH dp port still using transcoder B\n");
  1216. }
  1217. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1218. enum pipe pipe, i915_reg_t reg)
  1219. {
  1220. u32 val = I915_READ(reg);
  1221. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1222. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1223. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1224. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
  1225. && (val & SDVO_PIPE_B_SELECT),
  1226. "IBX PCH hdmi port still using transcoder B\n");
  1227. }
  1228. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1229. enum pipe pipe)
  1230. {
  1231. u32 val;
  1232. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1233. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1234. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1235. val = I915_READ(PCH_ADPA);
  1236. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1237. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1238. pipe_name(pipe));
  1239. val = I915_READ(PCH_LVDS);
  1240. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1241. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1242. pipe_name(pipe));
  1243. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1244. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1245. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1246. }
  1247. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1248. const struct intel_crtc_state *pipe_config)
  1249. {
  1250. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1251. enum pipe pipe = crtc->pipe;
  1252. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1253. POSTING_READ(DPLL(pipe));
  1254. udelay(150);
  1255. if (intel_wait_for_register(dev_priv,
  1256. DPLL(pipe),
  1257. DPLL_LOCK_VLV,
  1258. DPLL_LOCK_VLV,
  1259. 1))
  1260. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1261. }
  1262. static void vlv_enable_pll(struct intel_crtc *crtc,
  1263. const struct intel_crtc_state *pipe_config)
  1264. {
  1265. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1266. enum pipe pipe = crtc->pipe;
  1267. assert_pipe_disabled(dev_priv, pipe);
  1268. /* PLL is protected by panel, make sure we can write it */
  1269. assert_panel_unlocked(dev_priv, pipe);
  1270. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1271. _vlv_enable_pll(crtc, pipe_config);
  1272. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1273. POSTING_READ(DPLL_MD(pipe));
  1274. }
  1275. static void _chv_enable_pll(struct intel_crtc *crtc,
  1276. const struct intel_crtc_state *pipe_config)
  1277. {
  1278. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1279. enum pipe pipe = crtc->pipe;
  1280. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1281. u32 tmp;
  1282. mutex_lock(&dev_priv->sb_lock);
  1283. /* Enable back the 10bit clock to display controller */
  1284. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1285. tmp |= DPIO_DCLKP_EN;
  1286. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1287. mutex_unlock(&dev_priv->sb_lock);
  1288. /*
  1289. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1290. */
  1291. udelay(1);
  1292. /* Enable PLL */
  1293. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1294. /* Check PLL is locked */
  1295. if (intel_wait_for_register(dev_priv,
  1296. DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
  1297. 1))
  1298. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1299. }
  1300. static void chv_enable_pll(struct intel_crtc *crtc,
  1301. const struct intel_crtc_state *pipe_config)
  1302. {
  1303. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1304. enum pipe pipe = crtc->pipe;
  1305. assert_pipe_disabled(dev_priv, pipe);
  1306. /* PLL is protected by panel, make sure we can write it */
  1307. assert_panel_unlocked(dev_priv, pipe);
  1308. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1309. _chv_enable_pll(crtc, pipe_config);
  1310. if (pipe != PIPE_A) {
  1311. /*
  1312. * WaPixelRepeatModeFixForC0:chv
  1313. *
  1314. * DPLLCMD is AWOL. Use chicken bits to propagate
  1315. * the value from DPLLBMD to either pipe B or C.
  1316. */
  1317. I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
  1318. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1319. I915_WRITE(CBR4_VLV, 0);
  1320. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1321. /*
  1322. * DPLLB VGA mode also seems to cause problems.
  1323. * We should always have it disabled.
  1324. */
  1325. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1326. } else {
  1327. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1328. POSTING_READ(DPLL_MD(pipe));
  1329. }
  1330. }
  1331. static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
  1332. {
  1333. struct intel_crtc *crtc;
  1334. int count = 0;
  1335. for_each_intel_crtc(&dev_priv->drm, crtc) {
  1336. count += crtc->base.state->active &&
  1337. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
  1338. }
  1339. return count;
  1340. }
  1341. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1342. {
  1343. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1344. i915_reg_t reg = DPLL(crtc->pipe);
  1345. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1346. assert_pipe_disabled(dev_priv, crtc->pipe);
  1347. /* PLL is protected by panel, make sure we can write it */
  1348. if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
  1349. assert_panel_unlocked(dev_priv, crtc->pipe);
  1350. /* Enable DVO 2x clock on both PLLs if necessary */
  1351. if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
  1352. /*
  1353. * It appears to be important that we don't enable this
  1354. * for the current pipe before otherwise configuring the
  1355. * PLL. No idea how this should be handled if multiple
  1356. * DVO outputs are enabled simultaneosly.
  1357. */
  1358. dpll |= DPLL_DVO_2X_MODE;
  1359. I915_WRITE(DPLL(!crtc->pipe),
  1360. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1361. }
  1362. /*
  1363. * Apparently we need to have VGA mode enabled prior to changing
  1364. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1365. * dividers, even though the register value does change.
  1366. */
  1367. I915_WRITE(reg, 0);
  1368. I915_WRITE(reg, dpll);
  1369. /* Wait for the clocks to stabilize. */
  1370. POSTING_READ(reg);
  1371. udelay(150);
  1372. if (INTEL_GEN(dev_priv) >= 4) {
  1373. I915_WRITE(DPLL_MD(crtc->pipe),
  1374. crtc->config->dpll_hw_state.dpll_md);
  1375. } else {
  1376. /* The pixel multiplier can only be updated once the
  1377. * DPLL is enabled and the clocks are stable.
  1378. *
  1379. * So write it again.
  1380. */
  1381. I915_WRITE(reg, dpll);
  1382. }
  1383. /* We do this three times for luck */
  1384. I915_WRITE(reg, dpll);
  1385. POSTING_READ(reg);
  1386. udelay(150); /* wait for warmup */
  1387. I915_WRITE(reg, dpll);
  1388. POSTING_READ(reg);
  1389. udelay(150); /* wait for warmup */
  1390. I915_WRITE(reg, dpll);
  1391. POSTING_READ(reg);
  1392. udelay(150); /* wait for warmup */
  1393. }
  1394. /**
  1395. * i9xx_disable_pll - disable a PLL
  1396. * @dev_priv: i915 private structure
  1397. * @pipe: pipe PLL to disable
  1398. *
  1399. * Disable the PLL for @pipe, making sure the pipe is off first.
  1400. *
  1401. * Note! This is for pre-ILK only.
  1402. */
  1403. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1404. {
  1405. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1406. enum pipe pipe = crtc->pipe;
  1407. /* Disable DVO 2x clock on both PLLs if necessary */
  1408. if (IS_I830(dev_priv) &&
  1409. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
  1410. !intel_num_dvo_pipes(dev_priv)) {
  1411. I915_WRITE(DPLL(PIPE_B),
  1412. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1413. I915_WRITE(DPLL(PIPE_A),
  1414. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1415. }
  1416. /* Don't disable pipe or pipe PLLs if needed */
  1417. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1418. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1419. return;
  1420. /* Make sure the pipe isn't still relying on us */
  1421. assert_pipe_disabled(dev_priv, pipe);
  1422. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1423. POSTING_READ(DPLL(pipe));
  1424. }
  1425. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1426. {
  1427. u32 val;
  1428. /* Make sure the pipe isn't still relying on us */
  1429. assert_pipe_disabled(dev_priv, pipe);
  1430. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1431. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1432. if (pipe != PIPE_A)
  1433. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1434. I915_WRITE(DPLL(pipe), val);
  1435. POSTING_READ(DPLL(pipe));
  1436. }
  1437. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1438. {
  1439. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1440. u32 val;
  1441. /* Make sure the pipe isn't still relying on us */
  1442. assert_pipe_disabled(dev_priv, pipe);
  1443. val = DPLL_SSC_REF_CLK_CHV |
  1444. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1445. if (pipe != PIPE_A)
  1446. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1447. I915_WRITE(DPLL(pipe), val);
  1448. POSTING_READ(DPLL(pipe));
  1449. mutex_lock(&dev_priv->sb_lock);
  1450. /* Disable 10bit clock to display controller */
  1451. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1452. val &= ~DPIO_DCLKP_EN;
  1453. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1454. mutex_unlock(&dev_priv->sb_lock);
  1455. }
  1456. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1457. struct intel_digital_port *dport,
  1458. unsigned int expected_mask)
  1459. {
  1460. u32 port_mask;
  1461. i915_reg_t dpll_reg;
  1462. switch (dport->port) {
  1463. case PORT_B:
  1464. port_mask = DPLL_PORTB_READY_MASK;
  1465. dpll_reg = DPLL(0);
  1466. break;
  1467. case PORT_C:
  1468. port_mask = DPLL_PORTC_READY_MASK;
  1469. dpll_reg = DPLL(0);
  1470. expected_mask <<= 4;
  1471. break;
  1472. case PORT_D:
  1473. port_mask = DPLL_PORTD_READY_MASK;
  1474. dpll_reg = DPIO_PHY_STATUS;
  1475. break;
  1476. default:
  1477. BUG();
  1478. }
  1479. if (intel_wait_for_register(dev_priv,
  1480. dpll_reg, port_mask, expected_mask,
  1481. 1000))
  1482. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1483. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1484. }
  1485. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1486. enum pipe pipe)
  1487. {
  1488. struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
  1489. pipe);
  1490. i915_reg_t reg;
  1491. uint32_t val, pipeconf_val;
  1492. /* Make sure PCH DPLL is enabled */
  1493. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1494. /* FDI must be feeding us bits for PCH ports */
  1495. assert_fdi_tx_enabled(dev_priv, pipe);
  1496. assert_fdi_rx_enabled(dev_priv, pipe);
  1497. if (HAS_PCH_CPT(dev_priv)) {
  1498. /* Workaround: Set the timing override bit before enabling the
  1499. * pch transcoder. */
  1500. reg = TRANS_CHICKEN2(pipe);
  1501. val = I915_READ(reg);
  1502. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1503. I915_WRITE(reg, val);
  1504. }
  1505. reg = PCH_TRANSCONF(pipe);
  1506. val = I915_READ(reg);
  1507. pipeconf_val = I915_READ(PIPECONF(pipe));
  1508. if (HAS_PCH_IBX(dev_priv)) {
  1509. /*
  1510. * Make the BPC in transcoder be consistent with
  1511. * that in pipeconf reg. For HDMI we must use 8bpc
  1512. * here for both 8bpc and 12bpc.
  1513. */
  1514. val &= ~PIPECONF_BPC_MASK;
  1515. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
  1516. val |= PIPECONF_8BPC;
  1517. else
  1518. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1519. }
  1520. val &= ~TRANS_INTERLACE_MASK;
  1521. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1522. if (HAS_PCH_IBX(dev_priv) &&
  1523. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  1524. val |= TRANS_LEGACY_INTERLACED_ILK;
  1525. else
  1526. val |= TRANS_INTERLACED;
  1527. else
  1528. val |= TRANS_PROGRESSIVE;
  1529. I915_WRITE(reg, val | TRANS_ENABLE);
  1530. if (intel_wait_for_register(dev_priv,
  1531. reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
  1532. 100))
  1533. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1534. }
  1535. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1536. enum transcoder cpu_transcoder)
  1537. {
  1538. u32 val, pipeconf_val;
  1539. /* FDI must be feeding us bits for PCH ports */
  1540. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1541. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1542. /* Workaround: set timing override bit. */
  1543. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1544. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1545. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1546. val = TRANS_ENABLE;
  1547. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1548. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1549. PIPECONF_INTERLACED_ILK)
  1550. val |= TRANS_INTERLACED;
  1551. else
  1552. val |= TRANS_PROGRESSIVE;
  1553. I915_WRITE(LPT_TRANSCONF, val);
  1554. if (intel_wait_for_register(dev_priv,
  1555. LPT_TRANSCONF,
  1556. TRANS_STATE_ENABLE,
  1557. TRANS_STATE_ENABLE,
  1558. 100))
  1559. DRM_ERROR("Failed to enable PCH transcoder\n");
  1560. }
  1561. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1562. enum pipe pipe)
  1563. {
  1564. i915_reg_t reg;
  1565. uint32_t val;
  1566. /* FDI relies on the transcoder */
  1567. assert_fdi_tx_disabled(dev_priv, pipe);
  1568. assert_fdi_rx_disabled(dev_priv, pipe);
  1569. /* Ports must be off as well */
  1570. assert_pch_ports_disabled(dev_priv, pipe);
  1571. reg = PCH_TRANSCONF(pipe);
  1572. val = I915_READ(reg);
  1573. val &= ~TRANS_ENABLE;
  1574. I915_WRITE(reg, val);
  1575. /* wait for PCH transcoder off, transcoder state */
  1576. if (intel_wait_for_register(dev_priv,
  1577. reg, TRANS_STATE_ENABLE, 0,
  1578. 50))
  1579. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1580. if (HAS_PCH_CPT(dev_priv)) {
  1581. /* Workaround: Clear the timing override chicken bit again. */
  1582. reg = TRANS_CHICKEN2(pipe);
  1583. val = I915_READ(reg);
  1584. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1585. I915_WRITE(reg, val);
  1586. }
  1587. }
  1588. void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1589. {
  1590. u32 val;
  1591. val = I915_READ(LPT_TRANSCONF);
  1592. val &= ~TRANS_ENABLE;
  1593. I915_WRITE(LPT_TRANSCONF, val);
  1594. /* wait for PCH transcoder off, transcoder state */
  1595. if (intel_wait_for_register(dev_priv,
  1596. LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
  1597. 50))
  1598. DRM_ERROR("Failed to disable PCH transcoder\n");
  1599. /* Workaround: clear timing override bit. */
  1600. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1601. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1602. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1603. }
  1604. enum transcoder intel_crtc_pch_transcoder(struct intel_crtc *crtc)
  1605. {
  1606. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1607. WARN_ON(!crtc->config->has_pch_encoder);
  1608. if (HAS_PCH_LPT(dev_priv))
  1609. return TRANSCODER_A;
  1610. else
  1611. return (enum transcoder) crtc->pipe;
  1612. }
  1613. /**
  1614. * intel_enable_pipe - enable a pipe, asserting requirements
  1615. * @crtc: crtc responsible for the pipe
  1616. *
  1617. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1618. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1619. */
  1620. static void intel_enable_pipe(struct intel_crtc *crtc)
  1621. {
  1622. struct drm_device *dev = crtc->base.dev;
  1623. struct drm_i915_private *dev_priv = to_i915(dev);
  1624. enum pipe pipe = crtc->pipe;
  1625. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1626. i915_reg_t reg;
  1627. u32 val;
  1628. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1629. assert_planes_disabled(dev_priv, pipe);
  1630. assert_cursor_disabled(dev_priv, pipe);
  1631. assert_sprites_disabled(dev_priv, pipe);
  1632. /*
  1633. * A pipe without a PLL won't actually be able to drive bits from
  1634. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1635. * need the check.
  1636. */
  1637. if (HAS_GMCH_DISPLAY(dev_priv)) {
  1638. if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
  1639. assert_dsi_pll_enabled(dev_priv);
  1640. else
  1641. assert_pll_enabled(dev_priv, pipe);
  1642. } else {
  1643. if (crtc->config->has_pch_encoder) {
  1644. /* if driving the PCH, we need FDI enabled */
  1645. assert_fdi_rx_pll_enabled(dev_priv,
  1646. (enum pipe) intel_crtc_pch_transcoder(crtc));
  1647. assert_fdi_tx_pll_enabled(dev_priv,
  1648. (enum pipe) cpu_transcoder);
  1649. }
  1650. /* FIXME: assert CPU port conditions for SNB+ */
  1651. }
  1652. reg = PIPECONF(cpu_transcoder);
  1653. val = I915_READ(reg);
  1654. if (val & PIPECONF_ENABLE) {
  1655. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1656. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1657. return;
  1658. }
  1659. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1660. POSTING_READ(reg);
  1661. /*
  1662. * Until the pipe starts DSL will read as 0, which would cause
  1663. * an apparent vblank timestamp jump, which messes up also the
  1664. * frame count when it's derived from the timestamps. So let's
  1665. * wait for the pipe to start properly before we call
  1666. * drm_crtc_vblank_on()
  1667. */
  1668. if (dev->max_vblank_count == 0 &&
  1669. wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
  1670. DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
  1671. }
  1672. /**
  1673. * intel_disable_pipe - disable a pipe, asserting requirements
  1674. * @crtc: crtc whose pipes is to be disabled
  1675. *
  1676. * Disable the pipe of @crtc, making sure that various hardware
  1677. * specific requirements are met, if applicable, e.g. plane
  1678. * disabled, panel fitter off, etc.
  1679. *
  1680. * Will wait until the pipe has shut down before returning.
  1681. */
  1682. static void intel_disable_pipe(struct intel_crtc *crtc)
  1683. {
  1684. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1685. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1686. enum pipe pipe = crtc->pipe;
  1687. i915_reg_t reg;
  1688. u32 val;
  1689. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1690. /*
  1691. * Make sure planes won't keep trying to pump pixels to us,
  1692. * or we might hang the display.
  1693. */
  1694. assert_planes_disabled(dev_priv, pipe);
  1695. assert_cursor_disabled(dev_priv, pipe);
  1696. assert_sprites_disabled(dev_priv, pipe);
  1697. reg = PIPECONF(cpu_transcoder);
  1698. val = I915_READ(reg);
  1699. if ((val & PIPECONF_ENABLE) == 0)
  1700. return;
  1701. /*
  1702. * Double wide has implications for planes
  1703. * so best keep it disabled when not needed.
  1704. */
  1705. if (crtc->config->double_wide)
  1706. val &= ~PIPECONF_DOUBLE_WIDE;
  1707. /* Don't disable pipe or pipe PLLs if needed */
  1708. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1709. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1710. val &= ~PIPECONF_ENABLE;
  1711. I915_WRITE(reg, val);
  1712. if ((val & PIPECONF_ENABLE) == 0)
  1713. intel_wait_for_pipe_off(crtc);
  1714. }
  1715. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1716. {
  1717. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1718. }
  1719. static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
  1720. uint64_t fb_modifier, unsigned int cpp)
  1721. {
  1722. switch (fb_modifier) {
  1723. case DRM_FORMAT_MOD_NONE:
  1724. return cpp;
  1725. case I915_FORMAT_MOD_X_TILED:
  1726. if (IS_GEN2(dev_priv))
  1727. return 128;
  1728. else
  1729. return 512;
  1730. case I915_FORMAT_MOD_Y_TILED:
  1731. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1732. return 128;
  1733. else
  1734. return 512;
  1735. case I915_FORMAT_MOD_Yf_TILED:
  1736. switch (cpp) {
  1737. case 1:
  1738. return 64;
  1739. case 2:
  1740. case 4:
  1741. return 128;
  1742. case 8:
  1743. case 16:
  1744. return 256;
  1745. default:
  1746. MISSING_CASE(cpp);
  1747. return cpp;
  1748. }
  1749. break;
  1750. default:
  1751. MISSING_CASE(fb_modifier);
  1752. return cpp;
  1753. }
  1754. }
  1755. unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
  1756. uint64_t fb_modifier, unsigned int cpp)
  1757. {
  1758. if (fb_modifier == DRM_FORMAT_MOD_NONE)
  1759. return 1;
  1760. else
  1761. return intel_tile_size(dev_priv) /
  1762. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1763. }
  1764. /* Return the tile dimensions in pixel units */
  1765. static void intel_tile_dims(const struct drm_i915_private *dev_priv,
  1766. unsigned int *tile_width,
  1767. unsigned int *tile_height,
  1768. uint64_t fb_modifier,
  1769. unsigned int cpp)
  1770. {
  1771. unsigned int tile_width_bytes =
  1772. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1773. *tile_width = tile_width_bytes / cpp;
  1774. *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
  1775. }
  1776. unsigned int
  1777. intel_fb_align_height(struct drm_i915_private *dev_priv,
  1778. unsigned int height,
  1779. uint32_t pixel_format,
  1780. uint64_t fb_modifier)
  1781. {
  1782. unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
  1783. unsigned int tile_height = intel_tile_height(dev_priv, fb_modifier, cpp);
  1784. return ALIGN(height, tile_height);
  1785. }
  1786. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1787. {
  1788. unsigned int size = 0;
  1789. int i;
  1790. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1791. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1792. return size;
  1793. }
  1794. static void
  1795. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1796. const struct drm_framebuffer *fb,
  1797. unsigned int rotation)
  1798. {
  1799. view->type = I915_GGTT_VIEW_NORMAL;
  1800. if (drm_rotation_90_or_270(rotation)) {
  1801. view->type = I915_GGTT_VIEW_ROTATED;
  1802. view->rotated = to_intel_framebuffer(fb)->rot_info;
  1803. }
  1804. }
  1805. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1806. {
  1807. if (INTEL_INFO(dev_priv)->gen >= 9)
  1808. return 256 * 1024;
  1809. else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
  1810. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1811. return 128 * 1024;
  1812. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1813. return 4 * 1024;
  1814. else
  1815. return 0;
  1816. }
  1817. static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
  1818. uint64_t fb_modifier)
  1819. {
  1820. switch (fb_modifier) {
  1821. case DRM_FORMAT_MOD_NONE:
  1822. return intel_linear_alignment(dev_priv);
  1823. case I915_FORMAT_MOD_X_TILED:
  1824. if (INTEL_INFO(dev_priv)->gen >= 9)
  1825. return 256 * 1024;
  1826. return 0;
  1827. case I915_FORMAT_MOD_Y_TILED:
  1828. case I915_FORMAT_MOD_Yf_TILED:
  1829. return 1 * 1024 * 1024;
  1830. default:
  1831. MISSING_CASE(fb_modifier);
  1832. return 0;
  1833. }
  1834. }
  1835. struct i915_vma *
  1836. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1837. {
  1838. struct drm_device *dev = fb->dev;
  1839. struct drm_i915_private *dev_priv = to_i915(dev);
  1840. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1841. struct i915_ggtt_view view;
  1842. struct i915_vma *vma;
  1843. u32 alignment;
  1844. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1845. alignment = intel_surf_alignment(dev_priv, fb->modifier);
  1846. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1847. /* Note that the w/a also requires 64 PTE of padding following the
  1848. * bo. We currently fill all unused PTE with the shadow page and so
  1849. * we should always have valid PTE following the scanout preventing
  1850. * the VT-d warning.
  1851. */
  1852. if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
  1853. alignment = 256 * 1024;
  1854. /*
  1855. * Global gtt pte registers are special registers which actually forward
  1856. * writes to a chunk of system memory. Which means that there is no risk
  1857. * that the register values disappear as soon as we call
  1858. * intel_runtime_pm_put(), so it is correct to wrap only the
  1859. * pin/unpin/fence and not more.
  1860. */
  1861. intel_runtime_pm_get(dev_priv);
  1862. vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
  1863. if (IS_ERR(vma))
  1864. goto err;
  1865. if (i915_vma_is_map_and_fenceable(vma)) {
  1866. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1867. * fence, whereas 965+ only requires a fence if using
  1868. * framebuffer compression. For simplicity, we always, when
  1869. * possible, install a fence as the cost is not that onerous.
  1870. *
  1871. * If we fail to fence the tiled scanout, then either the
  1872. * modeset will reject the change (which is highly unlikely as
  1873. * the affected systems, all but one, do not have unmappable
  1874. * space) or we will not be able to enable full powersaving
  1875. * techniques (also likely not to apply due to various limits
  1876. * FBC and the like impose on the size of the buffer, which
  1877. * presumably we violated anyway with this unmappable buffer).
  1878. * Anyway, it is presumably better to stumble onwards with
  1879. * something and try to run the system in a "less than optimal"
  1880. * mode that matches the user configuration.
  1881. */
  1882. if (i915_vma_get_fence(vma) == 0)
  1883. i915_vma_pin_fence(vma);
  1884. }
  1885. i915_vma_get(vma);
  1886. err:
  1887. intel_runtime_pm_put(dev_priv);
  1888. return vma;
  1889. }
  1890. void intel_unpin_fb_vma(struct i915_vma *vma)
  1891. {
  1892. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  1893. i915_vma_unpin_fence(vma);
  1894. i915_gem_object_unpin_from_display_plane(vma);
  1895. i915_vma_put(vma);
  1896. }
  1897. static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
  1898. unsigned int rotation)
  1899. {
  1900. if (drm_rotation_90_or_270(rotation))
  1901. return to_intel_framebuffer(fb)->rotated[plane].pitch;
  1902. else
  1903. return fb->pitches[plane];
  1904. }
  1905. /*
  1906. * Convert the x/y offsets into a linear offset.
  1907. * Only valid with 0/180 degree rotation, which is fine since linear
  1908. * offset is only used with linear buffers on pre-hsw and tiled buffers
  1909. * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
  1910. */
  1911. u32 intel_fb_xy_to_linear(int x, int y,
  1912. const struct intel_plane_state *state,
  1913. int plane)
  1914. {
  1915. const struct drm_framebuffer *fb = state->base.fb;
  1916. unsigned int cpp = fb->format->cpp[plane];
  1917. unsigned int pitch = fb->pitches[plane];
  1918. return y * pitch + x * cpp;
  1919. }
  1920. /*
  1921. * Add the x/y offsets derived from fb->offsets[] to the user
  1922. * specified plane src x/y offsets. The resulting x/y offsets
  1923. * specify the start of scanout from the beginning of the gtt mapping.
  1924. */
  1925. void intel_add_fb_offsets(int *x, int *y,
  1926. const struct intel_plane_state *state,
  1927. int plane)
  1928. {
  1929. const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
  1930. unsigned int rotation = state->base.rotation;
  1931. if (drm_rotation_90_or_270(rotation)) {
  1932. *x += intel_fb->rotated[plane].x;
  1933. *y += intel_fb->rotated[plane].y;
  1934. } else {
  1935. *x += intel_fb->normal[plane].x;
  1936. *y += intel_fb->normal[plane].y;
  1937. }
  1938. }
  1939. /*
  1940. * Input tile dimensions and pitch must already be
  1941. * rotated to match x and y, and in pixel units.
  1942. */
  1943. static u32 _intel_adjust_tile_offset(int *x, int *y,
  1944. unsigned int tile_width,
  1945. unsigned int tile_height,
  1946. unsigned int tile_size,
  1947. unsigned int pitch_tiles,
  1948. u32 old_offset,
  1949. u32 new_offset)
  1950. {
  1951. unsigned int pitch_pixels = pitch_tiles * tile_width;
  1952. unsigned int tiles;
  1953. WARN_ON(old_offset & (tile_size - 1));
  1954. WARN_ON(new_offset & (tile_size - 1));
  1955. WARN_ON(new_offset > old_offset);
  1956. tiles = (old_offset - new_offset) / tile_size;
  1957. *y += tiles / pitch_tiles * tile_height;
  1958. *x += tiles % pitch_tiles * tile_width;
  1959. /* minimize x in case it got needlessly big */
  1960. *y += *x / pitch_pixels * tile_height;
  1961. *x %= pitch_pixels;
  1962. return new_offset;
  1963. }
  1964. /*
  1965. * Adjust the tile offset by moving the difference into
  1966. * the x/y offsets.
  1967. */
  1968. static u32 intel_adjust_tile_offset(int *x, int *y,
  1969. const struct intel_plane_state *state, int plane,
  1970. u32 old_offset, u32 new_offset)
  1971. {
  1972. const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
  1973. const struct drm_framebuffer *fb = state->base.fb;
  1974. unsigned int cpp = fb->format->cpp[plane];
  1975. unsigned int rotation = state->base.rotation;
  1976. unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
  1977. WARN_ON(new_offset > old_offset);
  1978. if (fb->modifier != DRM_FORMAT_MOD_NONE) {
  1979. unsigned int tile_size, tile_width, tile_height;
  1980. unsigned int pitch_tiles;
  1981. tile_size = intel_tile_size(dev_priv);
  1982. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  1983. fb->modifier, cpp);
  1984. if (drm_rotation_90_or_270(rotation)) {
  1985. pitch_tiles = pitch / tile_height;
  1986. swap(tile_width, tile_height);
  1987. } else {
  1988. pitch_tiles = pitch / (tile_width * cpp);
  1989. }
  1990. _intel_adjust_tile_offset(x, y, tile_width, tile_height,
  1991. tile_size, pitch_tiles,
  1992. old_offset, new_offset);
  1993. } else {
  1994. old_offset += *y * pitch + *x * cpp;
  1995. *y = (old_offset - new_offset) / pitch;
  1996. *x = ((old_offset - new_offset) - *y * pitch) / cpp;
  1997. }
  1998. return new_offset;
  1999. }
  2000. /*
  2001. * Computes the linear offset to the base tile and adjusts
  2002. * x, y. bytes per pixel is assumed to be a power-of-two.
  2003. *
  2004. * In the 90/270 rotated case, x and y are assumed
  2005. * to be already rotated to match the rotated GTT view, and
  2006. * pitch is the tile_height aligned framebuffer height.
  2007. *
  2008. * This function is used when computing the derived information
  2009. * under intel_framebuffer, so using any of that information
  2010. * here is not allowed. Anything under drm_framebuffer can be
  2011. * used. This is why the user has to pass in the pitch since it
  2012. * is specified in the rotated orientation.
  2013. */
  2014. static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
  2015. int *x, int *y,
  2016. const struct drm_framebuffer *fb, int plane,
  2017. unsigned int pitch,
  2018. unsigned int rotation,
  2019. u32 alignment)
  2020. {
  2021. uint64_t fb_modifier = fb->modifier;
  2022. unsigned int cpp = fb->format->cpp[plane];
  2023. u32 offset, offset_aligned;
  2024. if (alignment)
  2025. alignment--;
  2026. if (fb_modifier != DRM_FORMAT_MOD_NONE) {
  2027. unsigned int tile_size, tile_width, tile_height;
  2028. unsigned int tile_rows, tiles, pitch_tiles;
  2029. tile_size = intel_tile_size(dev_priv);
  2030. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2031. fb_modifier, cpp);
  2032. if (drm_rotation_90_or_270(rotation)) {
  2033. pitch_tiles = pitch / tile_height;
  2034. swap(tile_width, tile_height);
  2035. } else {
  2036. pitch_tiles = pitch / (tile_width * cpp);
  2037. }
  2038. tile_rows = *y / tile_height;
  2039. *y %= tile_height;
  2040. tiles = *x / tile_width;
  2041. *x %= tile_width;
  2042. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  2043. offset_aligned = offset & ~alignment;
  2044. _intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2045. tile_size, pitch_tiles,
  2046. offset, offset_aligned);
  2047. } else {
  2048. offset = *y * pitch + *x * cpp;
  2049. offset_aligned = offset & ~alignment;
  2050. *y = (offset & alignment) / pitch;
  2051. *x = ((offset & alignment) - *y * pitch) / cpp;
  2052. }
  2053. return offset_aligned;
  2054. }
  2055. u32 intel_compute_tile_offset(int *x, int *y,
  2056. const struct intel_plane_state *state,
  2057. int plane)
  2058. {
  2059. const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
  2060. const struct drm_framebuffer *fb = state->base.fb;
  2061. unsigned int rotation = state->base.rotation;
  2062. int pitch = intel_fb_pitch(fb, plane, rotation);
  2063. u32 alignment;
  2064. /* AUX_DIST needs only 4K alignment */
  2065. if (fb->format->format == DRM_FORMAT_NV12 && plane == 1)
  2066. alignment = 4096;
  2067. else
  2068. alignment = intel_surf_alignment(dev_priv, fb->modifier);
  2069. return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
  2070. rotation, alignment);
  2071. }
  2072. /* Convert the fb->offset[] linear offset into x/y offsets */
  2073. static void intel_fb_offset_to_xy(int *x, int *y,
  2074. const struct drm_framebuffer *fb, int plane)
  2075. {
  2076. unsigned int cpp = fb->format->cpp[plane];
  2077. unsigned int pitch = fb->pitches[plane];
  2078. u32 linear_offset = fb->offsets[plane];
  2079. *y = linear_offset / pitch;
  2080. *x = linear_offset % pitch / cpp;
  2081. }
  2082. static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
  2083. {
  2084. switch (fb_modifier) {
  2085. case I915_FORMAT_MOD_X_TILED:
  2086. return I915_TILING_X;
  2087. case I915_FORMAT_MOD_Y_TILED:
  2088. return I915_TILING_Y;
  2089. default:
  2090. return I915_TILING_NONE;
  2091. }
  2092. }
  2093. static int
  2094. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  2095. struct drm_framebuffer *fb)
  2096. {
  2097. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  2098. struct intel_rotation_info *rot_info = &intel_fb->rot_info;
  2099. u32 gtt_offset_rotated = 0;
  2100. unsigned int max_size = 0;
  2101. int i, num_planes = fb->format->num_planes;
  2102. unsigned int tile_size = intel_tile_size(dev_priv);
  2103. for (i = 0; i < num_planes; i++) {
  2104. unsigned int width, height;
  2105. unsigned int cpp, size;
  2106. u32 offset;
  2107. int x, y;
  2108. cpp = fb->format->cpp[i];
  2109. width = drm_framebuffer_plane_width(fb->width, fb, i);
  2110. height = drm_framebuffer_plane_height(fb->height, fb, i);
  2111. intel_fb_offset_to_xy(&x, &y, fb, i);
  2112. /*
  2113. * The fence (if used) is aligned to the start of the object
  2114. * so having the framebuffer wrap around across the edge of the
  2115. * fenced region doesn't really work. We have no API to configure
  2116. * the fence start offset within the object (nor could we probably
  2117. * on gen2/3). So it's just easier if we just require that the
  2118. * fb layout agrees with the fence layout. We already check that the
  2119. * fb stride matches the fence stride elsewhere.
  2120. */
  2121. if (i915_gem_object_is_tiled(intel_fb->obj) &&
  2122. (x + width) * cpp > fb->pitches[i]) {
  2123. DRM_DEBUG("bad fb plane %d offset: 0x%x\n",
  2124. i, fb->offsets[i]);
  2125. return -EINVAL;
  2126. }
  2127. /*
  2128. * First pixel of the framebuffer from
  2129. * the start of the normal gtt mapping.
  2130. */
  2131. intel_fb->normal[i].x = x;
  2132. intel_fb->normal[i].y = y;
  2133. offset = _intel_compute_tile_offset(dev_priv, &x, &y,
  2134. fb, 0, fb->pitches[i],
  2135. DRM_ROTATE_0, tile_size);
  2136. offset /= tile_size;
  2137. if (fb->modifier != DRM_FORMAT_MOD_NONE) {
  2138. unsigned int tile_width, tile_height;
  2139. unsigned int pitch_tiles;
  2140. struct drm_rect r;
  2141. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2142. fb->modifier, cpp);
  2143. rot_info->plane[i].offset = offset;
  2144. rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
  2145. rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
  2146. rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
  2147. intel_fb->rotated[i].pitch =
  2148. rot_info->plane[i].height * tile_height;
  2149. /* how many tiles does this plane need */
  2150. size = rot_info->plane[i].stride * rot_info->plane[i].height;
  2151. /*
  2152. * If the plane isn't horizontally tile aligned,
  2153. * we need one more tile.
  2154. */
  2155. if (x != 0)
  2156. size++;
  2157. /* rotate the x/y offsets to match the GTT view */
  2158. r.x1 = x;
  2159. r.y1 = y;
  2160. r.x2 = x + width;
  2161. r.y2 = y + height;
  2162. drm_rect_rotate(&r,
  2163. rot_info->plane[i].width * tile_width,
  2164. rot_info->plane[i].height * tile_height,
  2165. DRM_ROTATE_270);
  2166. x = r.x1;
  2167. y = r.y1;
  2168. /* rotate the tile dimensions to match the GTT view */
  2169. pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
  2170. swap(tile_width, tile_height);
  2171. /*
  2172. * We only keep the x/y offsets, so push all of the
  2173. * gtt offset into the x/y offsets.
  2174. */
  2175. _intel_adjust_tile_offset(&x, &y,
  2176. tile_width, tile_height,
  2177. tile_size, pitch_tiles,
  2178. gtt_offset_rotated * tile_size, 0);
  2179. gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
  2180. /*
  2181. * First pixel of the framebuffer from
  2182. * the start of the rotated gtt mapping.
  2183. */
  2184. intel_fb->rotated[i].x = x;
  2185. intel_fb->rotated[i].y = y;
  2186. } else {
  2187. size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
  2188. x * cpp, tile_size);
  2189. }
  2190. /* how many tiles in total needed in the bo */
  2191. max_size = max(max_size, offset + size);
  2192. }
  2193. if (max_size * tile_size > to_intel_framebuffer(fb)->obj->base.size) {
  2194. DRM_DEBUG("fb too big for bo (need %u bytes, have %zu bytes)\n",
  2195. max_size * tile_size, to_intel_framebuffer(fb)->obj->base.size);
  2196. return -EINVAL;
  2197. }
  2198. return 0;
  2199. }
  2200. static int i9xx_format_to_fourcc(int format)
  2201. {
  2202. switch (format) {
  2203. case DISPPLANE_8BPP:
  2204. return DRM_FORMAT_C8;
  2205. case DISPPLANE_BGRX555:
  2206. return DRM_FORMAT_XRGB1555;
  2207. case DISPPLANE_BGRX565:
  2208. return DRM_FORMAT_RGB565;
  2209. default:
  2210. case DISPPLANE_BGRX888:
  2211. return DRM_FORMAT_XRGB8888;
  2212. case DISPPLANE_RGBX888:
  2213. return DRM_FORMAT_XBGR8888;
  2214. case DISPPLANE_BGRX101010:
  2215. return DRM_FORMAT_XRGB2101010;
  2216. case DISPPLANE_RGBX101010:
  2217. return DRM_FORMAT_XBGR2101010;
  2218. }
  2219. }
  2220. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2221. {
  2222. switch (format) {
  2223. case PLANE_CTL_FORMAT_RGB_565:
  2224. return DRM_FORMAT_RGB565;
  2225. default:
  2226. case PLANE_CTL_FORMAT_XRGB_8888:
  2227. if (rgb_order) {
  2228. if (alpha)
  2229. return DRM_FORMAT_ABGR8888;
  2230. else
  2231. return DRM_FORMAT_XBGR8888;
  2232. } else {
  2233. if (alpha)
  2234. return DRM_FORMAT_ARGB8888;
  2235. else
  2236. return DRM_FORMAT_XRGB8888;
  2237. }
  2238. case PLANE_CTL_FORMAT_XRGB_2101010:
  2239. if (rgb_order)
  2240. return DRM_FORMAT_XBGR2101010;
  2241. else
  2242. return DRM_FORMAT_XRGB2101010;
  2243. }
  2244. }
  2245. static bool
  2246. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2247. struct intel_initial_plane_config *plane_config)
  2248. {
  2249. struct drm_device *dev = crtc->base.dev;
  2250. struct drm_i915_private *dev_priv = to_i915(dev);
  2251. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2252. struct drm_i915_gem_object *obj = NULL;
  2253. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2254. struct drm_framebuffer *fb = &plane_config->fb->base;
  2255. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2256. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2257. PAGE_SIZE);
  2258. size_aligned -= base_aligned;
  2259. if (plane_config->size == 0)
  2260. return false;
  2261. /* If the FB is too big, just don't use it since fbdev is not very
  2262. * important and we should probably use that space with FBC or other
  2263. * features. */
  2264. if (size_aligned * 2 > ggtt->stolen_usable_size)
  2265. return false;
  2266. mutex_lock(&dev->struct_mutex);
  2267. obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
  2268. base_aligned,
  2269. base_aligned,
  2270. size_aligned);
  2271. mutex_unlock(&dev->struct_mutex);
  2272. if (!obj)
  2273. return false;
  2274. if (plane_config->tiling == I915_TILING_X)
  2275. obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
  2276. mode_cmd.pixel_format = fb->format->format;
  2277. mode_cmd.width = fb->width;
  2278. mode_cmd.height = fb->height;
  2279. mode_cmd.pitches[0] = fb->pitches[0];
  2280. mode_cmd.modifier[0] = fb->modifier;
  2281. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2282. if (intel_framebuffer_init(to_intel_framebuffer(fb), obj, &mode_cmd)) {
  2283. DRM_DEBUG_KMS("intel fb init failed\n");
  2284. goto out_unref_obj;
  2285. }
  2286. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2287. return true;
  2288. out_unref_obj:
  2289. i915_gem_object_put(obj);
  2290. return false;
  2291. }
  2292. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2293. static void
  2294. update_state_fb(struct drm_plane *plane)
  2295. {
  2296. if (plane->fb == plane->state->fb)
  2297. return;
  2298. if (plane->state->fb)
  2299. drm_framebuffer_unreference(plane->state->fb);
  2300. plane->state->fb = plane->fb;
  2301. if (plane->state->fb)
  2302. drm_framebuffer_reference(plane->state->fb);
  2303. }
  2304. static void
  2305. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2306. struct intel_initial_plane_config *plane_config)
  2307. {
  2308. struct drm_device *dev = intel_crtc->base.dev;
  2309. struct drm_i915_private *dev_priv = to_i915(dev);
  2310. struct drm_crtc *c;
  2311. struct drm_i915_gem_object *obj;
  2312. struct drm_plane *primary = intel_crtc->base.primary;
  2313. struct drm_plane_state *plane_state = primary->state;
  2314. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2315. struct intel_plane *intel_plane = to_intel_plane(primary);
  2316. struct intel_plane_state *intel_state =
  2317. to_intel_plane_state(plane_state);
  2318. struct drm_framebuffer *fb;
  2319. if (!plane_config->fb)
  2320. return;
  2321. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2322. fb = &plane_config->fb->base;
  2323. goto valid_fb;
  2324. }
  2325. kfree(plane_config->fb);
  2326. /*
  2327. * Failed to alloc the obj, check to see if we should share
  2328. * an fb with another CRTC instead
  2329. */
  2330. for_each_crtc(dev, c) {
  2331. struct intel_plane_state *state;
  2332. if (c == &intel_crtc->base)
  2333. continue;
  2334. if (!to_intel_crtc(c)->active)
  2335. continue;
  2336. state = to_intel_plane_state(c->primary->state);
  2337. if (!state->vma)
  2338. continue;
  2339. if (intel_plane_ggtt_offset(state) == plane_config->base) {
  2340. fb = c->primary->fb;
  2341. drm_framebuffer_reference(fb);
  2342. goto valid_fb;
  2343. }
  2344. }
  2345. /*
  2346. * We've failed to reconstruct the BIOS FB. Current display state
  2347. * indicates that the primary plane is visible, but has a NULL FB,
  2348. * which will lead to problems later if we don't fix it up. The
  2349. * simplest solution is to just disable the primary plane now and
  2350. * pretend the BIOS never had it enabled.
  2351. */
  2352. plane_state->visible = false;
  2353. crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
  2354. intel_pre_disable_primary_noatomic(&intel_crtc->base);
  2355. intel_plane->disable_plane(primary, &intel_crtc->base);
  2356. return;
  2357. valid_fb:
  2358. mutex_lock(&dev->struct_mutex);
  2359. intel_state->vma =
  2360. intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  2361. mutex_unlock(&dev->struct_mutex);
  2362. if (IS_ERR(intel_state->vma)) {
  2363. DRM_ERROR("failed to pin boot fb on pipe %d: %li\n",
  2364. intel_crtc->pipe, PTR_ERR(intel_state->vma));
  2365. intel_state->vma = NULL;
  2366. drm_framebuffer_unreference(fb);
  2367. return;
  2368. }
  2369. plane_state->src_x = 0;
  2370. plane_state->src_y = 0;
  2371. plane_state->src_w = fb->width << 16;
  2372. plane_state->src_h = fb->height << 16;
  2373. plane_state->crtc_x = 0;
  2374. plane_state->crtc_y = 0;
  2375. plane_state->crtc_w = fb->width;
  2376. plane_state->crtc_h = fb->height;
  2377. intel_state->base.src = drm_plane_state_src(plane_state);
  2378. intel_state->base.dst = drm_plane_state_dest(plane_state);
  2379. obj = intel_fb_obj(fb);
  2380. if (i915_gem_object_is_tiled(obj))
  2381. dev_priv->preserve_bios_swizzle = true;
  2382. drm_framebuffer_reference(fb);
  2383. primary->fb = primary->state->fb = fb;
  2384. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2385. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2386. atomic_or(to_intel_plane(primary)->frontbuffer_bit,
  2387. &obj->frontbuffer_bits);
  2388. }
  2389. static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
  2390. unsigned int rotation)
  2391. {
  2392. int cpp = fb->format->cpp[plane];
  2393. switch (fb->modifier) {
  2394. case DRM_FORMAT_MOD_NONE:
  2395. case I915_FORMAT_MOD_X_TILED:
  2396. switch (cpp) {
  2397. case 8:
  2398. return 4096;
  2399. case 4:
  2400. case 2:
  2401. case 1:
  2402. return 8192;
  2403. default:
  2404. MISSING_CASE(cpp);
  2405. break;
  2406. }
  2407. break;
  2408. case I915_FORMAT_MOD_Y_TILED:
  2409. case I915_FORMAT_MOD_Yf_TILED:
  2410. switch (cpp) {
  2411. case 8:
  2412. return 2048;
  2413. case 4:
  2414. return 4096;
  2415. case 2:
  2416. case 1:
  2417. return 8192;
  2418. default:
  2419. MISSING_CASE(cpp);
  2420. break;
  2421. }
  2422. break;
  2423. default:
  2424. MISSING_CASE(fb->modifier);
  2425. }
  2426. return 2048;
  2427. }
  2428. static int skl_check_main_surface(struct intel_plane_state *plane_state)
  2429. {
  2430. const struct drm_i915_private *dev_priv = to_i915(plane_state->base.plane->dev);
  2431. const struct drm_framebuffer *fb = plane_state->base.fb;
  2432. unsigned int rotation = plane_state->base.rotation;
  2433. int x = plane_state->base.src.x1 >> 16;
  2434. int y = plane_state->base.src.y1 >> 16;
  2435. int w = drm_rect_width(&plane_state->base.src) >> 16;
  2436. int h = drm_rect_height(&plane_state->base.src) >> 16;
  2437. int max_width = skl_max_plane_width(fb, 0, rotation);
  2438. int max_height = 4096;
  2439. u32 alignment, offset, aux_offset = plane_state->aux.offset;
  2440. if (w > max_width || h > max_height) {
  2441. DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
  2442. w, h, max_width, max_height);
  2443. return -EINVAL;
  2444. }
  2445. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2446. offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
  2447. alignment = intel_surf_alignment(dev_priv, fb->modifier);
  2448. /*
  2449. * AUX surface offset is specified as the distance from the
  2450. * main surface offset, and it must be non-negative. Make
  2451. * sure that is what we will get.
  2452. */
  2453. if (offset > aux_offset)
  2454. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2455. offset, aux_offset & ~(alignment - 1));
  2456. /*
  2457. * When using an X-tiled surface, the plane blows up
  2458. * if the x offset + width exceed the stride.
  2459. *
  2460. * TODO: linear and Y-tiled seem fine, Yf untested,
  2461. */
  2462. if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
  2463. int cpp = fb->format->cpp[0];
  2464. while ((x + w) * cpp > fb->pitches[0]) {
  2465. if (offset == 0) {
  2466. DRM_DEBUG_KMS("Unable to find suitable display surface offset\n");
  2467. return -EINVAL;
  2468. }
  2469. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2470. offset, offset - alignment);
  2471. }
  2472. }
  2473. plane_state->main.offset = offset;
  2474. plane_state->main.x = x;
  2475. plane_state->main.y = y;
  2476. return 0;
  2477. }
  2478. static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
  2479. {
  2480. const struct drm_framebuffer *fb = plane_state->base.fb;
  2481. unsigned int rotation = plane_state->base.rotation;
  2482. int max_width = skl_max_plane_width(fb, 1, rotation);
  2483. int max_height = 4096;
  2484. int x = plane_state->base.src.x1 >> 17;
  2485. int y = plane_state->base.src.y1 >> 17;
  2486. int w = drm_rect_width(&plane_state->base.src) >> 17;
  2487. int h = drm_rect_height(&plane_state->base.src) >> 17;
  2488. u32 offset;
  2489. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2490. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2491. /* FIXME not quite sure how/if these apply to the chroma plane */
  2492. if (w > max_width || h > max_height) {
  2493. DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
  2494. w, h, max_width, max_height);
  2495. return -EINVAL;
  2496. }
  2497. plane_state->aux.offset = offset;
  2498. plane_state->aux.x = x;
  2499. plane_state->aux.y = y;
  2500. return 0;
  2501. }
  2502. int skl_check_plane_surface(struct intel_plane_state *plane_state)
  2503. {
  2504. const struct drm_framebuffer *fb = plane_state->base.fb;
  2505. unsigned int rotation = plane_state->base.rotation;
  2506. int ret;
  2507. if (!plane_state->base.visible)
  2508. return 0;
  2509. /* Rotate src coordinates to match rotated GTT view */
  2510. if (drm_rotation_90_or_270(rotation))
  2511. drm_rect_rotate(&plane_state->base.src,
  2512. fb->width << 16, fb->height << 16,
  2513. DRM_ROTATE_270);
  2514. /*
  2515. * Handle the AUX surface first since
  2516. * the main surface setup depends on it.
  2517. */
  2518. if (fb->format->format == DRM_FORMAT_NV12) {
  2519. ret = skl_check_nv12_aux_surface(plane_state);
  2520. if (ret)
  2521. return ret;
  2522. } else {
  2523. plane_state->aux.offset = ~0xfff;
  2524. plane_state->aux.x = 0;
  2525. plane_state->aux.y = 0;
  2526. }
  2527. ret = skl_check_main_surface(plane_state);
  2528. if (ret)
  2529. return ret;
  2530. return 0;
  2531. }
  2532. static void i9xx_update_primary_plane(struct drm_plane *primary,
  2533. const struct intel_crtc_state *crtc_state,
  2534. const struct intel_plane_state *plane_state)
  2535. {
  2536. struct drm_i915_private *dev_priv = to_i915(primary->dev);
  2537. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2538. struct drm_framebuffer *fb = plane_state->base.fb;
  2539. int plane = intel_crtc->plane;
  2540. u32 linear_offset;
  2541. u32 dspcntr;
  2542. i915_reg_t reg = DSPCNTR(plane);
  2543. unsigned int rotation = plane_state->base.rotation;
  2544. int x = plane_state->base.src.x1 >> 16;
  2545. int y = plane_state->base.src.y1 >> 16;
  2546. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2547. dspcntr |= DISPLAY_PLANE_ENABLE;
  2548. if (INTEL_GEN(dev_priv) < 4) {
  2549. if (intel_crtc->pipe == PIPE_B)
  2550. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2551. /* pipesrc and dspsize control the size that is scaled from,
  2552. * which should always be the user's requested size.
  2553. */
  2554. I915_WRITE(DSPSIZE(plane),
  2555. ((crtc_state->pipe_src_h - 1) << 16) |
  2556. (crtc_state->pipe_src_w - 1));
  2557. I915_WRITE(DSPPOS(plane), 0);
  2558. } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
  2559. I915_WRITE(PRIMSIZE(plane),
  2560. ((crtc_state->pipe_src_h - 1) << 16) |
  2561. (crtc_state->pipe_src_w - 1));
  2562. I915_WRITE(PRIMPOS(plane), 0);
  2563. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2564. }
  2565. switch (fb->format->format) {
  2566. case DRM_FORMAT_C8:
  2567. dspcntr |= DISPPLANE_8BPP;
  2568. break;
  2569. case DRM_FORMAT_XRGB1555:
  2570. dspcntr |= DISPPLANE_BGRX555;
  2571. break;
  2572. case DRM_FORMAT_RGB565:
  2573. dspcntr |= DISPPLANE_BGRX565;
  2574. break;
  2575. case DRM_FORMAT_XRGB8888:
  2576. dspcntr |= DISPPLANE_BGRX888;
  2577. break;
  2578. case DRM_FORMAT_XBGR8888:
  2579. dspcntr |= DISPPLANE_RGBX888;
  2580. break;
  2581. case DRM_FORMAT_XRGB2101010:
  2582. dspcntr |= DISPPLANE_BGRX101010;
  2583. break;
  2584. case DRM_FORMAT_XBGR2101010:
  2585. dspcntr |= DISPPLANE_RGBX101010;
  2586. break;
  2587. default:
  2588. BUG();
  2589. }
  2590. if (INTEL_GEN(dev_priv) >= 4 &&
  2591. fb->modifier == I915_FORMAT_MOD_X_TILED)
  2592. dspcntr |= DISPPLANE_TILED;
  2593. if (rotation & DRM_ROTATE_180)
  2594. dspcntr |= DISPPLANE_ROTATE_180;
  2595. if (rotation & DRM_REFLECT_X)
  2596. dspcntr |= DISPPLANE_MIRROR;
  2597. if (IS_G4X(dev_priv))
  2598. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2599. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2600. if (INTEL_GEN(dev_priv) >= 4)
  2601. intel_crtc->dspaddr_offset =
  2602. intel_compute_tile_offset(&x, &y, plane_state, 0);
  2603. if (rotation & DRM_ROTATE_180) {
  2604. x += crtc_state->pipe_src_w - 1;
  2605. y += crtc_state->pipe_src_h - 1;
  2606. } else if (rotation & DRM_REFLECT_X) {
  2607. x += crtc_state->pipe_src_w - 1;
  2608. }
  2609. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2610. if (INTEL_GEN(dev_priv) < 4)
  2611. intel_crtc->dspaddr_offset = linear_offset;
  2612. intel_crtc->adjusted_x = x;
  2613. intel_crtc->adjusted_y = y;
  2614. I915_WRITE(reg, dspcntr);
  2615. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2616. if (INTEL_GEN(dev_priv) >= 4) {
  2617. I915_WRITE(DSPSURF(plane),
  2618. intel_plane_ggtt_offset(plane_state) +
  2619. intel_crtc->dspaddr_offset);
  2620. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2621. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2622. } else {
  2623. I915_WRITE(DSPADDR(plane),
  2624. intel_plane_ggtt_offset(plane_state) +
  2625. intel_crtc->dspaddr_offset);
  2626. }
  2627. POSTING_READ(reg);
  2628. }
  2629. static void i9xx_disable_primary_plane(struct drm_plane *primary,
  2630. struct drm_crtc *crtc)
  2631. {
  2632. struct drm_device *dev = crtc->dev;
  2633. struct drm_i915_private *dev_priv = to_i915(dev);
  2634. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2635. int plane = intel_crtc->plane;
  2636. I915_WRITE(DSPCNTR(plane), 0);
  2637. if (INTEL_INFO(dev_priv)->gen >= 4)
  2638. I915_WRITE(DSPSURF(plane), 0);
  2639. else
  2640. I915_WRITE(DSPADDR(plane), 0);
  2641. POSTING_READ(DSPCNTR(plane));
  2642. }
  2643. static void ironlake_update_primary_plane(struct drm_plane *primary,
  2644. const struct intel_crtc_state *crtc_state,
  2645. const struct intel_plane_state *plane_state)
  2646. {
  2647. struct drm_device *dev = primary->dev;
  2648. struct drm_i915_private *dev_priv = to_i915(dev);
  2649. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2650. struct drm_framebuffer *fb = plane_state->base.fb;
  2651. int plane = intel_crtc->plane;
  2652. u32 linear_offset;
  2653. u32 dspcntr;
  2654. i915_reg_t reg = DSPCNTR(plane);
  2655. unsigned int rotation = plane_state->base.rotation;
  2656. int x = plane_state->base.src.x1 >> 16;
  2657. int y = plane_state->base.src.y1 >> 16;
  2658. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2659. dspcntr |= DISPLAY_PLANE_ENABLE;
  2660. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  2661. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2662. switch (fb->format->format) {
  2663. case DRM_FORMAT_C8:
  2664. dspcntr |= DISPPLANE_8BPP;
  2665. break;
  2666. case DRM_FORMAT_RGB565:
  2667. dspcntr |= DISPPLANE_BGRX565;
  2668. break;
  2669. case DRM_FORMAT_XRGB8888:
  2670. dspcntr |= DISPPLANE_BGRX888;
  2671. break;
  2672. case DRM_FORMAT_XBGR8888:
  2673. dspcntr |= DISPPLANE_RGBX888;
  2674. break;
  2675. case DRM_FORMAT_XRGB2101010:
  2676. dspcntr |= DISPPLANE_BGRX101010;
  2677. break;
  2678. case DRM_FORMAT_XBGR2101010:
  2679. dspcntr |= DISPPLANE_RGBX101010;
  2680. break;
  2681. default:
  2682. BUG();
  2683. }
  2684. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  2685. dspcntr |= DISPPLANE_TILED;
  2686. if (rotation & DRM_ROTATE_180)
  2687. dspcntr |= DISPPLANE_ROTATE_180;
  2688. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv))
  2689. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2690. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2691. intel_crtc->dspaddr_offset =
  2692. intel_compute_tile_offset(&x, &y, plane_state, 0);
  2693. /* HSW+ does this automagically in hardware */
  2694. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv) &&
  2695. rotation & DRM_ROTATE_180) {
  2696. x += crtc_state->pipe_src_w - 1;
  2697. y += crtc_state->pipe_src_h - 1;
  2698. }
  2699. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2700. intel_crtc->adjusted_x = x;
  2701. intel_crtc->adjusted_y = y;
  2702. I915_WRITE(reg, dspcntr);
  2703. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2704. I915_WRITE(DSPSURF(plane),
  2705. intel_plane_ggtt_offset(plane_state) +
  2706. intel_crtc->dspaddr_offset);
  2707. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  2708. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2709. } else {
  2710. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2711. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2712. }
  2713. POSTING_READ(reg);
  2714. }
  2715. u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
  2716. uint64_t fb_modifier, uint32_t pixel_format)
  2717. {
  2718. if (fb_modifier == DRM_FORMAT_MOD_NONE) {
  2719. return 64;
  2720. } else {
  2721. int cpp = drm_format_plane_cpp(pixel_format, 0);
  2722. return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  2723. }
  2724. }
  2725. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2726. {
  2727. struct drm_device *dev = intel_crtc->base.dev;
  2728. struct drm_i915_private *dev_priv = to_i915(dev);
  2729. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2730. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2731. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2732. }
  2733. /*
  2734. * This function detaches (aka. unbinds) unused scalers in hardware
  2735. */
  2736. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2737. {
  2738. struct intel_crtc_scaler_state *scaler_state;
  2739. int i;
  2740. scaler_state = &intel_crtc->config->scaler_state;
  2741. /* loop through and disable scalers that aren't in use */
  2742. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2743. if (!scaler_state->scalers[i].in_use)
  2744. skl_detach_scaler(intel_crtc, i);
  2745. }
  2746. }
  2747. u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
  2748. unsigned int rotation)
  2749. {
  2750. const struct drm_i915_private *dev_priv = to_i915(fb->dev);
  2751. u32 stride = intel_fb_pitch(fb, plane, rotation);
  2752. /*
  2753. * The stride is either expressed as a multiple of 64 bytes chunks for
  2754. * linear buffers or in number of tiles for tiled buffers.
  2755. */
  2756. if (drm_rotation_90_or_270(rotation)) {
  2757. int cpp = fb->format->cpp[plane];
  2758. stride /= intel_tile_height(dev_priv, fb->modifier, cpp);
  2759. } else {
  2760. stride /= intel_fb_stride_alignment(dev_priv, fb->modifier,
  2761. fb->format->format);
  2762. }
  2763. return stride;
  2764. }
  2765. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2766. {
  2767. switch (pixel_format) {
  2768. case DRM_FORMAT_C8:
  2769. return PLANE_CTL_FORMAT_INDEXED;
  2770. case DRM_FORMAT_RGB565:
  2771. return PLANE_CTL_FORMAT_RGB_565;
  2772. case DRM_FORMAT_XBGR8888:
  2773. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2774. case DRM_FORMAT_XRGB8888:
  2775. return PLANE_CTL_FORMAT_XRGB_8888;
  2776. /*
  2777. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2778. * to be already pre-multiplied. We need to add a knob (or a different
  2779. * DRM_FORMAT) for user-space to configure that.
  2780. */
  2781. case DRM_FORMAT_ABGR8888:
  2782. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2783. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2784. case DRM_FORMAT_ARGB8888:
  2785. return PLANE_CTL_FORMAT_XRGB_8888 |
  2786. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2787. case DRM_FORMAT_XRGB2101010:
  2788. return PLANE_CTL_FORMAT_XRGB_2101010;
  2789. case DRM_FORMAT_XBGR2101010:
  2790. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2791. case DRM_FORMAT_YUYV:
  2792. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2793. case DRM_FORMAT_YVYU:
  2794. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2795. case DRM_FORMAT_UYVY:
  2796. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2797. case DRM_FORMAT_VYUY:
  2798. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2799. default:
  2800. MISSING_CASE(pixel_format);
  2801. }
  2802. return 0;
  2803. }
  2804. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2805. {
  2806. switch (fb_modifier) {
  2807. case DRM_FORMAT_MOD_NONE:
  2808. break;
  2809. case I915_FORMAT_MOD_X_TILED:
  2810. return PLANE_CTL_TILED_X;
  2811. case I915_FORMAT_MOD_Y_TILED:
  2812. return PLANE_CTL_TILED_Y;
  2813. case I915_FORMAT_MOD_Yf_TILED:
  2814. return PLANE_CTL_TILED_YF;
  2815. default:
  2816. MISSING_CASE(fb_modifier);
  2817. }
  2818. return 0;
  2819. }
  2820. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2821. {
  2822. switch (rotation) {
  2823. case DRM_ROTATE_0:
  2824. break;
  2825. /*
  2826. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2827. * while i915 HW rotation is clockwise, thats why this swapping.
  2828. */
  2829. case DRM_ROTATE_90:
  2830. return PLANE_CTL_ROTATE_270;
  2831. case DRM_ROTATE_180:
  2832. return PLANE_CTL_ROTATE_180;
  2833. case DRM_ROTATE_270:
  2834. return PLANE_CTL_ROTATE_90;
  2835. default:
  2836. MISSING_CASE(rotation);
  2837. }
  2838. return 0;
  2839. }
  2840. static void skylake_update_primary_plane(struct drm_plane *plane,
  2841. const struct intel_crtc_state *crtc_state,
  2842. const struct intel_plane_state *plane_state)
  2843. {
  2844. struct drm_device *dev = plane->dev;
  2845. struct drm_i915_private *dev_priv = to_i915(dev);
  2846. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2847. struct drm_framebuffer *fb = plane_state->base.fb;
  2848. enum plane_id plane_id = to_intel_plane(plane)->id;
  2849. enum pipe pipe = to_intel_plane(plane)->pipe;
  2850. u32 plane_ctl;
  2851. unsigned int rotation = plane_state->base.rotation;
  2852. u32 stride = skl_plane_stride(fb, 0, rotation);
  2853. u32 surf_addr = plane_state->main.offset;
  2854. int scaler_id = plane_state->scaler_id;
  2855. int src_x = plane_state->main.x;
  2856. int src_y = plane_state->main.y;
  2857. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  2858. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  2859. int dst_x = plane_state->base.dst.x1;
  2860. int dst_y = plane_state->base.dst.y1;
  2861. int dst_w = drm_rect_width(&plane_state->base.dst);
  2862. int dst_h = drm_rect_height(&plane_state->base.dst);
  2863. plane_ctl = PLANE_CTL_ENABLE;
  2864. if (IS_GEMINILAKE(dev_priv)) {
  2865. I915_WRITE(PLANE_COLOR_CTL(pipe, plane_id),
  2866. PLANE_COLOR_PIPE_GAMMA_ENABLE |
  2867. PLANE_COLOR_PIPE_CSC_ENABLE |
  2868. PLANE_COLOR_PLANE_GAMMA_DISABLE);
  2869. } else {
  2870. plane_ctl |=
  2871. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2872. PLANE_CTL_PIPE_CSC_ENABLE |
  2873. PLANE_CTL_PLANE_GAMMA_DISABLE;
  2874. }
  2875. plane_ctl |= skl_plane_ctl_format(fb->format->format);
  2876. plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
  2877. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2878. /* Sizes are 0 based */
  2879. src_w--;
  2880. src_h--;
  2881. dst_w--;
  2882. dst_h--;
  2883. intel_crtc->dspaddr_offset = surf_addr;
  2884. intel_crtc->adjusted_x = src_x;
  2885. intel_crtc->adjusted_y = src_y;
  2886. I915_WRITE(PLANE_CTL(pipe, plane_id), plane_ctl);
  2887. I915_WRITE(PLANE_OFFSET(pipe, plane_id), (src_y << 16) | src_x);
  2888. I915_WRITE(PLANE_STRIDE(pipe, plane_id), stride);
  2889. I915_WRITE(PLANE_SIZE(pipe, plane_id), (src_h << 16) | src_w);
  2890. if (scaler_id >= 0) {
  2891. uint32_t ps_ctrl = 0;
  2892. WARN_ON(!dst_w || !dst_h);
  2893. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(plane_id) |
  2894. crtc_state->scaler_state.scalers[scaler_id].mode;
  2895. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2896. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2897. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2898. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2899. I915_WRITE(PLANE_POS(pipe, plane_id), 0);
  2900. } else {
  2901. I915_WRITE(PLANE_POS(pipe, plane_id), (dst_y << 16) | dst_x);
  2902. }
  2903. I915_WRITE(PLANE_SURF(pipe, plane_id),
  2904. intel_plane_ggtt_offset(plane_state) + surf_addr);
  2905. POSTING_READ(PLANE_SURF(pipe, plane_id));
  2906. }
  2907. static void skylake_disable_primary_plane(struct drm_plane *primary,
  2908. struct drm_crtc *crtc)
  2909. {
  2910. struct drm_device *dev = crtc->dev;
  2911. struct drm_i915_private *dev_priv = to_i915(dev);
  2912. enum plane_id plane_id = to_intel_plane(primary)->id;
  2913. enum pipe pipe = to_intel_plane(primary)->pipe;
  2914. I915_WRITE(PLANE_CTL(pipe, plane_id), 0);
  2915. I915_WRITE(PLANE_SURF(pipe, plane_id), 0);
  2916. POSTING_READ(PLANE_SURF(pipe, plane_id));
  2917. }
  2918. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2919. static int
  2920. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2921. int x, int y, enum mode_set_atomic state)
  2922. {
  2923. /* Support for kgdboc is disabled, this needs a major rework. */
  2924. DRM_ERROR("legacy panic handler not supported any more.\n");
  2925. return -ENODEV;
  2926. }
  2927. static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
  2928. {
  2929. struct intel_crtc *crtc;
  2930. for_each_intel_crtc(&dev_priv->drm, crtc)
  2931. intel_finish_page_flip_cs(dev_priv, crtc->pipe);
  2932. }
  2933. static void intel_update_primary_planes(struct drm_device *dev)
  2934. {
  2935. struct drm_crtc *crtc;
  2936. for_each_crtc(dev, crtc) {
  2937. struct intel_plane *plane = to_intel_plane(crtc->primary);
  2938. struct intel_plane_state *plane_state =
  2939. to_intel_plane_state(plane->base.state);
  2940. if (plane_state->base.visible)
  2941. plane->update_plane(&plane->base,
  2942. to_intel_crtc_state(crtc->state),
  2943. plane_state);
  2944. }
  2945. }
  2946. static int
  2947. __intel_display_resume(struct drm_device *dev,
  2948. struct drm_atomic_state *state)
  2949. {
  2950. struct drm_crtc_state *crtc_state;
  2951. struct drm_crtc *crtc;
  2952. int i, ret;
  2953. intel_modeset_setup_hw_state(dev);
  2954. i915_redisable_vga(to_i915(dev));
  2955. if (!state)
  2956. return 0;
  2957. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  2958. /*
  2959. * Force recalculation even if we restore
  2960. * current state. With fast modeset this may not result
  2961. * in a modeset when the state is compatible.
  2962. */
  2963. crtc_state->mode_changed = true;
  2964. }
  2965. /* ignore any reset values/BIOS leftovers in the WM registers */
  2966. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  2967. ret = drm_atomic_commit(state);
  2968. WARN_ON(ret == -EDEADLK);
  2969. return ret;
  2970. }
  2971. static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
  2972. {
  2973. return intel_has_gpu_reset(dev_priv) &&
  2974. INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
  2975. }
  2976. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  2977. {
  2978. struct drm_device *dev = &dev_priv->drm;
  2979. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  2980. struct drm_atomic_state *state;
  2981. int ret;
  2982. /*
  2983. * Need mode_config.mutex so that we don't
  2984. * trample ongoing ->detect() and whatnot.
  2985. */
  2986. mutex_lock(&dev->mode_config.mutex);
  2987. drm_modeset_acquire_init(ctx, 0);
  2988. while (1) {
  2989. ret = drm_modeset_lock_all_ctx(dev, ctx);
  2990. if (ret != -EDEADLK)
  2991. break;
  2992. drm_modeset_backoff(ctx);
  2993. }
  2994. /* reset doesn't touch the display, but flips might get nuked anyway, */
  2995. if (!i915.force_reset_modeset_test &&
  2996. !gpu_reset_clobbers_display(dev_priv))
  2997. return;
  2998. /*
  2999. * Disabling the crtcs gracefully seems nicer. Also the
  3000. * g33 docs say we should at least disable all the planes.
  3001. */
  3002. state = drm_atomic_helper_duplicate_state(dev, ctx);
  3003. if (IS_ERR(state)) {
  3004. ret = PTR_ERR(state);
  3005. DRM_ERROR("Duplicating state failed with %i\n", ret);
  3006. return;
  3007. }
  3008. ret = drm_atomic_helper_disable_all(dev, ctx);
  3009. if (ret) {
  3010. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  3011. drm_atomic_state_put(state);
  3012. return;
  3013. }
  3014. dev_priv->modeset_restore_state = state;
  3015. state->acquire_ctx = ctx;
  3016. }
  3017. void intel_finish_reset(struct drm_i915_private *dev_priv)
  3018. {
  3019. struct drm_device *dev = &dev_priv->drm;
  3020. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3021. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  3022. int ret;
  3023. /*
  3024. * Flips in the rings will be nuked by the reset,
  3025. * so complete all pending flips so that user space
  3026. * will get its events and not get stuck.
  3027. */
  3028. intel_complete_page_flips(dev_priv);
  3029. dev_priv->modeset_restore_state = NULL;
  3030. /* reset doesn't touch the display */
  3031. if (!gpu_reset_clobbers_display(dev_priv)) {
  3032. if (!state) {
  3033. /*
  3034. * Flips in the rings have been nuked by the reset,
  3035. * so update the base address of all primary
  3036. * planes to the the last fb to make sure we're
  3037. * showing the correct fb after a reset.
  3038. *
  3039. * FIXME: Atomic will make this obsolete since we won't schedule
  3040. * CS-based flips (which might get lost in gpu resets) any more.
  3041. */
  3042. intel_update_primary_planes(dev);
  3043. } else {
  3044. ret = __intel_display_resume(dev, state);
  3045. if (ret)
  3046. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3047. }
  3048. } else {
  3049. /*
  3050. * The display has been reset as well,
  3051. * so need a full re-initialization.
  3052. */
  3053. intel_runtime_pm_disable_interrupts(dev_priv);
  3054. intel_runtime_pm_enable_interrupts(dev_priv);
  3055. intel_pps_unlock_regs_wa(dev_priv);
  3056. intel_modeset_init_hw(dev);
  3057. spin_lock_irq(&dev_priv->irq_lock);
  3058. if (dev_priv->display.hpd_irq_setup)
  3059. dev_priv->display.hpd_irq_setup(dev_priv);
  3060. spin_unlock_irq(&dev_priv->irq_lock);
  3061. ret = __intel_display_resume(dev, state);
  3062. if (ret)
  3063. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3064. intel_hpd_init(dev_priv);
  3065. }
  3066. if (state)
  3067. drm_atomic_state_put(state);
  3068. drm_modeset_drop_locks(ctx);
  3069. drm_modeset_acquire_fini(ctx);
  3070. mutex_unlock(&dev->mode_config.mutex);
  3071. }
  3072. static bool abort_flip_on_reset(struct intel_crtc *crtc)
  3073. {
  3074. struct i915_gpu_error *error = &to_i915(crtc->base.dev)->gpu_error;
  3075. if (i915_reset_in_progress(error))
  3076. return true;
  3077. if (crtc->reset_count != i915_reset_count(error))
  3078. return true;
  3079. return false;
  3080. }
  3081. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  3082. {
  3083. struct drm_device *dev = crtc->dev;
  3084. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3085. bool pending;
  3086. if (abort_flip_on_reset(intel_crtc))
  3087. return false;
  3088. spin_lock_irq(&dev->event_lock);
  3089. pending = to_intel_crtc(crtc)->flip_work != NULL;
  3090. spin_unlock_irq(&dev->event_lock);
  3091. return pending;
  3092. }
  3093. static void intel_update_pipe_config(struct intel_crtc *crtc,
  3094. struct intel_crtc_state *old_crtc_state)
  3095. {
  3096. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3097. struct intel_crtc_state *pipe_config =
  3098. to_intel_crtc_state(crtc->base.state);
  3099. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  3100. crtc->base.mode = crtc->base.state->mode;
  3101. DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
  3102. old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
  3103. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  3104. /*
  3105. * Update pipe size and adjust fitter if needed: the reason for this is
  3106. * that in compute_mode_changes we check the native mode (not the pfit
  3107. * mode) to see if we can flip rather than do a full mode set. In the
  3108. * fastboot case, we'll flip, but if we don't update the pipesrc and
  3109. * pfit state, we'll end up with a big fb scanned out into the wrong
  3110. * sized surface.
  3111. */
  3112. I915_WRITE(PIPESRC(crtc->pipe),
  3113. ((pipe_config->pipe_src_w - 1) << 16) |
  3114. (pipe_config->pipe_src_h - 1));
  3115. /* on skylake this is done by detaching scalers */
  3116. if (INTEL_GEN(dev_priv) >= 9) {
  3117. skl_detach_scalers(crtc);
  3118. if (pipe_config->pch_pfit.enabled)
  3119. skylake_pfit_enable(crtc);
  3120. } else if (HAS_PCH_SPLIT(dev_priv)) {
  3121. if (pipe_config->pch_pfit.enabled)
  3122. ironlake_pfit_enable(crtc);
  3123. else if (old_crtc_state->pch_pfit.enabled)
  3124. ironlake_pfit_disable(crtc, true);
  3125. }
  3126. }
  3127. static void intel_fdi_normal_train(struct intel_crtc *crtc)
  3128. {
  3129. struct drm_device *dev = crtc->base.dev;
  3130. struct drm_i915_private *dev_priv = to_i915(dev);
  3131. int pipe = crtc->pipe;
  3132. i915_reg_t reg;
  3133. u32 temp;
  3134. /* enable normal train */
  3135. reg = FDI_TX_CTL(pipe);
  3136. temp = I915_READ(reg);
  3137. if (IS_IVYBRIDGE(dev_priv)) {
  3138. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3139. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  3140. } else {
  3141. temp &= ~FDI_LINK_TRAIN_NONE;
  3142. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  3143. }
  3144. I915_WRITE(reg, temp);
  3145. reg = FDI_RX_CTL(pipe);
  3146. temp = I915_READ(reg);
  3147. if (HAS_PCH_CPT(dev_priv)) {
  3148. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3149. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  3150. } else {
  3151. temp &= ~FDI_LINK_TRAIN_NONE;
  3152. temp |= FDI_LINK_TRAIN_NONE;
  3153. }
  3154. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  3155. /* wait one idle pattern time */
  3156. POSTING_READ(reg);
  3157. udelay(1000);
  3158. /* IVB wants error correction enabled */
  3159. if (IS_IVYBRIDGE(dev_priv))
  3160. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  3161. FDI_FE_ERRC_ENABLE);
  3162. }
  3163. /* The FDI link training functions for ILK/Ibexpeak. */
  3164. static void ironlake_fdi_link_train(struct intel_crtc *crtc,
  3165. const struct intel_crtc_state *crtc_state)
  3166. {
  3167. struct drm_device *dev = crtc->base.dev;
  3168. struct drm_i915_private *dev_priv = to_i915(dev);
  3169. int pipe = crtc->pipe;
  3170. i915_reg_t reg;
  3171. u32 temp, tries;
  3172. /* FDI needs bits from pipe first */
  3173. assert_pipe_enabled(dev_priv, pipe);
  3174. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3175. for train result */
  3176. reg = FDI_RX_IMR(pipe);
  3177. temp = I915_READ(reg);
  3178. temp &= ~FDI_RX_SYMBOL_LOCK;
  3179. temp &= ~FDI_RX_BIT_LOCK;
  3180. I915_WRITE(reg, temp);
  3181. I915_READ(reg);
  3182. udelay(150);
  3183. /* enable CPU FDI TX and PCH FDI RX */
  3184. reg = FDI_TX_CTL(pipe);
  3185. temp = I915_READ(reg);
  3186. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3187. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3188. temp &= ~FDI_LINK_TRAIN_NONE;
  3189. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3190. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3191. reg = FDI_RX_CTL(pipe);
  3192. temp = I915_READ(reg);
  3193. temp &= ~FDI_LINK_TRAIN_NONE;
  3194. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3195. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3196. POSTING_READ(reg);
  3197. udelay(150);
  3198. /* Ironlake workaround, enable clock pointer after FDI enable*/
  3199. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3200. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  3201. FDI_RX_PHASE_SYNC_POINTER_EN);
  3202. reg = FDI_RX_IIR(pipe);
  3203. for (tries = 0; tries < 5; tries++) {
  3204. temp = I915_READ(reg);
  3205. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3206. if ((temp & FDI_RX_BIT_LOCK)) {
  3207. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3208. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3209. break;
  3210. }
  3211. }
  3212. if (tries == 5)
  3213. DRM_ERROR("FDI train 1 fail!\n");
  3214. /* Train 2 */
  3215. reg = FDI_TX_CTL(pipe);
  3216. temp = I915_READ(reg);
  3217. temp &= ~FDI_LINK_TRAIN_NONE;
  3218. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3219. I915_WRITE(reg, temp);
  3220. reg = FDI_RX_CTL(pipe);
  3221. temp = I915_READ(reg);
  3222. temp &= ~FDI_LINK_TRAIN_NONE;
  3223. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3224. I915_WRITE(reg, temp);
  3225. POSTING_READ(reg);
  3226. udelay(150);
  3227. reg = FDI_RX_IIR(pipe);
  3228. for (tries = 0; tries < 5; tries++) {
  3229. temp = I915_READ(reg);
  3230. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3231. if (temp & FDI_RX_SYMBOL_LOCK) {
  3232. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3233. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3234. break;
  3235. }
  3236. }
  3237. if (tries == 5)
  3238. DRM_ERROR("FDI train 2 fail!\n");
  3239. DRM_DEBUG_KMS("FDI train done\n");
  3240. }
  3241. static const int snb_b_fdi_train_param[] = {
  3242. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3243. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3244. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3245. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3246. };
  3247. /* The FDI link training functions for SNB/Cougarpoint. */
  3248. static void gen6_fdi_link_train(struct intel_crtc *crtc,
  3249. const struct intel_crtc_state *crtc_state)
  3250. {
  3251. struct drm_device *dev = crtc->base.dev;
  3252. struct drm_i915_private *dev_priv = to_i915(dev);
  3253. int pipe = crtc->pipe;
  3254. i915_reg_t reg;
  3255. u32 temp, i, retry;
  3256. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3257. for train result */
  3258. reg = FDI_RX_IMR(pipe);
  3259. temp = I915_READ(reg);
  3260. temp &= ~FDI_RX_SYMBOL_LOCK;
  3261. temp &= ~FDI_RX_BIT_LOCK;
  3262. I915_WRITE(reg, temp);
  3263. POSTING_READ(reg);
  3264. udelay(150);
  3265. /* enable CPU FDI TX and PCH FDI RX */
  3266. reg = FDI_TX_CTL(pipe);
  3267. temp = I915_READ(reg);
  3268. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3269. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3270. temp &= ~FDI_LINK_TRAIN_NONE;
  3271. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3272. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3273. /* SNB-B */
  3274. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3275. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3276. I915_WRITE(FDI_RX_MISC(pipe),
  3277. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3278. reg = FDI_RX_CTL(pipe);
  3279. temp = I915_READ(reg);
  3280. if (HAS_PCH_CPT(dev_priv)) {
  3281. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3282. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3283. } else {
  3284. temp &= ~FDI_LINK_TRAIN_NONE;
  3285. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3286. }
  3287. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3288. POSTING_READ(reg);
  3289. udelay(150);
  3290. for (i = 0; i < 4; i++) {
  3291. reg = FDI_TX_CTL(pipe);
  3292. temp = I915_READ(reg);
  3293. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3294. temp |= snb_b_fdi_train_param[i];
  3295. I915_WRITE(reg, temp);
  3296. POSTING_READ(reg);
  3297. udelay(500);
  3298. for (retry = 0; retry < 5; retry++) {
  3299. reg = FDI_RX_IIR(pipe);
  3300. temp = I915_READ(reg);
  3301. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3302. if (temp & FDI_RX_BIT_LOCK) {
  3303. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3304. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3305. break;
  3306. }
  3307. udelay(50);
  3308. }
  3309. if (retry < 5)
  3310. break;
  3311. }
  3312. if (i == 4)
  3313. DRM_ERROR("FDI train 1 fail!\n");
  3314. /* Train 2 */
  3315. reg = FDI_TX_CTL(pipe);
  3316. temp = I915_READ(reg);
  3317. temp &= ~FDI_LINK_TRAIN_NONE;
  3318. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3319. if (IS_GEN6(dev_priv)) {
  3320. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3321. /* SNB-B */
  3322. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3323. }
  3324. I915_WRITE(reg, temp);
  3325. reg = FDI_RX_CTL(pipe);
  3326. temp = I915_READ(reg);
  3327. if (HAS_PCH_CPT(dev_priv)) {
  3328. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3329. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3330. } else {
  3331. temp &= ~FDI_LINK_TRAIN_NONE;
  3332. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3333. }
  3334. I915_WRITE(reg, temp);
  3335. POSTING_READ(reg);
  3336. udelay(150);
  3337. for (i = 0; i < 4; i++) {
  3338. reg = FDI_TX_CTL(pipe);
  3339. temp = I915_READ(reg);
  3340. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3341. temp |= snb_b_fdi_train_param[i];
  3342. I915_WRITE(reg, temp);
  3343. POSTING_READ(reg);
  3344. udelay(500);
  3345. for (retry = 0; retry < 5; retry++) {
  3346. reg = FDI_RX_IIR(pipe);
  3347. temp = I915_READ(reg);
  3348. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3349. if (temp & FDI_RX_SYMBOL_LOCK) {
  3350. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3351. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3352. break;
  3353. }
  3354. udelay(50);
  3355. }
  3356. if (retry < 5)
  3357. break;
  3358. }
  3359. if (i == 4)
  3360. DRM_ERROR("FDI train 2 fail!\n");
  3361. DRM_DEBUG_KMS("FDI train done.\n");
  3362. }
  3363. /* Manual link training for Ivy Bridge A0 parts */
  3364. static void ivb_manual_fdi_link_train(struct intel_crtc *crtc,
  3365. const struct intel_crtc_state *crtc_state)
  3366. {
  3367. struct drm_device *dev = crtc->base.dev;
  3368. struct drm_i915_private *dev_priv = to_i915(dev);
  3369. int pipe = crtc->pipe;
  3370. i915_reg_t reg;
  3371. u32 temp, i, j;
  3372. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3373. for train result */
  3374. reg = FDI_RX_IMR(pipe);
  3375. temp = I915_READ(reg);
  3376. temp &= ~FDI_RX_SYMBOL_LOCK;
  3377. temp &= ~FDI_RX_BIT_LOCK;
  3378. I915_WRITE(reg, temp);
  3379. POSTING_READ(reg);
  3380. udelay(150);
  3381. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3382. I915_READ(FDI_RX_IIR(pipe)));
  3383. /* Try each vswing and preemphasis setting twice before moving on */
  3384. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3385. /* disable first in case we need to retry */
  3386. reg = FDI_TX_CTL(pipe);
  3387. temp = I915_READ(reg);
  3388. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3389. temp &= ~FDI_TX_ENABLE;
  3390. I915_WRITE(reg, temp);
  3391. reg = FDI_RX_CTL(pipe);
  3392. temp = I915_READ(reg);
  3393. temp &= ~FDI_LINK_TRAIN_AUTO;
  3394. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3395. temp &= ~FDI_RX_ENABLE;
  3396. I915_WRITE(reg, temp);
  3397. /* enable CPU FDI TX and PCH FDI RX */
  3398. reg = FDI_TX_CTL(pipe);
  3399. temp = I915_READ(reg);
  3400. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3401. temp |= FDI_DP_PORT_WIDTH(crtc_state->fdi_lanes);
  3402. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3403. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3404. temp |= snb_b_fdi_train_param[j/2];
  3405. temp |= FDI_COMPOSITE_SYNC;
  3406. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3407. I915_WRITE(FDI_RX_MISC(pipe),
  3408. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3409. reg = FDI_RX_CTL(pipe);
  3410. temp = I915_READ(reg);
  3411. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3412. temp |= FDI_COMPOSITE_SYNC;
  3413. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3414. POSTING_READ(reg);
  3415. udelay(1); /* should be 0.5us */
  3416. for (i = 0; i < 4; i++) {
  3417. reg = FDI_RX_IIR(pipe);
  3418. temp = I915_READ(reg);
  3419. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3420. if (temp & FDI_RX_BIT_LOCK ||
  3421. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3422. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3423. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3424. i);
  3425. break;
  3426. }
  3427. udelay(1); /* should be 0.5us */
  3428. }
  3429. if (i == 4) {
  3430. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3431. continue;
  3432. }
  3433. /* Train 2 */
  3434. reg = FDI_TX_CTL(pipe);
  3435. temp = I915_READ(reg);
  3436. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3437. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3438. I915_WRITE(reg, temp);
  3439. reg = FDI_RX_CTL(pipe);
  3440. temp = I915_READ(reg);
  3441. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3442. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3443. I915_WRITE(reg, temp);
  3444. POSTING_READ(reg);
  3445. udelay(2); /* should be 1.5us */
  3446. for (i = 0; i < 4; i++) {
  3447. reg = FDI_RX_IIR(pipe);
  3448. temp = I915_READ(reg);
  3449. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3450. if (temp & FDI_RX_SYMBOL_LOCK ||
  3451. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3452. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3453. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3454. i);
  3455. goto train_done;
  3456. }
  3457. udelay(2); /* should be 1.5us */
  3458. }
  3459. if (i == 4)
  3460. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3461. }
  3462. train_done:
  3463. DRM_DEBUG_KMS("FDI train done.\n");
  3464. }
  3465. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3466. {
  3467. struct drm_device *dev = intel_crtc->base.dev;
  3468. struct drm_i915_private *dev_priv = to_i915(dev);
  3469. int pipe = intel_crtc->pipe;
  3470. i915_reg_t reg;
  3471. u32 temp;
  3472. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3473. reg = FDI_RX_CTL(pipe);
  3474. temp = I915_READ(reg);
  3475. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3476. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3477. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3478. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3479. POSTING_READ(reg);
  3480. udelay(200);
  3481. /* Switch from Rawclk to PCDclk */
  3482. temp = I915_READ(reg);
  3483. I915_WRITE(reg, temp | FDI_PCDCLK);
  3484. POSTING_READ(reg);
  3485. udelay(200);
  3486. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3487. reg = FDI_TX_CTL(pipe);
  3488. temp = I915_READ(reg);
  3489. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3490. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3491. POSTING_READ(reg);
  3492. udelay(100);
  3493. }
  3494. }
  3495. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3496. {
  3497. struct drm_device *dev = intel_crtc->base.dev;
  3498. struct drm_i915_private *dev_priv = to_i915(dev);
  3499. int pipe = intel_crtc->pipe;
  3500. i915_reg_t reg;
  3501. u32 temp;
  3502. /* Switch from PCDclk to Rawclk */
  3503. reg = FDI_RX_CTL(pipe);
  3504. temp = I915_READ(reg);
  3505. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3506. /* Disable CPU FDI TX PLL */
  3507. reg = FDI_TX_CTL(pipe);
  3508. temp = I915_READ(reg);
  3509. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3510. POSTING_READ(reg);
  3511. udelay(100);
  3512. reg = FDI_RX_CTL(pipe);
  3513. temp = I915_READ(reg);
  3514. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3515. /* Wait for the clocks to turn off. */
  3516. POSTING_READ(reg);
  3517. udelay(100);
  3518. }
  3519. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3520. {
  3521. struct drm_device *dev = crtc->dev;
  3522. struct drm_i915_private *dev_priv = to_i915(dev);
  3523. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3524. int pipe = intel_crtc->pipe;
  3525. i915_reg_t reg;
  3526. u32 temp;
  3527. /* disable CPU FDI tx and PCH FDI rx */
  3528. reg = FDI_TX_CTL(pipe);
  3529. temp = I915_READ(reg);
  3530. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3531. POSTING_READ(reg);
  3532. reg = FDI_RX_CTL(pipe);
  3533. temp = I915_READ(reg);
  3534. temp &= ~(0x7 << 16);
  3535. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3536. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3537. POSTING_READ(reg);
  3538. udelay(100);
  3539. /* Ironlake workaround, disable clock pointer after downing FDI */
  3540. if (HAS_PCH_IBX(dev_priv))
  3541. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3542. /* still set train pattern 1 */
  3543. reg = FDI_TX_CTL(pipe);
  3544. temp = I915_READ(reg);
  3545. temp &= ~FDI_LINK_TRAIN_NONE;
  3546. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3547. I915_WRITE(reg, temp);
  3548. reg = FDI_RX_CTL(pipe);
  3549. temp = I915_READ(reg);
  3550. if (HAS_PCH_CPT(dev_priv)) {
  3551. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3552. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3553. } else {
  3554. temp &= ~FDI_LINK_TRAIN_NONE;
  3555. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3556. }
  3557. /* BPC in FDI rx is consistent with that in PIPECONF */
  3558. temp &= ~(0x07 << 16);
  3559. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3560. I915_WRITE(reg, temp);
  3561. POSTING_READ(reg);
  3562. udelay(100);
  3563. }
  3564. bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
  3565. {
  3566. struct intel_crtc *crtc;
  3567. /* Note that we don't need to be called with mode_config.lock here
  3568. * as our list of CRTC objects is static for the lifetime of the
  3569. * device and so cannot disappear as we iterate. Similarly, we can
  3570. * happily treat the predicates as racy, atomic checks as userspace
  3571. * cannot claim and pin a new fb without at least acquring the
  3572. * struct_mutex and so serialising with us.
  3573. */
  3574. for_each_intel_crtc(&dev_priv->drm, crtc) {
  3575. if (atomic_read(&crtc->unpin_work_count) == 0)
  3576. continue;
  3577. if (crtc->flip_work)
  3578. intel_wait_for_vblank(dev_priv, crtc->pipe);
  3579. return true;
  3580. }
  3581. return false;
  3582. }
  3583. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3584. {
  3585. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3586. struct intel_flip_work *work = intel_crtc->flip_work;
  3587. intel_crtc->flip_work = NULL;
  3588. if (work->event)
  3589. drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
  3590. drm_crtc_vblank_put(&intel_crtc->base);
  3591. wake_up_all(&dev_priv->pending_flip_queue);
  3592. trace_i915_flip_complete(intel_crtc->plane,
  3593. work->pending_flip_obj);
  3594. queue_work(dev_priv->wq, &work->unpin_work);
  3595. }
  3596. static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3597. {
  3598. struct drm_device *dev = crtc->dev;
  3599. struct drm_i915_private *dev_priv = to_i915(dev);
  3600. long ret;
  3601. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3602. ret = wait_event_interruptible_timeout(
  3603. dev_priv->pending_flip_queue,
  3604. !intel_crtc_has_pending_flip(crtc),
  3605. 60*HZ);
  3606. if (ret < 0)
  3607. return ret;
  3608. if (ret == 0) {
  3609. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3610. struct intel_flip_work *work;
  3611. spin_lock_irq(&dev->event_lock);
  3612. work = intel_crtc->flip_work;
  3613. if (work && !is_mmio_work(work)) {
  3614. WARN_ONCE(1, "Removing stuck page flip\n");
  3615. page_flip_completed(intel_crtc);
  3616. }
  3617. spin_unlock_irq(&dev->event_lock);
  3618. }
  3619. return 0;
  3620. }
  3621. void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3622. {
  3623. u32 temp;
  3624. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3625. mutex_lock(&dev_priv->sb_lock);
  3626. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3627. temp |= SBI_SSCCTL_DISABLE;
  3628. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3629. mutex_unlock(&dev_priv->sb_lock);
  3630. }
  3631. /* Program iCLKIP clock to the desired frequency */
  3632. static void lpt_program_iclkip(struct intel_crtc *crtc)
  3633. {
  3634. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3635. int clock = crtc->config->base.adjusted_mode.crtc_clock;
  3636. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3637. u32 temp;
  3638. lpt_disable_iclkip(dev_priv);
  3639. /* The iCLK virtual clock root frequency is in MHz,
  3640. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3641. * divisors, it is necessary to divide one by another, so we
  3642. * convert the virtual clock precision to KHz here for higher
  3643. * precision.
  3644. */
  3645. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3646. u32 iclk_virtual_root_freq = 172800 * 1000;
  3647. u32 iclk_pi_range = 64;
  3648. u32 desired_divisor;
  3649. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3650. clock << auxdiv);
  3651. divsel = (desired_divisor / iclk_pi_range) - 2;
  3652. phaseinc = desired_divisor % iclk_pi_range;
  3653. /*
  3654. * Near 20MHz is a corner case which is
  3655. * out of range for the 7-bit divisor
  3656. */
  3657. if (divsel <= 0x7f)
  3658. break;
  3659. }
  3660. /* This should not happen with any sane values */
  3661. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3662. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3663. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3664. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3665. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3666. clock,
  3667. auxdiv,
  3668. divsel,
  3669. phasedir,
  3670. phaseinc);
  3671. mutex_lock(&dev_priv->sb_lock);
  3672. /* Program SSCDIVINTPHASE6 */
  3673. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3674. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3675. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3676. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3677. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3678. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3679. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3680. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3681. /* Program SSCAUXDIV */
  3682. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3683. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3684. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3685. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3686. /* Enable modulator and associated divider */
  3687. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3688. temp &= ~SBI_SSCCTL_DISABLE;
  3689. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3690. mutex_unlock(&dev_priv->sb_lock);
  3691. /* Wait for initialization time */
  3692. udelay(24);
  3693. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3694. }
  3695. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3696. {
  3697. u32 divsel, phaseinc, auxdiv;
  3698. u32 iclk_virtual_root_freq = 172800 * 1000;
  3699. u32 iclk_pi_range = 64;
  3700. u32 desired_divisor;
  3701. u32 temp;
  3702. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3703. return 0;
  3704. mutex_lock(&dev_priv->sb_lock);
  3705. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3706. if (temp & SBI_SSCCTL_DISABLE) {
  3707. mutex_unlock(&dev_priv->sb_lock);
  3708. return 0;
  3709. }
  3710. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3711. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3712. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3713. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3714. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3715. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3716. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3717. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3718. mutex_unlock(&dev_priv->sb_lock);
  3719. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3720. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3721. desired_divisor << auxdiv);
  3722. }
  3723. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3724. enum pipe pch_transcoder)
  3725. {
  3726. struct drm_device *dev = crtc->base.dev;
  3727. struct drm_i915_private *dev_priv = to_i915(dev);
  3728. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3729. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3730. I915_READ(HTOTAL(cpu_transcoder)));
  3731. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3732. I915_READ(HBLANK(cpu_transcoder)));
  3733. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3734. I915_READ(HSYNC(cpu_transcoder)));
  3735. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3736. I915_READ(VTOTAL(cpu_transcoder)));
  3737. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3738. I915_READ(VBLANK(cpu_transcoder)));
  3739. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3740. I915_READ(VSYNC(cpu_transcoder)));
  3741. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3742. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3743. }
  3744. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3745. {
  3746. struct drm_i915_private *dev_priv = to_i915(dev);
  3747. uint32_t temp;
  3748. temp = I915_READ(SOUTH_CHICKEN1);
  3749. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3750. return;
  3751. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3752. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3753. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3754. if (enable)
  3755. temp |= FDI_BC_BIFURCATION_SELECT;
  3756. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3757. I915_WRITE(SOUTH_CHICKEN1, temp);
  3758. POSTING_READ(SOUTH_CHICKEN1);
  3759. }
  3760. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3761. {
  3762. struct drm_device *dev = intel_crtc->base.dev;
  3763. switch (intel_crtc->pipe) {
  3764. case PIPE_A:
  3765. break;
  3766. case PIPE_B:
  3767. if (intel_crtc->config->fdi_lanes > 2)
  3768. cpt_set_fdi_bc_bifurcation(dev, false);
  3769. else
  3770. cpt_set_fdi_bc_bifurcation(dev, true);
  3771. break;
  3772. case PIPE_C:
  3773. cpt_set_fdi_bc_bifurcation(dev, true);
  3774. break;
  3775. default:
  3776. BUG();
  3777. }
  3778. }
  3779. /* Return which DP Port should be selected for Transcoder DP control */
  3780. static enum port
  3781. intel_trans_dp_port_sel(struct intel_crtc *crtc)
  3782. {
  3783. struct drm_device *dev = crtc->base.dev;
  3784. struct intel_encoder *encoder;
  3785. for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
  3786. if (encoder->type == INTEL_OUTPUT_DP ||
  3787. encoder->type == INTEL_OUTPUT_EDP)
  3788. return enc_to_dig_port(&encoder->base)->port;
  3789. }
  3790. return -1;
  3791. }
  3792. /*
  3793. * Enable PCH resources required for PCH ports:
  3794. * - PCH PLLs
  3795. * - FDI training & RX/TX
  3796. * - update transcoder timings
  3797. * - DP transcoding bits
  3798. * - transcoder
  3799. */
  3800. static void ironlake_pch_enable(const struct intel_crtc_state *crtc_state)
  3801. {
  3802. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3803. struct drm_device *dev = crtc->base.dev;
  3804. struct drm_i915_private *dev_priv = to_i915(dev);
  3805. int pipe = crtc->pipe;
  3806. u32 temp;
  3807. assert_pch_transcoder_disabled(dev_priv, pipe);
  3808. if (IS_IVYBRIDGE(dev_priv))
  3809. ivybridge_update_fdi_bc_bifurcation(crtc);
  3810. /* Write the TU size bits before fdi link training, so that error
  3811. * detection works. */
  3812. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3813. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3814. /* For PCH output, training FDI link */
  3815. dev_priv->display.fdi_link_train(crtc, crtc_state);
  3816. /* We need to program the right clock selection before writing the pixel
  3817. * mutliplier into the DPLL. */
  3818. if (HAS_PCH_CPT(dev_priv)) {
  3819. u32 sel;
  3820. temp = I915_READ(PCH_DPLL_SEL);
  3821. temp |= TRANS_DPLL_ENABLE(pipe);
  3822. sel = TRANS_DPLLB_SEL(pipe);
  3823. if (crtc_state->shared_dpll ==
  3824. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3825. temp |= sel;
  3826. else
  3827. temp &= ~sel;
  3828. I915_WRITE(PCH_DPLL_SEL, temp);
  3829. }
  3830. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3831. * transcoder, and we actually should do this to not upset any PCH
  3832. * transcoder that already use the clock when we share it.
  3833. *
  3834. * Note that enable_shared_dpll tries to do the right thing, but
  3835. * get_shared_dpll unconditionally resets the pll - we need that to have
  3836. * the right LVDS enable sequence. */
  3837. intel_enable_shared_dpll(crtc);
  3838. /* set transcoder timing, panel must allow it */
  3839. assert_panel_unlocked(dev_priv, pipe);
  3840. ironlake_pch_transcoder_set_timings(crtc, pipe);
  3841. intel_fdi_normal_train(crtc);
  3842. /* For PCH DP, enable TRANS_DP_CTL */
  3843. if (HAS_PCH_CPT(dev_priv) &&
  3844. intel_crtc_has_dp_encoder(crtc_state)) {
  3845. const struct drm_display_mode *adjusted_mode =
  3846. &crtc_state->base.adjusted_mode;
  3847. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3848. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3849. temp = I915_READ(reg);
  3850. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3851. TRANS_DP_SYNC_MASK |
  3852. TRANS_DP_BPC_MASK);
  3853. temp |= TRANS_DP_OUTPUT_ENABLE;
  3854. temp |= bpc << 9; /* same format but at 11:9 */
  3855. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3856. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3857. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3858. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3859. switch (intel_trans_dp_port_sel(crtc)) {
  3860. case PORT_B:
  3861. temp |= TRANS_DP_PORT_SEL_B;
  3862. break;
  3863. case PORT_C:
  3864. temp |= TRANS_DP_PORT_SEL_C;
  3865. break;
  3866. case PORT_D:
  3867. temp |= TRANS_DP_PORT_SEL_D;
  3868. break;
  3869. default:
  3870. BUG();
  3871. }
  3872. I915_WRITE(reg, temp);
  3873. }
  3874. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3875. }
  3876. static void lpt_pch_enable(const struct intel_crtc_state *crtc_state)
  3877. {
  3878. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  3879. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3880. enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
  3881. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3882. lpt_program_iclkip(crtc);
  3883. /* Set transcoder timing. */
  3884. ironlake_pch_transcoder_set_timings(crtc, PIPE_A);
  3885. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3886. }
  3887. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3888. {
  3889. struct drm_i915_private *dev_priv = to_i915(dev);
  3890. i915_reg_t dslreg = PIPEDSL(pipe);
  3891. u32 temp;
  3892. temp = I915_READ(dslreg);
  3893. udelay(500);
  3894. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3895. if (wait_for(I915_READ(dslreg) != temp, 5))
  3896. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3897. }
  3898. }
  3899. static int
  3900. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3901. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3902. int src_w, int src_h, int dst_w, int dst_h)
  3903. {
  3904. struct intel_crtc_scaler_state *scaler_state =
  3905. &crtc_state->scaler_state;
  3906. struct intel_crtc *intel_crtc =
  3907. to_intel_crtc(crtc_state->base.crtc);
  3908. int need_scaling;
  3909. need_scaling = drm_rotation_90_or_270(rotation) ?
  3910. (src_h != dst_w || src_w != dst_h):
  3911. (src_w != dst_w || src_h != dst_h);
  3912. /*
  3913. * if plane is being disabled or scaler is no more required or force detach
  3914. * - free scaler binded to this plane/crtc
  3915. * - in order to do this, update crtc->scaler_usage
  3916. *
  3917. * Here scaler state in crtc_state is set free so that
  3918. * scaler can be assigned to other user. Actual register
  3919. * update to free the scaler is done in plane/panel-fit programming.
  3920. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3921. */
  3922. if (force_detach || !need_scaling) {
  3923. if (*scaler_id >= 0) {
  3924. scaler_state->scaler_users &= ~(1 << scaler_user);
  3925. scaler_state->scalers[*scaler_id].in_use = 0;
  3926. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3927. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3928. intel_crtc->pipe, scaler_user, *scaler_id,
  3929. scaler_state->scaler_users);
  3930. *scaler_id = -1;
  3931. }
  3932. return 0;
  3933. }
  3934. /* range checks */
  3935. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3936. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3937. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3938. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3939. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3940. "size is out of scaler range\n",
  3941. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3942. return -EINVAL;
  3943. }
  3944. /* mark this plane as a scaler user in crtc_state */
  3945. scaler_state->scaler_users |= (1 << scaler_user);
  3946. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3947. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3948. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3949. scaler_state->scaler_users);
  3950. return 0;
  3951. }
  3952. /**
  3953. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  3954. *
  3955. * @state: crtc's scaler state
  3956. *
  3957. * Return
  3958. * 0 - scaler_usage updated successfully
  3959. * error - requested scaling cannot be supported or other error condition
  3960. */
  3961. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  3962. {
  3963. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  3964. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  3965. &state->scaler_state.scaler_id, DRM_ROTATE_0,
  3966. state->pipe_src_w, state->pipe_src_h,
  3967. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  3968. }
  3969. /**
  3970. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  3971. *
  3972. * @state: crtc's scaler state
  3973. * @plane_state: atomic plane state to update
  3974. *
  3975. * Return
  3976. * 0 - scaler_usage updated successfully
  3977. * error - requested scaling cannot be supported or other error condition
  3978. */
  3979. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  3980. struct intel_plane_state *plane_state)
  3981. {
  3982. struct intel_plane *intel_plane =
  3983. to_intel_plane(plane_state->base.plane);
  3984. struct drm_framebuffer *fb = plane_state->base.fb;
  3985. int ret;
  3986. bool force_detach = !fb || !plane_state->base.visible;
  3987. ret = skl_update_scaler(crtc_state, force_detach,
  3988. drm_plane_index(&intel_plane->base),
  3989. &plane_state->scaler_id,
  3990. plane_state->base.rotation,
  3991. drm_rect_width(&plane_state->base.src) >> 16,
  3992. drm_rect_height(&plane_state->base.src) >> 16,
  3993. drm_rect_width(&plane_state->base.dst),
  3994. drm_rect_height(&plane_state->base.dst));
  3995. if (ret || plane_state->scaler_id < 0)
  3996. return ret;
  3997. /* check colorkey */
  3998. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  3999. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  4000. intel_plane->base.base.id,
  4001. intel_plane->base.name);
  4002. return -EINVAL;
  4003. }
  4004. /* Check src format */
  4005. switch (fb->format->format) {
  4006. case DRM_FORMAT_RGB565:
  4007. case DRM_FORMAT_XBGR8888:
  4008. case DRM_FORMAT_XRGB8888:
  4009. case DRM_FORMAT_ABGR8888:
  4010. case DRM_FORMAT_ARGB8888:
  4011. case DRM_FORMAT_XRGB2101010:
  4012. case DRM_FORMAT_XBGR2101010:
  4013. case DRM_FORMAT_YUYV:
  4014. case DRM_FORMAT_YVYU:
  4015. case DRM_FORMAT_UYVY:
  4016. case DRM_FORMAT_VYUY:
  4017. break;
  4018. default:
  4019. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  4020. intel_plane->base.base.id, intel_plane->base.name,
  4021. fb->base.id, fb->format->format);
  4022. return -EINVAL;
  4023. }
  4024. return 0;
  4025. }
  4026. static void skylake_scaler_disable(struct intel_crtc *crtc)
  4027. {
  4028. int i;
  4029. for (i = 0; i < crtc->num_scalers; i++)
  4030. skl_detach_scaler(crtc, i);
  4031. }
  4032. static void skylake_pfit_enable(struct intel_crtc *crtc)
  4033. {
  4034. struct drm_device *dev = crtc->base.dev;
  4035. struct drm_i915_private *dev_priv = to_i915(dev);
  4036. int pipe = crtc->pipe;
  4037. struct intel_crtc_scaler_state *scaler_state =
  4038. &crtc->config->scaler_state;
  4039. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  4040. if (crtc->config->pch_pfit.enabled) {
  4041. int id;
  4042. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  4043. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  4044. return;
  4045. }
  4046. id = scaler_state->scaler_id;
  4047. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  4048. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  4049. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  4050. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  4051. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  4052. }
  4053. }
  4054. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  4055. {
  4056. struct drm_device *dev = crtc->base.dev;
  4057. struct drm_i915_private *dev_priv = to_i915(dev);
  4058. int pipe = crtc->pipe;
  4059. if (crtc->config->pch_pfit.enabled) {
  4060. /* Force use of hard-coded filter coefficients
  4061. * as some pre-programmed values are broken,
  4062. * e.g. x201.
  4063. */
  4064. if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
  4065. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  4066. PF_PIPE_SEL_IVB(pipe));
  4067. else
  4068. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  4069. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  4070. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  4071. }
  4072. }
  4073. void hsw_enable_ips(struct intel_crtc *crtc)
  4074. {
  4075. struct drm_device *dev = crtc->base.dev;
  4076. struct drm_i915_private *dev_priv = to_i915(dev);
  4077. if (!crtc->config->ips_enabled)
  4078. return;
  4079. /*
  4080. * We can only enable IPS after we enable a plane and wait for a vblank
  4081. * This function is called from post_plane_update, which is run after
  4082. * a vblank wait.
  4083. */
  4084. assert_plane_enabled(dev_priv, crtc->plane);
  4085. if (IS_BROADWELL(dev_priv)) {
  4086. mutex_lock(&dev_priv->rps.hw_lock);
  4087. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  4088. mutex_unlock(&dev_priv->rps.hw_lock);
  4089. /* Quoting Art Runyan: "its not safe to expect any particular
  4090. * value in IPS_CTL bit 31 after enabling IPS through the
  4091. * mailbox." Moreover, the mailbox may return a bogus state,
  4092. * so we need to just enable it and continue on.
  4093. */
  4094. } else {
  4095. I915_WRITE(IPS_CTL, IPS_ENABLE);
  4096. /* The bit only becomes 1 in the next vblank, so this wait here
  4097. * is essentially intel_wait_for_vblank. If we don't have this
  4098. * and don't wait for vblanks until the end of crtc_enable, then
  4099. * the HW state readout code will complain that the expected
  4100. * IPS_CTL value is not the one we read. */
  4101. if (intel_wait_for_register(dev_priv,
  4102. IPS_CTL, IPS_ENABLE, IPS_ENABLE,
  4103. 50))
  4104. DRM_ERROR("Timed out waiting for IPS enable\n");
  4105. }
  4106. }
  4107. void hsw_disable_ips(struct intel_crtc *crtc)
  4108. {
  4109. struct drm_device *dev = crtc->base.dev;
  4110. struct drm_i915_private *dev_priv = to_i915(dev);
  4111. if (!crtc->config->ips_enabled)
  4112. return;
  4113. assert_plane_enabled(dev_priv, crtc->plane);
  4114. if (IS_BROADWELL(dev_priv)) {
  4115. mutex_lock(&dev_priv->rps.hw_lock);
  4116. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  4117. mutex_unlock(&dev_priv->rps.hw_lock);
  4118. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  4119. if (intel_wait_for_register(dev_priv,
  4120. IPS_CTL, IPS_ENABLE, 0,
  4121. 42))
  4122. DRM_ERROR("Timed out waiting for IPS disable\n");
  4123. } else {
  4124. I915_WRITE(IPS_CTL, 0);
  4125. POSTING_READ(IPS_CTL);
  4126. }
  4127. /* We need to wait for a vblank before we can disable the plane. */
  4128. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4129. }
  4130. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  4131. {
  4132. if (intel_crtc->overlay) {
  4133. struct drm_device *dev = intel_crtc->base.dev;
  4134. struct drm_i915_private *dev_priv = to_i915(dev);
  4135. mutex_lock(&dev->struct_mutex);
  4136. dev_priv->mm.interruptible = false;
  4137. (void) intel_overlay_switch_off(intel_crtc->overlay);
  4138. dev_priv->mm.interruptible = true;
  4139. mutex_unlock(&dev->struct_mutex);
  4140. }
  4141. /* Let userspace switch the overlay on again. In most cases userspace
  4142. * has to recompute where to put it anyway.
  4143. */
  4144. }
  4145. /**
  4146. * intel_post_enable_primary - Perform operations after enabling primary plane
  4147. * @crtc: the CRTC whose primary plane was just enabled
  4148. *
  4149. * Performs potentially sleeping operations that must be done after the primary
  4150. * plane is enabled, such as updating FBC and IPS. Note that this may be
  4151. * called due to an explicit primary plane update, or due to an implicit
  4152. * re-enable that is caused when a sprite plane is updated to no longer
  4153. * completely hide the primary plane.
  4154. */
  4155. static void
  4156. intel_post_enable_primary(struct drm_crtc *crtc)
  4157. {
  4158. struct drm_device *dev = crtc->dev;
  4159. struct drm_i915_private *dev_priv = to_i915(dev);
  4160. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4161. int pipe = intel_crtc->pipe;
  4162. /*
  4163. * FIXME IPS should be fine as long as one plane is
  4164. * enabled, but in practice it seems to have problems
  4165. * when going from primary only to sprite only and vice
  4166. * versa.
  4167. */
  4168. hsw_enable_ips(intel_crtc);
  4169. /*
  4170. * Gen2 reports pipe underruns whenever all planes are disabled.
  4171. * So don't enable underrun reporting before at least some planes
  4172. * are enabled.
  4173. * FIXME: Need to fix the logic to work when we turn off all planes
  4174. * but leave the pipe running.
  4175. */
  4176. if (IS_GEN2(dev_priv))
  4177. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4178. /* Underruns don't always raise interrupts, so check manually. */
  4179. intel_check_cpu_fifo_underruns(dev_priv);
  4180. intel_check_pch_fifo_underruns(dev_priv);
  4181. }
  4182. /* FIXME move all this to pre_plane_update() with proper state tracking */
  4183. static void
  4184. intel_pre_disable_primary(struct drm_crtc *crtc)
  4185. {
  4186. struct drm_device *dev = crtc->dev;
  4187. struct drm_i915_private *dev_priv = to_i915(dev);
  4188. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4189. int pipe = intel_crtc->pipe;
  4190. /*
  4191. * Gen2 reports pipe underruns whenever all planes are disabled.
  4192. * So diasble underrun reporting before all the planes get disabled.
  4193. * FIXME: Need to fix the logic to work when we turn off all planes
  4194. * but leave the pipe running.
  4195. */
  4196. if (IS_GEN2(dev_priv))
  4197. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4198. /*
  4199. * FIXME IPS should be fine as long as one plane is
  4200. * enabled, but in practice it seems to have problems
  4201. * when going from primary only to sprite only and vice
  4202. * versa.
  4203. */
  4204. hsw_disable_ips(intel_crtc);
  4205. }
  4206. /* FIXME get rid of this and use pre_plane_update */
  4207. static void
  4208. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  4209. {
  4210. struct drm_device *dev = crtc->dev;
  4211. struct drm_i915_private *dev_priv = to_i915(dev);
  4212. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4213. int pipe = intel_crtc->pipe;
  4214. intel_pre_disable_primary(crtc);
  4215. /*
  4216. * Vblank time updates from the shadow to live plane control register
  4217. * are blocked if the memory self-refresh mode is active at that
  4218. * moment. So to make sure the plane gets truly disabled, disable
  4219. * first the self-refresh mode. The self-refresh enable bit in turn
  4220. * will be checked/applied by the HW only at the next frame start
  4221. * event which is after the vblank start event, so we need to have a
  4222. * wait-for-vblank between disabling the plane and the pipe.
  4223. */
  4224. if (HAS_GMCH_DISPLAY(dev_priv) &&
  4225. intel_set_memory_cxsr(dev_priv, false))
  4226. intel_wait_for_vblank(dev_priv, pipe);
  4227. }
  4228. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  4229. {
  4230. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4231. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4232. struct intel_crtc_state *pipe_config =
  4233. to_intel_crtc_state(crtc->base.state);
  4234. struct drm_plane *primary = crtc->base.primary;
  4235. struct drm_plane_state *old_pri_state =
  4236. drm_atomic_get_existing_plane_state(old_state, primary);
  4237. intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
  4238. crtc->wm.cxsr_allowed = true;
  4239. if (pipe_config->update_wm_post && pipe_config->base.active)
  4240. intel_update_watermarks(crtc);
  4241. if (old_pri_state) {
  4242. struct intel_plane_state *primary_state =
  4243. to_intel_plane_state(primary->state);
  4244. struct intel_plane_state *old_primary_state =
  4245. to_intel_plane_state(old_pri_state);
  4246. intel_fbc_post_update(crtc);
  4247. if (primary_state->base.visible &&
  4248. (needs_modeset(&pipe_config->base) ||
  4249. !old_primary_state->base.visible))
  4250. intel_post_enable_primary(&crtc->base);
  4251. }
  4252. }
  4253. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
  4254. {
  4255. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4256. struct drm_device *dev = crtc->base.dev;
  4257. struct drm_i915_private *dev_priv = to_i915(dev);
  4258. struct intel_crtc_state *pipe_config =
  4259. to_intel_crtc_state(crtc->base.state);
  4260. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4261. struct drm_plane *primary = crtc->base.primary;
  4262. struct drm_plane_state *old_pri_state =
  4263. drm_atomic_get_existing_plane_state(old_state, primary);
  4264. bool modeset = needs_modeset(&pipe_config->base);
  4265. struct intel_atomic_state *old_intel_state =
  4266. to_intel_atomic_state(old_state);
  4267. if (old_pri_state) {
  4268. struct intel_plane_state *primary_state =
  4269. to_intel_plane_state(primary->state);
  4270. struct intel_plane_state *old_primary_state =
  4271. to_intel_plane_state(old_pri_state);
  4272. intel_fbc_pre_update(crtc, pipe_config, primary_state);
  4273. if (old_primary_state->base.visible &&
  4274. (modeset || !primary_state->base.visible))
  4275. intel_pre_disable_primary(&crtc->base);
  4276. }
  4277. if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev_priv)) {
  4278. crtc->wm.cxsr_allowed = false;
  4279. /*
  4280. * Vblank time updates from the shadow to live plane control register
  4281. * are blocked if the memory self-refresh mode is active at that
  4282. * moment. So to make sure the plane gets truly disabled, disable
  4283. * first the self-refresh mode. The self-refresh enable bit in turn
  4284. * will be checked/applied by the HW only at the next frame start
  4285. * event which is after the vblank start event, so we need to have a
  4286. * wait-for-vblank between disabling the plane and the pipe.
  4287. */
  4288. if (old_crtc_state->base.active &&
  4289. intel_set_memory_cxsr(dev_priv, false))
  4290. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4291. }
  4292. /*
  4293. * IVB workaround: must disable low power watermarks for at least
  4294. * one frame before enabling scaling. LP watermarks can be re-enabled
  4295. * when scaling is disabled.
  4296. *
  4297. * WaCxSRDisabledForSpriteScaling:ivb
  4298. */
  4299. if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev))
  4300. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4301. /*
  4302. * If we're doing a modeset, we're done. No need to do any pre-vblank
  4303. * watermark programming here.
  4304. */
  4305. if (needs_modeset(&pipe_config->base))
  4306. return;
  4307. /*
  4308. * For platforms that support atomic watermarks, program the
  4309. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  4310. * will be the intermediate values that are safe for both pre- and
  4311. * post- vblank; when vblank happens, the 'active' values will be set
  4312. * to the final 'target' values and we'll do this again to get the
  4313. * optimal watermarks. For gen9+ platforms, the values we program here
  4314. * will be the final target values which will get automatically latched
  4315. * at vblank time; no further programming will be necessary.
  4316. *
  4317. * If a platform hasn't been transitioned to atomic watermarks yet,
  4318. * we'll continue to update watermarks the old way, if flags tell
  4319. * us to.
  4320. */
  4321. if (dev_priv->display.initial_watermarks != NULL)
  4322. dev_priv->display.initial_watermarks(old_intel_state,
  4323. pipe_config);
  4324. else if (pipe_config->update_wm_pre)
  4325. intel_update_watermarks(crtc);
  4326. }
  4327. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4328. {
  4329. struct drm_device *dev = crtc->dev;
  4330. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4331. struct drm_plane *p;
  4332. int pipe = intel_crtc->pipe;
  4333. intel_crtc_dpms_overlay_disable(intel_crtc);
  4334. drm_for_each_plane_mask(p, dev, plane_mask)
  4335. to_intel_plane(p)->disable_plane(p, crtc);
  4336. /*
  4337. * FIXME: Once we grow proper nuclear flip support out of this we need
  4338. * to compute the mask of flip planes precisely. For the time being
  4339. * consider this a flip to a NULL plane.
  4340. */
  4341. intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4342. }
  4343. static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
  4344. struct intel_crtc_state *crtc_state,
  4345. struct drm_atomic_state *old_state)
  4346. {
  4347. struct drm_connector_state *old_conn_state;
  4348. struct drm_connector *conn;
  4349. int i;
  4350. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4351. struct drm_connector_state *conn_state = conn->state;
  4352. struct intel_encoder *encoder =
  4353. to_intel_encoder(conn_state->best_encoder);
  4354. if (conn_state->crtc != crtc)
  4355. continue;
  4356. if (encoder->pre_pll_enable)
  4357. encoder->pre_pll_enable(encoder, crtc_state, conn_state);
  4358. }
  4359. }
  4360. static void intel_encoders_pre_enable(struct drm_crtc *crtc,
  4361. struct intel_crtc_state *crtc_state,
  4362. struct drm_atomic_state *old_state)
  4363. {
  4364. struct drm_connector_state *old_conn_state;
  4365. struct drm_connector *conn;
  4366. int i;
  4367. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4368. struct drm_connector_state *conn_state = conn->state;
  4369. struct intel_encoder *encoder =
  4370. to_intel_encoder(conn_state->best_encoder);
  4371. if (conn_state->crtc != crtc)
  4372. continue;
  4373. if (encoder->pre_enable)
  4374. encoder->pre_enable(encoder, crtc_state, conn_state);
  4375. }
  4376. }
  4377. static void intel_encoders_enable(struct drm_crtc *crtc,
  4378. struct intel_crtc_state *crtc_state,
  4379. struct drm_atomic_state *old_state)
  4380. {
  4381. struct drm_connector_state *old_conn_state;
  4382. struct drm_connector *conn;
  4383. int i;
  4384. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4385. struct drm_connector_state *conn_state = conn->state;
  4386. struct intel_encoder *encoder =
  4387. to_intel_encoder(conn_state->best_encoder);
  4388. if (conn_state->crtc != crtc)
  4389. continue;
  4390. encoder->enable(encoder, crtc_state, conn_state);
  4391. intel_opregion_notify_encoder(encoder, true);
  4392. }
  4393. }
  4394. static void intel_encoders_disable(struct drm_crtc *crtc,
  4395. struct intel_crtc_state *old_crtc_state,
  4396. struct drm_atomic_state *old_state)
  4397. {
  4398. struct drm_connector_state *old_conn_state;
  4399. struct drm_connector *conn;
  4400. int i;
  4401. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4402. struct intel_encoder *encoder =
  4403. to_intel_encoder(old_conn_state->best_encoder);
  4404. if (old_conn_state->crtc != crtc)
  4405. continue;
  4406. intel_opregion_notify_encoder(encoder, false);
  4407. encoder->disable(encoder, old_crtc_state, old_conn_state);
  4408. }
  4409. }
  4410. static void intel_encoders_post_disable(struct drm_crtc *crtc,
  4411. struct intel_crtc_state *old_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 intel_encoder *encoder =
  4419. to_intel_encoder(old_conn_state->best_encoder);
  4420. if (old_conn_state->crtc != crtc)
  4421. continue;
  4422. if (encoder->post_disable)
  4423. encoder->post_disable(encoder, old_crtc_state, old_conn_state);
  4424. }
  4425. }
  4426. static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
  4427. struct intel_crtc_state *old_crtc_state,
  4428. struct drm_atomic_state *old_state)
  4429. {
  4430. struct drm_connector_state *old_conn_state;
  4431. struct drm_connector *conn;
  4432. int i;
  4433. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4434. struct intel_encoder *encoder =
  4435. to_intel_encoder(old_conn_state->best_encoder);
  4436. if (old_conn_state->crtc != crtc)
  4437. continue;
  4438. if (encoder->post_pll_disable)
  4439. encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
  4440. }
  4441. }
  4442. static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
  4443. struct drm_atomic_state *old_state)
  4444. {
  4445. struct drm_crtc *crtc = pipe_config->base.crtc;
  4446. struct drm_device *dev = crtc->dev;
  4447. struct drm_i915_private *dev_priv = to_i915(dev);
  4448. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4449. int pipe = intel_crtc->pipe;
  4450. struct intel_atomic_state *old_intel_state =
  4451. to_intel_atomic_state(old_state);
  4452. if (WARN_ON(intel_crtc->active))
  4453. return;
  4454. /*
  4455. * Sometimes spurious CPU pipe underruns happen during FDI
  4456. * training, at least with VGA+HDMI cloning. Suppress them.
  4457. *
  4458. * On ILK we get an occasional spurious CPU pipe underruns
  4459. * between eDP port A enable and vdd enable. Also PCH port
  4460. * enable seems to result in the occasional CPU pipe underrun.
  4461. *
  4462. * Spurious PCH underruns also occur during PCH enabling.
  4463. */
  4464. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4465. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4466. if (intel_crtc->config->has_pch_encoder)
  4467. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4468. if (intel_crtc->config->has_pch_encoder)
  4469. intel_prepare_shared_dpll(intel_crtc);
  4470. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4471. intel_dp_set_m_n(intel_crtc, M1_N1);
  4472. intel_set_pipe_timings(intel_crtc);
  4473. intel_set_pipe_src_size(intel_crtc);
  4474. if (intel_crtc->config->has_pch_encoder) {
  4475. intel_cpu_transcoder_set_m_n(intel_crtc,
  4476. &intel_crtc->config->fdi_m_n, NULL);
  4477. }
  4478. ironlake_set_pipeconf(crtc);
  4479. intel_crtc->active = true;
  4480. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4481. if (intel_crtc->config->has_pch_encoder) {
  4482. /* Note: FDI PLL enabling _must_ be done before we enable the
  4483. * cpu pipes, hence this is separate from all the other fdi/pch
  4484. * enabling. */
  4485. ironlake_fdi_pll_enable(intel_crtc);
  4486. } else {
  4487. assert_fdi_tx_disabled(dev_priv, pipe);
  4488. assert_fdi_rx_disabled(dev_priv, pipe);
  4489. }
  4490. ironlake_pfit_enable(intel_crtc);
  4491. /*
  4492. * On ILK+ LUT must be loaded before the pipe is running but with
  4493. * clocks enabled
  4494. */
  4495. intel_color_load_luts(&pipe_config->base);
  4496. if (dev_priv->display.initial_watermarks != NULL)
  4497. dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
  4498. intel_enable_pipe(intel_crtc);
  4499. if (intel_crtc->config->has_pch_encoder)
  4500. ironlake_pch_enable(pipe_config);
  4501. assert_vblank_disabled(crtc);
  4502. drm_crtc_vblank_on(crtc);
  4503. intel_encoders_enable(crtc, pipe_config, old_state);
  4504. if (HAS_PCH_CPT(dev_priv))
  4505. cpt_verify_modeset(dev, intel_crtc->pipe);
  4506. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4507. if (intel_crtc->config->has_pch_encoder)
  4508. intel_wait_for_vblank(dev_priv, pipe);
  4509. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4510. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4511. }
  4512. /* IPS only exists on ULT machines and is tied to pipe A. */
  4513. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4514. {
  4515. return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
  4516. }
  4517. static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
  4518. struct drm_atomic_state *old_state)
  4519. {
  4520. struct drm_crtc *crtc = pipe_config->base.crtc;
  4521. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4522. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4523. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4524. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4525. struct intel_atomic_state *old_intel_state =
  4526. to_intel_atomic_state(old_state);
  4527. if (WARN_ON(intel_crtc->active))
  4528. return;
  4529. if (intel_crtc->config->has_pch_encoder)
  4530. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4531. false);
  4532. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4533. if (intel_crtc->config->shared_dpll)
  4534. intel_enable_shared_dpll(intel_crtc);
  4535. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4536. intel_dp_set_m_n(intel_crtc, M1_N1);
  4537. if (!transcoder_is_dsi(cpu_transcoder))
  4538. intel_set_pipe_timings(intel_crtc);
  4539. intel_set_pipe_src_size(intel_crtc);
  4540. if (cpu_transcoder != TRANSCODER_EDP &&
  4541. !transcoder_is_dsi(cpu_transcoder)) {
  4542. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4543. intel_crtc->config->pixel_multiplier - 1);
  4544. }
  4545. if (intel_crtc->config->has_pch_encoder) {
  4546. intel_cpu_transcoder_set_m_n(intel_crtc,
  4547. &intel_crtc->config->fdi_m_n, NULL);
  4548. }
  4549. if (!transcoder_is_dsi(cpu_transcoder))
  4550. haswell_set_pipeconf(crtc);
  4551. haswell_set_pipemisc(crtc);
  4552. intel_color_set_csc(&pipe_config->base);
  4553. intel_crtc->active = true;
  4554. if (intel_crtc->config->has_pch_encoder)
  4555. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4556. else
  4557. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4558. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4559. if (intel_crtc->config->has_pch_encoder)
  4560. dev_priv->display.fdi_link_train(intel_crtc, pipe_config);
  4561. if (!transcoder_is_dsi(cpu_transcoder))
  4562. intel_ddi_enable_pipe_clock(intel_crtc);
  4563. if (INTEL_GEN(dev_priv) >= 9)
  4564. skylake_pfit_enable(intel_crtc);
  4565. else
  4566. ironlake_pfit_enable(intel_crtc);
  4567. /*
  4568. * On ILK+ LUT must be loaded before the pipe is running but with
  4569. * clocks enabled
  4570. */
  4571. intel_color_load_luts(&pipe_config->base);
  4572. intel_ddi_set_pipe_settings(intel_crtc);
  4573. if (!transcoder_is_dsi(cpu_transcoder))
  4574. intel_ddi_enable_transcoder_func(intel_crtc);
  4575. if (dev_priv->display.initial_watermarks != NULL)
  4576. dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
  4577. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4578. if (!transcoder_is_dsi(cpu_transcoder))
  4579. intel_enable_pipe(intel_crtc);
  4580. if (intel_crtc->config->has_pch_encoder)
  4581. lpt_pch_enable(pipe_config);
  4582. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4583. intel_ddi_set_vc_payload_alloc(intel_crtc, true);
  4584. assert_vblank_disabled(crtc);
  4585. drm_crtc_vblank_on(crtc);
  4586. intel_encoders_enable(crtc, pipe_config, old_state);
  4587. if (intel_crtc->config->has_pch_encoder) {
  4588. intel_wait_for_vblank(dev_priv, pipe);
  4589. intel_wait_for_vblank(dev_priv, pipe);
  4590. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4591. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4592. true);
  4593. }
  4594. /* If we change the relative order between pipe/planes enabling, we need
  4595. * to change the workaround. */
  4596. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4597. if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
  4598. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4599. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4600. }
  4601. }
  4602. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4603. {
  4604. struct drm_device *dev = crtc->base.dev;
  4605. struct drm_i915_private *dev_priv = to_i915(dev);
  4606. int pipe = crtc->pipe;
  4607. /* To avoid upsetting the power well on haswell only disable the pfit if
  4608. * it's in use. The hw state code will make sure we get this right. */
  4609. if (force || crtc->config->pch_pfit.enabled) {
  4610. I915_WRITE(PF_CTL(pipe), 0);
  4611. I915_WRITE(PF_WIN_POS(pipe), 0);
  4612. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4613. }
  4614. }
  4615. static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4616. struct drm_atomic_state *old_state)
  4617. {
  4618. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4619. struct drm_device *dev = crtc->dev;
  4620. struct drm_i915_private *dev_priv = to_i915(dev);
  4621. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4622. int pipe = intel_crtc->pipe;
  4623. /*
  4624. * Sometimes spurious CPU pipe underruns happen when the
  4625. * pipe is already disabled, but FDI RX/TX is still enabled.
  4626. * Happens at least with VGA+HDMI cloning. Suppress them.
  4627. */
  4628. if (intel_crtc->config->has_pch_encoder) {
  4629. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4630. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4631. }
  4632. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4633. drm_crtc_vblank_off(crtc);
  4634. assert_vblank_disabled(crtc);
  4635. intel_disable_pipe(intel_crtc);
  4636. ironlake_pfit_disable(intel_crtc, false);
  4637. if (intel_crtc->config->has_pch_encoder)
  4638. ironlake_fdi_disable(crtc);
  4639. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4640. if (intel_crtc->config->has_pch_encoder) {
  4641. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4642. if (HAS_PCH_CPT(dev_priv)) {
  4643. i915_reg_t reg;
  4644. u32 temp;
  4645. /* disable TRANS_DP_CTL */
  4646. reg = TRANS_DP_CTL(pipe);
  4647. temp = I915_READ(reg);
  4648. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4649. TRANS_DP_PORT_SEL_MASK);
  4650. temp |= TRANS_DP_PORT_SEL_NONE;
  4651. I915_WRITE(reg, temp);
  4652. /* disable DPLL_SEL */
  4653. temp = I915_READ(PCH_DPLL_SEL);
  4654. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4655. I915_WRITE(PCH_DPLL_SEL, temp);
  4656. }
  4657. ironlake_fdi_pll_disable(intel_crtc);
  4658. }
  4659. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4660. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4661. }
  4662. static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4663. struct drm_atomic_state *old_state)
  4664. {
  4665. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4666. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4667. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4668. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4669. if (intel_crtc->config->has_pch_encoder)
  4670. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4671. false);
  4672. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4673. drm_crtc_vblank_off(crtc);
  4674. assert_vblank_disabled(crtc);
  4675. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4676. if (!transcoder_is_dsi(cpu_transcoder))
  4677. intel_disable_pipe(intel_crtc);
  4678. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4679. intel_ddi_set_vc_payload_alloc(intel_crtc, false);
  4680. if (!transcoder_is_dsi(cpu_transcoder))
  4681. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4682. if (INTEL_GEN(dev_priv) >= 9)
  4683. skylake_scaler_disable(intel_crtc);
  4684. else
  4685. ironlake_pfit_disable(intel_crtc, false);
  4686. if (!transcoder_is_dsi(cpu_transcoder))
  4687. intel_ddi_disable_pipe_clock(intel_crtc);
  4688. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4689. if (old_crtc_state->has_pch_encoder)
  4690. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4691. true);
  4692. }
  4693. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4694. {
  4695. struct drm_device *dev = crtc->base.dev;
  4696. struct drm_i915_private *dev_priv = to_i915(dev);
  4697. struct intel_crtc_state *pipe_config = crtc->config;
  4698. if (!pipe_config->gmch_pfit.control)
  4699. return;
  4700. /*
  4701. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4702. * according to register description and PRM.
  4703. */
  4704. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4705. assert_pipe_disabled(dev_priv, crtc->pipe);
  4706. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4707. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4708. /* Border color in case we don't scale up to the full screen. Black by
  4709. * default, change to something else for debugging. */
  4710. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4711. }
  4712. enum intel_display_power_domain intel_port_to_power_domain(enum port port)
  4713. {
  4714. switch (port) {
  4715. case PORT_A:
  4716. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4717. case PORT_B:
  4718. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4719. case PORT_C:
  4720. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4721. case PORT_D:
  4722. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4723. case PORT_E:
  4724. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4725. default:
  4726. MISSING_CASE(port);
  4727. return POWER_DOMAIN_PORT_OTHER;
  4728. }
  4729. }
  4730. static u64 get_crtc_power_domains(struct drm_crtc *crtc,
  4731. struct intel_crtc_state *crtc_state)
  4732. {
  4733. struct drm_device *dev = crtc->dev;
  4734. struct drm_i915_private *dev_priv = to_i915(dev);
  4735. struct drm_encoder *encoder;
  4736. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4737. enum pipe pipe = intel_crtc->pipe;
  4738. u64 mask;
  4739. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4740. if (!crtc_state->base.active)
  4741. return 0;
  4742. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4743. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4744. if (crtc_state->pch_pfit.enabled ||
  4745. crtc_state->pch_pfit.force_thru)
  4746. mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4747. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4748. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4749. mask |= BIT_ULL(intel_encoder->power_domain);
  4750. }
  4751. if (HAS_DDI(dev_priv) && crtc_state->has_audio)
  4752. mask |= BIT(POWER_DOMAIN_AUDIO);
  4753. if (crtc_state->shared_dpll)
  4754. mask |= BIT_ULL(POWER_DOMAIN_PLLS);
  4755. return mask;
  4756. }
  4757. static u64
  4758. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4759. struct intel_crtc_state *crtc_state)
  4760. {
  4761. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4762. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4763. enum intel_display_power_domain domain;
  4764. u64 domains, new_domains, old_domains;
  4765. old_domains = intel_crtc->enabled_power_domains;
  4766. intel_crtc->enabled_power_domains = new_domains =
  4767. get_crtc_power_domains(crtc, crtc_state);
  4768. domains = new_domains & ~old_domains;
  4769. for_each_power_domain(domain, domains)
  4770. intel_display_power_get(dev_priv, domain);
  4771. return old_domains & ~new_domains;
  4772. }
  4773. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4774. u64 domains)
  4775. {
  4776. enum intel_display_power_domain domain;
  4777. for_each_power_domain(domain, domains)
  4778. intel_display_power_put(dev_priv, domain);
  4779. }
  4780. static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
  4781. struct drm_atomic_state *old_state)
  4782. {
  4783. struct drm_crtc *crtc = pipe_config->base.crtc;
  4784. struct drm_device *dev = crtc->dev;
  4785. struct drm_i915_private *dev_priv = to_i915(dev);
  4786. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4787. int pipe = intel_crtc->pipe;
  4788. if (WARN_ON(intel_crtc->active))
  4789. return;
  4790. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4791. intel_dp_set_m_n(intel_crtc, M1_N1);
  4792. intel_set_pipe_timings(intel_crtc);
  4793. intel_set_pipe_src_size(intel_crtc);
  4794. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  4795. struct drm_i915_private *dev_priv = to_i915(dev);
  4796. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  4797. I915_WRITE(CHV_CANVAS(pipe), 0);
  4798. }
  4799. i9xx_set_pipeconf(intel_crtc);
  4800. intel_crtc->active = true;
  4801. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4802. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4803. if (IS_CHERRYVIEW(dev_priv)) {
  4804. chv_prepare_pll(intel_crtc, intel_crtc->config);
  4805. chv_enable_pll(intel_crtc, intel_crtc->config);
  4806. } else {
  4807. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  4808. vlv_enable_pll(intel_crtc, intel_crtc->config);
  4809. }
  4810. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4811. i9xx_pfit_enable(intel_crtc);
  4812. intel_color_load_luts(&pipe_config->base);
  4813. intel_update_watermarks(intel_crtc);
  4814. intel_enable_pipe(intel_crtc);
  4815. assert_vblank_disabled(crtc);
  4816. drm_crtc_vblank_on(crtc);
  4817. intel_encoders_enable(crtc, pipe_config, old_state);
  4818. }
  4819. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  4820. {
  4821. struct drm_device *dev = crtc->base.dev;
  4822. struct drm_i915_private *dev_priv = to_i915(dev);
  4823. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  4824. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  4825. }
  4826. static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
  4827. struct drm_atomic_state *old_state)
  4828. {
  4829. struct drm_crtc *crtc = pipe_config->base.crtc;
  4830. struct drm_device *dev = crtc->dev;
  4831. struct drm_i915_private *dev_priv = to_i915(dev);
  4832. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4833. enum pipe pipe = intel_crtc->pipe;
  4834. if (WARN_ON(intel_crtc->active))
  4835. return;
  4836. i9xx_set_pll_dividers(intel_crtc);
  4837. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4838. intel_dp_set_m_n(intel_crtc, M1_N1);
  4839. intel_set_pipe_timings(intel_crtc);
  4840. intel_set_pipe_src_size(intel_crtc);
  4841. i9xx_set_pipeconf(intel_crtc);
  4842. intel_crtc->active = true;
  4843. if (!IS_GEN2(dev_priv))
  4844. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4845. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4846. i9xx_enable_pll(intel_crtc);
  4847. i9xx_pfit_enable(intel_crtc);
  4848. intel_color_load_luts(&pipe_config->base);
  4849. intel_update_watermarks(intel_crtc);
  4850. intel_enable_pipe(intel_crtc);
  4851. assert_vblank_disabled(crtc);
  4852. drm_crtc_vblank_on(crtc);
  4853. intel_encoders_enable(crtc, pipe_config, old_state);
  4854. }
  4855. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  4856. {
  4857. struct drm_device *dev = crtc->base.dev;
  4858. struct drm_i915_private *dev_priv = to_i915(dev);
  4859. if (!crtc->config->gmch_pfit.control)
  4860. return;
  4861. assert_pipe_disabled(dev_priv, crtc->pipe);
  4862. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  4863. I915_READ(PFIT_CONTROL));
  4864. I915_WRITE(PFIT_CONTROL, 0);
  4865. }
  4866. static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4867. struct drm_atomic_state *old_state)
  4868. {
  4869. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4870. struct drm_device *dev = crtc->dev;
  4871. struct drm_i915_private *dev_priv = to_i915(dev);
  4872. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4873. int pipe = intel_crtc->pipe;
  4874. /*
  4875. * On gen2 planes are double buffered but the pipe isn't, so we must
  4876. * wait for planes to fully turn off before disabling the pipe.
  4877. */
  4878. if (IS_GEN2(dev_priv))
  4879. intel_wait_for_vblank(dev_priv, pipe);
  4880. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4881. drm_crtc_vblank_off(crtc);
  4882. assert_vblank_disabled(crtc);
  4883. intel_disable_pipe(intel_crtc);
  4884. i9xx_pfit_disable(intel_crtc);
  4885. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4886. if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
  4887. if (IS_CHERRYVIEW(dev_priv))
  4888. chv_disable_pll(dev_priv, pipe);
  4889. else if (IS_VALLEYVIEW(dev_priv))
  4890. vlv_disable_pll(dev_priv, pipe);
  4891. else
  4892. i9xx_disable_pll(intel_crtc);
  4893. }
  4894. intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
  4895. if (!IS_GEN2(dev_priv))
  4896. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4897. }
  4898. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  4899. {
  4900. struct intel_encoder *encoder;
  4901. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4902. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4903. enum intel_display_power_domain domain;
  4904. u64 domains;
  4905. struct drm_atomic_state *state;
  4906. struct intel_crtc_state *crtc_state;
  4907. int ret;
  4908. if (!intel_crtc->active)
  4909. return;
  4910. if (crtc->primary->state->visible) {
  4911. WARN_ON(intel_crtc->flip_work);
  4912. intel_pre_disable_primary_noatomic(crtc);
  4913. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  4914. crtc->primary->state->visible = false;
  4915. }
  4916. state = drm_atomic_state_alloc(crtc->dev);
  4917. if (!state) {
  4918. DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
  4919. crtc->base.id, crtc->name);
  4920. return;
  4921. }
  4922. state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
  4923. /* Everything's already locked, -EDEADLK can't happen. */
  4924. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  4925. ret = drm_atomic_add_affected_connectors(state, crtc);
  4926. WARN_ON(IS_ERR(crtc_state) || ret);
  4927. dev_priv->display.crtc_disable(crtc_state, state);
  4928. drm_atomic_state_put(state);
  4929. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  4930. crtc->base.id, crtc->name);
  4931. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  4932. crtc->state->active = false;
  4933. intel_crtc->active = false;
  4934. crtc->enabled = false;
  4935. crtc->state->connector_mask = 0;
  4936. crtc->state->encoder_mask = 0;
  4937. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  4938. encoder->base.crtc = NULL;
  4939. intel_fbc_disable(intel_crtc);
  4940. intel_update_watermarks(intel_crtc);
  4941. intel_disable_shared_dpll(intel_crtc);
  4942. domains = intel_crtc->enabled_power_domains;
  4943. for_each_power_domain(domain, domains)
  4944. intel_display_power_put(dev_priv, domain);
  4945. intel_crtc->enabled_power_domains = 0;
  4946. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  4947. dev_priv->min_pixclk[intel_crtc->pipe] = 0;
  4948. }
  4949. /*
  4950. * turn all crtc's off, but do not adjust state
  4951. * This has to be paired with a call to intel_modeset_setup_hw_state.
  4952. */
  4953. int intel_display_suspend(struct drm_device *dev)
  4954. {
  4955. struct drm_i915_private *dev_priv = to_i915(dev);
  4956. struct drm_atomic_state *state;
  4957. int ret;
  4958. state = drm_atomic_helper_suspend(dev);
  4959. ret = PTR_ERR_OR_ZERO(state);
  4960. if (ret)
  4961. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  4962. else
  4963. dev_priv->modeset_restore_state = state;
  4964. return ret;
  4965. }
  4966. void intel_encoder_destroy(struct drm_encoder *encoder)
  4967. {
  4968. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4969. drm_encoder_cleanup(encoder);
  4970. kfree(intel_encoder);
  4971. }
  4972. /* Cross check the actual hw state with our own modeset state tracking (and it's
  4973. * internal consistency). */
  4974. static void intel_connector_verify_state(struct intel_connector *connector)
  4975. {
  4976. struct drm_crtc *crtc = connector->base.state->crtc;
  4977. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  4978. connector->base.base.id,
  4979. connector->base.name);
  4980. if (connector->get_hw_state(connector)) {
  4981. struct intel_encoder *encoder = connector->encoder;
  4982. struct drm_connector_state *conn_state = connector->base.state;
  4983. I915_STATE_WARN(!crtc,
  4984. "connector enabled without attached crtc\n");
  4985. if (!crtc)
  4986. return;
  4987. I915_STATE_WARN(!crtc->state->active,
  4988. "connector is active, but attached crtc isn't\n");
  4989. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  4990. return;
  4991. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  4992. "atomic encoder doesn't match attached encoder\n");
  4993. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  4994. "attached encoder crtc differs from connector crtc\n");
  4995. } else {
  4996. I915_STATE_WARN(crtc && crtc->state->active,
  4997. "attached crtc is active, but connector isn't\n");
  4998. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  4999. "best encoder set without crtc!\n");
  5000. }
  5001. }
  5002. int intel_connector_init(struct intel_connector *connector)
  5003. {
  5004. drm_atomic_helper_connector_reset(&connector->base);
  5005. if (!connector->base.state)
  5006. return -ENOMEM;
  5007. return 0;
  5008. }
  5009. struct intel_connector *intel_connector_alloc(void)
  5010. {
  5011. struct intel_connector *connector;
  5012. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5013. if (!connector)
  5014. return NULL;
  5015. if (intel_connector_init(connector) < 0) {
  5016. kfree(connector);
  5017. return NULL;
  5018. }
  5019. return connector;
  5020. }
  5021. /* Simple connector->get_hw_state implementation for encoders that support only
  5022. * one connector and no cloning and hence the encoder state determines the state
  5023. * of the connector. */
  5024. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5025. {
  5026. enum pipe pipe = 0;
  5027. struct intel_encoder *encoder = connector->encoder;
  5028. return encoder->get_hw_state(encoder, &pipe);
  5029. }
  5030. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5031. {
  5032. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5033. return crtc_state->fdi_lanes;
  5034. return 0;
  5035. }
  5036. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5037. struct intel_crtc_state *pipe_config)
  5038. {
  5039. struct drm_i915_private *dev_priv = to_i915(dev);
  5040. struct drm_atomic_state *state = pipe_config->base.state;
  5041. struct intel_crtc *other_crtc;
  5042. struct intel_crtc_state *other_crtc_state;
  5043. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5044. pipe_name(pipe), pipe_config->fdi_lanes);
  5045. if (pipe_config->fdi_lanes > 4) {
  5046. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5047. pipe_name(pipe), pipe_config->fdi_lanes);
  5048. return -EINVAL;
  5049. }
  5050. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  5051. if (pipe_config->fdi_lanes > 2) {
  5052. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5053. pipe_config->fdi_lanes);
  5054. return -EINVAL;
  5055. } else {
  5056. return 0;
  5057. }
  5058. }
  5059. if (INTEL_INFO(dev_priv)->num_pipes == 2)
  5060. return 0;
  5061. /* Ivybridge 3 pipe is really complicated */
  5062. switch (pipe) {
  5063. case PIPE_A:
  5064. return 0;
  5065. case PIPE_B:
  5066. if (pipe_config->fdi_lanes <= 2)
  5067. return 0;
  5068. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
  5069. other_crtc_state =
  5070. intel_atomic_get_crtc_state(state, other_crtc);
  5071. if (IS_ERR(other_crtc_state))
  5072. return PTR_ERR(other_crtc_state);
  5073. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5074. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5075. pipe_name(pipe), pipe_config->fdi_lanes);
  5076. return -EINVAL;
  5077. }
  5078. return 0;
  5079. case PIPE_C:
  5080. if (pipe_config->fdi_lanes > 2) {
  5081. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5082. pipe_name(pipe), pipe_config->fdi_lanes);
  5083. return -EINVAL;
  5084. }
  5085. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
  5086. other_crtc_state =
  5087. intel_atomic_get_crtc_state(state, other_crtc);
  5088. if (IS_ERR(other_crtc_state))
  5089. return PTR_ERR(other_crtc_state);
  5090. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5091. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5092. return -EINVAL;
  5093. }
  5094. return 0;
  5095. default:
  5096. BUG();
  5097. }
  5098. }
  5099. #define RETRY 1
  5100. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5101. struct intel_crtc_state *pipe_config)
  5102. {
  5103. struct drm_device *dev = intel_crtc->base.dev;
  5104. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5105. int lane, link_bw, fdi_dotclock, ret;
  5106. bool needs_recompute = false;
  5107. retry:
  5108. /* FDI is a binary signal running at ~2.7GHz, encoding
  5109. * each output octet as 10 bits. The actual frequency
  5110. * is stored as a divider into a 100MHz clock, and the
  5111. * mode pixel clock is stored in units of 1KHz.
  5112. * Hence the bw of each lane in terms of the mode signal
  5113. * is:
  5114. */
  5115. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5116. fdi_dotclock = adjusted_mode->crtc_clock;
  5117. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5118. pipe_config->pipe_bpp);
  5119. pipe_config->fdi_lanes = lane;
  5120. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5121. link_bw, &pipe_config->fdi_m_n);
  5122. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5123. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5124. pipe_config->pipe_bpp -= 2*3;
  5125. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5126. pipe_config->pipe_bpp);
  5127. needs_recompute = true;
  5128. pipe_config->bw_constrained = true;
  5129. goto retry;
  5130. }
  5131. if (needs_recompute)
  5132. return RETRY;
  5133. return ret;
  5134. }
  5135. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  5136. struct intel_crtc_state *pipe_config)
  5137. {
  5138. if (pipe_config->pipe_bpp > 24)
  5139. return false;
  5140. /* HSW can handle pixel rate up to cdclk? */
  5141. if (IS_HASWELL(dev_priv))
  5142. return true;
  5143. /*
  5144. * We compare against max which means we must take
  5145. * the increased cdclk requirement into account when
  5146. * calculating the new cdclk.
  5147. *
  5148. * Should measure whether using a lower cdclk w/o IPS
  5149. */
  5150. return pipe_config->pixel_rate <=
  5151. dev_priv->max_cdclk_freq * 95 / 100;
  5152. }
  5153. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  5154. struct intel_crtc_state *pipe_config)
  5155. {
  5156. struct drm_device *dev = crtc->base.dev;
  5157. struct drm_i915_private *dev_priv = to_i915(dev);
  5158. pipe_config->ips_enabled = i915.enable_ips &&
  5159. hsw_crtc_supports_ips(crtc) &&
  5160. pipe_config_supports_ips(dev_priv, pipe_config);
  5161. }
  5162. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  5163. {
  5164. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5165. /* GDG double wide on either pipe, otherwise pipe A only */
  5166. return INTEL_INFO(dev_priv)->gen < 4 &&
  5167. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  5168. }
  5169. static uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
  5170. {
  5171. uint32_t pixel_rate;
  5172. pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
  5173. /*
  5174. * We only use IF-ID interlacing. If we ever use
  5175. * PF-ID we'll need to adjust the pixel_rate here.
  5176. */
  5177. if (pipe_config->pch_pfit.enabled) {
  5178. uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
  5179. uint32_t pfit_size = pipe_config->pch_pfit.size;
  5180. pipe_w = pipe_config->pipe_src_w;
  5181. pipe_h = pipe_config->pipe_src_h;
  5182. pfit_w = (pfit_size >> 16) & 0xFFFF;
  5183. pfit_h = pfit_size & 0xFFFF;
  5184. if (pipe_w < pfit_w)
  5185. pipe_w = pfit_w;
  5186. if (pipe_h < pfit_h)
  5187. pipe_h = pfit_h;
  5188. if (WARN_ON(!pfit_w || !pfit_h))
  5189. return pixel_rate;
  5190. pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
  5191. pfit_w * pfit_h);
  5192. }
  5193. return pixel_rate;
  5194. }
  5195. static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
  5196. {
  5197. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  5198. if (HAS_GMCH_DISPLAY(dev_priv))
  5199. /* FIXME calculate proper pipe pixel rate for GMCH pfit */
  5200. crtc_state->pixel_rate =
  5201. crtc_state->base.adjusted_mode.crtc_clock;
  5202. else
  5203. crtc_state->pixel_rate =
  5204. ilk_pipe_pixel_rate(crtc_state);
  5205. }
  5206. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  5207. struct intel_crtc_state *pipe_config)
  5208. {
  5209. struct drm_device *dev = crtc->base.dev;
  5210. struct drm_i915_private *dev_priv = to_i915(dev);
  5211. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5212. int clock_limit = dev_priv->max_dotclk_freq;
  5213. if (INTEL_GEN(dev_priv) < 4) {
  5214. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  5215. /*
  5216. * Enable double wide mode when the dot clock
  5217. * is > 90% of the (display) core speed.
  5218. */
  5219. if (intel_crtc_supports_double_wide(crtc) &&
  5220. adjusted_mode->crtc_clock > clock_limit) {
  5221. clock_limit = dev_priv->max_dotclk_freq;
  5222. pipe_config->double_wide = true;
  5223. }
  5224. }
  5225. if (adjusted_mode->crtc_clock > clock_limit) {
  5226. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  5227. adjusted_mode->crtc_clock, clock_limit,
  5228. yesno(pipe_config->double_wide));
  5229. return -EINVAL;
  5230. }
  5231. /*
  5232. * Pipe horizontal size must be even in:
  5233. * - DVO ganged mode
  5234. * - LVDS dual channel mode
  5235. * - Double wide pipe
  5236. */
  5237. if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  5238. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  5239. pipe_config->pipe_src_w &= ~1;
  5240. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  5241. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  5242. */
  5243. if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
  5244. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  5245. return -EINVAL;
  5246. intel_crtc_compute_pixel_rate(pipe_config);
  5247. if (HAS_IPS(dev_priv))
  5248. hsw_compute_ips_config(crtc, pipe_config);
  5249. if (pipe_config->has_pch_encoder)
  5250. return ironlake_fdi_compute_config(crtc, pipe_config);
  5251. return 0;
  5252. }
  5253. static void
  5254. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  5255. {
  5256. while (*num > DATA_LINK_M_N_MASK ||
  5257. *den > DATA_LINK_M_N_MASK) {
  5258. *num >>= 1;
  5259. *den >>= 1;
  5260. }
  5261. }
  5262. static void compute_m_n(unsigned int m, unsigned int n,
  5263. uint32_t *ret_m, uint32_t *ret_n)
  5264. {
  5265. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  5266. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  5267. intel_reduce_m_n_ratio(ret_m, ret_n);
  5268. }
  5269. void
  5270. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  5271. int pixel_clock, int link_clock,
  5272. struct intel_link_m_n *m_n)
  5273. {
  5274. m_n->tu = 64;
  5275. compute_m_n(bits_per_pixel * pixel_clock,
  5276. link_clock * nlanes * 8,
  5277. &m_n->gmch_m, &m_n->gmch_n);
  5278. compute_m_n(pixel_clock, link_clock,
  5279. &m_n->link_m, &m_n->link_n);
  5280. }
  5281. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  5282. {
  5283. if (i915.panel_use_ssc >= 0)
  5284. return i915.panel_use_ssc != 0;
  5285. return dev_priv->vbt.lvds_use_ssc
  5286. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  5287. }
  5288. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  5289. {
  5290. return (1 << dpll->n) << 16 | dpll->m2;
  5291. }
  5292. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  5293. {
  5294. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  5295. }
  5296. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  5297. struct intel_crtc_state *crtc_state,
  5298. struct dpll *reduced_clock)
  5299. {
  5300. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5301. u32 fp, fp2 = 0;
  5302. if (IS_PINEVIEW(dev_priv)) {
  5303. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  5304. if (reduced_clock)
  5305. fp2 = pnv_dpll_compute_fp(reduced_clock);
  5306. } else {
  5307. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  5308. if (reduced_clock)
  5309. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  5310. }
  5311. crtc_state->dpll_hw_state.fp0 = fp;
  5312. crtc->lowfreq_avail = false;
  5313. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5314. reduced_clock) {
  5315. crtc_state->dpll_hw_state.fp1 = fp2;
  5316. crtc->lowfreq_avail = true;
  5317. } else {
  5318. crtc_state->dpll_hw_state.fp1 = fp;
  5319. }
  5320. }
  5321. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  5322. pipe)
  5323. {
  5324. u32 reg_val;
  5325. /*
  5326. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  5327. * and set it to a reasonable value instead.
  5328. */
  5329. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5330. reg_val &= 0xffffff00;
  5331. reg_val |= 0x00000030;
  5332. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5333. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5334. reg_val &= 0x8cffffff;
  5335. reg_val = 0x8c000000;
  5336. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5337. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  5338. reg_val &= 0xffffff00;
  5339. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  5340. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  5341. reg_val &= 0x00ffffff;
  5342. reg_val |= 0xb0000000;
  5343. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  5344. }
  5345. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  5346. struct intel_link_m_n *m_n)
  5347. {
  5348. struct drm_device *dev = crtc->base.dev;
  5349. struct drm_i915_private *dev_priv = to_i915(dev);
  5350. int pipe = crtc->pipe;
  5351. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5352. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  5353. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  5354. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  5355. }
  5356. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  5357. struct intel_link_m_n *m_n,
  5358. struct intel_link_m_n *m2_n2)
  5359. {
  5360. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5361. int pipe = crtc->pipe;
  5362. enum transcoder transcoder = crtc->config->cpu_transcoder;
  5363. if (INTEL_GEN(dev_priv) >= 5) {
  5364. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5365. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  5366. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  5367. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  5368. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  5369. * for gen < 8) and if DRRS is supported (to make sure the
  5370. * registers are not unnecessarily accessed).
  5371. */
  5372. if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
  5373. INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
  5374. I915_WRITE(PIPE_DATA_M2(transcoder),
  5375. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  5376. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  5377. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  5378. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  5379. }
  5380. } else {
  5381. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  5382. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  5383. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  5384. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  5385. }
  5386. }
  5387. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  5388. {
  5389. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  5390. if (m_n == M1_N1) {
  5391. dp_m_n = &crtc->config->dp_m_n;
  5392. dp_m2_n2 = &crtc->config->dp_m2_n2;
  5393. } else if (m_n == M2_N2) {
  5394. /*
  5395. * M2_N2 registers are not supported. Hence m2_n2 divider value
  5396. * needs to be programmed into M1_N1.
  5397. */
  5398. dp_m_n = &crtc->config->dp_m2_n2;
  5399. } else {
  5400. DRM_ERROR("Unsupported divider value\n");
  5401. return;
  5402. }
  5403. if (crtc->config->has_pch_encoder)
  5404. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  5405. else
  5406. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  5407. }
  5408. static void vlv_compute_dpll(struct intel_crtc *crtc,
  5409. struct intel_crtc_state *pipe_config)
  5410. {
  5411. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  5412. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5413. if (crtc->pipe != PIPE_A)
  5414. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5415. /* DPLL not used with DSI, but still need the rest set up */
  5416. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5417. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  5418. DPLL_EXT_BUFFER_ENABLE_VLV;
  5419. pipe_config->dpll_hw_state.dpll_md =
  5420. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5421. }
  5422. static void chv_compute_dpll(struct intel_crtc *crtc,
  5423. struct intel_crtc_state *pipe_config)
  5424. {
  5425. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  5426. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  5427. if (crtc->pipe != PIPE_A)
  5428. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  5429. /* DPLL not used with DSI, but still need the rest set up */
  5430. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  5431. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  5432. pipe_config->dpll_hw_state.dpll_md =
  5433. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5434. }
  5435. static void vlv_prepare_pll(struct intel_crtc *crtc,
  5436. const struct intel_crtc_state *pipe_config)
  5437. {
  5438. struct drm_device *dev = crtc->base.dev;
  5439. struct drm_i915_private *dev_priv = to_i915(dev);
  5440. enum pipe pipe = crtc->pipe;
  5441. u32 mdiv;
  5442. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  5443. u32 coreclk, reg_val;
  5444. /* Enable Refclk */
  5445. I915_WRITE(DPLL(pipe),
  5446. pipe_config->dpll_hw_state.dpll &
  5447. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  5448. /* No need to actually set up the DPLL with DSI */
  5449. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5450. return;
  5451. mutex_lock(&dev_priv->sb_lock);
  5452. bestn = pipe_config->dpll.n;
  5453. bestm1 = pipe_config->dpll.m1;
  5454. bestm2 = pipe_config->dpll.m2;
  5455. bestp1 = pipe_config->dpll.p1;
  5456. bestp2 = pipe_config->dpll.p2;
  5457. /* See eDP HDMI DPIO driver vbios notes doc */
  5458. /* PLL B needs special handling */
  5459. if (pipe == PIPE_B)
  5460. vlv_pllb_recal_opamp(dev_priv, pipe);
  5461. /* Set up Tx target for periodic Rcomp update */
  5462. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  5463. /* Disable target IRef on PLL */
  5464. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  5465. reg_val &= 0x00ffffff;
  5466. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  5467. /* Disable fast lock */
  5468. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  5469. /* Set idtafcrecal before PLL is enabled */
  5470. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  5471. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  5472. mdiv |= ((bestn << DPIO_N_SHIFT));
  5473. mdiv |= (1 << DPIO_K_SHIFT);
  5474. /*
  5475. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  5476. * but we don't support that).
  5477. * Note: don't use the DAC post divider as it seems unstable.
  5478. */
  5479. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  5480. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5481. mdiv |= DPIO_ENABLE_CALIBRATION;
  5482. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  5483. /* Set HBR and RBR LPF coefficients */
  5484. if (pipe_config->port_clock == 162000 ||
  5485. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
  5486. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
  5487. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5488. 0x009f0003);
  5489. else
  5490. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  5491. 0x00d0000f);
  5492. if (intel_crtc_has_dp_encoder(pipe_config)) {
  5493. /* Use SSC source */
  5494. if (pipe == PIPE_A)
  5495. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5496. 0x0df40000);
  5497. else
  5498. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5499. 0x0df70000);
  5500. } else { /* HDMI or VGA */
  5501. /* Use bend source */
  5502. if (pipe == PIPE_A)
  5503. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5504. 0x0df70000);
  5505. else
  5506. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  5507. 0x0df40000);
  5508. }
  5509. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  5510. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  5511. if (intel_crtc_has_dp_encoder(crtc->config))
  5512. coreclk |= 0x01000000;
  5513. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  5514. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  5515. mutex_unlock(&dev_priv->sb_lock);
  5516. }
  5517. static void chv_prepare_pll(struct intel_crtc *crtc,
  5518. const struct intel_crtc_state *pipe_config)
  5519. {
  5520. struct drm_device *dev = crtc->base.dev;
  5521. struct drm_i915_private *dev_priv = to_i915(dev);
  5522. enum pipe pipe = crtc->pipe;
  5523. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  5524. u32 loopfilter, tribuf_calcntr;
  5525. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  5526. u32 dpio_val;
  5527. int vco;
  5528. /* Enable Refclk and SSC */
  5529. I915_WRITE(DPLL(pipe),
  5530. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  5531. /* No need to actually set up the DPLL with DSI */
  5532. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  5533. return;
  5534. bestn = pipe_config->dpll.n;
  5535. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  5536. bestm1 = pipe_config->dpll.m1;
  5537. bestm2 = pipe_config->dpll.m2 >> 22;
  5538. bestp1 = pipe_config->dpll.p1;
  5539. bestp2 = pipe_config->dpll.p2;
  5540. vco = pipe_config->dpll.vco;
  5541. dpio_val = 0;
  5542. loopfilter = 0;
  5543. mutex_lock(&dev_priv->sb_lock);
  5544. /* p1 and p2 divider */
  5545. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  5546. 5 << DPIO_CHV_S1_DIV_SHIFT |
  5547. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  5548. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  5549. 1 << DPIO_CHV_K_DIV_SHIFT);
  5550. /* Feedback post-divider - m2 */
  5551. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  5552. /* Feedback refclk divider - n and m1 */
  5553. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  5554. DPIO_CHV_M1_DIV_BY_2 |
  5555. 1 << DPIO_CHV_N_DIV_SHIFT);
  5556. /* M2 fraction division */
  5557. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  5558. /* M2 fraction division enable */
  5559. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  5560. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  5561. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  5562. if (bestm2_frac)
  5563. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  5564. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  5565. /* Program digital lock detect threshold */
  5566. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  5567. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  5568. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  5569. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  5570. if (!bestm2_frac)
  5571. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  5572. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  5573. /* Loop filter */
  5574. if (vco == 5400000) {
  5575. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  5576. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  5577. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5578. tribuf_calcntr = 0x9;
  5579. } else if (vco <= 6200000) {
  5580. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  5581. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  5582. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5583. tribuf_calcntr = 0x9;
  5584. } else if (vco <= 6480000) {
  5585. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5586. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5587. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5588. tribuf_calcntr = 0x8;
  5589. } else {
  5590. /* Not supported. Apply the same limits as in the max case */
  5591. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  5592. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  5593. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  5594. tribuf_calcntr = 0;
  5595. }
  5596. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  5597. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  5598. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  5599. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  5600. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  5601. /* AFC Recal */
  5602. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  5603. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  5604. DPIO_AFC_RECAL);
  5605. mutex_unlock(&dev_priv->sb_lock);
  5606. }
  5607. /**
  5608. * vlv_force_pll_on - forcibly enable just the PLL
  5609. * @dev_priv: i915 private structure
  5610. * @pipe: pipe PLL to enable
  5611. * @dpll: PLL configuration
  5612. *
  5613. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  5614. * in cases where we need the PLL enabled even when @pipe is not going to
  5615. * be enabled.
  5616. */
  5617. int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
  5618. const struct dpll *dpll)
  5619. {
  5620. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  5621. struct intel_crtc_state *pipe_config;
  5622. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  5623. if (!pipe_config)
  5624. return -ENOMEM;
  5625. pipe_config->base.crtc = &crtc->base;
  5626. pipe_config->pixel_multiplier = 1;
  5627. pipe_config->dpll = *dpll;
  5628. if (IS_CHERRYVIEW(dev_priv)) {
  5629. chv_compute_dpll(crtc, pipe_config);
  5630. chv_prepare_pll(crtc, pipe_config);
  5631. chv_enable_pll(crtc, pipe_config);
  5632. } else {
  5633. vlv_compute_dpll(crtc, pipe_config);
  5634. vlv_prepare_pll(crtc, pipe_config);
  5635. vlv_enable_pll(crtc, pipe_config);
  5636. }
  5637. kfree(pipe_config);
  5638. return 0;
  5639. }
  5640. /**
  5641. * vlv_force_pll_off - forcibly disable just the PLL
  5642. * @dev_priv: i915 private structure
  5643. * @pipe: pipe PLL to disable
  5644. *
  5645. * Disable the PLL for @pipe. To be used in cases where we need
  5646. * the PLL enabled even when @pipe is not going to be enabled.
  5647. */
  5648. void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
  5649. {
  5650. if (IS_CHERRYVIEW(dev_priv))
  5651. chv_disable_pll(dev_priv, pipe);
  5652. else
  5653. vlv_disable_pll(dev_priv, pipe);
  5654. }
  5655. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  5656. struct intel_crtc_state *crtc_state,
  5657. struct dpll *reduced_clock)
  5658. {
  5659. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  5660. u32 dpll;
  5661. struct dpll *clock = &crtc_state->dpll;
  5662. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5663. dpll = DPLL_VGA_MODE_DIS;
  5664. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  5665. dpll |= DPLLB_MODE_LVDS;
  5666. else
  5667. dpll |= DPLLB_MODE_DAC_SERIAL;
  5668. if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  5669. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  5670. dpll |= (crtc_state->pixel_multiplier - 1)
  5671. << SDVO_MULTIPLIER_SHIFT_HIRES;
  5672. }
  5673. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  5674. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  5675. dpll |= DPLL_SDVO_HIGH_SPEED;
  5676. if (intel_crtc_has_dp_encoder(crtc_state))
  5677. dpll |= DPLL_SDVO_HIGH_SPEED;
  5678. /* compute bitmask from p1 value */
  5679. if (IS_PINEVIEW(dev_priv))
  5680. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  5681. else {
  5682. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5683. if (IS_G4X(dev_priv) && reduced_clock)
  5684. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  5685. }
  5686. switch (clock->p2) {
  5687. case 5:
  5688. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  5689. break;
  5690. case 7:
  5691. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  5692. break;
  5693. case 10:
  5694. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  5695. break;
  5696. case 14:
  5697. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  5698. break;
  5699. }
  5700. if (INTEL_GEN(dev_priv) >= 4)
  5701. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  5702. if (crtc_state->sdvo_tv_clock)
  5703. dpll |= PLL_REF_INPUT_TVCLKINBC;
  5704. else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5705. intel_panel_use_ssc(dev_priv))
  5706. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5707. else
  5708. dpll |= PLL_REF_INPUT_DREFCLK;
  5709. dpll |= DPLL_VCO_ENABLE;
  5710. crtc_state->dpll_hw_state.dpll = dpll;
  5711. if (INTEL_GEN(dev_priv) >= 4) {
  5712. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  5713. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  5714. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  5715. }
  5716. }
  5717. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  5718. struct intel_crtc_state *crtc_state,
  5719. struct dpll *reduced_clock)
  5720. {
  5721. struct drm_device *dev = crtc->base.dev;
  5722. struct drm_i915_private *dev_priv = to_i915(dev);
  5723. u32 dpll;
  5724. struct dpll *clock = &crtc_state->dpll;
  5725. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  5726. dpll = DPLL_VGA_MODE_DIS;
  5727. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  5728. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5729. } else {
  5730. if (clock->p1 == 2)
  5731. dpll |= PLL_P1_DIVIDE_BY_TWO;
  5732. else
  5733. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  5734. if (clock->p2 == 4)
  5735. dpll |= PLL_P2_DIVIDE_BY_4;
  5736. }
  5737. if (!IS_I830(dev_priv) &&
  5738. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
  5739. dpll |= DPLL_DVO_2X_MODE;
  5740. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  5741. intel_panel_use_ssc(dev_priv))
  5742. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  5743. else
  5744. dpll |= PLL_REF_INPUT_DREFCLK;
  5745. dpll |= DPLL_VCO_ENABLE;
  5746. crtc_state->dpll_hw_state.dpll = dpll;
  5747. }
  5748. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  5749. {
  5750. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  5751. enum pipe pipe = intel_crtc->pipe;
  5752. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  5753. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  5754. uint32_t crtc_vtotal, crtc_vblank_end;
  5755. int vsyncshift = 0;
  5756. /* We need to be careful not to changed the adjusted mode, for otherwise
  5757. * the hw state checker will get angry at the mismatch. */
  5758. crtc_vtotal = adjusted_mode->crtc_vtotal;
  5759. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  5760. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  5761. /* the chip adds 2 halflines automatically */
  5762. crtc_vtotal -= 1;
  5763. crtc_vblank_end -= 1;
  5764. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  5765. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  5766. else
  5767. vsyncshift = adjusted_mode->crtc_hsync_start -
  5768. adjusted_mode->crtc_htotal / 2;
  5769. if (vsyncshift < 0)
  5770. vsyncshift += adjusted_mode->crtc_htotal;
  5771. }
  5772. if (INTEL_GEN(dev_priv) > 3)
  5773. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  5774. I915_WRITE(HTOTAL(cpu_transcoder),
  5775. (adjusted_mode->crtc_hdisplay - 1) |
  5776. ((adjusted_mode->crtc_htotal - 1) << 16));
  5777. I915_WRITE(HBLANK(cpu_transcoder),
  5778. (adjusted_mode->crtc_hblank_start - 1) |
  5779. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  5780. I915_WRITE(HSYNC(cpu_transcoder),
  5781. (adjusted_mode->crtc_hsync_start - 1) |
  5782. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  5783. I915_WRITE(VTOTAL(cpu_transcoder),
  5784. (adjusted_mode->crtc_vdisplay - 1) |
  5785. ((crtc_vtotal - 1) << 16));
  5786. I915_WRITE(VBLANK(cpu_transcoder),
  5787. (adjusted_mode->crtc_vblank_start - 1) |
  5788. ((crtc_vblank_end - 1) << 16));
  5789. I915_WRITE(VSYNC(cpu_transcoder),
  5790. (adjusted_mode->crtc_vsync_start - 1) |
  5791. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  5792. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  5793. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  5794. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  5795. * bits. */
  5796. if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
  5797. (pipe == PIPE_B || pipe == PIPE_C))
  5798. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  5799. }
  5800. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  5801. {
  5802. struct drm_device *dev = intel_crtc->base.dev;
  5803. struct drm_i915_private *dev_priv = to_i915(dev);
  5804. enum pipe pipe = intel_crtc->pipe;
  5805. /* pipesrc controls the size that is scaled from, which should
  5806. * always be the user's requested size.
  5807. */
  5808. I915_WRITE(PIPESRC(pipe),
  5809. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  5810. (intel_crtc->config->pipe_src_h - 1));
  5811. }
  5812. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  5813. struct intel_crtc_state *pipe_config)
  5814. {
  5815. struct drm_device *dev = crtc->base.dev;
  5816. struct drm_i915_private *dev_priv = to_i915(dev);
  5817. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  5818. uint32_t tmp;
  5819. tmp = I915_READ(HTOTAL(cpu_transcoder));
  5820. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  5821. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  5822. tmp = I915_READ(HBLANK(cpu_transcoder));
  5823. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  5824. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  5825. tmp = I915_READ(HSYNC(cpu_transcoder));
  5826. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  5827. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  5828. tmp = I915_READ(VTOTAL(cpu_transcoder));
  5829. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  5830. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  5831. tmp = I915_READ(VBLANK(cpu_transcoder));
  5832. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  5833. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  5834. tmp = I915_READ(VSYNC(cpu_transcoder));
  5835. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  5836. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  5837. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  5838. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  5839. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  5840. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  5841. }
  5842. }
  5843. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  5844. struct intel_crtc_state *pipe_config)
  5845. {
  5846. struct drm_device *dev = crtc->base.dev;
  5847. struct drm_i915_private *dev_priv = to_i915(dev);
  5848. u32 tmp;
  5849. tmp = I915_READ(PIPESRC(crtc->pipe));
  5850. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  5851. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  5852. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  5853. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  5854. }
  5855. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  5856. struct intel_crtc_state *pipe_config)
  5857. {
  5858. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  5859. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  5860. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  5861. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  5862. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  5863. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  5864. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  5865. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  5866. mode->flags = pipe_config->base.adjusted_mode.flags;
  5867. mode->type = DRM_MODE_TYPE_DRIVER;
  5868. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  5869. mode->hsync = drm_mode_hsync(mode);
  5870. mode->vrefresh = drm_mode_vrefresh(mode);
  5871. drm_mode_set_name(mode);
  5872. }
  5873. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  5874. {
  5875. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  5876. uint32_t pipeconf;
  5877. pipeconf = 0;
  5878. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  5879. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  5880. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  5881. if (intel_crtc->config->double_wide)
  5882. pipeconf |= PIPECONF_DOUBLE_WIDE;
  5883. /* only g4x and later have fancy bpc/dither controls */
  5884. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  5885. IS_CHERRYVIEW(dev_priv)) {
  5886. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  5887. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  5888. pipeconf |= PIPECONF_DITHER_EN |
  5889. PIPECONF_DITHER_TYPE_SP;
  5890. switch (intel_crtc->config->pipe_bpp) {
  5891. case 18:
  5892. pipeconf |= PIPECONF_6BPC;
  5893. break;
  5894. case 24:
  5895. pipeconf |= PIPECONF_8BPC;
  5896. break;
  5897. case 30:
  5898. pipeconf |= PIPECONF_10BPC;
  5899. break;
  5900. default:
  5901. /* Case prevented by intel_choose_pipe_bpp_dither. */
  5902. BUG();
  5903. }
  5904. }
  5905. if (HAS_PIPE_CXSR(dev_priv)) {
  5906. if (intel_crtc->lowfreq_avail) {
  5907. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  5908. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  5909. } else {
  5910. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  5911. }
  5912. }
  5913. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  5914. if (INTEL_GEN(dev_priv) < 4 ||
  5915. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  5916. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  5917. else
  5918. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  5919. } else
  5920. pipeconf |= PIPECONF_PROGRESSIVE;
  5921. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  5922. intel_crtc->config->limited_color_range)
  5923. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  5924. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  5925. POSTING_READ(PIPECONF(intel_crtc->pipe));
  5926. }
  5927. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  5928. struct intel_crtc_state *crtc_state)
  5929. {
  5930. struct drm_device *dev = crtc->base.dev;
  5931. struct drm_i915_private *dev_priv = to_i915(dev);
  5932. const struct intel_limit *limit;
  5933. int refclk = 48000;
  5934. memset(&crtc_state->dpll_hw_state, 0,
  5935. sizeof(crtc_state->dpll_hw_state));
  5936. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  5937. if (intel_panel_use_ssc(dev_priv)) {
  5938. refclk = dev_priv->vbt.lvds_ssc_freq;
  5939. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5940. }
  5941. limit = &intel_limits_i8xx_lvds;
  5942. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
  5943. limit = &intel_limits_i8xx_dvo;
  5944. } else {
  5945. limit = &intel_limits_i8xx_dac;
  5946. }
  5947. if (!crtc_state->clock_set &&
  5948. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  5949. refclk, NULL, &crtc_state->dpll)) {
  5950. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5951. return -EINVAL;
  5952. }
  5953. i8xx_compute_dpll(crtc, crtc_state, NULL);
  5954. return 0;
  5955. }
  5956. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  5957. struct intel_crtc_state *crtc_state)
  5958. {
  5959. struct drm_device *dev = crtc->base.dev;
  5960. struct drm_i915_private *dev_priv = to_i915(dev);
  5961. const struct intel_limit *limit;
  5962. int refclk = 96000;
  5963. memset(&crtc_state->dpll_hw_state, 0,
  5964. sizeof(crtc_state->dpll_hw_state));
  5965. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  5966. if (intel_panel_use_ssc(dev_priv)) {
  5967. refclk = dev_priv->vbt.lvds_ssc_freq;
  5968. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  5969. }
  5970. if (intel_is_dual_link_lvds(dev))
  5971. limit = &intel_limits_g4x_dual_channel_lvds;
  5972. else
  5973. limit = &intel_limits_g4x_single_channel_lvds;
  5974. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  5975. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  5976. limit = &intel_limits_g4x_hdmi;
  5977. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  5978. limit = &intel_limits_g4x_sdvo;
  5979. } else {
  5980. /* The option is for other outputs */
  5981. limit = &intel_limits_i9xx_sdvo;
  5982. }
  5983. if (!crtc_state->clock_set &&
  5984. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  5985. refclk, NULL, &crtc_state->dpll)) {
  5986. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  5987. return -EINVAL;
  5988. }
  5989. i9xx_compute_dpll(crtc, crtc_state, NULL);
  5990. return 0;
  5991. }
  5992. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  5993. struct intel_crtc_state *crtc_state)
  5994. {
  5995. struct drm_device *dev = crtc->base.dev;
  5996. struct drm_i915_private *dev_priv = to_i915(dev);
  5997. const struct intel_limit *limit;
  5998. int refclk = 96000;
  5999. memset(&crtc_state->dpll_hw_state, 0,
  6000. sizeof(crtc_state->dpll_hw_state));
  6001. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6002. if (intel_panel_use_ssc(dev_priv)) {
  6003. refclk = dev_priv->vbt.lvds_ssc_freq;
  6004. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6005. }
  6006. limit = &intel_limits_pineview_lvds;
  6007. } else {
  6008. limit = &intel_limits_pineview_sdvo;
  6009. }
  6010. if (!crtc_state->clock_set &&
  6011. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6012. refclk, NULL, &crtc_state->dpll)) {
  6013. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6014. return -EINVAL;
  6015. }
  6016. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6017. return 0;
  6018. }
  6019. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  6020. struct intel_crtc_state *crtc_state)
  6021. {
  6022. struct drm_device *dev = crtc->base.dev;
  6023. struct drm_i915_private *dev_priv = to_i915(dev);
  6024. const struct intel_limit *limit;
  6025. int refclk = 96000;
  6026. memset(&crtc_state->dpll_hw_state, 0,
  6027. sizeof(crtc_state->dpll_hw_state));
  6028. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6029. if (intel_panel_use_ssc(dev_priv)) {
  6030. refclk = dev_priv->vbt.lvds_ssc_freq;
  6031. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  6032. }
  6033. limit = &intel_limits_i9xx_lvds;
  6034. } else {
  6035. limit = &intel_limits_i9xx_sdvo;
  6036. }
  6037. if (!crtc_state->clock_set &&
  6038. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6039. refclk, NULL, &crtc_state->dpll)) {
  6040. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6041. return -EINVAL;
  6042. }
  6043. i9xx_compute_dpll(crtc, crtc_state, NULL);
  6044. return 0;
  6045. }
  6046. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  6047. struct intel_crtc_state *crtc_state)
  6048. {
  6049. int refclk = 100000;
  6050. const struct intel_limit *limit = &intel_limits_chv;
  6051. memset(&crtc_state->dpll_hw_state, 0,
  6052. sizeof(crtc_state->dpll_hw_state));
  6053. if (!crtc_state->clock_set &&
  6054. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6055. refclk, NULL, &crtc_state->dpll)) {
  6056. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6057. return -EINVAL;
  6058. }
  6059. chv_compute_dpll(crtc, crtc_state);
  6060. return 0;
  6061. }
  6062. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  6063. struct intel_crtc_state *crtc_state)
  6064. {
  6065. int refclk = 100000;
  6066. const struct intel_limit *limit = &intel_limits_vlv;
  6067. memset(&crtc_state->dpll_hw_state, 0,
  6068. sizeof(crtc_state->dpll_hw_state));
  6069. if (!crtc_state->clock_set &&
  6070. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6071. refclk, NULL, &crtc_state->dpll)) {
  6072. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6073. return -EINVAL;
  6074. }
  6075. vlv_compute_dpll(crtc, crtc_state);
  6076. return 0;
  6077. }
  6078. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  6079. struct intel_crtc_state *pipe_config)
  6080. {
  6081. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6082. uint32_t tmp;
  6083. if (INTEL_GEN(dev_priv) <= 3 &&
  6084. (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
  6085. return;
  6086. tmp = I915_READ(PFIT_CONTROL);
  6087. if (!(tmp & PFIT_ENABLE))
  6088. return;
  6089. /* Check whether the pfit is attached to our pipe. */
  6090. if (INTEL_GEN(dev_priv) < 4) {
  6091. if (crtc->pipe != PIPE_B)
  6092. return;
  6093. } else {
  6094. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  6095. return;
  6096. }
  6097. pipe_config->gmch_pfit.control = tmp;
  6098. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  6099. }
  6100. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  6101. struct intel_crtc_state *pipe_config)
  6102. {
  6103. struct drm_device *dev = crtc->base.dev;
  6104. struct drm_i915_private *dev_priv = to_i915(dev);
  6105. int pipe = pipe_config->cpu_transcoder;
  6106. struct dpll clock;
  6107. u32 mdiv;
  6108. int refclk = 100000;
  6109. /* In case of DSI, DPLL will not be used */
  6110. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6111. return;
  6112. mutex_lock(&dev_priv->sb_lock);
  6113. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  6114. mutex_unlock(&dev_priv->sb_lock);
  6115. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  6116. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  6117. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  6118. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  6119. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  6120. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  6121. }
  6122. static void
  6123. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  6124. struct intel_initial_plane_config *plane_config)
  6125. {
  6126. struct drm_device *dev = crtc->base.dev;
  6127. struct drm_i915_private *dev_priv = to_i915(dev);
  6128. u32 val, base, offset;
  6129. int pipe = crtc->pipe, plane = crtc->plane;
  6130. int fourcc, pixel_format;
  6131. unsigned int aligned_height;
  6132. struct drm_framebuffer *fb;
  6133. struct intel_framebuffer *intel_fb;
  6134. val = I915_READ(DSPCNTR(plane));
  6135. if (!(val & DISPLAY_PLANE_ENABLE))
  6136. return;
  6137. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6138. if (!intel_fb) {
  6139. DRM_DEBUG_KMS("failed to alloc fb\n");
  6140. return;
  6141. }
  6142. fb = &intel_fb->base;
  6143. fb->dev = dev;
  6144. if (INTEL_GEN(dev_priv) >= 4) {
  6145. if (val & DISPPLANE_TILED) {
  6146. plane_config->tiling = I915_TILING_X;
  6147. fb->modifier = I915_FORMAT_MOD_X_TILED;
  6148. }
  6149. }
  6150. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  6151. fourcc = i9xx_format_to_fourcc(pixel_format);
  6152. fb->format = drm_format_info(fourcc);
  6153. if (INTEL_GEN(dev_priv) >= 4) {
  6154. if (plane_config->tiling)
  6155. offset = I915_READ(DSPTILEOFF(plane));
  6156. else
  6157. offset = I915_READ(DSPLINOFF(plane));
  6158. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  6159. } else {
  6160. base = I915_READ(DSPADDR(plane));
  6161. }
  6162. plane_config->base = base;
  6163. val = I915_READ(PIPESRC(pipe));
  6164. fb->width = ((val >> 16) & 0xfff) + 1;
  6165. fb->height = ((val >> 0) & 0xfff) + 1;
  6166. val = I915_READ(DSPSTRIDE(pipe));
  6167. fb->pitches[0] = val & 0xffffffc0;
  6168. aligned_height = intel_fb_align_height(dev_priv,
  6169. fb->height,
  6170. fb->format->format,
  6171. fb->modifier);
  6172. plane_config->size = fb->pitches[0] * aligned_height;
  6173. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  6174. pipe_name(pipe), plane, fb->width, fb->height,
  6175. fb->format->cpp[0] * 8, base, fb->pitches[0],
  6176. plane_config->size);
  6177. plane_config->fb = intel_fb;
  6178. }
  6179. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  6180. struct intel_crtc_state *pipe_config)
  6181. {
  6182. struct drm_device *dev = crtc->base.dev;
  6183. struct drm_i915_private *dev_priv = to_i915(dev);
  6184. int pipe = pipe_config->cpu_transcoder;
  6185. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6186. struct dpll clock;
  6187. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  6188. int refclk = 100000;
  6189. /* In case of DSI, DPLL will not be used */
  6190. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6191. return;
  6192. mutex_lock(&dev_priv->sb_lock);
  6193. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  6194. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  6195. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  6196. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  6197. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6198. mutex_unlock(&dev_priv->sb_lock);
  6199. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  6200. clock.m2 = (pll_dw0 & 0xff) << 22;
  6201. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  6202. clock.m2 |= pll_dw2 & 0x3fffff;
  6203. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  6204. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  6205. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  6206. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  6207. }
  6208. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  6209. struct intel_crtc_state *pipe_config)
  6210. {
  6211. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6212. enum intel_display_power_domain power_domain;
  6213. uint32_t tmp;
  6214. bool ret;
  6215. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  6216. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  6217. return false;
  6218. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  6219. pipe_config->shared_dpll = NULL;
  6220. ret = false;
  6221. tmp = I915_READ(PIPECONF(crtc->pipe));
  6222. if (!(tmp & PIPECONF_ENABLE))
  6223. goto out;
  6224. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  6225. IS_CHERRYVIEW(dev_priv)) {
  6226. switch (tmp & PIPECONF_BPC_MASK) {
  6227. case PIPECONF_6BPC:
  6228. pipe_config->pipe_bpp = 18;
  6229. break;
  6230. case PIPECONF_8BPC:
  6231. pipe_config->pipe_bpp = 24;
  6232. break;
  6233. case PIPECONF_10BPC:
  6234. pipe_config->pipe_bpp = 30;
  6235. break;
  6236. default:
  6237. break;
  6238. }
  6239. }
  6240. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  6241. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  6242. pipe_config->limited_color_range = true;
  6243. if (INTEL_GEN(dev_priv) < 4)
  6244. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  6245. intel_get_pipe_timings(crtc, pipe_config);
  6246. intel_get_pipe_src_size(crtc, pipe_config);
  6247. i9xx_get_pfit_config(crtc, pipe_config);
  6248. if (INTEL_GEN(dev_priv) >= 4) {
  6249. /* No way to read it out on pipes B and C */
  6250. if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
  6251. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  6252. else
  6253. tmp = I915_READ(DPLL_MD(crtc->pipe));
  6254. pipe_config->pixel_multiplier =
  6255. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  6256. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  6257. pipe_config->dpll_hw_state.dpll_md = tmp;
  6258. } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  6259. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  6260. tmp = I915_READ(DPLL(crtc->pipe));
  6261. pipe_config->pixel_multiplier =
  6262. ((tmp & SDVO_MULTIPLIER_MASK)
  6263. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  6264. } else {
  6265. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  6266. * port and will be fixed up in the encoder->get_config
  6267. * function. */
  6268. pipe_config->pixel_multiplier = 1;
  6269. }
  6270. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  6271. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  6272. /*
  6273. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  6274. * on 830. Filter it out here so that we don't
  6275. * report errors due to that.
  6276. */
  6277. if (IS_I830(dev_priv))
  6278. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  6279. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  6280. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  6281. } else {
  6282. /* Mask out read-only status bits. */
  6283. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  6284. DPLL_PORTC_READY_MASK |
  6285. DPLL_PORTB_READY_MASK);
  6286. }
  6287. if (IS_CHERRYVIEW(dev_priv))
  6288. chv_crtc_clock_get(crtc, pipe_config);
  6289. else if (IS_VALLEYVIEW(dev_priv))
  6290. vlv_crtc_clock_get(crtc, pipe_config);
  6291. else
  6292. i9xx_crtc_clock_get(crtc, pipe_config);
  6293. /*
  6294. * Normally the dotclock is filled in by the encoder .get_config()
  6295. * but in case the pipe is enabled w/o any ports we need a sane
  6296. * default.
  6297. */
  6298. pipe_config->base.adjusted_mode.crtc_clock =
  6299. pipe_config->port_clock / pipe_config->pixel_multiplier;
  6300. ret = true;
  6301. out:
  6302. intel_display_power_put(dev_priv, power_domain);
  6303. return ret;
  6304. }
  6305. static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
  6306. {
  6307. struct intel_encoder *encoder;
  6308. int i;
  6309. u32 val, final;
  6310. bool has_lvds = false;
  6311. bool has_cpu_edp = false;
  6312. bool has_panel = false;
  6313. bool has_ck505 = false;
  6314. bool can_ssc = false;
  6315. bool using_ssc_source = false;
  6316. /* We need to take the global config into account */
  6317. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6318. switch (encoder->type) {
  6319. case INTEL_OUTPUT_LVDS:
  6320. has_panel = true;
  6321. has_lvds = true;
  6322. break;
  6323. case INTEL_OUTPUT_EDP:
  6324. has_panel = true;
  6325. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  6326. has_cpu_edp = true;
  6327. break;
  6328. default:
  6329. break;
  6330. }
  6331. }
  6332. if (HAS_PCH_IBX(dev_priv)) {
  6333. has_ck505 = dev_priv->vbt.display_clock_mode;
  6334. can_ssc = has_ck505;
  6335. } else {
  6336. has_ck505 = false;
  6337. can_ssc = true;
  6338. }
  6339. /* Check if any DPLLs are using the SSC source */
  6340. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  6341. u32 temp = I915_READ(PCH_DPLL(i));
  6342. if (!(temp & DPLL_VCO_ENABLE))
  6343. continue;
  6344. if ((temp & PLL_REF_INPUT_MASK) ==
  6345. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  6346. using_ssc_source = true;
  6347. break;
  6348. }
  6349. }
  6350. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  6351. has_panel, has_lvds, has_ck505, using_ssc_source);
  6352. /* Ironlake: try to setup display ref clock before DPLL
  6353. * enabling. This is only under driver's control after
  6354. * PCH B stepping, previous chipset stepping should be
  6355. * ignoring this setting.
  6356. */
  6357. val = I915_READ(PCH_DREF_CONTROL);
  6358. /* As we must carefully and slowly disable/enable each source in turn,
  6359. * compute the final state we want first and check if we need to
  6360. * make any changes at all.
  6361. */
  6362. final = val;
  6363. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  6364. if (has_ck505)
  6365. final |= DREF_NONSPREAD_CK505_ENABLE;
  6366. else
  6367. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  6368. final &= ~DREF_SSC_SOURCE_MASK;
  6369. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6370. final &= ~DREF_SSC1_ENABLE;
  6371. if (has_panel) {
  6372. final |= DREF_SSC_SOURCE_ENABLE;
  6373. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6374. final |= DREF_SSC1_ENABLE;
  6375. if (has_cpu_edp) {
  6376. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  6377. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6378. else
  6379. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6380. } else
  6381. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6382. } else if (using_ssc_source) {
  6383. final |= DREF_SSC_SOURCE_ENABLE;
  6384. final |= DREF_SSC1_ENABLE;
  6385. }
  6386. if (final == val)
  6387. return;
  6388. /* Always enable nonspread source */
  6389. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  6390. if (has_ck505)
  6391. val |= DREF_NONSPREAD_CK505_ENABLE;
  6392. else
  6393. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  6394. if (has_panel) {
  6395. val &= ~DREF_SSC_SOURCE_MASK;
  6396. val |= DREF_SSC_SOURCE_ENABLE;
  6397. /* SSC must be turned on before enabling the CPU output */
  6398. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6399. DRM_DEBUG_KMS("Using SSC on panel\n");
  6400. val |= DREF_SSC1_ENABLE;
  6401. } else
  6402. val &= ~DREF_SSC1_ENABLE;
  6403. /* Get SSC going before enabling the outputs */
  6404. I915_WRITE(PCH_DREF_CONTROL, val);
  6405. POSTING_READ(PCH_DREF_CONTROL);
  6406. udelay(200);
  6407. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6408. /* Enable CPU source on CPU attached eDP */
  6409. if (has_cpu_edp) {
  6410. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  6411. DRM_DEBUG_KMS("Using SSC on eDP\n");
  6412. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  6413. } else
  6414. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  6415. } else
  6416. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6417. I915_WRITE(PCH_DREF_CONTROL, val);
  6418. POSTING_READ(PCH_DREF_CONTROL);
  6419. udelay(200);
  6420. } else {
  6421. DRM_DEBUG_KMS("Disabling CPU source output\n");
  6422. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  6423. /* Turn off CPU output */
  6424. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  6425. I915_WRITE(PCH_DREF_CONTROL, val);
  6426. POSTING_READ(PCH_DREF_CONTROL);
  6427. udelay(200);
  6428. if (!using_ssc_source) {
  6429. DRM_DEBUG_KMS("Disabling SSC source\n");
  6430. /* Turn off the SSC source */
  6431. val &= ~DREF_SSC_SOURCE_MASK;
  6432. val |= DREF_SSC_SOURCE_DISABLE;
  6433. /* Turn off SSC1 */
  6434. val &= ~DREF_SSC1_ENABLE;
  6435. I915_WRITE(PCH_DREF_CONTROL, val);
  6436. POSTING_READ(PCH_DREF_CONTROL);
  6437. udelay(200);
  6438. }
  6439. }
  6440. BUG_ON(val != final);
  6441. }
  6442. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  6443. {
  6444. uint32_t tmp;
  6445. tmp = I915_READ(SOUTH_CHICKEN2);
  6446. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  6447. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6448. if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
  6449. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  6450. DRM_ERROR("FDI mPHY reset assert timeout\n");
  6451. tmp = I915_READ(SOUTH_CHICKEN2);
  6452. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  6453. I915_WRITE(SOUTH_CHICKEN2, tmp);
  6454. if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
  6455. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  6456. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  6457. }
  6458. /* WaMPhyProgramming:hsw */
  6459. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  6460. {
  6461. uint32_t tmp;
  6462. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  6463. tmp &= ~(0xFF << 24);
  6464. tmp |= (0x12 << 24);
  6465. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  6466. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  6467. tmp |= (1 << 11);
  6468. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  6469. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  6470. tmp |= (1 << 11);
  6471. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  6472. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  6473. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6474. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  6475. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  6476. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  6477. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  6478. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  6479. tmp &= ~(7 << 13);
  6480. tmp |= (5 << 13);
  6481. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  6482. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  6483. tmp &= ~(7 << 13);
  6484. tmp |= (5 << 13);
  6485. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  6486. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  6487. tmp &= ~0xFF;
  6488. tmp |= 0x1C;
  6489. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  6490. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  6491. tmp &= ~0xFF;
  6492. tmp |= 0x1C;
  6493. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  6494. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  6495. tmp &= ~(0xFF << 16);
  6496. tmp |= (0x1C << 16);
  6497. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  6498. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  6499. tmp &= ~(0xFF << 16);
  6500. tmp |= (0x1C << 16);
  6501. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  6502. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  6503. tmp |= (1 << 27);
  6504. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  6505. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  6506. tmp |= (1 << 27);
  6507. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  6508. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  6509. tmp &= ~(0xF << 28);
  6510. tmp |= (4 << 28);
  6511. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  6512. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  6513. tmp &= ~(0xF << 28);
  6514. tmp |= (4 << 28);
  6515. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  6516. }
  6517. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  6518. * Programming" based on the parameters passed:
  6519. * - Sequence to enable CLKOUT_DP
  6520. * - Sequence to enable CLKOUT_DP without spread
  6521. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  6522. */
  6523. static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
  6524. bool with_spread, bool with_fdi)
  6525. {
  6526. uint32_t reg, tmp;
  6527. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  6528. with_spread = true;
  6529. if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
  6530. with_fdi, "LP PCH doesn't have FDI\n"))
  6531. with_fdi = false;
  6532. mutex_lock(&dev_priv->sb_lock);
  6533. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6534. tmp &= ~SBI_SSCCTL_DISABLE;
  6535. tmp |= SBI_SSCCTL_PATHALT;
  6536. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6537. udelay(24);
  6538. if (with_spread) {
  6539. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6540. tmp &= ~SBI_SSCCTL_PATHALT;
  6541. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6542. if (with_fdi) {
  6543. lpt_reset_fdi_mphy(dev_priv);
  6544. lpt_program_fdi_mphy(dev_priv);
  6545. }
  6546. }
  6547. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6548. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6549. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6550. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6551. mutex_unlock(&dev_priv->sb_lock);
  6552. }
  6553. /* Sequence to disable CLKOUT_DP */
  6554. static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
  6555. {
  6556. uint32_t reg, tmp;
  6557. mutex_lock(&dev_priv->sb_lock);
  6558. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  6559. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  6560. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  6561. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  6562. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  6563. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  6564. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  6565. tmp |= SBI_SSCCTL_PATHALT;
  6566. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6567. udelay(32);
  6568. }
  6569. tmp |= SBI_SSCCTL_DISABLE;
  6570. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  6571. }
  6572. mutex_unlock(&dev_priv->sb_lock);
  6573. }
  6574. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  6575. static const uint16_t sscdivintphase[] = {
  6576. [BEND_IDX( 50)] = 0x3B23,
  6577. [BEND_IDX( 45)] = 0x3B23,
  6578. [BEND_IDX( 40)] = 0x3C23,
  6579. [BEND_IDX( 35)] = 0x3C23,
  6580. [BEND_IDX( 30)] = 0x3D23,
  6581. [BEND_IDX( 25)] = 0x3D23,
  6582. [BEND_IDX( 20)] = 0x3E23,
  6583. [BEND_IDX( 15)] = 0x3E23,
  6584. [BEND_IDX( 10)] = 0x3F23,
  6585. [BEND_IDX( 5)] = 0x3F23,
  6586. [BEND_IDX( 0)] = 0x0025,
  6587. [BEND_IDX( -5)] = 0x0025,
  6588. [BEND_IDX(-10)] = 0x0125,
  6589. [BEND_IDX(-15)] = 0x0125,
  6590. [BEND_IDX(-20)] = 0x0225,
  6591. [BEND_IDX(-25)] = 0x0225,
  6592. [BEND_IDX(-30)] = 0x0325,
  6593. [BEND_IDX(-35)] = 0x0325,
  6594. [BEND_IDX(-40)] = 0x0425,
  6595. [BEND_IDX(-45)] = 0x0425,
  6596. [BEND_IDX(-50)] = 0x0525,
  6597. };
  6598. /*
  6599. * Bend CLKOUT_DP
  6600. * steps -50 to 50 inclusive, in steps of 5
  6601. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  6602. * change in clock period = -(steps / 10) * 5.787 ps
  6603. */
  6604. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  6605. {
  6606. uint32_t tmp;
  6607. int idx = BEND_IDX(steps);
  6608. if (WARN_ON(steps % 5 != 0))
  6609. return;
  6610. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  6611. return;
  6612. mutex_lock(&dev_priv->sb_lock);
  6613. if (steps % 10 != 0)
  6614. tmp = 0xAAAAAAAB;
  6615. else
  6616. tmp = 0x00000000;
  6617. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  6618. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  6619. tmp &= 0xffff0000;
  6620. tmp |= sscdivintphase[idx];
  6621. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  6622. mutex_unlock(&dev_priv->sb_lock);
  6623. }
  6624. #undef BEND_IDX
  6625. static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
  6626. {
  6627. struct intel_encoder *encoder;
  6628. bool has_vga = false;
  6629. for_each_intel_encoder(&dev_priv->drm, encoder) {
  6630. switch (encoder->type) {
  6631. case INTEL_OUTPUT_ANALOG:
  6632. has_vga = true;
  6633. break;
  6634. default:
  6635. break;
  6636. }
  6637. }
  6638. if (has_vga) {
  6639. lpt_bend_clkout_dp(dev_priv, 0);
  6640. lpt_enable_clkout_dp(dev_priv, true, true);
  6641. } else {
  6642. lpt_disable_clkout_dp(dev_priv);
  6643. }
  6644. }
  6645. /*
  6646. * Initialize reference clocks when the driver loads
  6647. */
  6648. void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
  6649. {
  6650. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
  6651. ironlake_init_pch_refclk(dev_priv);
  6652. else if (HAS_PCH_LPT(dev_priv))
  6653. lpt_init_pch_refclk(dev_priv);
  6654. }
  6655. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  6656. {
  6657. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6658. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6659. int pipe = intel_crtc->pipe;
  6660. uint32_t val;
  6661. val = 0;
  6662. switch (intel_crtc->config->pipe_bpp) {
  6663. case 18:
  6664. val |= PIPECONF_6BPC;
  6665. break;
  6666. case 24:
  6667. val |= PIPECONF_8BPC;
  6668. break;
  6669. case 30:
  6670. val |= PIPECONF_10BPC;
  6671. break;
  6672. case 36:
  6673. val |= PIPECONF_12BPC;
  6674. break;
  6675. default:
  6676. /* Case prevented by intel_choose_pipe_bpp_dither. */
  6677. BUG();
  6678. }
  6679. if (intel_crtc->config->dither)
  6680. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6681. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6682. val |= PIPECONF_INTERLACED_ILK;
  6683. else
  6684. val |= PIPECONF_PROGRESSIVE;
  6685. if (intel_crtc->config->limited_color_range)
  6686. val |= PIPECONF_COLOR_RANGE_SELECT;
  6687. I915_WRITE(PIPECONF(pipe), val);
  6688. POSTING_READ(PIPECONF(pipe));
  6689. }
  6690. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  6691. {
  6692. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6693. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6694. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6695. u32 val = 0;
  6696. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  6697. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  6698. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  6699. val |= PIPECONF_INTERLACED_ILK;
  6700. else
  6701. val |= PIPECONF_PROGRESSIVE;
  6702. I915_WRITE(PIPECONF(cpu_transcoder), val);
  6703. POSTING_READ(PIPECONF(cpu_transcoder));
  6704. }
  6705. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  6706. {
  6707. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  6708. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  6709. if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
  6710. u32 val = 0;
  6711. switch (intel_crtc->config->pipe_bpp) {
  6712. case 18:
  6713. val |= PIPEMISC_DITHER_6_BPC;
  6714. break;
  6715. case 24:
  6716. val |= PIPEMISC_DITHER_8_BPC;
  6717. break;
  6718. case 30:
  6719. val |= PIPEMISC_DITHER_10_BPC;
  6720. break;
  6721. case 36:
  6722. val |= PIPEMISC_DITHER_12_BPC;
  6723. break;
  6724. default:
  6725. /* Case prevented by pipe_config_set_bpp. */
  6726. BUG();
  6727. }
  6728. if (intel_crtc->config->dither)
  6729. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  6730. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  6731. }
  6732. }
  6733. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  6734. {
  6735. /*
  6736. * Account for spread spectrum to avoid
  6737. * oversubscribing the link. Max center spread
  6738. * is 2.5%; use 5% for safety's sake.
  6739. */
  6740. u32 bps = target_clock * bpp * 21 / 20;
  6741. return DIV_ROUND_UP(bps, link_bw * 8);
  6742. }
  6743. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  6744. {
  6745. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  6746. }
  6747. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  6748. struct intel_crtc_state *crtc_state,
  6749. struct dpll *reduced_clock)
  6750. {
  6751. struct drm_crtc *crtc = &intel_crtc->base;
  6752. struct drm_device *dev = crtc->dev;
  6753. struct drm_i915_private *dev_priv = to_i915(dev);
  6754. u32 dpll, fp, fp2;
  6755. int factor;
  6756. /* Enable autotuning of the PLL clock (if permissible) */
  6757. factor = 21;
  6758. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6759. if ((intel_panel_use_ssc(dev_priv) &&
  6760. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  6761. (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
  6762. factor = 25;
  6763. } else if (crtc_state->sdvo_tv_clock)
  6764. factor = 20;
  6765. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6766. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  6767. fp |= FP_CB_TUNE;
  6768. if (reduced_clock) {
  6769. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6770. if (reduced_clock->m < factor * reduced_clock->n)
  6771. fp2 |= FP_CB_TUNE;
  6772. } else {
  6773. fp2 = fp;
  6774. }
  6775. dpll = 0;
  6776. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  6777. dpll |= DPLLB_MODE_LVDS;
  6778. else
  6779. dpll |= DPLLB_MODE_DAC_SERIAL;
  6780. dpll |= (crtc_state->pixel_multiplier - 1)
  6781. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  6782. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6783. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  6784. dpll |= DPLL_SDVO_HIGH_SPEED;
  6785. if (intel_crtc_has_dp_encoder(crtc_state))
  6786. dpll |= DPLL_SDVO_HIGH_SPEED;
  6787. /*
  6788. * The high speed IO clock is only really required for
  6789. * SDVO/HDMI/DP, but we also enable it for CRT to make it
  6790. * possible to share the DPLL between CRT and HDMI. Enabling
  6791. * the clock needlessly does no real harm, except use up a
  6792. * bit of power potentially.
  6793. *
  6794. * We'll limit this to IVB with 3 pipes, since it has only two
  6795. * DPLLs and so DPLL sharing is the only way to get three pipes
  6796. * driving PCH ports at the same time. On SNB we could do this,
  6797. * and potentially avoid enabling the second DPLL, but it's not
  6798. * clear if it''s a win or loss power wise. No point in doing
  6799. * this on ILK at all since it has a fixed DPLL<->pipe mapping.
  6800. */
  6801. if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
  6802. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
  6803. dpll |= DPLL_SDVO_HIGH_SPEED;
  6804. /* compute bitmask from p1 value */
  6805. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6806. /* also FPA1 */
  6807. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6808. switch (crtc_state->dpll.p2) {
  6809. case 5:
  6810. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6811. break;
  6812. case 7:
  6813. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6814. break;
  6815. case 10:
  6816. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6817. break;
  6818. case 14:
  6819. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6820. break;
  6821. }
  6822. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6823. intel_panel_use_ssc(dev_priv))
  6824. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6825. else
  6826. dpll |= PLL_REF_INPUT_DREFCLK;
  6827. dpll |= DPLL_VCO_ENABLE;
  6828. crtc_state->dpll_hw_state.dpll = dpll;
  6829. crtc_state->dpll_hw_state.fp0 = fp;
  6830. crtc_state->dpll_hw_state.fp1 = fp2;
  6831. }
  6832. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  6833. struct intel_crtc_state *crtc_state)
  6834. {
  6835. struct drm_device *dev = crtc->base.dev;
  6836. struct drm_i915_private *dev_priv = to_i915(dev);
  6837. struct dpll reduced_clock;
  6838. bool has_reduced_clock = false;
  6839. struct intel_shared_dpll *pll;
  6840. const struct intel_limit *limit;
  6841. int refclk = 120000;
  6842. memset(&crtc_state->dpll_hw_state, 0,
  6843. sizeof(crtc_state->dpll_hw_state));
  6844. crtc->lowfreq_avail = false;
  6845. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  6846. if (!crtc_state->has_pch_encoder)
  6847. return 0;
  6848. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6849. if (intel_panel_use_ssc(dev_priv)) {
  6850. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  6851. dev_priv->vbt.lvds_ssc_freq);
  6852. refclk = dev_priv->vbt.lvds_ssc_freq;
  6853. }
  6854. if (intel_is_dual_link_lvds(dev)) {
  6855. if (refclk == 100000)
  6856. limit = &intel_limits_ironlake_dual_lvds_100m;
  6857. else
  6858. limit = &intel_limits_ironlake_dual_lvds;
  6859. } else {
  6860. if (refclk == 100000)
  6861. limit = &intel_limits_ironlake_single_lvds_100m;
  6862. else
  6863. limit = &intel_limits_ironlake_single_lvds;
  6864. }
  6865. } else {
  6866. limit = &intel_limits_ironlake_dac;
  6867. }
  6868. if (!crtc_state->clock_set &&
  6869. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  6870. refclk, NULL, &crtc_state->dpll)) {
  6871. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  6872. return -EINVAL;
  6873. }
  6874. ironlake_compute_dpll(crtc, crtc_state,
  6875. has_reduced_clock ? &reduced_clock : NULL);
  6876. pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
  6877. if (pll == NULL) {
  6878. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  6879. pipe_name(crtc->pipe));
  6880. return -EINVAL;
  6881. }
  6882. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6883. has_reduced_clock)
  6884. crtc->lowfreq_avail = true;
  6885. return 0;
  6886. }
  6887. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  6888. struct intel_link_m_n *m_n)
  6889. {
  6890. struct drm_device *dev = crtc->base.dev;
  6891. struct drm_i915_private *dev_priv = to_i915(dev);
  6892. enum pipe pipe = crtc->pipe;
  6893. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  6894. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  6895. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  6896. & ~TU_SIZE_MASK;
  6897. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  6898. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  6899. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6900. }
  6901. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  6902. enum transcoder transcoder,
  6903. struct intel_link_m_n *m_n,
  6904. struct intel_link_m_n *m2_n2)
  6905. {
  6906. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6907. enum pipe pipe = crtc->pipe;
  6908. if (INTEL_GEN(dev_priv) >= 5) {
  6909. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  6910. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  6911. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  6912. & ~TU_SIZE_MASK;
  6913. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  6914. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  6915. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6916. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  6917. * gen < 8) and if DRRS is supported (to make sure the
  6918. * registers are not unnecessarily read).
  6919. */
  6920. if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
  6921. crtc->config->has_drrs) {
  6922. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  6923. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  6924. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  6925. & ~TU_SIZE_MASK;
  6926. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  6927. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  6928. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6929. }
  6930. } else {
  6931. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  6932. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  6933. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  6934. & ~TU_SIZE_MASK;
  6935. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  6936. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  6937. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  6938. }
  6939. }
  6940. void intel_dp_get_m_n(struct intel_crtc *crtc,
  6941. struct intel_crtc_state *pipe_config)
  6942. {
  6943. if (pipe_config->has_pch_encoder)
  6944. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  6945. else
  6946. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6947. &pipe_config->dp_m_n,
  6948. &pipe_config->dp_m2_n2);
  6949. }
  6950. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  6951. struct intel_crtc_state *pipe_config)
  6952. {
  6953. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  6954. &pipe_config->fdi_m_n, NULL);
  6955. }
  6956. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  6957. struct intel_crtc_state *pipe_config)
  6958. {
  6959. struct drm_device *dev = crtc->base.dev;
  6960. struct drm_i915_private *dev_priv = to_i915(dev);
  6961. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  6962. uint32_t ps_ctrl = 0;
  6963. int id = -1;
  6964. int i;
  6965. /* find scaler attached to this pipe */
  6966. for (i = 0; i < crtc->num_scalers; i++) {
  6967. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  6968. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  6969. id = i;
  6970. pipe_config->pch_pfit.enabled = true;
  6971. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  6972. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  6973. break;
  6974. }
  6975. }
  6976. scaler_state->scaler_id = id;
  6977. if (id >= 0) {
  6978. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  6979. } else {
  6980. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  6981. }
  6982. }
  6983. static void
  6984. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  6985. struct intel_initial_plane_config *plane_config)
  6986. {
  6987. struct drm_device *dev = crtc->base.dev;
  6988. struct drm_i915_private *dev_priv = to_i915(dev);
  6989. u32 val, base, offset, stride_mult, tiling;
  6990. int pipe = crtc->pipe;
  6991. int fourcc, pixel_format;
  6992. unsigned int aligned_height;
  6993. struct drm_framebuffer *fb;
  6994. struct intel_framebuffer *intel_fb;
  6995. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  6996. if (!intel_fb) {
  6997. DRM_DEBUG_KMS("failed to alloc fb\n");
  6998. return;
  6999. }
  7000. fb = &intel_fb->base;
  7001. fb->dev = dev;
  7002. val = I915_READ(PLANE_CTL(pipe, 0));
  7003. if (!(val & PLANE_CTL_ENABLE))
  7004. goto error;
  7005. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  7006. fourcc = skl_format_to_fourcc(pixel_format,
  7007. val & PLANE_CTL_ORDER_RGBX,
  7008. val & PLANE_CTL_ALPHA_MASK);
  7009. fb->format = drm_format_info(fourcc);
  7010. tiling = val & PLANE_CTL_TILED_MASK;
  7011. switch (tiling) {
  7012. case PLANE_CTL_TILED_LINEAR:
  7013. fb->modifier = DRM_FORMAT_MOD_NONE;
  7014. break;
  7015. case PLANE_CTL_TILED_X:
  7016. plane_config->tiling = I915_TILING_X;
  7017. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7018. break;
  7019. case PLANE_CTL_TILED_Y:
  7020. fb->modifier = I915_FORMAT_MOD_Y_TILED;
  7021. break;
  7022. case PLANE_CTL_TILED_YF:
  7023. fb->modifier = I915_FORMAT_MOD_Yf_TILED;
  7024. break;
  7025. default:
  7026. MISSING_CASE(tiling);
  7027. goto error;
  7028. }
  7029. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  7030. plane_config->base = base;
  7031. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  7032. val = I915_READ(PLANE_SIZE(pipe, 0));
  7033. fb->height = ((val >> 16) & 0xfff) + 1;
  7034. fb->width = ((val >> 0) & 0x1fff) + 1;
  7035. val = I915_READ(PLANE_STRIDE(pipe, 0));
  7036. stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier,
  7037. fb->format->format);
  7038. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  7039. aligned_height = intel_fb_align_height(dev_priv,
  7040. fb->height,
  7041. fb->format->format,
  7042. fb->modifier);
  7043. plane_config->size = fb->pitches[0] * aligned_height;
  7044. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7045. pipe_name(pipe), fb->width, fb->height,
  7046. fb->format->cpp[0] * 8, base, fb->pitches[0],
  7047. plane_config->size);
  7048. plane_config->fb = intel_fb;
  7049. return;
  7050. error:
  7051. kfree(intel_fb);
  7052. }
  7053. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  7054. struct intel_crtc_state *pipe_config)
  7055. {
  7056. struct drm_device *dev = crtc->base.dev;
  7057. struct drm_i915_private *dev_priv = to_i915(dev);
  7058. uint32_t tmp;
  7059. tmp = I915_READ(PF_CTL(crtc->pipe));
  7060. if (tmp & PF_ENABLE) {
  7061. pipe_config->pch_pfit.enabled = true;
  7062. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  7063. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  7064. /* We currently do not free assignements of panel fitters on
  7065. * ivb/hsw (since we don't use the higher upscaling modes which
  7066. * differentiates them) so just WARN about this case for now. */
  7067. if (IS_GEN7(dev_priv)) {
  7068. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  7069. PF_PIPE_SEL_IVB(crtc->pipe));
  7070. }
  7071. }
  7072. }
  7073. static void
  7074. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  7075. struct intel_initial_plane_config *plane_config)
  7076. {
  7077. struct drm_device *dev = crtc->base.dev;
  7078. struct drm_i915_private *dev_priv = to_i915(dev);
  7079. u32 val, base, offset;
  7080. int pipe = crtc->pipe;
  7081. int fourcc, pixel_format;
  7082. unsigned int aligned_height;
  7083. struct drm_framebuffer *fb;
  7084. struct intel_framebuffer *intel_fb;
  7085. val = I915_READ(DSPCNTR(pipe));
  7086. if (!(val & DISPLAY_PLANE_ENABLE))
  7087. return;
  7088. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7089. if (!intel_fb) {
  7090. DRM_DEBUG_KMS("failed to alloc fb\n");
  7091. return;
  7092. }
  7093. fb = &intel_fb->base;
  7094. fb->dev = dev;
  7095. if (INTEL_GEN(dev_priv) >= 4) {
  7096. if (val & DISPPLANE_TILED) {
  7097. plane_config->tiling = I915_TILING_X;
  7098. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7099. }
  7100. }
  7101. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7102. fourcc = i9xx_format_to_fourcc(pixel_format);
  7103. fb->format = drm_format_info(fourcc);
  7104. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  7105. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  7106. offset = I915_READ(DSPOFFSET(pipe));
  7107. } else {
  7108. if (plane_config->tiling)
  7109. offset = I915_READ(DSPTILEOFF(pipe));
  7110. else
  7111. offset = I915_READ(DSPLINOFF(pipe));
  7112. }
  7113. plane_config->base = base;
  7114. val = I915_READ(PIPESRC(pipe));
  7115. fb->width = ((val >> 16) & 0xfff) + 1;
  7116. fb->height = ((val >> 0) & 0xfff) + 1;
  7117. val = I915_READ(DSPSTRIDE(pipe));
  7118. fb->pitches[0] = val & 0xffffffc0;
  7119. aligned_height = intel_fb_align_height(dev_priv,
  7120. fb->height,
  7121. fb->format->format,
  7122. fb->modifier);
  7123. plane_config->size = fb->pitches[0] * aligned_height;
  7124. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7125. pipe_name(pipe), fb->width, fb->height,
  7126. fb->format->cpp[0] * 8, base, fb->pitches[0],
  7127. plane_config->size);
  7128. plane_config->fb = intel_fb;
  7129. }
  7130. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  7131. struct intel_crtc_state *pipe_config)
  7132. {
  7133. struct drm_device *dev = crtc->base.dev;
  7134. struct drm_i915_private *dev_priv = to_i915(dev);
  7135. enum intel_display_power_domain power_domain;
  7136. uint32_t tmp;
  7137. bool ret;
  7138. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7139. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7140. return false;
  7141. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7142. pipe_config->shared_dpll = NULL;
  7143. ret = false;
  7144. tmp = I915_READ(PIPECONF(crtc->pipe));
  7145. if (!(tmp & PIPECONF_ENABLE))
  7146. goto out;
  7147. switch (tmp & PIPECONF_BPC_MASK) {
  7148. case PIPECONF_6BPC:
  7149. pipe_config->pipe_bpp = 18;
  7150. break;
  7151. case PIPECONF_8BPC:
  7152. pipe_config->pipe_bpp = 24;
  7153. break;
  7154. case PIPECONF_10BPC:
  7155. pipe_config->pipe_bpp = 30;
  7156. break;
  7157. case PIPECONF_12BPC:
  7158. pipe_config->pipe_bpp = 36;
  7159. break;
  7160. default:
  7161. break;
  7162. }
  7163. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  7164. pipe_config->limited_color_range = true;
  7165. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  7166. struct intel_shared_dpll *pll;
  7167. enum intel_dpll_id pll_id;
  7168. pipe_config->has_pch_encoder = true;
  7169. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  7170. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7171. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7172. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7173. if (HAS_PCH_IBX(dev_priv)) {
  7174. /*
  7175. * The pipe->pch transcoder and pch transcoder->pll
  7176. * mapping is fixed.
  7177. */
  7178. pll_id = (enum intel_dpll_id) crtc->pipe;
  7179. } else {
  7180. tmp = I915_READ(PCH_DPLL_SEL);
  7181. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  7182. pll_id = DPLL_ID_PCH_PLL_B;
  7183. else
  7184. pll_id= DPLL_ID_PCH_PLL_A;
  7185. }
  7186. pipe_config->shared_dpll =
  7187. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  7188. pll = pipe_config->shared_dpll;
  7189. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7190. &pipe_config->dpll_hw_state));
  7191. tmp = pipe_config->dpll_hw_state.dpll;
  7192. pipe_config->pixel_multiplier =
  7193. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  7194. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  7195. ironlake_pch_clock_get(crtc, pipe_config);
  7196. } else {
  7197. pipe_config->pixel_multiplier = 1;
  7198. }
  7199. intel_get_pipe_timings(crtc, pipe_config);
  7200. intel_get_pipe_src_size(crtc, pipe_config);
  7201. ironlake_get_pfit_config(crtc, pipe_config);
  7202. ret = true;
  7203. out:
  7204. intel_display_power_put(dev_priv, power_domain);
  7205. return ret;
  7206. }
  7207. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  7208. {
  7209. struct drm_device *dev = &dev_priv->drm;
  7210. struct intel_crtc *crtc;
  7211. for_each_intel_crtc(dev, crtc)
  7212. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  7213. pipe_name(crtc->pipe));
  7214. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  7215. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  7216. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  7217. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  7218. I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
  7219. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  7220. "CPU PWM1 enabled\n");
  7221. if (IS_HASWELL(dev_priv))
  7222. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  7223. "CPU PWM2 enabled\n");
  7224. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  7225. "PCH PWM1 enabled\n");
  7226. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  7227. "Utility pin enabled\n");
  7228. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  7229. /*
  7230. * In theory we can still leave IRQs enabled, as long as only the HPD
  7231. * interrupts remain enabled. We used to check for that, but since it's
  7232. * gen-specific and since we only disable LCPLL after we fully disable
  7233. * the interrupts, the check below should be enough.
  7234. */
  7235. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  7236. }
  7237. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  7238. {
  7239. if (IS_HASWELL(dev_priv))
  7240. return I915_READ(D_COMP_HSW);
  7241. else
  7242. return I915_READ(D_COMP_BDW);
  7243. }
  7244. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  7245. {
  7246. if (IS_HASWELL(dev_priv)) {
  7247. mutex_lock(&dev_priv->rps.hw_lock);
  7248. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  7249. val))
  7250. DRM_DEBUG_KMS("Failed to write to D_COMP\n");
  7251. mutex_unlock(&dev_priv->rps.hw_lock);
  7252. } else {
  7253. I915_WRITE(D_COMP_BDW, val);
  7254. POSTING_READ(D_COMP_BDW);
  7255. }
  7256. }
  7257. /*
  7258. * This function implements pieces of two sequences from BSpec:
  7259. * - Sequence for display software to disable LCPLL
  7260. * - Sequence for display software to allow package C8+
  7261. * The steps implemented here are just the steps that actually touch the LCPLL
  7262. * register. Callers should take care of disabling all the display engine
  7263. * functions, doing the mode unset, fixing interrupts, etc.
  7264. */
  7265. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  7266. bool switch_to_fclk, bool allow_power_down)
  7267. {
  7268. uint32_t val;
  7269. assert_can_disable_lcpll(dev_priv);
  7270. val = I915_READ(LCPLL_CTL);
  7271. if (switch_to_fclk) {
  7272. val |= LCPLL_CD_SOURCE_FCLK;
  7273. I915_WRITE(LCPLL_CTL, val);
  7274. if (wait_for_us(I915_READ(LCPLL_CTL) &
  7275. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  7276. DRM_ERROR("Switching to FCLK failed\n");
  7277. val = I915_READ(LCPLL_CTL);
  7278. }
  7279. val |= LCPLL_PLL_DISABLE;
  7280. I915_WRITE(LCPLL_CTL, val);
  7281. POSTING_READ(LCPLL_CTL);
  7282. if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
  7283. DRM_ERROR("LCPLL still locked\n");
  7284. val = hsw_read_dcomp(dev_priv);
  7285. val |= D_COMP_COMP_DISABLE;
  7286. hsw_write_dcomp(dev_priv, val);
  7287. ndelay(100);
  7288. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  7289. 1))
  7290. DRM_ERROR("D_COMP RCOMP still in progress\n");
  7291. if (allow_power_down) {
  7292. val = I915_READ(LCPLL_CTL);
  7293. val |= LCPLL_POWER_DOWN_ALLOW;
  7294. I915_WRITE(LCPLL_CTL, val);
  7295. POSTING_READ(LCPLL_CTL);
  7296. }
  7297. }
  7298. /*
  7299. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  7300. * source.
  7301. */
  7302. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  7303. {
  7304. uint32_t val;
  7305. val = I915_READ(LCPLL_CTL);
  7306. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  7307. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  7308. return;
  7309. /*
  7310. * Make sure we're not on PC8 state before disabling PC8, otherwise
  7311. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  7312. */
  7313. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  7314. if (val & LCPLL_POWER_DOWN_ALLOW) {
  7315. val &= ~LCPLL_POWER_DOWN_ALLOW;
  7316. I915_WRITE(LCPLL_CTL, val);
  7317. POSTING_READ(LCPLL_CTL);
  7318. }
  7319. val = hsw_read_dcomp(dev_priv);
  7320. val |= D_COMP_COMP_FORCE;
  7321. val &= ~D_COMP_COMP_DISABLE;
  7322. hsw_write_dcomp(dev_priv, val);
  7323. val = I915_READ(LCPLL_CTL);
  7324. val &= ~LCPLL_PLL_DISABLE;
  7325. I915_WRITE(LCPLL_CTL, val);
  7326. if (intel_wait_for_register(dev_priv,
  7327. LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  7328. 5))
  7329. DRM_ERROR("LCPLL not locked yet\n");
  7330. if (val & LCPLL_CD_SOURCE_FCLK) {
  7331. val = I915_READ(LCPLL_CTL);
  7332. val &= ~LCPLL_CD_SOURCE_FCLK;
  7333. I915_WRITE(LCPLL_CTL, val);
  7334. if (wait_for_us((I915_READ(LCPLL_CTL) &
  7335. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  7336. DRM_ERROR("Switching back to LCPLL failed\n");
  7337. }
  7338. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  7339. intel_update_cdclk(dev_priv);
  7340. }
  7341. /*
  7342. * Package states C8 and deeper are really deep PC states that can only be
  7343. * reached when all the devices on the system allow it, so even if the graphics
  7344. * device allows PC8+, it doesn't mean the system will actually get to these
  7345. * states. Our driver only allows PC8+ when going into runtime PM.
  7346. *
  7347. * The requirements for PC8+ are that all the outputs are disabled, the power
  7348. * well is disabled and most interrupts are disabled, and these are also
  7349. * requirements for runtime PM. When these conditions are met, we manually do
  7350. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  7351. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  7352. * hang the machine.
  7353. *
  7354. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  7355. * the state of some registers, so when we come back from PC8+ we need to
  7356. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  7357. * need to take care of the registers kept by RC6. Notice that this happens even
  7358. * if we don't put the device in PCI D3 state (which is what currently happens
  7359. * because of the runtime PM support).
  7360. *
  7361. * For more, read "Display Sequences for Package C8" on the hardware
  7362. * documentation.
  7363. */
  7364. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  7365. {
  7366. uint32_t val;
  7367. DRM_DEBUG_KMS("Enabling package C8+\n");
  7368. if (HAS_PCH_LPT_LP(dev_priv)) {
  7369. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7370. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  7371. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7372. }
  7373. lpt_disable_clkout_dp(dev_priv);
  7374. hsw_disable_lcpll(dev_priv, true, true);
  7375. }
  7376. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  7377. {
  7378. uint32_t val;
  7379. DRM_DEBUG_KMS("Disabling package C8+\n");
  7380. hsw_restore_lcpll(dev_priv);
  7381. lpt_init_pch_refclk(dev_priv);
  7382. if (HAS_PCH_LPT_LP(dev_priv)) {
  7383. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  7384. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  7385. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  7386. }
  7387. }
  7388. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  7389. struct intel_crtc_state *crtc_state)
  7390. {
  7391. if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
  7392. if (!intel_ddi_pll_select(crtc, crtc_state))
  7393. return -EINVAL;
  7394. }
  7395. crtc->lowfreq_avail = false;
  7396. return 0;
  7397. }
  7398. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  7399. enum port port,
  7400. struct intel_crtc_state *pipe_config)
  7401. {
  7402. enum intel_dpll_id id;
  7403. switch (port) {
  7404. case PORT_A:
  7405. id = DPLL_ID_SKL_DPLL0;
  7406. break;
  7407. case PORT_B:
  7408. id = DPLL_ID_SKL_DPLL1;
  7409. break;
  7410. case PORT_C:
  7411. id = DPLL_ID_SKL_DPLL2;
  7412. break;
  7413. default:
  7414. DRM_ERROR("Incorrect port type\n");
  7415. return;
  7416. }
  7417. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7418. }
  7419. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  7420. enum port port,
  7421. struct intel_crtc_state *pipe_config)
  7422. {
  7423. enum intel_dpll_id id;
  7424. u32 temp;
  7425. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  7426. id = temp >> (port * 3 + 1);
  7427. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
  7428. return;
  7429. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7430. }
  7431. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  7432. enum port port,
  7433. struct intel_crtc_state *pipe_config)
  7434. {
  7435. enum intel_dpll_id id;
  7436. uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  7437. switch (ddi_pll_sel) {
  7438. case PORT_CLK_SEL_WRPLL1:
  7439. id = DPLL_ID_WRPLL1;
  7440. break;
  7441. case PORT_CLK_SEL_WRPLL2:
  7442. id = DPLL_ID_WRPLL2;
  7443. break;
  7444. case PORT_CLK_SEL_SPLL:
  7445. id = DPLL_ID_SPLL;
  7446. break;
  7447. case PORT_CLK_SEL_LCPLL_810:
  7448. id = DPLL_ID_LCPLL_810;
  7449. break;
  7450. case PORT_CLK_SEL_LCPLL_1350:
  7451. id = DPLL_ID_LCPLL_1350;
  7452. break;
  7453. case PORT_CLK_SEL_LCPLL_2700:
  7454. id = DPLL_ID_LCPLL_2700;
  7455. break;
  7456. default:
  7457. MISSING_CASE(ddi_pll_sel);
  7458. /* fall through */
  7459. case PORT_CLK_SEL_NONE:
  7460. return;
  7461. }
  7462. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  7463. }
  7464. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  7465. struct intel_crtc_state *pipe_config,
  7466. u64 *power_domain_mask)
  7467. {
  7468. struct drm_device *dev = crtc->base.dev;
  7469. struct drm_i915_private *dev_priv = to_i915(dev);
  7470. enum intel_display_power_domain power_domain;
  7471. u32 tmp;
  7472. /*
  7473. * The pipe->transcoder mapping is fixed with the exception of the eDP
  7474. * transcoder handled below.
  7475. */
  7476. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7477. /*
  7478. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  7479. * consistency and less surprising code; it's in always on power).
  7480. */
  7481. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  7482. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  7483. enum pipe trans_edp_pipe;
  7484. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  7485. default:
  7486. WARN(1, "unknown pipe linked to edp transcoder\n");
  7487. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  7488. case TRANS_DDI_EDP_INPUT_A_ON:
  7489. trans_edp_pipe = PIPE_A;
  7490. break;
  7491. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  7492. trans_edp_pipe = PIPE_B;
  7493. break;
  7494. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  7495. trans_edp_pipe = PIPE_C;
  7496. break;
  7497. }
  7498. if (trans_edp_pipe == crtc->pipe)
  7499. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  7500. }
  7501. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  7502. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7503. return false;
  7504. *power_domain_mask |= BIT_ULL(power_domain);
  7505. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  7506. return tmp & PIPECONF_ENABLE;
  7507. }
  7508. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  7509. struct intel_crtc_state *pipe_config,
  7510. u64 *power_domain_mask)
  7511. {
  7512. struct drm_device *dev = crtc->base.dev;
  7513. struct drm_i915_private *dev_priv = to_i915(dev);
  7514. enum intel_display_power_domain power_domain;
  7515. enum port port;
  7516. enum transcoder cpu_transcoder;
  7517. u32 tmp;
  7518. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  7519. if (port == PORT_A)
  7520. cpu_transcoder = TRANSCODER_DSI_A;
  7521. else
  7522. cpu_transcoder = TRANSCODER_DSI_C;
  7523. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  7524. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7525. continue;
  7526. *power_domain_mask |= BIT_ULL(power_domain);
  7527. /*
  7528. * The PLL needs to be enabled with a valid divider
  7529. * configuration, otherwise accessing DSI registers will hang
  7530. * the machine. See BSpec North Display Engine
  7531. * registers/MIPI[BXT]. We can break out here early, since we
  7532. * need the same DSI PLL to be enabled for both DSI ports.
  7533. */
  7534. if (!intel_dsi_pll_is_enabled(dev_priv))
  7535. break;
  7536. /* XXX: this works for video mode only */
  7537. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  7538. if (!(tmp & DPI_ENABLE))
  7539. continue;
  7540. tmp = I915_READ(MIPI_CTRL(port));
  7541. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  7542. continue;
  7543. pipe_config->cpu_transcoder = cpu_transcoder;
  7544. break;
  7545. }
  7546. return transcoder_is_dsi(pipe_config->cpu_transcoder);
  7547. }
  7548. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  7549. struct intel_crtc_state *pipe_config)
  7550. {
  7551. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7552. struct intel_shared_dpll *pll;
  7553. enum port port;
  7554. uint32_t tmp;
  7555. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  7556. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  7557. if (IS_GEN9_BC(dev_priv))
  7558. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  7559. else if (IS_GEN9_LP(dev_priv))
  7560. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  7561. else
  7562. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  7563. pll = pipe_config->shared_dpll;
  7564. if (pll) {
  7565. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  7566. &pipe_config->dpll_hw_state));
  7567. }
  7568. /*
  7569. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  7570. * DDI E. So just check whether this pipe is wired to DDI E and whether
  7571. * the PCH transcoder is on.
  7572. */
  7573. if (INTEL_GEN(dev_priv) < 9 &&
  7574. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  7575. pipe_config->has_pch_encoder = true;
  7576. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  7577. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  7578. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  7579. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  7580. }
  7581. }
  7582. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  7583. struct intel_crtc_state *pipe_config)
  7584. {
  7585. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7586. enum intel_display_power_domain power_domain;
  7587. u64 power_domain_mask;
  7588. bool active;
  7589. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7590. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7591. return false;
  7592. power_domain_mask = BIT_ULL(power_domain);
  7593. pipe_config->shared_dpll = NULL;
  7594. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  7595. if (IS_GEN9_LP(dev_priv) &&
  7596. bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
  7597. WARN_ON(active);
  7598. active = true;
  7599. }
  7600. if (!active)
  7601. goto out;
  7602. if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7603. haswell_get_ddi_port_state(crtc, pipe_config);
  7604. intel_get_pipe_timings(crtc, pipe_config);
  7605. }
  7606. intel_get_pipe_src_size(crtc, pipe_config);
  7607. pipe_config->gamma_mode =
  7608. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  7609. if (INTEL_GEN(dev_priv) >= 9) {
  7610. intel_crtc_init_scalers(crtc, pipe_config);
  7611. pipe_config->scaler_state.scaler_id = -1;
  7612. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  7613. }
  7614. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  7615. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  7616. power_domain_mask |= BIT_ULL(power_domain);
  7617. if (INTEL_GEN(dev_priv) >= 9)
  7618. skylake_get_pfit_config(crtc, pipe_config);
  7619. else
  7620. ironlake_get_pfit_config(crtc, pipe_config);
  7621. }
  7622. if (IS_HASWELL(dev_priv))
  7623. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  7624. (I915_READ(IPS_CTL) & IPS_ENABLE);
  7625. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  7626. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  7627. pipe_config->pixel_multiplier =
  7628. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  7629. } else {
  7630. pipe_config->pixel_multiplier = 1;
  7631. }
  7632. out:
  7633. for_each_power_domain(power_domain, power_domain_mask)
  7634. intel_display_power_put(dev_priv, power_domain);
  7635. return active;
  7636. }
  7637. static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
  7638. const struct intel_plane_state *plane_state)
  7639. {
  7640. struct drm_device *dev = crtc->dev;
  7641. struct drm_i915_private *dev_priv = to_i915(dev);
  7642. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7643. uint32_t cntl = 0, size = 0;
  7644. if (plane_state && plane_state->base.visible) {
  7645. unsigned int width = plane_state->base.crtc_w;
  7646. unsigned int height = plane_state->base.crtc_h;
  7647. unsigned int stride = roundup_pow_of_two(width) * 4;
  7648. switch (stride) {
  7649. default:
  7650. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  7651. width, stride);
  7652. stride = 256;
  7653. /* fallthrough */
  7654. case 256:
  7655. case 512:
  7656. case 1024:
  7657. case 2048:
  7658. break;
  7659. }
  7660. cntl |= CURSOR_ENABLE |
  7661. CURSOR_GAMMA_ENABLE |
  7662. CURSOR_FORMAT_ARGB |
  7663. CURSOR_STRIDE(stride);
  7664. size = (height << 12) | width;
  7665. }
  7666. if (intel_crtc->cursor_cntl != 0 &&
  7667. (intel_crtc->cursor_base != base ||
  7668. intel_crtc->cursor_size != size ||
  7669. intel_crtc->cursor_cntl != cntl)) {
  7670. /* On these chipsets we can only modify the base/size/stride
  7671. * whilst the cursor is disabled.
  7672. */
  7673. I915_WRITE(CURCNTR(PIPE_A), 0);
  7674. POSTING_READ(CURCNTR(PIPE_A));
  7675. intel_crtc->cursor_cntl = 0;
  7676. }
  7677. if (intel_crtc->cursor_base != base) {
  7678. I915_WRITE(CURBASE(PIPE_A), base);
  7679. intel_crtc->cursor_base = base;
  7680. }
  7681. if (intel_crtc->cursor_size != size) {
  7682. I915_WRITE(CURSIZE, size);
  7683. intel_crtc->cursor_size = size;
  7684. }
  7685. if (intel_crtc->cursor_cntl != cntl) {
  7686. I915_WRITE(CURCNTR(PIPE_A), cntl);
  7687. POSTING_READ(CURCNTR(PIPE_A));
  7688. intel_crtc->cursor_cntl = cntl;
  7689. }
  7690. }
  7691. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
  7692. const struct intel_plane_state *plane_state)
  7693. {
  7694. struct drm_device *dev = crtc->dev;
  7695. struct drm_i915_private *dev_priv = to_i915(dev);
  7696. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7697. int pipe = intel_crtc->pipe;
  7698. uint32_t cntl = 0;
  7699. if (plane_state && plane_state->base.visible) {
  7700. cntl = MCURSOR_GAMMA_ENABLE;
  7701. switch (plane_state->base.crtc_w) {
  7702. case 64:
  7703. cntl |= CURSOR_MODE_64_ARGB_AX;
  7704. break;
  7705. case 128:
  7706. cntl |= CURSOR_MODE_128_ARGB_AX;
  7707. break;
  7708. case 256:
  7709. cntl |= CURSOR_MODE_256_ARGB_AX;
  7710. break;
  7711. default:
  7712. MISSING_CASE(plane_state->base.crtc_w);
  7713. return;
  7714. }
  7715. cntl |= pipe << 28; /* Connect to correct pipe */
  7716. if (HAS_DDI(dev_priv))
  7717. cntl |= CURSOR_PIPE_CSC_ENABLE;
  7718. if (plane_state->base.rotation & DRM_ROTATE_180)
  7719. cntl |= CURSOR_ROTATE_180;
  7720. }
  7721. if (intel_crtc->cursor_cntl != cntl) {
  7722. I915_WRITE(CURCNTR(pipe), cntl);
  7723. POSTING_READ(CURCNTR(pipe));
  7724. intel_crtc->cursor_cntl = cntl;
  7725. }
  7726. /* and commit changes on next vblank */
  7727. I915_WRITE(CURBASE(pipe), base);
  7728. POSTING_READ(CURBASE(pipe));
  7729. intel_crtc->cursor_base = base;
  7730. }
  7731. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  7732. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  7733. const struct intel_plane_state *plane_state)
  7734. {
  7735. struct drm_device *dev = crtc->dev;
  7736. struct drm_i915_private *dev_priv = to_i915(dev);
  7737. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7738. int pipe = intel_crtc->pipe;
  7739. u32 base = intel_crtc->cursor_addr;
  7740. u32 pos = 0;
  7741. if (plane_state) {
  7742. int x = plane_state->base.crtc_x;
  7743. int y = plane_state->base.crtc_y;
  7744. if (x < 0) {
  7745. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  7746. x = -x;
  7747. }
  7748. pos |= x << CURSOR_X_SHIFT;
  7749. if (y < 0) {
  7750. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  7751. y = -y;
  7752. }
  7753. pos |= y << CURSOR_Y_SHIFT;
  7754. /* ILK+ do this automagically */
  7755. if (HAS_GMCH_DISPLAY(dev_priv) &&
  7756. plane_state->base.rotation & DRM_ROTATE_180) {
  7757. base += (plane_state->base.crtc_h *
  7758. plane_state->base.crtc_w - 1) * 4;
  7759. }
  7760. }
  7761. I915_WRITE(CURPOS(pipe), pos);
  7762. if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  7763. i845_update_cursor(crtc, base, plane_state);
  7764. else
  7765. i9xx_update_cursor(crtc, base, plane_state);
  7766. }
  7767. static bool cursor_size_ok(struct drm_i915_private *dev_priv,
  7768. uint32_t width, uint32_t height)
  7769. {
  7770. if (width == 0 || height == 0)
  7771. return false;
  7772. /*
  7773. * 845g/865g are special in that they are only limited by
  7774. * the width of their cursors, the height is arbitrary up to
  7775. * the precision of the register. Everything else requires
  7776. * square cursors, limited to a few power-of-two sizes.
  7777. */
  7778. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  7779. if ((width & 63) != 0)
  7780. return false;
  7781. if (width > (IS_I845G(dev_priv) ? 64 : 512))
  7782. return false;
  7783. if (height > 1023)
  7784. return false;
  7785. } else {
  7786. switch (width | height) {
  7787. case 256:
  7788. case 128:
  7789. if (IS_GEN2(dev_priv))
  7790. return false;
  7791. case 64:
  7792. break;
  7793. default:
  7794. return false;
  7795. }
  7796. }
  7797. return true;
  7798. }
  7799. /* VESA 640x480x72Hz mode to set on the pipe */
  7800. static struct drm_display_mode load_detect_mode = {
  7801. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  7802. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  7803. };
  7804. struct drm_framebuffer *
  7805. intel_framebuffer_create(struct drm_i915_gem_object *obj,
  7806. struct drm_mode_fb_cmd2 *mode_cmd)
  7807. {
  7808. struct intel_framebuffer *intel_fb;
  7809. int ret;
  7810. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7811. if (!intel_fb)
  7812. return ERR_PTR(-ENOMEM);
  7813. ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
  7814. if (ret)
  7815. goto err;
  7816. return &intel_fb->base;
  7817. err:
  7818. kfree(intel_fb);
  7819. return ERR_PTR(ret);
  7820. }
  7821. static u32
  7822. intel_framebuffer_pitch_for_width(int width, int bpp)
  7823. {
  7824. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  7825. return ALIGN(pitch, 64);
  7826. }
  7827. static u32
  7828. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  7829. {
  7830. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  7831. return PAGE_ALIGN(pitch * mode->vdisplay);
  7832. }
  7833. static struct drm_framebuffer *
  7834. intel_framebuffer_create_for_mode(struct drm_device *dev,
  7835. struct drm_display_mode *mode,
  7836. int depth, int bpp)
  7837. {
  7838. struct drm_framebuffer *fb;
  7839. struct drm_i915_gem_object *obj;
  7840. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  7841. obj = i915_gem_object_create(to_i915(dev),
  7842. intel_framebuffer_size_for_mode(mode, bpp));
  7843. if (IS_ERR(obj))
  7844. return ERR_CAST(obj);
  7845. mode_cmd.width = mode->hdisplay;
  7846. mode_cmd.height = mode->vdisplay;
  7847. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  7848. bpp);
  7849. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  7850. fb = intel_framebuffer_create(obj, &mode_cmd);
  7851. if (IS_ERR(fb))
  7852. i915_gem_object_put(obj);
  7853. return fb;
  7854. }
  7855. static struct drm_framebuffer *
  7856. mode_fits_in_fbdev(struct drm_device *dev,
  7857. struct drm_display_mode *mode)
  7858. {
  7859. #ifdef CONFIG_DRM_FBDEV_EMULATION
  7860. struct drm_i915_private *dev_priv = to_i915(dev);
  7861. struct drm_i915_gem_object *obj;
  7862. struct drm_framebuffer *fb;
  7863. if (!dev_priv->fbdev)
  7864. return NULL;
  7865. if (!dev_priv->fbdev->fb)
  7866. return NULL;
  7867. obj = dev_priv->fbdev->fb->obj;
  7868. BUG_ON(!obj);
  7869. fb = &dev_priv->fbdev->fb->base;
  7870. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  7871. fb->format->cpp[0] * 8))
  7872. return NULL;
  7873. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  7874. return NULL;
  7875. drm_framebuffer_reference(fb);
  7876. return fb;
  7877. #else
  7878. return NULL;
  7879. #endif
  7880. }
  7881. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  7882. struct drm_crtc *crtc,
  7883. struct drm_display_mode *mode,
  7884. struct drm_framebuffer *fb,
  7885. int x, int y)
  7886. {
  7887. struct drm_plane_state *plane_state;
  7888. int hdisplay, vdisplay;
  7889. int ret;
  7890. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  7891. if (IS_ERR(plane_state))
  7892. return PTR_ERR(plane_state);
  7893. if (mode)
  7894. drm_mode_get_hv_timing(mode, &hdisplay, &vdisplay);
  7895. else
  7896. hdisplay = vdisplay = 0;
  7897. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  7898. if (ret)
  7899. return ret;
  7900. drm_atomic_set_fb_for_plane(plane_state, fb);
  7901. plane_state->crtc_x = 0;
  7902. plane_state->crtc_y = 0;
  7903. plane_state->crtc_w = hdisplay;
  7904. plane_state->crtc_h = vdisplay;
  7905. plane_state->src_x = x << 16;
  7906. plane_state->src_y = y << 16;
  7907. plane_state->src_w = hdisplay << 16;
  7908. plane_state->src_h = vdisplay << 16;
  7909. return 0;
  7910. }
  7911. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  7912. struct drm_display_mode *mode,
  7913. struct intel_load_detect_pipe *old,
  7914. struct drm_modeset_acquire_ctx *ctx)
  7915. {
  7916. struct intel_crtc *intel_crtc;
  7917. struct intel_encoder *intel_encoder =
  7918. intel_attached_encoder(connector);
  7919. struct drm_crtc *possible_crtc;
  7920. struct drm_encoder *encoder = &intel_encoder->base;
  7921. struct drm_crtc *crtc = NULL;
  7922. struct drm_device *dev = encoder->dev;
  7923. struct drm_i915_private *dev_priv = to_i915(dev);
  7924. struct drm_framebuffer *fb;
  7925. struct drm_mode_config *config = &dev->mode_config;
  7926. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  7927. struct drm_connector_state *connector_state;
  7928. struct intel_crtc_state *crtc_state;
  7929. int ret, i = -1;
  7930. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  7931. connector->base.id, connector->name,
  7932. encoder->base.id, encoder->name);
  7933. old->restore_state = NULL;
  7934. retry:
  7935. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  7936. if (ret)
  7937. goto fail;
  7938. /*
  7939. * Algorithm gets a little messy:
  7940. *
  7941. * - if the connector already has an assigned crtc, use it (but make
  7942. * sure it's on first)
  7943. *
  7944. * - try to find the first unused crtc that can drive this connector,
  7945. * and use that if we find one
  7946. */
  7947. /* See if we already have a CRTC for this connector */
  7948. if (connector->state->crtc) {
  7949. crtc = connector->state->crtc;
  7950. ret = drm_modeset_lock(&crtc->mutex, ctx);
  7951. if (ret)
  7952. goto fail;
  7953. /* Make sure the crtc and connector are running */
  7954. goto found;
  7955. }
  7956. /* Find an unused one (if possible) */
  7957. for_each_crtc(dev, possible_crtc) {
  7958. i++;
  7959. if (!(encoder->possible_crtcs & (1 << i)))
  7960. continue;
  7961. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  7962. if (ret)
  7963. goto fail;
  7964. if (possible_crtc->state->enable) {
  7965. drm_modeset_unlock(&possible_crtc->mutex);
  7966. continue;
  7967. }
  7968. crtc = possible_crtc;
  7969. break;
  7970. }
  7971. /*
  7972. * If we didn't find an unused CRTC, don't use any.
  7973. */
  7974. if (!crtc) {
  7975. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  7976. goto fail;
  7977. }
  7978. found:
  7979. intel_crtc = to_intel_crtc(crtc);
  7980. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  7981. if (ret)
  7982. goto fail;
  7983. state = drm_atomic_state_alloc(dev);
  7984. restore_state = drm_atomic_state_alloc(dev);
  7985. if (!state || !restore_state) {
  7986. ret = -ENOMEM;
  7987. goto fail;
  7988. }
  7989. state->acquire_ctx = ctx;
  7990. restore_state->acquire_ctx = ctx;
  7991. connector_state = drm_atomic_get_connector_state(state, connector);
  7992. if (IS_ERR(connector_state)) {
  7993. ret = PTR_ERR(connector_state);
  7994. goto fail;
  7995. }
  7996. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  7997. if (ret)
  7998. goto fail;
  7999. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  8000. if (IS_ERR(crtc_state)) {
  8001. ret = PTR_ERR(crtc_state);
  8002. goto fail;
  8003. }
  8004. crtc_state->base.active = crtc_state->base.enable = true;
  8005. if (!mode)
  8006. mode = &load_detect_mode;
  8007. /* We need a framebuffer large enough to accommodate all accesses
  8008. * that the plane may generate whilst we perform load detection.
  8009. * We can not rely on the fbcon either being present (we get called
  8010. * during its initialisation to detect all boot displays, or it may
  8011. * not even exist) or that it is large enough to satisfy the
  8012. * requested mode.
  8013. */
  8014. fb = mode_fits_in_fbdev(dev, mode);
  8015. if (fb == NULL) {
  8016. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  8017. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  8018. } else
  8019. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  8020. if (IS_ERR(fb)) {
  8021. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  8022. goto fail;
  8023. }
  8024. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  8025. if (ret)
  8026. goto fail;
  8027. drm_framebuffer_unreference(fb);
  8028. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  8029. if (ret)
  8030. goto fail;
  8031. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  8032. if (!ret)
  8033. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  8034. if (!ret)
  8035. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
  8036. if (ret) {
  8037. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  8038. goto fail;
  8039. }
  8040. ret = drm_atomic_commit(state);
  8041. if (ret) {
  8042. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  8043. goto fail;
  8044. }
  8045. old->restore_state = restore_state;
  8046. drm_atomic_state_put(state);
  8047. /* let the connector get through one full cycle before testing */
  8048. intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
  8049. return true;
  8050. fail:
  8051. if (state) {
  8052. drm_atomic_state_put(state);
  8053. state = NULL;
  8054. }
  8055. if (restore_state) {
  8056. drm_atomic_state_put(restore_state);
  8057. restore_state = NULL;
  8058. }
  8059. if (ret == -EDEADLK) {
  8060. drm_modeset_backoff(ctx);
  8061. goto retry;
  8062. }
  8063. return false;
  8064. }
  8065. void intel_release_load_detect_pipe(struct drm_connector *connector,
  8066. struct intel_load_detect_pipe *old,
  8067. struct drm_modeset_acquire_ctx *ctx)
  8068. {
  8069. struct intel_encoder *intel_encoder =
  8070. intel_attached_encoder(connector);
  8071. struct drm_encoder *encoder = &intel_encoder->base;
  8072. struct drm_atomic_state *state = old->restore_state;
  8073. int ret;
  8074. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  8075. connector->base.id, connector->name,
  8076. encoder->base.id, encoder->name);
  8077. if (!state)
  8078. return;
  8079. ret = drm_atomic_commit(state);
  8080. if (ret)
  8081. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  8082. drm_atomic_state_put(state);
  8083. }
  8084. static int i9xx_pll_refclk(struct drm_device *dev,
  8085. const struct intel_crtc_state *pipe_config)
  8086. {
  8087. struct drm_i915_private *dev_priv = to_i915(dev);
  8088. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8089. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  8090. return dev_priv->vbt.lvds_ssc_freq;
  8091. else if (HAS_PCH_SPLIT(dev_priv))
  8092. return 120000;
  8093. else if (!IS_GEN2(dev_priv))
  8094. return 96000;
  8095. else
  8096. return 48000;
  8097. }
  8098. /* Returns the clock of the currently programmed mode of the given pipe. */
  8099. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  8100. struct intel_crtc_state *pipe_config)
  8101. {
  8102. struct drm_device *dev = crtc->base.dev;
  8103. struct drm_i915_private *dev_priv = to_i915(dev);
  8104. int pipe = pipe_config->cpu_transcoder;
  8105. u32 dpll = pipe_config->dpll_hw_state.dpll;
  8106. u32 fp;
  8107. struct dpll clock;
  8108. int port_clock;
  8109. int refclk = i9xx_pll_refclk(dev, pipe_config);
  8110. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  8111. fp = pipe_config->dpll_hw_state.fp0;
  8112. else
  8113. fp = pipe_config->dpll_hw_state.fp1;
  8114. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  8115. if (IS_PINEVIEW(dev_priv)) {
  8116. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  8117. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8118. } else {
  8119. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  8120. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  8121. }
  8122. if (!IS_GEN2(dev_priv)) {
  8123. if (IS_PINEVIEW(dev_priv))
  8124. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  8125. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  8126. else
  8127. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  8128. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8129. switch (dpll & DPLL_MODE_MASK) {
  8130. case DPLLB_MODE_DAC_SERIAL:
  8131. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  8132. 5 : 10;
  8133. break;
  8134. case DPLLB_MODE_LVDS:
  8135. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  8136. 7 : 14;
  8137. break;
  8138. default:
  8139. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  8140. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  8141. return;
  8142. }
  8143. if (IS_PINEVIEW(dev_priv))
  8144. port_clock = pnv_calc_dpll_params(refclk, &clock);
  8145. else
  8146. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8147. } else {
  8148. u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
  8149. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  8150. if (is_lvds) {
  8151. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  8152. DPLL_FPA01_P1_POST_DIV_SHIFT);
  8153. if (lvds & LVDS_CLKB_POWER_UP)
  8154. clock.p2 = 7;
  8155. else
  8156. clock.p2 = 14;
  8157. } else {
  8158. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  8159. clock.p1 = 2;
  8160. else {
  8161. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  8162. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  8163. }
  8164. if (dpll & PLL_P2_DIVIDE_BY_4)
  8165. clock.p2 = 4;
  8166. else
  8167. clock.p2 = 2;
  8168. }
  8169. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  8170. }
  8171. /*
  8172. * This value includes pixel_multiplier. We will use
  8173. * port_clock to compute adjusted_mode.crtc_clock in the
  8174. * encoder's get_config() function.
  8175. */
  8176. pipe_config->port_clock = port_clock;
  8177. }
  8178. int intel_dotclock_calculate(int link_freq,
  8179. const struct intel_link_m_n *m_n)
  8180. {
  8181. /*
  8182. * The calculation for the data clock is:
  8183. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  8184. * But we want to avoid losing precison if possible, so:
  8185. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  8186. *
  8187. * and the link clock is simpler:
  8188. * link_clock = (m * link_clock) / n
  8189. */
  8190. if (!m_n->link_n)
  8191. return 0;
  8192. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  8193. }
  8194. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  8195. struct intel_crtc_state *pipe_config)
  8196. {
  8197. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8198. /* read out port_clock from the DPLL */
  8199. i9xx_crtc_clock_get(crtc, pipe_config);
  8200. /*
  8201. * In case there is an active pipe without active ports,
  8202. * we may need some idea for the dotclock anyway.
  8203. * Calculate one based on the FDI configuration.
  8204. */
  8205. pipe_config->base.adjusted_mode.crtc_clock =
  8206. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  8207. &pipe_config->fdi_m_n);
  8208. }
  8209. /** Returns the currently programmed mode of the given pipe. */
  8210. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  8211. struct drm_crtc *crtc)
  8212. {
  8213. struct drm_i915_private *dev_priv = to_i915(dev);
  8214. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8215. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  8216. struct drm_display_mode *mode;
  8217. struct intel_crtc_state *pipe_config;
  8218. int htot = I915_READ(HTOTAL(cpu_transcoder));
  8219. int hsync = I915_READ(HSYNC(cpu_transcoder));
  8220. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  8221. int vsync = I915_READ(VSYNC(cpu_transcoder));
  8222. enum pipe pipe = intel_crtc->pipe;
  8223. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  8224. if (!mode)
  8225. return NULL;
  8226. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  8227. if (!pipe_config) {
  8228. kfree(mode);
  8229. return NULL;
  8230. }
  8231. /*
  8232. * Construct a pipe_config sufficient for getting the clock info
  8233. * back out of crtc_clock_get.
  8234. *
  8235. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  8236. * to use a real value here instead.
  8237. */
  8238. pipe_config->cpu_transcoder = (enum transcoder) pipe;
  8239. pipe_config->pixel_multiplier = 1;
  8240. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  8241. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  8242. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  8243. i9xx_crtc_clock_get(intel_crtc, pipe_config);
  8244. mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
  8245. mode->hdisplay = (htot & 0xffff) + 1;
  8246. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  8247. mode->hsync_start = (hsync & 0xffff) + 1;
  8248. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  8249. mode->vdisplay = (vtot & 0xffff) + 1;
  8250. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  8251. mode->vsync_start = (vsync & 0xffff) + 1;
  8252. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  8253. drm_mode_set_name(mode);
  8254. kfree(pipe_config);
  8255. return mode;
  8256. }
  8257. static void intel_crtc_destroy(struct drm_crtc *crtc)
  8258. {
  8259. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8260. struct drm_device *dev = crtc->dev;
  8261. struct intel_flip_work *work;
  8262. spin_lock_irq(&dev->event_lock);
  8263. work = intel_crtc->flip_work;
  8264. intel_crtc->flip_work = NULL;
  8265. spin_unlock_irq(&dev->event_lock);
  8266. if (work) {
  8267. cancel_work_sync(&work->mmio_work);
  8268. cancel_work_sync(&work->unpin_work);
  8269. kfree(work);
  8270. }
  8271. drm_crtc_cleanup(crtc);
  8272. kfree(intel_crtc);
  8273. }
  8274. static void intel_unpin_work_fn(struct work_struct *__work)
  8275. {
  8276. struct intel_flip_work *work =
  8277. container_of(__work, struct intel_flip_work, unpin_work);
  8278. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  8279. struct drm_device *dev = crtc->base.dev;
  8280. struct drm_plane *primary = crtc->base.primary;
  8281. if (is_mmio_work(work))
  8282. flush_work(&work->mmio_work);
  8283. mutex_lock(&dev->struct_mutex);
  8284. intel_unpin_fb_vma(work->old_vma);
  8285. i915_gem_object_put(work->pending_flip_obj);
  8286. mutex_unlock(&dev->struct_mutex);
  8287. i915_gem_request_put(work->flip_queued_req);
  8288. intel_frontbuffer_flip_complete(to_i915(dev),
  8289. to_intel_plane(primary)->frontbuffer_bit);
  8290. intel_fbc_post_update(crtc);
  8291. drm_framebuffer_unreference(work->old_fb);
  8292. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  8293. atomic_dec(&crtc->unpin_work_count);
  8294. kfree(work);
  8295. }
  8296. /* Is 'a' after or equal to 'b'? */
  8297. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  8298. {
  8299. return !((a - b) & 0x80000000);
  8300. }
  8301. static bool __pageflip_finished_cs(struct intel_crtc *crtc,
  8302. struct intel_flip_work *work)
  8303. {
  8304. struct drm_device *dev = crtc->base.dev;
  8305. struct drm_i915_private *dev_priv = to_i915(dev);
  8306. if (abort_flip_on_reset(crtc))
  8307. return true;
  8308. /*
  8309. * The relevant registers doen't exist on pre-ctg.
  8310. * As the flip done interrupt doesn't trigger for mmio
  8311. * flips on gmch platforms, a flip count check isn't
  8312. * really needed there. But since ctg has the registers,
  8313. * include it in the check anyway.
  8314. */
  8315. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  8316. return true;
  8317. /*
  8318. * BDW signals flip done immediately if the plane
  8319. * is disabled, even if the plane enable is already
  8320. * armed to occur at the next vblank :(
  8321. */
  8322. /*
  8323. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  8324. * used the same base address. In that case the mmio flip might
  8325. * have completed, but the CS hasn't even executed the flip yet.
  8326. *
  8327. * A flip count check isn't enough as the CS might have updated
  8328. * the base address just after start of vblank, but before we
  8329. * managed to process the interrupt. This means we'd complete the
  8330. * CS flip too soon.
  8331. *
  8332. * Combining both checks should get us a good enough result. It may
  8333. * still happen that the CS flip has been executed, but has not
  8334. * yet actually completed. But in case the base address is the same
  8335. * anyway, we don't really care.
  8336. */
  8337. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  8338. crtc->flip_work->gtt_offset &&
  8339. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
  8340. crtc->flip_work->flip_count);
  8341. }
  8342. static bool
  8343. __pageflip_finished_mmio(struct intel_crtc *crtc,
  8344. struct intel_flip_work *work)
  8345. {
  8346. /*
  8347. * MMIO work completes when vblank is different from
  8348. * flip_queued_vblank.
  8349. *
  8350. * Reset counter value doesn't matter, this is handled by
  8351. * i915_wait_request finishing early, so no need to handle
  8352. * reset here.
  8353. */
  8354. return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
  8355. }
  8356. static bool pageflip_finished(struct intel_crtc *crtc,
  8357. struct intel_flip_work *work)
  8358. {
  8359. if (!atomic_read(&work->pending))
  8360. return false;
  8361. smp_rmb();
  8362. if (is_mmio_work(work))
  8363. return __pageflip_finished_mmio(crtc, work);
  8364. else
  8365. return __pageflip_finished_cs(crtc, work);
  8366. }
  8367. void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
  8368. {
  8369. struct drm_device *dev = &dev_priv->drm;
  8370. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  8371. struct intel_flip_work *work;
  8372. unsigned long flags;
  8373. /* Ignore early vblank irqs */
  8374. if (!crtc)
  8375. return;
  8376. /*
  8377. * This is called both by irq handlers and the reset code (to complete
  8378. * lost pageflips) so needs the full irqsave spinlocks.
  8379. */
  8380. spin_lock_irqsave(&dev->event_lock, flags);
  8381. work = crtc->flip_work;
  8382. if (work != NULL &&
  8383. !is_mmio_work(work) &&
  8384. pageflip_finished(crtc, work))
  8385. page_flip_completed(crtc);
  8386. spin_unlock_irqrestore(&dev->event_lock, flags);
  8387. }
  8388. void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
  8389. {
  8390. struct drm_device *dev = &dev_priv->drm;
  8391. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  8392. struct intel_flip_work *work;
  8393. unsigned long flags;
  8394. /* Ignore early vblank irqs */
  8395. if (!crtc)
  8396. return;
  8397. /*
  8398. * This is called both by irq handlers and the reset code (to complete
  8399. * lost pageflips) so needs the full irqsave spinlocks.
  8400. */
  8401. spin_lock_irqsave(&dev->event_lock, flags);
  8402. work = crtc->flip_work;
  8403. if (work != NULL &&
  8404. is_mmio_work(work) &&
  8405. pageflip_finished(crtc, work))
  8406. page_flip_completed(crtc);
  8407. spin_unlock_irqrestore(&dev->event_lock, flags);
  8408. }
  8409. static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
  8410. struct intel_flip_work *work)
  8411. {
  8412. work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
  8413. /* Ensure that the work item is consistent when activating it ... */
  8414. smp_mb__before_atomic();
  8415. atomic_set(&work->pending, 1);
  8416. }
  8417. static int intel_gen2_queue_flip(struct drm_device *dev,
  8418. struct drm_crtc *crtc,
  8419. struct drm_framebuffer *fb,
  8420. struct drm_i915_gem_object *obj,
  8421. struct drm_i915_gem_request *req,
  8422. uint32_t flags)
  8423. {
  8424. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8425. u32 flip_mask, *cs;
  8426. cs = intel_ring_begin(req, 6);
  8427. if (IS_ERR(cs))
  8428. return PTR_ERR(cs);
  8429. /* Can't queue multiple flips, so wait for the previous
  8430. * one to finish before executing the next.
  8431. */
  8432. if (intel_crtc->plane)
  8433. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  8434. else
  8435. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  8436. *cs++ = MI_WAIT_FOR_EVENT | flip_mask;
  8437. *cs++ = MI_NOOP;
  8438. *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
  8439. *cs++ = fb->pitches[0];
  8440. *cs++ = intel_crtc->flip_work->gtt_offset;
  8441. *cs++ = 0; /* aux display base address, unused */
  8442. return 0;
  8443. }
  8444. static int intel_gen3_queue_flip(struct drm_device *dev,
  8445. struct drm_crtc *crtc,
  8446. struct drm_framebuffer *fb,
  8447. struct drm_i915_gem_object *obj,
  8448. struct drm_i915_gem_request *req,
  8449. uint32_t flags)
  8450. {
  8451. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8452. u32 flip_mask, *cs;
  8453. cs = intel_ring_begin(req, 6);
  8454. if (IS_ERR(cs))
  8455. return PTR_ERR(cs);
  8456. if (intel_crtc->plane)
  8457. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  8458. else
  8459. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  8460. *cs++ = MI_WAIT_FOR_EVENT | flip_mask;
  8461. *cs++ = MI_NOOP;
  8462. *cs++ = MI_DISPLAY_FLIP_I915 | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
  8463. *cs++ = fb->pitches[0];
  8464. *cs++ = intel_crtc->flip_work->gtt_offset;
  8465. *cs++ = MI_NOOP;
  8466. return 0;
  8467. }
  8468. static int intel_gen4_queue_flip(struct drm_device *dev,
  8469. struct drm_crtc *crtc,
  8470. struct drm_framebuffer *fb,
  8471. struct drm_i915_gem_object *obj,
  8472. struct drm_i915_gem_request *req,
  8473. uint32_t flags)
  8474. {
  8475. struct drm_i915_private *dev_priv = to_i915(dev);
  8476. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8477. u32 pf, pipesrc, *cs;
  8478. cs = intel_ring_begin(req, 4);
  8479. if (IS_ERR(cs))
  8480. return PTR_ERR(cs);
  8481. /* i965+ uses the linear or tiled offsets from the
  8482. * Display Registers (which do not change across a page-flip)
  8483. * so we need only reprogram the base address.
  8484. */
  8485. *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
  8486. *cs++ = fb->pitches[0];
  8487. *cs++ = intel_crtc->flip_work->gtt_offset |
  8488. intel_fb_modifier_to_tiling(fb->modifier);
  8489. /* XXX Enabling the panel-fitter across page-flip is so far
  8490. * untested on non-native modes, so ignore it for now.
  8491. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  8492. */
  8493. pf = 0;
  8494. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  8495. *cs++ = pf | pipesrc;
  8496. return 0;
  8497. }
  8498. static int intel_gen6_queue_flip(struct drm_device *dev,
  8499. struct drm_crtc *crtc,
  8500. struct drm_framebuffer *fb,
  8501. struct drm_i915_gem_object *obj,
  8502. struct drm_i915_gem_request *req,
  8503. uint32_t flags)
  8504. {
  8505. struct drm_i915_private *dev_priv = to_i915(dev);
  8506. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8507. u32 pf, pipesrc, *cs;
  8508. cs = intel_ring_begin(req, 4);
  8509. if (IS_ERR(cs))
  8510. return PTR_ERR(cs);
  8511. *cs++ = MI_DISPLAY_FLIP | MI_DISPLAY_FLIP_PLANE(intel_crtc->plane);
  8512. *cs++ = fb->pitches[0] | intel_fb_modifier_to_tiling(fb->modifier);
  8513. *cs++ = intel_crtc->flip_work->gtt_offset;
  8514. /* Contrary to the suggestions in the documentation,
  8515. * "Enable Panel Fitter" does not seem to be required when page
  8516. * flipping with a non-native mode, and worse causes a normal
  8517. * modeset to fail.
  8518. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  8519. */
  8520. pf = 0;
  8521. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  8522. *cs++ = pf | pipesrc;
  8523. return 0;
  8524. }
  8525. static int intel_gen7_queue_flip(struct drm_device *dev,
  8526. struct drm_crtc *crtc,
  8527. struct drm_framebuffer *fb,
  8528. struct drm_i915_gem_object *obj,
  8529. struct drm_i915_gem_request *req,
  8530. uint32_t flags)
  8531. {
  8532. struct drm_i915_private *dev_priv = to_i915(dev);
  8533. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8534. u32 *cs, plane_bit = 0;
  8535. int len, ret;
  8536. switch (intel_crtc->plane) {
  8537. case PLANE_A:
  8538. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  8539. break;
  8540. case PLANE_B:
  8541. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  8542. break;
  8543. case PLANE_C:
  8544. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  8545. break;
  8546. default:
  8547. WARN_ONCE(1, "unknown plane in flip command\n");
  8548. return -ENODEV;
  8549. }
  8550. len = 4;
  8551. if (req->engine->id == RCS) {
  8552. len += 6;
  8553. /*
  8554. * On Gen 8, SRM is now taking an extra dword to accommodate
  8555. * 48bits addresses, and we need a NOOP for the batch size to
  8556. * stay even.
  8557. */
  8558. if (IS_GEN8(dev_priv))
  8559. len += 2;
  8560. }
  8561. /*
  8562. * BSpec MI_DISPLAY_FLIP for IVB:
  8563. * "The full packet must be contained within the same cache line."
  8564. *
  8565. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  8566. * cacheline, if we ever start emitting more commands before
  8567. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  8568. * then do the cacheline alignment, and finally emit the
  8569. * MI_DISPLAY_FLIP.
  8570. */
  8571. ret = intel_ring_cacheline_align(req);
  8572. if (ret)
  8573. return ret;
  8574. cs = intel_ring_begin(req, len);
  8575. if (IS_ERR(cs))
  8576. return PTR_ERR(cs);
  8577. /* Unmask the flip-done completion message. Note that the bspec says that
  8578. * we should do this for both the BCS and RCS, and that we must not unmask
  8579. * more than one flip event at any time (or ensure that one flip message
  8580. * can be sent by waiting for flip-done prior to queueing new flips).
  8581. * Experimentation says that BCS works despite DERRMR masking all
  8582. * flip-done completion events and that unmasking all planes at once
  8583. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  8584. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  8585. */
  8586. if (req->engine->id == RCS) {
  8587. *cs++ = MI_LOAD_REGISTER_IMM(1);
  8588. *cs++ = i915_mmio_reg_offset(DERRMR);
  8589. *cs++ = ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  8590. DERRMR_PIPEB_PRI_FLIP_DONE |
  8591. DERRMR_PIPEC_PRI_FLIP_DONE);
  8592. if (IS_GEN8(dev_priv))
  8593. *cs++ = MI_STORE_REGISTER_MEM_GEN8 |
  8594. MI_SRM_LRM_GLOBAL_GTT;
  8595. else
  8596. *cs++ = MI_STORE_REGISTER_MEM | MI_SRM_LRM_GLOBAL_GTT;
  8597. *cs++ = i915_mmio_reg_offset(DERRMR);
  8598. *cs++ = i915_ggtt_offset(req->engine->scratch) + 256;
  8599. if (IS_GEN8(dev_priv)) {
  8600. *cs++ = 0;
  8601. *cs++ = MI_NOOP;
  8602. }
  8603. }
  8604. *cs++ = MI_DISPLAY_FLIP_I915 | plane_bit;
  8605. *cs++ = fb->pitches[0] | intel_fb_modifier_to_tiling(fb->modifier);
  8606. *cs++ = intel_crtc->flip_work->gtt_offset;
  8607. *cs++ = MI_NOOP;
  8608. return 0;
  8609. }
  8610. static bool use_mmio_flip(struct intel_engine_cs *engine,
  8611. struct drm_i915_gem_object *obj)
  8612. {
  8613. /*
  8614. * This is not being used for older platforms, because
  8615. * non-availability of flip done interrupt forces us to use
  8616. * CS flips. Older platforms derive flip done using some clever
  8617. * tricks involving the flip_pending status bits and vblank irqs.
  8618. * So using MMIO flips there would disrupt this mechanism.
  8619. */
  8620. if (engine == NULL)
  8621. return true;
  8622. if (INTEL_GEN(engine->i915) < 5)
  8623. return false;
  8624. if (i915.use_mmio_flip < 0)
  8625. return false;
  8626. else if (i915.use_mmio_flip > 0)
  8627. return true;
  8628. else if (i915.enable_execlists)
  8629. return true;
  8630. return engine != i915_gem_object_last_write_engine(obj);
  8631. }
  8632. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
  8633. unsigned int rotation,
  8634. struct intel_flip_work *work)
  8635. {
  8636. struct drm_device *dev = intel_crtc->base.dev;
  8637. struct drm_i915_private *dev_priv = to_i915(dev);
  8638. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  8639. const enum pipe pipe = intel_crtc->pipe;
  8640. u32 ctl, stride = skl_plane_stride(fb, 0, rotation);
  8641. ctl = I915_READ(PLANE_CTL(pipe, 0));
  8642. ctl &= ~PLANE_CTL_TILED_MASK;
  8643. switch (fb->modifier) {
  8644. case DRM_FORMAT_MOD_NONE:
  8645. break;
  8646. case I915_FORMAT_MOD_X_TILED:
  8647. ctl |= PLANE_CTL_TILED_X;
  8648. break;
  8649. case I915_FORMAT_MOD_Y_TILED:
  8650. ctl |= PLANE_CTL_TILED_Y;
  8651. break;
  8652. case I915_FORMAT_MOD_Yf_TILED:
  8653. ctl |= PLANE_CTL_TILED_YF;
  8654. break;
  8655. default:
  8656. MISSING_CASE(fb->modifier);
  8657. }
  8658. /*
  8659. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  8660. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  8661. */
  8662. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  8663. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  8664. I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
  8665. POSTING_READ(PLANE_SURF(pipe, 0));
  8666. }
  8667. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
  8668. struct intel_flip_work *work)
  8669. {
  8670. struct drm_device *dev = intel_crtc->base.dev;
  8671. struct drm_i915_private *dev_priv = to_i915(dev);
  8672. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  8673. i915_reg_t reg = DSPCNTR(intel_crtc->plane);
  8674. u32 dspcntr;
  8675. dspcntr = I915_READ(reg);
  8676. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  8677. dspcntr |= DISPPLANE_TILED;
  8678. else
  8679. dspcntr &= ~DISPPLANE_TILED;
  8680. I915_WRITE(reg, dspcntr);
  8681. I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
  8682. POSTING_READ(DSPSURF(intel_crtc->plane));
  8683. }
  8684. static void intel_mmio_flip_work_func(struct work_struct *w)
  8685. {
  8686. struct intel_flip_work *work =
  8687. container_of(w, struct intel_flip_work, mmio_work);
  8688. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  8689. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8690. struct intel_framebuffer *intel_fb =
  8691. to_intel_framebuffer(crtc->base.primary->fb);
  8692. struct drm_i915_gem_object *obj = intel_fb->obj;
  8693. WARN_ON(i915_gem_object_wait(obj, 0, MAX_SCHEDULE_TIMEOUT, NULL) < 0);
  8694. intel_pipe_update_start(crtc);
  8695. if (INTEL_GEN(dev_priv) >= 9)
  8696. skl_do_mmio_flip(crtc, work->rotation, work);
  8697. else
  8698. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  8699. ilk_do_mmio_flip(crtc, work);
  8700. intel_pipe_update_end(crtc, work);
  8701. }
  8702. static int intel_default_queue_flip(struct drm_device *dev,
  8703. struct drm_crtc *crtc,
  8704. struct drm_framebuffer *fb,
  8705. struct drm_i915_gem_object *obj,
  8706. struct drm_i915_gem_request *req,
  8707. uint32_t flags)
  8708. {
  8709. return -ENODEV;
  8710. }
  8711. static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
  8712. struct intel_crtc *intel_crtc,
  8713. struct intel_flip_work *work)
  8714. {
  8715. u32 addr, vblank;
  8716. if (!atomic_read(&work->pending))
  8717. return false;
  8718. smp_rmb();
  8719. vblank = intel_crtc_get_vblank_counter(intel_crtc);
  8720. if (work->flip_ready_vblank == 0) {
  8721. if (work->flip_queued_req &&
  8722. !i915_gem_request_completed(work->flip_queued_req))
  8723. return false;
  8724. work->flip_ready_vblank = vblank;
  8725. }
  8726. if (vblank - work->flip_ready_vblank < 3)
  8727. return false;
  8728. /* Potential stall - if we see that the flip has happened,
  8729. * assume a missed interrupt. */
  8730. if (INTEL_GEN(dev_priv) >= 4)
  8731. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  8732. else
  8733. addr = I915_READ(DSPADDR(intel_crtc->plane));
  8734. /* There is a potential issue here with a false positive after a flip
  8735. * to the same address. We could address this by checking for a
  8736. * non-incrementing frame counter.
  8737. */
  8738. return addr == work->gtt_offset;
  8739. }
  8740. void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
  8741. {
  8742. struct drm_device *dev = &dev_priv->drm;
  8743. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  8744. struct intel_flip_work *work;
  8745. WARN_ON(!in_interrupt());
  8746. if (crtc == NULL)
  8747. return;
  8748. spin_lock(&dev->event_lock);
  8749. work = crtc->flip_work;
  8750. if (work != NULL && !is_mmio_work(work) &&
  8751. __pageflip_stall_check_cs(dev_priv, crtc, work)) {
  8752. WARN_ONCE(1,
  8753. "Kicking stuck page flip: queued at %d, now %d\n",
  8754. work->flip_queued_vblank, intel_crtc_get_vblank_counter(crtc));
  8755. page_flip_completed(crtc);
  8756. work = NULL;
  8757. }
  8758. if (work != NULL && !is_mmio_work(work) &&
  8759. intel_crtc_get_vblank_counter(crtc) - work->flip_queued_vblank > 1)
  8760. intel_queue_rps_boost_for_request(work->flip_queued_req);
  8761. spin_unlock(&dev->event_lock);
  8762. }
  8763. __maybe_unused
  8764. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  8765. struct drm_framebuffer *fb,
  8766. struct drm_pending_vblank_event *event,
  8767. uint32_t page_flip_flags)
  8768. {
  8769. struct drm_device *dev = crtc->dev;
  8770. struct drm_i915_private *dev_priv = to_i915(dev);
  8771. struct drm_framebuffer *old_fb = crtc->primary->fb;
  8772. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  8773. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  8774. struct drm_plane *primary = crtc->primary;
  8775. enum pipe pipe = intel_crtc->pipe;
  8776. struct intel_flip_work *work;
  8777. struct intel_engine_cs *engine;
  8778. bool mmio_flip;
  8779. struct drm_i915_gem_request *request;
  8780. struct i915_vma *vma;
  8781. int ret;
  8782. /*
  8783. * drm_mode_page_flip_ioctl() should already catch this, but double
  8784. * check to be safe. In the future we may enable pageflipping from
  8785. * a disabled primary plane.
  8786. */
  8787. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  8788. return -EBUSY;
  8789. /* Can't change pixel format via MI display flips. */
  8790. if (fb->format != crtc->primary->fb->format)
  8791. return -EINVAL;
  8792. /*
  8793. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  8794. * Note that pitch changes could also affect these register.
  8795. */
  8796. if (INTEL_GEN(dev_priv) > 3 &&
  8797. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  8798. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  8799. return -EINVAL;
  8800. if (i915_terminally_wedged(&dev_priv->gpu_error))
  8801. goto out_hang;
  8802. work = kzalloc(sizeof(*work), GFP_KERNEL);
  8803. if (work == NULL)
  8804. return -ENOMEM;
  8805. work->event = event;
  8806. work->crtc = crtc;
  8807. work->old_fb = old_fb;
  8808. INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
  8809. ret = drm_crtc_vblank_get(crtc);
  8810. if (ret)
  8811. goto free_work;
  8812. /* We borrow the event spin lock for protecting flip_work */
  8813. spin_lock_irq(&dev->event_lock);
  8814. if (intel_crtc->flip_work) {
  8815. /* Before declaring the flip queue wedged, check if
  8816. * the hardware completed the operation behind our backs.
  8817. */
  8818. if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
  8819. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  8820. page_flip_completed(intel_crtc);
  8821. } else {
  8822. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  8823. spin_unlock_irq(&dev->event_lock);
  8824. drm_crtc_vblank_put(crtc);
  8825. kfree(work);
  8826. return -EBUSY;
  8827. }
  8828. }
  8829. intel_crtc->flip_work = work;
  8830. spin_unlock_irq(&dev->event_lock);
  8831. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  8832. flush_workqueue(dev_priv->wq);
  8833. /* Reference the objects for the scheduled work. */
  8834. drm_framebuffer_reference(work->old_fb);
  8835. crtc->primary->fb = fb;
  8836. update_state_fb(crtc->primary);
  8837. work->pending_flip_obj = i915_gem_object_get(obj);
  8838. ret = i915_mutex_lock_interruptible(dev);
  8839. if (ret)
  8840. goto cleanup;
  8841. intel_crtc->reset_count = i915_reset_count(&dev_priv->gpu_error);
  8842. if (i915_reset_in_progress_or_wedged(&dev_priv->gpu_error)) {
  8843. ret = -EIO;
  8844. goto unlock;
  8845. }
  8846. atomic_inc(&intel_crtc->unpin_work_count);
  8847. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  8848. work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
  8849. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  8850. engine = dev_priv->engine[BCS];
  8851. if (fb->modifier != old_fb->modifier)
  8852. /* vlv: DISPLAY_FLIP fails to change tiling */
  8853. engine = NULL;
  8854. } else if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
  8855. engine = dev_priv->engine[BCS];
  8856. } else if (INTEL_GEN(dev_priv) >= 7) {
  8857. engine = i915_gem_object_last_write_engine(obj);
  8858. if (engine == NULL || engine->id != RCS)
  8859. engine = dev_priv->engine[BCS];
  8860. } else {
  8861. engine = dev_priv->engine[RCS];
  8862. }
  8863. mmio_flip = use_mmio_flip(engine, obj);
  8864. vma = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  8865. if (IS_ERR(vma)) {
  8866. ret = PTR_ERR(vma);
  8867. goto cleanup_pending;
  8868. }
  8869. work->old_vma = to_intel_plane_state(primary->state)->vma;
  8870. to_intel_plane_state(primary->state)->vma = vma;
  8871. work->gtt_offset = i915_ggtt_offset(vma) + intel_crtc->dspaddr_offset;
  8872. work->rotation = crtc->primary->state->rotation;
  8873. /*
  8874. * There's the potential that the next frame will not be compatible with
  8875. * FBC, so we want to call pre_update() before the actual page flip.
  8876. * The problem is that pre_update() caches some information about the fb
  8877. * object, so we want to do this only after the object is pinned. Let's
  8878. * be on the safe side and do this immediately before scheduling the
  8879. * flip.
  8880. */
  8881. intel_fbc_pre_update(intel_crtc, intel_crtc->config,
  8882. to_intel_plane_state(primary->state));
  8883. if (mmio_flip) {
  8884. INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
  8885. queue_work(system_unbound_wq, &work->mmio_work);
  8886. } else {
  8887. request = i915_gem_request_alloc(engine,
  8888. dev_priv->kernel_context);
  8889. if (IS_ERR(request)) {
  8890. ret = PTR_ERR(request);
  8891. goto cleanup_unpin;
  8892. }
  8893. ret = i915_gem_request_await_object(request, obj, false);
  8894. if (ret)
  8895. goto cleanup_request;
  8896. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  8897. page_flip_flags);
  8898. if (ret)
  8899. goto cleanup_request;
  8900. intel_mark_page_flip_active(intel_crtc, work);
  8901. work->flip_queued_req = i915_gem_request_get(request);
  8902. i915_add_request_no_flush(request);
  8903. }
  8904. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  8905. i915_gem_track_fb(intel_fb_obj(old_fb), obj,
  8906. to_intel_plane(primary)->frontbuffer_bit);
  8907. mutex_unlock(&dev->struct_mutex);
  8908. intel_frontbuffer_flip_prepare(to_i915(dev),
  8909. to_intel_plane(primary)->frontbuffer_bit);
  8910. trace_i915_flip_request(intel_crtc->plane, obj);
  8911. return 0;
  8912. cleanup_request:
  8913. i915_add_request_no_flush(request);
  8914. cleanup_unpin:
  8915. to_intel_plane_state(primary->state)->vma = work->old_vma;
  8916. intel_unpin_fb_vma(vma);
  8917. cleanup_pending:
  8918. atomic_dec(&intel_crtc->unpin_work_count);
  8919. unlock:
  8920. mutex_unlock(&dev->struct_mutex);
  8921. cleanup:
  8922. crtc->primary->fb = old_fb;
  8923. update_state_fb(crtc->primary);
  8924. i915_gem_object_put(obj);
  8925. drm_framebuffer_unreference(work->old_fb);
  8926. spin_lock_irq(&dev->event_lock);
  8927. intel_crtc->flip_work = NULL;
  8928. spin_unlock_irq(&dev->event_lock);
  8929. drm_crtc_vblank_put(crtc);
  8930. free_work:
  8931. kfree(work);
  8932. if (ret == -EIO) {
  8933. struct drm_atomic_state *state;
  8934. struct drm_plane_state *plane_state;
  8935. out_hang:
  8936. state = drm_atomic_state_alloc(dev);
  8937. if (!state)
  8938. return -ENOMEM;
  8939. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  8940. retry:
  8941. plane_state = drm_atomic_get_plane_state(state, primary);
  8942. ret = PTR_ERR_OR_ZERO(plane_state);
  8943. if (!ret) {
  8944. drm_atomic_set_fb_for_plane(plane_state, fb);
  8945. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  8946. if (!ret)
  8947. ret = drm_atomic_commit(state);
  8948. }
  8949. if (ret == -EDEADLK) {
  8950. drm_modeset_backoff(state->acquire_ctx);
  8951. drm_atomic_state_clear(state);
  8952. goto retry;
  8953. }
  8954. drm_atomic_state_put(state);
  8955. if (ret == 0 && event) {
  8956. spin_lock_irq(&dev->event_lock);
  8957. drm_crtc_send_vblank_event(crtc, event);
  8958. spin_unlock_irq(&dev->event_lock);
  8959. }
  8960. }
  8961. return ret;
  8962. }
  8963. /**
  8964. * intel_wm_need_update - Check whether watermarks need updating
  8965. * @plane: drm plane
  8966. * @state: new plane state
  8967. *
  8968. * Check current plane state versus the new one to determine whether
  8969. * watermarks need to be recalculated.
  8970. *
  8971. * Returns true or false.
  8972. */
  8973. static bool intel_wm_need_update(struct drm_plane *plane,
  8974. struct drm_plane_state *state)
  8975. {
  8976. struct intel_plane_state *new = to_intel_plane_state(state);
  8977. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  8978. /* Update watermarks on tiling or size changes. */
  8979. if (new->base.visible != cur->base.visible)
  8980. return true;
  8981. if (!cur->base.fb || !new->base.fb)
  8982. return false;
  8983. if (cur->base.fb->modifier != new->base.fb->modifier ||
  8984. cur->base.rotation != new->base.rotation ||
  8985. drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
  8986. drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
  8987. drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
  8988. drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
  8989. return true;
  8990. return false;
  8991. }
  8992. static bool needs_scaling(struct intel_plane_state *state)
  8993. {
  8994. int src_w = drm_rect_width(&state->base.src) >> 16;
  8995. int src_h = drm_rect_height(&state->base.src) >> 16;
  8996. int dst_w = drm_rect_width(&state->base.dst);
  8997. int dst_h = drm_rect_height(&state->base.dst);
  8998. return (src_w != dst_w || src_h != dst_h);
  8999. }
  9000. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  9001. struct drm_plane_state *plane_state)
  9002. {
  9003. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  9004. struct drm_crtc *crtc = crtc_state->crtc;
  9005. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9006. struct drm_plane *plane = plane_state->plane;
  9007. struct drm_device *dev = crtc->dev;
  9008. struct drm_i915_private *dev_priv = to_i915(dev);
  9009. struct intel_plane_state *old_plane_state =
  9010. to_intel_plane_state(plane->state);
  9011. bool mode_changed = needs_modeset(crtc_state);
  9012. bool was_crtc_enabled = crtc->state->active;
  9013. bool is_crtc_enabled = crtc_state->active;
  9014. bool turn_off, turn_on, visible, was_visible;
  9015. struct drm_framebuffer *fb = plane_state->fb;
  9016. int ret;
  9017. if (INTEL_GEN(dev_priv) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
  9018. ret = skl_update_scaler_plane(
  9019. to_intel_crtc_state(crtc_state),
  9020. to_intel_plane_state(plane_state));
  9021. if (ret)
  9022. return ret;
  9023. }
  9024. was_visible = old_plane_state->base.visible;
  9025. visible = plane_state->visible;
  9026. if (!was_crtc_enabled && WARN_ON(was_visible))
  9027. was_visible = false;
  9028. /*
  9029. * Visibility is calculated as if the crtc was on, but
  9030. * after scaler setup everything depends on it being off
  9031. * when the crtc isn't active.
  9032. *
  9033. * FIXME this is wrong for watermarks. Watermarks should also
  9034. * be computed as if the pipe would be active. Perhaps move
  9035. * per-plane wm computation to the .check_plane() hook, and
  9036. * only combine the results from all planes in the current place?
  9037. */
  9038. if (!is_crtc_enabled)
  9039. plane_state->visible = visible = false;
  9040. if (!was_visible && !visible)
  9041. return 0;
  9042. if (fb != old_plane_state->base.fb)
  9043. pipe_config->fb_changed = true;
  9044. turn_off = was_visible && (!visible || mode_changed);
  9045. turn_on = visible && (!was_visible || mode_changed);
  9046. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  9047. intel_crtc->base.base.id,
  9048. intel_crtc->base.name,
  9049. plane->base.id, plane->name,
  9050. fb ? fb->base.id : -1);
  9051. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  9052. plane->base.id, plane->name,
  9053. was_visible, visible,
  9054. turn_off, turn_on, mode_changed);
  9055. if (turn_on) {
  9056. pipe_config->update_wm_pre = true;
  9057. /* must disable cxsr around plane enable/disable */
  9058. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  9059. pipe_config->disable_cxsr = true;
  9060. } else if (turn_off) {
  9061. pipe_config->update_wm_post = true;
  9062. /* must disable cxsr around plane enable/disable */
  9063. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  9064. pipe_config->disable_cxsr = true;
  9065. } else if (intel_wm_need_update(plane, plane_state)) {
  9066. /* FIXME bollocks */
  9067. pipe_config->update_wm_pre = true;
  9068. pipe_config->update_wm_post = true;
  9069. }
  9070. /* Pre-gen9 platforms need two-step watermark updates */
  9071. if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
  9072. INTEL_GEN(dev_priv) < 9 && dev_priv->display.optimize_watermarks)
  9073. to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
  9074. if (visible || was_visible)
  9075. pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
  9076. /*
  9077. * WaCxSRDisabledForSpriteScaling:ivb
  9078. *
  9079. * cstate->update_wm was already set above, so this flag will
  9080. * take effect when we commit and program watermarks.
  9081. */
  9082. if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev_priv) &&
  9083. needs_scaling(to_intel_plane_state(plane_state)) &&
  9084. !needs_scaling(old_plane_state))
  9085. pipe_config->disable_lp_wm = true;
  9086. return 0;
  9087. }
  9088. static bool encoders_cloneable(const struct intel_encoder *a,
  9089. const struct intel_encoder *b)
  9090. {
  9091. /* masks could be asymmetric, so check both ways */
  9092. return a == b || (a->cloneable & (1 << b->type) &&
  9093. b->cloneable & (1 << a->type));
  9094. }
  9095. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  9096. struct intel_crtc *crtc,
  9097. struct intel_encoder *encoder)
  9098. {
  9099. struct intel_encoder *source_encoder;
  9100. struct drm_connector *connector;
  9101. struct drm_connector_state *connector_state;
  9102. int i;
  9103. for_each_connector_in_state(state, connector, connector_state, i) {
  9104. if (connector_state->crtc != &crtc->base)
  9105. continue;
  9106. source_encoder =
  9107. to_intel_encoder(connector_state->best_encoder);
  9108. if (!encoders_cloneable(encoder, source_encoder))
  9109. return false;
  9110. }
  9111. return true;
  9112. }
  9113. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  9114. struct drm_crtc_state *crtc_state)
  9115. {
  9116. struct drm_device *dev = crtc->dev;
  9117. struct drm_i915_private *dev_priv = to_i915(dev);
  9118. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9119. struct intel_crtc_state *pipe_config =
  9120. to_intel_crtc_state(crtc_state);
  9121. struct drm_atomic_state *state = crtc_state->state;
  9122. int ret;
  9123. bool mode_changed = needs_modeset(crtc_state);
  9124. if (mode_changed && !crtc_state->active)
  9125. pipe_config->update_wm_post = true;
  9126. if (mode_changed && crtc_state->enable &&
  9127. dev_priv->display.crtc_compute_clock &&
  9128. !WARN_ON(pipe_config->shared_dpll)) {
  9129. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  9130. pipe_config);
  9131. if (ret)
  9132. return ret;
  9133. }
  9134. if (crtc_state->color_mgmt_changed) {
  9135. ret = intel_color_check(crtc, crtc_state);
  9136. if (ret)
  9137. return ret;
  9138. /*
  9139. * Changing color management on Intel hardware is
  9140. * handled as part of planes update.
  9141. */
  9142. crtc_state->planes_changed = true;
  9143. }
  9144. ret = 0;
  9145. if (dev_priv->display.compute_pipe_wm) {
  9146. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  9147. if (ret) {
  9148. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  9149. return ret;
  9150. }
  9151. }
  9152. if (dev_priv->display.compute_intermediate_wm &&
  9153. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  9154. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  9155. return 0;
  9156. /*
  9157. * Calculate 'intermediate' watermarks that satisfy both the
  9158. * old state and the new state. We can program these
  9159. * immediately.
  9160. */
  9161. ret = dev_priv->display.compute_intermediate_wm(dev,
  9162. intel_crtc,
  9163. pipe_config);
  9164. if (ret) {
  9165. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  9166. return ret;
  9167. }
  9168. } else if (dev_priv->display.compute_intermediate_wm) {
  9169. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  9170. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  9171. }
  9172. if (INTEL_GEN(dev_priv) >= 9) {
  9173. if (mode_changed)
  9174. ret = skl_update_scaler_crtc(pipe_config);
  9175. if (!ret)
  9176. ret = intel_atomic_setup_scalers(dev_priv, intel_crtc,
  9177. pipe_config);
  9178. }
  9179. return ret;
  9180. }
  9181. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  9182. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  9183. .atomic_begin = intel_begin_crtc_commit,
  9184. .atomic_flush = intel_finish_crtc_commit,
  9185. .atomic_check = intel_crtc_atomic_check,
  9186. };
  9187. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  9188. {
  9189. struct intel_connector *connector;
  9190. for_each_intel_connector(dev, connector) {
  9191. if (connector->base.state->crtc)
  9192. drm_connector_unreference(&connector->base);
  9193. if (connector->base.encoder) {
  9194. connector->base.state->best_encoder =
  9195. connector->base.encoder;
  9196. connector->base.state->crtc =
  9197. connector->base.encoder->crtc;
  9198. drm_connector_reference(&connector->base);
  9199. } else {
  9200. connector->base.state->best_encoder = NULL;
  9201. connector->base.state->crtc = NULL;
  9202. }
  9203. }
  9204. }
  9205. static void
  9206. connected_sink_compute_bpp(struct intel_connector *connector,
  9207. struct intel_crtc_state *pipe_config)
  9208. {
  9209. const struct drm_display_info *info = &connector->base.display_info;
  9210. int bpp = pipe_config->pipe_bpp;
  9211. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  9212. connector->base.base.id,
  9213. connector->base.name);
  9214. /* Don't use an invalid EDID bpc value */
  9215. if (info->bpc != 0 && info->bpc * 3 < bpp) {
  9216. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  9217. bpp, info->bpc * 3);
  9218. pipe_config->pipe_bpp = info->bpc * 3;
  9219. }
  9220. /* Clamp bpp to 8 on screens without EDID 1.4 */
  9221. if (info->bpc == 0 && bpp > 24) {
  9222. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  9223. bpp);
  9224. pipe_config->pipe_bpp = 24;
  9225. }
  9226. }
  9227. static int
  9228. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  9229. struct intel_crtc_state *pipe_config)
  9230. {
  9231. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9232. struct drm_atomic_state *state;
  9233. struct drm_connector *connector;
  9234. struct drm_connector_state *connector_state;
  9235. int bpp, i;
  9236. if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  9237. IS_CHERRYVIEW(dev_priv)))
  9238. bpp = 10*3;
  9239. else if (INTEL_GEN(dev_priv) >= 5)
  9240. bpp = 12*3;
  9241. else
  9242. bpp = 8*3;
  9243. pipe_config->pipe_bpp = bpp;
  9244. state = pipe_config->base.state;
  9245. /* Clamp display bpp to EDID value */
  9246. for_each_connector_in_state(state, connector, connector_state, i) {
  9247. if (connector_state->crtc != &crtc->base)
  9248. continue;
  9249. connected_sink_compute_bpp(to_intel_connector(connector),
  9250. pipe_config);
  9251. }
  9252. return bpp;
  9253. }
  9254. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  9255. {
  9256. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  9257. "type: 0x%x flags: 0x%x\n",
  9258. mode->crtc_clock,
  9259. mode->crtc_hdisplay, mode->crtc_hsync_start,
  9260. mode->crtc_hsync_end, mode->crtc_htotal,
  9261. mode->crtc_vdisplay, mode->crtc_vsync_start,
  9262. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  9263. }
  9264. static inline void
  9265. intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
  9266. unsigned int lane_count, struct intel_link_m_n *m_n)
  9267. {
  9268. DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  9269. id, lane_count,
  9270. m_n->gmch_m, m_n->gmch_n,
  9271. m_n->link_m, m_n->link_n, m_n->tu);
  9272. }
  9273. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  9274. struct intel_crtc_state *pipe_config,
  9275. const char *context)
  9276. {
  9277. struct drm_device *dev = crtc->base.dev;
  9278. struct drm_i915_private *dev_priv = to_i915(dev);
  9279. struct drm_plane *plane;
  9280. struct intel_plane *intel_plane;
  9281. struct intel_plane_state *state;
  9282. struct drm_framebuffer *fb;
  9283. DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
  9284. crtc->base.base.id, crtc->base.name, context);
  9285. DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
  9286. transcoder_name(pipe_config->cpu_transcoder),
  9287. pipe_config->pipe_bpp, pipe_config->dither);
  9288. if (pipe_config->has_pch_encoder)
  9289. intel_dump_m_n_config(pipe_config, "fdi",
  9290. pipe_config->fdi_lanes,
  9291. &pipe_config->fdi_m_n);
  9292. if (intel_crtc_has_dp_encoder(pipe_config)) {
  9293. intel_dump_m_n_config(pipe_config, "dp m_n",
  9294. pipe_config->lane_count, &pipe_config->dp_m_n);
  9295. if (pipe_config->has_drrs)
  9296. intel_dump_m_n_config(pipe_config, "dp m2_n2",
  9297. pipe_config->lane_count,
  9298. &pipe_config->dp_m2_n2);
  9299. }
  9300. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  9301. pipe_config->has_audio, pipe_config->has_infoframe);
  9302. DRM_DEBUG_KMS("requested mode:\n");
  9303. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  9304. DRM_DEBUG_KMS("adjusted mode:\n");
  9305. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  9306. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  9307. DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
  9308. pipe_config->port_clock,
  9309. pipe_config->pipe_src_w, pipe_config->pipe_src_h,
  9310. pipe_config->pixel_rate);
  9311. if (INTEL_GEN(dev_priv) >= 9)
  9312. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  9313. crtc->num_scalers,
  9314. pipe_config->scaler_state.scaler_users,
  9315. pipe_config->scaler_state.scaler_id);
  9316. if (HAS_GMCH_DISPLAY(dev_priv))
  9317. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  9318. pipe_config->gmch_pfit.control,
  9319. pipe_config->gmch_pfit.pgm_ratios,
  9320. pipe_config->gmch_pfit.lvds_border_bits);
  9321. else
  9322. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  9323. pipe_config->pch_pfit.pos,
  9324. pipe_config->pch_pfit.size,
  9325. enableddisabled(pipe_config->pch_pfit.enabled));
  9326. DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
  9327. pipe_config->ips_enabled, pipe_config->double_wide);
  9328. intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
  9329. DRM_DEBUG_KMS("planes on this crtc\n");
  9330. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  9331. struct drm_format_name_buf format_name;
  9332. intel_plane = to_intel_plane(plane);
  9333. if (intel_plane->pipe != crtc->pipe)
  9334. continue;
  9335. state = to_intel_plane_state(plane->state);
  9336. fb = state->base.fb;
  9337. if (!fb) {
  9338. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  9339. plane->base.id, plane->name, state->scaler_id);
  9340. continue;
  9341. }
  9342. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
  9343. plane->base.id, plane->name,
  9344. fb->base.id, fb->width, fb->height,
  9345. drm_get_format_name(fb->format->format, &format_name));
  9346. if (INTEL_GEN(dev_priv) >= 9)
  9347. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  9348. state->scaler_id,
  9349. state->base.src.x1 >> 16,
  9350. state->base.src.y1 >> 16,
  9351. drm_rect_width(&state->base.src) >> 16,
  9352. drm_rect_height(&state->base.src) >> 16,
  9353. state->base.dst.x1, state->base.dst.y1,
  9354. drm_rect_width(&state->base.dst),
  9355. drm_rect_height(&state->base.dst));
  9356. }
  9357. }
  9358. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  9359. {
  9360. struct drm_device *dev = state->dev;
  9361. struct drm_connector *connector;
  9362. unsigned int used_ports = 0;
  9363. unsigned int used_mst_ports = 0;
  9364. /*
  9365. * Walk the connector list instead of the encoder
  9366. * list to detect the problem on ddi platforms
  9367. * where there's just one encoder per digital port.
  9368. */
  9369. drm_for_each_connector(connector, dev) {
  9370. struct drm_connector_state *connector_state;
  9371. struct intel_encoder *encoder;
  9372. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  9373. if (!connector_state)
  9374. connector_state = connector->state;
  9375. if (!connector_state->best_encoder)
  9376. continue;
  9377. encoder = to_intel_encoder(connector_state->best_encoder);
  9378. WARN_ON(!connector_state->crtc);
  9379. switch (encoder->type) {
  9380. unsigned int port_mask;
  9381. case INTEL_OUTPUT_UNKNOWN:
  9382. if (WARN_ON(!HAS_DDI(to_i915(dev))))
  9383. break;
  9384. case INTEL_OUTPUT_DP:
  9385. case INTEL_OUTPUT_HDMI:
  9386. case INTEL_OUTPUT_EDP:
  9387. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  9388. /* the same port mustn't appear more than once */
  9389. if (used_ports & port_mask)
  9390. return false;
  9391. used_ports |= port_mask;
  9392. break;
  9393. case INTEL_OUTPUT_DP_MST:
  9394. used_mst_ports |=
  9395. 1 << enc_to_mst(&encoder->base)->primary->port;
  9396. break;
  9397. default:
  9398. break;
  9399. }
  9400. }
  9401. /* can't mix MST and SST/HDMI on the same port */
  9402. if (used_ports & used_mst_ports)
  9403. return false;
  9404. return true;
  9405. }
  9406. static void
  9407. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  9408. {
  9409. struct drm_crtc_state tmp_state;
  9410. struct intel_crtc_scaler_state scaler_state;
  9411. struct intel_dpll_hw_state dpll_hw_state;
  9412. struct intel_shared_dpll *shared_dpll;
  9413. bool force_thru;
  9414. /* FIXME: before the switch to atomic started, a new pipe_config was
  9415. * kzalloc'd. Code that depends on any field being zero should be
  9416. * fixed, so that the crtc_state can be safely duplicated. For now,
  9417. * only fields that are know to not cause problems are preserved. */
  9418. tmp_state = crtc_state->base;
  9419. scaler_state = crtc_state->scaler_state;
  9420. shared_dpll = crtc_state->shared_dpll;
  9421. dpll_hw_state = crtc_state->dpll_hw_state;
  9422. force_thru = crtc_state->pch_pfit.force_thru;
  9423. memset(crtc_state, 0, sizeof *crtc_state);
  9424. crtc_state->base = tmp_state;
  9425. crtc_state->scaler_state = scaler_state;
  9426. crtc_state->shared_dpll = shared_dpll;
  9427. crtc_state->dpll_hw_state = dpll_hw_state;
  9428. crtc_state->pch_pfit.force_thru = force_thru;
  9429. }
  9430. static int
  9431. intel_modeset_pipe_config(struct drm_crtc *crtc,
  9432. struct intel_crtc_state *pipe_config)
  9433. {
  9434. struct drm_atomic_state *state = pipe_config->base.state;
  9435. struct intel_encoder *encoder;
  9436. struct drm_connector *connector;
  9437. struct drm_connector_state *connector_state;
  9438. int base_bpp, ret = -EINVAL;
  9439. int i;
  9440. bool retry = true;
  9441. clear_intel_crtc_state(pipe_config);
  9442. pipe_config->cpu_transcoder =
  9443. (enum transcoder) to_intel_crtc(crtc)->pipe;
  9444. /*
  9445. * Sanitize sync polarity flags based on requested ones. If neither
  9446. * positive or negative polarity is requested, treat this as meaning
  9447. * negative polarity.
  9448. */
  9449. if (!(pipe_config->base.adjusted_mode.flags &
  9450. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  9451. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  9452. if (!(pipe_config->base.adjusted_mode.flags &
  9453. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  9454. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  9455. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  9456. pipe_config);
  9457. if (base_bpp < 0)
  9458. goto fail;
  9459. /*
  9460. * Determine the real pipe dimensions. Note that stereo modes can
  9461. * increase the actual pipe size due to the frame doubling and
  9462. * insertion of additional space for blanks between the frame. This
  9463. * is stored in the crtc timings. We use the requested mode to do this
  9464. * computation to clearly distinguish it from the adjusted mode, which
  9465. * can be changed by the connectors in the below retry loop.
  9466. */
  9467. drm_mode_get_hv_timing(&pipe_config->base.mode,
  9468. &pipe_config->pipe_src_w,
  9469. &pipe_config->pipe_src_h);
  9470. for_each_connector_in_state(state, connector, connector_state, i) {
  9471. if (connector_state->crtc != crtc)
  9472. continue;
  9473. encoder = to_intel_encoder(connector_state->best_encoder);
  9474. if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
  9475. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  9476. goto fail;
  9477. }
  9478. /*
  9479. * Determine output_types before calling the .compute_config()
  9480. * hooks so that the hooks can use this information safely.
  9481. */
  9482. pipe_config->output_types |= 1 << encoder->type;
  9483. }
  9484. encoder_retry:
  9485. /* Ensure the port clock defaults are reset when retrying. */
  9486. pipe_config->port_clock = 0;
  9487. pipe_config->pixel_multiplier = 1;
  9488. /* Fill in default crtc timings, allow encoders to overwrite them. */
  9489. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  9490. CRTC_STEREO_DOUBLE);
  9491. /* Pass our mode to the connectors and the CRTC to give them a chance to
  9492. * adjust it according to limitations or connector properties, and also
  9493. * a chance to reject the mode entirely.
  9494. */
  9495. for_each_connector_in_state(state, connector, connector_state, i) {
  9496. if (connector_state->crtc != crtc)
  9497. continue;
  9498. encoder = to_intel_encoder(connector_state->best_encoder);
  9499. if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
  9500. DRM_DEBUG_KMS("Encoder config failure\n");
  9501. goto fail;
  9502. }
  9503. }
  9504. /* Set default port clock if not overwritten by the encoder. Needs to be
  9505. * done afterwards in case the encoder adjusts the mode. */
  9506. if (!pipe_config->port_clock)
  9507. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  9508. * pipe_config->pixel_multiplier;
  9509. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  9510. if (ret < 0) {
  9511. DRM_DEBUG_KMS("CRTC fixup failed\n");
  9512. goto fail;
  9513. }
  9514. if (ret == RETRY) {
  9515. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  9516. ret = -EINVAL;
  9517. goto fail;
  9518. }
  9519. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  9520. retry = false;
  9521. goto encoder_retry;
  9522. }
  9523. /* Dithering seems to not pass-through bits correctly when it should, so
  9524. * only enable it on 6bpc panels and when its not a compliance
  9525. * test requesting 6bpc video pattern.
  9526. */
  9527. pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
  9528. !pipe_config->dither_force_disable;
  9529. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  9530. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  9531. fail:
  9532. return ret;
  9533. }
  9534. static void
  9535. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  9536. {
  9537. struct drm_crtc *crtc;
  9538. struct drm_crtc_state *crtc_state;
  9539. int i;
  9540. /* Double check state. */
  9541. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  9542. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  9543. /* Update hwmode for vblank functions */
  9544. if (crtc->state->active)
  9545. crtc->hwmode = crtc->state->adjusted_mode;
  9546. else
  9547. crtc->hwmode.crtc_clock = 0;
  9548. /*
  9549. * Update legacy state to satisfy fbc code. This can
  9550. * be removed when fbc uses the atomic state.
  9551. */
  9552. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  9553. struct drm_plane_state *plane_state = crtc->primary->state;
  9554. crtc->primary->fb = plane_state->fb;
  9555. crtc->x = plane_state->src_x >> 16;
  9556. crtc->y = plane_state->src_y >> 16;
  9557. }
  9558. }
  9559. }
  9560. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  9561. {
  9562. int diff;
  9563. if (clock1 == clock2)
  9564. return true;
  9565. if (!clock1 || !clock2)
  9566. return false;
  9567. diff = abs(clock1 - clock2);
  9568. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  9569. return true;
  9570. return false;
  9571. }
  9572. static bool
  9573. intel_compare_m_n(unsigned int m, unsigned int n,
  9574. unsigned int m2, unsigned int n2,
  9575. bool exact)
  9576. {
  9577. if (m == m2 && n == n2)
  9578. return true;
  9579. if (exact || !m || !n || !m2 || !n2)
  9580. return false;
  9581. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  9582. if (n > n2) {
  9583. while (n > n2) {
  9584. m2 <<= 1;
  9585. n2 <<= 1;
  9586. }
  9587. } else if (n < n2) {
  9588. while (n < n2) {
  9589. m <<= 1;
  9590. n <<= 1;
  9591. }
  9592. }
  9593. if (n != n2)
  9594. return false;
  9595. return intel_fuzzy_clock_check(m, m2);
  9596. }
  9597. static bool
  9598. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  9599. struct intel_link_m_n *m2_n2,
  9600. bool adjust)
  9601. {
  9602. if (m_n->tu == m2_n2->tu &&
  9603. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  9604. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  9605. intel_compare_m_n(m_n->link_m, m_n->link_n,
  9606. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  9607. if (adjust)
  9608. *m2_n2 = *m_n;
  9609. return true;
  9610. }
  9611. return false;
  9612. }
  9613. static void __printf(3, 4)
  9614. pipe_config_err(bool adjust, const char *name, const char *format, ...)
  9615. {
  9616. char *level;
  9617. unsigned int category;
  9618. struct va_format vaf;
  9619. va_list args;
  9620. if (adjust) {
  9621. level = KERN_DEBUG;
  9622. category = DRM_UT_KMS;
  9623. } else {
  9624. level = KERN_ERR;
  9625. category = DRM_UT_NONE;
  9626. }
  9627. va_start(args, format);
  9628. vaf.fmt = format;
  9629. vaf.va = &args;
  9630. drm_printk(level, category, "mismatch in %s %pV", name, &vaf);
  9631. va_end(args);
  9632. }
  9633. static bool
  9634. intel_pipe_config_compare(struct drm_i915_private *dev_priv,
  9635. struct intel_crtc_state *current_config,
  9636. struct intel_crtc_state *pipe_config,
  9637. bool adjust)
  9638. {
  9639. bool ret = true;
  9640. #define PIPE_CONF_CHECK_X(name) \
  9641. if (current_config->name != pipe_config->name) { \
  9642. pipe_config_err(adjust, __stringify(name), \
  9643. "(expected 0x%08x, found 0x%08x)\n", \
  9644. current_config->name, \
  9645. pipe_config->name); \
  9646. ret = false; \
  9647. }
  9648. #define PIPE_CONF_CHECK_I(name) \
  9649. if (current_config->name != pipe_config->name) { \
  9650. pipe_config_err(adjust, __stringify(name), \
  9651. "(expected %i, found %i)\n", \
  9652. current_config->name, \
  9653. pipe_config->name); \
  9654. ret = false; \
  9655. }
  9656. #define PIPE_CONF_CHECK_P(name) \
  9657. if (current_config->name != pipe_config->name) { \
  9658. pipe_config_err(adjust, __stringify(name), \
  9659. "(expected %p, found %p)\n", \
  9660. current_config->name, \
  9661. pipe_config->name); \
  9662. ret = false; \
  9663. }
  9664. #define PIPE_CONF_CHECK_M_N(name) \
  9665. if (!intel_compare_link_m_n(&current_config->name, \
  9666. &pipe_config->name,\
  9667. adjust)) { \
  9668. pipe_config_err(adjust, __stringify(name), \
  9669. "(expected tu %i gmch %i/%i link %i/%i, " \
  9670. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9671. current_config->name.tu, \
  9672. current_config->name.gmch_m, \
  9673. current_config->name.gmch_n, \
  9674. current_config->name.link_m, \
  9675. current_config->name.link_n, \
  9676. pipe_config->name.tu, \
  9677. pipe_config->name.gmch_m, \
  9678. pipe_config->name.gmch_n, \
  9679. pipe_config->name.link_m, \
  9680. pipe_config->name.link_n); \
  9681. ret = false; \
  9682. }
  9683. /* This is required for BDW+ where there is only one set of registers for
  9684. * switching between high and low RR.
  9685. * This macro can be used whenever a comparison has to be made between one
  9686. * hw state and multiple sw state variables.
  9687. */
  9688. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  9689. if (!intel_compare_link_m_n(&current_config->name, \
  9690. &pipe_config->name, adjust) && \
  9691. !intel_compare_link_m_n(&current_config->alt_name, \
  9692. &pipe_config->name, adjust)) { \
  9693. pipe_config_err(adjust, __stringify(name), \
  9694. "(expected tu %i gmch %i/%i link %i/%i, " \
  9695. "or tu %i gmch %i/%i link %i/%i, " \
  9696. "found tu %i, gmch %i/%i link %i/%i)\n", \
  9697. current_config->name.tu, \
  9698. current_config->name.gmch_m, \
  9699. current_config->name.gmch_n, \
  9700. current_config->name.link_m, \
  9701. current_config->name.link_n, \
  9702. current_config->alt_name.tu, \
  9703. current_config->alt_name.gmch_m, \
  9704. current_config->alt_name.gmch_n, \
  9705. current_config->alt_name.link_m, \
  9706. current_config->alt_name.link_n, \
  9707. pipe_config->name.tu, \
  9708. pipe_config->name.gmch_m, \
  9709. pipe_config->name.gmch_n, \
  9710. pipe_config->name.link_m, \
  9711. pipe_config->name.link_n); \
  9712. ret = false; \
  9713. }
  9714. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  9715. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  9716. pipe_config_err(adjust, __stringify(name), \
  9717. "(%x) (expected %i, found %i)\n", \
  9718. (mask), \
  9719. current_config->name & (mask), \
  9720. pipe_config->name & (mask)); \
  9721. ret = false; \
  9722. }
  9723. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  9724. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  9725. pipe_config_err(adjust, __stringify(name), \
  9726. "(expected %i, found %i)\n", \
  9727. current_config->name, \
  9728. pipe_config->name); \
  9729. ret = false; \
  9730. }
  9731. #define PIPE_CONF_QUIRK(quirk) \
  9732. ((current_config->quirks | pipe_config->quirks) & (quirk))
  9733. PIPE_CONF_CHECK_I(cpu_transcoder);
  9734. PIPE_CONF_CHECK_I(has_pch_encoder);
  9735. PIPE_CONF_CHECK_I(fdi_lanes);
  9736. PIPE_CONF_CHECK_M_N(fdi_m_n);
  9737. PIPE_CONF_CHECK_I(lane_count);
  9738. PIPE_CONF_CHECK_X(lane_lat_optim_mask);
  9739. if (INTEL_GEN(dev_priv) < 8) {
  9740. PIPE_CONF_CHECK_M_N(dp_m_n);
  9741. if (current_config->has_drrs)
  9742. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  9743. } else
  9744. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  9745. PIPE_CONF_CHECK_X(output_types);
  9746. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  9747. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  9748. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  9749. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  9750. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  9751. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  9752. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  9753. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  9754. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  9755. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  9756. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  9757. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  9758. PIPE_CONF_CHECK_I(pixel_multiplier);
  9759. PIPE_CONF_CHECK_I(has_hdmi_sink);
  9760. if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
  9761. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  9762. PIPE_CONF_CHECK_I(limited_color_range);
  9763. PIPE_CONF_CHECK_I(has_infoframe);
  9764. PIPE_CONF_CHECK_I(has_audio);
  9765. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9766. DRM_MODE_FLAG_INTERLACE);
  9767. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  9768. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9769. DRM_MODE_FLAG_PHSYNC);
  9770. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9771. DRM_MODE_FLAG_NHSYNC);
  9772. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9773. DRM_MODE_FLAG_PVSYNC);
  9774. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  9775. DRM_MODE_FLAG_NVSYNC);
  9776. }
  9777. PIPE_CONF_CHECK_X(gmch_pfit.control);
  9778. /* pfit ratios are autocomputed by the hw on gen4+ */
  9779. if (INTEL_GEN(dev_priv) < 4)
  9780. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  9781. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  9782. if (!adjust) {
  9783. PIPE_CONF_CHECK_I(pipe_src_w);
  9784. PIPE_CONF_CHECK_I(pipe_src_h);
  9785. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  9786. if (current_config->pch_pfit.enabled) {
  9787. PIPE_CONF_CHECK_X(pch_pfit.pos);
  9788. PIPE_CONF_CHECK_X(pch_pfit.size);
  9789. }
  9790. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  9791. PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
  9792. }
  9793. /* BDW+ don't expose a synchronous way to read the state */
  9794. if (IS_HASWELL(dev_priv))
  9795. PIPE_CONF_CHECK_I(ips_enabled);
  9796. PIPE_CONF_CHECK_I(double_wide);
  9797. PIPE_CONF_CHECK_P(shared_dpll);
  9798. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  9799. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  9800. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  9801. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  9802. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  9803. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  9804. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  9805. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  9806. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  9807. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  9808. PIPE_CONF_CHECK_X(dsi_pll.div);
  9809. if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
  9810. PIPE_CONF_CHECK_I(pipe_bpp);
  9811. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  9812. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  9813. #undef PIPE_CONF_CHECK_X
  9814. #undef PIPE_CONF_CHECK_I
  9815. #undef PIPE_CONF_CHECK_P
  9816. #undef PIPE_CONF_CHECK_FLAGS
  9817. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  9818. #undef PIPE_CONF_QUIRK
  9819. return ret;
  9820. }
  9821. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  9822. const struct intel_crtc_state *pipe_config)
  9823. {
  9824. if (pipe_config->has_pch_encoder) {
  9825. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9826. &pipe_config->fdi_m_n);
  9827. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  9828. /*
  9829. * FDI already provided one idea for the dotclock.
  9830. * Yell if the encoder disagrees.
  9831. */
  9832. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  9833. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  9834. fdi_dotclock, dotclock);
  9835. }
  9836. }
  9837. static void verify_wm_state(struct drm_crtc *crtc,
  9838. struct drm_crtc_state *new_state)
  9839. {
  9840. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  9841. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  9842. struct skl_pipe_wm hw_wm, *sw_wm;
  9843. struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
  9844. struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
  9845. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9846. const enum pipe pipe = intel_crtc->pipe;
  9847. int plane, level, max_level = ilk_wm_max_level(dev_priv);
  9848. if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
  9849. return;
  9850. skl_pipe_wm_get_hw_state(crtc, &hw_wm);
  9851. sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
  9852. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  9853. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  9854. /* planes */
  9855. for_each_universal_plane(dev_priv, pipe, plane) {
  9856. hw_plane_wm = &hw_wm.planes[plane];
  9857. sw_plane_wm = &sw_wm->planes[plane];
  9858. /* Watermarks */
  9859. for (level = 0; level <= max_level; level++) {
  9860. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9861. &sw_plane_wm->wm[level]))
  9862. continue;
  9863. 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",
  9864. pipe_name(pipe), plane + 1, level,
  9865. sw_plane_wm->wm[level].plane_en,
  9866. sw_plane_wm->wm[level].plane_res_b,
  9867. sw_plane_wm->wm[level].plane_res_l,
  9868. hw_plane_wm->wm[level].plane_en,
  9869. hw_plane_wm->wm[level].plane_res_b,
  9870. hw_plane_wm->wm[level].plane_res_l);
  9871. }
  9872. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9873. &sw_plane_wm->trans_wm)) {
  9874. 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",
  9875. pipe_name(pipe), plane + 1,
  9876. sw_plane_wm->trans_wm.plane_en,
  9877. sw_plane_wm->trans_wm.plane_res_b,
  9878. sw_plane_wm->trans_wm.plane_res_l,
  9879. hw_plane_wm->trans_wm.plane_en,
  9880. hw_plane_wm->trans_wm.plane_res_b,
  9881. hw_plane_wm->trans_wm.plane_res_l);
  9882. }
  9883. /* DDB */
  9884. hw_ddb_entry = &hw_ddb.plane[pipe][plane];
  9885. sw_ddb_entry = &sw_ddb->plane[pipe][plane];
  9886. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9887. DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
  9888. pipe_name(pipe), plane + 1,
  9889. sw_ddb_entry->start, sw_ddb_entry->end,
  9890. hw_ddb_entry->start, hw_ddb_entry->end);
  9891. }
  9892. }
  9893. /*
  9894. * cursor
  9895. * If the cursor plane isn't active, we may not have updated it's ddb
  9896. * allocation. In that case since the ddb allocation will be updated
  9897. * once the plane becomes visible, we can skip this check
  9898. */
  9899. if (intel_crtc->cursor_addr) {
  9900. hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
  9901. sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
  9902. /* Watermarks */
  9903. for (level = 0; level <= max_level; level++) {
  9904. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  9905. &sw_plane_wm->wm[level]))
  9906. continue;
  9907. 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",
  9908. pipe_name(pipe), level,
  9909. sw_plane_wm->wm[level].plane_en,
  9910. sw_plane_wm->wm[level].plane_res_b,
  9911. sw_plane_wm->wm[level].plane_res_l,
  9912. hw_plane_wm->wm[level].plane_en,
  9913. hw_plane_wm->wm[level].plane_res_b,
  9914. hw_plane_wm->wm[level].plane_res_l);
  9915. }
  9916. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  9917. &sw_plane_wm->trans_wm)) {
  9918. DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  9919. pipe_name(pipe),
  9920. sw_plane_wm->trans_wm.plane_en,
  9921. sw_plane_wm->trans_wm.plane_res_b,
  9922. sw_plane_wm->trans_wm.plane_res_l,
  9923. hw_plane_wm->trans_wm.plane_en,
  9924. hw_plane_wm->trans_wm.plane_res_b,
  9925. hw_plane_wm->trans_wm.plane_res_l);
  9926. }
  9927. /* DDB */
  9928. hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  9929. sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  9930. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  9931. DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
  9932. pipe_name(pipe),
  9933. sw_ddb_entry->start, sw_ddb_entry->end,
  9934. hw_ddb_entry->start, hw_ddb_entry->end);
  9935. }
  9936. }
  9937. }
  9938. static void
  9939. verify_connector_state(struct drm_device *dev,
  9940. struct drm_atomic_state *state,
  9941. struct drm_crtc *crtc)
  9942. {
  9943. struct drm_connector *connector;
  9944. struct drm_connector_state *old_conn_state;
  9945. int i;
  9946. for_each_connector_in_state(state, connector, old_conn_state, i) {
  9947. struct drm_encoder *encoder = connector->encoder;
  9948. struct drm_connector_state *state = connector->state;
  9949. if (state->crtc != crtc)
  9950. continue;
  9951. intel_connector_verify_state(to_intel_connector(connector));
  9952. I915_STATE_WARN(state->best_encoder != encoder,
  9953. "connector's atomic encoder doesn't match legacy encoder\n");
  9954. }
  9955. }
  9956. static void
  9957. verify_encoder_state(struct drm_device *dev)
  9958. {
  9959. struct intel_encoder *encoder;
  9960. struct intel_connector *connector;
  9961. for_each_intel_encoder(dev, encoder) {
  9962. bool enabled = false;
  9963. enum pipe pipe;
  9964. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  9965. encoder->base.base.id,
  9966. encoder->base.name);
  9967. for_each_intel_connector(dev, connector) {
  9968. if (connector->base.state->best_encoder != &encoder->base)
  9969. continue;
  9970. enabled = true;
  9971. I915_STATE_WARN(connector->base.state->crtc !=
  9972. encoder->base.crtc,
  9973. "connector's crtc doesn't match encoder crtc\n");
  9974. }
  9975. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  9976. "encoder's enabled state mismatch "
  9977. "(expected %i, found %i)\n",
  9978. !!encoder->base.crtc, enabled);
  9979. if (!encoder->base.crtc) {
  9980. bool active;
  9981. active = encoder->get_hw_state(encoder, &pipe);
  9982. I915_STATE_WARN(active,
  9983. "encoder detached but still enabled on pipe %c.\n",
  9984. pipe_name(pipe));
  9985. }
  9986. }
  9987. }
  9988. static void
  9989. verify_crtc_state(struct drm_crtc *crtc,
  9990. struct drm_crtc_state *old_crtc_state,
  9991. struct drm_crtc_state *new_crtc_state)
  9992. {
  9993. struct drm_device *dev = crtc->dev;
  9994. struct drm_i915_private *dev_priv = to_i915(dev);
  9995. struct intel_encoder *encoder;
  9996. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9997. struct intel_crtc_state *pipe_config, *sw_config;
  9998. struct drm_atomic_state *old_state;
  9999. bool active;
  10000. old_state = old_crtc_state->state;
  10001. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  10002. pipe_config = to_intel_crtc_state(old_crtc_state);
  10003. memset(pipe_config, 0, sizeof(*pipe_config));
  10004. pipe_config->base.crtc = crtc;
  10005. pipe_config->base.state = old_state;
  10006. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  10007. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  10008. /* hw state is inconsistent with the pipe quirk */
  10009. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  10010. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  10011. active = new_crtc_state->active;
  10012. I915_STATE_WARN(new_crtc_state->active != active,
  10013. "crtc active state doesn't match with hw state "
  10014. "(expected %i, found %i)\n", new_crtc_state->active, active);
  10015. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  10016. "transitional active state does not match atomic hw state "
  10017. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  10018. for_each_encoder_on_crtc(dev, crtc, encoder) {
  10019. enum pipe pipe;
  10020. active = encoder->get_hw_state(encoder, &pipe);
  10021. I915_STATE_WARN(active != new_crtc_state->active,
  10022. "[ENCODER:%i] active %i with crtc active %i\n",
  10023. encoder->base.base.id, active, new_crtc_state->active);
  10024. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  10025. "Encoder connected to wrong pipe %c\n",
  10026. pipe_name(pipe));
  10027. if (active) {
  10028. pipe_config->output_types |= 1 << encoder->type;
  10029. encoder->get_config(encoder, pipe_config);
  10030. }
  10031. }
  10032. intel_crtc_compute_pixel_rate(pipe_config);
  10033. if (!new_crtc_state->active)
  10034. return;
  10035. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  10036. sw_config = to_intel_crtc_state(crtc->state);
  10037. if (!intel_pipe_config_compare(dev_priv, sw_config,
  10038. pipe_config, false)) {
  10039. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  10040. intel_dump_pipe_config(intel_crtc, pipe_config,
  10041. "[hw state]");
  10042. intel_dump_pipe_config(intel_crtc, sw_config,
  10043. "[sw state]");
  10044. }
  10045. }
  10046. static void
  10047. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  10048. struct intel_shared_dpll *pll,
  10049. struct drm_crtc *crtc,
  10050. struct drm_crtc_state *new_state)
  10051. {
  10052. struct intel_dpll_hw_state dpll_hw_state;
  10053. unsigned crtc_mask;
  10054. bool active;
  10055. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  10056. DRM_DEBUG_KMS("%s\n", pll->name);
  10057. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  10058. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  10059. I915_STATE_WARN(!pll->on && pll->active_mask,
  10060. "pll in active use but not on in sw tracking\n");
  10061. I915_STATE_WARN(pll->on && !pll->active_mask,
  10062. "pll is on but not used by any active crtc\n");
  10063. I915_STATE_WARN(pll->on != active,
  10064. "pll on state mismatch (expected %i, found %i)\n",
  10065. pll->on, active);
  10066. }
  10067. if (!crtc) {
  10068. I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
  10069. "more active pll users than references: %x vs %x\n",
  10070. pll->active_mask, pll->state.crtc_mask);
  10071. return;
  10072. }
  10073. crtc_mask = 1 << drm_crtc_index(crtc);
  10074. if (new_state->active)
  10075. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  10076. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  10077. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  10078. else
  10079. I915_STATE_WARN(pll->active_mask & crtc_mask,
  10080. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  10081. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  10082. I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
  10083. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  10084. crtc_mask, pll->state.crtc_mask);
  10085. I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
  10086. &dpll_hw_state,
  10087. sizeof(dpll_hw_state)),
  10088. "pll hw state mismatch\n");
  10089. }
  10090. static void
  10091. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  10092. struct drm_crtc_state *old_crtc_state,
  10093. struct drm_crtc_state *new_crtc_state)
  10094. {
  10095. struct drm_i915_private *dev_priv = to_i915(dev);
  10096. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  10097. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  10098. if (new_state->shared_dpll)
  10099. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  10100. if (old_state->shared_dpll &&
  10101. old_state->shared_dpll != new_state->shared_dpll) {
  10102. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  10103. struct intel_shared_dpll *pll = old_state->shared_dpll;
  10104. I915_STATE_WARN(pll->active_mask & crtc_mask,
  10105. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  10106. pipe_name(drm_crtc_index(crtc)));
  10107. I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
  10108. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  10109. pipe_name(drm_crtc_index(crtc)));
  10110. }
  10111. }
  10112. static void
  10113. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  10114. struct drm_atomic_state *state,
  10115. struct drm_crtc_state *old_state,
  10116. struct drm_crtc_state *new_state)
  10117. {
  10118. if (!needs_modeset(new_state) &&
  10119. !to_intel_crtc_state(new_state)->update_pipe)
  10120. return;
  10121. verify_wm_state(crtc, new_state);
  10122. verify_connector_state(crtc->dev, state, crtc);
  10123. verify_crtc_state(crtc, old_state, new_state);
  10124. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  10125. }
  10126. static void
  10127. verify_disabled_dpll_state(struct drm_device *dev)
  10128. {
  10129. struct drm_i915_private *dev_priv = to_i915(dev);
  10130. int i;
  10131. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  10132. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  10133. }
  10134. static void
  10135. intel_modeset_verify_disabled(struct drm_device *dev,
  10136. struct drm_atomic_state *state)
  10137. {
  10138. verify_encoder_state(dev);
  10139. verify_connector_state(dev, state, NULL);
  10140. verify_disabled_dpll_state(dev);
  10141. }
  10142. static void update_scanline_offset(struct intel_crtc *crtc)
  10143. {
  10144. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  10145. /*
  10146. * The scanline counter increments at the leading edge of hsync.
  10147. *
  10148. * On most platforms it starts counting from vtotal-1 on the
  10149. * first active line. That means the scanline counter value is
  10150. * always one less than what we would expect. Ie. just after
  10151. * start of vblank, which also occurs at start of hsync (on the
  10152. * last active line), the scanline counter will read vblank_start-1.
  10153. *
  10154. * On gen2 the scanline counter starts counting from 1 instead
  10155. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  10156. * to keep the value positive), instead of adding one.
  10157. *
  10158. * On HSW+ the behaviour of the scanline counter depends on the output
  10159. * type. For DP ports it behaves like most other platforms, but on HDMI
  10160. * there's an extra 1 line difference. So we need to add two instead of
  10161. * one to the value.
  10162. */
  10163. if (IS_GEN2(dev_priv)) {
  10164. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  10165. int vtotal;
  10166. vtotal = adjusted_mode->crtc_vtotal;
  10167. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  10168. vtotal /= 2;
  10169. crtc->scanline_offset = vtotal - 1;
  10170. } else if (HAS_DDI(dev_priv) &&
  10171. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
  10172. crtc->scanline_offset = 2;
  10173. } else
  10174. crtc->scanline_offset = 1;
  10175. }
  10176. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  10177. {
  10178. struct drm_device *dev = state->dev;
  10179. struct drm_i915_private *dev_priv = to_i915(dev);
  10180. struct drm_crtc *crtc;
  10181. struct drm_crtc_state *crtc_state;
  10182. int i;
  10183. if (!dev_priv->display.crtc_compute_clock)
  10184. return;
  10185. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10186. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10187. struct intel_shared_dpll *old_dpll =
  10188. to_intel_crtc_state(crtc->state)->shared_dpll;
  10189. if (!needs_modeset(crtc_state))
  10190. continue;
  10191. to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
  10192. if (!old_dpll)
  10193. continue;
  10194. intel_release_shared_dpll(old_dpll, intel_crtc, state);
  10195. }
  10196. }
  10197. /*
  10198. * This implements the workaround described in the "notes" section of the mode
  10199. * set sequence documentation. When going from no pipes or single pipe to
  10200. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  10201. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  10202. */
  10203. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  10204. {
  10205. struct drm_crtc_state *crtc_state;
  10206. struct intel_crtc *intel_crtc;
  10207. struct drm_crtc *crtc;
  10208. struct intel_crtc_state *first_crtc_state = NULL;
  10209. struct intel_crtc_state *other_crtc_state = NULL;
  10210. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  10211. int i;
  10212. /* look at all crtc's that are going to be enabled in during modeset */
  10213. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10214. intel_crtc = to_intel_crtc(crtc);
  10215. if (!crtc_state->active || !needs_modeset(crtc_state))
  10216. continue;
  10217. if (first_crtc_state) {
  10218. other_crtc_state = to_intel_crtc_state(crtc_state);
  10219. break;
  10220. } else {
  10221. first_crtc_state = to_intel_crtc_state(crtc_state);
  10222. first_pipe = intel_crtc->pipe;
  10223. }
  10224. }
  10225. /* No workaround needed? */
  10226. if (!first_crtc_state)
  10227. return 0;
  10228. /* w/a possibly needed, check how many crtc's are already enabled. */
  10229. for_each_intel_crtc(state->dev, intel_crtc) {
  10230. struct intel_crtc_state *pipe_config;
  10231. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  10232. if (IS_ERR(pipe_config))
  10233. return PTR_ERR(pipe_config);
  10234. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  10235. if (!pipe_config->base.active ||
  10236. needs_modeset(&pipe_config->base))
  10237. continue;
  10238. /* 2 or more enabled crtcs means no need for w/a */
  10239. if (enabled_pipe != INVALID_PIPE)
  10240. return 0;
  10241. enabled_pipe = intel_crtc->pipe;
  10242. }
  10243. if (enabled_pipe != INVALID_PIPE)
  10244. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  10245. else if (other_crtc_state)
  10246. other_crtc_state->hsw_workaround_pipe = first_pipe;
  10247. return 0;
  10248. }
  10249. static int intel_lock_all_pipes(struct drm_atomic_state *state)
  10250. {
  10251. struct drm_crtc *crtc;
  10252. /* Add all pipes to the state */
  10253. for_each_crtc(state->dev, crtc) {
  10254. struct drm_crtc_state *crtc_state;
  10255. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10256. if (IS_ERR(crtc_state))
  10257. return PTR_ERR(crtc_state);
  10258. }
  10259. return 0;
  10260. }
  10261. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  10262. {
  10263. struct drm_crtc *crtc;
  10264. /*
  10265. * Add all pipes to the state, and force
  10266. * a modeset on all the active ones.
  10267. */
  10268. for_each_crtc(state->dev, crtc) {
  10269. struct drm_crtc_state *crtc_state;
  10270. int ret;
  10271. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10272. if (IS_ERR(crtc_state))
  10273. return PTR_ERR(crtc_state);
  10274. if (!crtc_state->active || needs_modeset(crtc_state))
  10275. continue;
  10276. crtc_state->mode_changed = true;
  10277. ret = drm_atomic_add_affected_connectors(state, crtc);
  10278. if (ret)
  10279. return ret;
  10280. ret = drm_atomic_add_affected_planes(state, crtc);
  10281. if (ret)
  10282. return ret;
  10283. }
  10284. return 0;
  10285. }
  10286. static int intel_modeset_checks(struct drm_atomic_state *state)
  10287. {
  10288. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10289. struct drm_i915_private *dev_priv = to_i915(state->dev);
  10290. struct drm_crtc *crtc;
  10291. struct drm_crtc_state *crtc_state;
  10292. int ret = 0, i;
  10293. if (!check_digital_port_conflicts(state)) {
  10294. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  10295. return -EINVAL;
  10296. }
  10297. intel_state->modeset = true;
  10298. intel_state->active_crtcs = dev_priv->active_crtcs;
  10299. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  10300. intel_state->cdclk.actual = dev_priv->cdclk.actual;
  10301. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10302. if (crtc_state->active)
  10303. intel_state->active_crtcs |= 1 << i;
  10304. else
  10305. intel_state->active_crtcs &= ~(1 << i);
  10306. if (crtc_state->active != crtc->state->active)
  10307. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  10308. }
  10309. /*
  10310. * See if the config requires any additional preparation, e.g.
  10311. * to adjust global state with pipes off. We need to do this
  10312. * here so we can get the modeset_pipe updated config for the new
  10313. * mode set on this crtc. For other crtcs we need to use the
  10314. * adjusted_mode bits in the crtc directly.
  10315. */
  10316. if (dev_priv->display.modeset_calc_cdclk) {
  10317. ret = dev_priv->display.modeset_calc_cdclk(state);
  10318. if (ret < 0)
  10319. return ret;
  10320. /*
  10321. * Writes to dev_priv->cdclk.logical must protected by
  10322. * holding all the crtc locks, even if we don't end up
  10323. * touching the hardware
  10324. */
  10325. if (!intel_cdclk_state_compare(&dev_priv->cdclk.logical,
  10326. &intel_state->cdclk.logical)) {
  10327. ret = intel_lock_all_pipes(state);
  10328. if (ret < 0)
  10329. return ret;
  10330. }
  10331. /* All pipes must be switched off while we change the cdclk. */
  10332. if (!intel_cdclk_state_compare(&dev_priv->cdclk.actual,
  10333. &intel_state->cdclk.actual)) {
  10334. ret = intel_modeset_all_pipes(state);
  10335. if (ret < 0)
  10336. return ret;
  10337. }
  10338. DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
  10339. intel_state->cdclk.logical.cdclk,
  10340. intel_state->cdclk.actual.cdclk);
  10341. } else {
  10342. to_intel_atomic_state(state)->cdclk.logical = dev_priv->cdclk.logical;
  10343. }
  10344. intel_modeset_clear_plls(state);
  10345. if (IS_HASWELL(dev_priv))
  10346. return haswell_mode_set_planes_workaround(state);
  10347. return 0;
  10348. }
  10349. /*
  10350. * Handle calculation of various watermark data at the end of the atomic check
  10351. * phase. The code here should be run after the per-crtc and per-plane 'check'
  10352. * handlers to ensure that all derived state has been updated.
  10353. */
  10354. static int calc_watermark_data(struct drm_atomic_state *state)
  10355. {
  10356. struct drm_device *dev = state->dev;
  10357. struct drm_i915_private *dev_priv = to_i915(dev);
  10358. /* Is there platform-specific watermark information to calculate? */
  10359. if (dev_priv->display.compute_global_watermarks)
  10360. return dev_priv->display.compute_global_watermarks(state);
  10361. return 0;
  10362. }
  10363. /**
  10364. * intel_atomic_check - validate state object
  10365. * @dev: drm device
  10366. * @state: state to validate
  10367. */
  10368. static int intel_atomic_check(struct drm_device *dev,
  10369. struct drm_atomic_state *state)
  10370. {
  10371. struct drm_i915_private *dev_priv = to_i915(dev);
  10372. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10373. struct drm_crtc *crtc;
  10374. struct drm_crtc_state *crtc_state;
  10375. int ret, i;
  10376. bool any_ms = false;
  10377. ret = drm_atomic_helper_check_modeset(dev, state);
  10378. if (ret)
  10379. return ret;
  10380. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10381. struct intel_crtc_state *pipe_config =
  10382. to_intel_crtc_state(crtc_state);
  10383. /* Catch I915_MODE_FLAG_INHERITED */
  10384. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  10385. crtc_state->mode_changed = true;
  10386. if (!needs_modeset(crtc_state))
  10387. continue;
  10388. if (!crtc_state->enable) {
  10389. any_ms = true;
  10390. continue;
  10391. }
  10392. /* FIXME: For only active_changed we shouldn't need to do any
  10393. * state recomputation at all. */
  10394. ret = drm_atomic_add_affected_connectors(state, crtc);
  10395. if (ret)
  10396. return ret;
  10397. ret = intel_modeset_pipe_config(crtc, pipe_config);
  10398. if (ret) {
  10399. intel_dump_pipe_config(to_intel_crtc(crtc),
  10400. pipe_config, "[failed]");
  10401. return ret;
  10402. }
  10403. if (i915.fastboot &&
  10404. intel_pipe_config_compare(dev_priv,
  10405. to_intel_crtc_state(crtc->state),
  10406. pipe_config, true)) {
  10407. crtc_state->mode_changed = false;
  10408. to_intel_crtc_state(crtc_state)->update_pipe = true;
  10409. }
  10410. if (needs_modeset(crtc_state))
  10411. any_ms = true;
  10412. ret = drm_atomic_add_affected_planes(state, crtc);
  10413. if (ret)
  10414. return ret;
  10415. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  10416. needs_modeset(crtc_state) ?
  10417. "[modeset]" : "[fastset]");
  10418. }
  10419. if (any_ms) {
  10420. ret = intel_modeset_checks(state);
  10421. if (ret)
  10422. return ret;
  10423. } else {
  10424. intel_state->cdclk.logical = dev_priv->cdclk.logical;
  10425. }
  10426. ret = drm_atomic_helper_check_planes(dev, state);
  10427. if (ret)
  10428. return ret;
  10429. intel_fbc_choose_crtc(dev_priv, state);
  10430. return calc_watermark_data(state);
  10431. }
  10432. static int intel_atomic_prepare_commit(struct drm_device *dev,
  10433. struct drm_atomic_state *state)
  10434. {
  10435. struct drm_i915_private *dev_priv = to_i915(dev);
  10436. struct drm_crtc_state *crtc_state;
  10437. struct drm_crtc *crtc;
  10438. int i, ret;
  10439. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10440. if (state->legacy_cursor_update)
  10441. continue;
  10442. ret = intel_crtc_wait_for_pending_flips(crtc);
  10443. if (ret)
  10444. return ret;
  10445. if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
  10446. flush_workqueue(dev_priv->wq);
  10447. }
  10448. ret = mutex_lock_interruptible(&dev->struct_mutex);
  10449. if (ret)
  10450. return ret;
  10451. ret = drm_atomic_helper_prepare_planes(dev, state);
  10452. mutex_unlock(&dev->struct_mutex);
  10453. return ret;
  10454. }
  10455. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  10456. {
  10457. struct drm_device *dev = crtc->base.dev;
  10458. if (!dev->max_vblank_count)
  10459. return drm_accurate_vblank_count(&crtc->base);
  10460. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  10461. }
  10462. static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
  10463. struct drm_i915_private *dev_priv,
  10464. unsigned crtc_mask)
  10465. {
  10466. unsigned last_vblank_count[I915_MAX_PIPES];
  10467. enum pipe pipe;
  10468. int ret;
  10469. if (!crtc_mask)
  10470. return;
  10471. for_each_pipe(dev_priv, pipe) {
  10472. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  10473. pipe);
  10474. if (!((1 << pipe) & crtc_mask))
  10475. continue;
  10476. ret = drm_crtc_vblank_get(&crtc->base);
  10477. if (WARN_ON(ret != 0)) {
  10478. crtc_mask &= ~(1 << pipe);
  10479. continue;
  10480. }
  10481. last_vblank_count[pipe] = drm_crtc_vblank_count(&crtc->base);
  10482. }
  10483. for_each_pipe(dev_priv, pipe) {
  10484. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  10485. pipe);
  10486. long lret;
  10487. if (!((1 << pipe) & crtc_mask))
  10488. continue;
  10489. lret = wait_event_timeout(dev->vblank[pipe].queue,
  10490. last_vblank_count[pipe] !=
  10491. drm_crtc_vblank_count(&crtc->base),
  10492. msecs_to_jiffies(50));
  10493. WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
  10494. drm_crtc_vblank_put(&crtc->base);
  10495. }
  10496. }
  10497. static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
  10498. {
  10499. /* fb updated, need to unpin old fb */
  10500. if (crtc_state->fb_changed)
  10501. return true;
  10502. /* wm changes, need vblank before final wm's */
  10503. if (crtc_state->update_wm_post)
  10504. return true;
  10505. /*
  10506. * cxsr is re-enabled after vblank.
  10507. * This is already handled by crtc_state->update_wm_post,
  10508. * but added for clarity.
  10509. */
  10510. if (crtc_state->disable_cxsr)
  10511. return true;
  10512. return false;
  10513. }
  10514. static void intel_update_crtc(struct drm_crtc *crtc,
  10515. struct drm_atomic_state *state,
  10516. struct drm_crtc_state *old_crtc_state,
  10517. unsigned int *crtc_vblank_mask)
  10518. {
  10519. struct drm_device *dev = crtc->dev;
  10520. struct drm_i915_private *dev_priv = to_i915(dev);
  10521. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10522. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc->state);
  10523. bool modeset = needs_modeset(crtc->state);
  10524. if (modeset) {
  10525. update_scanline_offset(intel_crtc);
  10526. dev_priv->display.crtc_enable(pipe_config, state);
  10527. } else {
  10528. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  10529. }
  10530. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10531. intel_fbc_enable(
  10532. intel_crtc, pipe_config,
  10533. to_intel_plane_state(crtc->primary->state));
  10534. }
  10535. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  10536. if (needs_vblank_wait(pipe_config))
  10537. *crtc_vblank_mask |= drm_crtc_mask(crtc);
  10538. }
  10539. static void intel_update_crtcs(struct drm_atomic_state *state,
  10540. unsigned int *crtc_vblank_mask)
  10541. {
  10542. struct drm_crtc *crtc;
  10543. struct drm_crtc_state *old_crtc_state;
  10544. int i;
  10545. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  10546. if (!crtc->state->active)
  10547. continue;
  10548. intel_update_crtc(crtc, state, old_crtc_state,
  10549. crtc_vblank_mask);
  10550. }
  10551. }
  10552. static void skl_update_crtcs(struct drm_atomic_state *state,
  10553. unsigned int *crtc_vblank_mask)
  10554. {
  10555. struct drm_i915_private *dev_priv = to_i915(state->dev);
  10556. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10557. struct drm_crtc *crtc;
  10558. struct intel_crtc *intel_crtc;
  10559. struct drm_crtc_state *old_crtc_state;
  10560. struct intel_crtc_state *cstate;
  10561. unsigned int updated = 0;
  10562. bool progress;
  10563. enum pipe pipe;
  10564. int i;
  10565. const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
  10566. for_each_crtc_in_state(state, crtc, old_crtc_state, i)
  10567. /* ignore allocations for crtc's that have been turned off. */
  10568. if (crtc->state->active)
  10569. entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
  10570. /*
  10571. * Whenever the number of active pipes changes, we need to make sure we
  10572. * update the pipes in the right order so that their ddb allocations
  10573. * never overlap with eachother inbetween CRTC updates. Otherwise we'll
  10574. * cause pipe underruns and other bad stuff.
  10575. */
  10576. do {
  10577. progress = false;
  10578. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  10579. bool vbl_wait = false;
  10580. unsigned int cmask = drm_crtc_mask(crtc);
  10581. intel_crtc = to_intel_crtc(crtc);
  10582. cstate = to_intel_crtc_state(crtc->state);
  10583. pipe = intel_crtc->pipe;
  10584. if (updated & cmask || !cstate->base.active)
  10585. continue;
  10586. if (skl_ddb_allocation_overlaps(entries, &cstate->wm.skl.ddb, i))
  10587. continue;
  10588. updated |= cmask;
  10589. entries[i] = &cstate->wm.skl.ddb;
  10590. /*
  10591. * If this is an already active pipe, it's DDB changed,
  10592. * and this isn't the last pipe that needs updating
  10593. * then we need to wait for a vblank to pass for the
  10594. * new ddb allocation to take effect.
  10595. */
  10596. if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
  10597. &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
  10598. !crtc->state->active_changed &&
  10599. intel_state->wm_results.dirty_pipes != updated)
  10600. vbl_wait = true;
  10601. intel_update_crtc(crtc, state, old_crtc_state,
  10602. crtc_vblank_mask);
  10603. if (vbl_wait)
  10604. intel_wait_for_vblank(dev_priv, pipe);
  10605. progress = true;
  10606. }
  10607. } while (progress);
  10608. }
  10609. static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
  10610. {
  10611. struct intel_atomic_state *state, *next;
  10612. struct llist_node *freed;
  10613. freed = llist_del_all(&dev_priv->atomic_helper.free_list);
  10614. llist_for_each_entry_safe(state, next, freed, freed)
  10615. drm_atomic_state_put(&state->base);
  10616. }
  10617. static void intel_atomic_helper_free_state_worker(struct work_struct *work)
  10618. {
  10619. struct drm_i915_private *dev_priv =
  10620. container_of(work, typeof(*dev_priv), atomic_helper.free_work);
  10621. intel_atomic_helper_free_state(dev_priv);
  10622. }
  10623. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  10624. {
  10625. struct drm_device *dev = state->dev;
  10626. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10627. struct drm_i915_private *dev_priv = to_i915(dev);
  10628. struct drm_crtc_state *old_crtc_state;
  10629. struct drm_crtc *crtc;
  10630. struct intel_crtc_state *intel_cstate;
  10631. bool hw_check = intel_state->modeset;
  10632. u64 put_domains[I915_MAX_PIPES] = {};
  10633. unsigned crtc_vblank_mask = 0;
  10634. int i;
  10635. drm_atomic_helper_wait_for_dependencies(state);
  10636. if (intel_state->modeset)
  10637. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  10638. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  10639. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10640. if (needs_modeset(crtc->state) ||
  10641. to_intel_crtc_state(crtc->state)->update_pipe) {
  10642. hw_check = true;
  10643. put_domains[to_intel_crtc(crtc)->pipe] =
  10644. modeset_get_crtc_power_domains(crtc,
  10645. to_intel_crtc_state(crtc->state));
  10646. }
  10647. if (!needs_modeset(crtc->state))
  10648. continue;
  10649. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  10650. if (old_crtc_state->active) {
  10651. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  10652. dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
  10653. intel_crtc->active = false;
  10654. intel_fbc_disable(intel_crtc);
  10655. intel_disable_shared_dpll(intel_crtc);
  10656. /*
  10657. * Underruns don't always raise
  10658. * interrupts, so check manually.
  10659. */
  10660. intel_check_cpu_fifo_underruns(dev_priv);
  10661. intel_check_pch_fifo_underruns(dev_priv);
  10662. if (!crtc->state->active) {
  10663. /*
  10664. * Make sure we don't call initial_watermarks
  10665. * for ILK-style watermark updates.
  10666. */
  10667. if (dev_priv->display.atomic_update_watermarks)
  10668. dev_priv->display.initial_watermarks(intel_state,
  10669. to_intel_crtc_state(crtc->state));
  10670. else
  10671. intel_update_watermarks(intel_crtc);
  10672. }
  10673. }
  10674. }
  10675. /* Only after disabling all output pipelines that will be changed can we
  10676. * update the the output configuration. */
  10677. intel_modeset_update_crtc_state(state);
  10678. if (intel_state->modeset) {
  10679. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  10680. intel_set_cdclk(dev_priv, &dev_priv->cdclk.actual);
  10681. /*
  10682. * SKL workaround: bspec recommends we disable the SAGV when we
  10683. * have more then one pipe enabled
  10684. */
  10685. if (!intel_can_enable_sagv(state))
  10686. intel_disable_sagv(dev_priv);
  10687. intel_modeset_verify_disabled(dev, state);
  10688. }
  10689. /* Complete the events for pipes that have now been disabled */
  10690. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  10691. bool modeset = needs_modeset(crtc->state);
  10692. /* Complete events for now disable pipes here. */
  10693. if (modeset && !crtc->state->active && crtc->state->event) {
  10694. spin_lock_irq(&dev->event_lock);
  10695. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  10696. spin_unlock_irq(&dev->event_lock);
  10697. crtc->state->event = NULL;
  10698. }
  10699. }
  10700. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  10701. dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
  10702. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  10703. * already, but still need the state for the delayed optimization. To
  10704. * fix this:
  10705. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  10706. * - schedule that vblank worker _before_ calling hw_done
  10707. * - at the start of commit_tail, cancel it _synchrously
  10708. * - switch over to the vblank wait helper in the core after that since
  10709. * we don't need out special handling any more.
  10710. */
  10711. if (!state->legacy_cursor_update)
  10712. intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
  10713. /*
  10714. * Now that the vblank has passed, we can go ahead and program the
  10715. * optimal watermarks on platforms that need two-step watermark
  10716. * programming.
  10717. *
  10718. * TODO: Move this (and other cleanup) to an async worker eventually.
  10719. */
  10720. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  10721. intel_cstate = to_intel_crtc_state(crtc->state);
  10722. if (dev_priv->display.optimize_watermarks)
  10723. dev_priv->display.optimize_watermarks(intel_state,
  10724. intel_cstate);
  10725. }
  10726. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  10727. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  10728. if (put_domains[i])
  10729. modeset_put_power_domains(dev_priv, put_domains[i]);
  10730. intel_modeset_verify_crtc(crtc, state, old_crtc_state, crtc->state);
  10731. }
  10732. if (intel_state->modeset && intel_can_enable_sagv(state))
  10733. intel_enable_sagv(dev_priv);
  10734. drm_atomic_helper_commit_hw_done(state);
  10735. if (intel_state->modeset)
  10736. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  10737. mutex_lock(&dev->struct_mutex);
  10738. drm_atomic_helper_cleanup_planes(dev, state);
  10739. mutex_unlock(&dev->struct_mutex);
  10740. drm_atomic_helper_commit_cleanup_done(state);
  10741. drm_atomic_state_put(state);
  10742. /* As one of the primary mmio accessors, KMS has a high likelihood
  10743. * of triggering bugs in unclaimed access. After we finish
  10744. * modesetting, see if an error has been flagged, and if so
  10745. * enable debugging for the next modeset - and hope we catch
  10746. * the culprit.
  10747. *
  10748. * XXX note that we assume display power is on at this point.
  10749. * This might hold true now but we need to add pm helper to check
  10750. * unclaimed only when the hardware is on, as atomic commits
  10751. * can happen also when the device is completely off.
  10752. */
  10753. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  10754. intel_atomic_helper_free_state(dev_priv);
  10755. }
  10756. static void intel_atomic_commit_work(struct work_struct *work)
  10757. {
  10758. struct drm_atomic_state *state =
  10759. container_of(work, struct drm_atomic_state, commit_work);
  10760. intel_atomic_commit_tail(state);
  10761. }
  10762. static int __i915_sw_fence_call
  10763. intel_atomic_commit_ready(struct i915_sw_fence *fence,
  10764. enum i915_sw_fence_notify notify)
  10765. {
  10766. struct intel_atomic_state *state =
  10767. container_of(fence, struct intel_atomic_state, commit_ready);
  10768. switch (notify) {
  10769. case FENCE_COMPLETE:
  10770. if (state->base.commit_work.func)
  10771. queue_work(system_unbound_wq, &state->base.commit_work);
  10772. break;
  10773. case FENCE_FREE:
  10774. {
  10775. struct intel_atomic_helper *helper =
  10776. &to_i915(state->base.dev)->atomic_helper;
  10777. if (llist_add(&state->freed, &helper->free_list))
  10778. schedule_work(&helper->free_work);
  10779. break;
  10780. }
  10781. }
  10782. return NOTIFY_DONE;
  10783. }
  10784. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  10785. {
  10786. struct drm_plane_state *old_plane_state;
  10787. struct drm_plane *plane;
  10788. int i;
  10789. for_each_plane_in_state(state, plane, old_plane_state, i)
  10790. i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
  10791. intel_fb_obj(plane->state->fb),
  10792. to_intel_plane(plane)->frontbuffer_bit);
  10793. }
  10794. /**
  10795. * intel_atomic_commit - commit validated state object
  10796. * @dev: DRM device
  10797. * @state: the top-level driver state object
  10798. * @nonblock: nonblocking commit
  10799. *
  10800. * This function commits a top-level state object that has been validated
  10801. * with drm_atomic_helper_check().
  10802. *
  10803. * RETURNS
  10804. * Zero for success or -errno.
  10805. */
  10806. static int intel_atomic_commit(struct drm_device *dev,
  10807. struct drm_atomic_state *state,
  10808. bool nonblock)
  10809. {
  10810. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  10811. struct drm_i915_private *dev_priv = to_i915(dev);
  10812. int ret = 0;
  10813. /*
  10814. * The intel_legacy_cursor_update() fast path takes care
  10815. * of avoiding the vblank waits for simple cursor
  10816. * movement and flips. For cursor on/off and size changes,
  10817. * we want to perform the vblank waits so that watermark
  10818. * updates happen during the correct frames. Gen9+ have
  10819. * double buffered watermarks and so shouldn't need this.
  10820. */
  10821. if (INTEL_GEN(dev_priv) < 9)
  10822. state->legacy_cursor_update = false;
  10823. ret = drm_atomic_helper_setup_commit(state, nonblock);
  10824. if (ret)
  10825. return ret;
  10826. drm_atomic_state_get(state);
  10827. i915_sw_fence_init(&intel_state->commit_ready,
  10828. intel_atomic_commit_ready);
  10829. ret = intel_atomic_prepare_commit(dev, state);
  10830. if (ret) {
  10831. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  10832. i915_sw_fence_commit(&intel_state->commit_ready);
  10833. return ret;
  10834. }
  10835. drm_atomic_helper_swap_state(state, true);
  10836. dev_priv->wm.distrust_bios_wm = false;
  10837. intel_shared_dpll_swap_state(state);
  10838. intel_atomic_track_fbs(state);
  10839. if (intel_state->modeset) {
  10840. memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
  10841. sizeof(intel_state->min_pixclk));
  10842. dev_priv->active_crtcs = intel_state->active_crtcs;
  10843. dev_priv->cdclk.logical = intel_state->cdclk.logical;
  10844. dev_priv->cdclk.actual = intel_state->cdclk.actual;
  10845. }
  10846. drm_atomic_state_get(state);
  10847. INIT_WORK(&state->commit_work,
  10848. nonblock ? intel_atomic_commit_work : NULL);
  10849. i915_sw_fence_commit(&intel_state->commit_ready);
  10850. if (!nonblock) {
  10851. i915_sw_fence_wait(&intel_state->commit_ready);
  10852. intel_atomic_commit_tail(state);
  10853. }
  10854. return 0;
  10855. }
  10856. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  10857. {
  10858. struct drm_device *dev = crtc->dev;
  10859. struct drm_atomic_state *state;
  10860. struct drm_crtc_state *crtc_state;
  10861. int ret;
  10862. state = drm_atomic_state_alloc(dev);
  10863. if (!state) {
  10864. DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
  10865. crtc->base.id, crtc->name);
  10866. return;
  10867. }
  10868. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  10869. retry:
  10870. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  10871. ret = PTR_ERR_OR_ZERO(crtc_state);
  10872. if (!ret) {
  10873. if (!crtc_state->active)
  10874. goto out;
  10875. crtc_state->mode_changed = true;
  10876. ret = drm_atomic_commit(state);
  10877. }
  10878. if (ret == -EDEADLK) {
  10879. drm_atomic_state_clear(state);
  10880. drm_modeset_backoff(state->acquire_ctx);
  10881. goto retry;
  10882. }
  10883. out:
  10884. drm_atomic_state_put(state);
  10885. }
  10886. /*
  10887. * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
  10888. * drm_atomic_helper_legacy_gamma_set() directly.
  10889. */
  10890. static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
  10891. u16 *red, u16 *green, u16 *blue,
  10892. uint32_t size)
  10893. {
  10894. struct drm_device *dev = crtc->dev;
  10895. struct drm_mode_config *config = &dev->mode_config;
  10896. struct drm_crtc_state *state;
  10897. int ret;
  10898. ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
  10899. if (ret)
  10900. return ret;
  10901. /*
  10902. * Make sure we update the legacy properties so this works when
  10903. * atomic is not enabled.
  10904. */
  10905. state = crtc->state;
  10906. drm_object_property_set_value(&crtc->base,
  10907. config->degamma_lut_property,
  10908. (state->degamma_lut) ?
  10909. state->degamma_lut->base.id : 0);
  10910. drm_object_property_set_value(&crtc->base,
  10911. config->ctm_property,
  10912. (state->ctm) ?
  10913. state->ctm->base.id : 0);
  10914. drm_object_property_set_value(&crtc->base,
  10915. config->gamma_lut_property,
  10916. (state->gamma_lut) ?
  10917. state->gamma_lut->base.id : 0);
  10918. return 0;
  10919. }
  10920. static const struct drm_crtc_funcs intel_crtc_funcs = {
  10921. .gamma_set = intel_atomic_legacy_gamma_set,
  10922. .set_config = drm_atomic_helper_set_config,
  10923. .set_property = drm_atomic_helper_crtc_set_property,
  10924. .destroy = intel_crtc_destroy,
  10925. .page_flip = drm_atomic_helper_page_flip,
  10926. .atomic_duplicate_state = intel_crtc_duplicate_state,
  10927. .atomic_destroy_state = intel_crtc_destroy_state,
  10928. .set_crc_source = intel_crtc_set_crc_source,
  10929. };
  10930. /**
  10931. * intel_prepare_plane_fb - Prepare fb for usage on plane
  10932. * @plane: drm plane to prepare for
  10933. * @fb: framebuffer to prepare for presentation
  10934. *
  10935. * Prepares a framebuffer for usage on a display plane. Generally this
  10936. * involves pinning the underlying object and updating the frontbuffer tracking
  10937. * bits. Some older platforms need special physical address handling for
  10938. * cursor planes.
  10939. *
  10940. * Must be called with struct_mutex held.
  10941. *
  10942. * Returns 0 on success, negative error code on failure.
  10943. */
  10944. int
  10945. intel_prepare_plane_fb(struct drm_plane *plane,
  10946. struct drm_plane_state *new_state)
  10947. {
  10948. struct intel_atomic_state *intel_state =
  10949. to_intel_atomic_state(new_state->state);
  10950. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  10951. struct drm_framebuffer *fb = new_state->fb;
  10952. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10953. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  10954. int ret;
  10955. if (obj) {
  10956. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  10957. INTEL_INFO(dev_priv)->cursor_needs_physical) {
  10958. const int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
  10959. ret = i915_gem_object_attach_phys(obj, align);
  10960. if (ret) {
  10961. DRM_DEBUG_KMS("failed to attach phys object\n");
  10962. return ret;
  10963. }
  10964. } else {
  10965. struct i915_vma *vma;
  10966. vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  10967. if (IS_ERR(vma)) {
  10968. DRM_DEBUG_KMS("failed to pin object\n");
  10969. return PTR_ERR(vma);
  10970. }
  10971. to_intel_plane_state(new_state)->vma = vma;
  10972. }
  10973. }
  10974. if (!obj && !old_obj)
  10975. return 0;
  10976. if (old_obj) {
  10977. struct drm_crtc_state *crtc_state =
  10978. drm_atomic_get_existing_crtc_state(new_state->state,
  10979. plane->state->crtc);
  10980. /* Big Hammer, we also need to ensure that any pending
  10981. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  10982. * current scanout is retired before unpinning the old
  10983. * framebuffer. Note that we rely on userspace rendering
  10984. * into the buffer attached to the pipe they are waiting
  10985. * on. If not, userspace generates a GPU hang with IPEHR
  10986. * point to the MI_WAIT_FOR_EVENT.
  10987. *
  10988. * This should only fail upon a hung GPU, in which case we
  10989. * can safely continue.
  10990. */
  10991. if (needs_modeset(crtc_state)) {
  10992. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  10993. old_obj->resv, NULL,
  10994. false, 0,
  10995. GFP_KERNEL);
  10996. if (ret < 0)
  10997. return ret;
  10998. }
  10999. }
  11000. if (new_state->fence) { /* explicit fencing */
  11001. ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
  11002. new_state->fence,
  11003. I915_FENCE_TIMEOUT,
  11004. GFP_KERNEL);
  11005. if (ret < 0)
  11006. return ret;
  11007. }
  11008. if (!obj)
  11009. return 0;
  11010. if (!new_state->fence) { /* implicit fencing */
  11011. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  11012. obj->resv, NULL,
  11013. false, I915_FENCE_TIMEOUT,
  11014. GFP_KERNEL);
  11015. if (ret < 0)
  11016. return ret;
  11017. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  11018. }
  11019. return 0;
  11020. }
  11021. /**
  11022. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  11023. * @plane: drm plane to clean up for
  11024. * @fb: old framebuffer that was on plane
  11025. *
  11026. * Cleans up a framebuffer that has just been removed from a plane.
  11027. *
  11028. * Must be called with struct_mutex held.
  11029. */
  11030. void
  11031. intel_cleanup_plane_fb(struct drm_plane *plane,
  11032. struct drm_plane_state *old_state)
  11033. {
  11034. struct i915_vma *vma;
  11035. /* Should only be called after a successful intel_prepare_plane_fb()! */
  11036. vma = fetch_and_zero(&to_intel_plane_state(old_state)->vma);
  11037. if (vma)
  11038. intel_unpin_fb_vma(vma);
  11039. }
  11040. int
  11041. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  11042. {
  11043. struct drm_i915_private *dev_priv;
  11044. int max_scale;
  11045. int crtc_clock, max_dotclk;
  11046. if (!intel_crtc || !crtc_state->base.enable)
  11047. return DRM_PLANE_HELPER_NO_SCALING;
  11048. dev_priv = to_i915(intel_crtc->base.dev);
  11049. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  11050. max_dotclk = to_intel_atomic_state(crtc_state->base.state)->cdclk.logical.cdclk;
  11051. if (IS_GEMINILAKE(dev_priv))
  11052. max_dotclk *= 2;
  11053. if (WARN_ON_ONCE(!crtc_clock || max_dotclk < crtc_clock))
  11054. return DRM_PLANE_HELPER_NO_SCALING;
  11055. /*
  11056. * skl max scale is lower of:
  11057. * close to 3 but not 3, -1 is for that purpose
  11058. * or
  11059. * cdclk/crtc_clock
  11060. */
  11061. max_scale = min((1 << 16) * 3 - 1,
  11062. (1 << 8) * ((max_dotclk << 8) / crtc_clock));
  11063. return max_scale;
  11064. }
  11065. static int
  11066. intel_check_primary_plane(struct drm_plane *plane,
  11067. struct intel_crtc_state *crtc_state,
  11068. struct intel_plane_state *state)
  11069. {
  11070. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  11071. struct drm_crtc *crtc = state->base.crtc;
  11072. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  11073. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  11074. bool can_position = false;
  11075. int ret;
  11076. if (INTEL_GEN(dev_priv) >= 9) {
  11077. /* use scaler when colorkey is not required */
  11078. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  11079. min_scale = 1;
  11080. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  11081. }
  11082. can_position = true;
  11083. }
  11084. ret = drm_plane_helper_check_state(&state->base,
  11085. &state->clip,
  11086. min_scale, max_scale,
  11087. can_position, true);
  11088. if (ret)
  11089. return ret;
  11090. if (!state->base.fb)
  11091. return 0;
  11092. if (INTEL_GEN(dev_priv) >= 9) {
  11093. ret = skl_check_plane_surface(state);
  11094. if (ret)
  11095. return ret;
  11096. }
  11097. return 0;
  11098. }
  11099. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  11100. struct drm_crtc_state *old_crtc_state)
  11101. {
  11102. struct drm_device *dev = crtc->dev;
  11103. struct drm_i915_private *dev_priv = to_i915(dev);
  11104. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11105. struct intel_crtc_state *intel_cstate =
  11106. to_intel_crtc_state(crtc->state);
  11107. struct intel_crtc_state *old_intel_cstate =
  11108. to_intel_crtc_state(old_crtc_state);
  11109. struct intel_atomic_state *old_intel_state =
  11110. to_intel_atomic_state(old_crtc_state->state);
  11111. bool modeset = needs_modeset(crtc->state);
  11112. /* Perform vblank evasion around commit operation */
  11113. intel_pipe_update_start(intel_crtc);
  11114. if (modeset)
  11115. goto out;
  11116. if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
  11117. intel_color_set_csc(crtc->state);
  11118. intel_color_load_luts(crtc->state);
  11119. }
  11120. if (intel_cstate->update_pipe)
  11121. intel_update_pipe_config(intel_crtc, old_intel_cstate);
  11122. else if (INTEL_GEN(dev_priv) >= 9)
  11123. skl_detach_scalers(intel_crtc);
  11124. out:
  11125. if (dev_priv->display.atomic_update_watermarks)
  11126. dev_priv->display.atomic_update_watermarks(old_intel_state,
  11127. intel_cstate);
  11128. }
  11129. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  11130. struct drm_crtc_state *old_crtc_state)
  11131. {
  11132. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11133. intel_pipe_update_end(intel_crtc, NULL);
  11134. }
  11135. /**
  11136. * intel_plane_destroy - destroy a plane
  11137. * @plane: plane to destroy
  11138. *
  11139. * Common destruction function for all types of planes (primary, cursor,
  11140. * sprite).
  11141. */
  11142. void intel_plane_destroy(struct drm_plane *plane)
  11143. {
  11144. drm_plane_cleanup(plane);
  11145. kfree(to_intel_plane(plane));
  11146. }
  11147. const struct drm_plane_funcs intel_plane_funcs = {
  11148. .update_plane = drm_atomic_helper_update_plane,
  11149. .disable_plane = drm_atomic_helper_disable_plane,
  11150. .destroy = intel_plane_destroy,
  11151. .set_property = drm_atomic_helper_plane_set_property,
  11152. .atomic_get_property = intel_plane_atomic_get_property,
  11153. .atomic_set_property = intel_plane_atomic_set_property,
  11154. .atomic_duplicate_state = intel_plane_duplicate_state,
  11155. .atomic_destroy_state = intel_plane_destroy_state,
  11156. };
  11157. static int
  11158. intel_legacy_cursor_update(struct drm_plane *plane,
  11159. struct drm_crtc *crtc,
  11160. struct drm_framebuffer *fb,
  11161. int crtc_x, int crtc_y,
  11162. unsigned int crtc_w, unsigned int crtc_h,
  11163. uint32_t src_x, uint32_t src_y,
  11164. uint32_t src_w, uint32_t src_h)
  11165. {
  11166. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  11167. int ret;
  11168. struct drm_plane_state *old_plane_state, *new_plane_state;
  11169. struct intel_plane *intel_plane = to_intel_plane(plane);
  11170. struct drm_framebuffer *old_fb;
  11171. struct drm_crtc_state *crtc_state = crtc->state;
  11172. struct i915_vma *old_vma;
  11173. /*
  11174. * When crtc is inactive or there is a modeset pending,
  11175. * wait for it to complete in the slowpath
  11176. */
  11177. if (!crtc_state->active || needs_modeset(crtc_state) ||
  11178. to_intel_crtc_state(crtc_state)->update_pipe)
  11179. goto slow;
  11180. old_plane_state = plane->state;
  11181. /*
  11182. * If any parameters change that may affect watermarks,
  11183. * take the slowpath. Only changing fb or position should be
  11184. * in the fastpath.
  11185. */
  11186. if (old_plane_state->crtc != crtc ||
  11187. old_plane_state->src_w != src_w ||
  11188. old_plane_state->src_h != src_h ||
  11189. old_plane_state->crtc_w != crtc_w ||
  11190. old_plane_state->crtc_h != crtc_h ||
  11191. !old_plane_state->fb != !fb)
  11192. goto slow;
  11193. new_plane_state = intel_plane_duplicate_state(plane);
  11194. if (!new_plane_state)
  11195. return -ENOMEM;
  11196. drm_atomic_set_fb_for_plane(new_plane_state, fb);
  11197. new_plane_state->src_x = src_x;
  11198. new_plane_state->src_y = src_y;
  11199. new_plane_state->src_w = src_w;
  11200. new_plane_state->src_h = src_h;
  11201. new_plane_state->crtc_x = crtc_x;
  11202. new_plane_state->crtc_y = crtc_y;
  11203. new_plane_state->crtc_w = crtc_w;
  11204. new_plane_state->crtc_h = crtc_h;
  11205. ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
  11206. to_intel_plane_state(new_plane_state));
  11207. if (ret)
  11208. goto out_free;
  11209. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  11210. if (ret)
  11211. goto out_free;
  11212. if (INTEL_INFO(dev_priv)->cursor_needs_physical) {
  11213. int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
  11214. ret = i915_gem_object_attach_phys(intel_fb_obj(fb), align);
  11215. if (ret) {
  11216. DRM_DEBUG_KMS("failed to attach phys object\n");
  11217. goto out_unlock;
  11218. }
  11219. } else {
  11220. struct i915_vma *vma;
  11221. vma = intel_pin_and_fence_fb_obj(fb, new_plane_state->rotation);
  11222. if (IS_ERR(vma)) {
  11223. DRM_DEBUG_KMS("failed to pin object\n");
  11224. ret = PTR_ERR(vma);
  11225. goto out_unlock;
  11226. }
  11227. to_intel_plane_state(new_plane_state)->vma = vma;
  11228. }
  11229. old_fb = old_plane_state->fb;
  11230. old_vma = to_intel_plane_state(old_plane_state)->vma;
  11231. i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
  11232. intel_plane->frontbuffer_bit);
  11233. /* Swap plane state */
  11234. new_plane_state->fence = old_plane_state->fence;
  11235. *to_intel_plane_state(old_plane_state) = *to_intel_plane_state(new_plane_state);
  11236. new_plane_state->fence = NULL;
  11237. new_plane_state->fb = old_fb;
  11238. to_intel_plane_state(new_plane_state)->vma = old_vma;
  11239. if (plane->state->visible)
  11240. intel_plane->update_plane(plane,
  11241. to_intel_crtc_state(crtc->state),
  11242. to_intel_plane_state(plane->state));
  11243. else
  11244. intel_plane->disable_plane(plane, crtc);
  11245. intel_cleanup_plane_fb(plane, new_plane_state);
  11246. out_unlock:
  11247. mutex_unlock(&dev_priv->drm.struct_mutex);
  11248. out_free:
  11249. intel_plane_destroy_state(plane, new_plane_state);
  11250. return ret;
  11251. slow:
  11252. return drm_atomic_helper_update_plane(plane, crtc, fb,
  11253. crtc_x, crtc_y, crtc_w, crtc_h,
  11254. src_x, src_y, src_w, src_h);
  11255. }
  11256. static const struct drm_plane_funcs intel_cursor_plane_funcs = {
  11257. .update_plane = intel_legacy_cursor_update,
  11258. .disable_plane = drm_atomic_helper_disable_plane,
  11259. .destroy = intel_plane_destroy,
  11260. .set_property = drm_atomic_helper_plane_set_property,
  11261. .atomic_get_property = intel_plane_atomic_get_property,
  11262. .atomic_set_property = intel_plane_atomic_set_property,
  11263. .atomic_duplicate_state = intel_plane_duplicate_state,
  11264. .atomic_destroy_state = intel_plane_destroy_state,
  11265. };
  11266. static struct intel_plane *
  11267. intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  11268. {
  11269. struct intel_plane *primary = NULL;
  11270. struct intel_plane_state *state = NULL;
  11271. const uint32_t *intel_primary_formats;
  11272. unsigned int supported_rotations;
  11273. unsigned int num_formats;
  11274. int ret;
  11275. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  11276. if (!primary) {
  11277. ret = -ENOMEM;
  11278. goto fail;
  11279. }
  11280. state = intel_create_plane_state(&primary->base);
  11281. if (!state) {
  11282. ret = -ENOMEM;
  11283. goto fail;
  11284. }
  11285. primary->base.state = &state->base;
  11286. primary->can_scale = false;
  11287. primary->max_downscale = 1;
  11288. if (INTEL_GEN(dev_priv) >= 9) {
  11289. primary->can_scale = true;
  11290. state->scaler_id = -1;
  11291. }
  11292. primary->pipe = pipe;
  11293. /*
  11294. * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
  11295. * port is hooked to pipe B. Hence we want plane A feeding pipe B.
  11296. */
  11297. if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
  11298. primary->plane = (enum plane) !pipe;
  11299. else
  11300. primary->plane = (enum plane) pipe;
  11301. primary->id = PLANE_PRIMARY;
  11302. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  11303. primary->check_plane = intel_check_primary_plane;
  11304. if (INTEL_GEN(dev_priv) >= 9) {
  11305. intel_primary_formats = skl_primary_formats;
  11306. num_formats = ARRAY_SIZE(skl_primary_formats);
  11307. primary->update_plane = skylake_update_primary_plane;
  11308. primary->disable_plane = skylake_disable_primary_plane;
  11309. } else if (HAS_PCH_SPLIT(dev_priv)) {
  11310. intel_primary_formats = i965_primary_formats;
  11311. num_formats = ARRAY_SIZE(i965_primary_formats);
  11312. primary->update_plane = ironlake_update_primary_plane;
  11313. primary->disable_plane = i9xx_disable_primary_plane;
  11314. } else if (INTEL_GEN(dev_priv) >= 4) {
  11315. intel_primary_formats = i965_primary_formats;
  11316. num_formats = ARRAY_SIZE(i965_primary_formats);
  11317. primary->update_plane = i9xx_update_primary_plane;
  11318. primary->disable_plane = i9xx_disable_primary_plane;
  11319. } else {
  11320. intel_primary_formats = i8xx_primary_formats;
  11321. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  11322. primary->update_plane = i9xx_update_primary_plane;
  11323. primary->disable_plane = i9xx_disable_primary_plane;
  11324. }
  11325. if (INTEL_GEN(dev_priv) >= 9)
  11326. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11327. 0, &intel_plane_funcs,
  11328. intel_primary_formats, num_formats,
  11329. DRM_PLANE_TYPE_PRIMARY,
  11330. "plane 1%c", pipe_name(pipe));
  11331. else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  11332. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11333. 0, &intel_plane_funcs,
  11334. intel_primary_formats, num_formats,
  11335. DRM_PLANE_TYPE_PRIMARY,
  11336. "primary %c", pipe_name(pipe));
  11337. else
  11338. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  11339. 0, &intel_plane_funcs,
  11340. intel_primary_formats, num_formats,
  11341. DRM_PLANE_TYPE_PRIMARY,
  11342. "plane %c", plane_name(primary->plane));
  11343. if (ret)
  11344. goto fail;
  11345. if (INTEL_GEN(dev_priv) >= 9) {
  11346. supported_rotations =
  11347. DRM_ROTATE_0 | DRM_ROTATE_90 |
  11348. DRM_ROTATE_180 | DRM_ROTATE_270;
  11349. } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  11350. supported_rotations =
  11351. DRM_ROTATE_0 | DRM_ROTATE_180 |
  11352. DRM_REFLECT_X;
  11353. } else if (INTEL_GEN(dev_priv) >= 4) {
  11354. supported_rotations =
  11355. DRM_ROTATE_0 | DRM_ROTATE_180;
  11356. } else {
  11357. supported_rotations = DRM_ROTATE_0;
  11358. }
  11359. if (INTEL_GEN(dev_priv) >= 4)
  11360. drm_plane_create_rotation_property(&primary->base,
  11361. DRM_ROTATE_0,
  11362. supported_rotations);
  11363. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  11364. return primary;
  11365. fail:
  11366. kfree(state);
  11367. kfree(primary);
  11368. return ERR_PTR(ret);
  11369. }
  11370. static int
  11371. intel_check_cursor_plane(struct drm_plane *plane,
  11372. struct intel_crtc_state *crtc_state,
  11373. struct intel_plane_state *state)
  11374. {
  11375. struct drm_framebuffer *fb = state->base.fb;
  11376. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11377. enum pipe pipe = to_intel_plane(plane)->pipe;
  11378. unsigned stride;
  11379. int ret;
  11380. ret = drm_plane_helper_check_state(&state->base,
  11381. &state->clip,
  11382. DRM_PLANE_HELPER_NO_SCALING,
  11383. DRM_PLANE_HELPER_NO_SCALING,
  11384. true, true);
  11385. if (ret)
  11386. return ret;
  11387. /* if we want to turn off the cursor ignore width and height */
  11388. if (!obj)
  11389. return 0;
  11390. /* Check for which cursor types we support */
  11391. if (!cursor_size_ok(to_i915(plane->dev), state->base.crtc_w,
  11392. state->base.crtc_h)) {
  11393. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  11394. state->base.crtc_w, state->base.crtc_h);
  11395. return -EINVAL;
  11396. }
  11397. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  11398. if (obj->base.size < stride * state->base.crtc_h) {
  11399. DRM_DEBUG_KMS("buffer is too small\n");
  11400. return -ENOMEM;
  11401. }
  11402. if (fb->modifier != DRM_FORMAT_MOD_NONE) {
  11403. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  11404. return -EINVAL;
  11405. }
  11406. /*
  11407. * There's something wrong with the cursor on CHV pipe C.
  11408. * If it straddles the left edge of the screen then
  11409. * moving it away from the edge or disabling it often
  11410. * results in a pipe underrun, and often that can lead to
  11411. * dead pipe (constant underrun reported, and it scans
  11412. * out just a solid color). To recover from that, the
  11413. * display power well must be turned off and on again.
  11414. * Refuse the put the cursor into that compromised position.
  11415. */
  11416. if (IS_CHERRYVIEW(to_i915(plane->dev)) && pipe == PIPE_C &&
  11417. state->base.visible && state->base.crtc_x < 0) {
  11418. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  11419. return -EINVAL;
  11420. }
  11421. return 0;
  11422. }
  11423. static void
  11424. intel_disable_cursor_plane(struct drm_plane *plane,
  11425. struct drm_crtc *crtc)
  11426. {
  11427. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11428. intel_crtc->cursor_addr = 0;
  11429. intel_crtc_update_cursor(crtc, NULL);
  11430. }
  11431. static void
  11432. intel_update_cursor_plane(struct drm_plane *plane,
  11433. const struct intel_crtc_state *crtc_state,
  11434. const struct intel_plane_state *state)
  11435. {
  11436. struct drm_crtc *crtc = crtc_state->base.crtc;
  11437. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11438. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  11439. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  11440. uint32_t addr;
  11441. if (!obj)
  11442. addr = 0;
  11443. else if (!INTEL_INFO(dev_priv)->cursor_needs_physical)
  11444. addr = intel_plane_ggtt_offset(state);
  11445. else
  11446. addr = obj->phys_handle->busaddr;
  11447. intel_crtc->cursor_addr = addr;
  11448. intel_crtc_update_cursor(crtc, state);
  11449. }
  11450. static struct intel_plane *
  11451. intel_cursor_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  11452. {
  11453. struct intel_plane *cursor = NULL;
  11454. struct intel_plane_state *state = NULL;
  11455. int ret;
  11456. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  11457. if (!cursor) {
  11458. ret = -ENOMEM;
  11459. goto fail;
  11460. }
  11461. state = intel_create_plane_state(&cursor->base);
  11462. if (!state) {
  11463. ret = -ENOMEM;
  11464. goto fail;
  11465. }
  11466. cursor->base.state = &state->base;
  11467. cursor->can_scale = false;
  11468. cursor->max_downscale = 1;
  11469. cursor->pipe = pipe;
  11470. cursor->plane = pipe;
  11471. cursor->id = PLANE_CURSOR;
  11472. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  11473. cursor->check_plane = intel_check_cursor_plane;
  11474. cursor->update_plane = intel_update_cursor_plane;
  11475. cursor->disable_plane = intel_disable_cursor_plane;
  11476. ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
  11477. 0, &intel_cursor_plane_funcs,
  11478. intel_cursor_formats,
  11479. ARRAY_SIZE(intel_cursor_formats),
  11480. DRM_PLANE_TYPE_CURSOR,
  11481. "cursor %c", pipe_name(pipe));
  11482. if (ret)
  11483. goto fail;
  11484. if (INTEL_GEN(dev_priv) >= 4)
  11485. drm_plane_create_rotation_property(&cursor->base,
  11486. DRM_ROTATE_0,
  11487. DRM_ROTATE_0 |
  11488. DRM_ROTATE_180);
  11489. if (INTEL_GEN(dev_priv) >= 9)
  11490. state->scaler_id = -1;
  11491. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  11492. return cursor;
  11493. fail:
  11494. kfree(state);
  11495. kfree(cursor);
  11496. return ERR_PTR(ret);
  11497. }
  11498. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  11499. struct intel_crtc_state *crtc_state)
  11500. {
  11501. struct intel_crtc_scaler_state *scaler_state =
  11502. &crtc_state->scaler_state;
  11503. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  11504. int i;
  11505. crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
  11506. if (!crtc->num_scalers)
  11507. return;
  11508. for (i = 0; i < crtc->num_scalers; i++) {
  11509. struct intel_scaler *scaler = &scaler_state->scalers[i];
  11510. scaler->in_use = 0;
  11511. scaler->mode = PS_SCALER_MODE_DYN;
  11512. }
  11513. scaler_state->scaler_id = -1;
  11514. }
  11515. static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
  11516. {
  11517. struct intel_crtc *intel_crtc;
  11518. struct intel_crtc_state *crtc_state = NULL;
  11519. struct intel_plane *primary = NULL;
  11520. struct intel_plane *cursor = NULL;
  11521. int sprite, ret;
  11522. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  11523. if (!intel_crtc)
  11524. return -ENOMEM;
  11525. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  11526. if (!crtc_state) {
  11527. ret = -ENOMEM;
  11528. goto fail;
  11529. }
  11530. intel_crtc->config = crtc_state;
  11531. intel_crtc->base.state = &crtc_state->base;
  11532. crtc_state->base.crtc = &intel_crtc->base;
  11533. primary = intel_primary_plane_create(dev_priv, pipe);
  11534. if (IS_ERR(primary)) {
  11535. ret = PTR_ERR(primary);
  11536. goto fail;
  11537. }
  11538. intel_crtc->plane_ids_mask |= BIT(primary->id);
  11539. for_each_sprite(dev_priv, pipe, sprite) {
  11540. struct intel_plane *plane;
  11541. plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
  11542. if (IS_ERR(plane)) {
  11543. ret = PTR_ERR(plane);
  11544. goto fail;
  11545. }
  11546. intel_crtc->plane_ids_mask |= BIT(plane->id);
  11547. }
  11548. cursor = intel_cursor_plane_create(dev_priv, pipe);
  11549. if (IS_ERR(cursor)) {
  11550. ret = PTR_ERR(cursor);
  11551. goto fail;
  11552. }
  11553. intel_crtc->plane_ids_mask |= BIT(cursor->id);
  11554. ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
  11555. &primary->base, &cursor->base,
  11556. &intel_crtc_funcs,
  11557. "pipe %c", pipe_name(pipe));
  11558. if (ret)
  11559. goto fail;
  11560. intel_crtc->pipe = pipe;
  11561. intel_crtc->plane = primary->plane;
  11562. intel_crtc->cursor_base = ~0;
  11563. intel_crtc->cursor_cntl = ~0;
  11564. intel_crtc->cursor_size = ~0;
  11565. intel_crtc->wm.cxsr_allowed = true;
  11566. /* initialize shared scalers */
  11567. intel_crtc_init_scalers(intel_crtc, crtc_state);
  11568. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  11569. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  11570. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
  11571. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
  11572. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  11573. intel_color_init(&intel_crtc->base);
  11574. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  11575. return 0;
  11576. fail:
  11577. /*
  11578. * drm_mode_config_cleanup() will free up any
  11579. * crtcs/planes already initialized.
  11580. */
  11581. kfree(crtc_state);
  11582. kfree(intel_crtc);
  11583. return ret;
  11584. }
  11585. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  11586. {
  11587. struct drm_encoder *encoder = connector->base.encoder;
  11588. struct drm_device *dev = connector->base.dev;
  11589. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  11590. if (!encoder || WARN_ON(!encoder->crtc))
  11591. return INVALID_PIPE;
  11592. return to_intel_crtc(encoder->crtc)->pipe;
  11593. }
  11594. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  11595. struct drm_file *file)
  11596. {
  11597. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  11598. struct drm_crtc *drmmode_crtc;
  11599. struct intel_crtc *crtc;
  11600. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  11601. if (!drmmode_crtc)
  11602. return -ENOENT;
  11603. crtc = to_intel_crtc(drmmode_crtc);
  11604. pipe_from_crtc_id->pipe = crtc->pipe;
  11605. return 0;
  11606. }
  11607. static int intel_encoder_clones(struct intel_encoder *encoder)
  11608. {
  11609. struct drm_device *dev = encoder->base.dev;
  11610. struct intel_encoder *source_encoder;
  11611. int index_mask = 0;
  11612. int entry = 0;
  11613. for_each_intel_encoder(dev, source_encoder) {
  11614. if (encoders_cloneable(encoder, source_encoder))
  11615. index_mask |= (1 << entry);
  11616. entry++;
  11617. }
  11618. return index_mask;
  11619. }
  11620. static bool has_edp_a(struct drm_i915_private *dev_priv)
  11621. {
  11622. if (!IS_MOBILE(dev_priv))
  11623. return false;
  11624. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  11625. return false;
  11626. if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  11627. return false;
  11628. return true;
  11629. }
  11630. static bool intel_crt_present(struct drm_i915_private *dev_priv)
  11631. {
  11632. if (INTEL_GEN(dev_priv) >= 9)
  11633. return false;
  11634. if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
  11635. return false;
  11636. if (IS_CHERRYVIEW(dev_priv))
  11637. return false;
  11638. if (HAS_PCH_LPT_H(dev_priv) &&
  11639. I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  11640. return false;
  11641. /* DDI E can't be used if DDI A requires 4 lanes */
  11642. if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  11643. return false;
  11644. if (!dev_priv->vbt.int_crt_support)
  11645. return false;
  11646. return true;
  11647. }
  11648. void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
  11649. {
  11650. int pps_num;
  11651. int pps_idx;
  11652. if (HAS_DDI(dev_priv))
  11653. return;
  11654. /*
  11655. * This w/a is needed at least on CPT/PPT, but to be sure apply it
  11656. * everywhere where registers can be write protected.
  11657. */
  11658. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11659. pps_num = 2;
  11660. else
  11661. pps_num = 1;
  11662. for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
  11663. u32 val = I915_READ(PP_CONTROL(pps_idx));
  11664. val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
  11665. I915_WRITE(PP_CONTROL(pps_idx), val);
  11666. }
  11667. }
  11668. static void intel_pps_init(struct drm_i915_private *dev_priv)
  11669. {
  11670. if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
  11671. dev_priv->pps_mmio_base = PCH_PPS_BASE;
  11672. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11673. dev_priv->pps_mmio_base = VLV_PPS_BASE;
  11674. else
  11675. dev_priv->pps_mmio_base = PPS_BASE;
  11676. intel_pps_unlock_regs_wa(dev_priv);
  11677. }
  11678. static void intel_setup_outputs(struct drm_i915_private *dev_priv)
  11679. {
  11680. struct intel_encoder *encoder;
  11681. bool dpd_is_edp = false;
  11682. intel_pps_init(dev_priv);
  11683. /*
  11684. * intel_edp_init_connector() depends on this completing first, to
  11685. * prevent the registeration of both eDP and LVDS and the incorrect
  11686. * sharing of the PPS.
  11687. */
  11688. intel_lvds_init(dev_priv);
  11689. if (intel_crt_present(dev_priv))
  11690. intel_crt_init(dev_priv);
  11691. if (IS_GEN9_LP(dev_priv)) {
  11692. /*
  11693. * FIXME: Broxton doesn't support port detection via the
  11694. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  11695. * detect the ports.
  11696. */
  11697. intel_ddi_init(dev_priv, PORT_A);
  11698. intel_ddi_init(dev_priv, PORT_B);
  11699. intel_ddi_init(dev_priv, PORT_C);
  11700. intel_dsi_init(dev_priv);
  11701. } else if (HAS_DDI(dev_priv)) {
  11702. int found;
  11703. /*
  11704. * Haswell uses DDI functions to detect digital outputs.
  11705. * On SKL pre-D0 the strap isn't connected, so we assume
  11706. * it's there.
  11707. */
  11708. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  11709. /* WaIgnoreDDIAStrap: skl */
  11710. if (found || IS_GEN9_BC(dev_priv))
  11711. intel_ddi_init(dev_priv, PORT_A);
  11712. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  11713. * register */
  11714. found = I915_READ(SFUSE_STRAP);
  11715. if (found & SFUSE_STRAP_DDIB_DETECTED)
  11716. intel_ddi_init(dev_priv, PORT_B);
  11717. if (found & SFUSE_STRAP_DDIC_DETECTED)
  11718. intel_ddi_init(dev_priv, PORT_C);
  11719. if (found & SFUSE_STRAP_DDID_DETECTED)
  11720. intel_ddi_init(dev_priv, PORT_D);
  11721. /*
  11722. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  11723. */
  11724. if (IS_GEN9_BC(dev_priv) &&
  11725. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  11726. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  11727. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  11728. intel_ddi_init(dev_priv, PORT_E);
  11729. } else if (HAS_PCH_SPLIT(dev_priv)) {
  11730. int found;
  11731. dpd_is_edp = intel_dp_is_edp(dev_priv, PORT_D);
  11732. if (has_edp_a(dev_priv))
  11733. intel_dp_init(dev_priv, DP_A, PORT_A);
  11734. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  11735. /* PCH SDVOB multiplex with HDMIB */
  11736. found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
  11737. if (!found)
  11738. intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
  11739. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  11740. intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
  11741. }
  11742. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  11743. intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
  11744. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  11745. intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
  11746. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  11747. intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
  11748. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  11749. intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
  11750. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  11751. bool has_edp, has_port;
  11752. /*
  11753. * The DP_DETECTED bit is the latched state of the DDC
  11754. * SDA pin at boot. However since eDP doesn't require DDC
  11755. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  11756. * eDP ports may have been muxed to an alternate function.
  11757. * Thus we can't rely on the DP_DETECTED bit alone to detect
  11758. * eDP ports. Consult the VBT as well as DP_DETECTED to
  11759. * detect eDP ports.
  11760. *
  11761. * Sadly the straps seem to be missing sometimes even for HDMI
  11762. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  11763. * and VBT for the presence of the port. Additionally we can't
  11764. * trust the port type the VBT declares as we've seen at least
  11765. * HDMI ports that the VBT claim are DP or eDP.
  11766. */
  11767. has_edp = intel_dp_is_edp(dev_priv, PORT_B);
  11768. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  11769. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  11770. has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
  11771. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  11772. intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
  11773. has_edp = intel_dp_is_edp(dev_priv, PORT_C);
  11774. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  11775. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  11776. has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
  11777. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  11778. intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
  11779. if (IS_CHERRYVIEW(dev_priv)) {
  11780. /*
  11781. * eDP not supported on port D,
  11782. * so no need to worry about it
  11783. */
  11784. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  11785. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  11786. intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
  11787. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  11788. intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
  11789. }
  11790. intel_dsi_init(dev_priv);
  11791. } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
  11792. bool found = false;
  11793. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11794. DRM_DEBUG_KMS("probing SDVOB\n");
  11795. found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
  11796. if (!found && IS_G4X(dev_priv)) {
  11797. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  11798. intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
  11799. }
  11800. if (!found && IS_G4X(dev_priv))
  11801. intel_dp_init(dev_priv, DP_B, PORT_B);
  11802. }
  11803. /* Before G4X SDVOC doesn't have its own detect register */
  11804. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  11805. DRM_DEBUG_KMS("probing SDVOC\n");
  11806. found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
  11807. }
  11808. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  11809. if (IS_G4X(dev_priv)) {
  11810. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  11811. intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
  11812. }
  11813. if (IS_G4X(dev_priv))
  11814. intel_dp_init(dev_priv, DP_C, PORT_C);
  11815. }
  11816. if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
  11817. intel_dp_init(dev_priv, DP_D, PORT_D);
  11818. } else if (IS_GEN2(dev_priv))
  11819. intel_dvo_init(dev_priv);
  11820. if (SUPPORTS_TV(dev_priv))
  11821. intel_tv_init(dev_priv);
  11822. intel_psr_init(dev_priv);
  11823. for_each_intel_encoder(&dev_priv->drm, encoder) {
  11824. encoder->base.possible_crtcs = encoder->crtc_mask;
  11825. encoder->base.possible_clones =
  11826. intel_encoder_clones(encoder);
  11827. }
  11828. intel_init_pch_refclk(dev_priv);
  11829. drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
  11830. }
  11831. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  11832. {
  11833. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11834. drm_framebuffer_cleanup(fb);
  11835. i915_gem_object_lock(intel_fb->obj);
  11836. WARN_ON(!intel_fb->obj->framebuffer_references--);
  11837. i915_gem_object_unlock(intel_fb->obj);
  11838. i915_gem_object_put(intel_fb->obj);
  11839. kfree(intel_fb);
  11840. }
  11841. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  11842. struct drm_file *file,
  11843. unsigned int *handle)
  11844. {
  11845. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  11846. struct drm_i915_gem_object *obj = intel_fb->obj;
  11847. if (obj->userptr.mm) {
  11848. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  11849. return -EINVAL;
  11850. }
  11851. return drm_gem_handle_create(file, &obj->base, handle);
  11852. }
  11853. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  11854. struct drm_file *file,
  11855. unsigned flags, unsigned color,
  11856. struct drm_clip_rect *clips,
  11857. unsigned num_clips)
  11858. {
  11859. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  11860. i915_gem_object_flush_if_display(obj);
  11861. intel_fb_obj_flush(obj, ORIGIN_DIRTYFB);
  11862. return 0;
  11863. }
  11864. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  11865. .destroy = intel_user_framebuffer_destroy,
  11866. .create_handle = intel_user_framebuffer_create_handle,
  11867. .dirty = intel_user_framebuffer_dirty,
  11868. };
  11869. static
  11870. u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
  11871. uint64_t fb_modifier, uint32_t pixel_format)
  11872. {
  11873. u32 gen = INTEL_GEN(dev_priv);
  11874. if (gen >= 9) {
  11875. int cpp = drm_format_plane_cpp(pixel_format, 0);
  11876. /* "The stride in bytes must not exceed the of the size of 8K
  11877. * pixels and 32K bytes."
  11878. */
  11879. return min(8192 * cpp, 32768);
  11880. } else if (gen >= 5 && !HAS_GMCH_DISPLAY(dev_priv)) {
  11881. return 32*1024;
  11882. } else if (gen >= 4) {
  11883. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11884. return 16*1024;
  11885. else
  11886. return 32*1024;
  11887. } else if (gen >= 3) {
  11888. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  11889. return 8*1024;
  11890. else
  11891. return 16*1024;
  11892. } else {
  11893. /* XXX DSPC is limited to 4k tiled */
  11894. return 8*1024;
  11895. }
  11896. }
  11897. static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
  11898. struct drm_i915_gem_object *obj,
  11899. struct drm_mode_fb_cmd2 *mode_cmd)
  11900. {
  11901. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  11902. struct drm_format_name_buf format_name;
  11903. u32 pitch_limit, stride_alignment;
  11904. unsigned int tiling, stride;
  11905. int ret = -EINVAL;
  11906. i915_gem_object_lock(obj);
  11907. obj->framebuffer_references++;
  11908. tiling = i915_gem_object_get_tiling(obj);
  11909. stride = i915_gem_object_get_stride(obj);
  11910. i915_gem_object_unlock(obj);
  11911. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  11912. /*
  11913. * If there's a fence, enforce that
  11914. * the fb modifier and tiling mode match.
  11915. */
  11916. if (tiling != I915_TILING_NONE &&
  11917. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  11918. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  11919. goto err;
  11920. }
  11921. } else {
  11922. if (tiling == I915_TILING_X) {
  11923. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  11924. } else if (tiling == I915_TILING_Y) {
  11925. DRM_DEBUG("No Y tiling for legacy addfb\n");
  11926. goto err;
  11927. }
  11928. }
  11929. /* Passed in modifier sanity checking. */
  11930. switch (mode_cmd->modifier[0]) {
  11931. case I915_FORMAT_MOD_Y_TILED:
  11932. case I915_FORMAT_MOD_Yf_TILED:
  11933. if (INTEL_GEN(dev_priv) < 9) {
  11934. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  11935. mode_cmd->modifier[0]);
  11936. goto err;
  11937. }
  11938. case DRM_FORMAT_MOD_NONE:
  11939. case I915_FORMAT_MOD_X_TILED:
  11940. break;
  11941. default:
  11942. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  11943. mode_cmd->modifier[0]);
  11944. goto err;
  11945. }
  11946. /*
  11947. * gen2/3 display engine uses the fence if present,
  11948. * so the tiling mode must match the fb modifier exactly.
  11949. */
  11950. if (INTEL_INFO(dev_priv)->gen < 4 &&
  11951. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  11952. DRM_DEBUG("tiling_mode must match fb modifier exactly on gen2/3\n");
  11953. goto err;
  11954. }
  11955. stride_alignment = intel_fb_stride_alignment(dev_priv,
  11956. mode_cmd->modifier[0],
  11957. mode_cmd->pixel_format);
  11958. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  11959. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  11960. mode_cmd->pitches[0], stride_alignment);
  11961. goto err;
  11962. }
  11963. pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
  11964. mode_cmd->pixel_format);
  11965. if (mode_cmd->pitches[0] > pitch_limit) {
  11966. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  11967. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  11968. "tiled" : "linear",
  11969. mode_cmd->pitches[0], pitch_limit);
  11970. goto err;
  11971. }
  11972. /*
  11973. * If there's a fence, enforce that
  11974. * the fb pitch and fence stride match.
  11975. */
  11976. if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
  11977. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  11978. mode_cmd->pitches[0], stride);
  11979. goto err;
  11980. }
  11981. /* Reject formats not supported by any plane early. */
  11982. switch (mode_cmd->pixel_format) {
  11983. case DRM_FORMAT_C8:
  11984. case DRM_FORMAT_RGB565:
  11985. case DRM_FORMAT_XRGB8888:
  11986. case DRM_FORMAT_ARGB8888:
  11987. break;
  11988. case DRM_FORMAT_XRGB1555:
  11989. if (INTEL_GEN(dev_priv) > 3) {
  11990. DRM_DEBUG("unsupported pixel format: %s\n",
  11991. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  11992. goto err;
  11993. }
  11994. break;
  11995. case DRM_FORMAT_ABGR8888:
  11996. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  11997. INTEL_GEN(dev_priv) < 9) {
  11998. DRM_DEBUG("unsupported pixel format: %s\n",
  11999. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12000. goto err;
  12001. }
  12002. break;
  12003. case DRM_FORMAT_XBGR8888:
  12004. case DRM_FORMAT_XRGB2101010:
  12005. case DRM_FORMAT_XBGR2101010:
  12006. if (INTEL_GEN(dev_priv) < 4) {
  12007. DRM_DEBUG("unsupported pixel format: %s\n",
  12008. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12009. goto err;
  12010. }
  12011. break;
  12012. case DRM_FORMAT_ABGR2101010:
  12013. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  12014. DRM_DEBUG("unsupported pixel format: %s\n",
  12015. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12016. goto err;
  12017. }
  12018. break;
  12019. case DRM_FORMAT_YUYV:
  12020. case DRM_FORMAT_UYVY:
  12021. case DRM_FORMAT_YVYU:
  12022. case DRM_FORMAT_VYUY:
  12023. if (INTEL_GEN(dev_priv) < 5) {
  12024. DRM_DEBUG("unsupported pixel format: %s\n",
  12025. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12026. goto err;
  12027. }
  12028. break;
  12029. default:
  12030. DRM_DEBUG("unsupported pixel format: %s\n",
  12031. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  12032. goto err;
  12033. }
  12034. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  12035. if (mode_cmd->offsets[0] != 0)
  12036. goto err;
  12037. drm_helper_mode_fill_fb_struct(&dev_priv->drm,
  12038. &intel_fb->base, mode_cmd);
  12039. intel_fb->obj = obj;
  12040. ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
  12041. if (ret)
  12042. goto err;
  12043. ret = drm_framebuffer_init(obj->base.dev,
  12044. &intel_fb->base,
  12045. &intel_fb_funcs);
  12046. if (ret) {
  12047. DRM_ERROR("framebuffer init failed %d\n", ret);
  12048. goto err;
  12049. }
  12050. return 0;
  12051. err:
  12052. i915_gem_object_lock(obj);
  12053. obj->framebuffer_references--;
  12054. i915_gem_object_unlock(obj);
  12055. return ret;
  12056. }
  12057. static struct drm_framebuffer *
  12058. intel_user_framebuffer_create(struct drm_device *dev,
  12059. struct drm_file *filp,
  12060. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  12061. {
  12062. struct drm_framebuffer *fb;
  12063. struct drm_i915_gem_object *obj;
  12064. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  12065. obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
  12066. if (!obj)
  12067. return ERR_PTR(-ENOENT);
  12068. fb = intel_framebuffer_create(obj, &mode_cmd);
  12069. if (IS_ERR(fb))
  12070. i915_gem_object_put(obj);
  12071. return fb;
  12072. }
  12073. static void intel_atomic_state_free(struct drm_atomic_state *state)
  12074. {
  12075. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  12076. drm_atomic_state_default_release(state);
  12077. i915_sw_fence_fini(&intel_state->commit_ready);
  12078. kfree(state);
  12079. }
  12080. static const struct drm_mode_config_funcs intel_mode_funcs = {
  12081. .fb_create = intel_user_framebuffer_create,
  12082. .output_poll_changed = intel_fbdev_output_poll_changed,
  12083. .atomic_check = intel_atomic_check,
  12084. .atomic_commit = intel_atomic_commit,
  12085. .atomic_state_alloc = intel_atomic_state_alloc,
  12086. .atomic_state_clear = intel_atomic_state_clear,
  12087. .atomic_state_free = intel_atomic_state_free,
  12088. };
  12089. /**
  12090. * intel_init_display_hooks - initialize the display modesetting hooks
  12091. * @dev_priv: device private
  12092. */
  12093. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  12094. {
  12095. intel_init_cdclk_hooks(dev_priv);
  12096. if (INTEL_INFO(dev_priv)->gen >= 9) {
  12097. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12098. dev_priv->display.get_initial_plane_config =
  12099. skylake_get_initial_plane_config;
  12100. dev_priv->display.crtc_compute_clock =
  12101. haswell_crtc_compute_clock;
  12102. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12103. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12104. } else if (HAS_DDI(dev_priv)) {
  12105. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  12106. dev_priv->display.get_initial_plane_config =
  12107. ironlake_get_initial_plane_config;
  12108. dev_priv->display.crtc_compute_clock =
  12109. haswell_crtc_compute_clock;
  12110. dev_priv->display.crtc_enable = haswell_crtc_enable;
  12111. dev_priv->display.crtc_disable = haswell_crtc_disable;
  12112. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12113. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  12114. dev_priv->display.get_initial_plane_config =
  12115. ironlake_get_initial_plane_config;
  12116. dev_priv->display.crtc_compute_clock =
  12117. ironlake_crtc_compute_clock;
  12118. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  12119. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  12120. } else if (IS_CHERRYVIEW(dev_priv)) {
  12121. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12122. dev_priv->display.get_initial_plane_config =
  12123. i9xx_get_initial_plane_config;
  12124. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  12125. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12126. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12127. } else if (IS_VALLEYVIEW(dev_priv)) {
  12128. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12129. dev_priv->display.get_initial_plane_config =
  12130. i9xx_get_initial_plane_config;
  12131. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  12132. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  12133. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12134. } else if (IS_G4X(dev_priv)) {
  12135. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12136. dev_priv->display.get_initial_plane_config =
  12137. i9xx_get_initial_plane_config;
  12138. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  12139. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12140. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12141. } else if (IS_PINEVIEW(dev_priv)) {
  12142. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12143. dev_priv->display.get_initial_plane_config =
  12144. i9xx_get_initial_plane_config;
  12145. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  12146. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12147. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12148. } else if (!IS_GEN2(dev_priv)) {
  12149. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12150. dev_priv->display.get_initial_plane_config =
  12151. i9xx_get_initial_plane_config;
  12152. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  12153. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12154. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12155. } else {
  12156. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  12157. dev_priv->display.get_initial_plane_config =
  12158. i9xx_get_initial_plane_config;
  12159. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  12160. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  12161. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  12162. }
  12163. if (IS_GEN5(dev_priv)) {
  12164. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  12165. } else if (IS_GEN6(dev_priv)) {
  12166. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  12167. } else if (IS_IVYBRIDGE(dev_priv)) {
  12168. /* FIXME: detect B0+ stepping and use auto training */
  12169. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  12170. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  12171. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  12172. }
  12173. if (dev_priv->info.gen >= 9)
  12174. dev_priv->display.update_crtcs = skl_update_crtcs;
  12175. else
  12176. dev_priv->display.update_crtcs = intel_update_crtcs;
  12177. switch (INTEL_INFO(dev_priv)->gen) {
  12178. case 2:
  12179. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  12180. break;
  12181. case 3:
  12182. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  12183. break;
  12184. case 4:
  12185. case 5:
  12186. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  12187. break;
  12188. case 6:
  12189. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  12190. break;
  12191. case 7:
  12192. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  12193. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  12194. break;
  12195. case 9:
  12196. /* Drop through - unsupported since execlist only. */
  12197. default:
  12198. /* Default just returns -ENODEV to indicate unsupported */
  12199. dev_priv->display.queue_flip = intel_default_queue_flip;
  12200. }
  12201. }
  12202. /*
  12203. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  12204. * resume, or other times. This quirk makes sure that's the case for
  12205. * affected systems.
  12206. */
  12207. static void quirk_pipea_force(struct drm_device *dev)
  12208. {
  12209. struct drm_i915_private *dev_priv = to_i915(dev);
  12210. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  12211. DRM_INFO("applying pipe a force quirk\n");
  12212. }
  12213. static void quirk_pipeb_force(struct drm_device *dev)
  12214. {
  12215. struct drm_i915_private *dev_priv = to_i915(dev);
  12216. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  12217. DRM_INFO("applying pipe b force quirk\n");
  12218. }
  12219. /*
  12220. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  12221. */
  12222. static void quirk_ssc_force_disable(struct drm_device *dev)
  12223. {
  12224. struct drm_i915_private *dev_priv = to_i915(dev);
  12225. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  12226. DRM_INFO("applying lvds SSC disable quirk\n");
  12227. }
  12228. /*
  12229. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  12230. * brightness value
  12231. */
  12232. static void quirk_invert_brightness(struct drm_device *dev)
  12233. {
  12234. struct drm_i915_private *dev_priv = to_i915(dev);
  12235. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  12236. DRM_INFO("applying inverted panel brightness quirk\n");
  12237. }
  12238. /* Some VBT's incorrectly indicate no backlight is present */
  12239. static void quirk_backlight_present(struct drm_device *dev)
  12240. {
  12241. struct drm_i915_private *dev_priv = to_i915(dev);
  12242. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  12243. DRM_INFO("applying backlight present quirk\n");
  12244. }
  12245. struct intel_quirk {
  12246. int device;
  12247. int subsystem_vendor;
  12248. int subsystem_device;
  12249. void (*hook)(struct drm_device *dev);
  12250. };
  12251. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  12252. struct intel_dmi_quirk {
  12253. void (*hook)(struct drm_device *dev);
  12254. const struct dmi_system_id (*dmi_id_list)[];
  12255. };
  12256. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  12257. {
  12258. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  12259. return 1;
  12260. }
  12261. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  12262. {
  12263. .dmi_id_list = &(const struct dmi_system_id[]) {
  12264. {
  12265. .callback = intel_dmi_reverse_brightness,
  12266. .ident = "NCR Corporation",
  12267. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  12268. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  12269. },
  12270. },
  12271. { } /* terminating entry */
  12272. },
  12273. .hook = quirk_invert_brightness,
  12274. },
  12275. };
  12276. static struct intel_quirk intel_quirks[] = {
  12277. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  12278. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  12279. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  12280. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  12281. /* 830 needs to leave pipe A & dpll A up */
  12282. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  12283. /* 830 needs to leave pipe B & dpll B up */
  12284. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  12285. /* Lenovo U160 cannot use SSC on LVDS */
  12286. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  12287. /* Sony Vaio Y cannot use SSC on LVDS */
  12288. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  12289. /* Acer Aspire 5734Z must invert backlight brightness */
  12290. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  12291. /* Acer/eMachines G725 */
  12292. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  12293. /* Acer/eMachines e725 */
  12294. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  12295. /* Acer/Packard Bell NCL20 */
  12296. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  12297. /* Acer Aspire 4736Z */
  12298. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  12299. /* Acer Aspire 5336 */
  12300. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  12301. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  12302. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  12303. /* Acer C720 Chromebook (Core i3 4005U) */
  12304. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  12305. /* Apple Macbook 2,1 (Core 2 T7400) */
  12306. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  12307. /* Apple Macbook 4,1 */
  12308. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  12309. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  12310. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  12311. /* HP Chromebook 14 (Celeron 2955U) */
  12312. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  12313. /* Dell Chromebook 11 */
  12314. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  12315. /* Dell Chromebook 11 (2015 version) */
  12316. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  12317. };
  12318. static void intel_init_quirks(struct drm_device *dev)
  12319. {
  12320. struct pci_dev *d = dev->pdev;
  12321. int i;
  12322. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  12323. struct intel_quirk *q = &intel_quirks[i];
  12324. if (d->device == q->device &&
  12325. (d->subsystem_vendor == q->subsystem_vendor ||
  12326. q->subsystem_vendor == PCI_ANY_ID) &&
  12327. (d->subsystem_device == q->subsystem_device ||
  12328. q->subsystem_device == PCI_ANY_ID))
  12329. q->hook(dev);
  12330. }
  12331. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  12332. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  12333. intel_dmi_quirks[i].hook(dev);
  12334. }
  12335. }
  12336. /* Disable the VGA plane that we never use */
  12337. static void i915_disable_vga(struct drm_i915_private *dev_priv)
  12338. {
  12339. struct pci_dev *pdev = dev_priv->drm.pdev;
  12340. u8 sr1;
  12341. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12342. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  12343. vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
  12344. outb(SR01, VGA_SR_INDEX);
  12345. sr1 = inb(VGA_SR_DATA);
  12346. outb(sr1 | 1<<5, VGA_SR_DATA);
  12347. vga_put(pdev, VGA_RSRC_LEGACY_IO);
  12348. udelay(300);
  12349. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  12350. POSTING_READ(vga_reg);
  12351. }
  12352. void intel_modeset_init_hw(struct drm_device *dev)
  12353. {
  12354. struct drm_i915_private *dev_priv = to_i915(dev);
  12355. intel_update_cdclk(dev_priv);
  12356. dev_priv->cdclk.logical = dev_priv->cdclk.actual = dev_priv->cdclk.hw;
  12357. intel_init_clock_gating(dev_priv);
  12358. }
  12359. /*
  12360. * Calculate what we think the watermarks should be for the state we've read
  12361. * out of the hardware and then immediately program those watermarks so that
  12362. * we ensure the hardware settings match our internal state.
  12363. *
  12364. * We can calculate what we think WM's should be by creating a duplicate of the
  12365. * current state (which was constructed during hardware readout) and running it
  12366. * through the atomic check code to calculate new watermark values in the
  12367. * state object.
  12368. */
  12369. static void sanitize_watermarks(struct drm_device *dev)
  12370. {
  12371. struct drm_i915_private *dev_priv = to_i915(dev);
  12372. struct drm_atomic_state *state;
  12373. struct intel_atomic_state *intel_state;
  12374. struct drm_crtc *crtc;
  12375. struct drm_crtc_state *cstate;
  12376. struct drm_modeset_acquire_ctx ctx;
  12377. int ret;
  12378. int i;
  12379. /* Only supported on platforms that use atomic watermark design */
  12380. if (!dev_priv->display.optimize_watermarks)
  12381. return;
  12382. /*
  12383. * We need to hold connection_mutex before calling duplicate_state so
  12384. * that the connector loop is protected.
  12385. */
  12386. drm_modeset_acquire_init(&ctx, 0);
  12387. retry:
  12388. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12389. if (ret == -EDEADLK) {
  12390. drm_modeset_backoff(&ctx);
  12391. goto retry;
  12392. } else if (WARN_ON(ret)) {
  12393. goto fail;
  12394. }
  12395. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  12396. if (WARN_ON(IS_ERR(state)))
  12397. goto fail;
  12398. intel_state = to_intel_atomic_state(state);
  12399. /*
  12400. * Hardware readout is the only time we don't want to calculate
  12401. * intermediate watermarks (since we don't trust the current
  12402. * watermarks).
  12403. */
  12404. intel_state->skip_intermediate_wm = true;
  12405. ret = intel_atomic_check(dev, state);
  12406. if (ret) {
  12407. /*
  12408. * If we fail here, it means that the hardware appears to be
  12409. * programmed in a way that shouldn't be possible, given our
  12410. * understanding of watermark requirements. This might mean a
  12411. * mistake in the hardware readout code or a mistake in the
  12412. * watermark calculations for a given platform. Raise a WARN
  12413. * so that this is noticeable.
  12414. *
  12415. * If this actually happens, we'll have to just leave the
  12416. * BIOS-programmed watermarks untouched and hope for the best.
  12417. */
  12418. WARN(true, "Could not determine valid watermarks for inherited state\n");
  12419. goto put_state;
  12420. }
  12421. /* Write calculated watermark values back */
  12422. for_each_crtc_in_state(state, crtc, cstate, i) {
  12423. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  12424. cs->wm.need_postvbl_update = true;
  12425. dev_priv->display.optimize_watermarks(intel_state, cs);
  12426. }
  12427. put_state:
  12428. drm_atomic_state_put(state);
  12429. fail:
  12430. drm_modeset_drop_locks(&ctx);
  12431. drm_modeset_acquire_fini(&ctx);
  12432. }
  12433. int intel_modeset_init(struct drm_device *dev)
  12434. {
  12435. struct drm_i915_private *dev_priv = to_i915(dev);
  12436. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  12437. enum pipe pipe;
  12438. struct intel_crtc *crtc;
  12439. drm_mode_config_init(dev);
  12440. dev->mode_config.min_width = 0;
  12441. dev->mode_config.min_height = 0;
  12442. dev->mode_config.preferred_depth = 24;
  12443. dev->mode_config.prefer_shadow = 1;
  12444. dev->mode_config.allow_fb_modifiers = true;
  12445. dev->mode_config.funcs = &intel_mode_funcs;
  12446. INIT_WORK(&dev_priv->atomic_helper.free_work,
  12447. intel_atomic_helper_free_state_worker);
  12448. intel_init_quirks(dev);
  12449. intel_init_pm(dev_priv);
  12450. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  12451. return 0;
  12452. /*
  12453. * There may be no VBT; and if the BIOS enabled SSC we can
  12454. * just keep using it to avoid unnecessary flicker. Whereas if the
  12455. * BIOS isn't using it, don't assume it will work even if the VBT
  12456. * indicates as much.
  12457. */
  12458. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
  12459. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  12460. DREF_SSC1_ENABLE);
  12461. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  12462. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  12463. bios_lvds_use_ssc ? "en" : "dis",
  12464. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  12465. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  12466. }
  12467. }
  12468. if (IS_GEN2(dev_priv)) {
  12469. dev->mode_config.max_width = 2048;
  12470. dev->mode_config.max_height = 2048;
  12471. } else if (IS_GEN3(dev_priv)) {
  12472. dev->mode_config.max_width = 4096;
  12473. dev->mode_config.max_height = 4096;
  12474. } else {
  12475. dev->mode_config.max_width = 8192;
  12476. dev->mode_config.max_height = 8192;
  12477. }
  12478. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  12479. dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
  12480. dev->mode_config.cursor_height = 1023;
  12481. } else if (IS_GEN2(dev_priv)) {
  12482. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  12483. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  12484. } else {
  12485. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  12486. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  12487. }
  12488. dev->mode_config.fb_base = ggtt->mappable_base;
  12489. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  12490. INTEL_INFO(dev_priv)->num_pipes,
  12491. INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
  12492. for_each_pipe(dev_priv, pipe) {
  12493. int ret;
  12494. ret = intel_crtc_init(dev_priv, pipe);
  12495. if (ret) {
  12496. drm_mode_config_cleanup(dev);
  12497. return ret;
  12498. }
  12499. }
  12500. intel_shared_dpll_init(dev);
  12501. intel_update_czclk(dev_priv);
  12502. intel_modeset_init_hw(dev);
  12503. if (dev_priv->max_cdclk_freq == 0)
  12504. intel_update_max_cdclk(dev_priv);
  12505. /* Just disable it once at startup */
  12506. i915_disable_vga(dev_priv);
  12507. intel_setup_outputs(dev_priv);
  12508. drm_modeset_lock_all(dev);
  12509. intel_modeset_setup_hw_state(dev);
  12510. drm_modeset_unlock_all(dev);
  12511. for_each_intel_crtc(dev, crtc) {
  12512. struct intel_initial_plane_config plane_config = {};
  12513. if (!crtc->active)
  12514. continue;
  12515. /*
  12516. * Note that reserving the BIOS fb up front prevents us
  12517. * from stuffing other stolen allocations like the ring
  12518. * on top. This prevents some ugliness at boot time, and
  12519. * can even allow for smooth boot transitions if the BIOS
  12520. * fb is large enough for the active pipe configuration.
  12521. */
  12522. dev_priv->display.get_initial_plane_config(crtc,
  12523. &plane_config);
  12524. /*
  12525. * If the fb is shared between multiple heads, we'll
  12526. * just get the first one.
  12527. */
  12528. intel_find_initial_plane_obj(crtc, &plane_config);
  12529. }
  12530. /*
  12531. * Make sure hardware watermarks really match the state we read out.
  12532. * Note that we need to do this after reconstructing the BIOS fb's
  12533. * since the watermark calculation done here will use pstate->fb.
  12534. */
  12535. sanitize_watermarks(dev);
  12536. return 0;
  12537. }
  12538. static void intel_enable_pipe_a(struct drm_device *dev)
  12539. {
  12540. struct intel_connector *connector;
  12541. struct drm_connector *crt = NULL;
  12542. struct intel_load_detect_pipe load_detect_temp;
  12543. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  12544. /* We can't just switch on the pipe A, we need to set things up with a
  12545. * proper mode and output configuration. As a gross hack, enable pipe A
  12546. * by enabling the load detect pipe once. */
  12547. for_each_intel_connector(dev, connector) {
  12548. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  12549. crt = &connector->base;
  12550. break;
  12551. }
  12552. }
  12553. if (!crt)
  12554. return;
  12555. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  12556. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  12557. }
  12558. static bool
  12559. intel_check_plane_mapping(struct intel_crtc *crtc)
  12560. {
  12561. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  12562. u32 val;
  12563. if (INTEL_INFO(dev_priv)->num_pipes == 1)
  12564. return true;
  12565. val = I915_READ(DSPCNTR(!crtc->plane));
  12566. if ((val & DISPLAY_PLANE_ENABLE) &&
  12567. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  12568. return false;
  12569. return true;
  12570. }
  12571. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  12572. {
  12573. struct drm_device *dev = crtc->base.dev;
  12574. struct intel_encoder *encoder;
  12575. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  12576. return true;
  12577. return false;
  12578. }
  12579. static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
  12580. {
  12581. struct drm_device *dev = encoder->base.dev;
  12582. struct intel_connector *connector;
  12583. for_each_connector_on_encoder(dev, &encoder->base, connector)
  12584. return connector;
  12585. return NULL;
  12586. }
  12587. static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
  12588. enum transcoder pch_transcoder)
  12589. {
  12590. return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
  12591. (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
  12592. }
  12593. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  12594. {
  12595. struct drm_device *dev = crtc->base.dev;
  12596. struct drm_i915_private *dev_priv = to_i915(dev);
  12597. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  12598. /* Clear any frame start delays used for debugging left by the BIOS */
  12599. if (!transcoder_is_dsi(cpu_transcoder)) {
  12600. i915_reg_t reg = PIPECONF(cpu_transcoder);
  12601. I915_WRITE(reg,
  12602. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  12603. }
  12604. /* restore vblank interrupts to correct state */
  12605. drm_crtc_vblank_reset(&crtc->base);
  12606. if (crtc->active) {
  12607. struct intel_plane *plane;
  12608. drm_crtc_vblank_on(&crtc->base);
  12609. /* Disable everything but the primary plane */
  12610. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  12611. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  12612. continue;
  12613. plane->disable_plane(&plane->base, &crtc->base);
  12614. }
  12615. }
  12616. /* We need to sanitize the plane -> pipe mapping first because this will
  12617. * disable the crtc (and hence change the state) if it is wrong. Note
  12618. * that gen4+ has a fixed plane -> pipe mapping. */
  12619. if (INTEL_GEN(dev_priv) < 4 && !intel_check_plane_mapping(crtc)) {
  12620. bool plane;
  12621. DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
  12622. crtc->base.base.id, crtc->base.name);
  12623. /* Pipe has the wrong plane attached and the plane is active.
  12624. * Temporarily change the plane mapping and disable everything
  12625. * ... */
  12626. plane = crtc->plane;
  12627. crtc->base.primary->state->visible = true;
  12628. crtc->plane = !plane;
  12629. intel_crtc_disable_noatomic(&crtc->base);
  12630. crtc->plane = plane;
  12631. }
  12632. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  12633. crtc->pipe == PIPE_A && !crtc->active) {
  12634. /* BIOS forgot to enable pipe A, this mostly happens after
  12635. * resume. Force-enable the pipe to fix this, the update_dpms
  12636. * call below we restore the pipe to the right state, but leave
  12637. * the required bits on. */
  12638. intel_enable_pipe_a(dev);
  12639. }
  12640. /* Adjust the state of the output pipe according to whether we
  12641. * have active connectors/encoders. */
  12642. if (crtc->active && !intel_crtc_has_encoders(crtc))
  12643. intel_crtc_disable_noatomic(&crtc->base);
  12644. if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
  12645. /*
  12646. * We start out with underrun reporting disabled to avoid races.
  12647. * For correct bookkeeping mark this on active crtcs.
  12648. *
  12649. * Also on gmch platforms we dont have any hardware bits to
  12650. * disable the underrun reporting. Which means we need to start
  12651. * out with underrun reporting disabled also on inactive pipes,
  12652. * since otherwise we'll complain about the garbage we read when
  12653. * e.g. coming up after runtime pm.
  12654. *
  12655. * No protection against concurrent access is required - at
  12656. * worst a fifo underrun happens which also sets this to false.
  12657. */
  12658. crtc->cpu_fifo_underrun_disabled = true;
  12659. /*
  12660. * We track the PCH trancoder underrun reporting state
  12661. * within the crtc. With crtc for pipe A housing the underrun
  12662. * reporting state for PCH transcoder A, crtc for pipe B housing
  12663. * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
  12664. * and marking underrun reporting as disabled for the non-existing
  12665. * PCH transcoders B and C would prevent enabling the south
  12666. * error interrupt (see cpt_can_enable_serr_int()).
  12667. */
  12668. if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
  12669. crtc->pch_fifo_underrun_disabled = true;
  12670. }
  12671. }
  12672. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  12673. {
  12674. struct intel_connector *connector;
  12675. /* We need to check both for a crtc link (meaning that the
  12676. * encoder is active and trying to read from a pipe) and the
  12677. * pipe itself being active. */
  12678. bool has_active_crtc = encoder->base.crtc &&
  12679. to_intel_crtc(encoder->base.crtc)->active;
  12680. connector = intel_encoder_find_connector(encoder);
  12681. if (connector && !has_active_crtc) {
  12682. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  12683. encoder->base.base.id,
  12684. encoder->base.name);
  12685. /* Connector is active, but has no active pipe. This is
  12686. * fallout from our resume register restoring. Disable
  12687. * the encoder manually again. */
  12688. if (encoder->base.crtc) {
  12689. struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
  12690. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  12691. encoder->base.base.id,
  12692. encoder->base.name);
  12693. encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12694. if (encoder->post_disable)
  12695. encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  12696. }
  12697. encoder->base.crtc = NULL;
  12698. /* Inconsistent output/port/pipe state happens presumably due to
  12699. * a bug in one of the get_hw_state functions. Or someplace else
  12700. * in our code, like the register restore mess on resume. Clamp
  12701. * things to off as a safer default. */
  12702. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12703. connector->base.encoder = NULL;
  12704. }
  12705. /* Enabled encoders without active connectors will be fixed in
  12706. * the crtc fixup. */
  12707. }
  12708. void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
  12709. {
  12710. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  12711. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  12712. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  12713. i915_disable_vga(dev_priv);
  12714. }
  12715. }
  12716. void i915_redisable_vga(struct drm_i915_private *dev_priv)
  12717. {
  12718. /* This function can be called both from intel_modeset_setup_hw_state or
  12719. * at a very early point in our resume sequence, where the power well
  12720. * structures are not yet restored. Since this function is at a very
  12721. * paranoid "someone might have enabled VGA while we were not looking"
  12722. * level, just check if the power well is enabled instead of trying to
  12723. * follow the "don't touch the power well if we don't need it" policy
  12724. * the rest of the driver uses. */
  12725. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  12726. return;
  12727. i915_redisable_vga_power_on(dev_priv);
  12728. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  12729. }
  12730. static bool primary_get_hw_state(struct intel_plane *plane)
  12731. {
  12732. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  12733. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  12734. }
  12735. /* FIXME read out full plane state for all planes */
  12736. static void readout_plane_state(struct intel_crtc *crtc)
  12737. {
  12738. struct drm_plane *primary = crtc->base.primary;
  12739. struct intel_plane_state *plane_state =
  12740. to_intel_plane_state(primary->state);
  12741. plane_state->base.visible = crtc->active &&
  12742. primary_get_hw_state(to_intel_plane(primary));
  12743. if (plane_state->base.visible)
  12744. crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
  12745. }
  12746. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  12747. {
  12748. struct drm_i915_private *dev_priv = to_i915(dev);
  12749. enum pipe pipe;
  12750. struct intel_crtc *crtc;
  12751. struct intel_encoder *encoder;
  12752. struct intel_connector *connector;
  12753. int i;
  12754. dev_priv->active_crtcs = 0;
  12755. for_each_intel_crtc(dev, crtc) {
  12756. struct intel_crtc_state *crtc_state =
  12757. to_intel_crtc_state(crtc->base.state);
  12758. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  12759. memset(crtc_state, 0, sizeof(*crtc_state));
  12760. crtc_state->base.crtc = &crtc->base;
  12761. crtc_state->base.active = crtc_state->base.enable =
  12762. dev_priv->display.get_pipe_config(crtc, crtc_state);
  12763. crtc->base.enabled = crtc_state->base.enable;
  12764. crtc->active = crtc_state->base.active;
  12765. if (crtc_state->base.active)
  12766. dev_priv->active_crtcs |= 1 << crtc->pipe;
  12767. readout_plane_state(crtc);
  12768. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  12769. crtc->base.base.id, crtc->base.name,
  12770. enableddisabled(crtc_state->base.active));
  12771. }
  12772. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12773. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12774. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  12775. &pll->state.hw_state);
  12776. pll->state.crtc_mask = 0;
  12777. for_each_intel_crtc(dev, crtc) {
  12778. struct intel_crtc_state *crtc_state =
  12779. to_intel_crtc_state(crtc->base.state);
  12780. if (crtc_state->base.active &&
  12781. crtc_state->shared_dpll == pll)
  12782. pll->state.crtc_mask |= 1 << crtc->pipe;
  12783. }
  12784. pll->active_mask = pll->state.crtc_mask;
  12785. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  12786. pll->name, pll->state.crtc_mask, pll->on);
  12787. }
  12788. for_each_intel_encoder(dev, encoder) {
  12789. pipe = 0;
  12790. if (encoder->get_hw_state(encoder, &pipe)) {
  12791. struct intel_crtc_state *crtc_state;
  12792. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12793. crtc_state = to_intel_crtc_state(crtc->base.state);
  12794. encoder->base.crtc = &crtc->base;
  12795. crtc_state->output_types |= 1 << encoder->type;
  12796. encoder->get_config(encoder, crtc_state);
  12797. } else {
  12798. encoder->base.crtc = NULL;
  12799. }
  12800. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  12801. encoder->base.base.id, encoder->base.name,
  12802. enableddisabled(encoder->base.crtc),
  12803. pipe_name(pipe));
  12804. }
  12805. for_each_intel_connector(dev, connector) {
  12806. if (connector->get_hw_state(connector)) {
  12807. connector->base.dpms = DRM_MODE_DPMS_ON;
  12808. encoder = connector->encoder;
  12809. connector->base.encoder = &encoder->base;
  12810. if (encoder->base.crtc &&
  12811. encoder->base.crtc->state->active) {
  12812. /*
  12813. * This has to be done during hardware readout
  12814. * because anything calling .crtc_disable may
  12815. * rely on the connector_mask being accurate.
  12816. */
  12817. encoder->base.crtc->state->connector_mask |=
  12818. 1 << drm_connector_index(&connector->base);
  12819. encoder->base.crtc->state->encoder_mask |=
  12820. 1 << drm_encoder_index(&encoder->base);
  12821. }
  12822. } else {
  12823. connector->base.dpms = DRM_MODE_DPMS_OFF;
  12824. connector->base.encoder = NULL;
  12825. }
  12826. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  12827. connector->base.base.id, connector->base.name,
  12828. enableddisabled(connector->base.encoder));
  12829. }
  12830. for_each_intel_crtc(dev, crtc) {
  12831. struct intel_crtc_state *crtc_state =
  12832. to_intel_crtc_state(crtc->base.state);
  12833. int pixclk = 0;
  12834. crtc->base.hwmode = crtc_state->base.adjusted_mode;
  12835. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  12836. if (crtc_state->base.active) {
  12837. intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
  12838. intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
  12839. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  12840. /*
  12841. * The initial mode needs to be set in order to keep
  12842. * the atomic core happy. It wants a valid mode if the
  12843. * crtc's enabled, so we do the above call.
  12844. *
  12845. * But we don't set all the derived state fully, hence
  12846. * set a flag to indicate that a full recalculation is
  12847. * needed on the next commit.
  12848. */
  12849. crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
  12850. intel_crtc_compute_pixel_rate(crtc_state);
  12851. if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv) ||
  12852. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12853. pixclk = crtc_state->pixel_rate;
  12854. else
  12855. WARN_ON(dev_priv->display.modeset_calc_cdclk);
  12856. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  12857. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  12858. pixclk = DIV_ROUND_UP(pixclk * 100, 95);
  12859. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  12860. update_scanline_offset(crtc);
  12861. }
  12862. dev_priv->min_pixclk[crtc->pipe] = pixclk;
  12863. intel_pipe_config_sanity_check(dev_priv, crtc_state);
  12864. }
  12865. }
  12866. static void
  12867. get_encoder_power_domains(struct drm_i915_private *dev_priv)
  12868. {
  12869. struct intel_encoder *encoder;
  12870. for_each_intel_encoder(&dev_priv->drm, encoder) {
  12871. u64 get_domains;
  12872. enum intel_display_power_domain domain;
  12873. if (!encoder->get_power_domains)
  12874. continue;
  12875. get_domains = encoder->get_power_domains(encoder);
  12876. for_each_power_domain(domain, get_domains)
  12877. intel_display_power_get(dev_priv, domain);
  12878. }
  12879. }
  12880. /* Scan out the current hw modeset state,
  12881. * and sanitizes it to the current state
  12882. */
  12883. static void
  12884. intel_modeset_setup_hw_state(struct drm_device *dev)
  12885. {
  12886. struct drm_i915_private *dev_priv = to_i915(dev);
  12887. enum pipe pipe;
  12888. struct intel_crtc *crtc;
  12889. struct intel_encoder *encoder;
  12890. int i;
  12891. intel_modeset_readout_hw_state(dev);
  12892. /* HW state is read out, now we need to sanitize this mess. */
  12893. get_encoder_power_domains(dev_priv);
  12894. for_each_intel_encoder(dev, encoder) {
  12895. intel_sanitize_encoder(encoder);
  12896. }
  12897. for_each_pipe(dev_priv, pipe) {
  12898. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  12899. intel_sanitize_crtc(crtc);
  12900. intel_dump_pipe_config(crtc, crtc->config,
  12901. "[setup_hw_state]");
  12902. }
  12903. intel_modeset_update_connector_atomic_state(dev);
  12904. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  12905. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  12906. if (!pll->on || pll->active_mask)
  12907. continue;
  12908. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  12909. pll->funcs.disable(dev_priv, pll);
  12910. pll->on = false;
  12911. }
  12912. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12913. vlv_wm_get_hw_state(dev);
  12914. else if (IS_GEN9(dev_priv))
  12915. skl_wm_get_hw_state(dev);
  12916. else if (HAS_PCH_SPLIT(dev_priv))
  12917. ilk_wm_get_hw_state(dev);
  12918. for_each_intel_crtc(dev, crtc) {
  12919. u64 put_domains;
  12920. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  12921. if (WARN_ON(put_domains))
  12922. modeset_put_power_domains(dev_priv, put_domains);
  12923. }
  12924. intel_display_set_init_power(dev_priv, false);
  12925. intel_power_domains_verify_state(dev_priv);
  12926. intel_fbc_init_pipe_state(dev_priv);
  12927. }
  12928. void intel_display_resume(struct drm_device *dev)
  12929. {
  12930. struct drm_i915_private *dev_priv = to_i915(dev);
  12931. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  12932. struct drm_modeset_acquire_ctx ctx;
  12933. int ret;
  12934. dev_priv->modeset_restore_state = NULL;
  12935. if (state)
  12936. state->acquire_ctx = &ctx;
  12937. /*
  12938. * This is a cludge because with real atomic modeset mode_config.mutex
  12939. * won't be taken. Unfortunately some probed state like
  12940. * audio_codec_enable is still protected by mode_config.mutex, so lock
  12941. * it here for now.
  12942. */
  12943. mutex_lock(&dev->mode_config.mutex);
  12944. drm_modeset_acquire_init(&ctx, 0);
  12945. while (1) {
  12946. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  12947. if (ret != -EDEADLK)
  12948. break;
  12949. drm_modeset_backoff(&ctx);
  12950. }
  12951. if (!ret)
  12952. ret = __intel_display_resume(dev, state);
  12953. drm_modeset_drop_locks(&ctx);
  12954. drm_modeset_acquire_fini(&ctx);
  12955. mutex_unlock(&dev->mode_config.mutex);
  12956. if (ret)
  12957. DRM_ERROR("Restoring old state failed with %i\n", ret);
  12958. if (state)
  12959. drm_atomic_state_put(state);
  12960. }
  12961. void intel_modeset_gem_init(struct drm_device *dev)
  12962. {
  12963. struct drm_i915_private *dev_priv = to_i915(dev);
  12964. intel_init_gt_powersave(dev_priv);
  12965. intel_setup_overlay(dev_priv);
  12966. }
  12967. int intel_connector_register(struct drm_connector *connector)
  12968. {
  12969. struct intel_connector *intel_connector = to_intel_connector(connector);
  12970. int ret;
  12971. ret = intel_backlight_device_register(intel_connector);
  12972. if (ret)
  12973. goto err;
  12974. return 0;
  12975. err:
  12976. return ret;
  12977. }
  12978. void intel_connector_unregister(struct drm_connector *connector)
  12979. {
  12980. struct intel_connector *intel_connector = to_intel_connector(connector);
  12981. intel_backlight_device_unregister(intel_connector);
  12982. intel_panel_destroy_backlight(connector);
  12983. }
  12984. void intel_modeset_cleanup(struct drm_device *dev)
  12985. {
  12986. struct drm_i915_private *dev_priv = to_i915(dev);
  12987. flush_work(&dev_priv->atomic_helper.free_work);
  12988. WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
  12989. intel_disable_gt_powersave(dev_priv);
  12990. /*
  12991. * Interrupts and polling as the first thing to avoid creating havoc.
  12992. * Too much stuff here (turning of connectors, ...) would
  12993. * experience fancy races otherwise.
  12994. */
  12995. intel_irq_uninstall(dev_priv);
  12996. /*
  12997. * Due to the hpd irq storm handling the hotplug work can re-arm the
  12998. * poll handlers. Hence disable polling after hpd handling is shut down.
  12999. */
  13000. drm_kms_helper_poll_fini(dev);
  13001. intel_unregister_dsm_handler();
  13002. intel_fbc_global_disable(dev_priv);
  13003. /* flush any delayed tasks or pending work */
  13004. flush_scheduled_work();
  13005. drm_mode_config_cleanup(dev);
  13006. intel_cleanup_overlay(dev_priv);
  13007. intel_cleanup_gt_powersave(dev_priv);
  13008. intel_teardown_gmbus(dev_priv);
  13009. }
  13010. void intel_connector_attach_encoder(struct intel_connector *connector,
  13011. struct intel_encoder *encoder)
  13012. {
  13013. connector->encoder = encoder;
  13014. drm_mode_connector_attach_encoder(&connector->base,
  13015. &encoder->base);
  13016. }
  13017. /*
  13018. * set vga decode state - true == enable VGA decode
  13019. */
  13020. int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
  13021. {
  13022. unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  13023. u16 gmch_ctrl;
  13024. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  13025. DRM_ERROR("failed to read control word\n");
  13026. return -EIO;
  13027. }
  13028. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  13029. return 0;
  13030. if (state)
  13031. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  13032. else
  13033. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  13034. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  13035. DRM_ERROR("failed to write control word\n");
  13036. return -EIO;
  13037. }
  13038. return 0;
  13039. }
  13040. #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
  13041. struct intel_display_error_state {
  13042. u32 power_well_driver;
  13043. int num_transcoders;
  13044. struct intel_cursor_error_state {
  13045. u32 control;
  13046. u32 position;
  13047. u32 base;
  13048. u32 size;
  13049. } cursor[I915_MAX_PIPES];
  13050. struct intel_pipe_error_state {
  13051. bool power_domain_on;
  13052. u32 source;
  13053. u32 stat;
  13054. } pipe[I915_MAX_PIPES];
  13055. struct intel_plane_error_state {
  13056. u32 control;
  13057. u32 stride;
  13058. u32 size;
  13059. u32 pos;
  13060. u32 addr;
  13061. u32 surface;
  13062. u32 tile_offset;
  13063. } plane[I915_MAX_PIPES];
  13064. struct intel_transcoder_error_state {
  13065. bool power_domain_on;
  13066. enum transcoder cpu_transcoder;
  13067. u32 conf;
  13068. u32 htotal;
  13069. u32 hblank;
  13070. u32 hsync;
  13071. u32 vtotal;
  13072. u32 vblank;
  13073. u32 vsync;
  13074. } transcoder[4];
  13075. };
  13076. struct intel_display_error_state *
  13077. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  13078. {
  13079. struct intel_display_error_state *error;
  13080. int transcoders[] = {
  13081. TRANSCODER_A,
  13082. TRANSCODER_B,
  13083. TRANSCODER_C,
  13084. TRANSCODER_EDP,
  13085. };
  13086. int i;
  13087. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  13088. return NULL;
  13089. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  13090. if (error == NULL)
  13091. return NULL;
  13092. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  13093. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  13094. for_each_pipe(dev_priv, i) {
  13095. error->pipe[i].power_domain_on =
  13096. __intel_display_power_is_enabled(dev_priv,
  13097. POWER_DOMAIN_PIPE(i));
  13098. if (!error->pipe[i].power_domain_on)
  13099. continue;
  13100. error->cursor[i].control = I915_READ(CURCNTR(i));
  13101. error->cursor[i].position = I915_READ(CURPOS(i));
  13102. error->cursor[i].base = I915_READ(CURBASE(i));
  13103. error->plane[i].control = I915_READ(DSPCNTR(i));
  13104. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  13105. if (INTEL_GEN(dev_priv) <= 3) {
  13106. error->plane[i].size = I915_READ(DSPSIZE(i));
  13107. error->plane[i].pos = I915_READ(DSPPOS(i));
  13108. }
  13109. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  13110. error->plane[i].addr = I915_READ(DSPADDR(i));
  13111. if (INTEL_GEN(dev_priv) >= 4) {
  13112. error->plane[i].surface = I915_READ(DSPSURF(i));
  13113. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  13114. }
  13115. error->pipe[i].source = I915_READ(PIPESRC(i));
  13116. if (HAS_GMCH_DISPLAY(dev_priv))
  13117. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  13118. }
  13119. /* Note: this does not include DSI transcoders. */
  13120. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  13121. if (HAS_DDI(dev_priv))
  13122. error->num_transcoders++; /* Account for eDP. */
  13123. for (i = 0; i < error->num_transcoders; i++) {
  13124. enum transcoder cpu_transcoder = transcoders[i];
  13125. error->transcoder[i].power_domain_on =
  13126. __intel_display_power_is_enabled(dev_priv,
  13127. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  13128. if (!error->transcoder[i].power_domain_on)
  13129. continue;
  13130. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  13131. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  13132. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  13133. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  13134. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  13135. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  13136. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  13137. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  13138. }
  13139. return error;
  13140. }
  13141. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  13142. void
  13143. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  13144. struct intel_display_error_state *error)
  13145. {
  13146. struct drm_i915_private *dev_priv = m->i915;
  13147. int i;
  13148. if (!error)
  13149. return;
  13150. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
  13151. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  13152. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  13153. error->power_well_driver);
  13154. for_each_pipe(dev_priv, i) {
  13155. err_printf(m, "Pipe [%d]:\n", i);
  13156. err_printf(m, " Power: %s\n",
  13157. onoff(error->pipe[i].power_domain_on));
  13158. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  13159. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  13160. err_printf(m, "Plane [%d]:\n", i);
  13161. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  13162. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  13163. if (INTEL_GEN(dev_priv) <= 3) {
  13164. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  13165. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  13166. }
  13167. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  13168. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  13169. if (INTEL_GEN(dev_priv) >= 4) {
  13170. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  13171. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  13172. }
  13173. err_printf(m, "Cursor [%d]:\n", i);
  13174. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  13175. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  13176. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  13177. }
  13178. for (i = 0; i < error->num_transcoders; i++) {
  13179. err_printf(m, "CPU transcoder: %s\n",
  13180. transcoder_name(error->transcoder[i].cpu_transcoder));
  13181. err_printf(m, " Power: %s\n",
  13182. onoff(error->transcoder[i].power_domain_on));
  13183. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  13184. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  13185. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  13186. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  13187. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  13188. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  13189. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  13190. }
  13191. }
  13192. #endif