intel_display.c 488 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include "intel_frontbuffer.h"
  37. #include <drm/i915_drm.h>
  38. #include "i915_drv.h"
  39. #include "intel_dsi.h"
  40. #include "i915_trace.h"
  41. #include <drm/drm_atomic.h>
  42. #include <drm/drm_atomic_helper.h>
  43. #include <drm/drm_dp_helper.h>
  44. #include <drm/drm_crtc_helper.h>
  45. #include <drm/drm_plane_helper.h>
  46. #include <drm/drm_rect.h>
  47. #include <linux/dma_remapping.h>
  48. #include <linux/reservation.h>
  49. static bool is_mmio_work(struct intel_flip_work *work)
  50. {
  51. return work->mmio_work.func;
  52. }
  53. /* Primary plane formats for gen <= 3 */
  54. static const uint32_t i8xx_primary_formats[] = {
  55. DRM_FORMAT_C8,
  56. DRM_FORMAT_RGB565,
  57. DRM_FORMAT_XRGB1555,
  58. DRM_FORMAT_XRGB8888,
  59. };
  60. /* Primary plane formats for gen >= 4 */
  61. static const uint32_t i965_primary_formats[] = {
  62. DRM_FORMAT_C8,
  63. DRM_FORMAT_RGB565,
  64. DRM_FORMAT_XRGB8888,
  65. DRM_FORMAT_XBGR8888,
  66. DRM_FORMAT_XRGB2101010,
  67. DRM_FORMAT_XBGR2101010,
  68. };
  69. static const uint32_t skl_primary_formats[] = {
  70. DRM_FORMAT_C8,
  71. DRM_FORMAT_RGB565,
  72. DRM_FORMAT_XRGB8888,
  73. DRM_FORMAT_XBGR8888,
  74. DRM_FORMAT_ARGB8888,
  75. DRM_FORMAT_ABGR8888,
  76. DRM_FORMAT_XRGB2101010,
  77. DRM_FORMAT_XBGR2101010,
  78. DRM_FORMAT_YUYV,
  79. DRM_FORMAT_YVYU,
  80. DRM_FORMAT_UYVY,
  81. DRM_FORMAT_VYUY,
  82. };
  83. /* Cursor formats */
  84. static const uint32_t intel_cursor_formats[] = {
  85. DRM_FORMAT_ARGB8888,
  86. };
  87. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  88. struct intel_crtc_state *pipe_config);
  89. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  90. struct intel_crtc_state *pipe_config);
  91. static int intel_framebuffer_init(struct drm_device *dev,
  92. struct intel_framebuffer *ifb,
  93. struct drm_mode_fb_cmd2 *mode_cmd,
  94. struct drm_i915_gem_object *obj);
  95. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  96. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  97. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
  98. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  99. struct intel_link_m_n *m_n,
  100. struct intel_link_m_n *m2_n2);
  101. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  102. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  103. static void haswell_set_pipemisc(struct drm_crtc *crtc);
  104. static void vlv_prepare_pll(struct intel_crtc *crtc,
  105. const struct intel_crtc_state *pipe_config);
  106. static void chv_prepare_pll(struct intel_crtc *crtc,
  107. const struct intel_crtc_state *pipe_config);
  108. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  109. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  110. static void 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. static int ilk_max_pixel_rate(struct drm_atomic_state *state);
  118. static int glk_calc_cdclk(int max_pixclk);
  119. static int bxt_calc_cdclk(int max_pixclk);
  120. struct intel_limit {
  121. struct {
  122. int min, max;
  123. } dot, vco, n, m, m1, m2, p, p1;
  124. struct {
  125. int dot_limit;
  126. int p2_slow, p2_fast;
  127. } p2;
  128. };
  129. /* returns HPLL frequency in kHz */
  130. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  131. {
  132. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  133. /* Obtain SKU information */
  134. mutex_lock(&dev_priv->sb_lock);
  135. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  136. CCK_FUSE_HPLL_FREQ_MASK;
  137. mutex_unlock(&dev_priv->sb_lock);
  138. return vco_freq[hpll_freq] * 1000;
  139. }
  140. int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
  141. const char *name, u32 reg, int ref_freq)
  142. {
  143. u32 val;
  144. int divider;
  145. mutex_lock(&dev_priv->sb_lock);
  146. val = vlv_cck_read(dev_priv, reg);
  147. mutex_unlock(&dev_priv->sb_lock);
  148. divider = val & CCK_FREQUENCY_VALUES;
  149. WARN((val & CCK_FREQUENCY_STATUS) !=
  150. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  151. "%s change in progress\n", name);
  152. return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
  153. }
  154. static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  155. const char *name, u32 reg)
  156. {
  157. if (dev_priv->hpll_freq == 0)
  158. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  159. return vlv_get_cck_clock(dev_priv, name, reg,
  160. dev_priv->hpll_freq);
  161. }
  162. static int
  163. intel_pch_rawclk(struct drm_i915_private *dev_priv)
  164. {
  165. return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
  166. }
  167. static int
  168. intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
  169. {
  170. /* RAWCLK_FREQ_VLV register updated from power well code */
  171. return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
  172. CCK_DISPLAY_REF_CLOCK_CONTROL);
  173. }
  174. static int
  175. intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
  176. {
  177. uint32_t clkcfg;
  178. /* hrawclock is 1/4 the FSB frequency */
  179. clkcfg = I915_READ(CLKCFG);
  180. switch (clkcfg & CLKCFG_FSB_MASK) {
  181. case CLKCFG_FSB_400:
  182. return 100000;
  183. case CLKCFG_FSB_533:
  184. return 133333;
  185. case CLKCFG_FSB_667:
  186. return 166667;
  187. case CLKCFG_FSB_800:
  188. return 200000;
  189. case CLKCFG_FSB_1067:
  190. return 266667;
  191. case CLKCFG_FSB_1333:
  192. return 333333;
  193. /* these two are just a guess; one of them might be right */
  194. case CLKCFG_FSB_1600:
  195. case CLKCFG_FSB_1600_ALT:
  196. return 400000;
  197. default:
  198. return 133333;
  199. }
  200. }
  201. void intel_update_rawclk(struct drm_i915_private *dev_priv)
  202. {
  203. if (HAS_PCH_SPLIT(dev_priv))
  204. dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
  205. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  206. dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
  207. else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
  208. dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
  209. else
  210. return; /* no rawclk on other platforms, or no need to know it */
  211. DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
  212. }
  213. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  214. {
  215. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  216. return;
  217. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  218. CCK_CZ_CLOCK_CONTROL);
  219. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  220. }
  221. static inline u32 /* units of 100MHz */
  222. intel_fdi_link_freq(struct drm_i915_private *dev_priv,
  223. const struct intel_crtc_state *pipe_config)
  224. {
  225. if (HAS_DDI(dev_priv))
  226. return pipe_config->port_clock; /* SPLL */
  227. else if (IS_GEN5(dev_priv))
  228. return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
  229. else
  230. return 270000;
  231. }
  232. static const struct intel_limit intel_limits_i8xx_dac = {
  233. .dot = { .min = 25000, .max = 350000 },
  234. .vco = { .min = 908000, .max = 1512000 },
  235. .n = { .min = 2, .max = 16 },
  236. .m = { .min = 96, .max = 140 },
  237. .m1 = { .min = 18, .max = 26 },
  238. .m2 = { .min = 6, .max = 16 },
  239. .p = { .min = 4, .max = 128 },
  240. .p1 = { .min = 2, .max = 33 },
  241. .p2 = { .dot_limit = 165000,
  242. .p2_slow = 4, .p2_fast = 2 },
  243. };
  244. static const struct intel_limit intel_limits_i8xx_dvo = {
  245. .dot = { .min = 25000, .max = 350000 },
  246. .vco = { .min = 908000, .max = 1512000 },
  247. .n = { .min = 2, .max = 16 },
  248. .m = { .min = 96, .max = 140 },
  249. .m1 = { .min = 18, .max = 26 },
  250. .m2 = { .min = 6, .max = 16 },
  251. .p = { .min = 4, .max = 128 },
  252. .p1 = { .min = 2, .max = 33 },
  253. .p2 = { .dot_limit = 165000,
  254. .p2_slow = 4, .p2_fast = 4 },
  255. };
  256. static const struct intel_limit intel_limits_i8xx_lvds = {
  257. .dot = { .min = 25000, .max = 350000 },
  258. .vco = { .min = 908000, .max = 1512000 },
  259. .n = { .min = 2, .max = 16 },
  260. .m = { .min = 96, .max = 140 },
  261. .m1 = { .min = 18, .max = 26 },
  262. .m2 = { .min = 6, .max = 16 },
  263. .p = { .min = 4, .max = 128 },
  264. .p1 = { .min = 1, .max = 6 },
  265. .p2 = { .dot_limit = 165000,
  266. .p2_slow = 14, .p2_fast = 7 },
  267. };
  268. static const struct intel_limit intel_limits_i9xx_sdvo = {
  269. .dot = { .min = 20000, .max = 400000 },
  270. .vco = { .min = 1400000, .max = 2800000 },
  271. .n = { .min = 1, .max = 6 },
  272. .m = { .min = 70, .max = 120 },
  273. .m1 = { .min = 8, .max = 18 },
  274. .m2 = { .min = 3, .max = 7 },
  275. .p = { .min = 5, .max = 80 },
  276. .p1 = { .min = 1, .max = 8 },
  277. .p2 = { .dot_limit = 200000,
  278. .p2_slow = 10, .p2_fast = 5 },
  279. };
  280. static const struct intel_limit intel_limits_i9xx_lvds = {
  281. .dot = { .min = 20000, .max = 400000 },
  282. .vco = { .min = 1400000, .max = 2800000 },
  283. .n = { .min = 1, .max = 6 },
  284. .m = { .min = 70, .max = 120 },
  285. .m1 = { .min = 8, .max = 18 },
  286. .m2 = { .min = 3, .max = 7 },
  287. .p = { .min = 7, .max = 98 },
  288. .p1 = { .min = 1, .max = 8 },
  289. .p2 = { .dot_limit = 112000,
  290. .p2_slow = 14, .p2_fast = 7 },
  291. };
  292. static const struct intel_limit intel_limits_g4x_sdvo = {
  293. .dot = { .min = 25000, .max = 270000 },
  294. .vco = { .min = 1750000, .max = 3500000},
  295. .n = { .min = 1, .max = 4 },
  296. .m = { .min = 104, .max = 138 },
  297. .m1 = { .min = 17, .max = 23 },
  298. .m2 = { .min = 5, .max = 11 },
  299. .p = { .min = 10, .max = 30 },
  300. .p1 = { .min = 1, .max = 3},
  301. .p2 = { .dot_limit = 270000,
  302. .p2_slow = 10,
  303. .p2_fast = 10
  304. },
  305. };
  306. static const struct intel_limit intel_limits_g4x_hdmi = {
  307. .dot = { .min = 22000, .max = 400000 },
  308. .vco = { .min = 1750000, .max = 3500000},
  309. .n = { .min = 1, .max = 4 },
  310. .m = { .min = 104, .max = 138 },
  311. .m1 = { .min = 16, .max = 23 },
  312. .m2 = { .min = 5, .max = 11 },
  313. .p = { .min = 5, .max = 80 },
  314. .p1 = { .min = 1, .max = 8},
  315. .p2 = { .dot_limit = 165000,
  316. .p2_slow = 10, .p2_fast = 5 },
  317. };
  318. static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
  319. .dot = { .min = 20000, .max = 115000 },
  320. .vco = { .min = 1750000, .max = 3500000 },
  321. .n = { .min = 1, .max = 3 },
  322. .m = { .min = 104, .max = 138 },
  323. .m1 = { .min = 17, .max = 23 },
  324. .m2 = { .min = 5, .max = 11 },
  325. .p = { .min = 28, .max = 112 },
  326. .p1 = { .min = 2, .max = 8 },
  327. .p2 = { .dot_limit = 0,
  328. .p2_slow = 14, .p2_fast = 14
  329. },
  330. };
  331. static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
  332. .dot = { .min = 80000, .max = 224000 },
  333. .vco = { .min = 1750000, .max = 3500000 },
  334. .n = { .min = 1, .max = 3 },
  335. .m = { .min = 104, .max = 138 },
  336. .m1 = { .min = 17, .max = 23 },
  337. .m2 = { .min = 5, .max = 11 },
  338. .p = { .min = 14, .max = 42 },
  339. .p1 = { .min = 2, .max = 6 },
  340. .p2 = { .dot_limit = 0,
  341. .p2_slow = 7, .p2_fast = 7
  342. },
  343. };
  344. static const struct intel_limit intel_limits_pineview_sdvo = {
  345. .dot = { .min = 20000, .max = 400000},
  346. .vco = { .min = 1700000, .max = 3500000 },
  347. /* Pineview's Ncounter is a ring counter */
  348. .n = { .min = 3, .max = 6 },
  349. .m = { .min = 2, .max = 256 },
  350. /* Pineview only has one combined m divider, which we treat as m2. */
  351. .m1 = { .min = 0, .max = 0 },
  352. .m2 = { .min = 0, .max = 254 },
  353. .p = { .min = 5, .max = 80 },
  354. .p1 = { .min = 1, .max = 8 },
  355. .p2 = { .dot_limit = 200000,
  356. .p2_slow = 10, .p2_fast = 5 },
  357. };
  358. static const struct intel_limit intel_limits_pineview_lvds = {
  359. .dot = { .min = 20000, .max = 400000 },
  360. .vco = { .min = 1700000, .max = 3500000 },
  361. .n = { .min = 3, .max = 6 },
  362. .m = { .min = 2, .max = 256 },
  363. .m1 = { .min = 0, .max = 0 },
  364. .m2 = { .min = 0, .max = 254 },
  365. .p = { .min = 7, .max = 112 },
  366. .p1 = { .min = 1, .max = 8 },
  367. .p2 = { .dot_limit = 112000,
  368. .p2_slow = 14, .p2_fast = 14 },
  369. };
  370. /* Ironlake / Sandybridge
  371. *
  372. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  373. * the range value for them is (actual_value - 2).
  374. */
  375. static const struct intel_limit intel_limits_ironlake_dac = {
  376. .dot = { .min = 25000, .max = 350000 },
  377. .vco = { .min = 1760000, .max = 3510000 },
  378. .n = { .min = 1, .max = 5 },
  379. .m = { .min = 79, .max = 127 },
  380. .m1 = { .min = 12, .max = 22 },
  381. .m2 = { .min = 5, .max = 9 },
  382. .p = { .min = 5, .max = 80 },
  383. .p1 = { .min = 1, .max = 8 },
  384. .p2 = { .dot_limit = 225000,
  385. .p2_slow = 10, .p2_fast = 5 },
  386. };
  387. static const struct intel_limit intel_limits_ironlake_single_lvds = {
  388. .dot = { .min = 25000, .max = 350000 },
  389. .vco = { .min = 1760000, .max = 3510000 },
  390. .n = { .min = 1, .max = 3 },
  391. .m = { .min = 79, .max = 118 },
  392. .m1 = { .min = 12, .max = 22 },
  393. .m2 = { .min = 5, .max = 9 },
  394. .p = { .min = 28, .max = 112 },
  395. .p1 = { .min = 2, .max = 8 },
  396. .p2 = { .dot_limit = 225000,
  397. .p2_slow = 14, .p2_fast = 14 },
  398. };
  399. static const struct intel_limit intel_limits_ironlake_dual_lvds = {
  400. .dot = { .min = 25000, .max = 350000 },
  401. .vco = { .min = 1760000, .max = 3510000 },
  402. .n = { .min = 1, .max = 3 },
  403. .m = { .min = 79, .max = 127 },
  404. .m1 = { .min = 12, .max = 22 },
  405. .m2 = { .min = 5, .max = 9 },
  406. .p = { .min = 14, .max = 56 },
  407. .p1 = { .min = 2, .max = 8 },
  408. .p2 = { .dot_limit = 225000,
  409. .p2_slow = 7, .p2_fast = 7 },
  410. };
  411. /* LVDS 100mhz refclk limits. */
  412. static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
  413. .dot = { .min = 25000, .max = 350000 },
  414. .vco = { .min = 1760000, .max = 3510000 },
  415. .n = { .min = 1, .max = 2 },
  416. .m = { .min = 79, .max = 126 },
  417. .m1 = { .min = 12, .max = 22 },
  418. .m2 = { .min = 5, .max = 9 },
  419. .p = { .min = 28, .max = 112 },
  420. .p1 = { .min = 2, .max = 8 },
  421. .p2 = { .dot_limit = 225000,
  422. .p2_slow = 14, .p2_fast = 14 },
  423. };
  424. static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
  425. .dot = { .min = 25000, .max = 350000 },
  426. .vco = { .min = 1760000, .max = 3510000 },
  427. .n = { .min = 1, .max = 3 },
  428. .m = { .min = 79, .max = 126 },
  429. .m1 = { .min = 12, .max = 22 },
  430. .m2 = { .min = 5, .max = 9 },
  431. .p = { .min = 14, .max = 42 },
  432. .p1 = { .min = 2, .max = 6 },
  433. .p2 = { .dot_limit = 225000,
  434. .p2_slow = 7, .p2_fast = 7 },
  435. };
  436. static const struct intel_limit intel_limits_vlv = {
  437. /*
  438. * These are the data rate limits (measured in fast clocks)
  439. * since those are the strictest limits we have. The fast
  440. * clock and actual rate limits are more relaxed, so checking
  441. * them would make no difference.
  442. */
  443. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  444. .vco = { .min = 4000000, .max = 6000000 },
  445. .n = { .min = 1, .max = 7 },
  446. .m1 = { .min = 2, .max = 3 },
  447. .m2 = { .min = 11, .max = 156 },
  448. .p1 = { .min = 2, .max = 3 },
  449. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  450. };
  451. static const struct intel_limit intel_limits_chv = {
  452. /*
  453. * These are the data rate limits (measured in fast clocks)
  454. * since those are the strictest limits we have. The fast
  455. * clock and actual rate limits are more relaxed, so checking
  456. * them would make no difference.
  457. */
  458. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  459. .vco = { .min = 4800000, .max = 6480000 },
  460. .n = { .min = 1, .max = 1 },
  461. .m1 = { .min = 2, .max = 2 },
  462. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  463. .p1 = { .min = 2, .max = 4 },
  464. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  465. };
  466. static const struct intel_limit intel_limits_bxt = {
  467. /* FIXME: find real dot limits */
  468. .dot = { .min = 0, .max = INT_MAX },
  469. .vco = { .min = 4800000, .max = 6700000 },
  470. .n = { .min = 1, .max = 1 },
  471. .m1 = { .min = 2, .max = 2 },
  472. /* FIXME: find real m2 limits */
  473. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  474. .p1 = { .min = 2, .max = 4 },
  475. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  476. };
  477. static bool
  478. needs_modeset(struct drm_crtc_state *state)
  479. {
  480. return drm_atomic_crtc_needs_modeset(state);
  481. }
  482. /*
  483. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  484. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  485. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  486. * The helpers' return value is the rate of the clock that is fed to the
  487. * display engine's pipe which can be the above fast dot clock rate or a
  488. * divided-down version of it.
  489. */
  490. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  491. static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
  492. {
  493. clock->m = clock->m2 + 2;
  494. clock->p = clock->p1 * clock->p2;
  495. if (WARN_ON(clock->n == 0 || clock->p == 0))
  496. return 0;
  497. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  498. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  499. return clock->dot;
  500. }
  501. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  502. {
  503. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  504. }
  505. static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
  506. {
  507. clock->m = i9xx_dpll_compute_m(clock);
  508. clock->p = clock->p1 * clock->p2;
  509. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  510. return 0;
  511. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  512. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  513. return clock->dot;
  514. }
  515. static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
  516. {
  517. clock->m = clock->m1 * clock->m2;
  518. clock->p = clock->p1 * clock->p2;
  519. if (WARN_ON(clock->n == 0 || clock->p == 0))
  520. return 0;
  521. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  522. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  523. return clock->dot / 5;
  524. }
  525. int chv_calc_dpll_params(int refclk, struct dpll *clock)
  526. {
  527. clock->m = clock->m1 * clock->m2;
  528. clock->p = clock->p1 * clock->p2;
  529. if (WARN_ON(clock->n == 0 || clock->p == 0))
  530. return 0;
  531. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  532. clock->n << 22);
  533. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  534. return clock->dot / 5;
  535. }
  536. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  537. /**
  538. * Returns whether the given set of divisors are valid for a given refclk with
  539. * the given connectors.
  540. */
  541. static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
  542. const struct intel_limit *limit,
  543. const struct dpll *clock)
  544. {
  545. if (clock->n < limit->n.min || limit->n.max < clock->n)
  546. INTELPllInvalid("n out of range\n");
  547. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  548. INTELPllInvalid("p1 out of range\n");
  549. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  550. INTELPllInvalid("m2 out of range\n");
  551. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  552. INTELPllInvalid("m1 out of range\n");
  553. if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
  554. !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
  555. if (clock->m1 <= clock->m2)
  556. INTELPllInvalid("m1 <= m2\n");
  557. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  558. !IS_GEN9_LP(dev_priv)) {
  559. if (clock->p < limit->p.min || limit->p.max < clock->p)
  560. INTELPllInvalid("p out of range\n");
  561. if (clock->m < limit->m.min || limit->m.max < clock->m)
  562. INTELPllInvalid("m out of range\n");
  563. }
  564. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  565. INTELPllInvalid("vco out of range\n");
  566. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  567. * connector, etc., rather than just a single range.
  568. */
  569. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  570. INTELPllInvalid("dot out of range\n");
  571. return true;
  572. }
  573. static int
  574. i9xx_select_p2_div(const struct intel_limit *limit,
  575. const struct intel_crtc_state *crtc_state,
  576. int target)
  577. {
  578. struct drm_device *dev = crtc_state->base.crtc->dev;
  579. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  580. /*
  581. * For LVDS just rely on its current settings for dual-channel.
  582. * We haven't figured out how to reliably set up different
  583. * single/dual channel state, if we even can.
  584. */
  585. if (intel_is_dual_link_lvds(dev))
  586. return limit->p2.p2_fast;
  587. else
  588. return limit->p2.p2_slow;
  589. } else {
  590. if (target < limit->p2.dot_limit)
  591. return limit->p2.p2_slow;
  592. else
  593. return limit->p2.p2_fast;
  594. }
  595. }
  596. /*
  597. * Returns a set of divisors for the desired target clock with the given
  598. * refclk, or FALSE. The returned values represent the clock equation:
  599. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  600. *
  601. * Target and reference clocks are specified in kHz.
  602. *
  603. * If match_clock is provided, then best_clock P divider must match the P
  604. * divider from @match_clock used for LVDS downclocking.
  605. */
  606. static bool
  607. i9xx_find_best_dpll(const struct intel_limit *limit,
  608. struct intel_crtc_state *crtc_state,
  609. int target, int refclk, struct dpll *match_clock,
  610. struct dpll *best_clock)
  611. {
  612. struct drm_device *dev = crtc_state->base.crtc->dev;
  613. struct dpll clock;
  614. int err = target;
  615. memset(best_clock, 0, sizeof(*best_clock));
  616. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  617. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  618. clock.m1++) {
  619. for (clock.m2 = limit->m2.min;
  620. clock.m2 <= limit->m2.max; clock.m2++) {
  621. if (clock.m2 >= clock.m1)
  622. break;
  623. for (clock.n = limit->n.min;
  624. clock.n <= limit->n.max; clock.n++) {
  625. for (clock.p1 = limit->p1.min;
  626. clock.p1 <= limit->p1.max; clock.p1++) {
  627. int this_err;
  628. i9xx_calc_dpll_params(refclk, &clock);
  629. if (!intel_PLL_is_valid(to_i915(dev),
  630. limit,
  631. &clock))
  632. continue;
  633. if (match_clock &&
  634. clock.p != match_clock->p)
  635. continue;
  636. this_err = abs(clock.dot - target);
  637. if (this_err < err) {
  638. *best_clock = clock;
  639. err = this_err;
  640. }
  641. }
  642. }
  643. }
  644. }
  645. return (err != target);
  646. }
  647. /*
  648. * Returns a set of divisors for the desired target clock with the given
  649. * refclk, or FALSE. The returned values represent the clock equation:
  650. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  651. *
  652. * Target and reference clocks are specified in kHz.
  653. *
  654. * If match_clock is provided, then best_clock P divider must match the P
  655. * divider from @match_clock used for LVDS downclocking.
  656. */
  657. static bool
  658. pnv_find_best_dpll(const struct intel_limit *limit,
  659. struct intel_crtc_state *crtc_state,
  660. int target, int refclk, struct dpll *match_clock,
  661. struct dpll *best_clock)
  662. {
  663. struct drm_device *dev = crtc_state->base.crtc->dev;
  664. struct dpll clock;
  665. int err = target;
  666. memset(best_clock, 0, sizeof(*best_clock));
  667. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  668. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  669. clock.m1++) {
  670. for (clock.m2 = limit->m2.min;
  671. clock.m2 <= limit->m2.max; clock.m2++) {
  672. for (clock.n = limit->n.min;
  673. clock.n <= limit->n.max; clock.n++) {
  674. for (clock.p1 = limit->p1.min;
  675. clock.p1 <= limit->p1.max; clock.p1++) {
  676. int this_err;
  677. pnv_calc_dpll_params(refclk, &clock);
  678. if (!intel_PLL_is_valid(to_i915(dev),
  679. limit,
  680. &clock))
  681. continue;
  682. if (match_clock &&
  683. clock.p != match_clock->p)
  684. continue;
  685. this_err = abs(clock.dot - target);
  686. if (this_err < err) {
  687. *best_clock = clock;
  688. err = this_err;
  689. }
  690. }
  691. }
  692. }
  693. }
  694. return (err != target);
  695. }
  696. /*
  697. * Returns a set of divisors for the desired target clock with the given
  698. * refclk, or FALSE. The returned values represent the clock equation:
  699. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  700. *
  701. * Target and reference clocks are specified in kHz.
  702. *
  703. * If match_clock is provided, then best_clock P divider must match the P
  704. * divider from @match_clock used for LVDS downclocking.
  705. */
  706. static bool
  707. g4x_find_best_dpll(const struct intel_limit *limit,
  708. struct intel_crtc_state *crtc_state,
  709. int target, int refclk, struct dpll *match_clock,
  710. struct dpll *best_clock)
  711. {
  712. struct drm_device *dev = crtc_state->base.crtc->dev;
  713. struct dpll clock;
  714. int max_n;
  715. bool found = false;
  716. /* approximately equals target * 0.00585 */
  717. int err_most = (target >> 8) + (target >> 9);
  718. memset(best_clock, 0, sizeof(*best_clock));
  719. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  720. max_n = limit->n.max;
  721. /* based on hardware requirement, prefer smaller n to precision */
  722. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  723. /* based on hardware requirement, prefere larger m1,m2 */
  724. for (clock.m1 = limit->m1.max;
  725. clock.m1 >= limit->m1.min; clock.m1--) {
  726. for (clock.m2 = limit->m2.max;
  727. clock.m2 >= limit->m2.min; clock.m2--) {
  728. for (clock.p1 = limit->p1.max;
  729. clock.p1 >= limit->p1.min; clock.p1--) {
  730. int this_err;
  731. i9xx_calc_dpll_params(refclk, &clock);
  732. if (!intel_PLL_is_valid(to_i915(dev),
  733. limit,
  734. &clock))
  735. continue;
  736. this_err = abs(clock.dot - target);
  737. if (this_err < err_most) {
  738. *best_clock = clock;
  739. err_most = this_err;
  740. max_n = clock.n;
  741. found = true;
  742. }
  743. }
  744. }
  745. }
  746. }
  747. return found;
  748. }
  749. /*
  750. * Check if the calculated PLL configuration is more optimal compared to the
  751. * best configuration and error found so far. Return the calculated error.
  752. */
  753. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  754. const struct dpll *calculated_clock,
  755. const struct dpll *best_clock,
  756. unsigned int best_error_ppm,
  757. unsigned int *error_ppm)
  758. {
  759. /*
  760. * For CHV ignore the error and consider only the P value.
  761. * Prefer a bigger P value based on HW requirements.
  762. */
  763. if (IS_CHERRYVIEW(to_i915(dev))) {
  764. *error_ppm = 0;
  765. return calculated_clock->p > best_clock->p;
  766. }
  767. if (WARN_ON_ONCE(!target_freq))
  768. return false;
  769. *error_ppm = div_u64(1000000ULL *
  770. abs(target_freq - calculated_clock->dot),
  771. target_freq);
  772. /*
  773. * Prefer a better P value over a better (smaller) error if the error
  774. * is small. Ensure this preference for future configurations too by
  775. * setting the error to 0.
  776. */
  777. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  778. *error_ppm = 0;
  779. return true;
  780. }
  781. return *error_ppm + 10 < best_error_ppm;
  782. }
  783. /*
  784. * Returns a set of divisors for the desired target clock with the given
  785. * refclk, or FALSE. The returned values represent the clock equation:
  786. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  787. */
  788. static bool
  789. vlv_find_best_dpll(const struct intel_limit *limit,
  790. struct intel_crtc_state *crtc_state,
  791. int target, int refclk, struct dpll *match_clock,
  792. struct dpll *best_clock)
  793. {
  794. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  795. struct drm_device *dev = crtc->base.dev;
  796. struct dpll clock;
  797. unsigned int bestppm = 1000000;
  798. /* min update 19.2 MHz */
  799. int max_n = min(limit->n.max, refclk / 19200);
  800. bool found = false;
  801. target *= 5; /* fast clock */
  802. memset(best_clock, 0, sizeof(*best_clock));
  803. /* based on hardware requirement, prefer smaller n to precision */
  804. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  805. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  806. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  807. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  808. clock.p = clock.p1 * clock.p2;
  809. /* based on hardware requirement, prefer bigger m1,m2 values */
  810. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  811. unsigned int ppm;
  812. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  813. refclk * clock.m1);
  814. vlv_calc_dpll_params(refclk, &clock);
  815. if (!intel_PLL_is_valid(to_i915(dev),
  816. limit,
  817. &clock))
  818. continue;
  819. if (!vlv_PLL_is_optimal(dev, target,
  820. &clock,
  821. best_clock,
  822. bestppm, &ppm))
  823. continue;
  824. *best_clock = clock;
  825. bestppm = ppm;
  826. found = true;
  827. }
  828. }
  829. }
  830. }
  831. return found;
  832. }
  833. /*
  834. * Returns a set of divisors for the desired target clock with the given
  835. * refclk, or FALSE. The returned values represent the clock equation:
  836. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  837. */
  838. static bool
  839. chv_find_best_dpll(const struct intel_limit *limit,
  840. struct intel_crtc_state *crtc_state,
  841. int target, int refclk, struct dpll *match_clock,
  842. struct dpll *best_clock)
  843. {
  844. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  845. struct drm_device *dev = crtc->base.dev;
  846. unsigned int best_error_ppm;
  847. struct dpll clock;
  848. uint64_t m2;
  849. int found = false;
  850. memset(best_clock, 0, sizeof(*best_clock));
  851. best_error_ppm = 1000000;
  852. /*
  853. * Based on hardware doc, the n always set to 1, and m1 always
  854. * set to 2. If requires to support 200Mhz refclk, we need to
  855. * revisit this because n may not 1 anymore.
  856. */
  857. clock.n = 1, clock.m1 = 2;
  858. target *= 5; /* fast clock */
  859. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  860. for (clock.p2 = limit->p2.p2_fast;
  861. clock.p2 >= limit->p2.p2_slow;
  862. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  863. unsigned int error_ppm;
  864. clock.p = clock.p1 * clock.p2;
  865. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  866. clock.n) << 22, refclk * clock.m1);
  867. if (m2 > INT_MAX/clock.m1)
  868. continue;
  869. clock.m2 = m2;
  870. chv_calc_dpll_params(refclk, &clock);
  871. if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
  872. continue;
  873. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  874. best_error_ppm, &error_ppm))
  875. continue;
  876. *best_clock = clock;
  877. best_error_ppm = error_ppm;
  878. found = true;
  879. }
  880. }
  881. return found;
  882. }
  883. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  884. struct dpll *best_clock)
  885. {
  886. int refclk = 100000;
  887. const struct intel_limit *limit = &intel_limits_bxt;
  888. return chv_find_best_dpll(limit, crtc_state,
  889. target_clock, refclk, NULL, best_clock);
  890. }
  891. bool intel_crtc_active(struct intel_crtc *crtc)
  892. {
  893. /* Be paranoid as we can arrive here with only partial
  894. * state retrieved from the hardware during setup.
  895. *
  896. * We can ditch the adjusted_mode.crtc_clock check as soon
  897. * as Haswell has gained clock readout/fastboot support.
  898. *
  899. * We can ditch the crtc->primary->fb check as soon as we can
  900. * properly reconstruct framebuffers.
  901. *
  902. * FIXME: The intel_crtc->active here should be switched to
  903. * crtc->state->active once we have proper CRTC states wired up
  904. * for atomic.
  905. */
  906. return crtc->active && crtc->base.primary->state->fb &&
  907. crtc->config->base.adjusted_mode.crtc_clock;
  908. }
  909. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  910. enum pipe pipe)
  911. {
  912. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  913. return crtc->config->cpu_transcoder;
  914. }
  915. static bool pipe_dsl_stopped(struct drm_i915_private *dev_priv, enum pipe pipe)
  916. {
  917. i915_reg_t reg = PIPEDSL(pipe);
  918. u32 line1, line2;
  919. u32 line_mask;
  920. if (IS_GEN2(dev_priv))
  921. line_mask = DSL_LINEMASK_GEN2;
  922. else
  923. line_mask = DSL_LINEMASK_GEN3;
  924. line1 = I915_READ(reg) & line_mask;
  925. msleep(5);
  926. line2 = I915_READ(reg) & line_mask;
  927. return line1 == line2;
  928. }
  929. /*
  930. * intel_wait_for_pipe_off - wait for pipe to turn off
  931. * @crtc: crtc whose pipe to wait for
  932. *
  933. * After disabling a pipe, we can't wait for vblank in the usual way,
  934. * spinning on the vblank interrupt status bit, since we won't actually
  935. * see an interrupt when the pipe is disabled.
  936. *
  937. * On Gen4 and above:
  938. * wait for the pipe register state bit to turn off
  939. *
  940. * Otherwise:
  941. * wait for the display line value to settle (it usually
  942. * ends up stopping at the start of the next frame).
  943. *
  944. */
  945. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  946. {
  947. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  948. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  949. enum pipe pipe = crtc->pipe;
  950. if (INTEL_GEN(dev_priv) >= 4) {
  951. i915_reg_t reg = PIPECONF(cpu_transcoder);
  952. /* Wait for the Pipe State to go off */
  953. if (intel_wait_for_register(dev_priv,
  954. reg, I965_PIPECONF_ACTIVE, 0,
  955. 100))
  956. WARN(1, "pipe_off wait timed out\n");
  957. } else {
  958. /* Wait for the display line to settle */
  959. if (wait_for(pipe_dsl_stopped(dev_priv, pipe), 100))
  960. WARN(1, "pipe_off wait timed out\n");
  961. }
  962. }
  963. /* Only for pre-ILK configs */
  964. void assert_pll(struct drm_i915_private *dev_priv,
  965. enum pipe pipe, bool state)
  966. {
  967. u32 val;
  968. bool cur_state;
  969. val = I915_READ(DPLL(pipe));
  970. cur_state = !!(val & DPLL_VCO_ENABLE);
  971. I915_STATE_WARN(cur_state != state,
  972. "PLL state assertion failure (expected %s, current %s)\n",
  973. onoff(state), onoff(cur_state));
  974. }
  975. /* XXX: the dsi pll is shared between MIPI DSI ports */
  976. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  977. {
  978. u32 val;
  979. bool cur_state;
  980. mutex_lock(&dev_priv->sb_lock);
  981. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  982. mutex_unlock(&dev_priv->sb_lock);
  983. cur_state = val & DSI_PLL_VCO_EN;
  984. I915_STATE_WARN(cur_state != state,
  985. "DSI PLL state assertion failure (expected %s, current %s)\n",
  986. onoff(state), onoff(cur_state));
  987. }
  988. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  989. enum pipe pipe, bool state)
  990. {
  991. bool cur_state;
  992. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  993. pipe);
  994. if (HAS_DDI(dev_priv)) {
  995. /* DDI does not have a specific FDI_TX register */
  996. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  997. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  998. } else {
  999. u32 val = I915_READ(FDI_TX_CTL(pipe));
  1000. cur_state = !!(val & FDI_TX_ENABLE);
  1001. }
  1002. I915_STATE_WARN(cur_state != state,
  1003. "FDI TX state assertion failure (expected %s, current %s)\n",
  1004. onoff(state), onoff(cur_state));
  1005. }
  1006. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1007. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1008. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1009. enum pipe pipe, bool state)
  1010. {
  1011. u32 val;
  1012. bool cur_state;
  1013. val = I915_READ(FDI_RX_CTL(pipe));
  1014. cur_state = !!(val & FDI_RX_ENABLE);
  1015. I915_STATE_WARN(cur_state != state,
  1016. "FDI RX state assertion failure (expected %s, current %s)\n",
  1017. onoff(state), onoff(cur_state));
  1018. }
  1019. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1020. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1021. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1022. enum pipe pipe)
  1023. {
  1024. u32 val;
  1025. /* ILK FDI PLL is always enabled */
  1026. if (IS_GEN5(dev_priv))
  1027. return;
  1028. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1029. if (HAS_DDI(dev_priv))
  1030. return;
  1031. val = I915_READ(FDI_TX_CTL(pipe));
  1032. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1033. }
  1034. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1035. enum pipe pipe, bool state)
  1036. {
  1037. u32 val;
  1038. bool cur_state;
  1039. val = I915_READ(FDI_RX_CTL(pipe));
  1040. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1041. I915_STATE_WARN(cur_state != state,
  1042. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1043. onoff(state), onoff(cur_state));
  1044. }
  1045. void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
  1046. {
  1047. i915_reg_t pp_reg;
  1048. u32 val;
  1049. enum pipe panel_pipe = PIPE_A;
  1050. bool locked = true;
  1051. if (WARN_ON(HAS_DDI(dev_priv)))
  1052. return;
  1053. if (HAS_PCH_SPLIT(dev_priv)) {
  1054. u32 port_sel;
  1055. pp_reg = PP_CONTROL(0);
  1056. port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
  1057. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1058. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1059. panel_pipe = PIPE_B;
  1060. /* XXX: else fix for eDP */
  1061. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1062. /* presumably write lock depends on pipe, not port select */
  1063. pp_reg = PP_CONTROL(pipe);
  1064. panel_pipe = pipe;
  1065. } else {
  1066. pp_reg = PP_CONTROL(0);
  1067. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1068. panel_pipe = PIPE_B;
  1069. }
  1070. val = I915_READ(pp_reg);
  1071. if (!(val & PANEL_POWER_ON) ||
  1072. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1073. locked = false;
  1074. I915_STATE_WARN(panel_pipe == pipe && locked,
  1075. "panel assertion failure, pipe %c regs locked\n",
  1076. pipe_name(pipe));
  1077. }
  1078. static void assert_cursor(struct drm_i915_private *dev_priv,
  1079. enum pipe pipe, bool state)
  1080. {
  1081. bool cur_state;
  1082. if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  1083. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1084. else
  1085. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1086. I915_STATE_WARN(cur_state != state,
  1087. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1088. pipe_name(pipe), onoff(state), onoff(cur_state));
  1089. }
  1090. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1091. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1092. void assert_pipe(struct drm_i915_private *dev_priv,
  1093. enum pipe pipe, bool state)
  1094. {
  1095. bool cur_state;
  1096. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1097. pipe);
  1098. enum intel_display_power_domain power_domain;
  1099. /* if we need the pipe quirk it must be always on */
  1100. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1101. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1102. state = true;
  1103. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1104. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1105. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1106. cur_state = !!(val & PIPECONF_ENABLE);
  1107. intel_display_power_put(dev_priv, power_domain);
  1108. } else {
  1109. cur_state = false;
  1110. }
  1111. I915_STATE_WARN(cur_state != state,
  1112. "pipe %c assertion failure (expected %s, current %s)\n",
  1113. pipe_name(pipe), onoff(state), onoff(cur_state));
  1114. }
  1115. static void assert_plane(struct drm_i915_private *dev_priv,
  1116. enum plane plane, bool state)
  1117. {
  1118. u32 val;
  1119. bool cur_state;
  1120. val = I915_READ(DSPCNTR(plane));
  1121. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1122. I915_STATE_WARN(cur_state != state,
  1123. "plane %c assertion failure (expected %s, current %s)\n",
  1124. plane_name(plane), onoff(state), onoff(cur_state));
  1125. }
  1126. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1127. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1128. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1129. enum pipe pipe)
  1130. {
  1131. int i;
  1132. /* Primary planes are fixed to pipes on gen4+ */
  1133. if (INTEL_GEN(dev_priv) >= 4) {
  1134. u32 val = I915_READ(DSPCNTR(pipe));
  1135. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1136. "plane %c assertion failure, should be disabled but not\n",
  1137. plane_name(pipe));
  1138. return;
  1139. }
  1140. /* Need to check both planes against the pipe */
  1141. for_each_pipe(dev_priv, i) {
  1142. u32 val = I915_READ(DSPCNTR(i));
  1143. enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1144. DISPPLANE_SEL_PIPE_SHIFT;
  1145. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1146. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1147. plane_name(i), pipe_name(pipe));
  1148. }
  1149. }
  1150. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1151. enum pipe pipe)
  1152. {
  1153. int sprite;
  1154. if (INTEL_GEN(dev_priv) >= 9) {
  1155. for_each_sprite(dev_priv, pipe, sprite) {
  1156. u32 val = I915_READ(PLANE_CTL(pipe, sprite));
  1157. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1158. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1159. sprite, pipe_name(pipe));
  1160. }
  1161. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1162. for_each_sprite(dev_priv, pipe, sprite) {
  1163. u32 val = I915_READ(SPCNTR(pipe, PLANE_SPRITE0 + sprite));
  1164. I915_STATE_WARN(val & SP_ENABLE,
  1165. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1166. sprite_name(pipe, sprite), pipe_name(pipe));
  1167. }
  1168. } else if (INTEL_GEN(dev_priv) >= 7) {
  1169. u32 val = I915_READ(SPRCTL(pipe));
  1170. I915_STATE_WARN(val & SPRITE_ENABLE,
  1171. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1172. plane_name(pipe), pipe_name(pipe));
  1173. } else if (INTEL_GEN(dev_priv) >= 5) {
  1174. u32 val = I915_READ(DVSCNTR(pipe));
  1175. I915_STATE_WARN(val & DVS_ENABLE,
  1176. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1177. plane_name(pipe), pipe_name(pipe));
  1178. }
  1179. }
  1180. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1181. {
  1182. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1183. drm_crtc_vblank_put(crtc);
  1184. }
  1185. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1186. enum pipe pipe)
  1187. {
  1188. u32 val;
  1189. bool enabled;
  1190. val = I915_READ(PCH_TRANSCONF(pipe));
  1191. enabled = !!(val & TRANS_ENABLE);
  1192. I915_STATE_WARN(enabled,
  1193. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1194. pipe_name(pipe));
  1195. }
  1196. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1197. enum pipe pipe, u32 port_sel, u32 val)
  1198. {
  1199. if ((val & DP_PORT_EN) == 0)
  1200. return false;
  1201. if (HAS_PCH_CPT(dev_priv)) {
  1202. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1203. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1204. return false;
  1205. } else if (IS_CHERRYVIEW(dev_priv)) {
  1206. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1207. return false;
  1208. } else {
  1209. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1210. return false;
  1211. }
  1212. return true;
  1213. }
  1214. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1215. enum pipe pipe, u32 val)
  1216. {
  1217. if ((val & SDVO_ENABLE) == 0)
  1218. return false;
  1219. if (HAS_PCH_CPT(dev_priv)) {
  1220. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1221. return false;
  1222. } else if (IS_CHERRYVIEW(dev_priv)) {
  1223. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1224. return false;
  1225. } else {
  1226. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1227. return false;
  1228. }
  1229. return true;
  1230. }
  1231. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1232. enum pipe pipe, u32 val)
  1233. {
  1234. if ((val & LVDS_PORT_EN) == 0)
  1235. return false;
  1236. if (HAS_PCH_CPT(dev_priv)) {
  1237. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1238. return false;
  1239. } else {
  1240. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1241. return false;
  1242. }
  1243. return true;
  1244. }
  1245. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1246. enum pipe pipe, u32 val)
  1247. {
  1248. if ((val & ADPA_DAC_ENABLE) == 0)
  1249. return false;
  1250. if (HAS_PCH_CPT(dev_priv)) {
  1251. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1252. return false;
  1253. } else {
  1254. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1255. return false;
  1256. }
  1257. return true;
  1258. }
  1259. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1260. enum pipe pipe, i915_reg_t reg,
  1261. u32 port_sel)
  1262. {
  1263. u32 val = I915_READ(reg);
  1264. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1265. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1266. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1267. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
  1268. && (val & DP_PIPEB_SELECT),
  1269. "IBX PCH dp port still using transcoder B\n");
  1270. }
  1271. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1272. enum pipe pipe, i915_reg_t reg)
  1273. {
  1274. u32 val = I915_READ(reg);
  1275. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1276. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1277. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1278. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
  1279. && (val & SDVO_PIPE_B_SELECT),
  1280. "IBX PCH hdmi port still using transcoder B\n");
  1281. }
  1282. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1283. enum pipe pipe)
  1284. {
  1285. u32 val;
  1286. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1287. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1288. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1289. val = I915_READ(PCH_ADPA);
  1290. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1291. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1292. pipe_name(pipe));
  1293. val = I915_READ(PCH_LVDS);
  1294. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1295. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1296. pipe_name(pipe));
  1297. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1298. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1299. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1300. }
  1301. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1302. const struct intel_crtc_state *pipe_config)
  1303. {
  1304. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1305. enum pipe pipe = crtc->pipe;
  1306. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1307. POSTING_READ(DPLL(pipe));
  1308. udelay(150);
  1309. if (intel_wait_for_register(dev_priv,
  1310. DPLL(pipe),
  1311. DPLL_LOCK_VLV,
  1312. DPLL_LOCK_VLV,
  1313. 1))
  1314. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1315. }
  1316. static void vlv_enable_pll(struct intel_crtc *crtc,
  1317. const struct intel_crtc_state *pipe_config)
  1318. {
  1319. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1320. enum pipe pipe = crtc->pipe;
  1321. assert_pipe_disabled(dev_priv, pipe);
  1322. /* PLL is protected by panel, make sure we can write it */
  1323. assert_panel_unlocked(dev_priv, pipe);
  1324. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1325. _vlv_enable_pll(crtc, pipe_config);
  1326. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1327. POSTING_READ(DPLL_MD(pipe));
  1328. }
  1329. static void _chv_enable_pll(struct intel_crtc *crtc,
  1330. const struct intel_crtc_state *pipe_config)
  1331. {
  1332. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1333. enum pipe pipe = crtc->pipe;
  1334. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1335. u32 tmp;
  1336. mutex_lock(&dev_priv->sb_lock);
  1337. /* Enable back the 10bit clock to display controller */
  1338. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1339. tmp |= DPIO_DCLKP_EN;
  1340. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1341. mutex_unlock(&dev_priv->sb_lock);
  1342. /*
  1343. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1344. */
  1345. udelay(1);
  1346. /* Enable PLL */
  1347. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1348. /* Check PLL is locked */
  1349. if (intel_wait_for_register(dev_priv,
  1350. DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
  1351. 1))
  1352. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1353. }
  1354. static void chv_enable_pll(struct intel_crtc *crtc,
  1355. const struct intel_crtc_state *pipe_config)
  1356. {
  1357. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1358. enum pipe pipe = crtc->pipe;
  1359. assert_pipe_disabled(dev_priv, pipe);
  1360. /* PLL is protected by panel, make sure we can write it */
  1361. assert_panel_unlocked(dev_priv, pipe);
  1362. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1363. _chv_enable_pll(crtc, pipe_config);
  1364. if (pipe != PIPE_A) {
  1365. /*
  1366. * WaPixelRepeatModeFixForC0:chv
  1367. *
  1368. * DPLLCMD is AWOL. Use chicken bits to propagate
  1369. * the value from DPLLBMD to either pipe B or C.
  1370. */
  1371. I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
  1372. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1373. I915_WRITE(CBR4_VLV, 0);
  1374. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1375. /*
  1376. * DPLLB VGA mode also seems to cause problems.
  1377. * We should always have it disabled.
  1378. */
  1379. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1380. } else {
  1381. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1382. POSTING_READ(DPLL_MD(pipe));
  1383. }
  1384. }
  1385. static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
  1386. {
  1387. struct intel_crtc *crtc;
  1388. int count = 0;
  1389. for_each_intel_crtc(&dev_priv->drm, crtc) {
  1390. count += crtc->base.state->active &&
  1391. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
  1392. }
  1393. return count;
  1394. }
  1395. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1396. {
  1397. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1398. i915_reg_t reg = DPLL(crtc->pipe);
  1399. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1400. assert_pipe_disabled(dev_priv, crtc->pipe);
  1401. /* PLL is protected by panel, make sure we can write it */
  1402. if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
  1403. assert_panel_unlocked(dev_priv, crtc->pipe);
  1404. /* Enable DVO 2x clock on both PLLs if necessary */
  1405. if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
  1406. /*
  1407. * It appears to be important that we don't enable this
  1408. * for the current pipe before otherwise configuring the
  1409. * PLL. No idea how this should be handled if multiple
  1410. * DVO outputs are enabled simultaneosly.
  1411. */
  1412. dpll |= DPLL_DVO_2X_MODE;
  1413. I915_WRITE(DPLL(!crtc->pipe),
  1414. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1415. }
  1416. /*
  1417. * Apparently we need to have VGA mode enabled prior to changing
  1418. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1419. * dividers, even though the register value does change.
  1420. */
  1421. I915_WRITE(reg, 0);
  1422. I915_WRITE(reg, dpll);
  1423. /* Wait for the clocks to stabilize. */
  1424. POSTING_READ(reg);
  1425. udelay(150);
  1426. if (INTEL_GEN(dev_priv) >= 4) {
  1427. I915_WRITE(DPLL_MD(crtc->pipe),
  1428. crtc->config->dpll_hw_state.dpll_md);
  1429. } else {
  1430. /* The pixel multiplier can only be updated once the
  1431. * DPLL is enabled and the clocks are stable.
  1432. *
  1433. * So write it again.
  1434. */
  1435. I915_WRITE(reg, dpll);
  1436. }
  1437. /* We do this three times for luck */
  1438. I915_WRITE(reg, dpll);
  1439. POSTING_READ(reg);
  1440. udelay(150); /* wait for warmup */
  1441. I915_WRITE(reg, dpll);
  1442. POSTING_READ(reg);
  1443. udelay(150); /* wait for warmup */
  1444. I915_WRITE(reg, dpll);
  1445. POSTING_READ(reg);
  1446. udelay(150); /* wait for warmup */
  1447. }
  1448. /**
  1449. * i9xx_disable_pll - disable a PLL
  1450. * @dev_priv: i915 private structure
  1451. * @pipe: pipe PLL to disable
  1452. *
  1453. * Disable the PLL for @pipe, making sure the pipe is off first.
  1454. *
  1455. * Note! This is for pre-ILK only.
  1456. */
  1457. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1458. {
  1459. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1460. enum pipe pipe = crtc->pipe;
  1461. /* Disable DVO 2x clock on both PLLs if necessary */
  1462. if (IS_I830(dev_priv) &&
  1463. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
  1464. !intel_num_dvo_pipes(dev_priv)) {
  1465. I915_WRITE(DPLL(PIPE_B),
  1466. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1467. I915_WRITE(DPLL(PIPE_A),
  1468. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1469. }
  1470. /* Don't disable pipe or pipe PLLs if needed */
  1471. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1472. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1473. return;
  1474. /* Make sure the pipe isn't still relying on us */
  1475. assert_pipe_disabled(dev_priv, pipe);
  1476. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1477. POSTING_READ(DPLL(pipe));
  1478. }
  1479. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1480. {
  1481. u32 val;
  1482. /* Make sure the pipe isn't still relying on us */
  1483. assert_pipe_disabled(dev_priv, pipe);
  1484. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1485. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1486. if (pipe != PIPE_A)
  1487. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1488. I915_WRITE(DPLL(pipe), val);
  1489. POSTING_READ(DPLL(pipe));
  1490. }
  1491. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1492. {
  1493. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1494. u32 val;
  1495. /* Make sure the pipe isn't still relying on us */
  1496. assert_pipe_disabled(dev_priv, pipe);
  1497. val = DPLL_SSC_REF_CLK_CHV |
  1498. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1499. if (pipe != PIPE_A)
  1500. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1501. I915_WRITE(DPLL(pipe), val);
  1502. POSTING_READ(DPLL(pipe));
  1503. mutex_lock(&dev_priv->sb_lock);
  1504. /* Disable 10bit clock to display controller */
  1505. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1506. val &= ~DPIO_DCLKP_EN;
  1507. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1508. mutex_unlock(&dev_priv->sb_lock);
  1509. }
  1510. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1511. struct intel_digital_port *dport,
  1512. unsigned int expected_mask)
  1513. {
  1514. u32 port_mask;
  1515. i915_reg_t dpll_reg;
  1516. switch (dport->port) {
  1517. case PORT_B:
  1518. port_mask = DPLL_PORTB_READY_MASK;
  1519. dpll_reg = DPLL(0);
  1520. break;
  1521. case PORT_C:
  1522. port_mask = DPLL_PORTC_READY_MASK;
  1523. dpll_reg = DPLL(0);
  1524. expected_mask <<= 4;
  1525. break;
  1526. case PORT_D:
  1527. port_mask = DPLL_PORTD_READY_MASK;
  1528. dpll_reg = DPIO_PHY_STATUS;
  1529. break;
  1530. default:
  1531. BUG();
  1532. }
  1533. if (intel_wait_for_register(dev_priv,
  1534. dpll_reg, port_mask, expected_mask,
  1535. 1000))
  1536. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1537. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1538. }
  1539. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1540. enum pipe pipe)
  1541. {
  1542. struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
  1543. pipe);
  1544. i915_reg_t reg;
  1545. uint32_t val, pipeconf_val;
  1546. /* Make sure PCH DPLL is enabled */
  1547. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1548. /* FDI must be feeding us bits for PCH ports */
  1549. assert_fdi_tx_enabled(dev_priv, pipe);
  1550. assert_fdi_rx_enabled(dev_priv, pipe);
  1551. if (HAS_PCH_CPT(dev_priv)) {
  1552. /* Workaround: Set the timing override bit before enabling the
  1553. * pch transcoder. */
  1554. reg = TRANS_CHICKEN2(pipe);
  1555. val = I915_READ(reg);
  1556. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1557. I915_WRITE(reg, val);
  1558. }
  1559. reg = PCH_TRANSCONF(pipe);
  1560. val = I915_READ(reg);
  1561. pipeconf_val = I915_READ(PIPECONF(pipe));
  1562. if (HAS_PCH_IBX(dev_priv)) {
  1563. /*
  1564. * Make the BPC in transcoder be consistent with
  1565. * that in pipeconf reg. For HDMI we must use 8bpc
  1566. * here for both 8bpc and 12bpc.
  1567. */
  1568. val &= ~PIPECONF_BPC_MASK;
  1569. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
  1570. val |= PIPECONF_8BPC;
  1571. else
  1572. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1573. }
  1574. val &= ~TRANS_INTERLACE_MASK;
  1575. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1576. if (HAS_PCH_IBX(dev_priv) &&
  1577. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  1578. val |= TRANS_LEGACY_INTERLACED_ILK;
  1579. else
  1580. val |= TRANS_INTERLACED;
  1581. else
  1582. val |= TRANS_PROGRESSIVE;
  1583. I915_WRITE(reg, val | TRANS_ENABLE);
  1584. if (intel_wait_for_register(dev_priv,
  1585. reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
  1586. 100))
  1587. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1588. }
  1589. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1590. enum transcoder cpu_transcoder)
  1591. {
  1592. u32 val, pipeconf_val;
  1593. /* FDI must be feeding us bits for PCH ports */
  1594. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1595. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1596. /* Workaround: set timing override bit. */
  1597. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1598. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1599. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1600. val = TRANS_ENABLE;
  1601. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1602. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1603. PIPECONF_INTERLACED_ILK)
  1604. val |= TRANS_INTERLACED;
  1605. else
  1606. val |= TRANS_PROGRESSIVE;
  1607. I915_WRITE(LPT_TRANSCONF, val);
  1608. if (intel_wait_for_register(dev_priv,
  1609. LPT_TRANSCONF,
  1610. TRANS_STATE_ENABLE,
  1611. TRANS_STATE_ENABLE,
  1612. 100))
  1613. DRM_ERROR("Failed to enable PCH transcoder\n");
  1614. }
  1615. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1616. enum pipe pipe)
  1617. {
  1618. i915_reg_t reg;
  1619. uint32_t val;
  1620. /* FDI relies on the transcoder */
  1621. assert_fdi_tx_disabled(dev_priv, pipe);
  1622. assert_fdi_rx_disabled(dev_priv, pipe);
  1623. /* Ports must be off as well */
  1624. assert_pch_ports_disabled(dev_priv, pipe);
  1625. reg = PCH_TRANSCONF(pipe);
  1626. val = I915_READ(reg);
  1627. val &= ~TRANS_ENABLE;
  1628. I915_WRITE(reg, val);
  1629. /* wait for PCH transcoder off, transcoder state */
  1630. if (intel_wait_for_register(dev_priv,
  1631. reg, TRANS_STATE_ENABLE, 0,
  1632. 50))
  1633. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1634. if (HAS_PCH_CPT(dev_priv)) {
  1635. /* Workaround: Clear the timing override chicken bit again. */
  1636. reg = TRANS_CHICKEN2(pipe);
  1637. val = I915_READ(reg);
  1638. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1639. I915_WRITE(reg, val);
  1640. }
  1641. }
  1642. void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1643. {
  1644. u32 val;
  1645. val = I915_READ(LPT_TRANSCONF);
  1646. val &= ~TRANS_ENABLE;
  1647. I915_WRITE(LPT_TRANSCONF, val);
  1648. /* wait for PCH transcoder off, transcoder state */
  1649. if (intel_wait_for_register(dev_priv,
  1650. LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
  1651. 50))
  1652. DRM_ERROR("Failed to disable PCH transcoder\n");
  1653. /* Workaround: clear timing override bit. */
  1654. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1655. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1656. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1657. }
  1658. enum transcoder intel_crtc_pch_transcoder(struct intel_crtc *crtc)
  1659. {
  1660. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1661. WARN_ON(!crtc->config->has_pch_encoder);
  1662. if (HAS_PCH_LPT(dev_priv))
  1663. return TRANSCODER_A;
  1664. else
  1665. return (enum transcoder) crtc->pipe;
  1666. }
  1667. /**
  1668. * intel_enable_pipe - enable a pipe, asserting requirements
  1669. * @crtc: crtc responsible for the pipe
  1670. *
  1671. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1672. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1673. */
  1674. static void intel_enable_pipe(struct intel_crtc *crtc)
  1675. {
  1676. struct drm_device *dev = crtc->base.dev;
  1677. struct drm_i915_private *dev_priv = to_i915(dev);
  1678. enum pipe pipe = crtc->pipe;
  1679. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1680. i915_reg_t reg;
  1681. u32 val;
  1682. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1683. assert_planes_disabled(dev_priv, pipe);
  1684. assert_cursor_disabled(dev_priv, pipe);
  1685. assert_sprites_disabled(dev_priv, pipe);
  1686. /*
  1687. * A pipe without a PLL won't actually be able to drive bits from
  1688. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1689. * need the check.
  1690. */
  1691. if (HAS_GMCH_DISPLAY(dev_priv)) {
  1692. if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
  1693. assert_dsi_pll_enabled(dev_priv);
  1694. else
  1695. assert_pll_enabled(dev_priv, pipe);
  1696. } else {
  1697. if (crtc->config->has_pch_encoder) {
  1698. /* if driving the PCH, we need FDI enabled */
  1699. assert_fdi_rx_pll_enabled(dev_priv,
  1700. (enum pipe) intel_crtc_pch_transcoder(crtc));
  1701. assert_fdi_tx_pll_enabled(dev_priv,
  1702. (enum pipe) cpu_transcoder);
  1703. }
  1704. /* FIXME: assert CPU port conditions for SNB+ */
  1705. }
  1706. reg = PIPECONF(cpu_transcoder);
  1707. val = I915_READ(reg);
  1708. if (val & PIPECONF_ENABLE) {
  1709. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1710. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1711. return;
  1712. }
  1713. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1714. POSTING_READ(reg);
  1715. /*
  1716. * Until the pipe starts DSL will read as 0, which would cause
  1717. * an apparent vblank timestamp jump, which messes up also the
  1718. * frame count when it's derived from the timestamps. So let's
  1719. * wait for the pipe to start properly before we call
  1720. * drm_crtc_vblank_on()
  1721. */
  1722. if (dev->max_vblank_count == 0 &&
  1723. wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
  1724. DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
  1725. }
  1726. /**
  1727. * intel_disable_pipe - disable a pipe, asserting requirements
  1728. * @crtc: crtc whose pipes is to be disabled
  1729. *
  1730. * Disable the pipe of @crtc, making sure that various hardware
  1731. * specific requirements are met, if applicable, e.g. plane
  1732. * disabled, panel fitter off, etc.
  1733. *
  1734. * Will wait until the pipe has shut down before returning.
  1735. */
  1736. static void intel_disable_pipe(struct intel_crtc *crtc)
  1737. {
  1738. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1739. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1740. enum pipe pipe = crtc->pipe;
  1741. i915_reg_t reg;
  1742. u32 val;
  1743. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1744. /*
  1745. * Make sure planes won't keep trying to pump pixels to us,
  1746. * or we might hang the display.
  1747. */
  1748. assert_planes_disabled(dev_priv, pipe);
  1749. assert_cursor_disabled(dev_priv, pipe);
  1750. assert_sprites_disabled(dev_priv, pipe);
  1751. reg = PIPECONF(cpu_transcoder);
  1752. val = I915_READ(reg);
  1753. if ((val & PIPECONF_ENABLE) == 0)
  1754. return;
  1755. /*
  1756. * Double wide has implications for planes
  1757. * so best keep it disabled when not needed.
  1758. */
  1759. if (crtc->config->double_wide)
  1760. val &= ~PIPECONF_DOUBLE_WIDE;
  1761. /* Don't disable pipe or pipe PLLs if needed */
  1762. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1763. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1764. val &= ~PIPECONF_ENABLE;
  1765. I915_WRITE(reg, val);
  1766. if ((val & PIPECONF_ENABLE) == 0)
  1767. intel_wait_for_pipe_off(crtc);
  1768. }
  1769. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1770. {
  1771. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1772. }
  1773. static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
  1774. uint64_t fb_modifier, unsigned int cpp)
  1775. {
  1776. switch (fb_modifier) {
  1777. case DRM_FORMAT_MOD_NONE:
  1778. return cpp;
  1779. case I915_FORMAT_MOD_X_TILED:
  1780. if (IS_GEN2(dev_priv))
  1781. return 128;
  1782. else
  1783. return 512;
  1784. case I915_FORMAT_MOD_Y_TILED:
  1785. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1786. return 128;
  1787. else
  1788. return 512;
  1789. case I915_FORMAT_MOD_Yf_TILED:
  1790. switch (cpp) {
  1791. case 1:
  1792. return 64;
  1793. case 2:
  1794. case 4:
  1795. return 128;
  1796. case 8:
  1797. case 16:
  1798. return 256;
  1799. default:
  1800. MISSING_CASE(cpp);
  1801. return cpp;
  1802. }
  1803. break;
  1804. default:
  1805. MISSING_CASE(fb_modifier);
  1806. return cpp;
  1807. }
  1808. }
  1809. unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
  1810. uint64_t fb_modifier, unsigned int cpp)
  1811. {
  1812. if (fb_modifier == DRM_FORMAT_MOD_NONE)
  1813. return 1;
  1814. else
  1815. return intel_tile_size(dev_priv) /
  1816. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1817. }
  1818. /* Return the tile dimensions in pixel units */
  1819. static void intel_tile_dims(const struct drm_i915_private *dev_priv,
  1820. unsigned int *tile_width,
  1821. unsigned int *tile_height,
  1822. uint64_t fb_modifier,
  1823. unsigned int cpp)
  1824. {
  1825. unsigned int tile_width_bytes =
  1826. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1827. *tile_width = tile_width_bytes / cpp;
  1828. *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
  1829. }
  1830. unsigned int
  1831. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1832. uint32_t pixel_format, uint64_t fb_modifier)
  1833. {
  1834. unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
  1835. unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
  1836. return ALIGN(height, tile_height);
  1837. }
  1838. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1839. {
  1840. unsigned int size = 0;
  1841. int i;
  1842. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1843. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1844. return size;
  1845. }
  1846. static void
  1847. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1848. const struct drm_framebuffer *fb,
  1849. unsigned int rotation)
  1850. {
  1851. view->type = I915_GGTT_VIEW_NORMAL;
  1852. if (drm_rotation_90_or_270(rotation)) {
  1853. view->type = I915_GGTT_VIEW_ROTATED;
  1854. view->rotated = to_intel_framebuffer(fb)->rot_info;
  1855. }
  1856. }
  1857. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1858. {
  1859. if (INTEL_INFO(dev_priv)->gen >= 9)
  1860. return 256 * 1024;
  1861. else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
  1862. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1863. return 128 * 1024;
  1864. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1865. return 4 * 1024;
  1866. else
  1867. return 0;
  1868. }
  1869. static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
  1870. uint64_t fb_modifier)
  1871. {
  1872. switch (fb_modifier) {
  1873. case DRM_FORMAT_MOD_NONE:
  1874. return intel_linear_alignment(dev_priv);
  1875. case I915_FORMAT_MOD_X_TILED:
  1876. if (INTEL_INFO(dev_priv)->gen >= 9)
  1877. return 256 * 1024;
  1878. return 0;
  1879. case I915_FORMAT_MOD_Y_TILED:
  1880. case I915_FORMAT_MOD_Yf_TILED:
  1881. return 1 * 1024 * 1024;
  1882. default:
  1883. MISSING_CASE(fb_modifier);
  1884. return 0;
  1885. }
  1886. }
  1887. struct i915_vma *
  1888. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1889. {
  1890. struct drm_device *dev = fb->dev;
  1891. struct drm_i915_private *dev_priv = to_i915(dev);
  1892. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1893. struct i915_ggtt_view view;
  1894. struct i915_vma *vma;
  1895. u32 alignment;
  1896. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1897. alignment = intel_surf_alignment(dev_priv, fb->modifier);
  1898. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1899. /* Note that the w/a also requires 64 PTE of padding following the
  1900. * bo. We currently fill all unused PTE with the shadow page and so
  1901. * we should always have valid PTE following the scanout preventing
  1902. * the VT-d warning.
  1903. */
  1904. if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
  1905. alignment = 256 * 1024;
  1906. /*
  1907. * Global gtt pte registers are special registers which actually forward
  1908. * writes to a chunk of system memory. Which means that there is no risk
  1909. * that the register values disappear as soon as we call
  1910. * intel_runtime_pm_put(), so it is correct to wrap only the
  1911. * pin/unpin/fence and not more.
  1912. */
  1913. intel_runtime_pm_get(dev_priv);
  1914. vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
  1915. if (IS_ERR(vma))
  1916. goto err;
  1917. if (i915_vma_is_map_and_fenceable(vma)) {
  1918. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1919. * fence, whereas 965+ only requires a fence if using
  1920. * framebuffer compression. For simplicity, we always, when
  1921. * possible, install a fence as the cost is not that onerous.
  1922. *
  1923. * If we fail to fence the tiled scanout, then either the
  1924. * modeset will reject the change (which is highly unlikely as
  1925. * the affected systems, all but one, do not have unmappable
  1926. * space) or we will not be able to enable full powersaving
  1927. * techniques (also likely not to apply due to various limits
  1928. * FBC and the like impose on the size of the buffer, which
  1929. * presumably we violated anyway with this unmappable buffer).
  1930. * Anyway, it is presumably better to stumble onwards with
  1931. * something and try to run the system in a "less than optimal"
  1932. * mode that matches the user configuration.
  1933. */
  1934. if (i915_vma_get_fence(vma) == 0)
  1935. i915_vma_pin_fence(vma);
  1936. }
  1937. i915_vma_get(vma);
  1938. err:
  1939. intel_runtime_pm_put(dev_priv);
  1940. return vma;
  1941. }
  1942. void intel_unpin_fb_vma(struct i915_vma *vma)
  1943. {
  1944. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  1945. i915_vma_unpin_fence(vma);
  1946. i915_gem_object_unpin_from_display_plane(vma);
  1947. i915_vma_put(vma);
  1948. }
  1949. static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
  1950. unsigned int rotation)
  1951. {
  1952. if (drm_rotation_90_or_270(rotation))
  1953. return to_intel_framebuffer(fb)->rotated[plane].pitch;
  1954. else
  1955. return fb->pitches[plane];
  1956. }
  1957. /*
  1958. * Convert the x/y offsets into a linear offset.
  1959. * Only valid with 0/180 degree rotation, which is fine since linear
  1960. * offset is only used with linear buffers on pre-hsw and tiled buffers
  1961. * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
  1962. */
  1963. u32 intel_fb_xy_to_linear(int x, int y,
  1964. const struct intel_plane_state *state,
  1965. int plane)
  1966. {
  1967. const struct drm_framebuffer *fb = state->base.fb;
  1968. unsigned int cpp = fb->format->cpp[plane];
  1969. unsigned int pitch = fb->pitches[plane];
  1970. return y * pitch + x * cpp;
  1971. }
  1972. /*
  1973. * Add the x/y offsets derived from fb->offsets[] to the user
  1974. * specified plane src x/y offsets. The resulting x/y offsets
  1975. * specify the start of scanout from the beginning of the gtt mapping.
  1976. */
  1977. void intel_add_fb_offsets(int *x, int *y,
  1978. const struct intel_plane_state *state,
  1979. int plane)
  1980. {
  1981. const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
  1982. unsigned int rotation = state->base.rotation;
  1983. if (drm_rotation_90_or_270(rotation)) {
  1984. *x += intel_fb->rotated[plane].x;
  1985. *y += intel_fb->rotated[plane].y;
  1986. } else {
  1987. *x += intel_fb->normal[plane].x;
  1988. *y += intel_fb->normal[plane].y;
  1989. }
  1990. }
  1991. /*
  1992. * Input tile dimensions and pitch must already be
  1993. * rotated to match x and y, and in pixel units.
  1994. */
  1995. static u32 _intel_adjust_tile_offset(int *x, int *y,
  1996. unsigned int tile_width,
  1997. unsigned int tile_height,
  1998. unsigned int tile_size,
  1999. unsigned int pitch_tiles,
  2000. u32 old_offset,
  2001. u32 new_offset)
  2002. {
  2003. unsigned int pitch_pixels = pitch_tiles * tile_width;
  2004. unsigned int tiles;
  2005. WARN_ON(old_offset & (tile_size - 1));
  2006. WARN_ON(new_offset & (tile_size - 1));
  2007. WARN_ON(new_offset > old_offset);
  2008. tiles = (old_offset - new_offset) / tile_size;
  2009. *y += tiles / pitch_tiles * tile_height;
  2010. *x += tiles % pitch_tiles * tile_width;
  2011. /* minimize x in case it got needlessly big */
  2012. *y += *x / pitch_pixels * tile_height;
  2013. *x %= pitch_pixels;
  2014. return new_offset;
  2015. }
  2016. /*
  2017. * Adjust the tile offset by moving the difference into
  2018. * the x/y offsets.
  2019. */
  2020. static u32 intel_adjust_tile_offset(int *x, int *y,
  2021. const struct intel_plane_state *state, int plane,
  2022. u32 old_offset, u32 new_offset)
  2023. {
  2024. const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
  2025. const struct drm_framebuffer *fb = state->base.fb;
  2026. unsigned int cpp = fb->format->cpp[plane];
  2027. unsigned int rotation = state->base.rotation;
  2028. unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
  2029. WARN_ON(new_offset > old_offset);
  2030. if (fb->modifier != DRM_FORMAT_MOD_NONE) {
  2031. unsigned int tile_size, tile_width, tile_height;
  2032. unsigned int pitch_tiles;
  2033. tile_size = intel_tile_size(dev_priv);
  2034. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2035. fb->modifier, cpp);
  2036. if (drm_rotation_90_or_270(rotation)) {
  2037. pitch_tiles = pitch / tile_height;
  2038. swap(tile_width, tile_height);
  2039. } else {
  2040. pitch_tiles = pitch / (tile_width * cpp);
  2041. }
  2042. _intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2043. tile_size, pitch_tiles,
  2044. old_offset, new_offset);
  2045. } else {
  2046. old_offset += *y * pitch + *x * cpp;
  2047. *y = (old_offset - new_offset) / pitch;
  2048. *x = ((old_offset - new_offset) - *y * pitch) / cpp;
  2049. }
  2050. return new_offset;
  2051. }
  2052. /*
  2053. * Computes the linear offset to the base tile and adjusts
  2054. * x, y. bytes per pixel is assumed to be a power-of-two.
  2055. *
  2056. * In the 90/270 rotated case, x and y are assumed
  2057. * to be already rotated to match the rotated GTT view, and
  2058. * pitch is the tile_height aligned framebuffer height.
  2059. *
  2060. * This function is used when computing the derived information
  2061. * under intel_framebuffer, so using any of that information
  2062. * here is not allowed. Anything under drm_framebuffer can be
  2063. * used. This is why the user has to pass in the pitch since it
  2064. * is specified in the rotated orientation.
  2065. */
  2066. static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
  2067. int *x, int *y,
  2068. const struct drm_framebuffer *fb, int plane,
  2069. unsigned int pitch,
  2070. unsigned int rotation,
  2071. u32 alignment)
  2072. {
  2073. uint64_t fb_modifier = fb->modifier;
  2074. unsigned int cpp = fb->format->cpp[plane];
  2075. u32 offset, offset_aligned;
  2076. if (alignment)
  2077. alignment--;
  2078. if (fb_modifier != DRM_FORMAT_MOD_NONE) {
  2079. unsigned int tile_size, tile_width, tile_height;
  2080. unsigned int tile_rows, tiles, pitch_tiles;
  2081. tile_size = intel_tile_size(dev_priv);
  2082. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2083. fb_modifier, cpp);
  2084. if (drm_rotation_90_or_270(rotation)) {
  2085. pitch_tiles = pitch / tile_height;
  2086. swap(tile_width, tile_height);
  2087. } else {
  2088. pitch_tiles = pitch / (tile_width * cpp);
  2089. }
  2090. tile_rows = *y / tile_height;
  2091. *y %= tile_height;
  2092. tiles = *x / tile_width;
  2093. *x %= tile_width;
  2094. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  2095. offset_aligned = offset & ~alignment;
  2096. _intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2097. tile_size, pitch_tiles,
  2098. offset, offset_aligned);
  2099. } else {
  2100. offset = *y * pitch + *x * cpp;
  2101. offset_aligned = offset & ~alignment;
  2102. *y = (offset & alignment) / pitch;
  2103. *x = ((offset & alignment) - *y * pitch) / cpp;
  2104. }
  2105. return offset_aligned;
  2106. }
  2107. u32 intel_compute_tile_offset(int *x, int *y,
  2108. const struct intel_plane_state *state,
  2109. int plane)
  2110. {
  2111. const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
  2112. const struct drm_framebuffer *fb = state->base.fb;
  2113. unsigned int rotation = state->base.rotation;
  2114. int pitch = intel_fb_pitch(fb, plane, rotation);
  2115. u32 alignment;
  2116. /* AUX_DIST needs only 4K alignment */
  2117. if (fb->format->format == DRM_FORMAT_NV12 && plane == 1)
  2118. alignment = 4096;
  2119. else
  2120. alignment = intel_surf_alignment(dev_priv, fb->modifier);
  2121. return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
  2122. rotation, alignment);
  2123. }
  2124. /* Convert the fb->offset[] linear offset into x/y offsets */
  2125. static void intel_fb_offset_to_xy(int *x, int *y,
  2126. const struct drm_framebuffer *fb, int plane)
  2127. {
  2128. unsigned int cpp = fb->format->cpp[plane];
  2129. unsigned int pitch = fb->pitches[plane];
  2130. u32 linear_offset = fb->offsets[plane];
  2131. *y = linear_offset / pitch;
  2132. *x = linear_offset % pitch / cpp;
  2133. }
  2134. static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
  2135. {
  2136. switch (fb_modifier) {
  2137. case I915_FORMAT_MOD_X_TILED:
  2138. return I915_TILING_X;
  2139. case I915_FORMAT_MOD_Y_TILED:
  2140. return I915_TILING_Y;
  2141. default:
  2142. return I915_TILING_NONE;
  2143. }
  2144. }
  2145. static int
  2146. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  2147. struct drm_framebuffer *fb)
  2148. {
  2149. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  2150. struct intel_rotation_info *rot_info = &intel_fb->rot_info;
  2151. u32 gtt_offset_rotated = 0;
  2152. unsigned int max_size = 0;
  2153. int i, num_planes = fb->format->num_planes;
  2154. unsigned int tile_size = intel_tile_size(dev_priv);
  2155. for (i = 0; i < num_planes; i++) {
  2156. unsigned int width, height;
  2157. unsigned int cpp, size;
  2158. u32 offset;
  2159. int x, y;
  2160. cpp = fb->format->cpp[i];
  2161. width = drm_framebuffer_plane_width(fb->width, fb, i);
  2162. height = drm_framebuffer_plane_height(fb->height, fb, i);
  2163. intel_fb_offset_to_xy(&x, &y, fb, i);
  2164. /*
  2165. * The fence (if used) is aligned to the start of the object
  2166. * so having the framebuffer wrap around across the edge of the
  2167. * fenced region doesn't really work. We have no API to configure
  2168. * the fence start offset within the object (nor could we probably
  2169. * on gen2/3). So it's just easier if we just require that the
  2170. * fb layout agrees with the fence layout. We already check that the
  2171. * fb stride matches the fence stride elsewhere.
  2172. */
  2173. if (i915_gem_object_is_tiled(intel_fb->obj) &&
  2174. (x + width) * cpp > fb->pitches[i]) {
  2175. DRM_DEBUG("bad fb plane %d offset: 0x%x\n",
  2176. i, fb->offsets[i]);
  2177. return -EINVAL;
  2178. }
  2179. /*
  2180. * First pixel of the framebuffer from
  2181. * the start of the normal gtt mapping.
  2182. */
  2183. intel_fb->normal[i].x = x;
  2184. intel_fb->normal[i].y = y;
  2185. offset = _intel_compute_tile_offset(dev_priv, &x, &y,
  2186. fb, 0, fb->pitches[i],
  2187. DRM_ROTATE_0, tile_size);
  2188. offset /= tile_size;
  2189. if (fb->modifier != DRM_FORMAT_MOD_NONE) {
  2190. unsigned int tile_width, tile_height;
  2191. unsigned int pitch_tiles;
  2192. struct drm_rect r;
  2193. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2194. fb->modifier, cpp);
  2195. rot_info->plane[i].offset = offset;
  2196. rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
  2197. rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
  2198. rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
  2199. intel_fb->rotated[i].pitch =
  2200. rot_info->plane[i].height * tile_height;
  2201. /* how many tiles does this plane need */
  2202. size = rot_info->plane[i].stride * rot_info->plane[i].height;
  2203. /*
  2204. * If the plane isn't horizontally tile aligned,
  2205. * we need one more tile.
  2206. */
  2207. if (x != 0)
  2208. size++;
  2209. /* rotate the x/y offsets to match the GTT view */
  2210. r.x1 = x;
  2211. r.y1 = y;
  2212. r.x2 = x + width;
  2213. r.y2 = y + height;
  2214. drm_rect_rotate(&r,
  2215. rot_info->plane[i].width * tile_width,
  2216. rot_info->plane[i].height * tile_height,
  2217. DRM_ROTATE_270);
  2218. x = r.x1;
  2219. y = r.y1;
  2220. /* rotate the tile dimensions to match the GTT view */
  2221. pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
  2222. swap(tile_width, tile_height);
  2223. /*
  2224. * We only keep the x/y offsets, so push all of the
  2225. * gtt offset into the x/y offsets.
  2226. */
  2227. _intel_adjust_tile_offset(&x, &y,
  2228. tile_width, tile_height,
  2229. tile_size, pitch_tiles,
  2230. gtt_offset_rotated * tile_size, 0);
  2231. gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
  2232. /*
  2233. * First pixel of the framebuffer from
  2234. * the start of the rotated gtt mapping.
  2235. */
  2236. intel_fb->rotated[i].x = x;
  2237. intel_fb->rotated[i].y = y;
  2238. } else {
  2239. size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
  2240. x * cpp, tile_size);
  2241. }
  2242. /* how many tiles in total needed in the bo */
  2243. max_size = max(max_size, offset + size);
  2244. }
  2245. if (max_size * tile_size > to_intel_framebuffer(fb)->obj->base.size) {
  2246. DRM_DEBUG("fb too big for bo (need %u bytes, have %zu bytes)\n",
  2247. max_size * tile_size, to_intel_framebuffer(fb)->obj->base.size);
  2248. return -EINVAL;
  2249. }
  2250. return 0;
  2251. }
  2252. static int i9xx_format_to_fourcc(int format)
  2253. {
  2254. switch (format) {
  2255. case DISPPLANE_8BPP:
  2256. return DRM_FORMAT_C8;
  2257. case DISPPLANE_BGRX555:
  2258. return DRM_FORMAT_XRGB1555;
  2259. case DISPPLANE_BGRX565:
  2260. return DRM_FORMAT_RGB565;
  2261. default:
  2262. case DISPPLANE_BGRX888:
  2263. return DRM_FORMAT_XRGB8888;
  2264. case DISPPLANE_RGBX888:
  2265. return DRM_FORMAT_XBGR8888;
  2266. case DISPPLANE_BGRX101010:
  2267. return DRM_FORMAT_XRGB2101010;
  2268. case DISPPLANE_RGBX101010:
  2269. return DRM_FORMAT_XBGR2101010;
  2270. }
  2271. }
  2272. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2273. {
  2274. switch (format) {
  2275. case PLANE_CTL_FORMAT_RGB_565:
  2276. return DRM_FORMAT_RGB565;
  2277. default:
  2278. case PLANE_CTL_FORMAT_XRGB_8888:
  2279. if (rgb_order) {
  2280. if (alpha)
  2281. return DRM_FORMAT_ABGR8888;
  2282. else
  2283. return DRM_FORMAT_XBGR8888;
  2284. } else {
  2285. if (alpha)
  2286. return DRM_FORMAT_ARGB8888;
  2287. else
  2288. return DRM_FORMAT_XRGB8888;
  2289. }
  2290. case PLANE_CTL_FORMAT_XRGB_2101010:
  2291. if (rgb_order)
  2292. return DRM_FORMAT_XBGR2101010;
  2293. else
  2294. return DRM_FORMAT_XRGB2101010;
  2295. }
  2296. }
  2297. static bool
  2298. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2299. struct intel_initial_plane_config *plane_config)
  2300. {
  2301. struct drm_device *dev = crtc->base.dev;
  2302. struct drm_i915_private *dev_priv = to_i915(dev);
  2303. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2304. struct drm_i915_gem_object *obj = NULL;
  2305. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2306. struct drm_framebuffer *fb = &plane_config->fb->base;
  2307. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2308. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2309. PAGE_SIZE);
  2310. size_aligned -= base_aligned;
  2311. if (plane_config->size == 0)
  2312. return false;
  2313. /* If the FB is too big, just don't use it since fbdev is not very
  2314. * important and we should probably use that space with FBC or other
  2315. * features. */
  2316. if (size_aligned * 2 > ggtt->stolen_usable_size)
  2317. return false;
  2318. mutex_lock(&dev->struct_mutex);
  2319. obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
  2320. base_aligned,
  2321. base_aligned,
  2322. size_aligned);
  2323. if (!obj) {
  2324. mutex_unlock(&dev->struct_mutex);
  2325. return false;
  2326. }
  2327. if (plane_config->tiling == I915_TILING_X)
  2328. obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
  2329. mode_cmd.pixel_format = fb->format->format;
  2330. mode_cmd.width = fb->width;
  2331. mode_cmd.height = fb->height;
  2332. mode_cmd.pitches[0] = fb->pitches[0];
  2333. mode_cmd.modifier[0] = fb->modifier;
  2334. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2335. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2336. &mode_cmd, obj)) {
  2337. DRM_DEBUG_KMS("intel fb init failed\n");
  2338. goto out_unref_obj;
  2339. }
  2340. mutex_unlock(&dev->struct_mutex);
  2341. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2342. return true;
  2343. out_unref_obj:
  2344. i915_gem_object_put(obj);
  2345. mutex_unlock(&dev->struct_mutex);
  2346. return false;
  2347. }
  2348. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2349. static void
  2350. update_state_fb(struct drm_plane *plane)
  2351. {
  2352. if (plane->fb == plane->state->fb)
  2353. return;
  2354. if (plane->state->fb)
  2355. drm_framebuffer_unreference(plane->state->fb);
  2356. plane->state->fb = plane->fb;
  2357. if (plane->state->fb)
  2358. drm_framebuffer_reference(plane->state->fb);
  2359. }
  2360. static void
  2361. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2362. struct intel_initial_plane_config *plane_config)
  2363. {
  2364. struct drm_device *dev = intel_crtc->base.dev;
  2365. struct drm_i915_private *dev_priv = to_i915(dev);
  2366. struct drm_crtc *c;
  2367. struct drm_i915_gem_object *obj;
  2368. struct drm_plane *primary = intel_crtc->base.primary;
  2369. struct drm_plane_state *plane_state = primary->state;
  2370. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2371. struct intel_plane *intel_plane = to_intel_plane(primary);
  2372. struct intel_plane_state *intel_state =
  2373. to_intel_plane_state(plane_state);
  2374. struct drm_framebuffer *fb;
  2375. if (!plane_config->fb)
  2376. return;
  2377. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2378. fb = &plane_config->fb->base;
  2379. goto valid_fb;
  2380. }
  2381. kfree(plane_config->fb);
  2382. /*
  2383. * Failed to alloc the obj, check to see if we should share
  2384. * an fb with another CRTC instead
  2385. */
  2386. for_each_crtc(dev, c) {
  2387. struct intel_plane_state *state;
  2388. if (c == &intel_crtc->base)
  2389. continue;
  2390. if (!to_intel_crtc(c)->active)
  2391. continue;
  2392. state = to_intel_plane_state(c->primary->state);
  2393. if (!state->vma)
  2394. continue;
  2395. if (intel_plane_ggtt_offset(state) == plane_config->base) {
  2396. fb = c->primary->fb;
  2397. drm_framebuffer_reference(fb);
  2398. goto valid_fb;
  2399. }
  2400. }
  2401. /*
  2402. * We've failed to reconstruct the BIOS FB. Current display state
  2403. * indicates that the primary plane is visible, but has a NULL FB,
  2404. * which will lead to problems later if we don't fix it up. The
  2405. * simplest solution is to just disable the primary plane now and
  2406. * pretend the BIOS never had it enabled.
  2407. */
  2408. plane_state->visible = false;
  2409. crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
  2410. intel_pre_disable_primary_noatomic(&intel_crtc->base);
  2411. intel_plane->disable_plane(primary, &intel_crtc->base);
  2412. return;
  2413. valid_fb:
  2414. mutex_lock(&dev->struct_mutex);
  2415. intel_state->vma =
  2416. intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  2417. mutex_unlock(&dev->struct_mutex);
  2418. if (IS_ERR(intel_state->vma)) {
  2419. DRM_ERROR("failed to pin boot fb on pipe %d: %li\n",
  2420. intel_crtc->pipe, PTR_ERR(intel_state->vma));
  2421. intel_state->vma = NULL;
  2422. drm_framebuffer_unreference(fb);
  2423. return;
  2424. }
  2425. plane_state->src_x = 0;
  2426. plane_state->src_y = 0;
  2427. plane_state->src_w = fb->width << 16;
  2428. plane_state->src_h = fb->height << 16;
  2429. plane_state->crtc_x = 0;
  2430. plane_state->crtc_y = 0;
  2431. plane_state->crtc_w = fb->width;
  2432. plane_state->crtc_h = fb->height;
  2433. intel_state->base.src = drm_plane_state_src(plane_state);
  2434. intel_state->base.dst = drm_plane_state_dest(plane_state);
  2435. obj = intel_fb_obj(fb);
  2436. if (i915_gem_object_is_tiled(obj))
  2437. dev_priv->preserve_bios_swizzle = true;
  2438. drm_framebuffer_reference(fb);
  2439. primary->fb = primary->state->fb = fb;
  2440. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2441. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2442. atomic_or(to_intel_plane(primary)->frontbuffer_bit,
  2443. &obj->frontbuffer_bits);
  2444. }
  2445. static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
  2446. unsigned int rotation)
  2447. {
  2448. int cpp = fb->format->cpp[plane];
  2449. switch (fb->modifier) {
  2450. case DRM_FORMAT_MOD_NONE:
  2451. case I915_FORMAT_MOD_X_TILED:
  2452. switch (cpp) {
  2453. case 8:
  2454. return 4096;
  2455. case 4:
  2456. case 2:
  2457. case 1:
  2458. return 8192;
  2459. default:
  2460. MISSING_CASE(cpp);
  2461. break;
  2462. }
  2463. break;
  2464. case I915_FORMAT_MOD_Y_TILED:
  2465. case I915_FORMAT_MOD_Yf_TILED:
  2466. switch (cpp) {
  2467. case 8:
  2468. return 2048;
  2469. case 4:
  2470. return 4096;
  2471. case 2:
  2472. case 1:
  2473. return 8192;
  2474. default:
  2475. MISSING_CASE(cpp);
  2476. break;
  2477. }
  2478. break;
  2479. default:
  2480. MISSING_CASE(fb->modifier);
  2481. }
  2482. return 2048;
  2483. }
  2484. static int skl_check_main_surface(struct intel_plane_state *plane_state)
  2485. {
  2486. const struct drm_i915_private *dev_priv = to_i915(plane_state->base.plane->dev);
  2487. const struct drm_framebuffer *fb = plane_state->base.fb;
  2488. unsigned int rotation = plane_state->base.rotation;
  2489. int x = plane_state->base.src.x1 >> 16;
  2490. int y = plane_state->base.src.y1 >> 16;
  2491. int w = drm_rect_width(&plane_state->base.src) >> 16;
  2492. int h = drm_rect_height(&plane_state->base.src) >> 16;
  2493. int max_width = skl_max_plane_width(fb, 0, rotation);
  2494. int max_height = 4096;
  2495. u32 alignment, offset, aux_offset = plane_state->aux.offset;
  2496. if (w > max_width || h > max_height) {
  2497. DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
  2498. w, h, max_width, max_height);
  2499. return -EINVAL;
  2500. }
  2501. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2502. offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
  2503. alignment = intel_surf_alignment(dev_priv, fb->modifier);
  2504. /*
  2505. * AUX surface offset is specified as the distance from the
  2506. * main surface offset, and it must be non-negative. Make
  2507. * sure that is what we will get.
  2508. */
  2509. if (offset > aux_offset)
  2510. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2511. offset, aux_offset & ~(alignment - 1));
  2512. /*
  2513. * When using an X-tiled surface, the plane blows up
  2514. * if the x offset + width exceed the stride.
  2515. *
  2516. * TODO: linear and Y-tiled seem fine, Yf untested,
  2517. */
  2518. if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
  2519. int cpp = fb->format->cpp[0];
  2520. while ((x + w) * cpp > fb->pitches[0]) {
  2521. if (offset == 0) {
  2522. DRM_DEBUG_KMS("Unable to find suitable display surface offset\n");
  2523. return -EINVAL;
  2524. }
  2525. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2526. offset, offset - alignment);
  2527. }
  2528. }
  2529. plane_state->main.offset = offset;
  2530. plane_state->main.x = x;
  2531. plane_state->main.y = y;
  2532. return 0;
  2533. }
  2534. static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
  2535. {
  2536. const struct drm_framebuffer *fb = plane_state->base.fb;
  2537. unsigned int rotation = plane_state->base.rotation;
  2538. int max_width = skl_max_plane_width(fb, 1, rotation);
  2539. int max_height = 4096;
  2540. int x = plane_state->base.src.x1 >> 17;
  2541. int y = plane_state->base.src.y1 >> 17;
  2542. int w = drm_rect_width(&plane_state->base.src) >> 17;
  2543. int h = drm_rect_height(&plane_state->base.src) >> 17;
  2544. u32 offset;
  2545. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2546. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2547. /* FIXME not quite sure how/if these apply to the chroma plane */
  2548. if (w > max_width || h > max_height) {
  2549. DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
  2550. w, h, max_width, max_height);
  2551. return -EINVAL;
  2552. }
  2553. plane_state->aux.offset = offset;
  2554. plane_state->aux.x = x;
  2555. plane_state->aux.y = y;
  2556. return 0;
  2557. }
  2558. int skl_check_plane_surface(struct intel_plane_state *plane_state)
  2559. {
  2560. const struct drm_framebuffer *fb = plane_state->base.fb;
  2561. unsigned int rotation = plane_state->base.rotation;
  2562. int ret;
  2563. if (!plane_state->base.visible)
  2564. return 0;
  2565. /* Rotate src coordinates to match rotated GTT view */
  2566. if (drm_rotation_90_or_270(rotation))
  2567. drm_rect_rotate(&plane_state->base.src,
  2568. fb->width << 16, fb->height << 16,
  2569. DRM_ROTATE_270);
  2570. /*
  2571. * Handle the AUX surface first since
  2572. * the main surface setup depends on it.
  2573. */
  2574. if (fb->format->format == DRM_FORMAT_NV12) {
  2575. ret = skl_check_nv12_aux_surface(plane_state);
  2576. if (ret)
  2577. return ret;
  2578. } else {
  2579. plane_state->aux.offset = ~0xfff;
  2580. plane_state->aux.x = 0;
  2581. plane_state->aux.y = 0;
  2582. }
  2583. ret = skl_check_main_surface(plane_state);
  2584. if (ret)
  2585. return ret;
  2586. return 0;
  2587. }
  2588. static void i9xx_update_primary_plane(struct drm_plane *primary,
  2589. const struct intel_crtc_state *crtc_state,
  2590. const struct intel_plane_state *plane_state)
  2591. {
  2592. struct drm_i915_private *dev_priv = to_i915(primary->dev);
  2593. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2594. struct drm_framebuffer *fb = plane_state->base.fb;
  2595. int plane = intel_crtc->plane;
  2596. u32 linear_offset;
  2597. u32 dspcntr;
  2598. i915_reg_t reg = DSPCNTR(plane);
  2599. unsigned int rotation = plane_state->base.rotation;
  2600. int x = plane_state->base.src.x1 >> 16;
  2601. int y = plane_state->base.src.y1 >> 16;
  2602. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2603. dspcntr |= DISPLAY_PLANE_ENABLE;
  2604. if (INTEL_GEN(dev_priv) < 4) {
  2605. if (intel_crtc->pipe == PIPE_B)
  2606. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2607. /* pipesrc and dspsize control the size that is scaled from,
  2608. * which should always be the user's requested size.
  2609. */
  2610. I915_WRITE(DSPSIZE(plane),
  2611. ((crtc_state->pipe_src_h - 1) << 16) |
  2612. (crtc_state->pipe_src_w - 1));
  2613. I915_WRITE(DSPPOS(plane), 0);
  2614. } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
  2615. I915_WRITE(PRIMSIZE(plane),
  2616. ((crtc_state->pipe_src_h - 1) << 16) |
  2617. (crtc_state->pipe_src_w - 1));
  2618. I915_WRITE(PRIMPOS(plane), 0);
  2619. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2620. }
  2621. switch (fb->format->format) {
  2622. case DRM_FORMAT_C8:
  2623. dspcntr |= DISPPLANE_8BPP;
  2624. break;
  2625. case DRM_FORMAT_XRGB1555:
  2626. dspcntr |= DISPPLANE_BGRX555;
  2627. break;
  2628. case DRM_FORMAT_RGB565:
  2629. dspcntr |= DISPPLANE_BGRX565;
  2630. break;
  2631. case DRM_FORMAT_XRGB8888:
  2632. dspcntr |= DISPPLANE_BGRX888;
  2633. break;
  2634. case DRM_FORMAT_XBGR8888:
  2635. dspcntr |= DISPPLANE_RGBX888;
  2636. break;
  2637. case DRM_FORMAT_XRGB2101010:
  2638. dspcntr |= DISPPLANE_BGRX101010;
  2639. break;
  2640. case DRM_FORMAT_XBGR2101010:
  2641. dspcntr |= DISPPLANE_RGBX101010;
  2642. break;
  2643. default:
  2644. BUG();
  2645. }
  2646. if (INTEL_GEN(dev_priv) >= 4 &&
  2647. fb->modifier == I915_FORMAT_MOD_X_TILED)
  2648. dspcntr |= DISPPLANE_TILED;
  2649. if (rotation & DRM_ROTATE_180)
  2650. dspcntr |= DISPPLANE_ROTATE_180;
  2651. if (rotation & DRM_REFLECT_X)
  2652. dspcntr |= DISPPLANE_MIRROR;
  2653. if (IS_G4X(dev_priv))
  2654. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2655. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2656. if (INTEL_GEN(dev_priv) >= 4)
  2657. intel_crtc->dspaddr_offset =
  2658. intel_compute_tile_offset(&x, &y, plane_state, 0);
  2659. if (rotation & DRM_ROTATE_180) {
  2660. x += crtc_state->pipe_src_w - 1;
  2661. y += crtc_state->pipe_src_h - 1;
  2662. } else if (rotation & DRM_REFLECT_X) {
  2663. x += crtc_state->pipe_src_w - 1;
  2664. }
  2665. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2666. if (INTEL_GEN(dev_priv) < 4)
  2667. intel_crtc->dspaddr_offset = linear_offset;
  2668. intel_crtc->adjusted_x = x;
  2669. intel_crtc->adjusted_y = y;
  2670. I915_WRITE(reg, dspcntr);
  2671. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2672. if (INTEL_GEN(dev_priv) >= 4) {
  2673. I915_WRITE(DSPSURF(plane),
  2674. intel_plane_ggtt_offset(plane_state) +
  2675. intel_crtc->dspaddr_offset);
  2676. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2677. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2678. } else {
  2679. I915_WRITE(DSPADDR(plane),
  2680. intel_plane_ggtt_offset(plane_state) +
  2681. intel_crtc->dspaddr_offset);
  2682. }
  2683. POSTING_READ(reg);
  2684. }
  2685. static void i9xx_disable_primary_plane(struct drm_plane *primary,
  2686. struct drm_crtc *crtc)
  2687. {
  2688. struct drm_device *dev = crtc->dev;
  2689. struct drm_i915_private *dev_priv = to_i915(dev);
  2690. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2691. int plane = intel_crtc->plane;
  2692. I915_WRITE(DSPCNTR(plane), 0);
  2693. if (INTEL_INFO(dev_priv)->gen >= 4)
  2694. I915_WRITE(DSPSURF(plane), 0);
  2695. else
  2696. I915_WRITE(DSPADDR(plane), 0);
  2697. POSTING_READ(DSPCNTR(plane));
  2698. }
  2699. static void ironlake_update_primary_plane(struct drm_plane *primary,
  2700. const struct intel_crtc_state *crtc_state,
  2701. const struct intel_plane_state *plane_state)
  2702. {
  2703. struct drm_device *dev = primary->dev;
  2704. struct drm_i915_private *dev_priv = to_i915(dev);
  2705. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2706. struct drm_framebuffer *fb = plane_state->base.fb;
  2707. int plane = intel_crtc->plane;
  2708. u32 linear_offset;
  2709. u32 dspcntr;
  2710. i915_reg_t reg = DSPCNTR(plane);
  2711. unsigned int rotation = plane_state->base.rotation;
  2712. int x = plane_state->base.src.x1 >> 16;
  2713. int y = plane_state->base.src.y1 >> 16;
  2714. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2715. dspcntr |= DISPLAY_PLANE_ENABLE;
  2716. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  2717. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2718. switch (fb->format->format) {
  2719. case DRM_FORMAT_C8:
  2720. dspcntr |= DISPPLANE_8BPP;
  2721. break;
  2722. case DRM_FORMAT_RGB565:
  2723. dspcntr |= DISPPLANE_BGRX565;
  2724. break;
  2725. case DRM_FORMAT_XRGB8888:
  2726. dspcntr |= DISPPLANE_BGRX888;
  2727. break;
  2728. case DRM_FORMAT_XBGR8888:
  2729. dspcntr |= DISPPLANE_RGBX888;
  2730. break;
  2731. case DRM_FORMAT_XRGB2101010:
  2732. dspcntr |= DISPPLANE_BGRX101010;
  2733. break;
  2734. case DRM_FORMAT_XBGR2101010:
  2735. dspcntr |= DISPPLANE_RGBX101010;
  2736. break;
  2737. default:
  2738. BUG();
  2739. }
  2740. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  2741. dspcntr |= DISPPLANE_TILED;
  2742. if (rotation & DRM_ROTATE_180)
  2743. dspcntr |= DISPPLANE_ROTATE_180;
  2744. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv))
  2745. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2746. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2747. intel_crtc->dspaddr_offset =
  2748. intel_compute_tile_offset(&x, &y, plane_state, 0);
  2749. /* HSW+ does this automagically in hardware */
  2750. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv) &&
  2751. rotation & DRM_ROTATE_180) {
  2752. x += crtc_state->pipe_src_w - 1;
  2753. y += crtc_state->pipe_src_h - 1;
  2754. }
  2755. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2756. intel_crtc->adjusted_x = x;
  2757. intel_crtc->adjusted_y = y;
  2758. I915_WRITE(reg, dspcntr);
  2759. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2760. I915_WRITE(DSPSURF(plane),
  2761. intel_plane_ggtt_offset(plane_state) +
  2762. intel_crtc->dspaddr_offset);
  2763. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  2764. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2765. } else {
  2766. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2767. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2768. }
  2769. POSTING_READ(reg);
  2770. }
  2771. u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
  2772. uint64_t fb_modifier, uint32_t pixel_format)
  2773. {
  2774. if (fb_modifier == DRM_FORMAT_MOD_NONE) {
  2775. return 64;
  2776. } else {
  2777. int cpp = drm_format_plane_cpp(pixel_format, 0);
  2778. return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  2779. }
  2780. }
  2781. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2782. {
  2783. struct drm_device *dev = intel_crtc->base.dev;
  2784. struct drm_i915_private *dev_priv = to_i915(dev);
  2785. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2786. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2787. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2788. }
  2789. /*
  2790. * This function detaches (aka. unbinds) unused scalers in hardware
  2791. */
  2792. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2793. {
  2794. struct intel_crtc_scaler_state *scaler_state;
  2795. int i;
  2796. scaler_state = &intel_crtc->config->scaler_state;
  2797. /* loop through and disable scalers that aren't in use */
  2798. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2799. if (!scaler_state->scalers[i].in_use)
  2800. skl_detach_scaler(intel_crtc, i);
  2801. }
  2802. }
  2803. u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
  2804. unsigned int rotation)
  2805. {
  2806. const struct drm_i915_private *dev_priv = to_i915(fb->dev);
  2807. u32 stride = intel_fb_pitch(fb, plane, rotation);
  2808. /*
  2809. * The stride is either expressed as a multiple of 64 bytes chunks for
  2810. * linear buffers or in number of tiles for tiled buffers.
  2811. */
  2812. if (drm_rotation_90_or_270(rotation)) {
  2813. int cpp = fb->format->cpp[plane];
  2814. stride /= intel_tile_height(dev_priv, fb->modifier, cpp);
  2815. } else {
  2816. stride /= intel_fb_stride_alignment(dev_priv, fb->modifier,
  2817. fb->format->format);
  2818. }
  2819. return stride;
  2820. }
  2821. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2822. {
  2823. switch (pixel_format) {
  2824. case DRM_FORMAT_C8:
  2825. return PLANE_CTL_FORMAT_INDEXED;
  2826. case DRM_FORMAT_RGB565:
  2827. return PLANE_CTL_FORMAT_RGB_565;
  2828. case DRM_FORMAT_XBGR8888:
  2829. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2830. case DRM_FORMAT_XRGB8888:
  2831. return PLANE_CTL_FORMAT_XRGB_8888;
  2832. /*
  2833. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2834. * to be already pre-multiplied. We need to add a knob (or a different
  2835. * DRM_FORMAT) for user-space to configure that.
  2836. */
  2837. case DRM_FORMAT_ABGR8888:
  2838. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2839. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2840. case DRM_FORMAT_ARGB8888:
  2841. return PLANE_CTL_FORMAT_XRGB_8888 |
  2842. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2843. case DRM_FORMAT_XRGB2101010:
  2844. return PLANE_CTL_FORMAT_XRGB_2101010;
  2845. case DRM_FORMAT_XBGR2101010:
  2846. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2847. case DRM_FORMAT_YUYV:
  2848. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2849. case DRM_FORMAT_YVYU:
  2850. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2851. case DRM_FORMAT_UYVY:
  2852. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2853. case DRM_FORMAT_VYUY:
  2854. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2855. default:
  2856. MISSING_CASE(pixel_format);
  2857. }
  2858. return 0;
  2859. }
  2860. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2861. {
  2862. switch (fb_modifier) {
  2863. case DRM_FORMAT_MOD_NONE:
  2864. break;
  2865. case I915_FORMAT_MOD_X_TILED:
  2866. return PLANE_CTL_TILED_X;
  2867. case I915_FORMAT_MOD_Y_TILED:
  2868. return PLANE_CTL_TILED_Y;
  2869. case I915_FORMAT_MOD_Yf_TILED:
  2870. return PLANE_CTL_TILED_YF;
  2871. default:
  2872. MISSING_CASE(fb_modifier);
  2873. }
  2874. return 0;
  2875. }
  2876. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2877. {
  2878. switch (rotation) {
  2879. case DRM_ROTATE_0:
  2880. break;
  2881. /*
  2882. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2883. * while i915 HW rotation is clockwise, thats why this swapping.
  2884. */
  2885. case DRM_ROTATE_90:
  2886. return PLANE_CTL_ROTATE_270;
  2887. case DRM_ROTATE_180:
  2888. return PLANE_CTL_ROTATE_180;
  2889. case DRM_ROTATE_270:
  2890. return PLANE_CTL_ROTATE_90;
  2891. default:
  2892. MISSING_CASE(rotation);
  2893. }
  2894. return 0;
  2895. }
  2896. static void skylake_update_primary_plane(struct drm_plane *plane,
  2897. const struct intel_crtc_state *crtc_state,
  2898. const struct intel_plane_state *plane_state)
  2899. {
  2900. struct drm_device *dev = plane->dev;
  2901. struct drm_i915_private *dev_priv = to_i915(dev);
  2902. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2903. struct drm_framebuffer *fb = plane_state->base.fb;
  2904. enum plane_id plane_id = to_intel_plane(plane)->id;
  2905. enum pipe pipe = to_intel_plane(plane)->pipe;
  2906. u32 plane_ctl;
  2907. unsigned int rotation = plane_state->base.rotation;
  2908. u32 stride = skl_plane_stride(fb, 0, rotation);
  2909. u32 surf_addr = plane_state->main.offset;
  2910. int scaler_id = plane_state->scaler_id;
  2911. int src_x = plane_state->main.x;
  2912. int src_y = plane_state->main.y;
  2913. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  2914. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  2915. int dst_x = plane_state->base.dst.x1;
  2916. int dst_y = plane_state->base.dst.y1;
  2917. int dst_w = drm_rect_width(&plane_state->base.dst);
  2918. int dst_h = drm_rect_height(&plane_state->base.dst);
  2919. plane_ctl = PLANE_CTL_ENABLE |
  2920. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2921. PLANE_CTL_PIPE_CSC_ENABLE;
  2922. plane_ctl |= skl_plane_ctl_format(fb->format->format);
  2923. plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
  2924. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2925. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2926. /* Sizes are 0 based */
  2927. src_w--;
  2928. src_h--;
  2929. dst_w--;
  2930. dst_h--;
  2931. intel_crtc->dspaddr_offset = surf_addr;
  2932. intel_crtc->adjusted_x = src_x;
  2933. intel_crtc->adjusted_y = src_y;
  2934. I915_WRITE(PLANE_CTL(pipe, plane_id), plane_ctl);
  2935. I915_WRITE(PLANE_OFFSET(pipe, plane_id), (src_y << 16) | src_x);
  2936. I915_WRITE(PLANE_STRIDE(pipe, plane_id), stride);
  2937. I915_WRITE(PLANE_SIZE(pipe, plane_id), (src_h << 16) | src_w);
  2938. if (scaler_id >= 0) {
  2939. uint32_t ps_ctrl = 0;
  2940. WARN_ON(!dst_w || !dst_h);
  2941. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(plane_id) |
  2942. crtc_state->scaler_state.scalers[scaler_id].mode;
  2943. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2944. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2945. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2946. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2947. I915_WRITE(PLANE_POS(pipe, plane_id), 0);
  2948. } else {
  2949. I915_WRITE(PLANE_POS(pipe, plane_id), (dst_y << 16) | dst_x);
  2950. }
  2951. I915_WRITE(PLANE_SURF(pipe, plane_id),
  2952. intel_plane_ggtt_offset(plane_state) + surf_addr);
  2953. POSTING_READ(PLANE_SURF(pipe, plane_id));
  2954. }
  2955. static void skylake_disable_primary_plane(struct drm_plane *primary,
  2956. struct drm_crtc *crtc)
  2957. {
  2958. struct drm_device *dev = crtc->dev;
  2959. struct drm_i915_private *dev_priv = to_i915(dev);
  2960. enum plane_id plane_id = to_intel_plane(primary)->id;
  2961. enum pipe pipe = to_intel_plane(primary)->pipe;
  2962. I915_WRITE(PLANE_CTL(pipe, plane_id), 0);
  2963. I915_WRITE(PLANE_SURF(pipe, plane_id), 0);
  2964. POSTING_READ(PLANE_SURF(pipe, plane_id));
  2965. }
  2966. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2967. static int
  2968. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2969. int x, int y, enum mode_set_atomic state)
  2970. {
  2971. /* Support for kgdboc is disabled, this needs a major rework. */
  2972. DRM_ERROR("legacy panic handler not supported any more.\n");
  2973. return -ENODEV;
  2974. }
  2975. static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
  2976. {
  2977. struct intel_crtc *crtc;
  2978. for_each_intel_crtc(&dev_priv->drm, crtc)
  2979. intel_finish_page_flip_cs(dev_priv, crtc->pipe);
  2980. }
  2981. static void intel_update_primary_planes(struct drm_device *dev)
  2982. {
  2983. struct drm_crtc *crtc;
  2984. for_each_crtc(dev, crtc) {
  2985. struct intel_plane *plane = to_intel_plane(crtc->primary);
  2986. struct intel_plane_state *plane_state =
  2987. to_intel_plane_state(plane->base.state);
  2988. if (plane_state->base.visible)
  2989. plane->update_plane(&plane->base,
  2990. to_intel_crtc_state(crtc->state),
  2991. plane_state);
  2992. }
  2993. }
  2994. static int
  2995. __intel_display_resume(struct drm_device *dev,
  2996. struct drm_atomic_state *state)
  2997. {
  2998. struct drm_crtc_state *crtc_state;
  2999. struct drm_crtc *crtc;
  3000. int i, ret;
  3001. intel_modeset_setup_hw_state(dev);
  3002. i915_redisable_vga(to_i915(dev));
  3003. if (!state)
  3004. return 0;
  3005. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  3006. /*
  3007. * Force recalculation even if we restore
  3008. * current state. With fast modeset this may not result
  3009. * in a modeset when the state is compatible.
  3010. */
  3011. crtc_state->mode_changed = true;
  3012. }
  3013. /* ignore any reset values/BIOS leftovers in the WM registers */
  3014. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  3015. ret = drm_atomic_commit(state);
  3016. WARN_ON(ret == -EDEADLK);
  3017. return ret;
  3018. }
  3019. static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
  3020. {
  3021. return intel_has_gpu_reset(dev_priv) &&
  3022. INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
  3023. }
  3024. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  3025. {
  3026. struct drm_device *dev = &dev_priv->drm;
  3027. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3028. struct drm_atomic_state *state;
  3029. int ret;
  3030. /*
  3031. * Need mode_config.mutex so that we don't
  3032. * trample ongoing ->detect() and whatnot.
  3033. */
  3034. mutex_lock(&dev->mode_config.mutex);
  3035. drm_modeset_acquire_init(ctx, 0);
  3036. while (1) {
  3037. ret = drm_modeset_lock_all_ctx(dev, ctx);
  3038. if (ret != -EDEADLK)
  3039. break;
  3040. drm_modeset_backoff(ctx);
  3041. }
  3042. /* reset doesn't touch the display, but flips might get nuked anyway, */
  3043. if (!i915.force_reset_modeset_test &&
  3044. !gpu_reset_clobbers_display(dev_priv))
  3045. return;
  3046. /*
  3047. * Disabling the crtcs gracefully seems nicer. Also the
  3048. * g33 docs say we should at least disable all the planes.
  3049. */
  3050. state = drm_atomic_helper_duplicate_state(dev, ctx);
  3051. if (IS_ERR(state)) {
  3052. ret = PTR_ERR(state);
  3053. DRM_ERROR("Duplicating state failed with %i\n", ret);
  3054. return;
  3055. }
  3056. ret = drm_atomic_helper_disable_all(dev, ctx);
  3057. if (ret) {
  3058. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  3059. drm_atomic_state_put(state);
  3060. return;
  3061. }
  3062. dev_priv->modeset_restore_state = state;
  3063. state->acquire_ctx = ctx;
  3064. }
  3065. void intel_finish_reset(struct drm_i915_private *dev_priv)
  3066. {
  3067. struct drm_device *dev = &dev_priv->drm;
  3068. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3069. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  3070. int ret;
  3071. /*
  3072. * Flips in the rings will be nuked by the reset,
  3073. * so complete all pending flips so that user space
  3074. * will get its events and not get stuck.
  3075. */
  3076. intel_complete_page_flips(dev_priv);
  3077. dev_priv->modeset_restore_state = NULL;
  3078. /* reset doesn't touch the display */
  3079. if (!gpu_reset_clobbers_display(dev_priv)) {
  3080. if (!state) {
  3081. /*
  3082. * Flips in the rings have been nuked by the reset,
  3083. * so update the base address of all primary
  3084. * planes to the the last fb to make sure we're
  3085. * showing the correct fb after a reset.
  3086. *
  3087. * FIXME: Atomic will make this obsolete since we won't schedule
  3088. * CS-based flips (which might get lost in gpu resets) any more.
  3089. */
  3090. intel_update_primary_planes(dev);
  3091. } else {
  3092. ret = __intel_display_resume(dev, state);
  3093. if (ret)
  3094. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3095. }
  3096. } else {
  3097. /*
  3098. * The display has been reset as well,
  3099. * so need a full re-initialization.
  3100. */
  3101. intel_runtime_pm_disable_interrupts(dev_priv);
  3102. intel_runtime_pm_enable_interrupts(dev_priv);
  3103. intel_pps_unlock_regs_wa(dev_priv);
  3104. intel_modeset_init_hw(dev);
  3105. spin_lock_irq(&dev_priv->irq_lock);
  3106. if (dev_priv->display.hpd_irq_setup)
  3107. dev_priv->display.hpd_irq_setup(dev_priv);
  3108. spin_unlock_irq(&dev_priv->irq_lock);
  3109. ret = __intel_display_resume(dev, state);
  3110. if (ret)
  3111. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3112. intel_hpd_init(dev_priv);
  3113. }
  3114. if (state)
  3115. drm_atomic_state_put(state);
  3116. drm_modeset_drop_locks(ctx);
  3117. drm_modeset_acquire_fini(ctx);
  3118. mutex_unlock(&dev->mode_config.mutex);
  3119. }
  3120. static bool abort_flip_on_reset(struct intel_crtc *crtc)
  3121. {
  3122. struct i915_gpu_error *error = &to_i915(crtc->base.dev)->gpu_error;
  3123. if (i915_reset_in_progress(error))
  3124. return true;
  3125. if (crtc->reset_count != i915_reset_count(error))
  3126. return true;
  3127. return false;
  3128. }
  3129. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  3130. {
  3131. struct drm_device *dev = crtc->dev;
  3132. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3133. bool pending;
  3134. if (abort_flip_on_reset(intel_crtc))
  3135. return false;
  3136. spin_lock_irq(&dev->event_lock);
  3137. pending = to_intel_crtc(crtc)->flip_work != NULL;
  3138. spin_unlock_irq(&dev->event_lock);
  3139. return pending;
  3140. }
  3141. static void intel_update_pipe_config(struct intel_crtc *crtc,
  3142. struct intel_crtc_state *old_crtc_state)
  3143. {
  3144. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3145. struct intel_crtc_state *pipe_config =
  3146. to_intel_crtc_state(crtc->base.state);
  3147. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  3148. crtc->base.mode = crtc->base.state->mode;
  3149. /*
  3150. * Update pipe size and adjust fitter if needed: the reason for this is
  3151. * that in compute_mode_changes we check the native mode (not the pfit
  3152. * mode) to see if we can flip rather than do a full mode set. In the
  3153. * fastboot case, we'll flip, but if we don't update the pipesrc and
  3154. * pfit state, we'll end up with a big fb scanned out into the wrong
  3155. * sized surface.
  3156. */
  3157. I915_WRITE(PIPESRC(crtc->pipe),
  3158. ((pipe_config->pipe_src_w - 1) << 16) |
  3159. (pipe_config->pipe_src_h - 1));
  3160. /* on skylake this is done by detaching scalers */
  3161. if (INTEL_GEN(dev_priv) >= 9) {
  3162. skl_detach_scalers(crtc);
  3163. if (pipe_config->pch_pfit.enabled)
  3164. skylake_pfit_enable(crtc);
  3165. } else if (HAS_PCH_SPLIT(dev_priv)) {
  3166. if (pipe_config->pch_pfit.enabled)
  3167. ironlake_pfit_enable(crtc);
  3168. else if (old_crtc_state->pch_pfit.enabled)
  3169. ironlake_pfit_disable(crtc, true);
  3170. }
  3171. }
  3172. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  3173. {
  3174. struct drm_device *dev = crtc->dev;
  3175. struct drm_i915_private *dev_priv = to_i915(dev);
  3176. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3177. int pipe = intel_crtc->pipe;
  3178. i915_reg_t reg;
  3179. u32 temp;
  3180. /* enable normal train */
  3181. reg = FDI_TX_CTL(pipe);
  3182. temp = I915_READ(reg);
  3183. if (IS_IVYBRIDGE(dev_priv)) {
  3184. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3185. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  3186. } else {
  3187. temp &= ~FDI_LINK_TRAIN_NONE;
  3188. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  3189. }
  3190. I915_WRITE(reg, temp);
  3191. reg = FDI_RX_CTL(pipe);
  3192. temp = I915_READ(reg);
  3193. if (HAS_PCH_CPT(dev_priv)) {
  3194. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3195. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  3196. } else {
  3197. temp &= ~FDI_LINK_TRAIN_NONE;
  3198. temp |= FDI_LINK_TRAIN_NONE;
  3199. }
  3200. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  3201. /* wait one idle pattern time */
  3202. POSTING_READ(reg);
  3203. udelay(1000);
  3204. /* IVB wants error correction enabled */
  3205. if (IS_IVYBRIDGE(dev_priv))
  3206. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  3207. FDI_FE_ERRC_ENABLE);
  3208. }
  3209. /* The FDI link training functions for ILK/Ibexpeak. */
  3210. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  3211. {
  3212. struct drm_device *dev = crtc->dev;
  3213. struct drm_i915_private *dev_priv = to_i915(dev);
  3214. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3215. int pipe = intel_crtc->pipe;
  3216. i915_reg_t reg;
  3217. u32 temp, tries;
  3218. /* FDI needs bits from pipe first */
  3219. assert_pipe_enabled(dev_priv, pipe);
  3220. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3221. for train result */
  3222. reg = FDI_RX_IMR(pipe);
  3223. temp = I915_READ(reg);
  3224. temp &= ~FDI_RX_SYMBOL_LOCK;
  3225. temp &= ~FDI_RX_BIT_LOCK;
  3226. I915_WRITE(reg, temp);
  3227. I915_READ(reg);
  3228. udelay(150);
  3229. /* enable CPU FDI TX and PCH FDI RX */
  3230. reg = FDI_TX_CTL(pipe);
  3231. temp = I915_READ(reg);
  3232. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3233. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3234. temp &= ~FDI_LINK_TRAIN_NONE;
  3235. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3236. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3237. reg = FDI_RX_CTL(pipe);
  3238. temp = I915_READ(reg);
  3239. temp &= ~FDI_LINK_TRAIN_NONE;
  3240. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3241. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3242. POSTING_READ(reg);
  3243. udelay(150);
  3244. /* Ironlake workaround, enable clock pointer after FDI enable*/
  3245. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3246. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  3247. FDI_RX_PHASE_SYNC_POINTER_EN);
  3248. reg = FDI_RX_IIR(pipe);
  3249. for (tries = 0; tries < 5; tries++) {
  3250. temp = I915_READ(reg);
  3251. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3252. if ((temp & FDI_RX_BIT_LOCK)) {
  3253. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3254. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3255. break;
  3256. }
  3257. }
  3258. if (tries == 5)
  3259. DRM_ERROR("FDI train 1 fail!\n");
  3260. /* Train 2 */
  3261. reg = FDI_TX_CTL(pipe);
  3262. temp = I915_READ(reg);
  3263. temp &= ~FDI_LINK_TRAIN_NONE;
  3264. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3265. I915_WRITE(reg, temp);
  3266. reg = FDI_RX_CTL(pipe);
  3267. temp = I915_READ(reg);
  3268. temp &= ~FDI_LINK_TRAIN_NONE;
  3269. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3270. I915_WRITE(reg, temp);
  3271. POSTING_READ(reg);
  3272. udelay(150);
  3273. reg = FDI_RX_IIR(pipe);
  3274. for (tries = 0; tries < 5; tries++) {
  3275. temp = I915_READ(reg);
  3276. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3277. if (temp & FDI_RX_SYMBOL_LOCK) {
  3278. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3279. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3280. break;
  3281. }
  3282. }
  3283. if (tries == 5)
  3284. DRM_ERROR("FDI train 2 fail!\n");
  3285. DRM_DEBUG_KMS("FDI train done\n");
  3286. }
  3287. static const int snb_b_fdi_train_param[] = {
  3288. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3289. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3290. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3291. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3292. };
  3293. /* The FDI link training functions for SNB/Cougarpoint. */
  3294. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  3295. {
  3296. struct drm_device *dev = crtc->dev;
  3297. struct drm_i915_private *dev_priv = to_i915(dev);
  3298. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3299. int pipe = intel_crtc->pipe;
  3300. i915_reg_t reg;
  3301. u32 temp, i, retry;
  3302. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3303. for train result */
  3304. reg = FDI_RX_IMR(pipe);
  3305. temp = I915_READ(reg);
  3306. temp &= ~FDI_RX_SYMBOL_LOCK;
  3307. temp &= ~FDI_RX_BIT_LOCK;
  3308. I915_WRITE(reg, temp);
  3309. POSTING_READ(reg);
  3310. udelay(150);
  3311. /* enable CPU FDI TX and PCH FDI RX */
  3312. reg = FDI_TX_CTL(pipe);
  3313. temp = I915_READ(reg);
  3314. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3315. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3316. temp &= ~FDI_LINK_TRAIN_NONE;
  3317. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3318. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3319. /* SNB-B */
  3320. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3321. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3322. I915_WRITE(FDI_RX_MISC(pipe),
  3323. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3324. reg = FDI_RX_CTL(pipe);
  3325. temp = I915_READ(reg);
  3326. if (HAS_PCH_CPT(dev_priv)) {
  3327. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3328. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3329. } else {
  3330. temp &= ~FDI_LINK_TRAIN_NONE;
  3331. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3332. }
  3333. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3334. POSTING_READ(reg);
  3335. udelay(150);
  3336. for (i = 0; i < 4; i++) {
  3337. reg = FDI_TX_CTL(pipe);
  3338. temp = I915_READ(reg);
  3339. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3340. temp |= snb_b_fdi_train_param[i];
  3341. I915_WRITE(reg, temp);
  3342. POSTING_READ(reg);
  3343. udelay(500);
  3344. for (retry = 0; retry < 5; retry++) {
  3345. reg = FDI_RX_IIR(pipe);
  3346. temp = I915_READ(reg);
  3347. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3348. if (temp & FDI_RX_BIT_LOCK) {
  3349. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3350. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3351. break;
  3352. }
  3353. udelay(50);
  3354. }
  3355. if (retry < 5)
  3356. break;
  3357. }
  3358. if (i == 4)
  3359. DRM_ERROR("FDI train 1 fail!\n");
  3360. /* Train 2 */
  3361. reg = FDI_TX_CTL(pipe);
  3362. temp = I915_READ(reg);
  3363. temp &= ~FDI_LINK_TRAIN_NONE;
  3364. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3365. if (IS_GEN6(dev_priv)) {
  3366. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3367. /* SNB-B */
  3368. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3369. }
  3370. I915_WRITE(reg, temp);
  3371. reg = FDI_RX_CTL(pipe);
  3372. temp = I915_READ(reg);
  3373. if (HAS_PCH_CPT(dev_priv)) {
  3374. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3375. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3376. } else {
  3377. temp &= ~FDI_LINK_TRAIN_NONE;
  3378. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3379. }
  3380. I915_WRITE(reg, temp);
  3381. POSTING_READ(reg);
  3382. udelay(150);
  3383. for (i = 0; i < 4; i++) {
  3384. reg = FDI_TX_CTL(pipe);
  3385. temp = I915_READ(reg);
  3386. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3387. temp |= snb_b_fdi_train_param[i];
  3388. I915_WRITE(reg, temp);
  3389. POSTING_READ(reg);
  3390. udelay(500);
  3391. for (retry = 0; retry < 5; retry++) {
  3392. reg = FDI_RX_IIR(pipe);
  3393. temp = I915_READ(reg);
  3394. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3395. if (temp & FDI_RX_SYMBOL_LOCK) {
  3396. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3397. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3398. break;
  3399. }
  3400. udelay(50);
  3401. }
  3402. if (retry < 5)
  3403. break;
  3404. }
  3405. if (i == 4)
  3406. DRM_ERROR("FDI train 2 fail!\n");
  3407. DRM_DEBUG_KMS("FDI train done.\n");
  3408. }
  3409. /* Manual link training for Ivy Bridge A0 parts */
  3410. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3411. {
  3412. struct drm_device *dev = crtc->dev;
  3413. struct drm_i915_private *dev_priv = to_i915(dev);
  3414. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3415. int pipe = intel_crtc->pipe;
  3416. i915_reg_t reg;
  3417. u32 temp, i, j;
  3418. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3419. for train result */
  3420. reg = FDI_RX_IMR(pipe);
  3421. temp = I915_READ(reg);
  3422. temp &= ~FDI_RX_SYMBOL_LOCK;
  3423. temp &= ~FDI_RX_BIT_LOCK;
  3424. I915_WRITE(reg, temp);
  3425. POSTING_READ(reg);
  3426. udelay(150);
  3427. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3428. I915_READ(FDI_RX_IIR(pipe)));
  3429. /* Try each vswing and preemphasis setting twice before moving on */
  3430. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3431. /* disable first in case we need to retry */
  3432. reg = FDI_TX_CTL(pipe);
  3433. temp = I915_READ(reg);
  3434. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3435. temp &= ~FDI_TX_ENABLE;
  3436. I915_WRITE(reg, temp);
  3437. reg = FDI_RX_CTL(pipe);
  3438. temp = I915_READ(reg);
  3439. temp &= ~FDI_LINK_TRAIN_AUTO;
  3440. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3441. temp &= ~FDI_RX_ENABLE;
  3442. I915_WRITE(reg, temp);
  3443. /* enable CPU FDI TX and PCH FDI RX */
  3444. reg = FDI_TX_CTL(pipe);
  3445. temp = I915_READ(reg);
  3446. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3447. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3448. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3449. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3450. temp |= snb_b_fdi_train_param[j/2];
  3451. temp |= FDI_COMPOSITE_SYNC;
  3452. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3453. I915_WRITE(FDI_RX_MISC(pipe),
  3454. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3455. reg = FDI_RX_CTL(pipe);
  3456. temp = I915_READ(reg);
  3457. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3458. temp |= FDI_COMPOSITE_SYNC;
  3459. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3460. POSTING_READ(reg);
  3461. udelay(1); /* should be 0.5us */
  3462. for (i = 0; i < 4; i++) {
  3463. reg = FDI_RX_IIR(pipe);
  3464. temp = I915_READ(reg);
  3465. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3466. if (temp & FDI_RX_BIT_LOCK ||
  3467. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3468. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3469. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3470. i);
  3471. break;
  3472. }
  3473. udelay(1); /* should be 0.5us */
  3474. }
  3475. if (i == 4) {
  3476. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3477. continue;
  3478. }
  3479. /* Train 2 */
  3480. reg = FDI_TX_CTL(pipe);
  3481. temp = I915_READ(reg);
  3482. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3483. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3484. I915_WRITE(reg, temp);
  3485. reg = FDI_RX_CTL(pipe);
  3486. temp = I915_READ(reg);
  3487. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3488. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3489. I915_WRITE(reg, temp);
  3490. POSTING_READ(reg);
  3491. udelay(2); /* should be 1.5us */
  3492. for (i = 0; i < 4; i++) {
  3493. reg = FDI_RX_IIR(pipe);
  3494. temp = I915_READ(reg);
  3495. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3496. if (temp & FDI_RX_SYMBOL_LOCK ||
  3497. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3498. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3499. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3500. i);
  3501. goto train_done;
  3502. }
  3503. udelay(2); /* should be 1.5us */
  3504. }
  3505. if (i == 4)
  3506. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3507. }
  3508. train_done:
  3509. DRM_DEBUG_KMS("FDI train done.\n");
  3510. }
  3511. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3512. {
  3513. struct drm_device *dev = intel_crtc->base.dev;
  3514. struct drm_i915_private *dev_priv = to_i915(dev);
  3515. int pipe = intel_crtc->pipe;
  3516. i915_reg_t reg;
  3517. u32 temp;
  3518. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3519. reg = FDI_RX_CTL(pipe);
  3520. temp = I915_READ(reg);
  3521. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3522. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3523. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3524. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3525. POSTING_READ(reg);
  3526. udelay(200);
  3527. /* Switch from Rawclk to PCDclk */
  3528. temp = I915_READ(reg);
  3529. I915_WRITE(reg, temp | FDI_PCDCLK);
  3530. POSTING_READ(reg);
  3531. udelay(200);
  3532. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3533. reg = FDI_TX_CTL(pipe);
  3534. temp = I915_READ(reg);
  3535. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3536. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3537. POSTING_READ(reg);
  3538. udelay(100);
  3539. }
  3540. }
  3541. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3542. {
  3543. struct drm_device *dev = intel_crtc->base.dev;
  3544. struct drm_i915_private *dev_priv = to_i915(dev);
  3545. int pipe = intel_crtc->pipe;
  3546. i915_reg_t reg;
  3547. u32 temp;
  3548. /* Switch from PCDclk to Rawclk */
  3549. reg = FDI_RX_CTL(pipe);
  3550. temp = I915_READ(reg);
  3551. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3552. /* Disable CPU FDI TX PLL */
  3553. reg = FDI_TX_CTL(pipe);
  3554. temp = I915_READ(reg);
  3555. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3556. POSTING_READ(reg);
  3557. udelay(100);
  3558. reg = FDI_RX_CTL(pipe);
  3559. temp = I915_READ(reg);
  3560. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3561. /* Wait for the clocks to turn off. */
  3562. POSTING_READ(reg);
  3563. udelay(100);
  3564. }
  3565. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3566. {
  3567. struct drm_device *dev = crtc->dev;
  3568. struct drm_i915_private *dev_priv = to_i915(dev);
  3569. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3570. int pipe = intel_crtc->pipe;
  3571. i915_reg_t reg;
  3572. u32 temp;
  3573. /* disable CPU FDI tx and PCH FDI rx */
  3574. reg = FDI_TX_CTL(pipe);
  3575. temp = I915_READ(reg);
  3576. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3577. POSTING_READ(reg);
  3578. reg = FDI_RX_CTL(pipe);
  3579. temp = I915_READ(reg);
  3580. temp &= ~(0x7 << 16);
  3581. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3582. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3583. POSTING_READ(reg);
  3584. udelay(100);
  3585. /* Ironlake workaround, disable clock pointer after downing FDI */
  3586. if (HAS_PCH_IBX(dev_priv))
  3587. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3588. /* still set train pattern 1 */
  3589. reg = FDI_TX_CTL(pipe);
  3590. temp = I915_READ(reg);
  3591. temp &= ~FDI_LINK_TRAIN_NONE;
  3592. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3593. I915_WRITE(reg, temp);
  3594. reg = FDI_RX_CTL(pipe);
  3595. temp = I915_READ(reg);
  3596. if (HAS_PCH_CPT(dev_priv)) {
  3597. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3598. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3599. } else {
  3600. temp &= ~FDI_LINK_TRAIN_NONE;
  3601. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3602. }
  3603. /* BPC in FDI rx is consistent with that in PIPECONF */
  3604. temp &= ~(0x07 << 16);
  3605. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3606. I915_WRITE(reg, temp);
  3607. POSTING_READ(reg);
  3608. udelay(100);
  3609. }
  3610. bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
  3611. {
  3612. struct intel_crtc *crtc;
  3613. /* Note that we don't need to be called with mode_config.lock here
  3614. * as our list of CRTC objects is static for the lifetime of the
  3615. * device and so cannot disappear as we iterate. Similarly, we can
  3616. * happily treat the predicates as racy, atomic checks as userspace
  3617. * cannot claim and pin a new fb without at least acquring the
  3618. * struct_mutex and so serialising with us.
  3619. */
  3620. for_each_intel_crtc(&dev_priv->drm, crtc) {
  3621. if (atomic_read(&crtc->unpin_work_count) == 0)
  3622. continue;
  3623. if (crtc->flip_work)
  3624. intel_wait_for_vblank(dev_priv, crtc->pipe);
  3625. return true;
  3626. }
  3627. return false;
  3628. }
  3629. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3630. {
  3631. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3632. struct intel_flip_work *work = intel_crtc->flip_work;
  3633. intel_crtc->flip_work = NULL;
  3634. if (work->event)
  3635. drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
  3636. drm_crtc_vblank_put(&intel_crtc->base);
  3637. wake_up_all(&dev_priv->pending_flip_queue);
  3638. trace_i915_flip_complete(intel_crtc->plane,
  3639. work->pending_flip_obj);
  3640. queue_work(dev_priv->wq, &work->unpin_work);
  3641. }
  3642. static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3643. {
  3644. struct drm_device *dev = crtc->dev;
  3645. struct drm_i915_private *dev_priv = to_i915(dev);
  3646. long ret;
  3647. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3648. ret = wait_event_interruptible_timeout(
  3649. dev_priv->pending_flip_queue,
  3650. !intel_crtc_has_pending_flip(crtc),
  3651. 60*HZ);
  3652. if (ret < 0)
  3653. return ret;
  3654. if (ret == 0) {
  3655. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3656. struct intel_flip_work *work;
  3657. spin_lock_irq(&dev->event_lock);
  3658. work = intel_crtc->flip_work;
  3659. if (work && !is_mmio_work(work)) {
  3660. WARN_ONCE(1, "Removing stuck page flip\n");
  3661. page_flip_completed(intel_crtc);
  3662. }
  3663. spin_unlock_irq(&dev->event_lock);
  3664. }
  3665. return 0;
  3666. }
  3667. void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3668. {
  3669. u32 temp;
  3670. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3671. mutex_lock(&dev_priv->sb_lock);
  3672. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3673. temp |= SBI_SSCCTL_DISABLE;
  3674. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3675. mutex_unlock(&dev_priv->sb_lock);
  3676. }
  3677. /* Program iCLKIP clock to the desired frequency */
  3678. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3679. {
  3680. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  3681. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3682. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3683. u32 temp;
  3684. lpt_disable_iclkip(dev_priv);
  3685. /* The iCLK virtual clock root frequency is in MHz,
  3686. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3687. * divisors, it is necessary to divide one by another, so we
  3688. * convert the virtual clock precision to KHz here for higher
  3689. * precision.
  3690. */
  3691. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3692. u32 iclk_virtual_root_freq = 172800 * 1000;
  3693. u32 iclk_pi_range = 64;
  3694. u32 desired_divisor;
  3695. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3696. clock << auxdiv);
  3697. divsel = (desired_divisor / iclk_pi_range) - 2;
  3698. phaseinc = desired_divisor % iclk_pi_range;
  3699. /*
  3700. * Near 20MHz is a corner case which is
  3701. * out of range for the 7-bit divisor
  3702. */
  3703. if (divsel <= 0x7f)
  3704. break;
  3705. }
  3706. /* This should not happen with any sane values */
  3707. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3708. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3709. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3710. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3711. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3712. clock,
  3713. auxdiv,
  3714. divsel,
  3715. phasedir,
  3716. phaseinc);
  3717. mutex_lock(&dev_priv->sb_lock);
  3718. /* Program SSCDIVINTPHASE6 */
  3719. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3720. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3721. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3722. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3723. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3724. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3725. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3726. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3727. /* Program SSCAUXDIV */
  3728. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3729. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3730. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3731. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3732. /* Enable modulator and associated divider */
  3733. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3734. temp &= ~SBI_SSCCTL_DISABLE;
  3735. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3736. mutex_unlock(&dev_priv->sb_lock);
  3737. /* Wait for initialization time */
  3738. udelay(24);
  3739. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3740. }
  3741. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3742. {
  3743. u32 divsel, phaseinc, auxdiv;
  3744. u32 iclk_virtual_root_freq = 172800 * 1000;
  3745. u32 iclk_pi_range = 64;
  3746. u32 desired_divisor;
  3747. u32 temp;
  3748. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3749. return 0;
  3750. mutex_lock(&dev_priv->sb_lock);
  3751. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3752. if (temp & SBI_SSCCTL_DISABLE) {
  3753. mutex_unlock(&dev_priv->sb_lock);
  3754. return 0;
  3755. }
  3756. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3757. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3758. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3759. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3760. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3761. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3762. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3763. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3764. mutex_unlock(&dev_priv->sb_lock);
  3765. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3766. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3767. desired_divisor << auxdiv);
  3768. }
  3769. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3770. enum pipe pch_transcoder)
  3771. {
  3772. struct drm_device *dev = crtc->base.dev;
  3773. struct drm_i915_private *dev_priv = to_i915(dev);
  3774. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3775. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3776. I915_READ(HTOTAL(cpu_transcoder)));
  3777. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3778. I915_READ(HBLANK(cpu_transcoder)));
  3779. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3780. I915_READ(HSYNC(cpu_transcoder)));
  3781. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3782. I915_READ(VTOTAL(cpu_transcoder)));
  3783. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3784. I915_READ(VBLANK(cpu_transcoder)));
  3785. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3786. I915_READ(VSYNC(cpu_transcoder)));
  3787. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3788. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3789. }
  3790. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3791. {
  3792. struct drm_i915_private *dev_priv = to_i915(dev);
  3793. uint32_t temp;
  3794. temp = I915_READ(SOUTH_CHICKEN1);
  3795. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3796. return;
  3797. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3798. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3799. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3800. if (enable)
  3801. temp |= FDI_BC_BIFURCATION_SELECT;
  3802. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3803. I915_WRITE(SOUTH_CHICKEN1, temp);
  3804. POSTING_READ(SOUTH_CHICKEN1);
  3805. }
  3806. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3807. {
  3808. struct drm_device *dev = intel_crtc->base.dev;
  3809. switch (intel_crtc->pipe) {
  3810. case PIPE_A:
  3811. break;
  3812. case PIPE_B:
  3813. if (intel_crtc->config->fdi_lanes > 2)
  3814. cpt_set_fdi_bc_bifurcation(dev, false);
  3815. else
  3816. cpt_set_fdi_bc_bifurcation(dev, true);
  3817. break;
  3818. case PIPE_C:
  3819. cpt_set_fdi_bc_bifurcation(dev, true);
  3820. break;
  3821. default:
  3822. BUG();
  3823. }
  3824. }
  3825. /* Return which DP Port should be selected for Transcoder DP control */
  3826. static enum port
  3827. intel_trans_dp_port_sel(struct drm_crtc *crtc)
  3828. {
  3829. struct drm_device *dev = crtc->dev;
  3830. struct intel_encoder *encoder;
  3831. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3832. if (encoder->type == INTEL_OUTPUT_DP ||
  3833. encoder->type == INTEL_OUTPUT_EDP)
  3834. return enc_to_dig_port(&encoder->base)->port;
  3835. }
  3836. return -1;
  3837. }
  3838. /*
  3839. * Enable PCH resources required for PCH ports:
  3840. * - PCH PLLs
  3841. * - FDI training & RX/TX
  3842. * - update transcoder timings
  3843. * - DP transcoding bits
  3844. * - transcoder
  3845. */
  3846. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3847. {
  3848. struct drm_device *dev = crtc->dev;
  3849. struct drm_i915_private *dev_priv = to_i915(dev);
  3850. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3851. int pipe = intel_crtc->pipe;
  3852. u32 temp;
  3853. assert_pch_transcoder_disabled(dev_priv, pipe);
  3854. if (IS_IVYBRIDGE(dev_priv))
  3855. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3856. /* Write the TU size bits before fdi link training, so that error
  3857. * detection works. */
  3858. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3859. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3860. /* For PCH output, training FDI link */
  3861. dev_priv->display.fdi_link_train(crtc);
  3862. /* We need to program the right clock selection before writing the pixel
  3863. * mutliplier into the DPLL. */
  3864. if (HAS_PCH_CPT(dev_priv)) {
  3865. u32 sel;
  3866. temp = I915_READ(PCH_DPLL_SEL);
  3867. temp |= TRANS_DPLL_ENABLE(pipe);
  3868. sel = TRANS_DPLLB_SEL(pipe);
  3869. if (intel_crtc->config->shared_dpll ==
  3870. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3871. temp |= sel;
  3872. else
  3873. temp &= ~sel;
  3874. I915_WRITE(PCH_DPLL_SEL, temp);
  3875. }
  3876. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3877. * transcoder, and we actually should do this to not upset any PCH
  3878. * transcoder that already use the clock when we share it.
  3879. *
  3880. * Note that enable_shared_dpll tries to do the right thing, but
  3881. * get_shared_dpll unconditionally resets the pll - we need that to have
  3882. * the right LVDS enable sequence. */
  3883. intel_enable_shared_dpll(intel_crtc);
  3884. /* set transcoder timing, panel must allow it */
  3885. assert_panel_unlocked(dev_priv, pipe);
  3886. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3887. intel_fdi_normal_train(crtc);
  3888. /* For PCH DP, enable TRANS_DP_CTL */
  3889. if (HAS_PCH_CPT(dev_priv) &&
  3890. intel_crtc_has_dp_encoder(intel_crtc->config)) {
  3891. const struct drm_display_mode *adjusted_mode =
  3892. &intel_crtc->config->base.adjusted_mode;
  3893. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3894. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3895. temp = I915_READ(reg);
  3896. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3897. TRANS_DP_SYNC_MASK |
  3898. TRANS_DP_BPC_MASK);
  3899. temp |= TRANS_DP_OUTPUT_ENABLE;
  3900. temp |= bpc << 9; /* same format but at 11:9 */
  3901. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3902. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3903. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3904. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3905. switch (intel_trans_dp_port_sel(crtc)) {
  3906. case PORT_B:
  3907. temp |= TRANS_DP_PORT_SEL_B;
  3908. break;
  3909. case PORT_C:
  3910. temp |= TRANS_DP_PORT_SEL_C;
  3911. break;
  3912. case PORT_D:
  3913. temp |= TRANS_DP_PORT_SEL_D;
  3914. break;
  3915. default:
  3916. BUG();
  3917. }
  3918. I915_WRITE(reg, temp);
  3919. }
  3920. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3921. }
  3922. static void lpt_pch_enable(struct drm_crtc *crtc)
  3923. {
  3924. struct drm_device *dev = crtc->dev;
  3925. struct drm_i915_private *dev_priv = to_i915(dev);
  3926. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3927. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3928. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3929. lpt_program_iclkip(crtc);
  3930. /* Set transcoder timing. */
  3931. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3932. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3933. }
  3934. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3935. {
  3936. struct drm_i915_private *dev_priv = to_i915(dev);
  3937. i915_reg_t dslreg = PIPEDSL(pipe);
  3938. u32 temp;
  3939. temp = I915_READ(dslreg);
  3940. udelay(500);
  3941. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3942. if (wait_for(I915_READ(dslreg) != temp, 5))
  3943. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3944. }
  3945. }
  3946. static int
  3947. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3948. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3949. int src_w, int src_h, int dst_w, int dst_h)
  3950. {
  3951. struct intel_crtc_scaler_state *scaler_state =
  3952. &crtc_state->scaler_state;
  3953. struct intel_crtc *intel_crtc =
  3954. to_intel_crtc(crtc_state->base.crtc);
  3955. int need_scaling;
  3956. need_scaling = drm_rotation_90_or_270(rotation) ?
  3957. (src_h != dst_w || src_w != dst_h):
  3958. (src_w != dst_w || src_h != dst_h);
  3959. /*
  3960. * if plane is being disabled or scaler is no more required or force detach
  3961. * - free scaler binded to this plane/crtc
  3962. * - in order to do this, update crtc->scaler_usage
  3963. *
  3964. * Here scaler state in crtc_state is set free so that
  3965. * scaler can be assigned to other user. Actual register
  3966. * update to free the scaler is done in plane/panel-fit programming.
  3967. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3968. */
  3969. if (force_detach || !need_scaling) {
  3970. if (*scaler_id >= 0) {
  3971. scaler_state->scaler_users &= ~(1 << scaler_user);
  3972. scaler_state->scalers[*scaler_id].in_use = 0;
  3973. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3974. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3975. intel_crtc->pipe, scaler_user, *scaler_id,
  3976. scaler_state->scaler_users);
  3977. *scaler_id = -1;
  3978. }
  3979. return 0;
  3980. }
  3981. /* range checks */
  3982. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3983. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3984. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3985. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3986. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3987. "size is out of scaler range\n",
  3988. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3989. return -EINVAL;
  3990. }
  3991. /* mark this plane as a scaler user in crtc_state */
  3992. scaler_state->scaler_users |= (1 << scaler_user);
  3993. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3994. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3995. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3996. scaler_state->scaler_users);
  3997. return 0;
  3998. }
  3999. /**
  4000. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  4001. *
  4002. * @state: crtc's scaler state
  4003. *
  4004. * Return
  4005. * 0 - scaler_usage updated successfully
  4006. * error - requested scaling cannot be supported or other error condition
  4007. */
  4008. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  4009. {
  4010. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  4011. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  4012. &state->scaler_state.scaler_id, DRM_ROTATE_0,
  4013. state->pipe_src_w, state->pipe_src_h,
  4014. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  4015. }
  4016. /**
  4017. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  4018. *
  4019. * @state: crtc's scaler state
  4020. * @plane_state: atomic plane state to update
  4021. *
  4022. * Return
  4023. * 0 - scaler_usage updated successfully
  4024. * error - requested scaling cannot be supported or other error condition
  4025. */
  4026. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  4027. struct intel_plane_state *plane_state)
  4028. {
  4029. struct intel_plane *intel_plane =
  4030. to_intel_plane(plane_state->base.plane);
  4031. struct drm_framebuffer *fb = plane_state->base.fb;
  4032. int ret;
  4033. bool force_detach = !fb || !plane_state->base.visible;
  4034. ret = skl_update_scaler(crtc_state, force_detach,
  4035. drm_plane_index(&intel_plane->base),
  4036. &plane_state->scaler_id,
  4037. plane_state->base.rotation,
  4038. drm_rect_width(&plane_state->base.src) >> 16,
  4039. drm_rect_height(&plane_state->base.src) >> 16,
  4040. drm_rect_width(&plane_state->base.dst),
  4041. drm_rect_height(&plane_state->base.dst));
  4042. if (ret || plane_state->scaler_id < 0)
  4043. return ret;
  4044. /* check colorkey */
  4045. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  4046. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  4047. intel_plane->base.base.id,
  4048. intel_plane->base.name);
  4049. return -EINVAL;
  4050. }
  4051. /* Check src format */
  4052. switch (fb->format->format) {
  4053. case DRM_FORMAT_RGB565:
  4054. case DRM_FORMAT_XBGR8888:
  4055. case DRM_FORMAT_XRGB8888:
  4056. case DRM_FORMAT_ABGR8888:
  4057. case DRM_FORMAT_ARGB8888:
  4058. case DRM_FORMAT_XRGB2101010:
  4059. case DRM_FORMAT_XBGR2101010:
  4060. case DRM_FORMAT_YUYV:
  4061. case DRM_FORMAT_YVYU:
  4062. case DRM_FORMAT_UYVY:
  4063. case DRM_FORMAT_VYUY:
  4064. break;
  4065. default:
  4066. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  4067. intel_plane->base.base.id, intel_plane->base.name,
  4068. fb->base.id, fb->format->format);
  4069. return -EINVAL;
  4070. }
  4071. return 0;
  4072. }
  4073. static void skylake_scaler_disable(struct intel_crtc *crtc)
  4074. {
  4075. int i;
  4076. for (i = 0; i < crtc->num_scalers; i++)
  4077. skl_detach_scaler(crtc, i);
  4078. }
  4079. static void skylake_pfit_enable(struct intel_crtc *crtc)
  4080. {
  4081. struct drm_device *dev = crtc->base.dev;
  4082. struct drm_i915_private *dev_priv = to_i915(dev);
  4083. int pipe = crtc->pipe;
  4084. struct intel_crtc_scaler_state *scaler_state =
  4085. &crtc->config->scaler_state;
  4086. if (crtc->config->pch_pfit.enabled) {
  4087. int id;
  4088. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0))
  4089. return;
  4090. id = scaler_state->scaler_id;
  4091. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  4092. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  4093. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  4094. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  4095. }
  4096. }
  4097. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  4098. {
  4099. struct drm_device *dev = crtc->base.dev;
  4100. struct drm_i915_private *dev_priv = to_i915(dev);
  4101. int pipe = crtc->pipe;
  4102. if (crtc->config->pch_pfit.enabled) {
  4103. /* Force use of hard-coded filter coefficients
  4104. * as some pre-programmed values are broken,
  4105. * e.g. x201.
  4106. */
  4107. if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
  4108. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  4109. PF_PIPE_SEL_IVB(pipe));
  4110. else
  4111. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  4112. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  4113. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  4114. }
  4115. }
  4116. void hsw_enable_ips(struct intel_crtc *crtc)
  4117. {
  4118. struct drm_device *dev = crtc->base.dev;
  4119. struct drm_i915_private *dev_priv = to_i915(dev);
  4120. if (!crtc->config->ips_enabled)
  4121. return;
  4122. /*
  4123. * We can only enable IPS after we enable a plane and wait for a vblank
  4124. * This function is called from post_plane_update, which is run after
  4125. * a vblank wait.
  4126. */
  4127. assert_plane_enabled(dev_priv, crtc->plane);
  4128. if (IS_BROADWELL(dev_priv)) {
  4129. mutex_lock(&dev_priv->rps.hw_lock);
  4130. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  4131. mutex_unlock(&dev_priv->rps.hw_lock);
  4132. /* Quoting Art Runyan: "its not safe to expect any particular
  4133. * value in IPS_CTL bit 31 after enabling IPS through the
  4134. * mailbox." Moreover, the mailbox may return a bogus state,
  4135. * so we need to just enable it and continue on.
  4136. */
  4137. } else {
  4138. I915_WRITE(IPS_CTL, IPS_ENABLE);
  4139. /* The bit only becomes 1 in the next vblank, so this wait here
  4140. * is essentially intel_wait_for_vblank. If we don't have this
  4141. * and don't wait for vblanks until the end of crtc_enable, then
  4142. * the HW state readout code will complain that the expected
  4143. * IPS_CTL value is not the one we read. */
  4144. if (intel_wait_for_register(dev_priv,
  4145. IPS_CTL, IPS_ENABLE, IPS_ENABLE,
  4146. 50))
  4147. DRM_ERROR("Timed out waiting for IPS enable\n");
  4148. }
  4149. }
  4150. void hsw_disable_ips(struct intel_crtc *crtc)
  4151. {
  4152. struct drm_device *dev = crtc->base.dev;
  4153. struct drm_i915_private *dev_priv = to_i915(dev);
  4154. if (!crtc->config->ips_enabled)
  4155. return;
  4156. assert_plane_enabled(dev_priv, crtc->plane);
  4157. if (IS_BROADWELL(dev_priv)) {
  4158. mutex_lock(&dev_priv->rps.hw_lock);
  4159. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  4160. mutex_unlock(&dev_priv->rps.hw_lock);
  4161. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  4162. if (intel_wait_for_register(dev_priv,
  4163. IPS_CTL, IPS_ENABLE, 0,
  4164. 42))
  4165. DRM_ERROR("Timed out waiting for IPS disable\n");
  4166. } else {
  4167. I915_WRITE(IPS_CTL, 0);
  4168. POSTING_READ(IPS_CTL);
  4169. }
  4170. /* We need to wait for a vblank before we can disable the plane. */
  4171. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4172. }
  4173. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  4174. {
  4175. if (intel_crtc->overlay) {
  4176. struct drm_device *dev = intel_crtc->base.dev;
  4177. struct drm_i915_private *dev_priv = to_i915(dev);
  4178. mutex_lock(&dev->struct_mutex);
  4179. dev_priv->mm.interruptible = false;
  4180. (void) intel_overlay_switch_off(intel_crtc->overlay);
  4181. dev_priv->mm.interruptible = true;
  4182. mutex_unlock(&dev->struct_mutex);
  4183. }
  4184. /* Let userspace switch the overlay on again. In most cases userspace
  4185. * has to recompute where to put it anyway.
  4186. */
  4187. }
  4188. /**
  4189. * intel_post_enable_primary - Perform operations after enabling primary plane
  4190. * @crtc: the CRTC whose primary plane was just enabled
  4191. *
  4192. * Performs potentially sleeping operations that must be done after the primary
  4193. * plane is enabled, such as updating FBC and IPS. Note that this may be
  4194. * called due to an explicit primary plane update, or due to an implicit
  4195. * re-enable that is caused when a sprite plane is updated to no longer
  4196. * completely hide the primary plane.
  4197. */
  4198. static void
  4199. intel_post_enable_primary(struct drm_crtc *crtc)
  4200. {
  4201. struct drm_device *dev = crtc->dev;
  4202. struct drm_i915_private *dev_priv = to_i915(dev);
  4203. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4204. int pipe = intel_crtc->pipe;
  4205. /*
  4206. * FIXME IPS should be fine as long as one plane is
  4207. * enabled, but in practice it seems to have problems
  4208. * when going from primary only to sprite only and vice
  4209. * versa.
  4210. */
  4211. hsw_enable_ips(intel_crtc);
  4212. /*
  4213. * Gen2 reports pipe underruns whenever all planes are disabled.
  4214. * So don't enable underrun reporting before at least some planes
  4215. * are enabled.
  4216. * FIXME: Need to fix the logic to work when we turn off all planes
  4217. * but leave the pipe running.
  4218. */
  4219. if (IS_GEN2(dev_priv))
  4220. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4221. /* Underruns don't always raise interrupts, so check manually. */
  4222. intel_check_cpu_fifo_underruns(dev_priv);
  4223. intel_check_pch_fifo_underruns(dev_priv);
  4224. }
  4225. /* FIXME move all this to pre_plane_update() with proper state tracking */
  4226. static void
  4227. intel_pre_disable_primary(struct drm_crtc *crtc)
  4228. {
  4229. struct drm_device *dev = crtc->dev;
  4230. struct drm_i915_private *dev_priv = to_i915(dev);
  4231. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4232. int pipe = intel_crtc->pipe;
  4233. /*
  4234. * Gen2 reports pipe underruns whenever all planes are disabled.
  4235. * So diasble underrun reporting before all the planes get disabled.
  4236. * FIXME: Need to fix the logic to work when we turn off all planes
  4237. * but leave the pipe running.
  4238. */
  4239. if (IS_GEN2(dev_priv))
  4240. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4241. /*
  4242. * FIXME IPS should be fine as long as one plane is
  4243. * enabled, but in practice it seems to have problems
  4244. * when going from primary only to sprite only and vice
  4245. * versa.
  4246. */
  4247. hsw_disable_ips(intel_crtc);
  4248. }
  4249. /* FIXME get rid of this and use pre_plane_update */
  4250. static void
  4251. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  4252. {
  4253. struct drm_device *dev = crtc->dev;
  4254. struct drm_i915_private *dev_priv = to_i915(dev);
  4255. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4256. int pipe = intel_crtc->pipe;
  4257. intel_pre_disable_primary(crtc);
  4258. /*
  4259. * Vblank time updates from the shadow to live plane control register
  4260. * are blocked if the memory self-refresh mode is active at that
  4261. * moment. So to make sure the plane gets truly disabled, disable
  4262. * first the self-refresh mode. The self-refresh enable bit in turn
  4263. * will be checked/applied by the HW only at the next frame start
  4264. * event which is after the vblank start event, so we need to have a
  4265. * wait-for-vblank between disabling the plane and the pipe.
  4266. */
  4267. if (HAS_GMCH_DISPLAY(dev_priv) &&
  4268. intel_set_memory_cxsr(dev_priv, false))
  4269. intel_wait_for_vblank(dev_priv, pipe);
  4270. }
  4271. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  4272. {
  4273. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4274. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4275. struct intel_crtc_state *pipe_config =
  4276. to_intel_crtc_state(crtc->base.state);
  4277. struct drm_plane *primary = crtc->base.primary;
  4278. struct drm_plane_state *old_pri_state =
  4279. drm_atomic_get_existing_plane_state(old_state, primary);
  4280. intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
  4281. crtc->wm.cxsr_allowed = true;
  4282. if (pipe_config->update_wm_post && pipe_config->base.active)
  4283. intel_update_watermarks(crtc);
  4284. if (old_pri_state) {
  4285. struct intel_plane_state *primary_state =
  4286. to_intel_plane_state(primary->state);
  4287. struct intel_plane_state *old_primary_state =
  4288. to_intel_plane_state(old_pri_state);
  4289. intel_fbc_post_update(crtc);
  4290. if (primary_state->base.visible &&
  4291. (needs_modeset(&pipe_config->base) ||
  4292. !old_primary_state->base.visible))
  4293. intel_post_enable_primary(&crtc->base);
  4294. }
  4295. }
  4296. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
  4297. {
  4298. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4299. struct drm_device *dev = crtc->base.dev;
  4300. struct drm_i915_private *dev_priv = to_i915(dev);
  4301. struct intel_crtc_state *pipe_config =
  4302. to_intel_crtc_state(crtc->base.state);
  4303. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4304. struct drm_plane *primary = crtc->base.primary;
  4305. struct drm_plane_state *old_pri_state =
  4306. drm_atomic_get_existing_plane_state(old_state, primary);
  4307. bool modeset = needs_modeset(&pipe_config->base);
  4308. struct intel_atomic_state *old_intel_state =
  4309. to_intel_atomic_state(old_state);
  4310. if (old_pri_state) {
  4311. struct intel_plane_state *primary_state =
  4312. to_intel_plane_state(primary->state);
  4313. struct intel_plane_state *old_primary_state =
  4314. to_intel_plane_state(old_pri_state);
  4315. intel_fbc_pre_update(crtc, pipe_config, primary_state);
  4316. if (old_primary_state->base.visible &&
  4317. (modeset || !primary_state->base.visible))
  4318. intel_pre_disable_primary(&crtc->base);
  4319. }
  4320. if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev_priv)) {
  4321. crtc->wm.cxsr_allowed = false;
  4322. /*
  4323. * Vblank time updates from the shadow to live plane control register
  4324. * are blocked if the memory self-refresh mode is active at that
  4325. * moment. So to make sure the plane gets truly disabled, disable
  4326. * first the self-refresh mode. The self-refresh enable bit in turn
  4327. * will be checked/applied by the HW only at the next frame start
  4328. * event which is after the vblank start event, so we need to have a
  4329. * wait-for-vblank between disabling the plane and the pipe.
  4330. */
  4331. if (old_crtc_state->base.active &&
  4332. intel_set_memory_cxsr(dev_priv, false))
  4333. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4334. }
  4335. /*
  4336. * IVB workaround: must disable low power watermarks for at least
  4337. * one frame before enabling scaling. LP watermarks can be re-enabled
  4338. * when scaling is disabled.
  4339. *
  4340. * WaCxSRDisabledForSpriteScaling:ivb
  4341. */
  4342. if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev))
  4343. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4344. /*
  4345. * If we're doing a modeset, we're done. No need to do any pre-vblank
  4346. * watermark programming here.
  4347. */
  4348. if (needs_modeset(&pipe_config->base))
  4349. return;
  4350. /*
  4351. * For platforms that support atomic watermarks, program the
  4352. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  4353. * will be the intermediate values that are safe for both pre- and
  4354. * post- vblank; when vblank happens, the 'active' values will be set
  4355. * to the final 'target' values and we'll do this again to get the
  4356. * optimal watermarks. For gen9+ platforms, the values we program here
  4357. * will be the final target values which will get automatically latched
  4358. * at vblank time; no further programming will be necessary.
  4359. *
  4360. * If a platform hasn't been transitioned to atomic watermarks yet,
  4361. * we'll continue to update watermarks the old way, if flags tell
  4362. * us to.
  4363. */
  4364. if (dev_priv->display.initial_watermarks != NULL)
  4365. dev_priv->display.initial_watermarks(old_intel_state,
  4366. pipe_config);
  4367. else if (pipe_config->update_wm_pre)
  4368. intel_update_watermarks(crtc);
  4369. }
  4370. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4371. {
  4372. struct drm_device *dev = crtc->dev;
  4373. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4374. struct drm_plane *p;
  4375. int pipe = intel_crtc->pipe;
  4376. intel_crtc_dpms_overlay_disable(intel_crtc);
  4377. drm_for_each_plane_mask(p, dev, plane_mask)
  4378. to_intel_plane(p)->disable_plane(p, crtc);
  4379. /*
  4380. * FIXME: Once we grow proper nuclear flip support out of this we need
  4381. * to compute the mask of flip planes precisely. For the time being
  4382. * consider this a flip to a NULL plane.
  4383. */
  4384. intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4385. }
  4386. static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
  4387. struct intel_crtc_state *crtc_state,
  4388. struct drm_atomic_state *old_state)
  4389. {
  4390. struct drm_connector_state *old_conn_state;
  4391. struct drm_connector *conn;
  4392. int i;
  4393. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4394. struct drm_connector_state *conn_state = conn->state;
  4395. struct intel_encoder *encoder =
  4396. to_intel_encoder(conn_state->best_encoder);
  4397. if (conn_state->crtc != crtc)
  4398. continue;
  4399. if (encoder->pre_pll_enable)
  4400. encoder->pre_pll_enable(encoder, crtc_state, conn_state);
  4401. }
  4402. }
  4403. static void intel_encoders_pre_enable(struct drm_crtc *crtc,
  4404. struct intel_crtc_state *crtc_state,
  4405. struct drm_atomic_state *old_state)
  4406. {
  4407. struct drm_connector_state *old_conn_state;
  4408. struct drm_connector *conn;
  4409. int i;
  4410. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4411. struct drm_connector_state *conn_state = conn->state;
  4412. struct intel_encoder *encoder =
  4413. to_intel_encoder(conn_state->best_encoder);
  4414. if (conn_state->crtc != crtc)
  4415. continue;
  4416. if (encoder->pre_enable)
  4417. encoder->pre_enable(encoder, crtc_state, conn_state);
  4418. }
  4419. }
  4420. static void intel_encoders_enable(struct drm_crtc *crtc,
  4421. struct intel_crtc_state *crtc_state,
  4422. struct drm_atomic_state *old_state)
  4423. {
  4424. struct drm_connector_state *old_conn_state;
  4425. struct drm_connector *conn;
  4426. int i;
  4427. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4428. struct drm_connector_state *conn_state = conn->state;
  4429. struct intel_encoder *encoder =
  4430. to_intel_encoder(conn_state->best_encoder);
  4431. if (conn_state->crtc != crtc)
  4432. continue;
  4433. encoder->enable(encoder, crtc_state, conn_state);
  4434. intel_opregion_notify_encoder(encoder, true);
  4435. }
  4436. }
  4437. static void intel_encoders_disable(struct drm_crtc *crtc,
  4438. struct intel_crtc_state *old_crtc_state,
  4439. struct drm_atomic_state *old_state)
  4440. {
  4441. struct drm_connector_state *old_conn_state;
  4442. struct drm_connector *conn;
  4443. int i;
  4444. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4445. struct intel_encoder *encoder =
  4446. to_intel_encoder(old_conn_state->best_encoder);
  4447. if (old_conn_state->crtc != crtc)
  4448. continue;
  4449. intel_opregion_notify_encoder(encoder, false);
  4450. encoder->disable(encoder, old_crtc_state, old_conn_state);
  4451. }
  4452. }
  4453. static void intel_encoders_post_disable(struct drm_crtc *crtc,
  4454. struct intel_crtc_state *old_crtc_state,
  4455. struct drm_atomic_state *old_state)
  4456. {
  4457. struct drm_connector_state *old_conn_state;
  4458. struct drm_connector *conn;
  4459. int i;
  4460. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4461. struct intel_encoder *encoder =
  4462. to_intel_encoder(old_conn_state->best_encoder);
  4463. if (old_conn_state->crtc != crtc)
  4464. continue;
  4465. if (encoder->post_disable)
  4466. encoder->post_disable(encoder, old_crtc_state, old_conn_state);
  4467. }
  4468. }
  4469. static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
  4470. struct intel_crtc_state *old_crtc_state,
  4471. struct drm_atomic_state *old_state)
  4472. {
  4473. struct drm_connector_state *old_conn_state;
  4474. struct drm_connector *conn;
  4475. int i;
  4476. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4477. struct intel_encoder *encoder =
  4478. to_intel_encoder(old_conn_state->best_encoder);
  4479. if (old_conn_state->crtc != crtc)
  4480. continue;
  4481. if (encoder->post_pll_disable)
  4482. encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
  4483. }
  4484. }
  4485. static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
  4486. struct drm_atomic_state *old_state)
  4487. {
  4488. struct drm_crtc *crtc = pipe_config->base.crtc;
  4489. struct drm_device *dev = crtc->dev;
  4490. struct drm_i915_private *dev_priv = to_i915(dev);
  4491. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4492. int pipe = intel_crtc->pipe;
  4493. struct intel_atomic_state *old_intel_state =
  4494. to_intel_atomic_state(old_state);
  4495. if (WARN_ON(intel_crtc->active))
  4496. return;
  4497. /*
  4498. * Sometimes spurious CPU pipe underruns happen during FDI
  4499. * training, at least with VGA+HDMI cloning. Suppress them.
  4500. *
  4501. * On ILK we get an occasional spurious CPU pipe underruns
  4502. * between eDP port A enable and vdd enable. Also PCH port
  4503. * enable seems to result in the occasional CPU pipe underrun.
  4504. *
  4505. * Spurious PCH underruns also occur during PCH enabling.
  4506. */
  4507. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4508. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4509. if (intel_crtc->config->has_pch_encoder)
  4510. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4511. if (intel_crtc->config->has_pch_encoder)
  4512. intel_prepare_shared_dpll(intel_crtc);
  4513. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4514. intel_dp_set_m_n(intel_crtc, M1_N1);
  4515. intel_set_pipe_timings(intel_crtc);
  4516. intel_set_pipe_src_size(intel_crtc);
  4517. if (intel_crtc->config->has_pch_encoder) {
  4518. intel_cpu_transcoder_set_m_n(intel_crtc,
  4519. &intel_crtc->config->fdi_m_n, NULL);
  4520. }
  4521. ironlake_set_pipeconf(crtc);
  4522. intel_crtc->active = true;
  4523. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4524. if (intel_crtc->config->has_pch_encoder) {
  4525. /* Note: FDI PLL enabling _must_ be done before we enable the
  4526. * cpu pipes, hence this is separate from all the other fdi/pch
  4527. * enabling. */
  4528. ironlake_fdi_pll_enable(intel_crtc);
  4529. } else {
  4530. assert_fdi_tx_disabled(dev_priv, pipe);
  4531. assert_fdi_rx_disabled(dev_priv, pipe);
  4532. }
  4533. ironlake_pfit_enable(intel_crtc);
  4534. /*
  4535. * On ILK+ LUT must be loaded before the pipe is running but with
  4536. * clocks enabled
  4537. */
  4538. intel_color_load_luts(&pipe_config->base);
  4539. if (dev_priv->display.initial_watermarks != NULL)
  4540. dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
  4541. intel_enable_pipe(intel_crtc);
  4542. if (intel_crtc->config->has_pch_encoder)
  4543. ironlake_pch_enable(crtc);
  4544. assert_vblank_disabled(crtc);
  4545. drm_crtc_vblank_on(crtc);
  4546. intel_encoders_enable(crtc, pipe_config, old_state);
  4547. if (HAS_PCH_CPT(dev_priv))
  4548. cpt_verify_modeset(dev, intel_crtc->pipe);
  4549. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4550. if (intel_crtc->config->has_pch_encoder)
  4551. intel_wait_for_vblank(dev_priv, pipe);
  4552. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4553. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4554. }
  4555. /* IPS only exists on ULT machines and is tied to pipe A. */
  4556. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4557. {
  4558. return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
  4559. }
  4560. static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
  4561. struct drm_atomic_state *old_state)
  4562. {
  4563. struct drm_crtc *crtc = pipe_config->base.crtc;
  4564. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4565. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4566. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4567. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4568. struct intel_atomic_state *old_intel_state =
  4569. to_intel_atomic_state(old_state);
  4570. if (WARN_ON(intel_crtc->active))
  4571. return;
  4572. if (intel_crtc->config->has_pch_encoder)
  4573. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4574. false);
  4575. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4576. if (intel_crtc->config->shared_dpll)
  4577. intel_enable_shared_dpll(intel_crtc);
  4578. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4579. intel_dp_set_m_n(intel_crtc, M1_N1);
  4580. if (!transcoder_is_dsi(cpu_transcoder))
  4581. intel_set_pipe_timings(intel_crtc);
  4582. intel_set_pipe_src_size(intel_crtc);
  4583. if (cpu_transcoder != TRANSCODER_EDP &&
  4584. !transcoder_is_dsi(cpu_transcoder)) {
  4585. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4586. intel_crtc->config->pixel_multiplier - 1);
  4587. }
  4588. if (intel_crtc->config->has_pch_encoder) {
  4589. intel_cpu_transcoder_set_m_n(intel_crtc,
  4590. &intel_crtc->config->fdi_m_n, NULL);
  4591. }
  4592. if (!transcoder_is_dsi(cpu_transcoder))
  4593. haswell_set_pipeconf(crtc);
  4594. haswell_set_pipemisc(crtc);
  4595. intel_color_set_csc(&pipe_config->base);
  4596. intel_crtc->active = true;
  4597. if (intel_crtc->config->has_pch_encoder)
  4598. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4599. else
  4600. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4601. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4602. if (intel_crtc->config->has_pch_encoder)
  4603. dev_priv->display.fdi_link_train(crtc);
  4604. if (!transcoder_is_dsi(cpu_transcoder))
  4605. intel_ddi_enable_pipe_clock(intel_crtc);
  4606. if (INTEL_GEN(dev_priv) >= 9)
  4607. skylake_pfit_enable(intel_crtc);
  4608. else
  4609. ironlake_pfit_enable(intel_crtc);
  4610. /*
  4611. * On ILK+ LUT must be loaded before the pipe is running but with
  4612. * clocks enabled
  4613. */
  4614. intel_color_load_luts(&pipe_config->base);
  4615. intel_ddi_set_pipe_settings(crtc);
  4616. if (!transcoder_is_dsi(cpu_transcoder))
  4617. intel_ddi_enable_transcoder_func(crtc);
  4618. if (dev_priv->display.initial_watermarks != NULL)
  4619. dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
  4620. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4621. if (!transcoder_is_dsi(cpu_transcoder))
  4622. intel_enable_pipe(intel_crtc);
  4623. if (intel_crtc->config->has_pch_encoder)
  4624. lpt_pch_enable(crtc);
  4625. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4626. intel_ddi_set_vc_payload_alloc(crtc, true);
  4627. assert_vblank_disabled(crtc);
  4628. drm_crtc_vblank_on(crtc);
  4629. intel_encoders_enable(crtc, pipe_config, old_state);
  4630. if (intel_crtc->config->has_pch_encoder) {
  4631. intel_wait_for_vblank(dev_priv, pipe);
  4632. intel_wait_for_vblank(dev_priv, pipe);
  4633. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4634. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4635. true);
  4636. }
  4637. /* If we change the relative order between pipe/planes enabling, we need
  4638. * to change the workaround. */
  4639. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4640. if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
  4641. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4642. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4643. }
  4644. }
  4645. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4646. {
  4647. struct drm_device *dev = crtc->base.dev;
  4648. struct drm_i915_private *dev_priv = to_i915(dev);
  4649. int pipe = crtc->pipe;
  4650. /* To avoid upsetting the power well on haswell only disable the pfit if
  4651. * it's in use. The hw state code will make sure we get this right. */
  4652. if (force || crtc->config->pch_pfit.enabled) {
  4653. I915_WRITE(PF_CTL(pipe), 0);
  4654. I915_WRITE(PF_WIN_POS(pipe), 0);
  4655. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4656. }
  4657. }
  4658. static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4659. struct drm_atomic_state *old_state)
  4660. {
  4661. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4662. struct drm_device *dev = crtc->dev;
  4663. struct drm_i915_private *dev_priv = to_i915(dev);
  4664. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4665. int pipe = intel_crtc->pipe;
  4666. /*
  4667. * Sometimes spurious CPU pipe underruns happen when the
  4668. * pipe is already disabled, but FDI RX/TX is still enabled.
  4669. * Happens at least with VGA+HDMI cloning. Suppress them.
  4670. */
  4671. if (intel_crtc->config->has_pch_encoder) {
  4672. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4673. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4674. }
  4675. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4676. drm_crtc_vblank_off(crtc);
  4677. assert_vblank_disabled(crtc);
  4678. intel_disable_pipe(intel_crtc);
  4679. ironlake_pfit_disable(intel_crtc, false);
  4680. if (intel_crtc->config->has_pch_encoder)
  4681. ironlake_fdi_disable(crtc);
  4682. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4683. if (intel_crtc->config->has_pch_encoder) {
  4684. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4685. if (HAS_PCH_CPT(dev_priv)) {
  4686. i915_reg_t reg;
  4687. u32 temp;
  4688. /* disable TRANS_DP_CTL */
  4689. reg = TRANS_DP_CTL(pipe);
  4690. temp = I915_READ(reg);
  4691. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4692. TRANS_DP_PORT_SEL_MASK);
  4693. temp |= TRANS_DP_PORT_SEL_NONE;
  4694. I915_WRITE(reg, temp);
  4695. /* disable DPLL_SEL */
  4696. temp = I915_READ(PCH_DPLL_SEL);
  4697. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4698. I915_WRITE(PCH_DPLL_SEL, temp);
  4699. }
  4700. ironlake_fdi_pll_disable(intel_crtc);
  4701. }
  4702. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4703. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4704. }
  4705. static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4706. struct drm_atomic_state *old_state)
  4707. {
  4708. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4709. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4710. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4711. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4712. if (intel_crtc->config->has_pch_encoder)
  4713. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4714. false);
  4715. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4716. drm_crtc_vblank_off(crtc);
  4717. assert_vblank_disabled(crtc);
  4718. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4719. if (!transcoder_is_dsi(cpu_transcoder))
  4720. intel_disable_pipe(intel_crtc);
  4721. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4722. intel_ddi_set_vc_payload_alloc(crtc, false);
  4723. if (!transcoder_is_dsi(cpu_transcoder))
  4724. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4725. if (INTEL_GEN(dev_priv) >= 9)
  4726. skylake_scaler_disable(intel_crtc);
  4727. else
  4728. ironlake_pfit_disable(intel_crtc, false);
  4729. if (!transcoder_is_dsi(cpu_transcoder))
  4730. intel_ddi_disable_pipe_clock(intel_crtc);
  4731. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4732. if (old_crtc_state->has_pch_encoder)
  4733. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4734. true);
  4735. }
  4736. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4737. {
  4738. struct drm_device *dev = crtc->base.dev;
  4739. struct drm_i915_private *dev_priv = to_i915(dev);
  4740. struct intel_crtc_state *pipe_config = crtc->config;
  4741. if (!pipe_config->gmch_pfit.control)
  4742. return;
  4743. /*
  4744. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4745. * according to register description and PRM.
  4746. */
  4747. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4748. assert_pipe_disabled(dev_priv, crtc->pipe);
  4749. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4750. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4751. /* Border color in case we don't scale up to the full screen. Black by
  4752. * default, change to something else for debugging. */
  4753. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4754. }
  4755. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4756. {
  4757. switch (port) {
  4758. case PORT_A:
  4759. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4760. case PORT_B:
  4761. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4762. case PORT_C:
  4763. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4764. case PORT_D:
  4765. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4766. case PORT_E:
  4767. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4768. default:
  4769. MISSING_CASE(port);
  4770. return POWER_DOMAIN_PORT_OTHER;
  4771. }
  4772. }
  4773. static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
  4774. {
  4775. switch (port) {
  4776. case PORT_A:
  4777. return POWER_DOMAIN_AUX_A;
  4778. case PORT_B:
  4779. return POWER_DOMAIN_AUX_B;
  4780. case PORT_C:
  4781. return POWER_DOMAIN_AUX_C;
  4782. case PORT_D:
  4783. return POWER_DOMAIN_AUX_D;
  4784. case PORT_E:
  4785. /* FIXME: Check VBT for actual wiring of PORT E */
  4786. return POWER_DOMAIN_AUX_D;
  4787. default:
  4788. MISSING_CASE(port);
  4789. return POWER_DOMAIN_AUX_A;
  4790. }
  4791. }
  4792. enum intel_display_power_domain
  4793. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4794. {
  4795. struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
  4796. struct intel_digital_port *intel_dig_port;
  4797. switch (intel_encoder->type) {
  4798. case INTEL_OUTPUT_UNKNOWN:
  4799. /* Only DDI platforms should ever use this output type */
  4800. WARN_ON_ONCE(!HAS_DDI(dev_priv));
  4801. case INTEL_OUTPUT_DP:
  4802. case INTEL_OUTPUT_HDMI:
  4803. case INTEL_OUTPUT_EDP:
  4804. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4805. return port_to_power_domain(intel_dig_port->port);
  4806. case INTEL_OUTPUT_DP_MST:
  4807. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4808. return port_to_power_domain(intel_dig_port->port);
  4809. case INTEL_OUTPUT_ANALOG:
  4810. return POWER_DOMAIN_PORT_CRT;
  4811. case INTEL_OUTPUT_DSI:
  4812. return POWER_DOMAIN_PORT_DSI;
  4813. default:
  4814. return POWER_DOMAIN_PORT_OTHER;
  4815. }
  4816. }
  4817. enum intel_display_power_domain
  4818. intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
  4819. {
  4820. struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
  4821. struct intel_digital_port *intel_dig_port;
  4822. switch (intel_encoder->type) {
  4823. case INTEL_OUTPUT_UNKNOWN:
  4824. case INTEL_OUTPUT_HDMI:
  4825. /*
  4826. * Only DDI platforms should ever use these output types.
  4827. * We can get here after the HDMI detect code has already set
  4828. * the type of the shared encoder. Since we can't be sure
  4829. * what's the status of the given connectors, play safe and
  4830. * run the DP detection too.
  4831. */
  4832. WARN_ON_ONCE(!HAS_DDI(dev_priv));
  4833. case INTEL_OUTPUT_DP:
  4834. case INTEL_OUTPUT_EDP:
  4835. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4836. return port_to_aux_power_domain(intel_dig_port->port);
  4837. case INTEL_OUTPUT_DP_MST:
  4838. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4839. return port_to_aux_power_domain(intel_dig_port->port);
  4840. default:
  4841. MISSING_CASE(intel_encoder->type);
  4842. return POWER_DOMAIN_AUX_A;
  4843. }
  4844. }
  4845. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
  4846. struct intel_crtc_state *crtc_state)
  4847. {
  4848. struct drm_device *dev = crtc->dev;
  4849. struct drm_encoder *encoder;
  4850. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4851. enum pipe pipe = intel_crtc->pipe;
  4852. unsigned long mask;
  4853. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4854. if (!crtc_state->base.active)
  4855. return 0;
  4856. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4857. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4858. if (crtc_state->pch_pfit.enabled ||
  4859. crtc_state->pch_pfit.force_thru)
  4860. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4861. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4862. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4863. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4864. }
  4865. if (crtc_state->shared_dpll)
  4866. mask |= BIT(POWER_DOMAIN_PLLS);
  4867. return mask;
  4868. }
  4869. static unsigned long
  4870. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4871. struct intel_crtc_state *crtc_state)
  4872. {
  4873. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4874. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4875. enum intel_display_power_domain domain;
  4876. unsigned long domains, new_domains, old_domains;
  4877. old_domains = intel_crtc->enabled_power_domains;
  4878. intel_crtc->enabled_power_domains = new_domains =
  4879. get_crtc_power_domains(crtc, crtc_state);
  4880. domains = new_domains & ~old_domains;
  4881. for_each_power_domain(domain, domains)
  4882. intel_display_power_get(dev_priv, domain);
  4883. return old_domains & ~new_domains;
  4884. }
  4885. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4886. unsigned long domains)
  4887. {
  4888. enum intel_display_power_domain domain;
  4889. for_each_power_domain(domain, domains)
  4890. intel_display_power_put(dev_priv, domain);
  4891. }
  4892. static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
  4893. {
  4894. int max_cdclk_freq = dev_priv->max_cdclk_freq;
  4895. if (IS_GEMINILAKE(dev_priv))
  4896. return 2 * max_cdclk_freq;
  4897. else if (INTEL_INFO(dev_priv)->gen >= 9 ||
  4898. IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  4899. return max_cdclk_freq;
  4900. else if (IS_CHERRYVIEW(dev_priv))
  4901. return max_cdclk_freq*95/100;
  4902. else if (INTEL_INFO(dev_priv)->gen < 4)
  4903. return 2*max_cdclk_freq*90/100;
  4904. else
  4905. return max_cdclk_freq*90/100;
  4906. }
  4907. static int skl_calc_cdclk(int max_pixclk, int vco);
  4908. static void intel_update_max_cdclk(struct drm_i915_private *dev_priv)
  4909. {
  4910. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
  4911. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4912. int max_cdclk, vco;
  4913. vco = dev_priv->skl_preferred_vco_freq;
  4914. WARN_ON(vco != 8100000 && vco != 8640000);
  4915. /*
  4916. * Use the lower (vco 8640) cdclk values as a
  4917. * first guess. skl_calc_cdclk() will correct it
  4918. * if the preferred vco is 8100 instead.
  4919. */
  4920. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4921. max_cdclk = 617143;
  4922. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4923. max_cdclk = 540000;
  4924. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4925. max_cdclk = 432000;
  4926. else
  4927. max_cdclk = 308571;
  4928. dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
  4929. } else if (IS_GEMINILAKE(dev_priv)) {
  4930. dev_priv->max_cdclk_freq = 316800;
  4931. } else if (IS_BROXTON(dev_priv)) {
  4932. dev_priv->max_cdclk_freq = 624000;
  4933. } else if (IS_BROADWELL(dev_priv)) {
  4934. /*
  4935. * FIXME with extra cooling we can allow
  4936. * 540 MHz for ULX and 675 Mhz for ULT.
  4937. * How can we know if extra cooling is
  4938. * available? PCI ID, VTB, something else?
  4939. */
  4940. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4941. dev_priv->max_cdclk_freq = 450000;
  4942. else if (IS_BDW_ULX(dev_priv))
  4943. dev_priv->max_cdclk_freq = 450000;
  4944. else if (IS_BDW_ULT(dev_priv))
  4945. dev_priv->max_cdclk_freq = 540000;
  4946. else
  4947. dev_priv->max_cdclk_freq = 675000;
  4948. } else if (IS_CHERRYVIEW(dev_priv)) {
  4949. dev_priv->max_cdclk_freq = 320000;
  4950. } else if (IS_VALLEYVIEW(dev_priv)) {
  4951. dev_priv->max_cdclk_freq = 400000;
  4952. } else {
  4953. /* otherwise assume cdclk is fixed */
  4954. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4955. }
  4956. dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
  4957. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4958. dev_priv->max_cdclk_freq);
  4959. DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
  4960. dev_priv->max_dotclk_freq);
  4961. }
  4962. static void intel_update_cdclk(struct drm_i915_private *dev_priv)
  4963. {
  4964. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev_priv);
  4965. if (INTEL_GEN(dev_priv) >= 9)
  4966. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
  4967. dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
  4968. dev_priv->cdclk_pll.ref);
  4969. else
  4970. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4971. dev_priv->cdclk_freq);
  4972. /*
  4973. * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
  4974. * Programmng [sic] note: bit[9:2] should be programmed to the number
  4975. * of cdclk that generates 4MHz reference clock freq which is used to
  4976. * generate GMBus clock. This will vary with the cdclk freq.
  4977. */
  4978. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  4979. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4980. }
  4981. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4982. static int skl_cdclk_decimal(int cdclk)
  4983. {
  4984. return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
  4985. }
  4986. static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
  4987. {
  4988. int ratio;
  4989. if (cdclk == dev_priv->cdclk_pll.ref)
  4990. return 0;
  4991. switch (cdclk) {
  4992. default:
  4993. MISSING_CASE(cdclk);
  4994. case 144000:
  4995. case 288000:
  4996. case 384000:
  4997. case 576000:
  4998. ratio = 60;
  4999. break;
  5000. case 624000:
  5001. ratio = 65;
  5002. break;
  5003. }
  5004. return dev_priv->cdclk_pll.ref * ratio;
  5005. }
  5006. static int glk_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
  5007. {
  5008. int ratio;
  5009. if (cdclk == dev_priv->cdclk_pll.ref)
  5010. return 0;
  5011. switch (cdclk) {
  5012. default:
  5013. MISSING_CASE(cdclk);
  5014. case 79200:
  5015. case 158400:
  5016. case 316800:
  5017. ratio = 33;
  5018. break;
  5019. }
  5020. return dev_priv->cdclk_pll.ref * ratio;
  5021. }
  5022. static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
  5023. {
  5024. I915_WRITE(BXT_DE_PLL_ENABLE, 0);
  5025. /* Timeout 200us */
  5026. if (intel_wait_for_register(dev_priv,
  5027. BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
  5028. 1))
  5029. DRM_ERROR("timeout waiting for DE PLL unlock\n");
  5030. dev_priv->cdclk_pll.vco = 0;
  5031. }
  5032. static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
  5033. {
  5034. int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
  5035. u32 val;
  5036. val = I915_READ(BXT_DE_PLL_CTL);
  5037. val &= ~BXT_DE_PLL_RATIO_MASK;
  5038. val |= BXT_DE_PLL_RATIO(ratio);
  5039. I915_WRITE(BXT_DE_PLL_CTL, val);
  5040. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  5041. /* Timeout 200us */
  5042. if (intel_wait_for_register(dev_priv,
  5043. BXT_DE_PLL_ENABLE,
  5044. BXT_DE_PLL_LOCK,
  5045. BXT_DE_PLL_LOCK,
  5046. 1))
  5047. DRM_ERROR("timeout waiting for DE PLL lock\n");
  5048. dev_priv->cdclk_pll.vco = vco;
  5049. }
  5050. static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
  5051. {
  5052. u32 val, divider;
  5053. int vco, ret;
  5054. if (IS_GEMINILAKE(dev_priv))
  5055. vco = glk_de_pll_vco(dev_priv, cdclk);
  5056. else
  5057. vco = bxt_de_pll_vco(dev_priv, cdclk);
  5058. DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
  5059. /* cdclk = vco / 2 / div{1,1.5,2,4} */
  5060. switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
  5061. case 8:
  5062. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  5063. break;
  5064. case 4:
  5065. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  5066. break;
  5067. case 3:
  5068. WARN(IS_GEMINILAKE(dev_priv), "Unsupported divider\n");
  5069. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  5070. break;
  5071. case 2:
  5072. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  5073. break;
  5074. default:
  5075. WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
  5076. WARN_ON(vco != 0);
  5077. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  5078. break;
  5079. }
  5080. /* Inform power controller of upcoming frequency change */
  5081. mutex_lock(&dev_priv->rps.hw_lock);
  5082. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  5083. 0x80000000);
  5084. mutex_unlock(&dev_priv->rps.hw_lock);
  5085. if (ret) {
  5086. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  5087. ret, cdclk);
  5088. return;
  5089. }
  5090. if (dev_priv->cdclk_pll.vco != 0 &&
  5091. dev_priv->cdclk_pll.vco != vco)
  5092. bxt_de_pll_disable(dev_priv);
  5093. if (dev_priv->cdclk_pll.vco != vco)
  5094. bxt_de_pll_enable(dev_priv, vco);
  5095. val = divider | skl_cdclk_decimal(cdclk);
  5096. /*
  5097. * FIXME if only the cd2x divider needs changing, it could be done
  5098. * without shutting off the pipe (if only one pipe is active).
  5099. */
  5100. val |= BXT_CDCLK_CD2X_PIPE_NONE;
  5101. /*
  5102. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  5103. * enable otherwise.
  5104. */
  5105. if (cdclk >= 500000)
  5106. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  5107. I915_WRITE(CDCLK_CTL, val);
  5108. mutex_lock(&dev_priv->rps.hw_lock);
  5109. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  5110. DIV_ROUND_UP(cdclk, 25000));
  5111. mutex_unlock(&dev_priv->rps.hw_lock);
  5112. if (ret) {
  5113. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  5114. ret, cdclk);
  5115. return;
  5116. }
  5117. intel_update_cdclk(dev_priv);
  5118. }
  5119. static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
  5120. {
  5121. u32 cdctl, expected;
  5122. intel_update_cdclk(dev_priv);
  5123. if (dev_priv->cdclk_pll.vco == 0 ||
  5124. dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
  5125. goto sanitize;
  5126. /* DPLL okay; verify the cdclock
  5127. *
  5128. * Some BIOS versions leave an incorrect decimal frequency value and
  5129. * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
  5130. * so sanitize this register.
  5131. */
  5132. cdctl = I915_READ(CDCLK_CTL);
  5133. /*
  5134. * Let's ignore the pipe field, since BIOS could have configured the
  5135. * dividers both synching to an active pipe, or asynchronously
  5136. * (PIPE_NONE).
  5137. */
  5138. cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
  5139. expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
  5140. skl_cdclk_decimal(dev_priv->cdclk_freq);
  5141. /*
  5142. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  5143. * enable otherwise.
  5144. */
  5145. if (dev_priv->cdclk_freq >= 500000)
  5146. expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  5147. if (cdctl == expected)
  5148. /* All well; nothing to sanitize */
  5149. return;
  5150. sanitize:
  5151. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  5152. /* force cdclk programming */
  5153. dev_priv->cdclk_freq = 0;
  5154. /* force full PLL disable + enable */
  5155. dev_priv->cdclk_pll.vco = -1;
  5156. }
  5157. void bxt_init_cdclk(struct drm_i915_private *dev_priv)
  5158. {
  5159. int cdclk;
  5160. bxt_sanitize_cdclk(dev_priv);
  5161. if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
  5162. return;
  5163. /*
  5164. * FIXME:
  5165. * - The initial CDCLK needs to be read from VBT.
  5166. * Need to make this change after VBT has changes for BXT.
  5167. */
  5168. if (IS_GEMINILAKE(dev_priv))
  5169. cdclk = glk_calc_cdclk(0);
  5170. else
  5171. cdclk = bxt_calc_cdclk(0);
  5172. bxt_set_cdclk(dev_priv, cdclk);
  5173. }
  5174. void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
  5175. {
  5176. bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
  5177. }
  5178. static int skl_calc_cdclk(int max_pixclk, int vco)
  5179. {
  5180. if (vco == 8640000) {
  5181. if (max_pixclk > 540000)
  5182. return 617143;
  5183. else if (max_pixclk > 432000)
  5184. return 540000;
  5185. else if (max_pixclk > 308571)
  5186. return 432000;
  5187. else
  5188. return 308571;
  5189. } else {
  5190. if (max_pixclk > 540000)
  5191. return 675000;
  5192. else if (max_pixclk > 450000)
  5193. return 540000;
  5194. else if (max_pixclk > 337500)
  5195. return 450000;
  5196. else
  5197. return 337500;
  5198. }
  5199. }
  5200. static void
  5201. skl_dpll0_update(struct drm_i915_private *dev_priv)
  5202. {
  5203. u32 val;
  5204. dev_priv->cdclk_pll.ref = 24000;
  5205. dev_priv->cdclk_pll.vco = 0;
  5206. val = I915_READ(LCPLL1_CTL);
  5207. if ((val & LCPLL_PLL_ENABLE) == 0)
  5208. return;
  5209. if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
  5210. return;
  5211. val = I915_READ(DPLL_CTRL1);
  5212. if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
  5213. DPLL_CTRL1_SSC(SKL_DPLL0) |
  5214. DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
  5215. DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
  5216. return;
  5217. switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
  5218. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
  5219. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
  5220. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
  5221. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
  5222. dev_priv->cdclk_pll.vco = 8100000;
  5223. break;
  5224. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
  5225. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
  5226. dev_priv->cdclk_pll.vco = 8640000;
  5227. break;
  5228. default:
  5229. MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  5230. break;
  5231. }
  5232. }
  5233. void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
  5234. {
  5235. bool changed = dev_priv->skl_preferred_vco_freq != vco;
  5236. dev_priv->skl_preferred_vco_freq = vco;
  5237. if (changed)
  5238. intel_update_max_cdclk(dev_priv);
  5239. }
  5240. static void
  5241. skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
  5242. {
  5243. int min_cdclk = skl_calc_cdclk(0, vco);
  5244. u32 val;
  5245. WARN_ON(vco != 8100000 && vco != 8640000);
  5246. /* select the minimum CDCLK before enabling DPLL 0 */
  5247. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
  5248. I915_WRITE(CDCLK_CTL, val);
  5249. POSTING_READ(CDCLK_CTL);
  5250. /*
  5251. * We always enable DPLL0 with the lowest link rate possible, but still
  5252. * taking into account the VCO required to operate the eDP panel at the
  5253. * desired frequency. The usual DP link rates operate with a VCO of
  5254. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  5255. * The modeset code is responsible for the selection of the exact link
  5256. * rate later on, with the constraint of choosing a frequency that
  5257. * works with vco.
  5258. */
  5259. val = I915_READ(DPLL_CTRL1);
  5260. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  5261. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  5262. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  5263. if (vco == 8640000)
  5264. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  5265. SKL_DPLL0);
  5266. else
  5267. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  5268. SKL_DPLL0);
  5269. I915_WRITE(DPLL_CTRL1, val);
  5270. POSTING_READ(DPLL_CTRL1);
  5271. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  5272. if (intel_wait_for_register(dev_priv,
  5273. LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  5274. 5))
  5275. DRM_ERROR("DPLL0 not locked\n");
  5276. dev_priv->cdclk_pll.vco = vco;
  5277. /* We'll want to keep using the current vco from now on. */
  5278. skl_set_preferred_cdclk_vco(dev_priv, vco);
  5279. }
  5280. static void
  5281. skl_dpll0_disable(struct drm_i915_private *dev_priv)
  5282. {
  5283. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  5284. if (intel_wait_for_register(dev_priv,
  5285. LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
  5286. 1))
  5287. DRM_ERROR("Couldn't disable DPLL0\n");
  5288. dev_priv->cdclk_pll.vco = 0;
  5289. }
  5290. static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
  5291. {
  5292. u32 freq_select, pcu_ack;
  5293. int ret;
  5294. WARN_ON((cdclk == 24000) != (vco == 0));
  5295. DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
  5296. mutex_lock(&dev_priv->rps.hw_lock);
  5297. ret = skl_pcode_request(dev_priv, SKL_PCODE_CDCLK_CONTROL,
  5298. SKL_CDCLK_PREPARE_FOR_CHANGE,
  5299. SKL_CDCLK_READY_FOR_CHANGE,
  5300. SKL_CDCLK_READY_FOR_CHANGE, 3);
  5301. mutex_unlock(&dev_priv->rps.hw_lock);
  5302. if (ret) {
  5303. DRM_ERROR("Failed to inform PCU about cdclk change (%d)\n",
  5304. ret);
  5305. return;
  5306. }
  5307. /* set CDCLK_CTL */
  5308. switch (cdclk) {
  5309. case 450000:
  5310. case 432000:
  5311. freq_select = CDCLK_FREQ_450_432;
  5312. pcu_ack = 1;
  5313. break;
  5314. case 540000:
  5315. freq_select = CDCLK_FREQ_540;
  5316. pcu_ack = 2;
  5317. break;
  5318. case 308571:
  5319. case 337500:
  5320. default:
  5321. freq_select = CDCLK_FREQ_337_308;
  5322. pcu_ack = 0;
  5323. break;
  5324. case 617143:
  5325. case 675000:
  5326. freq_select = CDCLK_FREQ_675_617;
  5327. pcu_ack = 3;
  5328. break;
  5329. }
  5330. if (dev_priv->cdclk_pll.vco != 0 &&
  5331. dev_priv->cdclk_pll.vco != vco)
  5332. skl_dpll0_disable(dev_priv);
  5333. if (dev_priv->cdclk_pll.vco != vco)
  5334. skl_dpll0_enable(dev_priv, vco);
  5335. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
  5336. POSTING_READ(CDCLK_CTL);
  5337. /* inform PCU of the change */
  5338. mutex_lock(&dev_priv->rps.hw_lock);
  5339. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  5340. mutex_unlock(&dev_priv->rps.hw_lock);
  5341. intel_update_cdclk(dev_priv);
  5342. }
  5343. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
  5344. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  5345. {
  5346. skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
  5347. }
  5348. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  5349. {
  5350. int cdclk, vco;
  5351. skl_sanitize_cdclk(dev_priv);
  5352. if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
  5353. /*
  5354. * Use the current vco as our initial
  5355. * guess as to what the preferred vco is.
  5356. */
  5357. if (dev_priv->skl_preferred_vco_freq == 0)
  5358. skl_set_preferred_cdclk_vco(dev_priv,
  5359. dev_priv->cdclk_pll.vco);
  5360. return;
  5361. }
  5362. vco = dev_priv->skl_preferred_vco_freq;
  5363. if (vco == 0)
  5364. vco = 8100000;
  5365. cdclk = skl_calc_cdclk(0, vco);
  5366. skl_set_cdclk(dev_priv, cdclk, vco);
  5367. }
  5368. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
  5369. {
  5370. uint32_t cdctl, expected;
  5371. /*
  5372. * check if the pre-os intialized the display
  5373. * There is SWF18 scratchpad register defined which is set by the
  5374. * pre-os which can be used by the OS drivers to check the status
  5375. */
  5376. if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
  5377. goto sanitize;
  5378. intel_update_cdclk(dev_priv);
  5379. /* Is PLL enabled and locked ? */
  5380. if (dev_priv->cdclk_pll.vco == 0 ||
  5381. dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
  5382. goto sanitize;
  5383. /* DPLL okay; verify the cdclock
  5384. *
  5385. * Noticed in some instances that the freq selection is correct but
  5386. * decimal part is programmed wrong from BIOS where pre-os does not
  5387. * enable display. Verify the same as well.
  5388. */
  5389. cdctl = I915_READ(CDCLK_CTL);
  5390. expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
  5391. skl_cdclk_decimal(dev_priv->cdclk_freq);
  5392. if (cdctl == expected)
  5393. /* All well; nothing to sanitize */
  5394. return;
  5395. sanitize:
  5396. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  5397. /* force cdclk programming */
  5398. dev_priv->cdclk_freq = 0;
  5399. /* force full PLL disable + enable */
  5400. dev_priv->cdclk_pll.vco = -1;
  5401. }
  5402. /* Adjust CDclk dividers to allow high res or save power if possible */
  5403. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  5404. {
  5405. struct drm_i915_private *dev_priv = to_i915(dev);
  5406. u32 val, cmd;
  5407. WARN_ON(dev_priv->display.get_display_clock_speed(dev_priv)
  5408. != dev_priv->cdclk_freq);
  5409. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  5410. cmd = 2;
  5411. else if (cdclk == 266667)
  5412. cmd = 1;
  5413. else
  5414. cmd = 0;
  5415. mutex_lock(&dev_priv->rps.hw_lock);
  5416. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  5417. val &= ~DSPFREQGUAR_MASK;
  5418. val |= (cmd << DSPFREQGUAR_SHIFT);
  5419. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  5420. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  5421. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  5422. 50)) {
  5423. DRM_ERROR("timed out waiting for CDclk change\n");
  5424. }
  5425. mutex_unlock(&dev_priv->rps.hw_lock);
  5426. mutex_lock(&dev_priv->sb_lock);
  5427. if (cdclk == 400000) {
  5428. u32 divider;
  5429. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  5430. /* adjust cdclk divider */
  5431. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  5432. val &= ~CCK_FREQUENCY_VALUES;
  5433. val |= divider;
  5434. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  5435. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  5436. CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
  5437. 50))
  5438. DRM_ERROR("timed out waiting for CDclk change\n");
  5439. }
  5440. /* adjust self-refresh exit latency value */
  5441. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  5442. val &= ~0x7f;
  5443. /*
  5444. * For high bandwidth configs, we set a higher latency in the bunit
  5445. * so that the core display fetch happens in time to avoid underruns.
  5446. */
  5447. if (cdclk == 400000)
  5448. val |= 4500 / 250; /* 4.5 usec */
  5449. else
  5450. val |= 3000 / 250; /* 3.0 usec */
  5451. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  5452. mutex_unlock(&dev_priv->sb_lock);
  5453. intel_update_cdclk(dev_priv);
  5454. }
  5455. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  5456. {
  5457. struct drm_i915_private *dev_priv = to_i915(dev);
  5458. u32 val, cmd;
  5459. WARN_ON(dev_priv->display.get_display_clock_speed(dev_priv)
  5460. != dev_priv->cdclk_freq);
  5461. switch (cdclk) {
  5462. case 333333:
  5463. case 320000:
  5464. case 266667:
  5465. case 200000:
  5466. break;
  5467. default:
  5468. MISSING_CASE(cdclk);
  5469. return;
  5470. }
  5471. /*
  5472. * Specs are full of misinformation, but testing on actual
  5473. * hardware has shown that we just need to write the desired
  5474. * CCK divider into the Punit register.
  5475. */
  5476. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  5477. mutex_lock(&dev_priv->rps.hw_lock);
  5478. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  5479. val &= ~DSPFREQGUAR_MASK_CHV;
  5480. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  5481. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  5482. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  5483. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  5484. 50)) {
  5485. DRM_ERROR("timed out waiting for CDclk change\n");
  5486. }
  5487. mutex_unlock(&dev_priv->rps.hw_lock);
  5488. intel_update_cdclk(dev_priv);
  5489. }
  5490. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  5491. int max_pixclk)
  5492. {
  5493. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  5494. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  5495. /*
  5496. * Really only a few cases to deal with, as only 4 CDclks are supported:
  5497. * 200MHz
  5498. * 267MHz
  5499. * 320/333MHz (depends on HPLL freq)
  5500. * 400MHz (VLV only)
  5501. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  5502. * of the lower bin and adjust if needed.
  5503. *
  5504. * We seem to get an unstable or solid color picture at 200MHz.
  5505. * Not sure what's wrong. For now use 200MHz only when all pipes
  5506. * are off.
  5507. */
  5508. if (!IS_CHERRYVIEW(dev_priv) &&
  5509. max_pixclk > freq_320*limit/100)
  5510. return 400000;
  5511. else if (max_pixclk > 266667*limit/100)
  5512. return freq_320;
  5513. else if (max_pixclk > 0)
  5514. return 266667;
  5515. else
  5516. return 200000;
  5517. }
  5518. static int glk_calc_cdclk(int max_pixclk)
  5519. {
  5520. if (max_pixclk > 2 * 158400)
  5521. return 316800;
  5522. else if (max_pixclk > 2 * 79200)
  5523. return 158400;
  5524. else
  5525. return 79200;
  5526. }
  5527. static int bxt_calc_cdclk(int max_pixclk)
  5528. {
  5529. if (max_pixclk > 576000)
  5530. return 624000;
  5531. else if (max_pixclk > 384000)
  5532. return 576000;
  5533. else if (max_pixclk > 288000)
  5534. return 384000;
  5535. else if (max_pixclk > 144000)
  5536. return 288000;
  5537. else
  5538. return 144000;
  5539. }
  5540. /* Compute the max pixel clock for new configuration. */
  5541. static int intel_mode_max_pixclk(struct drm_device *dev,
  5542. struct drm_atomic_state *state)
  5543. {
  5544. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  5545. struct drm_i915_private *dev_priv = to_i915(dev);
  5546. struct drm_crtc *crtc;
  5547. struct drm_crtc_state *crtc_state;
  5548. unsigned max_pixclk = 0, i;
  5549. enum pipe pipe;
  5550. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  5551. sizeof(intel_state->min_pixclk));
  5552. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  5553. int pixclk = 0;
  5554. if (crtc_state->enable)
  5555. pixclk = crtc_state->adjusted_mode.crtc_clock;
  5556. intel_state->min_pixclk[i] = pixclk;
  5557. }
  5558. for_each_pipe(dev_priv, pipe)
  5559. max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
  5560. return max_pixclk;
  5561. }
  5562. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  5563. {
  5564. struct drm_device *dev = state->dev;
  5565. struct drm_i915_private *dev_priv = to_i915(dev);
  5566. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5567. struct intel_atomic_state *intel_state =
  5568. to_intel_atomic_state(state);
  5569. intel_state->cdclk = intel_state->dev_cdclk =
  5570. valleyview_calc_cdclk(dev_priv, max_pixclk);
  5571. if (!intel_state->active_crtcs)
  5572. intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
  5573. return 0;
  5574. }
  5575. static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
  5576. {
  5577. struct drm_i915_private *dev_priv = to_i915(state->dev);
  5578. int max_pixclk = ilk_max_pixel_rate(state);
  5579. struct intel_atomic_state *intel_state =
  5580. to_intel_atomic_state(state);
  5581. int cdclk;
  5582. if (IS_GEMINILAKE(dev_priv))
  5583. cdclk = glk_calc_cdclk(max_pixclk);
  5584. else
  5585. cdclk = bxt_calc_cdclk(max_pixclk);
  5586. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  5587. if (!intel_state->active_crtcs) {
  5588. if (IS_GEMINILAKE(dev_priv))
  5589. cdclk = glk_calc_cdclk(0);
  5590. else
  5591. cdclk = bxt_calc_cdclk(0);
  5592. intel_state->dev_cdclk = cdclk;
  5593. }
  5594. return 0;
  5595. }
  5596. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5597. {
  5598. unsigned int credits, default_credits;
  5599. if (IS_CHERRYVIEW(dev_priv))
  5600. default_credits = PFI_CREDIT(12);
  5601. else
  5602. default_credits = PFI_CREDIT(8);
  5603. if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
  5604. /* CHV suggested value is 31 or 63 */
  5605. if (IS_CHERRYVIEW(dev_priv))
  5606. credits = PFI_CREDIT_63;
  5607. else
  5608. credits = PFI_CREDIT(15);
  5609. } else {
  5610. credits = default_credits;
  5611. }
  5612. /*
  5613. * WA - write default credits before re-programming
  5614. * FIXME: should we also set the resend bit here?
  5615. */
  5616. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5617. default_credits);
  5618. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5619. credits | PFI_CREDIT_RESEND);
  5620. /*
  5621. * FIXME is this guaranteed to clear
  5622. * immediately or should we poll for it?
  5623. */
  5624. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5625. }
  5626. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5627. {
  5628. struct drm_device *dev = old_state->dev;
  5629. struct drm_i915_private *dev_priv = to_i915(dev);
  5630. struct intel_atomic_state *old_intel_state =
  5631. to_intel_atomic_state(old_state);
  5632. unsigned req_cdclk = old_intel_state->dev_cdclk;
  5633. /*
  5634. * FIXME: We can end up here with all power domains off, yet
  5635. * with a CDCLK frequency other than the minimum. To account
  5636. * for this take the PIPE-A power domain, which covers the HW
  5637. * blocks needed for the following programming. This can be
  5638. * removed once it's guaranteed that we get here either with
  5639. * the minimum CDCLK set, or the required power domains
  5640. * enabled.
  5641. */
  5642. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5643. if (IS_CHERRYVIEW(dev_priv))
  5644. cherryview_set_cdclk(dev, req_cdclk);
  5645. else
  5646. valleyview_set_cdclk(dev, req_cdclk);
  5647. vlv_program_pfi_credits(dev_priv);
  5648. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5649. }
  5650. static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
  5651. struct drm_atomic_state *old_state)
  5652. {
  5653. struct drm_crtc *crtc = pipe_config->base.crtc;
  5654. struct drm_device *dev = crtc->dev;
  5655. struct drm_i915_private *dev_priv = to_i915(dev);
  5656. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5657. int pipe = intel_crtc->pipe;
  5658. if (WARN_ON(intel_crtc->active))
  5659. return;
  5660. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5661. intel_dp_set_m_n(intel_crtc, M1_N1);
  5662. intel_set_pipe_timings(intel_crtc);
  5663. intel_set_pipe_src_size(intel_crtc);
  5664. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  5665. struct drm_i915_private *dev_priv = to_i915(dev);
  5666. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5667. I915_WRITE(CHV_CANVAS(pipe), 0);
  5668. }
  5669. i9xx_set_pipeconf(intel_crtc);
  5670. intel_crtc->active = true;
  5671. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5672. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  5673. if (IS_CHERRYVIEW(dev_priv)) {
  5674. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5675. chv_enable_pll(intel_crtc, intel_crtc->config);
  5676. } else {
  5677. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5678. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5679. }
  5680. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  5681. i9xx_pfit_enable(intel_crtc);
  5682. intel_color_load_luts(&pipe_config->base);
  5683. intel_update_watermarks(intel_crtc);
  5684. intel_enable_pipe(intel_crtc);
  5685. assert_vblank_disabled(crtc);
  5686. drm_crtc_vblank_on(crtc);
  5687. intel_encoders_enable(crtc, pipe_config, old_state);
  5688. }
  5689. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5690. {
  5691. struct drm_device *dev = crtc->base.dev;
  5692. struct drm_i915_private *dev_priv = to_i915(dev);
  5693. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5694. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5695. }
  5696. static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
  5697. struct drm_atomic_state *old_state)
  5698. {
  5699. struct drm_crtc *crtc = pipe_config->base.crtc;
  5700. struct drm_device *dev = crtc->dev;
  5701. struct drm_i915_private *dev_priv = to_i915(dev);
  5702. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5703. enum pipe pipe = intel_crtc->pipe;
  5704. if (WARN_ON(intel_crtc->active))
  5705. return;
  5706. i9xx_set_pll_dividers(intel_crtc);
  5707. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5708. intel_dp_set_m_n(intel_crtc, M1_N1);
  5709. intel_set_pipe_timings(intel_crtc);
  5710. intel_set_pipe_src_size(intel_crtc);
  5711. i9xx_set_pipeconf(intel_crtc);
  5712. intel_crtc->active = true;
  5713. if (!IS_GEN2(dev_priv))
  5714. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5715. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  5716. i9xx_enable_pll(intel_crtc);
  5717. i9xx_pfit_enable(intel_crtc);
  5718. intel_color_load_luts(&pipe_config->base);
  5719. intel_update_watermarks(intel_crtc);
  5720. intel_enable_pipe(intel_crtc);
  5721. assert_vblank_disabled(crtc);
  5722. drm_crtc_vblank_on(crtc);
  5723. intel_encoders_enable(crtc, pipe_config, old_state);
  5724. }
  5725. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5726. {
  5727. struct drm_device *dev = crtc->base.dev;
  5728. struct drm_i915_private *dev_priv = to_i915(dev);
  5729. if (!crtc->config->gmch_pfit.control)
  5730. return;
  5731. assert_pipe_disabled(dev_priv, crtc->pipe);
  5732. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5733. I915_READ(PFIT_CONTROL));
  5734. I915_WRITE(PFIT_CONTROL, 0);
  5735. }
  5736. static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
  5737. struct drm_atomic_state *old_state)
  5738. {
  5739. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  5740. struct drm_device *dev = crtc->dev;
  5741. struct drm_i915_private *dev_priv = to_i915(dev);
  5742. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5743. int pipe = intel_crtc->pipe;
  5744. /*
  5745. * On gen2 planes are double buffered but the pipe isn't, so we must
  5746. * wait for planes to fully turn off before disabling the pipe.
  5747. */
  5748. if (IS_GEN2(dev_priv))
  5749. intel_wait_for_vblank(dev_priv, pipe);
  5750. intel_encoders_disable(crtc, old_crtc_state, old_state);
  5751. drm_crtc_vblank_off(crtc);
  5752. assert_vblank_disabled(crtc);
  5753. intel_disable_pipe(intel_crtc);
  5754. i9xx_pfit_disable(intel_crtc);
  5755. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  5756. if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
  5757. if (IS_CHERRYVIEW(dev_priv))
  5758. chv_disable_pll(dev_priv, pipe);
  5759. else if (IS_VALLEYVIEW(dev_priv))
  5760. vlv_disable_pll(dev_priv, pipe);
  5761. else
  5762. i9xx_disable_pll(intel_crtc);
  5763. }
  5764. intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
  5765. if (!IS_GEN2(dev_priv))
  5766. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5767. }
  5768. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5769. {
  5770. struct intel_encoder *encoder;
  5771. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5772. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5773. enum intel_display_power_domain domain;
  5774. unsigned long domains;
  5775. struct drm_atomic_state *state;
  5776. struct intel_crtc_state *crtc_state;
  5777. int ret;
  5778. if (!intel_crtc->active)
  5779. return;
  5780. if (crtc->primary->state->visible) {
  5781. WARN_ON(intel_crtc->flip_work);
  5782. intel_pre_disable_primary_noatomic(crtc);
  5783. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  5784. crtc->primary->state->visible = false;
  5785. }
  5786. state = drm_atomic_state_alloc(crtc->dev);
  5787. if (!state) {
  5788. DRM_DEBUG_KMS("failed to disable [CRTC:%d:%s], out of memory",
  5789. crtc->base.id, crtc->name);
  5790. return;
  5791. }
  5792. state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
  5793. /* Everything's already locked, -EDEADLK can't happen. */
  5794. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  5795. ret = drm_atomic_add_affected_connectors(state, crtc);
  5796. WARN_ON(IS_ERR(crtc_state) || ret);
  5797. dev_priv->display.crtc_disable(crtc_state, state);
  5798. drm_atomic_state_put(state);
  5799. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  5800. crtc->base.id, crtc->name);
  5801. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  5802. crtc->state->active = false;
  5803. intel_crtc->active = false;
  5804. crtc->enabled = false;
  5805. crtc->state->connector_mask = 0;
  5806. crtc->state->encoder_mask = 0;
  5807. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  5808. encoder->base.crtc = NULL;
  5809. intel_fbc_disable(intel_crtc);
  5810. intel_update_watermarks(intel_crtc);
  5811. intel_disable_shared_dpll(intel_crtc);
  5812. domains = intel_crtc->enabled_power_domains;
  5813. for_each_power_domain(domain, domains)
  5814. intel_display_power_put(dev_priv, domain);
  5815. intel_crtc->enabled_power_domains = 0;
  5816. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5817. dev_priv->min_pixclk[intel_crtc->pipe] = 0;
  5818. }
  5819. /*
  5820. * turn all crtc's off, but do not adjust state
  5821. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5822. */
  5823. int intel_display_suspend(struct drm_device *dev)
  5824. {
  5825. struct drm_i915_private *dev_priv = to_i915(dev);
  5826. struct drm_atomic_state *state;
  5827. int ret;
  5828. state = drm_atomic_helper_suspend(dev);
  5829. ret = PTR_ERR_OR_ZERO(state);
  5830. if (ret)
  5831. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5832. else
  5833. dev_priv->modeset_restore_state = state;
  5834. return ret;
  5835. }
  5836. void intel_encoder_destroy(struct drm_encoder *encoder)
  5837. {
  5838. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5839. drm_encoder_cleanup(encoder);
  5840. kfree(intel_encoder);
  5841. }
  5842. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5843. * internal consistency). */
  5844. static void intel_connector_verify_state(struct intel_connector *connector)
  5845. {
  5846. struct drm_crtc *crtc = connector->base.state->crtc;
  5847. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5848. connector->base.base.id,
  5849. connector->base.name);
  5850. if (connector->get_hw_state(connector)) {
  5851. struct intel_encoder *encoder = connector->encoder;
  5852. struct drm_connector_state *conn_state = connector->base.state;
  5853. I915_STATE_WARN(!crtc,
  5854. "connector enabled without attached crtc\n");
  5855. if (!crtc)
  5856. return;
  5857. I915_STATE_WARN(!crtc->state->active,
  5858. "connector is active, but attached crtc isn't\n");
  5859. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5860. return;
  5861. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5862. "atomic encoder doesn't match attached encoder\n");
  5863. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5864. "attached encoder crtc differs from connector crtc\n");
  5865. } else {
  5866. I915_STATE_WARN(crtc && crtc->state->active,
  5867. "attached crtc is active, but connector isn't\n");
  5868. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5869. "best encoder set without crtc!\n");
  5870. }
  5871. }
  5872. int intel_connector_init(struct intel_connector *connector)
  5873. {
  5874. drm_atomic_helper_connector_reset(&connector->base);
  5875. if (!connector->base.state)
  5876. return -ENOMEM;
  5877. return 0;
  5878. }
  5879. struct intel_connector *intel_connector_alloc(void)
  5880. {
  5881. struct intel_connector *connector;
  5882. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5883. if (!connector)
  5884. return NULL;
  5885. if (intel_connector_init(connector) < 0) {
  5886. kfree(connector);
  5887. return NULL;
  5888. }
  5889. return connector;
  5890. }
  5891. /* Simple connector->get_hw_state implementation for encoders that support only
  5892. * one connector and no cloning and hence the encoder state determines the state
  5893. * of the connector. */
  5894. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5895. {
  5896. enum pipe pipe = 0;
  5897. struct intel_encoder *encoder = connector->encoder;
  5898. return encoder->get_hw_state(encoder, &pipe);
  5899. }
  5900. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5901. {
  5902. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5903. return crtc_state->fdi_lanes;
  5904. return 0;
  5905. }
  5906. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5907. struct intel_crtc_state *pipe_config)
  5908. {
  5909. struct drm_i915_private *dev_priv = to_i915(dev);
  5910. struct drm_atomic_state *state = pipe_config->base.state;
  5911. struct intel_crtc *other_crtc;
  5912. struct intel_crtc_state *other_crtc_state;
  5913. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5914. pipe_name(pipe), pipe_config->fdi_lanes);
  5915. if (pipe_config->fdi_lanes > 4) {
  5916. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5917. pipe_name(pipe), pipe_config->fdi_lanes);
  5918. return -EINVAL;
  5919. }
  5920. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  5921. if (pipe_config->fdi_lanes > 2) {
  5922. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5923. pipe_config->fdi_lanes);
  5924. return -EINVAL;
  5925. } else {
  5926. return 0;
  5927. }
  5928. }
  5929. if (INTEL_INFO(dev_priv)->num_pipes == 2)
  5930. return 0;
  5931. /* Ivybridge 3 pipe is really complicated */
  5932. switch (pipe) {
  5933. case PIPE_A:
  5934. return 0;
  5935. case PIPE_B:
  5936. if (pipe_config->fdi_lanes <= 2)
  5937. return 0;
  5938. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
  5939. other_crtc_state =
  5940. intel_atomic_get_crtc_state(state, other_crtc);
  5941. if (IS_ERR(other_crtc_state))
  5942. return PTR_ERR(other_crtc_state);
  5943. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5944. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5945. pipe_name(pipe), pipe_config->fdi_lanes);
  5946. return -EINVAL;
  5947. }
  5948. return 0;
  5949. case PIPE_C:
  5950. if (pipe_config->fdi_lanes > 2) {
  5951. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5952. pipe_name(pipe), pipe_config->fdi_lanes);
  5953. return -EINVAL;
  5954. }
  5955. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
  5956. other_crtc_state =
  5957. intel_atomic_get_crtc_state(state, other_crtc);
  5958. if (IS_ERR(other_crtc_state))
  5959. return PTR_ERR(other_crtc_state);
  5960. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5961. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5962. return -EINVAL;
  5963. }
  5964. return 0;
  5965. default:
  5966. BUG();
  5967. }
  5968. }
  5969. #define RETRY 1
  5970. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5971. struct intel_crtc_state *pipe_config)
  5972. {
  5973. struct drm_device *dev = intel_crtc->base.dev;
  5974. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5975. int lane, link_bw, fdi_dotclock, ret;
  5976. bool needs_recompute = false;
  5977. retry:
  5978. /* FDI is a binary signal running at ~2.7GHz, encoding
  5979. * each output octet as 10 bits. The actual frequency
  5980. * is stored as a divider into a 100MHz clock, and the
  5981. * mode pixel clock is stored in units of 1KHz.
  5982. * Hence the bw of each lane in terms of the mode signal
  5983. * is:
  5984. */
  5985. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5986. fdi_dotclock = adjusted_mode->crtc_clock;
  5987. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5988. pipe_config->pipe_bpp);
  5989. pipe_config->fdi_lanes = lane;
  5990. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5991. link_bw, &pipe_config->fdi_m_n);
  5992. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5993. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5994. pipe_config->pipe_bpp -= 2*3;
  5995. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5996. pipe_config->pipe_bpp);
  5997. needs_recompute = true;
  5998. pipe_config->bw_constrained = true;
  5999. goto retry;
  6000. }
  6001. if (needs_recompute)
  6002. return RETRY;
  6003. return ret;
  6004. }
  6005. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  6006. struct intel_crtc_state *pipe_config)
  6007. {
  6008. if (pipe_config->pipe_bpp > 24)
  6009. return false;
  6010. /* HSW can handle pixel rate up to cdclk? */
  6011. if (IS_HASWELL(dev_priv))
  6012. return true;
  6013. /*
  6014. * We compare against max which means we must take
  6015. * the increased cdclk requirement into account when
  6016. * calculating the new cdclk.
  6017. *
  6018. * Should measure whether using a lower cdclk w/o IPS
  6019. */
  6020. return ilk_pipe_pixel_rate(pipe_config) <=
  6021. dev_priv->max_cdclk_freq * 95 / 100;
  6022. }
  6023. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  6024. struct intel_crtc_state *pipe_config)
  6025. {
  6026. struct drm_device *dev = crtc->base.dev;
  6027. struct drm_i915_private *dev_priv = to_i915(dev);
  6028. pipe_config->ips_enabled = i915.enable_ips &&
  6029. hsw_crtc_supports_ips(crtc) &&
  6030. pipe_config_supports_ips(dev_priv, pipe_config);
  6031. }
  6032. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  6033. {
  6034. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6035. /* GDG double wide on either pipe, otherwise pipe A only */
  6036. return INTEL_INFO(dev_priv)->gen < 4 &&
  6037. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  6038. }
  6039. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  6040. struct intel_crtc_state *pipe_config)
  6041. {
  6042. struct drm_device *dev = crtc->base.dev;
  6043. struct drm_i915_private *dev_priv = to_i915(dev);
  6044. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  6045. int clock_limit = dev_priv->max_dotclk_freq;
  6046. if (INTEL_GEN(dev_priv) < 4) {
  6047. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  6048. /*
  6049. * Enable double wide mode when the dot clock
  6050. * is > 90% of the (display) core speed.
  6051. */
  6052. if (intel_crtc_supports_double_wide(crtc) &&
  6053. adjusted_mode->crtc_clock > clock_limit) {
  6054. clock_limit = dev_priv->max_dotclk_freq;
  6055. pipe_config->double_wide = true;
  6056. }
  6057. }
  6058. if (adjusted_mode->crtc_clock > clock_limit) {
  6059. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  6060. adjusted_mode->crtc_clock, clock_limit,
  6061. yesno(pipe_config->double_wide));
  6062. return -EINVAL;
  6063. }
  6064. /*
  6065. * Pipe horizontal size must be even in:
  6066. * - DVO ganged mode
  6067. * - LVDS dual channel mode
  6068. * - Double wide pipe
  6069. */
  6070. if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  6071. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  6072. pipe_config->pipe_src_w &= ~1;
  6073. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  6074. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  6075. */
  6076. if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
  6077. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  6078. return -EINVAL;
  6079. if (HAS_IPS(dev_priv))
  6080. hsw_compute_ips_config(crtc, pipe_config);
  6081. if (pipe_config->has_pch_encoder)
  6082. return ironlake_fdi_compute_config(crtc, pipe_config);
  6083. return 0;
  6084. }
  6085. static int skylake_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6086. {
  6087. u32 cdctl;
  6088. skl_dpll0_update(dev_priv);
  6089. if (dev_priv->cdclk_pll.vco == 0)
  6090. return dev_priv->cdclk_pll.ref;
  6091. cdctl = I915_READ(CDCLK_CTL);
  6092. if (dev_priv->cdclk_pll.vco == 8640000) {
  6093. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  6094. case CDCLK_FREQ_450_432:
  6095. return 432000;
  6096. case CDCLK_FREQ_337_308:
  6097. return 308571;
  6098. case CDCLK_FREQ_540:
  6099. return 540000;
  6100. case CDCLK_FREQ_675_617:
  6101. return 617143;
  6102. default:
  6103. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  6104. }
  6105. } else {
  6106. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  6107. case CDCLK_FREQ_450_432:
  6108. return 450000;
  6109. case CDCLK_FREQ_337_308:
  6110. return 337500;
  6111. case CDCLK_FREQ_540:
  6112. return 540000;
  6113. case CDCLK_FREQ_675_617:
  6114. return 675000;
  6115. default:
  6116. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  6117. }
  6118. }
  6119. return dev_priv->cdclk_pll.ref;
  6120. }
  6121. static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
  6122. {
  6123. u32 val;
  6124. dev_priv->cdclk_pll.ref = 19200;
  6125. dev_priv->cdclk_pll.vco = 0;
  6126. val = I915_READ(BXT_DE_PLL_ENABLE);
  6127. if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
  6128. return;
  6129. if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
  6130. return;
  6131. val = I915_READ(BXT_DE_PLL_CTL);
  6132. dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
  6133. dev_priv->cdclk_pll.ref;
  6134. }
  6135. static int broxton_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6136. {
  6137. u32 divider;
  6138. int div, vco;
  6139. bxt_de_pll_update(dev_priv);
  6140. vco = dev_priv->cdclk_pll.vco;
  6141. if (vco == 0)
  6142. return dev_priv->cdclk_pll.ref;
  6143. divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
  6144. switch (divider) {
  6145. case BXT_CDCLK_CD2X_DIV_SEL_1:
  6146. div = 2;
  6147. break;
  6148. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  6149. WARN(IS_GEMINILAKE(dev_priv), "Unsupported divider\n");
  6150. div = 3;
  6151. break;
  6152. case BXT_CDCLK_CD2X_DIV_SEL_2:
  6153. div = 4;
  6154. break;
  6155. case BXT_CDCLK_CD2X_DIV_SEL_4:
  6156. div = 8;
  6157. break;
  6158. default:
  6159. MISSING_CASE(divider);
  6160. return dev_priv->cdclk_pll.ref;
  6161. }
  6162. return DIV_ROUND_CLOSEST(vco, div);
  6163. }
  6164. static int broadwell_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6165. {
  6166. uint32_t lcpll = I915_READ(LCPLL_CTL);
  6167. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  6168. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  6169. return 800000;
  6170. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  6171. return 450000;
  6172. else if (freq == LCPLL_CLK_FREQ_450)
  6173. return 450000;
  6174. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  6175. return 540000;
  6176. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  6177. return 337500;
  6178. else
  6179. return 675000;
  6180. }
  6181. static int haswell_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6182. {
  6183. uint32_t lcpll = I915_READ(LCPLL_CTL);
  6184. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  6185. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  6186. return 800000;
  6187. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  6188. return 450000;
  6189. else if (freq == LCPLL_CLK_FREQ_450)
  6190. return 450000;
  6191. else if (IS_HSW_ULT(dev_priv))
  6192. return 337500;
  6193. else
  6194. return 540000;
  6195. }
  6196. static int valleyview_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6197. {
  6198. return vlv_get_cck_clock_hpll(dev_priv, "cdclk",
  6199. CCK_DISPLAY_CLOCK_CONTROL);
  6200. }
  6201. static int ilk_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6202. {
  6203. return 450000;
  6204. }
  6205. static int i945_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6206. {
  6207. return 400000;
  6208. }
  6209. static int i915_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6210. {
  6211. return 333333;
  6212. }
  6213. static int i9xx_misc_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6214. {
  6215. return 200000;
  6216. }
  6217. static int pnv_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6218. {
  6219. struct pci_dev *pdev = dev_priv->drm.pdev;
  6220. u16 gcfgc = 0;
  6221. pci_read_config_word(pdev, GCFGC, &gcfgc);
  6222. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  6223. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  6224. return 266667;
  6225. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  6226. return 333333;
  6227. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  6228. return 444444;
  6229. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  6230. return 200000;
  6231. default:
  6232. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  6233. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  6234. return 133333;
  6235. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  6236. return 166667;
  6237. }
  6238. }
  6239. static int i915gm_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6240. {
  6241. struct pci_dev *pdev = dev_priv->drm.pdev;
  6242. u16 gcfgc = 0;
  6243. pci_read_config_word(pdev, GCFGC, &gcfgc);
  6244. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  6245. return 133333;
  6246. else {
  6247. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  6248. case GC_DISPLAY_CLOCK_333_MHZ:
  6249. return 333333;
  6250. default:
  6251. case GC_DISPLAY_CLOCK_190_200_MHZ:
  6252. return 190000;
  6253. }
  6254. }
  6255. }
  6256. static int i865_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6257. {
  6258. return 266667;
  6259. }
  6260. static int i85x_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6261. {
  6262. struct pci_dev *pdev = dev_priv->drm.pdev;
  6263. u16 hpllcc = 0;
  6264. /*
  6265. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  6266. * encoding is different :(
  6267. * FIXME is this the right way to detect 852GM/852GMV?
  6268. */
  6269. if (pdev->revision == 0x1)
  6270. return 133333;
  6271. pci_bus_read_config_word(pdev->bus,
  6272. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  6273. /* Assume that the hardware is in the high speed state. This
  6274. * should be the default.
  6275. */
  6276. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  6277. case GC_CLOCK_133_200:
  6278. case GC_CLOCK_133_200_2:
  6279. case GC_CLOCK_100_200:
  6280. return 200000;
  6281. case GC_CLOCK_166_250:
  6282. return 250000;
  6283. case GC_CLOCK_100_133:
  6284. return 133333;
  6285. case GC_CLOCK_133_266:
  6286. case GC_CLOCK_133_266_2:
  6287. case GC_CLOCK_166_266:
  6288. return 266667;
  6289. }
  6290. /* Shouldn't happen */
  6291. return 0;
  6292. }
  6293. static int i830_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6294. {
  6295. return 133333;
  6296. }
  6297. static unsigned int intel_hpll_vco(struct drm_i915_private *dev_priv)
  6298. {
  6299. static const unsigned int blb_vco[8] = {
  6300. [0] = 3200000,
  6301. [1] = 4000000,
  6302. [2] = 5333333,
  6303. [3] = 4800000,
  6304. [4] = 6400000,
  6305. };
  6306. static const unsigned int pnv_vco[8] = {
  6307. [0] = 3200000,
  6308. [1] = 4000000,
  6309. [2] = 5333333,
  6310. [3] = 4800000,
  6311. [4] = 2666667,
  6312. };
  6313. static const unsigned int cl_vco[8] = {
  6314. [0] = 3200000,
  6315. [1] = 4000000,
  6316. [2] = 5333333,
  6317. [3] = 6400000,
  6318. [4] = 3333333,
  6319. [5] = 3566667,
  6320. [6] = 4266667,
  6321. };
  6322. static const unsigned int elk_vco[8] = {
  6323. [0] = 3200000,
  6324. [1] = 4000000,
  6325. [2] = 5333333,
  6326. [3] = 4800000,
  6327. };
  6328. static const unsigned int ctg_vco[8] = {
  6329. [0] = 3200000,
  6330. [1] = 4000000,
  6331. [2] = 5333333,
  6332. [3] = 6400000,
  6333. [4] = 2666667,
  6334. [5] = 4266667,
  6335. };
  6336. const unsigned int *vco_table;
  6337. unsigned int vco;
  6338. uint8_t tmp = 0;
  6339. /* FIXME other chipsets? */
  6340. if (IS_GM45(dev_priv))
  6341. vco_table = ctg_vco;
  6342. else if (IS_G4X(dev_priv))
  6343. vco_table = elk_vco;
  6344. else if (IS_I965GM(dev_priv))
  6345. vco_table = cl_vco;
  6346. else if (IS_PINEVIEW(dev_priv))
  6347. vco_table = pnv_vco;
  6348. else if (IS_G33(dev_priv))
  6349. vco_table = blb_vco;
  6350. else
  6351. return 0;
  6352. tmp = I915_READ(IS_MOBILE(dev_priv) ? HPLLVCO_MOBILE : HPLLVCO);
  6353. vco = vco_table[tmp & 0x7];
  6354. if (vco == 0)
  6355. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  6356. else
  6357. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  6358. return vco;
  6359. }
  6360. static int gm45_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6361. {
  6362. struct pci_dev *pdev = dev_priv->drm.pdev;
  6363. unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
  6364. uint16_t tmp = 0;
  6365. pci_read_config_word(pdev, GCFGC, &tmp);
  6366. cdclk_sel = (tmp >> 12) & 0x1;
  6367. switch (vco) {
  6368. case 2666667:
  6369. case 4000000:
  6370. case 5333333:
  6371. return cdclk_sel ? 333333 : 222222;
  6372. case 3200000:
  6373. return cdclk_sel ? 320000 : 228571;
  6374. default:
  6375. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  6376. return 222222;
  6377. }
  6378. }
  6379. static int i965gm_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6380. {
  6381. struct pci_dev *pdev = dev_priv->drm.pdev;
  6382. static const uint8_t div_3200[] = { 16, 10, 8 };
  6383. static const uint8_t div_4000[] = { 20, 12, 10 };
  6384. static const uint8_t div_5333[] = { 24, 16, 14 };
  6385. const uint8_t *div_table;
  6386. unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
  6387. uint16_t tmp = 0;
  6388. pci_read_config_word(pdev, GCFGC, &tmp);
  6389. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  6390. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  6391. goto fail;
  6392. switch (vco) {
  6393. case 3200000:
  6394. div_table = div_3200;
  6395. break;
  6396. case 4000000:
  6397. div_table = div_4000;
  6398. break;
  6399. case 5333333:
  6400. div_table = div_5333;
  6401. break;
  6402. default:
  6403. goto fail;
  6404. }
  6405. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  6406. fail:
  6407. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  6408. return 200000;
  6409. }
  6410. static int g33_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6411. {
  6412. struct pci_dev *pdev = dev_priv->drm.pdev;
  6413. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  6414. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  6415. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  6416. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  6417. const uint8_t *div_table;
  6418. unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
  6419. uint16_t tmp = 0;
  6420. pci_read_config_word(pdev, GCFGC, &tmp);
  6421. cdclk_sel = (tmp >> 4) & 0x7;
  6422. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  6423. goto fail;
  6424. switch (vco) {
  6425. case 3200000:
  6426. div_table = div_3200;
  6427. break;
  6428. case 4000000:
  6429. div_table = div_4000;
  6430. break;
  6431. case 4800000:
  6432. div_table = div_4800;
  6433. break;
  6434. case 5333333:
  6435. div_table = div_5333;
  6436. break;
  6437. default:
  6438. goto fail;
  6439. }
  6440. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  6441. fail:
  6442. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  6443. return 190476;
  6444. }
  6445. static void
  6446. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  6447. {
  6448. while (*num > DATA_LINK_M_N_MASK ||
  6449. *den > DATA_LINK_M_N_MASK) {
  6450. *num >>= 1;
  6451. *den >>= 1;
  6452. }
  6453. }
  6454. static void compute_m_n(unsigned int m, unsigned int n,
  6455. uint32_t *ret_m, uint32_t *ret_n)
  6456. {
  6457. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  6458. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  6459. intel_reduce_m_n_ratio(ret_m, ret_n);
  6460. }
  6461. void
  6462. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  6463. int pixel_clock, int link_clock,
  6464. struct intel_link_m_n *m_n)
  6465. {
  6466. m_n->tu = 64;
  6467. compute_m_n(bits_per_pixel * pixel_clock,
  6468. link_clock * nlanes * 8,
  6469. &m_n->gmch_m, &m_n->gmch_n);
  6470. compute_m_n(pixel_clock, link_clock,
  6471. &m_n->link_m, &m_n->link_n);
  6472. }
  6473. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  6474. {
  6475. if (i915.panel_use_ssc >= 0)
  6476. return i915.panel_use_ssc != 0;
  6477. return dev_priv->vbt.lvds_use_ssc
  6478. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  6479. }
  6480. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  6481. {
  6482. return (1 << dpll->n) << 16 | dpll->m2;
  6483. }
  6484. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  6485. {
  6486. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  6487. }
  6488. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  6489. struct intel_crtc_state *crtc_state,
  6490. struct dpll *reduced_clock)
  6491. {
  6492. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6493. u32 fp, fp2 = 0;
  6494. if (IS_PINEVIEW(dev_priv)) {
  6495. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  6496. if (reduced_clock)
  6497. fp2 = pnv_dpll_compute_fp(reduced_clock);
  6498. } else {
  6499. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6500. if (reduced_clock)
  6501. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6502. }
  6503. crtc_state->dpll_hw_state.fp0 = fp;
  6504. crtc->lowfreq_avail = false;
  6505. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6506. reduced_clock) {
  6507. crtc_state->dpll_hw_state.fp1 = fp2;
  6508. crtc->lowfreq_avail = true;
  6509. } else {
  6510. crtc_state->dpll_hw_state.fp1 = fp;
  6511. }
  6512. }
  6513. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  6514. pipe)
  6515. {
  6516. u32 reg_val;
  6517. /*
  6518. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  6519. * and set it to a reasonable value instead.
  6520. */
  6521. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6522. reg_val &= 0xffffff00;
  6523. reg_val |= 0x00000030;
  6524. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6525. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6526. reg_val &= 0x8cffffff;
  6527. reg_val = 0x8c000000;
  6528. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6529. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6530. reg_val &= 0xffffff00;
  6531. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6532. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6533. reg_val &= 0x00ffffff;
  6534. reg_val |= 0xb0000000;
  6535. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6536. }
  6537. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  6538. struct intel_link_m_n *m_n)
  6539. {
  6540. struct drm_device *dev = crtc->base.dev;
  6541. struct drm_i915_private *dev_priv = to_i915(dev);
  6542. int pipe = crtc->pipe;
  6543. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6544. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  6545. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  6546. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  6547. }
  6548. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6549. struct intel_link_m_n *m_n,
  6550. struct intel_link_m_n *m2_n2)
  6551. {
  6552. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6553. int pipe = crtc->pipe;
  6554. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6555. if (INTEL_GEN(dev_priv) >= 5) {
  6556. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6557. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6558. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6559. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6560. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6561. * for gen < 8) and if DRRS is supported (to make sure the
  6562. * registers are not unnecessarily accessed).
  6563. */
  6564. if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
  6565. INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
  6566. I915_WRITE(PIPE_DATA_M2(transcoder),
  6567. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6568. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6569. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6570. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6571. }
  6572. } else {
  6573. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6574. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6575. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6576. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6577. }
  6578. }
  6579. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6580. {
  6581. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6582. if (m_n == M1_N1) {
  6583. dp_m_n = &crtc->config->dp_m_n;
  6584. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6585. } else if (m_n == M2_N2) {
  6586. /*
  6587. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6588. * needs to be programmed into M1_N1.
  6589. */
  6590. dp_m_n = &crtc->config->dp_m2_n2;
  6591. } else {
  6592. DRM_ERROR("Unsupported divider value\n");
  6593. return;
  6594. }
  6595. if (crtc->config->has_pch_encoder)
  6596. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6597. else
  6598. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6599. }
  6600. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6601. struct intel_crtc_state *pipe_config)
  6602. {
  6603. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  6604. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6605. if (crtc->pipe != PIPE_A)
  6606. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6607. /* DPLL not used with DSI, but still need the rest set up */
  6608. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  6609. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  6610. DPLL_EXT_BUFFER_ENABLE_VLV;
  6611. pipe_config->dpll_hw_state.dpll_md =
  6612. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6613. }
  6614. static void chv_compute_dpll(struct intel_crtc *crtc,
  6615. struct intel_crtc_state *pipe_config)
  6616. {
  6617. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  6618. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6619. if (crtc->pipe != PIPE_A)
  6620. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6621. /* DPLL not used with DSI, but still need the rest set up */
  6622. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  6623. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  6624. pipe_config->dpll_hw_state.dpll_md =
  6625. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6626. }
  6627. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6628. const struct intel_crtc_state *pipe_config)
  6629. {
  6630. struct drm_device *dev = crtc->base.dev;
  6631. struct drm_i915_private *dev_priv = to_i915(dev);
  6632. enum pipe pipe = crtc->pipe;
  6633. u32 mdiv;
  6634. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6635. u32 coreclk, reg_val;
  6636. /* Enable Refclk */
  6637. I915_WRITE(DPLL(pipe),
  6638. pipe_config->dpll_hw_state.dpll &
  6639. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  6640. /* No need to actually set up the DPLL with DSI */
  6641. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6642. return;
  6643. mutex_lock(&dev_priv->sb_lock);
  6644. bestn = pipe_config->dpll.n;
  6645. bestm1 = pipe_config->dpll.m1;
  6646. bestm2 = pipe_config->dpll.m2;
  6647. bestp1 = pipe_config->dpll.p1;
  6648. bestp2 = pipe_config->dpll.p2;
  6649. /* See eDP HDMI DPIO driver vbios notes doc */
  6650. /* PLL B needs special handling */
  6651. if (pipe == PIPE_B)
  6652. vlv_pllb_recal_opamp(dev_priv, pipe);
  6653. /* Set up Tx target for periodic Rcomp update */
  6654. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6655. /* Disable target IRef on PLL */
  6656. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6657. reg_val &= 0x00ffffff;
  6658. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6659. /* Disable fast lock */
  6660. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6661. /* Set idtafcrecal before PLL is enabled */
  6662. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6663. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6664. mdiv |= ((bestn << DPIO_N_SHIFT));
  6665. mdiv |= (1 << DPIO_K_SHIFT);
  6666. /*
  6667. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6668. * but we don't support that).
  6669. * Note: don't use the DAC post divider as it seems unstable.
  6670. */
  6671. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6672. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6673. mdiv |= DPIO_ENABLE_CALIBRATION;
  6674. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6675. /* Set HBR and RBR LPF coefficients */
  6676. if (pipe_config->port_clock == 162000 ||
  6677. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
  6678. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
  6679. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6680. 0x009f0003);
  6681. else
  6682. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6683. 0x00d0000f);
  6684. if (intel_crtc_has_dp_encoder(pipe_config)) {
  6685. /* Use SSC source */
  6686. if (pipe == PIPE_A)
  6687. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6688. 0x0df40000);
  6689. else
  6690. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6691. 0x0df70000);
  6692. } else { /* HDMI or VGA */
  6693. /* Use bend source */
  6694. if (pipe == PIPE_A)
  6695. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6696. 0x0df70000);
  6697. else
  6698. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6699. 0x0df40000);
  6700. }
  6701. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6702. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6703. if (intel_crtc_has_dp_encoder(crtc->config))
  6704. coreclk |= 0x01000000;
  6705. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6706. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6707. mutex_unlock(&dev_priv->sb_lock);
  6708. }
  6709. static void chv_prepare_pll(struct intel_crtc *crtc,
  6710. const struct intel_crtc_state *pipe_config)
  6711. {
  6712. struct drm_device *dev = crtc->base.dev;
  6713. struct drm_i915_private *dev_priv = to_i915(dev);
  6714. enum pipe pipe = crtc->pipe;
  6715. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6716. u32 loopfilter, tribuf_calcntr;
  6717. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6718. u32 dpio_val;
  6719. int vco;
  6720. /* Enable Refclk and SSC */
  6721. I915_WRITE(DPLL(pipe),
  6722. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6723. /* No need to actually set up the DPLL with DSI */
  6724. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6725. return;
  6726. bestn = pipe_config->dpll.n;
  6727. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6728. bestm1 = pipe_config->dpll.m1;
  6729. bestm2 = pipe_config->dpll.m2 >> 22;
  6730. bestp1 = pipe_config->dpll.p1;
  6731. bestp2 = pipe_config->dpll.p2;
  6732. vco = pipe_config->dpll.vco;
  6733. dpio_val = 0;
  6734. loopfilter = 0;
  6735. mutex_lock(&dev_priv->sb_lock);
  6736. /* p1 and p2 divider */
  6737. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6738. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6739. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6740. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6741. 1 << DPIO_CHV_K_DIV_SHIFT);
  6742. /* Feedback post-divider - m2 */
  6743. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6744. /* Feedback refclk divider - n and m1 */
  6745. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6746. DPIO_CHV_M1_DIV_BY_2 |
  6747. 1 << DPIO_CHV_N_DIV_SHIFT);
  6748. /* M2 fraction division */
  6749. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6750. /* M2 fraction division enable */
  6751. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6752. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6753. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6754. if (bestm2_frac)
  6755. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6756. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6757. /* Program digital lock detect threshold */
  6758. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6759. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6760. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6761. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6762. if (!bestm2_frac)
  6763. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6764. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6765. /* Loop filter */
  6766. if (vco == 5400000) {
  6767. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6768. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6769. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6770. tribuf_calcntr = 0x9;
  6771. } else if (vco <= 6200000) {
  6772. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6773. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6774. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6775. tribuf_calcntr = 0x9;
  6776. } else if (vco <= 6480000) {
  6777. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6778. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6779. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6780. tribuf_calcntr = 0x8;
  6781. } else {
  6782. /* Not supported. Apply the same limits as in the max case */
  6783. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6784. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6785. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6786. tribuf_calcntr = 0;
  6787. }
  6788. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6789. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6790. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6791. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6792. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6793. /* AFC Recal */
  6794. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6795. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6796. DPIO_AFC_RECAL);
  6797. mutex_unlock(&dev_priv->sb_lock);
  6798. }
  6799. /**
  6800. * vlv_force_pll_on - forcibly enable just the PLL
  6801. * @dev_priv: i915 private structure
  6802. * @pipe: pipe PLL to enable
  6803. * @dpll: PLL configuration
  6804. *
  6805. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6806. * in cases where we need the PLL enabled even when @pipe is not going to
  6807. * be enabled.
  6808. */
  6809. int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
  6810. const struct dpll *dpll)
  6811. {
  6812. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  6813. struct intel_crtc_state *pipe_config;
  6814. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  6815. if (!pipe_config)
  6816. return -ENOMEM;
  6817. pipe_config->base.crtc = &crtc->base;
  6818. pipe_config->pixel_multiplier = 1;
  6819. pipe_config->dpll = *dpll;
  6820. if (IS_CHERRYVIEW(dev_priv)) {
  6821. chv_compute_dpll(crtc, pipe_config);
  6822. chv_prepare_pll(crtc, pipe_config);
  6823. chv_enable_pll(crtc, pipe_config);
  6824. } else {
  6825. vlv_compute_dpll(crtc, pipe_config);
  6826. vlv_prepare_pll(crtc, pipe_config);
  6827. vlv_enable_pll(crtc, pipe_config);
  6828. }
  6829. kfree(pipe_config);
  6830. return 0;
  6831. }
  6832. /**
  6833. * vlv_force_pll_off - forcibly disable just the PLL
  6834. * @dev_priv: i915 private structure
  6835. * @pipe: pipe PLL to disable
  6836. *
  6837. * Disable the PLL for @pipe. To be used in cases where we need
  6838. * the PLL enabled even when @pipe is not going to be enabled.
  6839. */
  6840. void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
  6841. {
  6842. if (IS_CHERRYVIEW(dev_priv))
  6843. chv_disable_pll(dev_priv, pipe);
  6844. else
  6845. vlv_disable_pll(dev_priv, pipe);
  6846. }
  6847. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6848. struct intel_crtc_state *crtc_state,
  6849. struct dpll *reduced_clock)
  6850. {
  6851. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6852. u32 dpll;
  6853. struct dpll *clock = &crtc_state->dpll;
  6854. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6855. dpll = DPLL_VGA_MODE_DIS;
  6856. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  6857. dpll |= DPLLB_MODE_LVDS;
  6858. else
  6859. dpll |= DPLLB_MODE_DAC_SERIAL;
  6860. if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  6861. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  6862. dpll |= (crtc_state->pixel_multiplier - 1)
  6863. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6864. }
  6865. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6866. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  6867. dpll |= DPLL_SDVO_HIGH_SPEED;
  6868. if (intel_crtc_has_dp_encoder(crtc_state))
  6869. dpll |= DPLL_SDVO_HIGH_SPEED;
  6870. /* compute bitmask from p1 value */
  6871. if (IS_PINEVIEW(dev_priv))
  6872. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6873. else {
  6874. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6875. if (IS_G4X(dev_priv) && reduced_clock)
  6876. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6877. }
  6878. switch (clock->p2) {
  6879. case 5:
  6880. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6881. break;
  6882. case 7:
  6883. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6884. break;
  6885. case 10:
  6886. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6887. break;
  6888. case 14:
  6889. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6890. break;
  6891. }
  6892. if (INTEL_GEN(dev_priv) >= 4)
  6893. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6894. if (crtc_state->sdvo_tv_clock)
  6895. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6896. else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6897. intel_panel_use_ssc(dev_priv))
  6898. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6899. else
  6900. dpll |= PLL_REF_INPUT_DREFCLK;
  6901. dpll |= DPLL_VCO_ENABLE;
  6902. crtc_state->dpll_hw_state.dpll = dpll;
  6903. if (INTEL_GEN(dev_priv) >= 4) {
  6904. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6905. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6906. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6907. }
  6908. }
  6909. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6910. struct intel_crtc_state *crtc_state,
  6911. struct dpll *reduced_clock)
  6912. {
  6913. struct drm_device *dev = crtc->base.dev;
  6914. struct drm_i915_private *dev_priv = to_i915(dev);
  6915. u32 dpll;
  6916. struct dpll *clock = &crtc_state->dpll;
  6917. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6918. dpll = DPLL_VGA_MODE_DIS;
  6919. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6920. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6921. } else {
  6922. if (clock->p1 == 2)
  6923. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6924. else
  6925. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6926. if (clock->p2 == 4)
  6927. dpll |= PLL_P2_DIVIDE_BY_4;
  6928. }
  6929. if (!IS_I830(dev_priv) &&
  6930. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
  6931. dpll |= DPLL_DVO_2X_MODE;
  6932. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6933. intel_panel_use_ssc(dev_priv))
  6934. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6935. else
  6936. dpll |= PLL_REF_INPUT_DREFCLK;
  6937. dpll |= DPLL_VCO_ENABLE;
  6938. crtc_state->dpll_hw_state.dpll = dpll;
  6939. }
  6940. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6941. {
  6942. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  6943. enum pipe pipe = intel_crtc->pipe;
  6944. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6945. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  6946. uint32_t crtc_vtotal, crtc_vblank_end;
  6947. int vsyncshift = 0;
  6948. /* We need to be careful not to changed the adjusted mode, for otherwise
  6949. * the hw state checker will get angry at the mismatch. */
  6950. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6951. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6952. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6953. /* the chip adds 2 halflines automatically */
  6954. crtc_vtotal -= 1;
  6955. crtc_vblank_end -= 1;
  6956. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6957. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6958. else
  6959. vsyncshift = adjusted_mode->crtc_hsync_start -
  6960. adjusted_mode->crtc_htotal / 2;
  6961. if (vsyncshift < 0)
  6962. vsyncshift += adjusted_mode->crtc_htotal;
  6963. }
  6964. if (INTEL_GEN(dev_priv) > 3)
  6965. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6966. I915_WRITE(HTOTAL(cpu_transcoder),
  6967. (adjusted_mode->crtc_hdisplay - 1) |
  6968. ((adjusted_mode->crtc_htotal - 1) << 16));
  6969. I915_WRITE(HBLANK(cpu_transcoder),
  6970. (adjusted_mode->crtc_hblank_start - 1) |
  6971. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6972. I915_WRITE(HSYNC(cpu_transcoder),
  6973. (adjusted_mode->crtc_hsync_start - 1) |
  6974. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6975. I915_WRITE(VTOTAL(cpu_transcoder),
  6976. (adjusted_mode->crtc_vdisplay - 1) |
  6977. ((crtc_vtotal - 1) << 16));
  6978. I915_WRITE(VBLANK(cpu_transcoder),
  6979. (adjusted_mode->crtc_vblank_start - 1) |
  6980. ((crtc_vblank_end - 1) << 16));
  6981. I915_WRITE(VSYNC(cpu_transcoder),
  6982. (adjusted_mode->crtc_vsync_start - 1) |
  6983. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6984. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6985. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6986. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6987. * bits. */
  6988. if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
  6989. (pipe == PIPE_B || pipe == PIPE_C))
  6990. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6991. }
  6992. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  6993. {
  6994. struct drm_device *dev = intel_crtc->base.dev;
  6995. struct drm_i915_private *dev_priv = to_i915(dev);
  6996. enum pipe pipe = intel_crtc->pipe;
  6997. /* pipesrc controls the size that is scaled from, which should
  6998. * always be the user's requested size.
  6999. */
  7000. I915_WRITE(PIPESRC(pipe),
  7001. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  7002. (intel_crtc->config->pipe_src_h - 1));
  7003. }
  7004. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  7005. struct intel_crtc_state *pipe_config)
  7006. {
  7007. struct drm_device *dev = crtc->base.dev;
  7008. struct drm_i915_private *dev_priv = to_i915(dev);
  7009. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  7010. uint32_t tmp;
  7011. tmp = I915_READ(HTOTAL(cpu_transcoder));
  7012. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  7013. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  7014. tmp = I915_READ(HBLANK(cpu_transcoder));
  7015. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  7016. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  7017. tmp = I915_READ(HSYNC(cpu_transcoder));
  7018. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  7019. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  7020. tmp = I915_READ(VTOTAL(cpu_transcoder));
  7021. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  7022. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  7023. tmp = I915_READ(VBLANK(cpu_transcoder));
  7024. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  7025. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  7026. tmp = I915_READ(VSYNC(cpu_transcoder));
  7027. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  7028. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  7029. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  7030. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  7031. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  7032. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  7033. }
  7034. }
  7035. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  7036. struct intel_crtc_state *pipe_config)
  7037. {
  7038. struct drm_device *dev = crtc->base.dev;
  7039. struct drm_i915_private *dev_priv = to_i915(dev);
  7040. u32 tmp;
  7041. tmp = I915_READ(PIPESRC(crtc->pipe));
  7042. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  7043. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  7044. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  7045. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  7046. }
  7047. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  7048. struct intel_crtc_state *pipe_config)
  7049. {
  7050. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  7051. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  7052. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  7053. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  7054. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  7055. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  7056. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  7057. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  7058. mode->flags = pipe_config->base.adjusted_mode.flags;
  7059. mode->type = DRM_MODE_TYPE_DRIVER;
  7060. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  7061. mode->hsync = drm_mode_hsync(mode);
  7062. mode->vrefresh = drm_mode_vrefresh(mode);
  7063. drm_mode_set_name(mode);
  7064. }
  7065. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  7066. {
  7067. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  7068. uint32_t pipeconf;
  7069. pipeconf = 0;
  7070. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  7071. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  7072. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  7073. if (intel_crtc->config->double_wide)
  7074. pipeconf |= PIPECONF_DOUBLE_WIDE;
  7075. /* only g4x and later have fancy bpc/dither controls */
  7076. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  7077. IS_CHERRYVIEW(dev_priv)) {
  7078. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  7079. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  7080. pipeconf |= PIPECONF_DITHER_EN |
  7081. PIPECONF_DITHER_TYPE_SP;
  7082. switch (intel_crtc->config->pipe_bpp) {
  7083. case 18:
  7084. pipeconf |= PIPECONF_6BPC;
  7085. break;
  7086. case 24:
  7087. pipeconf |= PIPECONF_8BPC;
  7088. break;
  7089. case 30:
  7090. pipeconf |= PIPECONF_10BPC;
  7091. break;
  7092. default:
  7093. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7094. BUG();
  7095. }
  7096. }
  7097. if (HAS_PIPE_CXSR(dev_priv)) {
  7098. if (intel_crtc->lowfreq_avail) {
  7099. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  7100. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  7101. } else {
  7102. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  7103. }
  7104. }
  7105. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  7106. if (INTEL_GEN(dev_priv) < 4 ||
  7107. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  7108. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  7109. else
  7110. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  7111. } else
  7112. pipeconf |= PIPECONF_PROGRESSIVE;
  7113. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  7114. intel_crtc->config->limited_color_range)
  7115. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  7116. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  7117. POSTING_READ(PIPECONF(intel_crtc->pipe));
  7118. }
  7119. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  7120. struct intel_crtc_state *crtc_state)
  7121. {
  7122. struct drm_device *dev = crtc->base.dev;
  7123. struct drm_i915_private *dev_priv = to_i915(dev);
  7124. const struct intel_limit *limit;
  7125. int refclk = 48000;
  7126. memset(&crtc_state->dpll_hw_state, 0,
  7127. sizeof(crtc_state->dpll_hw_state));
  7128. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7129. if (intel_panel_use_ssc(dev_priv)) {
  7130. refclk = dev_priv->vbt.lvds_ssc_freq;
  7131. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7132. }
  7133. limit = &intel_limits_i8xx_lvds;
  7134. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
  7135. limit = &intel_limits_i8xx_dvo;
  7136. } else {
  7137. limit = &intel_limits_i8xx_dac;
  7138. }
  7139. if (!crtc_state->clock_set &&
  7140. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7141. refclk, NULL, &crtc_state->dpll)) {
  7142. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7143. return -EINVAL;
  7144. }
  7145. i8xx_compute_dpll(crtc, crtc_state, NULL);
  7146. return 0;
  7147. }
  7148. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  7149. struct intel_crtc_state *crtc_state)
  7150. {
  7151. struct drm_device *dev = crtc->base.dev;
  7152. struct drm_i915_private *dev_priv = to_i915(dev);
  7153. const struct intel_limit *limit;
  7154. int refclk = 96000;
  7155. memset(&crtc_state->dpll_hw_state, 0,
  7156. sizeof(crtc_state->dpll_hw_state));
  7157. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7158. if (intel_panel_use_ssc(dev_priv)) {
  7159. refclk = dev_priv->vbt.lvds_ssc_freq;
  7160. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7161. }
  7162. if (intel_is_dual_link_lvds(dev))
  7163. limit = &intel_limits_g4x_dual_channel_lvds;
  7164. else
  7165. limit = &intel_limits_g4x_single_channel_lvds;
  7166. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  7167. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  7168. limit = &intel_limits_g4x_hdmi;
  7169. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  7170. limit = &intel_limits_g4x_sdvo;
  7171. } else {
  7172. /* The option is for other outputs */
  7173. limit = &intel_limits_i9xx_sdvo;
  7174. }
  7175. if (!crtc_state->clock_set &&
  7176. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7177. refclk, NULL, &crtc_state->dpll)) {
  7178. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7179. return -EINVAL;
  7180. }
  7181. i9xx_compute_dpll(crtc, crtc_state, NULL);
  7182. return 0;
  7183. }
  7184. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  7185. struct intel_crtc_state *crtc_state)
  7186. {
  7187. struct drm_device *dev = crtc->base.dev;
  7188. struct drm_i915_private *dev_priv = to_i915(dev);
  7189. const struct intel_limit *limit;
  7190. int refclk = 96000;
  7191. memset(&crtc_state->dpll_hw_state, 0,
  7192. sizeof(crtc_state->dpll_hw_state));
  7193. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7194. if (intel_panel_use_ssc(dev_priv)) {
  7195. refclk = dev_priv->vbt.lvds_ssc_freq;
  7196. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7197. }
  7198. limit = &intel_limits_pineview_lvds;
  7199. } else {
  7200. limit = &intel_limits_pineview_sdvo;
  7201. }
  7202. if (!crtc_state->clock_set &&
  7203. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7204. refclk, NULL, &crtc_state->dpll)) {
  7205. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7206. return -EINVAL;
  7207. }
  7208. i9xx_compute_dpll(crtc, crtc_state, NULL);
  7209. return 0;
  7210. }
  7211. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  7212. struct intel_crtc_state *crtc_state)
  7213. {
  7214. struct drm_device *dev = crtc->base.dev;
  7215. struct drm_i915_private *dev_priv = to_i915(dev);
  7216. const struct intel_limit *limit;
  7217. int refclk = 96000;
  7218. memset(&crtc_state->dpll_hw_state, 0,
  7219. sizeof(crtc_state->dpll_hw_state));
  7220. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7221. if (intel_panel_use_ssc(dev_priv)) {
  7222. refclk = dev_priv->vbt.lvds_ssc_freq;
  7223. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7224. }
  7225. limit = &intel_limits_i9xx_lvds;
  7226. } else {
  7227. limit = &intel_limits_i9xx_sdvo;
  7228. }
  7229. if (!crtc_state->clock_set &&
  7230. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7231. refclk, NULL, &crtc_state->dpll)) {
  7232. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7233. return -EINVAL;
  7234. }
  7235. i9xx_compute_dpll(crtc, crtc_state, NULL);
  7236. return 0;
  7237. }
  7238. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  7239. struct intel_crtc_state *crtc_state)
  7240. {
  7241. int refclk = 100000;
  7242. const struct intel_limit *limit = &intel_limits_chv;
  7243. memset(&crtc_state->dpll_hw_state, 0,
  7244. sizeof(crtc_state->dpll_hw_state));
  7245. if (!crtc_state->clock_set &&
  7246. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7247. refclk, NULL, &crtc_state->dpll)) {
  7248. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7249. return -EINVAL;
  7250. }
  7251. chv_compute_dpll(crtc, crtc_state);
  7252. return 0;
  7253. }
  7254. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  7255. struct intel_crtc_state *crtc_state)
  7256. {
  7257. int refclk = 100000;
  7258. const struct intel_limit *limit = &intel_limits_vlv;
  7259. memset(&crtc_state->dpll_hw_state, 0,
  7260. sizeof(crtc_state->dpll_hw_state));
  7261. if (!crtc_state->clock_set &&
  7262. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7263. refclk, NULL, &crtc_state->dpll)) {
  7264. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7265. return -EINVAL;
  7266. }
  7267. vlv_compute_dpll(crtc, crtc_state);
  7268. return 0;
  7269. }
  7270. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  7271. struct intel_crtc_state *pipe_config)
  7272. {
  7273. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7274. uint32_t tmp;
  7275. if (INTEL_GEN(dev_priv) <= 3 &&
  7276. (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
  7277. return;
  7278. tmp = I915_READ(PFIT_CONTROL);
  7279. if (!(tmp & PFIT_ENABLE))
  7280. return;
  7281. /* Check whether the pfit is attached to our pipe. */
  7282. if (INTEL_GEN(dev_priv) < 4) {
  7283. if (crtc->pipe != PIPE_B)
  7284. return;
  7285. } else {
  7286. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  7287. return;
  7288. }
  7289. pipe_config->gmch_pfit.control = tmp;
  7290. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  7291. }
  7292. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  7293. struct intel_crtc_state *pipe_config)
  7294. {
  7295. struct drm_device *dev = crtc->base.dev;
  7296. struct drm_i915_private *dev_priv = to_i915(dev);
  7297. int pipe = pipe_config->cpu_transcoder;
  7298. struct dpll clock;
  7299. u32 mdiv;
  7300. int refclk = 100000;
  7301. /* In case of DSI, DPLL will not be used */
  7302. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  7303. return;
  7304. mutex_lock(&dev_priv->sb_lock);
  7305. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  7306. mutex_unlock(&dev_priv->sb_lock);
  7307. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  7308. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  7309. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  7310. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  7311. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  7312. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  7313. }
  7314. static void
  7315. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  7316. struct intel_initial_plane_config *plane_config)
  7317. {
  7318. struct drm_device *dev = crtc->base.dev;
  7319. struct drm_i915_private *dev_priv = to_i915(dev);
  7320. u32 val, base, offset;
  7321. int pipe = crtc->pipe, plane = crtc->plane;
  7322. int fourcc, pixel_format;
  7323. unsigned int aligned_height;
  7324. struct drm_framebuffer *fb;
  7325. struct intel_framebuffer *intel_fb;
  7326. val = I915_READ(DSPCNTR(plane));
  7327. if (!(val & DISPLAY_PLANE_ENABLE))
  7328. return;
  7329. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7330. if (!intel_fb) {
  7331. DRM_DEBUG_KMS("failed to alloc fb\n");
  7332. return;
  7333. }
  7334. fb = &intel_fb->base;
  7335. fb->dev = dev;
  7336. if (INTEL_GEN(dev_priv) >= 4) {
  7337. if (val & DISPPLANE_TILED) {
  7338. plane_config->tiling = I915_TILING_X;
  7339. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7340. }
  7341. }
  7342. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7343. fourcc = i9xx_format_to_fourcc(pixel_format);
  7344. fb->format = drm_format_info(fourcc);
  7345. if (INTEL_GEN(dev_priv) >= 4) {
  7346. if (plane_config->tiling)
  7347. offset = I915_READ(DSPTILEOFF(plane));
  7348. else
  7349. offset = I915_READ(DSPLINOFF(plane));
  7350. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  7351. } else {
  7352. base = I915_READ(DSPADDR(plane));
  7353. }
  7354. plane_config->base = base;
  7355. val = I915_READ(PIPESRC(pipe));
  7356. fb->width = ((val >> 16) & 0xfff) + 1;
  7357. fb->height = ((val >> 0) & 0xfff) + 1;
  7358. val = I915_READ(DSPSTRIDE(pipe));
  7359. fb->pitches[0] = val & 0xffffffc0;
  7360. aligned_height = intel_fb_align_height(dev, fb->height,
  7361. fb->format->format,
  7362. fb->modifier);
  7363. plane_config->size = fb->pitches[0] * aligned_height;
  7364. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7365. pipe_name(pipe), plane, fb->width, fb->height,
  7366. fb->format->cpp[0] * 8, base, fb->pitches[0],
  7367. plane_config->size);
  7368. plane_config->fb = intel_fb;
  7369. }
  7370. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  7371. struct intel_crtc_state *pipe_config)
  7372. {
  7373. struct drm_device *dev = crtc->base.dev;
  7374. struct drm_i915_private *dev_priv = to_i915(dev);
  7375. int pipe = pipe_config->cpu_transcoder;
  7376. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  7377. struct dpll clock;
  7378. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  7379. int refclk = 100000;
  7380. /* In case of DSI, DPLL will not be used */
  7381. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  7382. return;
  7383. mutex_lock(&dev_priv->sb_lock);
  7384. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  7385. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  7386. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  7387. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  7388. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  7389. mutex_unlock(&dev_priv->sb_lock);
  7390. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  7391. clock.m2 = (pll_dw0 & 0xff) << 22;
  7392. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  7393. clock.m2 |= pll_dw2 & 0x3fffff;
  7394. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  7395. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  7396. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  7397. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  7398. }
  7399. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  7400. struct intel_crtc_state *pipe_config)
  7401. {
  7402. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7403. enum intel_display_power_domain power_domain;
  7404. uint32_t tmp;
  7405. bool ret;
  7406. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7407. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7408. return false;
  7409. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7410. pipe_config->shared_dpll = NULL;
  7411. ret = false;
  7412. tmp = I915_READ(PIPECONF(crtc->pipe));
  7413. if (!(tmp & PIPECONF_ENABLE))
  7414. goto out;
  7415. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  7416. IS_CHERRYVIEW(dev_priv)) {
  7417. switch (tmp & PIPECONF_BPC_MASK) {
  7418. case PIPECONF_6BPC:
  7419. pipe_config->pipe_bpp = 18;
  7420. break;
  7421. case PIPECONF_8BPC:
  7422. pipe_config->pipe_bpp = 24;
  7423. break;
  7424. case PIPECONF_10BPC:
  7425. pipe_config->pipe_bpp = 30;
  7426. break;
  7427. default:
  7428. break;
  7429. }
  7430. }
  7431. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  7432. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  7433. pipe_config->limited_color_range = true;
  7434. if (INTEL_GEN(dev_priv) < 4)
  7435. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  7436. intel_get_pipe_timings(crtc, pipe_config);
  7437. intel_get_pipe_src_size(crtc, pipe_config);
  7438. i9xx_get_pfit_config(crtc, pipe_config);
  7439. if (INTEL_GEN(dev_priv) >= 4) {
  7440. /* No way to read it out on pipes B and C */
  7441. if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
  7442. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  7443. else
  7444. tmp = I915_READ(DPLL_MD(crtc->pipe));
  7445. pipe_config->pixel_multiplier =
  7446. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  7447. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  7448. pipe_config->dpll_hw_state.dpll_md = tmp;
  7449. } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  7450. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  7451. tmp = I915_READ(DPLL(crtc->pipe));
  7452. pipe_config->pixel_multiplier =
  7453. ((tmp & SDVO_MULTIPLIER_MASK)
  7454. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  7455. } else {
  7456. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  7457. * port and will be fixed up in the encoder->get_config
  7458. * function. */
  7459. pipe_config->pixel_multiplier = 1;
  7460. }
  7461. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  7462. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  7463. /*
  7464. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  7465. * on 830. Filter it out here so that we don't
  7466. * report errors due to that.
  7467. */
  7468. if (IS_I830(dev_priv))
  7469. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  7470. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  7471. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  7472. } else {
  7473. /* Mask out read-only status bits. */
  7474. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  7475. DPLL_PORTC_READY_MASK |
  7476. DPLL_PORTB_READY_MASK);
  7477. }
  7478. if (IS_CHERRYVIEW(dev_priv))
  7479. chv_crtc_clock_get(crtc, pipe_config);
  7480. else if (IS_VALLEYVIEW(dev_priv))
  7481. vlv_crtc_clock_get(crtc, pipe_config);
  7482. else
  7483. i9xx_crtc_clock_get(crtc, pipe_config);
  7484. /*
  7485. * Normally the dotclock is filled in by the encoder .get_config()
  7486. * but in case the pipe is enabled w/o any ports we need a sane
  7487. * default.
  7488. */
  7489. pipe_config->base.adjusted_mode.crtc_clock =
  7490. pipe_config->port_clock / pipe_config->pixel_multiplier;
  7491. ret = true;
  7492. out:
  7493. intel_display_power_put(dev_priv, power_domain);
  7494. return ret;
  7495. }
  7496. static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
  7497. {
  7498. struct intel_encoder *encoder;
  7499. int i;
  7500. u32 val, final;
  7501. bool has_lvds = false;
  7502. bool has_cpu_edp = false;
  7503. bool has_panel = false;
  7504. bool has_ck505 = false;
  7505. bool can_ssc = false;
  7506. bool using_ssc_source = false;
  7507. /* We need to take the global config into account */
  7508. for_each_intel_encoder(&dev_priv->drm, encoder) {
  7509. switch (encoder->type) {
  7510. case INTEL_OUTPUT_LVDS:
  7511. has_panel = true;
  7512. has_lvds = true;
  7513. break;
  7514. case INTEL_OUTPUT_EDP:
  7515. has_panel = true;
  7516. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  7517. has_cpu_edp = true;
  7518. break;
  7519. default:
  7520. break;
  7521. }
  7522. }
  7523. if (HAS_PCH_IBX(dev_priv)) {
  7524. has_ck505 = dev_priv->vbt.display_clock_mode;
  7525. can_ssc = has_ck505;
  7526. } else {
  7527. has_ck505 = false;
  7528. can_ssc = true;
  7529. }
  7530. /* Check if any DPLLs are using the SSC source */
  7531. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  7532. u32 temp = I915_READ(PCH_DPLL(i));
  7533. if (!(temp & DPLL_VCO_ENABLE))
  7534. continue;
  7535. if ((temp & PLL_REF_INPUT_MASK) ==
  7536. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  7537. using_ssc_source = true;
  7538. break;
  7539. }
  7540. }
  7541. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  7542. has_panel, has_lvds, has_ck505, using_ssc_source);
  7543. /* Ironlake: try to setup display ref clock before DPLL
  7544. * enabling. This is only under driver's control after
  7545. * PCH B stepping, previous chipset stepping should be
  7546. * ignoring this setting.
  7547. */
  7548. val = I915_READ(PCH_DREF_CONTROL);
  7549. /* As we must carefully and slowly disable/enable each source in turn,
  7550. * compute the final state we want first and check if we need to
  7551. * make any changes at all.
  7552. */
  7553. final = val;
  7554. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  7555. if (has_ck505)
  7556. final |= DREF_NONSPREAD_CK505_ENABLE;
  7557. else
  7558. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  7559. final &= ~DREF_SSC_SOURCE_MASK;
  7560. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7561. final &= ~DREF_SSC1_ENABLE;
  7562. if (has_panel) {
  7563. final |= DREF_SSC_SOURCE_ENABLE;
  7564. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7565. final |= DREF_SSC1_ENABLE;
  7566. if (has_cpu_edp) {
  7567. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7568. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7569. else
  7570. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7571. } else
  7572. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7573. } else if (using_ssc_source) {
  7574. final |= DREF_SSC_SOURCE_ENABLE;
  7575. final |= DREF_SSC1_ENABLE;
  7576. }
  7577. if (final == val)
  7578. return;
  7579. /* Always enable nonspread source */
  7580. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  7581. if (has_ck505)
  7582. val |= DREF_NONSPREAD_CK505_ENABLE;
  7583. else
  7584. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  7585. if (has_panel) {
  7586. val &= ~DREF_SSC_SOURCE_MASK;
  7587. val |= DREF_SSC_SOURCE_ENABLE;
  7588. /* SSC must be turned on before enabling the CPU output */
  7589. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7590. DRM_DEBUG_KMS("Using SSC on panel\n");
  7591. val |= DREF_SSC1_ENABLE;
  7592. } else
  7593. val &= ~DREF_SSC1_ENABLE;
  7594. /* Get SSC going before enabling the outputs */
  7595. I915_WRITE(PCH_DREF_CONTROL, val);
  7596. POSTING_READ(PCH_DREF_CONTROL);
  7597. udelay(200);
  7598. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7599. /* Enable CPU source on CPU attached eDP */
  7600. if (has_cpu_edp) {
  7601. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7602. DRM_DEBUG_KMS("Using SSC on eDP\n");
  7603. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7604. } else
  7605. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7606. } else
  7607. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7608. I915_WRITE(PCH_DREF_CONTROL, val);
  7609. POSTING_READ(PCH_DREF_CONTROL);
  7610. udelay(200);
  7611. } else {
  7612. DRM_DEBUG_KMS("Disabling CPU source output\n");
  7613. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7614. /* Turn off CPU output */
  7615. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7616. I915_WRITE(PCH_DREF_CONTROL, val);
  7617. POSTING_READ(PCH_DREF_CONTROL);
  7618. udelay(200);
  7619. if (!using_ssc_source) {
  7620. DRM_DEBUG_KMS("Disabling SSC source\n");
  7621. /* Turn off the SSC source */
  7622. val &= ~DREF_SSC_SOURCE_MASK;
  7623. val |= DREF_SSC_SOURCE_DISABLE;
  7624. /* Turn off SSC1 */
  7625. val &= ~DREF_SSC1_ENABLE;
  7626. I915_WRITE(PCH_DREF_CONTROL, val);
  7627. POSTING_READ(PCH_DREF_CONTROL);
  7628. udelay(200);
  7629. }
  7630. }
  7631. BUG_ON(val != final);
  7632. }
  7633. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  7634. {
  7635. uint32_t tmp;
  7636. tmp = I915_READ(SOUTH_CHICKEN2);
  7637. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  7638. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7639. if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
  7640. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  7641. DRM_ERROR("FDI mPHY reset assert timeout\n");
  7642. tmp = I915_READ(SOUTH_CHICKEN2);
  7643. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  7644. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7645. if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
  7646. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  7647. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  7648. }
  7649. /* WaMPhyProgramming:hsw */
  7650. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  7651. {
  7652. uint32_t tmp;
  7653. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  7654. tmp &= ~(0xFF << 24);
  7655. tmp |= (0x12 << 24);
  7656. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  7657. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  7658. tmp |= (1 << 11);
  7659. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  7660. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  7661. tmp |= (1 << 11);
  7662. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  7663. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  7664. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7665. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7666. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7667. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7668. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7669. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7670. tmp &= ~(7 << 13);
  7671. tmp |= (5 << 13);
  7672. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7673. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7674. tmp &= ~(7 << 13);
  7675. tmp |= (5 << 13);
  7676. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7677. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7678. tmp &= ~0xFF;
  7679. tmp |= 0x1C;
  7680. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7681. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7682. tmp &= ~0xFF;
  7683. tmp |= 0x1C;
  7684. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7685. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7686. tmp &= ~(0xFF << 16);
  7687. tmp |= (0x1C << 16);
  7688. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7689. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7690. tmp &= ~(0xFF << 16);
  7691. tmp |= (0x1C << 16);
  7692. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7693. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7694. tmp |= (1 << 27);
  7695. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7696. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7697. tmp |= (1 << 27);
  7698. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7699. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7700. tmp &= ~(0xF << 28);
  7701. tmp |= (4 << 28);
  7702. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7703. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7704. tmp &= ~(0xF << 28);
  7705. tmp |= (4 << 28);
  7706. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7707. }
  7708. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7709. * Programming" based on the parameters passed:
  7710. * - Sequence to enable CLKOUT_DP
  7711. * - Sequence to enable CLKOUT_DP without spread
  7712. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7713. */
  7714. static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
  7715. bool with_spread, bool with_fdi)
  7716. {
  7717. uint32_t reg, tmp;
  7718. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7719. with_spread = true;
  7720. if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
  7721. with_fdi, "LP PCH doesn't have FDI\n"))
  7722. with_fdi = false;
  7723. mutex_lock(&dev_priv->sb_lock);
  7724. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7725. tmp &= ~SBI_SSCCTL_DISABLE;
  7726. tmp |= SBI_SSCCTL_PATHALT;
  7727. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7728. udelay(24);
  7729. if (with_spread) {
  7730. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7731. tmp &= ~SBI_SSCCTL_PATHALT;
  7732. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7733. if (with_fdi) {
  7734. lpt_reset_fdi_mphy(dev_priv);
  7735. lpt_program_fdi_mphy(dev_priv);
  7736. }
  7737. }
  7738. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  7739. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7740. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7741. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7742. mutex_unlock(&dev_priv->sb_lock);
  7743. }
  7744. /* Sequence to disable CLKOUT_DP */
  7745. static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
  7746. {
  7747. uint32_t reg, tmp;
  7748. mutex_lock(&dev_priv->sb_lock);
  7749. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  7750. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7751. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7752. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7753. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7754. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7755. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7756. tmp |= SBI_SSCCTL_PATHALT;
  7757. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7758. udelay(32);
  7759. }
  7760. tmp |= SBI_SSCCTL_DISABLE;
  7761. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7762. }
  7763. mutex_unlock(&dev_priv->sb_lock);
  7764. }
  7765. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  7766. static const uint16_t sscdivintphase[] = {
  7767. [BEND_IDX( 50)] = 0x3B23,
  7768. [BEND_IDX( 45)] = 0x3B23,
  7769. [BEND_IDX( 40)] = 0x3C23,
  7770. [BEND_IDX( 35)] = 0x3C23,
  7771. [BEND_IDX( 30)] = 0x3D23,
  7772. [BEND_IDX( 25)] = 0x3D23,
  7773. [BEND_IDX( 20)] = 0x3E23,
  7774. [BEND_IDX( 15)] = 0x3E23,
  7775. [BEND_IDX( 10)] = 0x3F23,
  7776. [BEND_IDX( 5)] = 0x3F23,
  7777. [BEND_IDX( 0)] = 0x0025,
  7778. [BEND_IDX( -5)] = 0x0025,
  7779. [BEND_IDX(-10)] = 0x0125,
  7780. [BEND_IDX(-15)] = 0x0125,
  7781. [BEND_IDX(-20)] = 0x0225,
  7782. [BEND_IDX(-25)] = 0x0225,
  7783. [BEND_IDX(-30)] = 0x0325,
  7784. [BEND_IDX(-35)] = 0x0325,
  7785. [BEND_IDX(-40)] = 0x0425,
  7786. [BEND_IDX(-45)] = 0x0425,
  7787. [BEND_IDX(-50)] = 0x0525,
  7788. };
  7789. /*
  7790. * Bend CLKOUT_DP
  7791. * steps -50 to 50 inclusive, in steps of 5
  7792. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  7793. * change in clock period = -(steps / 10) * 5.787 ps
  7794. */
  7795. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  7796. {
  7797. uint32_t tmp;
  7798. int idx = BEND_IDX(steps);
  7799. if (WARN_ON(steps % 5 != 0))
  7800. return;
  7801. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  7802. return;
  7803. mutex_lock(&dev_priv->sb_lock);
  7804. if (steps % 10 != 0)
  7805. tmp = 0xAAAAAAAB;
  7806. else
  7807. tmp = 0x00000000;
  7808. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  7809. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  7810. tmp &= 0xffff0000;
  7811. tmp |= sscdivintphase[idx];
  7812. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  7813. mutex_unlock(&dev_priv->sb_lock);
  7814. }
  7815. #undef BEND_IDX
  7816. static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
  7817. {
  7818. struct intel_encoder *encoder;
  7819. bool has_vga = false;
  7820. for_each_intel_encoder(&dev_priv->drm, encoder) {
  7821. switch (encoder->type) {
  7822. case INTEL_OUTPUT_ANALOG:
  7823. has_vga = true;
  7824. break;
  7825. default:
  7826. break;
  7827. }
  7828. }
  7829. if (has_vga) {
  7830. lpt_bend_clkout_dp(dev_priv, 0);
  7831. lpt_enable_clkout_dp(dev_priv, true, true);
  7832. } else {
  7833. lpt_disable_clkout_dp(dev_priv);
  7834. }
  7835. }
  7836. /*
  7837. * Initialize reference clocks when the driver loads
  7838. */
  7839. void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
  7840. {
  7841. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
  7842. ironlake_init_pch_refclk(dev_priv);
  7843. else if (HAS_PCH_LPT(dev_priv))
  7844. lpt_init_pch_refclk(dev_priv);
  7845. }
  7846. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7847. {
  7848. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7849. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7850. int pipe = intel_crtc->pipe;
  7851. uint32_t val;
  7852. val = 0;
  7853. switch (intel_crtc->config->pipe_bpp) {
  7854. case 18:
  7855. val |= PIPECONF_6BPC;
  7856. break;
  7857. case 24:
  7858. val |= PIPECONF_8BPC;
  7859. break;
  7860. case 30:
  7861. val |= PIPECONF_10BPC;
  7862. break;
  7863. case 36:
  7864. val |= PIPECONF_12BPC;
  7865. break;
  7866. default:
  7867. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7868. BUG();
  7869. }
  7870. if (intel_crtc->config->dither)
  7871. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7872. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7873. val |= PIPECONF_INTERLACED_ILK;
  7874. else
  7875. val |= PIPECONF_PROGRESSIVE;
  7876. if (intel_crtc->config->limited_color_range)
  7877. val |= PIPECONF_COLOR_RANGE_SELECT;
  7878. I915_WRITE(PIPECONF(pipe), val);
  7879. POSTING_READ(PIPECONF(pipe));
  7880. }
  7881. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7882. {
  7883. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7884. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7885. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7886. u32 val = 0;
  7887. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  7888. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7889. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7890. val |= PIPECONF_INTERLACED_ILK;
  7891. else
  7892. val |= PIPECONF_PROGRESSIVE;
  7893. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7894. POSTING_READ(PIPECONF(cpu_transcoder));
  7895. }
  7896. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  7897. {
  7898. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7899. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7900. if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
  7901. u32 val = 0;
  7902. switch (intel_crtc->config->pipe_bpp) {
  7903. case 18:
  7904. val |= PIPEMISC_DITHER_6_BPC;
  7905. break;
  7906. case 24:
  7907. val |= PIPEMISC_DITHER_8_BPC;
  7908. break;
  7909. case 30:
  7910. val |= PIPEMISC_DITHER_10_BPC;
  7911. break;
  7912. case 36:
  7913. val |= PIPEMISC_DITHER_12_BPC;
  7914. break;
  7915. default:
  7916. /* Case prevented by pipe_config_set_bpp. */
  7917. BUG();
  7918. }
  7919. if (intel_crtc->config->dither)
  7920. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7921. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  7922. }
  7923. }
  7924. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7925. {
  7926. /*
  7927. * Account for spread spectrum to avoid
  7928. * oversubscribing the link. Max center spread
  7929. * is 2.5%; use 5% for safety's sake.
  7930. */
  7931. u32 bps = target_clock * bpp * 21 / 20;
  7932. return DIV_ROUND_UP(bps, link_bw * 8);
  7933. }
  7934. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7935. {
  7936. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7937. }
  7938. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7939. struct intel_crtc_state *crtc_state,
  7940. struct dpll *reduced_clock)
  7941. {
  7942. struct drm_crtc *crtc = &intel_crtc->base;
  7943. struct drm_device *dev = crtc->dev;
  7944. struct drm_i915_private *dev_priv = to_i915(dev);
  7945. u32 dpll, fp, fp2;
  7946. int factor;
  7947. /* Enable autotuning of the PLL clock (if permissible) */
  7948. factor = 21;
  7949. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7950. if ((intel_panel_use_ssc(dev_priv) &&
  7951. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7952. (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
  7953. factor = 25;
  7954. } else if (crtc_state->sdvo_tv_clock)
  7955. factor = 20;
  7956. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7957. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7958. fp |= FP_CB_TUNE;
  7959. if (reduced_clock) {
  7960. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  7961. if (reduced_clock->m < factor * reduced_clock->n)
  7962. fp2 |= FP_CB_TUNE;
  7963. } else {
  7964. fp2 = fp;
  7965. }
  7966. dpll = 0;
  7967. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  7968. dpll |= DPLLB_MODE_LVDS;
  7969. else
  7970. dpll |= DPLLB_MODE_DAC_SERIAL;
  7971. dpll |= (crtc_state->pixel_multiplier - 1)
  7972. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7973. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  7974. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  7975. dpll |= DPLL_SDVO_HIGH_SPEED;
  7976. if (intel_crtc_has_dp_encoder(crtc_state))
  7977. dpll |= DPLL_SDVO_HIGH_SPEED;
  7978. /*
  7979. * The high speed IO clock is only really required for
  7980. * SDVO/HDMI/DP, but we also enable it for CRT to make it
  7981. * possible to share the DPLL between CRT and HDMI. Enabling
  7982. * the clock needlessly does no real harm, except use up a
  7983. * bit of power potentially.
  7984. *
  7985. * We'll limit this to IVB with 3 pipes, since it has only two
  7986. * DPLLs and so DPLL sharing is the only way to get three pipes
  7987. * driving PCH ports at the same time. On SNB we could do this,
  7988. * and potentially avoid enabling the second DPLL, but it's not
  7989. * clear if it''s a win or loss power wise. No point in doing
  7990. * this on ILK at all since it has a fixed DPLL<->pipe mapping.
  7991. */
  7992. if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
  7993. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
  7994. dpll |= DPLL_SDVO_HIGH_SPEED;
  7995. /* compute bitmask from p1 value */
  7996. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7997. /* also FPA1 */
  7998. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  7999. switch (crtc_state->dpll.p2) {
  8000. case 5:
  8001. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  8002. break;
  8003. case 7:
  8004. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  8005. break;
  8006. case 10:
  8007. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  8008. break;
  8009. case 14:
  8010. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  8011. break;
  8012. }
  8013. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  8014. intel_panel_use_ssc(dev_priv))
  8015. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  8016. else
  8017. dpll |= PLL_REF_INPUT_DREFCLK;
  8018. dpll |= DPLL_VCO_ENABLE;
  8019. crtc_state->dpll_hw_state.dpll = dpll;
  8020. crtc_state->dpll_hw_state.fp0 = fp;
  8021. crtc_state->dpll_hw_state.fp1 = fp2;
  8022. }
  8023. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  8024. struct intel_crtc_state *crtc_state)
  8025. {
  8026. struct drm_device *dev = crtc->base.dev;
  8027. struct drm_i915_private *dev_priv = to_i915(dev);
  8028. struct dpll reduced_clock;
  8029. bool has_reduced_clock = false;
  8030. struct intel_shared_dpll *pll;
  8031. const struct intel_limit *limit;
  8032. int refclk = 120000;
  8033. memset(&crtc_state->dpll_hw_state, 0,
  8034. sizeof(crtc_state->dpll_hw_state));
  8035. crtc->lowfreq_avail = false;
  8036. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  8037. if (!crtc_state->has_pch_encoder)
  8038. return 0;
  8039. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  8040. if (intel_panel_use_ssc(dev_priv)) {
  8041. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  8042. dev_priv->vbt.lvds_ssc_freq);
  8043. refclk = dev_priv->vbt.lvds_ssc_freq;
  8044. }
  8045. if (intel_is_dual_link_lvds(dev)) {
  8046. if (refclk == 100000)
  8047. limit = &intel_limits_ironlake_dual_lvds_100m;
  8048. else
  8049. limit = &intel_limits_ironlake_dual_lvds;
  8050. } else {
  8051. if (refclk == 100000)
  8052. limit = &intel_limits_ironlake_single_lvds_100m;
  8053. else
  8054. limit = &intel_limits_ironlake_single_lvds;
  8055. }
  8056. } else {
  8057. limit = &intel_limits_ironlake_dac;
  8058. }
  8059. if (!crtc_state->clock_set &&
  8060. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  8061. refclk, NULL, &crtc_state->dpll)) {
  8062. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  8063. return -EINVAL;
  8064. }
  8065. ironlake_compute_dpll(crtc, crtc_state,
  8066. has_reduced_clock ? &reduced_clock : NULL);
  8067. pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
  8068. if (pll == NULL) {
  8069. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  8070. pipe_name(crtc->pipe));
  8071. return -EINVAL;
  8072. }
  8073. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  8074. has_reduced_clock)
  8075. crtc->lowfreq_avail = true;
  8076. return 0;
  8077. }
  8078. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  8079. struct intel_link_m_n *m_n)
  8080. {
  8081. struct drm_device *dev = crtc->base.dev;
  8082. struct drm_i915_private *dev_priv = to_i915(dev);
  8083. enum pipe pipe = crtc->pipe;
  8084. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  8085. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  8086. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  8087. & ~TU_SIZE_MASK;
  8088. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  8089. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  8090. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8091. }
  8092. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  8093. enum transcoder transcoder,
  8094. struct intel_link_m_n *m_n,
  8095. struct intel_link_m_n *m2_n2)
  8096. {
  8097. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8098. enum pipe pipe = crtc->pipe;
  8099. if (INTEL_GEN(dev_priv) >= 5) {
  8100. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  8101. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  8102. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  8103. & ~TU_SIZE_MASK;
  8104. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  8105. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  8106. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8107. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  8108. * gen < 8) and if DRRS is supported (to make sure the
  8109. * registers are not unnecessarily read).
  8110. */
  8111. if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
  8112. crtc->config->has_drrs) {
  8113. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  8114. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  8115. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  8116. & ~TU_SIZE_MASK;
  8117. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  8118. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  8119. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8120. }
  8121. } else {
  8122. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  8123. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  8124. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  8125. & ~TU_SIZE_MASK;
  8126. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  8127. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  8128. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8129. }
  8130. }
  8131. void intel_dp_get_m_n(struct intel_crtc *crtc,
  8132. struct intel_crtc_state *pipe_config)
  8133. {
  8134. if (pipe_config->has_pch_encoder)
  8135. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  8136. else
  8137. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  8138. &pipe_config->dp_m_n,
  8139. &pipe_config->dp_m2_n2);
  8140. }
  8141. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  8142. struct intel_crtc_state *pipe_config)
  8143. {
  8144. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  8145. &pipe_config->fdi_m_n, NULL);
  8146. }
  8147. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  8148. struct intel_crtc_state *pipe_config)
  8149. {
  8150. struct drm_device *dev = crtc->base.dev;
  8151. struct drm_i915_private *dev_priv = to_i915(dev);
  8152. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  8153. uint32_t ps_ctrl = 0;
  8154. int id = -1;
  8155. int i;
  8156. /* find scaler attached to this pipe */
  8157. for (i = 0; i < crtc->num_scalers; i++) {
  8158. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  8159. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  8160. id = i;
  8161. pipe_config->pch_pfit.enabled = true;
  8162. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  8163. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  8164. break;
  8165. }
  8166. }
  8167. scaler_state->scaler_id = id;
  8168. if (id >= 0) {
  8169. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  8170. } else {
  8171. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8172. }
  8173. }
  8174. static void
  8175. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  8176. struct intel_initial_plane_config *plane_config)
  8177. {
  8178. struct drm_device *dev = crtc->base.dev;
  8179. struct drm_i915_private *dev_priv = to_i915(dev);
  8180. u32 val, base, offset, stride_mult, tiling;
  8181. int pipe = crtc->pipe;
  8182. int fourcc, pixel_format;
  8183. unsigned int aligned_height;
  8184. struct drm_framebuffer *fb;
  8185. struct intel_framebuffer *intel_fb;
  8186. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8187. if (!intel_fb) {
  8188. DRM_DEBUG_KMS("failed to alloc fb\n");
  8189. return;
  8190. }
  8191. fb = &intel_fb->base;
  8192. fb->dev = dev;
  8193. val = I915_READ(PLANE_CTL(pipe, 0));
  8194. if (!(val & PLANE_CTL_ENABLE))
  8195. goto error;
  8196. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  8197. fourcc = skl_format_to_fourcc(pixel_format,
  8198. val & PLANE_CTL_ORDER_RGBX,
  8199. val & PLANE_CTL_ALPHA_MASK);
  8200. fb->format = drm_format_info(fourcc);
  8201. tiling = val & PLANE_CTL_TILED_MASK;
  8202. switch (tiling) {
  8203. case PLANE_CTL_TILED_LINEAR:
  8204. fb->modifier = DRM_FORMAT_MOD_NONE;
  8205. break;
  8206. case PLANE_CTL_TILED_X:
  8207. plane_config->tiling = I915_TILING_X;
  8208. fb->modifier = I915_FORMAT_MOD_X_TILED;
  8209. break;
  8210. case PLANE_CTL_TILED_Y:
  8211. fb->modifier = I915_FORMAT_MOD_Y_TILED;
  8212. break;
  8213. case PLANE_CTL_TILED_YF:
  8214. fb->modifier = I915_FORMAT_MOD_Yf_TILED;
  8215. break;
  8216. default:
  8217. MISSING_CASE(tiling);
  8218. goto error;
  8219. }
  8220. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  8221. plane_config->base = base;
  8222. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  8223. val = I915_READ(PLANE_SIZE(pipe, 0));
  8224. fb->height = ((val >> 16) & 0xfff) + 1;
  8225. fb->width = ((val >> 0) & 0x1fff) + 1;
  8226. val = I915_READ(PLANE_STRIDE(pipe, 0));
  8227. stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier,
  8228. fb->format->format);
  8229. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  8230. aligned_height = intel_fb_align_height(dev, fb->height,
  8231. fb->format->format,
  8232. fb->modifier);
  8233. plane_config->size = fb->pitches[0] * aligned_height;
  8234. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  8235. pipe_name(pipe), fb->width, fb->height,
  8236. fb->format->cpp[0] * 8, base, fb->pitches[0],
  8237. plane_config->size);
  8238. plane_config->fb = intel_fb;
  8239. return;
  8240. error:
  8241. kfree(intel_fb);
  8242. }
  8243. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  8244. struct intel_crtc_state *pipe_config)
  8245. {
  8246. struct drm_device *dev = crtc->base.dev;
  8247. struct drm_i915_private *dev_priv = to_i915(dev);
  8248. uint32_t tmp;
  8249. tmp = I915_READ(PF_CTL(crtc->pipe));
  8250. if (tmp & PF_ENABLE) {
  8251. pipe_config->pch_pfit.enabled = true;
  8252. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  8253. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  8254. /* We currently do not free assignements of panel fitters on
  8255. * ivb/hsw (since we don't use the higher upscaling modes which
  8256. * differentiates them) so just WARN about this case for now. */
  8257. if (IS_GEN7(dev_priv)) {
  8258. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  8259. PF_PIPE_SEL_IVB(crtc->pipe));
  8260. }
  8261. }
  8262. }
  8263. static void
  8264. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  8265. struct intel_initial_plane_config *plane_config)
  8266. {
  8267. struct drm_device *dev = crtc->base.dev;
  8268. struct drm_i915_private *dev_priv = to_i915(dev);
  8269. u32 val, base, offset;
  8270. int pipe = crtc->pipe;
  8271. int fourcc, pixel_format;
  8272. unsigned int aligned_height;
  8273. struct drm_framebuffer *fb;
  8274. struct intel_framebuffer *intel_fb;
  8275. val = I915_READ(DSPCNTR(pipe));
  8276. if (!(val & DISPLAY_PLANE_ENABLE))
  8277. return;
  8278. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8279. if (!intel_fb) {
  8280. DRM_DEBUG_KMS("failed to alloc fb\n");
  8281. return;
  8282. }
  8283. fb = &intel_fb->base;
  8284. fb->dev = dev;
  8285. if (INTEL_GEN(dev_priv) >= 4) {
  8286. if (val & DISPPLANE_TILED) {
  8287. plane_config->tiling = I915_TILING_X;
  8288. fb->modifier = I915_FORMAT_MOD_X_TILED;
  8289. }
  8290. }
  8291. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  8292. fourcc = i9xx_format_to_fourcc(pixel_format);
  8293. fb->format = drm_format_info(fourcc);
  8294. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  8295. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  8296. offset = I915_READ(DSPOFFSET(pipe));
  8297. } else {
  8298. if (plane_config->tiling)
  8299. offset = I915_READ(DSPTILEOFF(pipe));
  8300. else
  8301. offset = I915_READ(DSPLINOFF(pipe));
  8302. }
  8303. plane_config->base = base;
  8304. val = I915_READ(PIPESRC(pipe));
  8305. fb->width = ((val >> 16) & 0xfff) + 1;
  8306. fb->height = ((val >> 0) & 0xfff) + 1;
  8307. val = I915_READ(DSPSTRIDE(pipe));
  8308. fb->pitches[0] = val & 0xffffffc0;
  8309. aligned_height = intel_fb_align_height(dev, fb->height,
  8310. fb->format->format,
  8311. fb->modifier);
  8312. plane_config->size = fb->pitches[0] * aligned_height;
  8313. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  8314. pipe_name(pipe), fb->width, fb->height,
  8315. fb->format->cpp[0] * 8, base, fb->pitches[0],
  8316. plane_config->size);
  8317. plane_config->fb = intel_fb;
  8318. }
  8319. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  8320. struct intel_crtc_state *pipe_config)
  8321. {
  8322. struct drm_device *dev = crtc->base.dev;
  8323. struct drm_i915_private *dev_priv = to_i915(dev);
  8324. enum intel_display_power_domain power_domain;
  8325. uint32_t tmp;
  8326. bool ret;
  8327. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  8328. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8329. return false;
  8330. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8331. pipe_config->shared_dpll = NULL;
  8332. ret = false;
  8333. tmp = I915_READ(PIPECONF(crtc->pipe));
  8334. if (!(tmp & PIPECONF_ENABLE))
  8335. goto out;
  8336. switch (tmp & PIPECONF_BPC_MASK) {
  8337. case PIPECONF_6BPC:
  8338. pipe_config->pipe_bpp = 18;
  8339. break;
  8340. case PIPECONF_8BPC:
  8341. pipe_config->pipe_bpp = 24;
  8342. break;
  8343. case PIPECONF_10BPC:
  8344. pipe_config->pipe_bpp = 30;
  8345. break;
  8346. case PIPECONF_12BPC:
  8347. pipe_config->pipe_bpp = 36;
  8348. break;
  8349. default:
  8350. break;
  8351. }
  8352. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  8353. pipe_config->limited_color_range = true;
  8354. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  8355. struct intel_shared_dpll *pll;
  8356. enum intel_dpll_id pll_id;
  8357. pipe_config->has_pch_encoder = true;
  8358. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  8359. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8360. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8361. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8362. if (HAS_PCH_IBX(dev_priv)) {
  8363. /*
  8364. * The pipe->pch transcoder and pch transcoder->pll
  8365. * mapping is fixed.
  8366. */
  8367. pll_id = (enum intel_dpll_id) crtc->pipe;
  8368. } else {
  8369. tmp = I915_READ(PCH_DPLL_SEL);
  8370. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  8371. pll_id = DPLL_ID_PCH_PLL_B;
  8372. else
  8373. pll_id= DPLL_ID_PCH_PLL_A;
  8374. }
  8375. pipe_config->shared_dpll =
  8376. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  8377. pll = pipe_config->shared_dpll;
  8378. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  8379. &pipe_config->dpll_hw_state));
  8380. tmp = pipe_config->dpll_hw_state.dpll;
  8381. pipe_config->pixel_multiplier =
  8382. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  8383. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  8384. ironlake_pch_clock_get(crtc, pipe_config);
  8385. } else {
  8386. pipe_config->pixel_multiplier = 1;
  8387. }
  8388. intel_get_pipe_timings(crtc, pipe_config);
  8389. intel_get_pipe_src_size(crtc, pipe_config);
  8390. ironlake_get_pfit_config(crtc, pipe_config);
  8391. ret = true;
  8392. out:
  8393. intel_display_power_put(dev_priv, power_domain);
  8394. return ret;
  8395. }
  8396. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  8397. {
  8398. struct drm_device *dev = &dev_priv->drm;
  8399. struct intel_crtc *crtc;
  8400. for_each_intel_crtc(dev, crtc)
  8401. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  8402. pipe_name(crtc->pipe));
  8403. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  8404. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  8405. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  8406. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  8407. I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
  8408. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  8409. "CPU PWM1 enabled\n");
  8410. if (IS_HASWELL(dev_priv))
  8411. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  8412. "CPU PWM2 enabled\n");
  8413. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  8414. "PCH PWM1 enabled\n");
  8415. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  8416. "Utility pin enabled\n");
  8417. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  8418. /*
  8419. * In theory we can still leave IRQs enabled, as long as only the HPD
  8420. * interrupts remain enabled. We used to check for that, but since it's
  8421. * gen-specific and since we only disable LCPLL after we fully disable
  8422. * the interrupts, the check below should be enough.
  8423. */
  8424. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  8425. }
  8426. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  8427. {
  8428. if (IS_HASWELL(dev_priv))
  8429. return I915_READ(D_COMP_HSW);
  8430. else
  8431. return I915_READ(D_COMP_BDW);
  8432. }
  8433. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  8434. {
  8435. if (IS_HASWELL(dev_priv)) {
  8436. mutex_lock(&dev_priv->rps.hw_lock);
  8437. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  8438. val))
  8439. DRM_DEBUG_KMS("Failed to write to D_COMP\n");
  8440. mutex_unlock(&dev_priv->rps.hw_lock);
  8441. } else {
  8442. I915_WRITE(D_COMP_BDW, val);
  8443. POSTING_READ(D_COMP_BDW);
  8444. }
  8445. }
  8446. /*
  8447. * This function implements pieces of two sequences from BSpec:
  8448. * - Sequence for display software to disable LCPLL
  8449. * - Sequence for display software to allow package C8+
  8450. * The steps implemented here are just the steps that actually touch the LCPLL
  8451. * register. Callers should take care of disabling all the display engine
  8452. * functions, doing the mode unset, fixing interrupts, etc.
  8453. */
  8454. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  8455. bool switch_to_fclk, bool allow_power_down)
  8456. {
  8457. uint32_t val;
  8458. assert_can_disable_lcpll(dev_priv);
  8459. val = I915_READ(LCPLL_CTL);
  8460. if (switch_to_fclk) {
  8461. val |= LCPLL_CD_SOURCE_FCLK;
  8462. I915_WRITE(LCPLL_CTL, val);
  8463. if (wait_for_us(I915_READ(LCPLL_CTL) &
  8464. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8465. DRM_ERROR("Switching to FCLK failed\n");
  8466. val = I915_READ(LCPLL_CTL);
  8467. }
  8468. val |= LCPLL_PLL_DISABLE;
  8469. I915_WRITE(LCPLL_CTL, val);
  8470. POSTING_READ(LCPLL_CTL);
  8471. if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
  8472. DRM_ERROR("LCPLL still locked\n");
  8473. val = hsw_read_dcomp(dev_priv);
  8474. val |= D_COMP_COMP_DISABLE;
  8475. hsw_write_dcomp(dev_priv, val);
  8476. ndelay(100);
  8477. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  8478. 1))
  8479. DRM_ERROR("D_COMP RCOMP still in progress\n");
  8480. if (allow_power_down) {
  8481. val = I915_READ(LCPLL_CTL);
  8482. val |= LCPLL_POWER_DOWN_ALLOW;
  8483. I915_WRITE(LCPLL_CTL, val);
  8484. POSTING_READ(LCPLL_CTL);
  8485. }
  8486. }
  8487. /*
  8488. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  8489. * source.
  8490. */
  8491. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  8492. {
  8493. uint32_t val;
  8494. val = I915_READ(LCPLL_CTL);
  8495. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  8496. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  8497. return;
  8498. /*
  8499. * Make sure we're not on PC8 state before disabling PC8, otherwise
  8500. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  8501. */
  8502. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  8503. if (val & LCPLL_POWER_DOWN_ALLOW) {
  8504. val &= ~LCPLL_POWER_DOWN_ALLOW;
  8505. I915_WRITE(LCPLL_CTL, val);
  8506. POSTING_READ(LCPLL_CTL);
  8507. }
  8508. val = hsw_read_dcomp(dev_priv);
  8509. val |= D_COMP_COMP_FORCE;
  8510. val &= ~D_COMP_COMP_DISABLE;
  8511. hsw_write_dcomp(dev_priv, val);
  8512. val = I915_READ(LCPLL_CTL);
  8513. val &= ~LCPLL_PLL_DISABLE;
  8514. I915_WRITE(LCPLL_CTL, val);
  8515. if (intel_wait_for_register(dev_priv,
  8516. LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  8517. 5))
  8518. DRM_ERROR("LCPLL not locked yet\n");
  8519. if (val & LCPLL_CD_SOURCE_FCLK) {
  8520. val = I915_READ(LCPLL_CTL);
  8521. val &= ~LCPLL_CD_SOURCE_FCLK;
  8522. I915_WRITE(LCPLL_CTL, val);
  8523. if (wait_for_us((I915_READ(LCPLL_CTL) &
  8524. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8525. DRM_ERROR("Switching back to LCPLL failed\n");
  8526. }
  8527. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  8528. intel_update_cdclk(dev_priv);
  8529. }
  8530. /*
  8531. * Package states C8 and deeper are really deep PC states that can only be
  8532. * reached when all the devices on the system allow it, so even if the graphics
  8533. * device allows PC8+, it doesn't mean the system will actually get to these
  8534. * states. Our driver only allows PC8+ when going into runtime PM.
  8535. *
  8536. * The requirements for PC8+ are that all the outputs are disabled, the power
  8537. * well is disabled and most interrupts are disabled, and these are also
  8538. * requirements for runtime PM. When these conditions are met, we manually do
  8539. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  8540. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  8541. * hang the machine.
  8542. *
  8543. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  8544. * the state of some registers, so when we come back from PC8+ we need to
  8545. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  8546. * need to take care of the registers kept by RC6. Notice that this happens even
  8547. * if we don't put the device in PCI D3 state (which is what currently happens
  8548. * because of the runtime PM support).
  8549. *
  8550. * For more, read "Display Sequences for Package C8" on the hardware
  8551. * documentation.
  8552. */
  8553. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  8554. {
  8555. uint32_t val;
  8556. DRM_DEBUG_KMS("Enabling package C8+\n");
  8557. if (HAS_PCH_LPT_LP(dev_priv)) {
  8558. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8559. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  8560. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8561. }
  8562. lpt_disable_clkout_dp(dev_priv);
  8563. hsw_disable_lcpll(dev_priv, true, true);
  8564. }
  8565. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  8566. {
  8567. uint32_t val;
  8568. DRM_DEBUG_KMS("Disabling package C8+\n");
  8569. hsw_restore_lcpll(dev_priv);
  8570. lpt_init_pch_refclk(dev_priv);
  8571. if (HAS_PCH_LPT_LP(dev_priv)) {
  8572. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8573. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  8574. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8575. }
  8576. }
  8577. static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8578. {
  8579. struct drm_device *dev = old_state->dev;
  8580. struct intel_atomic_state *old_intel_state =
  8581. to_intel_atomic_state(old_state);
  8582. unsigned int req_cdclk = old_intel_state->dev_cdclk;
  8583. bxt_set_cdclk(to_i915(dev), req_cdclk);
  8584. }
  8585. static int bdw_adjust_min_pipe_pixel_rate(struct intel_crtc_state *crtc_state,
  8586. int pixel_rate)
  8587. {
  8588. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  8589. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  8590. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  8591. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  8592. /* BSpec says "Do not use DisplayPort with CDCLK less than
  8593. * 432 MHz, audio enabled, port width x4, and link rate
  8594. * HBR2 (5.4 GHz), or else there may be audio corruption or
  8595. * screen corruption."
  8596. */
  8597. if (intel_crtc_has_dp_encoder(crtc_state) &&
  8598. crtc_state->has_audio &&
  8599. crtc_state->port_clock >= 540000 &&
  8600. crtc_state->lane_count == 4)
  8601. pixel_rate = max(432000, pixel_rate);
  8602. return pixel_rate;
  8603. }
  8604. /* compute the max rate for new configuration */
  8605. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  8606. {
  8607. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8608. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8609. struct drm_crtc *crtc;
  8610. struct drm_crtc_state *cstate;
  8611. struct intel_crtc_state *crtc_state;
  8612. unsigned max_pixel_rate = 0, i;
  8613. enum pipe pipe;
  8614. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  8615. sizeof(intel_state->min_pixclk));
  8616. for_each_crtc_in_state(state, crtc, cstate, i) {
  8617. int pixel_rate;
  8618. crtc_state = to_intel_crtc_state(cstate);
  8619. if (!crtc_state->base.enable) {
  8620. intel_state->min_pixclk[i] = 0;
  8621. continue;
  8622. }
  8623. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  8624. if (IS_BROADWELL(dev_priv) || IS_GEN9(dev_priv))
  8625. pixel_rate = bdw_adjust_min_pipe_pixel_rate(crtc_state,
  8626. pixel_rate);
  8627. intel_state->min_pixclk[i] = pixel_rate;
  8628. }
  8629. for_each_pipe(dev_priv, pipe)
  8630. max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
  8631. return max_pixel_rate;
  8632. }
  8633. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  8634. {
  8635. struct drm_i915_private *dev_priv = to_i915(dev);
  8636. uint32_t val, data;
  8637. int ret;
  8638. if (WARN((I915_READ(LCPLL_CTL) &
  8639. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  8640. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  8641. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  8642. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  8643. "trying to change cdclk frequency with cdclk not enabled\n"))
  8644. return;
  8645. mutex_lock(&dev_priv->rps.hw_lock);
  8646. ret = sandybridge_pcode_write(dev_priv,
  8647. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  8648. mutex_unlock(&dev_priv->rps.hw_lock);
  8649. if (ret) {
  8650. DRM_ERROR("failed to inform pcode about cdclk change\n");
  8651. return;
  8652. }
  8653. val = I915_READ(LCPLL_CTL);
  8654. val |= LCPLL_CD_SOURCE_FCLK;
  8655. I915_WRITE(LCPLL_CTL, val);
  8656. if (wait_for_us(I915_READ(LCPLL_CTL) &
  8657. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8658. DRM_ERROR("Switching to FCLK failed\n");
  8659. val = I915_READ(LCPLL_CTL);
  8660. val &= ~LCPLL_CLK_FREQ_MASK;
  8661. switch (cdclk) {
  8662. case 450000:
  8663. val |= LCPLL_CLK_FREQ_450;
  8664. data = 0;
  8665. break;
  8666. case 540000:
  8667. val |= LCPLL_CLK_FREQ_54O_BDW;
  8668. data = 1;
  8669. break;
  8670. case 337500:
  8671. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8672. data = 2;
  8673. break;
  8674. case 675000:
  8675. val |= LCPLL_CLK_FREQ_675_BDW;
  8676. data = 3;
  8677. break;
  8678. default:
  8679. WARN(1, "invalid cdclk frequency\n");
  8680. return;
  8681. }
  8682. I915_WRITE(LCPLL_CTL, val);
  8683. val = I915_READ(LCPLL_CTL);
  8684. val &= ~LCPLL_CD_SOURCE_FCLK;
  8685. I915_WRITE(LCPLL_CTL, val);
  8686. if (wait_for_us((I915_READ(LCPLL_CTL) &
  8687. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8688. DRM_ERROR("Switching back to LCPLL failed\n");
  8689. mutex_lock(&dev_priv->rps.hw_lock);
  8690. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8691. mutex_unlock(&dev_priv->rps.hw_lock);
  8692. I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
  8693. intel_update_cdclk(dev_priv);
  8694. WARN(cdclk != dev_priv->cdclk_freq,
  8695. "cdclk requested %d kHz but got %d kHz\n",
  8696. cdclk, dev_priv->cdclk_freq);
  8697. }
  8698. static int broadwell_calc_cdclk(int max_pixclk)
  8699. {
  8700. if (max_pixclk > 540000)
  8701. return 675000;
  8702. else if (max_pixclk > 450000)
  8703. return 540000;
  8704. else if (max_pixclk > 337500)
  8705. return 450000;
  8706. else
  8707. return 337500;
  8708. }
  8709. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8710. {
  8711. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8712. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8713. int max_pixclk = ilk_max_pixel_rate(state);
  8714. int cdclk;
  8715. /*
  8716. * FIXME should also account for plane ratio
  8717. * once 64bpp pixel formats are supported.
  8718. */
  8719. cdclk = broadwell_calc_cdclk(max_pixclk);
  8720. if (cdclk > dev_priv->max_cdclk_freq) {
  8721. DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8722. cdclk, dev_priv->max_cdclk_freq);
  8723. return -EINVAL;
  8724. }
  8725. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8726. if (!intel_state->active_crtcs)
  8727. intel_state->dev_cdclk = broadwell_calc_cdclk(0);
  8728. return 0;
  8729. }
  8730. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8731. {
  8732. struct drm_device *dev = old_state->dev;
  8733. struct intel_atomic_state *old_intel_state =
  8734. to_intel_atomic_state(old_state);
  8735. unsigned req_cdclk = old_intel_state->dev_cdclk;
  8736. broadwell_set_cdclk(dev, req_cdclk);
  8737. }
  8738. static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
  8739. {
  8740. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8741. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8742. const int max_pixclk = ilk_max_pixel_rate(state);
  8743. int vco = intel_state->cdclk_pll_vco;
  8744. int cdclk;
  8745. /*
  8746. * FIXME should also account for plane ratio
  8747. * once 64bpp pixel formats are supported.
  8748. */
  8749. cdclk = skl_calc_cdclk(max_pixclk, vco);
  8750. /*
  8751. * FIXME move the cdclk caclulation to
  8752. * compute_config() so we can fail gracegully.
  8753. */
  8754. if (cdclk > dev_priv->max_cdclk_freq) {
  8755. DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8756. cdclk, dev_priv->max_cdclk_freq);
  8757. cdclk = dev_priv->max_cdclk_freq;
  8758. }
  8759. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8760. if (!intel_state->active_crtcs)
  8761. intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
  8762. return 0;
  8763. }
  8764. static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8765. {
  8766. struct drm_i915_private *dev_priv = to_i915(old_state->dev);
  8767. struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
  8768. unsigned int req_cdclk = intel_state->dev_cdclk;
  8769. unsigned int req_vco = intel_state->cdclk_pll_vco;
  8770. skl_set_cdclk(dev_priv, req_cdclk, req_vco);
  8771. }
  8772. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8773. struct intel_crtc_state *crtc_state)
  8774. {
  8775. if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
  8776. if (!intel_ddi_pll_select(crtc, crtc_state))
  8777. return -EINVAL;
  8778. }
  8779. crtc->lowfreq_avail = false;
  8780. return 0;
  8781. }
  8782. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8783. enum port port,
  8784. struct intel_crtc_state *pipe_config)
  8785. {
  8786. enum intel_dpll_id id;
  8787. switch (port) {
  8788. case PORT_A:
  8789. id = DPLL_ID_SKL_DPLL0;
  8790. break;
  8791. case PORT_B:
  8792. id = DPLL_ID_SKL_DPLL1;
  8793. break;
  8794. case PORT_C:
  8795. id = DPLL_ID_SKL_DPLL2;
  8796. break;
  8797. default:
  8798. DRM_ERROR("Incorrect port type\n");
  8799. return;
  8800. }
  8801. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8802. }
  8803. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8804. enum port port,
  8805. struct intel_crtc_state *pipe_config)
  8806. {
  8807. enum intel_dpll_id id;
  8808. u32 temp;
  8809. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8810. id = temp >> (port * 3 + 1);
  8811. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
  8812. return;
  8813. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8814. }
  8815. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8816. enum port port,
  8817. struct intel_crtc_state *pipe_config)
  8818. {
  8819. enum intel_dpll_id id;
  8820. uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8821. switch (ddi_pll_sel) {
  8822. case PORT_CLK_SEL_WRPLL1:
  8823. id = DPLL_ID_WRPLL1;
  8824. break;
  8825. case PORT_CLK_SEL_WRPLL2:
  8826. id = DPLL_ID_WRPLL2;
  8827. break;
  8828. case PORT_CLK_SEL_SPLL:
  8829. id = DPLL_ID_SPLL;
  8830. break;
  8831. case PORT_CLK_SEL_LCPLL_810:
  8832. id = DPLL_ID_LCPLL_810;
  8833. break;
  8834. case PORT_CLK_SEL_LCPLL_1350:
  8835. id = DPLL_ID_LCPLL_1350;
  8836. break;
  8837. case PORT_CLK_SEL_LCPLL_2700:
  8838. id = DPLL_ID_LCPLL_2700;
  8839. break;
  8840. default:
  8841. MISSING_CASE(ddi_pll_sel);
  8842. /* fall through */
  8843. case PORT_CLK_SEL_NONE:
  8844. return;
  8845. }
  8846. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8847. }
  8848. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  8849. struct intel_crtc_state *pipe_config,
  8850. unsigned long *power_domain_mask)
  8851. {
  8852. struct drm_device *dev = crtc->base.dev;
  8853. struct drm_i915_private *dev_priv = to_i915(dev);
  8854. enum intel_display_power_domain power_domain;
  8855. u32 tmp;
  8856. /*
  8857. * The pipe->transcoder mapping is fixed with the exception of the eDP
  8858. * transcoder handled below.
  8859. */
  8860. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8861. /*
  8862. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  8863. * consistency and less surprising code; it's in always on power).
  8864. */
  8865. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8866. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8867. enum pipe trans_edp_pipe;
  8868. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8869. default:
  8870. WARN(1, "unknown pipe linked to edp transcoder\n");
  8871. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8872. case TRANS_DDI_EDP_INPUT_A_ON:
  8873. trans_edp_pipe = PIPE_A;
  8874. break;
  8875. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8876. trans_edp_pipe = PIPE_B;
  8877. break;
  8878. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8879. trans_edp_pipe = PIPE_C;
  8880. break;
  8881. }
  8882. if (trans_edp_pipe == crtc->pipe)
  8883. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8884. }
  8885. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  8886. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8887. return false;
  8888. *power_domain_mask |= BIT(power_domain);
  8889. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8890. return tmp & PIPECONF_ENABLE;
  8891. }
  8892. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  8893. struct intel_crtc_state *pipe_config,
  8894. unsigned long *power_domain_mask)
  8895. {
  8896. struct drm_device *dev = crtc->base.dev;
  8897. struct drm_i915_private *dev_priv = to_i915(dev);
  8898. enum intel_display_power_domain power_domain;
  8899. enum port port;
  8900. enum transcoder cpu_transcoder;
  8901. u32 tmp;
  8902. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  8903. if (port == PORT_A)
  8904. cpu_transcoder = TRANSCODER_DSI_A;
  8905. else
  8906. cpu_transcoder = TRANSCODER_DSI_C;
  8907. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  8908. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8909. continue;
  8910. *power_domain_mask |= BIT(power_domain);
  8911. /*
  8912. * The PLL needs to be enabled with a valid divider
  8913. * configuration, otherwise accessing DSI registers will hang
  8914. * the machine. See BSpec North Display Engine
  8915. * registers/MIPI[BXT]. We can break out here early, since we
  8916. * need the same DSI PLL to be enabled for both DSI ports.
  8917. */
  8918. if (!intel_dsi_pll_is_enabled(dev_priv))
  8919. break;
  8920. /* XXX: this works for video mode only */
  8921. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  8922. if (!(tmp & DPI_ENABLE))
  8923. continue;
  8924. tmp = I915_READ(MIPI_CTRL(port));
  8925. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  8926. continue;
  8927. pipe_config->cpu_transcoder = cpu_transcoder;
  8928. break;
  8929. }
  8930. return transcoder_is_dsi(pipe_config->cpu_transcoder);
  8931. }
  8932. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8933. struct intel_crtc_state *pipe_config)
  8934. {
  8935. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8936. struct intel_shared_dpll *pll;
  8937. enum port port;
  8938. uint32_t tmp;
  8939. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8940. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8941. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  8942. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8943. else if (IS_GEN9_LP(dev_priv))
  8944. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8945. else
  8946. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8947. pll = pipe_config->shared_dpll;
  8948. if (pll) {
  8949. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  8950. &pipe_config->dpll_hw_state));
  8951. }
  8952. /*
  8953. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8954. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8955. * the PCH transcoder is on.
  8956. */
  8957. if (INTEL_GEN(dev_priv) < 9 &&
  8958. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8959. pipe_config->has_pch_encoder = true;
  8960. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8961. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8962. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8963. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8964. }
  8965. }
  8966. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8967. struct intel_crtc_state *pipe_config)
  8968. {
  8969. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8970. enum intel_display_power_domain power_domain;
  8971. unsigned long power_domain_mask;
  8972. bool active;
  8973. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  8974. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8975. return false;
  8976. power_domain_mask = BIT(power_domain);
  8977. pipe_config->shared_dpll = NULL;
  8978. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  8979. if (IS_GEN9_LP(dev_priv) &&
  8980. bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
  8981. WARN_ON(active);
  8982. active = true;
  8983. }
  8984. if (!active)
  8985. goto out;
  8986. if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  8987. haswell_get_ddi_port_state(crtc, pipe_config);
  8988. intel_get_pipe_timings(crtc, pipe_config);
  8989. }
  8990. intel_get_pipe_src_size(crtc, pipe_config);
  8991. pipe_config->gamma_mode =
  8992. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  8993. if (INTEL_GEN(dev_priv) >= 9) {
  8994. intel_crtc_init_scalers(crtc, pipe_config);
  8995. pipe_config->scaler_state.scaler_id = -1;
  8996. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8997. }
  8998. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  8999. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  9000. power_domain_mask |= BIT(power_domain);
  9001. if (INTEL_GEN(dev_priv) >= 9)
  9002. skylake_get_pfit_config(crtc, pipe_config);
  9003. else
  9004. ironlake_get_pfit_config(crtc, pipe_config);
  9005. }
  9006. if (IS_HASWELL(dev_priv))
  9007. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  9008. (I915_READ(IPS_CTL) & IPS_ENABLE);
  9009. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  9010. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  9011. pipe_config->pixel_multiplier =
  9012. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  9013. } else {
  9014. pipe_config->pixel_multiplier = 1;
  9015. }
  9016. out:
  9017. for_each_power_domain(power_domain, power_domain_mask)
  9018. intel_display_power_put(dev_priv, power_domain);
  9019. return active;
  9020. }
  9021. static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
  9022. const struct intel_plane_state *plane_state)
  9023. {
  9024. struct drm_device *dev = crtc->dev;
  9025. struct drm_i915_private *dev_priv = to_i915(dev);
  9026. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9027. uint32_t cntl = 0, size = 0;
  9028. if (plane_state && plane_state->base.visible) {
  9029. unsigned int width = plane_state->base.crtc_w;
  9030. unsigned int height = plane_state->base.crtc_h;
  9031. unsigned int stride = roundup_pow_of_two(width) * 4;
  9032. switch (stride) {
  9033. default:
  9034. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  9035. width, stride);
  9036. stride = 256;
  9037. /* fallthrough */
  9038. case 256:
  9039. case 512:
  9040. case 1024:
  9041. case 2048:
  9042. break;
  9043. }
  9044. cntl |= CURSOR_ENABLE |
  9045. CURSOR_GAMMA_ENABLE |
  9046. CURSOR_FORMAT_ARGB |
  9047. CURSOR_STRIDE(stride);
  9048. size = (height << 12) | width;
  9049. }
  9050. if (intel_crtc->cursor_cntl != 0 &&
  9051. (intel_crtc->cursor_base != base ||
  9052. intel_crtc->cursor_size != size ||
  9053. intel_crtc->cursor_cntl != cntl)) {
  9054. /* On these chipsets we can only modify the base/size/stride
  9055. * whilst the cursor is disabled.
  9056. */
  9057. I915_WRITE(CURCNTR(PIPE_A), 0);
  9058. POSTING_READ(CURCNTR(PIPE_A));
  9059. intel_crtc->cursor_cntl = 0;
  9060. }
  9061. if (intel_crtc->cursor_base != base) {
  9062. I915_WRITE(CURBASE(PIPE_A), base);
  9063. intel_crtc->cursor_base = base;
  9064. }
  9065. if (intel_crtc->cursor_size != size) {
  9066. I915_WRITE(CURSIZE, size);
  9067. intel_crtc->cursor_size = size;
  9068. }
  9069. if (intel_crtc->cursor_cntl != cntl) {
  9070. I915_WRITE(CURCNTR(PIPE_A), cntl);
  9071. POSTING_READ(CURCNTR(PIPE_A));
  9072. intel_crtc->cursor_cntl = cntl;
  9073. }
  9074. }
  9075. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
  9076. const struct intel_plane_state *plane_state)
  9077. {
  9078. struct drm_device *dev = crtc->dev;
  9079. struct drm_i915_private *dev_priv = to_i915(dev);
  9080. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9081. int pipe = intel_crtc->pipe;
  9082. uint32_t cntl = 0;
  9083. if (plane_state && plane_state->base.visible) {
  9084. cntl = MCURSOR_GAMMA_ENABLE;
  9085. switch (plane_state->base.crtc_w) {
  9086. case 64:
  9087. cntl |= CURSOR_MODE_64_ARGB_AX;
  9088. break;
  9089. case 128:
  9090. cntl |= CURSOR_MODE_128_ARGB_AX;
  9091. break;
  9092. case 256:
  9093. cntl |= CURSOR_MODE_256_ARGB_AX;
  9094. break;
  9095. default:
  9096. MISSING_CASE(plane_state->base.crtc_w);
  9097. return;
  9098. }
  9099. cntl |= pipe << 28; /* Connect to correct pipe */
  9100. if (HAS_DDI(dev_priv))
  9101. cntl |= CURSOR_PIPE_CSC_ENABLE;
  9102. if (plane_state->base.rotation & DRM_ROTATE_180)
  9103. cntl |= CURSOR_ROTATE_180;
  9104. }
  9105. if (intel_crtc->cursor_cntl != cntl) {
  9106. I915_WRITE(CURCNTR(pipe), cntl);
  9107. POSTING_READ(CURCNTR(pipe));
  9108. intel_crtc->cursor_cntl = cntl;
  9109. }
  9110. /* and commit changes on next vblank */
  9111. I915_WRITE(CURBASE(pipe), base);
  9112. POSTING_READ(CURBASE(pipe));
  9113. intel_crtc->cursor_base = base;
  9114. }
  9115. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  9116. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  9117. const struct intel_plane_state *plane_state)
  9118. {
  9119. struct drm_device *dev = crtc->dev;
  9120. struct drm_i915_private *dev_priv = to_i915(dev);
  9121. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9122. int pipe = intel_crtc->pipe;
  9123. u32 base = intel_crtc->cursor_addr;
  9124. u32 pos = 0;
  9125. if (plane_state) {
  9126. int x = plane_state->base.crtc_x;
  9127. int y = plane_state->base.crtc_y;
  9128. if (x < 0) {
  9129. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  9130. x = -x;
  9131. }
  9132. pos |= x << CURSOR_X_SHIFT;
  9133. if (y < 0) {
  9134. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  9135. y = -y;
  9136. }
  9137. pos |= y << CURSOR_Y_SHIFT;
  9138. /* ILK+ do this automagically */
  9139. if (HAS_GMCH_DISPLAY(dev_priv) &&
  9140. plane_state->base.rotation & DRM_ROTATE_180) {
  9141. base += (plane_state->base.crtc_h *
  9142. plane_state->base.crtc_w - 1) * 4;
  9143. }
  9144. }
  9145. I915_WRITE(CURPOS(pipe), pos);
  9146. if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  9147. i845_update_cursor(crtc, base, plane_state);
  9148. else
  9149. i9xx_update_cursor(crtc, base, plane_state);
  9150. }
  9151. static bool cursor_size_ok(struct drm_i915_private *dev_priv,
  9152. uint32_t width, uint32_t height)
  9153. {
  9154. if (width == 0 || height == 0)
  9155. return false;
  9156. /*
  9157. * 845g/865g are special in that they are only limited by
  9158. * the width of their cursors, the height is arbitrary up to
  9159. * the precision of the register. Everything else requires
  9160. * square cursors, limited to a few power-of-two sizes.
  9161. */
  9162. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  9163. if ((width & 63) != 0)
  9164. return false;
  9165. if (width > (IS_I845G(dev_priv) ? 64 : 512))
  9166. return false;
  9167. if (height > 1023)
  9168. return false;
  9169. } else {
  9170. switch (width | height) {
  9171. case 256:
  9172. case 128:
  9173. if (IS_GEN2(dev_priv))
  9174. return false;
  9175. case 64:
  9176. break;
  9177. default:
  9178. return false;
  9179. }
  9180. }
  9181. return true;
  9182. }
  9183. /* VESA 640x480x72Hz mode to set on the pipe */
  9184. static struct drm_display_mode load_detect_mode = {
  9185. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  9186. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  9187. };
  9188. struct drm_framebuffer *
  9189. __intel_framebuffer_create(struct drm_device *dev,
  9190. struct drm_mode_fb_cmd2 *mode_cmd,
  9191. struct drm_i915_gem_object *obj)
  9192. {
  9193. struct intel_framebuffer *intel_fb;
  9194. int ret;
  9195. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  9196. if (!intel_fb)
  9197. return ERR_PTR(-ENOMEM);
  9198. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  9199. if (ret)
  9200. goto err;
  9201. return &intel_fb->base;
  9202. err:
  9203. kfree(intel_fb);
  9204. return ERR_PTR(ret);
  9205. }
  9206. static struct drm_framebuffer *
  9207. intel_framebuffer_create(struct drm_device *dev,
  9208. struct drm_mode_fb_cmd2 *mode_cmd,
  9209. struct drm_i915_gem_object *obj)
  9210. {
  9211. struct drm_framebuffer *fb;
  9212. int ret;
  9213. ret = i915_mutex_lock_interruptible(dev);
  9214. if (ret)
  9215. return ERR_PTR(ret);
  9216. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  9217. mutex_unlock(&dev->struct_mutex);
  9218. return fb;
  9219. }
  9220. static u32
  9221. intel_framebuffer_pitch_for_width(int width, int bpp)
  9222. {
  9223. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  9224. return ALIGN(pitch, 64);
  9225. }
  9226. static u32
  9227. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  9228. {
  9229. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  9230. return PAGE_ALIGN(pitch * mode->vdisplay);
  9231. }
  9232. static struct drm_framebuffer *
  9233. intel_framebuffer_create_for_mode(struct drm_device *dev,
  9234. struct drm_display_mode *mode,
  9235. int depth, int bpp)
  9236. {
  9237. struct drm_framebuffer *fb;
  9238. struct drm_i915_gem_object *obj;
  9239. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  9240. obj = i915_gem_object_create(to_i915(dev),
  9241. intel_framebuffer_size_for_mode(mode, bpp));
  9242. if (IS_ERR(obj))
  9243. return ERR_CAST(obj);
  9244. mode_cmd.width = mode->hdisplay;
  9245. mode_cmd.height = mode->vdisplay;
  9246. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  9247. bpp);
  9248. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  9249. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  9250. if (IS_ERR(fb))
  9251. i915_gem_object_put(obj);
  9252. return fb;
  9253. }
  9254. static struct drm_framebuffer *
  9255. mode_fits_in_fbdev(struct drm_device *dev,
  9256. struct drm_display_mode *mode)
  9257. {
  9258. #ifdef CONFIG_DRM_FBDEV_EMULATION
  9259. struct drm_i915_private *dev_priv = to_i915(dev);
  9260. struct drm_i915_gem_object *obj;
  9261. struct drm_framebuffer *fb;
  9262. if (!dev_priv->fbdev)
  9263. return NULL;
  9264. if (!dev_priv->fbdev->fb)
  9265. return NULL;
  9266. obj = dev_priv->fbdev->fb->obj;
  9267. BUG_ON(!obj);
  9268. fb = &dev_priv->fbdev->fb->base;
  9269. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  9270. fb->format->cpp[0] * 8))
  9271. return NULL;
  9272. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  9273. return NULL;
  9274. drm_framebuffer_reference(fb);
  9275. return fb;
  9276. #else
  9277. return NULL;
  9278. #endif
  9279. }
  9280. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  9281. struct drm_crtc *crtc,
  9282. struct drm_display_mode *mode,
  9283. struct drm_framebuffer *fb,
  9284. int x, int y)
  9285. {
  9286. struct drm_plane_state *plane_state;
  9287. int hdisplay, vdisplay;
  9288. int ret;
  9289. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  9290. if (IS_ERR(plane_state))
  9291. return PTR_ERR(plane_state);
  9292. if (mode)
  9293. drm_mode_get_hv_timing(mode, &hdisplay, &vdisplay);
  9294. else
  9295. hdisplay = vdisplay = 0;
  9296. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  9297. if (ret)
  9298. return ret;
  9299. drm_atomic_set_fb_for_plane(plane_state, fb);
  9300. plane_state->crtc_x = 0;
  9301. plane_state->crtc_y = 0;
  9302. plane_state->crtc_w = hdisplay;
  9303. plane_state->crtc_h = vdisplay;
  9304. plane_state->src_x = x << 16;
  9305. plane_state->src_y = y << 16;
  9306. plane_state->src_w = hdisplay << 16;
  9307. plane_state->src_h = vdisplay << 16;
  9308. return 0;
  9309. }
  9310. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  9311. struct drm_display_mode *mode,
  9312. struct intel_load_detect_pipe *old,
  9313. struct drm_modeset_acquire_ctx *ctx)
  9314. {
  9315. struct intel_crtc *intel_crtc;
  9316. struct intel_encoder *intel_encoder =
  9317. intel_attached_encoder(connector);
  9318. struct drm_crtc *possible_crtc;
  9319. struct drm_encoder *encoder = &intel_encoder->base;
  9320. struct drm_crtc *crtc = NULL;
  9321. struct drm_device *dev = encoder->dev;
  9322. struct drm_i915_private *dev_priv = to_i915(dev);
  9323. struct drm_framebuffer *fb;
  9324. struct drm_mode_config *config = &dev->mode_config;
  9325. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  9326. struct drm_connector_state *connector_state;
  9327. struct intel_crtc_state *crtc_state;
  9328. int ret, i = -1;
  9329. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  9330. connector->base.id, connector->name,
  9331. encoder->base.id, encoder->name);
  9332. old->restore_state = NULL;
  9333. retry:
  9334. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  9335. if (ret)
  9336. goto fail;
  9337. /*
  9338. * Algorithm gets a little messy:
  9339. *
  9340. * - if the connector already has an assigned crtc, use it (but make
  9341. * sure it's on first)
  9342. *
  9343. * - try to find the first unused crtc that can drive this connector,
  9344. * and use that if we find one
  9345. */
  9346. /* See if we already have a CRTC for this connector */
  9347. if (connector->state->crtc) {
  9348. crtc = connector->state->crtc;
  9349. ret = drm_modeset_lock(&crtc->mutex, ctx);
  9350. if (ret)
  9351. goto fail;
  9352. /* Make sure the crtc and connector are running */
  9353. goto found;
  9354. }
  9355. /* Find an unused one (if possible) */
  9356. for_each_crtc(dev, possible_crtc) {
  9357. i++;
  9358. if (!(encoder->possible_crtcs & (1 << i)))
  9359. continue;
  9360. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  9361. if (ret)
  9362. goto fail;
  9363. if (possible_crtc->state->enable) {
  9364. drm_modeset_unlock(&possible_crtc->mutex);
  9365. continue;
  9366. }
  9367. crtc = possible_crtc;
  9368. break;
  9369. }
  9370. /*
  9371. * If we didn't find an unused CRTC, don't use any.
  9372. */
  9373. if (!crtc) {
  9374. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  9375. goto fail;
  9376. }
  9377. found:
  9378. intel_crtc = to_intel_crtc(crtc);
  9379. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  9380. if (ret)
  9381. goto fail;
  9382. state = drm_atomic_state_alloc(dev);
  9383. restore_state = drm_atomic_state_alloc(dev);
  9384. if (!state || !restore_state) {
  9385. ret = -ENOMEM;
  9386. goto fail;
  9387. }
  9388. state->acquire_ctx = ctx;
  9389. restore_state->acquire_ctx = ctx;
  9390. connector_state = drm_atomic_get_connector_state(state, connector);
  9391. if (IS_ERR(connector_state)) {
  9392. ret = PTR_ERR(connector_state);
  9393. goto fail;
  9394. }
  9395. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  9396. if (ret)
  9397. goto fail;
  9398. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  9399. if (IS_ERR(crtc_state)) {
  9400. ret = PTR_ERR(crtc_state);
  9401. goto fail;
  9402. }
  9403. crtc_state->base.active = crtc_state->base.enable = true;
  9404. if (!mode)
  9405. mode = &load_detect_mode;
  9406. /* We need a framebuffer large enough to accommodate all accesses
  9407. * that the plane may generate whilst we perform load detection.
  9408. * We can not rely on the fbcon either being present (we get called
  9409. * during its initialisation to detect all boot displays, or it may
  9410. * not even exist) or that it is large enough to satisfy the
  9411. * requested mode.
  9412. */
  9413. fb = mode_fits_in_fbdev(dev, mode);
  9414. if (fb == NULL) {
  9415. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  9416. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  9417. } else
  9418. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  9419. if (IS_ERR(fb)) {
  9420. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  9421. goto fail;
  9422. }
  9423. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  9424. if (ret)
  9425. goto fail;
  9426. drm_framebuffer_unreference(fb);
  9427. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  9428. if (ret)
  9429. goto fail;
  9430. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  9431. if (!ret)
  9432. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  9433. if (!ret)
  9434. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
  9435. if (ret) {
  9436. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  9437. goto fail;
  9438. }
  9439. ret = drm_atomic_commit(state);
  9440. if (ret) {
  9441. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  9442. goto fail;
  9443. }
  9444. old->restore_state = restore_state;
  9445. drm_atomic_state_put(state);
  9446. /* let the connector get through one full cycle before testing */
  9447. intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
  9448. return true;
  9449. fail:
  9450. if (state) {
  9451. drm_atomic_state_put(state);
  9452. state = NULL;
  9453. }
  9454. if (restore_state) {
  9455. drm_atomic_state_put(restore_state);
  9456. restore_state = NULL;
  9457. }
  9458. if (ret == -EDEADLK) {
  9459. drm_modeset_backoff(ctx);
  9460. goto retry;
  9461. }
  9462. return false;
  9463. }
  9464. void intel_release_load_detect_pipe(struct drm_connector *connector,
  9465. struct intel_load_detect_pipe *old,
  9466. struct drm_modeset_acquire_ctx *ctx)
  9467. {
  9468. struct intel_encoder *intel_encoder =
  9469. intel_attached_encoder(connector);
  9470. struct drm_encoder *encoder = &intel_encoder->base;
  9471. struct drm_atomic_state *state = old->restore_state;
  9472. int ret;
  9473. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  9474. connector->base.id, connector->name,
  9475. encoder->base.id, encoder->name);
  9476. if (!state)
  9477. return;
  9478. ret = drm_atomic_commit(state);
  9479. if (ret)
  9480. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  9481. drm_atomic_state_put(state);
  9482. }
  9483. static int i9xx_pll_refclk(struct drm_device *dev,
  9484. const struct intel_crtc_state *pipe_config)
  9485. {
  9486. struct drm_i915_private *dev_priv = to_i915(dev);
  9487. u32 dpll = pipe_config->dpll_hw_state.dpll;
  9488. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  9489. return dev_priv->vbt.lvds_ssc_freq;
  9490. else if (HAS_PCH_SPLIT(dev_priv))
  9491. return 120000;
  9492. else if (!IS_GEN2(dev_priv))
  9493. return 96000;
  9494. else
  9495. return 48000;
  9496. }
  9497. /* Returns the clock of the currently programmed mode of the given pipe. */
  9498. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  9499. struct intel_crtc_state *pipe_config)
  9500. {
  9501. struct drm_device *dev = crtc->base.dev;
  9502. struct drm_i915_private *dev_priv = to_i915(dev);
  9503. int pipe = pipe_config->cpu_transcoder;
  9504. u32 dpll = pipe_config->dpll_hw_state.dpll;
  9505. u32 fp;
  9506. struct dpll clock;
  9507. int port_clock;
  9508. int refclk = i9xx_pll_refclk(dev, pipe_config);
  9509. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  9510. fp = pipe_config->dpll_hw_state.fp0;
  9511. else
  9512. fp = pipe_config->dpll_hw_state.fp1;
  9513. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  9514. if (IS_PINEVIEW(dev_priv)) {
  9515. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  9516. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  9517. } else {
  9518. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  9519. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  9520. }
  9521. if (!IS_GEN2(dev_priv)) {
  9522. if (IS_PINEVIEW(dev_priv))
  9523. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  9524. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  9525. else
  9526. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  9527. DPLL_FPA01_P1_POST_DIV_SHIFT);
  9528. switch (dpll & DPLL_MODE_MASK) {
  9529. case DPLLB_MODE_DAC_SERIAL:
  9530. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  9531. 5 : 10;
  9532. break;
  9533. case DPLLB_MODE_LVDS:
  9534. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  9535. 7 : 14;
  9536. break;
  9537. default:
  9538. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  9539. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  9540. return;
  9541. }
  9542. if (IS_PINEVIEW(dev_priv))
  9543. port_clock = pnv_calc_dpll_params(refclk, &clock);
  9544. else
  9545. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  9546. } else {
  9547. u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
  9548. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  9549. if (is_lvds) {
  9550. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  9551. DPLL_FPA01_P1_POST_DIV_SHIFT);
  9552. if (lvds & LVDS_CLKB_POWER_UP)
  9553. clock.p2 = 7;
  9554. else
  9555. clock.p2 = 14;
  9556. } else {
  9557. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  9558. clock.p1 = 2;
  9559. else {
  9560. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  9561. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  9562. }
  9563. if (dpll & PLL_P2_DIVIDE_BY_4)
  9564. clock.p2 = 4;
  9565. else
  9566. clock.p2 = 2;
  9567. }
  9568. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  9569. }
  9570. /*
  9571. * This value includes pixel_multiplier. We will use
  9572. * port_clock to compute adjusted_mode.crtc_clock in the
  9573. * encoder's get_config() function.
  9574. */
  9575. pipe_config->port_clock = port_clock;
  9576. }
  9577. int intel_dotclock_calculate(int link_freq,
  9578. const struct intel_link_m_n *m_n)
  9579. {
  9580. /*
  9581. * The calculation for the data clock is:
  9582. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  9583. * But we want to avoid losing precison if possible, so:
  9584. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  9585. *
  9586. * and the link clock is simpler:
  9587. * link_clock = (m * link_clock) / n
  9588. */
  9589. if (!m_n->link_n)
  9590. return 0;
  9591. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  9592. }
  9593. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  9594. struct intel_crtc_state *pipe_config)
  9595. {
  9596. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9597. /* read out port_clock from the DPLL */
  9598. i9xx_crtc_clock_get(crtc, pipe_config);
  9599. /*
  9600. * In case there is an active pipe without active ports,
  9601. * we may need some idea for the dotclock anyway.
  9602. * Calculate one based on the FDI configuration.
  9603. */
  9604. pipe_config->base.adjusted_mode.crtc_clock =
  9605. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9606. &pipe_config->fdi_m_n);
  9607. }
  9608. /** Returns the currently programmed mode of the given pipe. */
  9609. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  9610. struct drm_crtc *crtc)
  9611. {
  9612. struct drm_i915_private *dev_priv = to_i915(dev);
  9613. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9614. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  9615. struct drm_display_mode *mode;
  9616. struct intel_crtc_state *pipe_config;
  9617. int htot = I915_READ(HTOTAL(cpu_transcoder));
  9618. int hsync = I915_READ(HSYNC(cpu_transcoder));
  9619. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  9620. int vsync = I915_READ(VSYNC(cpu_transcoder));
  9621. enum pipe pipe = intel_crtc->pipe;
  9622. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  9623. if (!mode)
  9624. return NULL;
  9625. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  9626. if (!pipe_config) {
  9627. kfree(mode);
  9628. return NULL;
  9629. }
  9630. /*
  9631. * Construct a pipe_config sufficient for getting the clock info
  9632. * back out of crtc_clock_get.
  9633. *
  9634. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  9635. * to use a real value here instead.
  9636. */
  9637. pipe_config->cpu_transcoder = (enum transcoder) pipe;
  9638. pipe_config->pixel_multiplier = 1;
  9639. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  9640. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  9641. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  9642. i9xx_crtc_clock_get(intel_crtc, pipe_config);
  9643. mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
  9644. mode->hdisplay = (htot & 0xffff) + 1;
  9645. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  9646. mode->hsync_start = (hsync & 0xffff) + 1;
  9647. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  9648. mode->vdisplay = (vtot & 0xffff) + 1;
  9649. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  9650. mode->vsync_start = (vsync & 0xffff) + 1;
  9651. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  9652. drm_mode_set_name(mode);
  9653. kfree(pipe_config);
  9654. return mode;
  9655. }
  9656. static void intel_crtc_destroy(struct drm_crtc *crtc)
  9657. {
  9658. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9659. struct drm_device *dev = crtc->dev;
  9660. struct intel_flip_work *work;
  9661. spin_lock_irq(&dev->event_lock);
  9662. work = intel_crtc->flip_work;
  9663. intel_crtc->flip_work = NULL;
  9664. spin_unlock_irq(&dev->event_lock);
  9665. if (work) {
  9666. cancel_work_sync(&work->mmio_work);
  9667. cancel_work_sync(&work->unpin_work);
  9668. kfree(work);
  9669. }
  9670. drm_crtc_cleanup(crtc);
  9671. kfree(intel_crtc);
  9672. }
  9673. static void intel_unpin_work_fn(struct work_struct *__work)
  9674. {
  9675. struct intel_flip_work *work =
  9676. container_of(__work, struct intel_flip_work, unpin_work);
  9677. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  9678. struct drm_device *dev = crtc->base.dev;
  9679. struct drm_plane *primary = crtc->base.primary;
  9680. if (is_mmio_work(work))
  9681. flush_work(&work->mmio_work);
  9682. mutex_lock(&dev->struct_mutex);
  9683. intel_unpin_fb_vma(work->old_vma);
  9684. i915_gem_object_put(work->pending_flip_obj);
  9685. mutex_unlock(&dev->struct_mutex);
  9686. i915_gem_request_put(work->flip_queued_req);
  9687. intel_frontbuffer_flip_complete(to_i915(dev),
  9688. to_intel_plane(primary)->frontbuffer_bit);
  9689. intel_fbc_post_update(crtc);
  9690. drm_framebuffer_unreference(work->old_fb);
  9691. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  9692. atomic_dec(&crtc->unpin_work_count);
  9693. kfree(work);
  9694. }
  9695. /* Is 'a' after or equal to 'b'? */
  9696. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9697. {
  9698. return !((a - b) & 0x80000000);
  9699. }
  9700. static bool __pageflip_finished_cs(struct intel_crtc *crtc,
  9701. struct intel_flip_work *work)
  9702. {
  9703. struct drm_device *dev = crtc->base.dev;
  9704. struct drm_i915_private *dev_priv = to_i915(dev);
  9705. if (abort_flip_on_reset(crtc))
  9706. return true;
  9707. /*
  9708. * The relevant registers doen't exist on pre-ctg.
  9709. * As the flip done interrupt doesn't trigger for mmio
  9710. * flips on gmch platforms, a flip count check isn't
  9711. * really needed there. But since ctg has the registers,
  9712. * include it in the check anyway.
  9713. */
  9714. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  9715. return true;
  9716. /*
  9717. * BDW signals flip done immediately if the plane
  9718. * is disabled, even if the plane enable is already
  9719. * armed to occur at the next vblank :(
  9720. */
  9721. /*
  9722. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9723. * used the same base address. In that case the mmio flip might
  9724. * have completed, but the CS hasn't even executed the flip yet.
  9725. *
  9726. * A flip count check isn't enough as the CS might have updated
  9727. * the base address just after start of vblank, but before we
  9728. * managed to process the interrupt. This means we'd complete the
  9729. * CS flip too soon.
  9730. *
  9731. * Combining both checks should get us a good enough result. It may
  9732. * still happen that the CS flip has been executed, but has not
  9733. * yet actually completed. But in case the base address is the same
  9734. * anyway, we don't really care.
  9735. */
  9736. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9737. crtc->flip_work->gtt_offset &&
  9738. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
  9739. crtc->flip_work->flip_count);
  9740. }
  9741. static bool
  9742. __pageflip_finished_mmio(struct intel_crtc *crtc,
  9743. struct intel_flip_work *work)
  9744. {
  9745. /*
  9746. * MMIO work completes when vblank is different from
  9747. * flip_queued_vblank.
  9748. *
  9749. * Reset counter value doesn't matter, this is handled by
  9750. * i915_wait_request finishing early, so no need to handle
  9751. * reset here.
  9752. */
  9753. return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
  9754. }
  9755. static bool pageflip_finished(struct intel_crtc *crtc,
  9756. struct intel_flip_work *work)
  9757. {
  9758. if (!atomic_read(&work->pending))
  9759. return false;
  9760. smp_rmb();
  9761. if (is_mmio_work(work))
  9762. return __pageflip_finished_mmio(crtc, work);
  9763. else
  9764. return __pageflip_finished_cs(crtc, work);
  9765. }
  9766. void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
  9767. {
  9768. struct drm_device *dev = &dev_priv->drm;
  9769. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  9770. struct intel_flip_work *work;
  9771. unsigned long flags;
  9772. /* Ignore early vblank irqs */
  9773. if (!crtc)
  9774. return;
  9775. /*
  9776. * This is called both by irq handlers and the reset code (to complete
  9777. * lost pageflips) so needs the full irqsave spinlocks.
  9778. */
  9779. spin_lock_irqsave(&dev->event_lock, flags);
  9780. work = crtc->flip_work;
  9781. if (work != NULL &&
  9782. !is_mmio_work(work) &&
  9783. pageflip_finished(crtc, work))
  9784. page_flip_completed(crtc);
  9785. spin_unlock_irqrestore(&dev->event_lock, flags);
  9786. }
  9787. void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
  9788. {
  9789. struct drm_device *dev = &dev_priv->drm;
  9790. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  9791. struct intel_flip_work *work;
  9792. unsigned long flags;
  9793. /* Ignore early vblank irqs */
  9794. if (!crtc)
  9795. return;
  9796. /*
  9797. * This is called both by irq handlers and the reset code (to complete
  9798. * lost pageflips) so needs the full irqsave spinlocks.
  9799. */
  9800. spin_lock_irqsave(&dev->event_lock, flags);
  9801. work = crtc->flip_work;
  9802. if (work != NULL &&
  9803. is_mmio_work(work) &&
  9804. pageflip_finished(crtc, work))
  9805. page_flip_completed(crtc);
  9806. spin_unlock_irqrestore(&dev->event_lock, flags);
  9807. }
  9808. static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
  9809. struct intel_flip_work *work)
  9810. {
  9811. work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
  9812. /* Ensure that the work item is consistent when activating it ... */
  9813. smp_mb__before_atomic();
  9814. atomic_set(&work->pending, 1);
  9815. }
  9816. static int intel_gen2_queue_flip(struct drm_device *dev,
  9817. struct drm_crtc *crtc,
  9818. struct drm_framebuffer *fb,
  9819. struct drm_i915_gem_object *obj,
  9820. struct drm_i915_gem_request *req,
  9821. uint32_t flags)
  9822. {
  9823. struct intel_ring *ring = req->ring;
  9824. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9825. u32 flip_mask;
  9826. int ret;
  9827. ret = intel_ring_begin(req, 6);
  9828. if (ret)
  9829. return ret;
  9830. /* Can't queue multiple flips, so wait for the previous
  9831. * one to finish before executing the next.
  9832. */
  9833. if (intel_crtc->plane)
  9834. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9835. else
  9836. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9837. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9838. intel_ring_emit(ring, MI_NOOP);
  9839. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9840. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9841. intel_ring_emit(ring, fb->pitches[0]);
  9842. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  9843. intel_ring_emit(ring, 0); /* aux display base address, unused */
  9844. return 0;
  9845. }
  9846. static int intel_gen3_queue_flip(struct drm_device *dev,
  9847. struct drm_crtc *crtc,
  9848. struct drm_framebuffer *fb,
  9849. struct drm_i915_gem_object *obj,
  9850. struct drm_i915_gem_request *req,
  9851. uint32_t flags)
  9852. {
  9853. struct intel_ring *ring = req->ring;
  9854. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9855. u32 flip_mask;
  9856. int ret;
  9857. ret = intel_ring_begin(req, 6);
  9858. if (ret)
  9859. return ret;
  9860. if (intel_crtc->plane)
  9861. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9862. else
  9863. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9864. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9865. intel_ring_emit(ring, MI_NOOP);
  9866. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  9867. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9868. intel_ring_emit(ring, fb->pitches[0]);
  9869. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  9870. intel_ring_emit(ring, MI_NOOP);
  9871. return 0;
  9872. }
  9873. static int intel_gen4_queue_flip(struct drm_device *dev,
  9874. struct drm_crtc *crtc,
  9875. struct drm_framebuffer *fb,
  9876. struct drm_i915_gem_object *obj,
  9877. struct drm_i915_gem_request *req,
  9878. uint32_t flags)
  9879. {
  9880. struct intel_ring *ring = req->ring;
  9881. struct drm_i915_private *dev_priv = to_i915(dev);
  9882. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9883. uint32_t pf, pipesrc;
  9884. int ret;
  9885. ret = intel_ring_begin(req, 4);
  9886. if (ret)
  9887. return ret;
  9888. /* i965+ uses the linear or tiled offsets from the
  9889. * Display Registers (which do not change across a page-flip)
  9890. * so we need only reprogram the base address.
  9891. */
  9892. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9893. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9894. intel_ring_emit(ring, fb->pitches[0]);
  9895. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset |
  9896. intel_fb_modifier_to_tiling(fb->modifier));
  9897. /* XXX Enabling the panel-fitter across page-flip is so far
  9898. * untested on non-native modes, so ignore it for now.
  9899. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9900. */
  9901. pf = 0;
  9902. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9903. intel_ring_emit(ring, pf | pipesrc);
  9904. return 0;
  9905. }
  9906. static int intel_gen6_queue_flip(struct drm_device *dev,
  9907. struct drm_crtc *crtc,
  9908. struct drm_framebuffer *fb,
  9909. struct drm_i915_gem_object *obj,
  9910. struct drm_i915_gem_request *req,
  9911. uint32_t flags)
  9912. {
  9913. struct intel_ring *ring = req->ring;
  9914. struct drm_i915_private *dev_priv = to_i915(dev);
  9915. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9916. uint32_t pf, pipesrc;
  9917. int ret;
  9918. ret = intel_ring_begin(req, 4);
  9919. if (ret)
  9920. return ret;
  9921. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9922. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9923. intel_ring_emit(ring, fb->pitches[0] |
  9924. intel_fb_modifier_to_tiling(fb->modifier));
  9925. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  9926. /* Contrary to the suggestions in the documentation,
  9927. * "Enable Panel Fitter" does not seem to be required when page
  9928. * flipping with a non-native mode, and worse causes a normal
  9929. * modeset to fail.
  9930. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9931. */
  9932. pf = 0;
  9933. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9934. intel_ring_emit(ring, pf | pipesrc);
  9935. return 0;
  9936. }
  9937. static int intel_gen7_queue_flip(struct drm_device *dev,
  9938. struct drm_crtc *crtc,
  9939. struct drm_framebuffer *fb,
  9940. struct drm_i915_gem_object *obj,
  9941. struct drm_i915_gem_request *req,
  9942. uint32_t flags)
  9943. {
  9944. struct drm_i915_private *dev_priv = to_i915(dev);
  9945. struct intel_ring *ring = req->ring;
  9946. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9947. uint32_t plane_bit = 0;
  9948. int len, ret;
  9949. switch (intel_crtc->plane) {
  9950. case PLANE_A:
  9951. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9952. break;
  9953. case PLANE_B:
  9954. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9955. break;
  9956. case PLANE_C:
  9957. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9958. break;
  9959. default:
  9960. WARN_ONCE(1, "unknown plane in flip command\n");
  9961. return -ENODEV;
  9962. }
  9963. len = 4;
  9964. if (req->engine->id == RCS) {
  9965. len += 6;
  9966. /*
  9967. * On Gen 8, SRM is now taking an extra dword to accommodate
  9968. * 48bits addresses, and we need a NOOP for the batch size to
  9969. * stay even.
  9970. */
  9971. if (IS_GEN8(dev_priv))
  9972. len += 2;
  9973. }
  9974. /*
  9975. * BSpec MI_DISPLAY_FLIP for IVB:
  9976. * "The full packet must be contained within the same cache line."
  9977. *
  9978. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9979. * cacheline, if we ever start emitting more commands before
  9980. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9981. * then do the cacheline alignment, and finally emit the
  9982. * MI_DISPLAY_FLIP.
  9983. */
  9984. ret = intel_ring_cacheline_align(req);
  9985. if (ret)
  9986. return ret;
  9987. ret = intel_ring_begin(req, len);
  9988. if (ret)
  9989. return ret;
  9990. /* Unmask the flip-done completion message. Note that the bspec says that
  9991. * we should do this for both the BCS and RCS, and that we must not unmask
  9992. * more than one flip event at any time (or ensure that one flip message
  9993. * can be sent by waiting for flip-done prior to queueing new flips).
  9994. * Experimentation says that BCS works despite DERRMR masking all
  9995. * flip-done completion events and that unmasking all planes at once
  9996. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9997. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9998. */
  9999. if (req->engine->id == RCS) {
  10000. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  10001. intel_ring_emit_reg(ring, DERRMR);
  10002. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  10003. DERRMR_PIPEB_PRI_FLIP_DONE |
  10004. DERRMR_PIPEC_PRI_FLIP_DONE));
  10005. if (IS_GEN8(dev_priv))
  10006. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
  10007. MI_SRM_LRM_GLOBAL_GTT);
  10008. else
  10009. intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
  10010. MI_SRM_LRM_GLOBAL_GTT);
  10011. intel_ring_emit_reg(ring, DERRMR);
  10012. intel_ring_emit(ring,
  10013. i915_ggtt_offset(req->engine->scratch) + 256);
  10014. if (IS_GEN8(dev_priv)) {
  10015. intel_ring_emit(ring, 0);
  10016. intel_ring_emit(ring, MI_NOOP);
  10017. }
  10018. }
  10019. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  10020. intel_ring_emit(ring, fb->pitches[0] |
  10021. intel_fb_modifier_to_tiling(fb->modifier));
  10022. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  10023. intel_ring_emit(ring, (MI_NOOP));
  10024. return 0;
  10025. }
  10026. static bool use_mmio_flip(struct intel_engine_cs *engine,
  10027. struct drm_i915_gem_object *obj)
  10028. {
  10029. /*
  10030. * This is not being used for older platforms, because
  10031. * non-availability of flip done interrupt forces us to use
  10032. * CS flips. Older platforms derive flip done using some clever
  10033. * tricks involving the flip_pending status bits and vblank irqs.
  10034. * So using MMIO flips there would disrupt this mechanism.
  10035. */
  10036. if (engine == NULL)
  10037. return true;
  10038. if (INTEL_GEN(engine->i915) < 5)
  10039. return false;
  10040. if (i915.use_mmio_flip < 0)
  10041. return false;
  10042. else if (i915.use_mmio_flip > 0)
  10043. return true;
  10044. else if (i915.enable_execlists)
  10045. return true;
  10046. return engine != i915_gem_object_last_write_engine(obj);
  10047. }
  10048. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
  10049. unsigned int rotation,
  10050. struct intel_flip_work *work)
  10051. {
  10052. struct drm_device *dev = intel_crtc->base.dev;
  10053. struct drm_i915_private *dev_priv = to_i915(dev);
  10054. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  10055. const enum pipe pipe = intel_crtc->pipe;
  10056. u32 ctl, stride = skl_plane_stride(fb, 0, rotation);
  10057. ctl = I915_READ(PLANE_CTL(pipe, 0));
  10058. ctl &= ~PLANE_CTL_TILED_MASK;
  10059. switch (fb->modifier) {
  10060. case DRM_FORMAT_MOD_NONE:
  10061. break;
  10062. case I915_FORMAT_MOD_X_TILED:
  10063. ctl |= PLANE_CTL_TILED_X;
  10064. break;
  10065. case I915_FORMAT_MOD_Y_TILED:
  10066. ctl |= PLANE_CTL_TILED_Y;
  10067. break;
  10068. case I915_FORMAT_MOD_Yf_TILED:
  10069. ctl |= PLANE_CTL_TILED_YF;
  10070. break;
  10071. default:
  10072. MISSING_CASE(fb->modifier);
  10073. }
  10074. /*
  10075. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  10076. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  10077. */
  10078. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  10079. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  10080. I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
  10081. POSTING_READ(PLANE_SURF(pipe, 0));
  10082. }
  10083. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
  10084. struct intel_flip_work *work)
  10085. {
  10086. struct drm_device *dev = intel_crtc->base.dev;
  10087. struct drm_i915_private *dev_priv = to_i915(dev);
  10088. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  10089. i915_reg_t reg = DSPCNTR(intel_crtc->plane);
  10090. u32 dspcntr;
  10091. dspcntr = I915_READ(reg);
  10092. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  10093. dspcntr |= DISPPLANE_TILED;
  10094. else
  10095. dspcntr &= ~DISPPLANE_TILED;
  10096. I915_WRITE(reg, dspcntr);
  10097. I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
  10098. POSTING_READ(DSPSURF(intel_crtc->plane));
  10099. }
  10100. static void intel_mmio_flip_work_func(struct work_struct *w)
  10101. {
  10102. struct intel_flip_work *work =
  10103. container_of(w, struct intel_flip_work, mmio_work);
  10104. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  10105. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  10106. struct intel_framebuffer *intel_fb =
  10107. to_intel_framebuffer(crtc->base.primary->fb);
  10108. struct drm_i915_gem_object *obj = intel_fb->obj;
  10109. WARN_ON(i915_gem_object_wait(obj, 0, MAX_SCHEDULE_TIMEOUT, NULL) < 0);
  10110. intel_pipe_update_start(crtc);
  10111. if (INTEL_GEN(dev_priv) >= 9)
  10112. skl_do_mmio_flip(crtc, work->rotation, work);
  10113. else
  10114. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  10115. ilk_do_mmio_flip(crtc, work);
  10116. intel_pipe_update_end(crtc, work);
  10117. }
  10118. static int intel_default_queue_flip(struct drm_device *dev,
  10119. struct drm_crtc *crtc,
  10120. struct drm_framebuffer *fb,
  10121. struct drm_i915_gem_object *obj,
  10122. struct drm_i915_gem_request *req,
  10123. uint32_t flags)
  10124. {
  10125. return -ENODEV;
  10126. }
  10127. static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
  10128. struct intel_crtc *intel_crtc,
  10129. struct intel_flip_work *work)
  10130. {
  10131. u32 addr, vblank;
  10132. if (!atomic_read(&work->pending))
  10133. return false;
  10134. smp_rmb();
  10135. vblank = intel_crtc_get_vblank_counter(intel_crtc);
  10136. if (work->flip_ready_vblank == 0) {
  10137. if (work->flip_queued_req &&
  10138. !i915_gem_request_completed(work->flip_queued_req))
  10139. return false;
  10140. work->flip_ready_vblank = vblank;
  10141. }
  10142. if (vblank - work->flip_ready_vblank < 3)
  10143. return false;
  10144. /* Potential stall - if we see that the flip has happened,
  10145. * assume a missed interrupt. */
  10146. if (INTEL_GEN(dev_priv) >= 4)
  10147. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  10148. else
  10149. addr = I915_READ(DSPADDR(intel_crtc->plane));
  10150. /* There is a potential issue here with a false positive after a flip
  10151. * to the same address. We could address this by checking for a
  10152. * non-incrementing frame counter.
  10153. */
  10154. return addr == work->gtt_offset;
  10155. }
  10156. void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
  10157. {
  10158. struct drm_device *dev = &dev_priv->drm;
  10159. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  10160. struct intel_flip_work *work;
  10161. WARN_ON(!in_interrupt());
  10162. if (crtc == NULL)
  10163. return;
  10164. spin_lock(&dev->event_lock);
  10165. work = crtc->flip_work;
  10166. if (work != NULL && !is_mmio_work(work) &&
  10167. __pageflip_stall_check_cs(dev_priv, crtc, work)) {
  10168. WARN_ONCE(1,
  10169. "Kicking stuck page flip: queued at %d, now %d\n",
  10170. work->flip_queued_vblank, intel_crtc_get_vblank_counter(crtc));
  10171. page_flip_completed(crtc);
  10172. work = NULL;
  10173. }
  10174. if (work != NULL && !is_mmio_work(work) &&
  10175. intel_crtc_get_vblank_counter(crtc) - work->flip_queued_vblank > 1)
  10176. intel_queue_rps_boost_for_request(work->flip_queued_req);
  10177. spin_unlock(&dev->event_lock);
  10178. }
  10179. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  10180. struct drm_framebuffer *fb,
  10181. struct drm_pending_vblank_event *event,
  10182. uint32_t page_flip_flags)
  10183. {
  10184. struct drm_device *dev = crtc->dev;
  10185. struct drm_i915_private *dev_priv = to_i915(dev);
  10186. struct drm_framebuffer *old_fb = crtc->primary->fb;
  10187. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10188. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10189. struct drm_plane *primary = crtc->primary;
  10190. enum pipe pipe = intel_crtc->pipe;
  10191. struct intel_flip_work *work;
  10192. struct intel_engine_cs *engine;
  10193. bool mmio_flip;
  10194. struct drm_i915_gem_request *request;
  10195. struct i915_vma *vma;
  10196. int ret;
  10197. /*
  10198. * drm_mode_page_flip_ioctl() should already catch this, but double
  10199. * check to be safe. In the future we may enable pageflipping from
  10200. * a disabled primary plane.
  10201. */
  10202. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  10203. return -EBUSY;
  10204. /* Can't change pixel format via MI display flips. */
  10205. if (fb->format != crtc->primary->fb->format)
  10206. return -EINVAL;
  10207. /*
  10208. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  10209. * Note that pitch changes could also affect these register.
  10210. */
  10211. if (INTEL_GEN(dev_priv) > 3 &&
  10212. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  10213. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  10214. return -EINVAL;
  10215. if (i915_terminally_wedged(&dev_priv->gpu_error))
  10216. goto out_hang;
  10217. work = kzalloc(sizeof(*work), GFP_KERNEL);
  10218. if (work == NULL)
  10219. return -ENOMEM;
  10220. work->event = event;
  10221. work->crtc = crtc;
  10222. work->old_fb = old_fb;
  10223. INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
  10224. ret = drm_crtc_vblank_get(crtc);
  10225. if (ret)
  10226. goto free_work;
  10227. /* We borrow the event spin lock for protecting flip_work */
  10228. spin_lock_irq(&dev->event_lock);
  10229. if (intel_crtc->flip_work) {
  10230. /* Before declaring the flip queue wedged, check if
  10231. * the hardware completed the operation behind our backs.
  10232. */
  10233. if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
  10234. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  10235. page_flip_completed(intel_crtc);
  10236. } else {
  10237. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  10238. spin_unlock_irq(&dev->event_lock);
  10239. drm_crtc_vblank_put(crtc);
  10240. kfree(work);
  10241. return -EBUSY;
  10242. }
  10243. }
  10244. intel_crtc->flip_work = work;
  10245. spin_unlock_irq(&dev->event_lock);
  10246. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  10247. flush_workqueue(dev_priv->wq);
  10248. /* Reference the objects for the scheduled work. */
  10249. drm_framebuffer_reference(work->old_fb);
  10250. crtc->primary->fb = fb;
  10251. update_state_fb(crtc->primary);
  10252. work->pending_flip_obj = i915_gem_object_get(obj);
  10253. ret = i915_mutex_lock_interruptible(dev);
  10254. if (ret)
  10255. goto cleanup;
  10256. intel_crtc->reset_count = i915_reset_count(&dev_priv->gpu_error);
  10257. if (i915_reset_in_progress_or_wedged(&dev_priv->gpu_error)) {
  10258. ret = -EIO;
  10259. goto unlock;
  10260. }
  10261. atomic_inc(&intel_crtc->unpin_work_count);
  10262. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  10263. work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
  10264. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  10265. engine = dev_priv->engine[BCS];
  10266. if (fb->modifier != old_fb->modifier)
  10267. /* vlv: DISPLAY_FLIP fails to change tiling */
  10268. engine = NULL;
  10269. } else if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
  10270. engine = dev_priv->engine[BCS];
  10271. } else if (INTEL_GEN(dev_priv) >= 7) {
  10272. engine = i915_gem_object_last_write_engine(obj);
  10273. if (engine == NULL || engine->id != RCS)
  10274. engine = dev_priv->engine[BCS];
  10275. } else {
  10276. engine = dev_priv->engine[RCS];
  10277. }
  10278. mmio_flip = use_mmio_flip(engine, obj);
  10279. vma = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  10280. if (IS_ERR(vma)) {
  10281. ret = PTR_ERR(vma);
  10282. goto cleanup_pending;
  10283. }
  10284. work->old_vma = to_intel_plane_state(primary->state)->vma;
  10285. to_intel_plane_state(primary->state)->vma = vma;
  10286. work->gtt_offset = i915_ggtt_offset(vma) + intel_crtc->dspaddr_offset;
  10287. work->rotation = crtc->primary->state->rotation;
  10288. /*
  10289. * There's the potential that the next frame will not be compatible with
  10290. * FBC, so we want to call pre_update() before the actual page flip.
  10291. * The problem is that pre_update() caches some information about the fb
  10292. * object, so we want to do this only after the object is pinned. Let's
  10293. * be on the safe side and do this immediately before scheduling the
  10294. * flip.
  10295. */
  10296. intel_fbc_pre_update(intel_crtc, intel_crtc->config,
  10297. to_intel_plane_state(primary->state));
  10298. if (mmio_flip) {
  10299. INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
  10300. queue_work(system_unbound_wq, &work->mmio_work);
  10301. } else {
  10302. request = i915_gem_request_alloc(engine,
  10303. dev_priv->kernel_context);
  10304. if (IS_ERR(request)) {
  10305. ret = PTR_ERR(request);
  10306. goto cleanup_unpin;
  10307. }
  10308. ret = i915_gem_request_await_object(request, obj, false);
  10309. if (ret)
  10310. goto cleanup_request;
  10311. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  10312. page_flip_flags);
  10313. if (ret)
  10314. goto cleanup_request;
  10315. intel_mark_page_flip_active(intel_crtc, work);
  10316. work->flip_queued_req = i915_gem_request_get(request);
  10317. i915_add_request_no_flush(request);
  10318. }
  10319. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  10320. i915_gem_track_fb(intel_fb_obj(old_fb), obj,
  10321. to_intel_plane(primary)->frontbuffer_bit);
  10322. mutex_unlock(&dev->struct_mutex);
  10323. intel_frontbuffer_flip_prepare(to_i915(dev),
  10324. to_intel_plane(primary)->frontbuffer_bit);
  10325. trace_i915_flip_request(intel_crtc->plane, obj);
  10326. return 0;
  10327. cleanup_request:
  10328. i915_add_request_no_flush(request);
  10329. cleanup_unpin:
  10330. to_intel_plane_state(primary->state)->vma = work->old_vma;
  10331. intel_unpin_fb_vma(vma);
  10332. cleanup_pending:
  10333. atomic_dec(&intel_crtc->unpin_work_count);
  10334. unlock:
  10335. mutex_unlock(&dev->struct_mutex);
  10336. cleanup:
  10337. crtc->primary->fb = old_fb;
  10338. update_state_fb(crtc->primary);
  10339. i915_gem_object_put(obj);
  10340. drm_framebuffer_unreference(work->old_fb);
  10341. spin_lock_irq(&dev->event_lock);
  10342. intel_crtc->flip_work = NULL;
  10343. spin_unlock_irq(&dev->event_lock);
  10344. drm_crtc_vblank_put(crtc);
  10345. free_work:
  10346. kfree(work);
  10347. if (ret == -EIO) {
  10348. struct drm_atomic_state *state;
  10349. struct drm_plane_state *plane_state;
  10350. out_hang:
  10351. state = drm_atomic_state_alloc(dev);
  10352. if (!state)
  10353. return -ENOMEM;
  10354. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  10355. retry:
  10356. plane_state = drm_atomic_get_plane_state(state, primary);
  10357. ret = PTR_ERR_OR_ZERO(plane_state);
  10358. if (!ret) {
  10359. drm_atomic_set_fb_for_plane(plane_state, fb);
  10360. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  10361. if (!ret)
  10362. ret = drm_atomic_commit(state);
  10363. }
  10364. if (ret == -EDEADLK) {
  10365. drm_modeset_backoff(state->acquire_ctx);
  10366. drm_atomic_state_clear(state);
  10367. goto retry;
  10368. }
  10369. drm_atomic_state_put(state);
  10370. if (ret == 0 && event) {
  10371. spin_lock_irq(&dev->event_lock);
  10372. drm_crtc_send_vblank_event(crtc, event);
  10373. spin_unlock_irq(&dev->event_lock);
  10374. }
  10375. }
  10376. return ret;
  10377. }
  10378. /**
  10379. * intel_wm_need_update - Check whether watermarks need updating
  10380. * @plane: drm plane
  10381. * @state: new plane state
  10382. *
  10383. * Check current plane state versus the new one to determine whether
  10384. * watermarks need to be recalculated.
  10385. *
  10386. * Returns true or false.
  10387. */
  10388. static bool intel_wm_need_update(struct drm_plane *plane,
  10389. struct drm_plane_state *state)
  10390. {
  10391. struct intel_plane_state *new = to_intel_plane_state(state);
  10392. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  10393. /* Update watermarks on tiling or size changes. */
  10394. if (new->base.visible != cur->base.visible)
  10395. return true;
  10396. if (!cur->base.fb || !new->base.fb)
  10397. return false;
  10398. if (cur->base.fb->modifier != new->base.fb->modifier ||
  10399. cur->base.rotation != new->base.rotation ||
  10400. drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
  10401. drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
  10402. drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
  10403. drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
  10404. return true;
  10405. return false;
  10406. }
  10407. static bool needs_scaling(struct intel_plane_state *state)
  10408. {
  10409. int src_w = drm_rect_width(&state->base.src) >> 16;
  10410. int src_h = drm_rect_height(&state->base.src) >> 16;
  10411. int dst_w = drm_rect_width(&state->base.dst);
  10412. int dst_h = drm_rect_height(&state->base.dst);
  10413. return (src_w != dst_w || src_h != dst_h);
  10414. }
  10415. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  10416. struct drm_plane_state *plane_state)
  10417. {
  10418. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  10419. struct drm_crtc *crtc = crtc_state->crtc;
  10420. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10421. struct drm_plane *plane = plane_state->plane;
  10422. struct drm_device *dev = crtc->dev;
  10423. struct drm_i915_private *dev_priv = to_i915(dev);
  10424. struct intel_plane_state *old_plane_state =
  10425. to_intel_plane_state(plane->state);
  10426. bool mode_changed = needs_modeset(crtc_state);
  10427. bool was_crtc_enabled = crtc->state->active;
  10428. bool is_crtc_enabled = crtc_state->active;
  10429. bool turn_off, turn_on, visible, was_visible;
  10430. struct drm_framebuffer *fb = plane_state->fb;
  10431. int ret;
  10432. if (INTEL_GEN(dev_priv) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
  10433. ret = skl_update_scaler_plane(
  10434. to_intel_crtc_state(crtc_state),
  10435. to_intel_plane_state(plane_state));
  10436. if (ret)
  10437. return ret;
  10438. }
  10439. was_visible = old_plane_state->base.visible;
  10440. visible = plane_state->visible;
  10441. if (!was_crtc_enabled && WARN_ON(was_visible))
  10442. was_visible = false;
  10443. /*
  10444. * Visibility is calculated as if the crtc was on, but
  10445. * after scaler setup everything depends on it being off
  10446. * when the crtc isn't active.
  10447. *
  10448. * FIXME this is wrong for watermarks. Watermarks should also
  10449. * be computed as if the pipe would be active. Perhaps move
  10450. * per-plane wm computation to the .check_plane() hook, and
  10451. * only combine the results from all planes in the current place?
  10452. */
  10453. if (!is_crtc_enabled)
  10454. plane_state->visible = visible = false;
  10455. if (!was_visible && !visible)
  10456. return 0;
  10457. if (fb != old_plane_state->base.fb)
  10458. pipe_config->fb_changed = true;
  10459. turn_off = was_visible && (!visible || mode_changed);
  10460. turn_on = visible && (!was_visible || mode_changed);
  10461. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  10462. intel_crtc->base.base.id,
  10463. intel_crtc->base.name,
  10464. plane->base.id, plane->name,
  10465. fb ? fb->base.id : -1);
  10466. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  10467. plane->base.id, plane->name,
  10468. was_visible, visible,
  10469. turn_off, turn_on, mode_changed);
  10470. if (turn_on) {
  10471. pipe_config->update_wm_pre = true;
  10472. /* must disable cxsr around plane enable/disable */
  10473. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  10474. pipe_config->disable_cxsr = true;
  10475. } else if (turn_off) {
  10476. pipe_config->update_wm_post = true;
  10477. /* must disable cxsr around plane enable/disable */
  10478. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  10479. pipe_config->disable_cxsr = true;
  10480. } else if (intel_wm_need_update(plane, plane_state)) {
  10481. /* FIXME bollocks */
  10482. pipe_config->update_wm_pre = true;
  10483. pipe_config->update_wm_post = true;
  10484. }
  10485. /* Pre-gen9 platforms need two-step watermark updates */
  10486. if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
  10487. INTEL_GEN(dev_priv) < 9 && dev_priv->display.optimize_watermarks)
  10488. to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
  10489. if (visible || was_visible)
  10490. pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
  10491. /*
  10492. * WaCxSRDisabledForSpriteScaling:ivb
  10493. *
  10494. * cstate->update_wm was already set above, so this flag will
  10495. * take effect when we commit and program watermarks.
  10496. */
  10497. if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev_priv) &&
  10498. needs_scaling(to_intel_plane_state(plane_state)) &&
  10499. !needs_scaling(old_plane_state))
  10500. pipe_config->disable_lp_wm = true;
  10501. return 0;
  10502. }
  10503. static bool encoders_cloneable(const struct intel_encoder *a,
  10504. const struct intel_encoder *b)
  10505. {
  10506. /* masks could be asymmetric, so check both ways */
  10507. return a == b || (a->cloneable & (1 << b->type) &&
  10508. b->cloneable & (1 << a->type));
  10509. }
  10510. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  10511. struct intel_crtc *crtc,
  10512. struct intel_encoder *encoder)
  10513. {
  10514. struct intel_encoder *source_encoder;
  10515. struct drm_connector *connector;
  10516. struct drm_connector_state *connector_state;
  10517. int i;
  10518. for_each_connector_in_state(state, connector, connector_state, i) {
  10519. if (connector_state->crtc != &crtc->base)
  10520. continue;
  10521. source_encoder =
  10522. to_intel_encoder(connector_state->best_encoder);
  10523. if (!encoders_cloneable(encoder, source_encoder))
  10524. return false;
  10525. }
  10526. return true;
  10527. }
  10528. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  10529. struct drm_crtc_state *crtc_state)
  10530. {
  10531. struct drm_device *dev = crtc->dev;
  10532. struct drm_i915_private *dev_priv = to_i915(dev);
  10533. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10534. struct intel_crtc_state *pipe_config =
  10535. to_intel_crtc_state(crtc_state);
  10536. struct drm_atomic_state *state = crtc_state->state;
  10537. int ret;
  10538. bool mode_changed = needs_modeset(crtc_state);
  10539. if (mode_changed && !crtc_state->active)
  10540. pipe_config->update_wm_post = true;
  10541. if (mode_changed && crtc_state->enable &&
  10542. dev_priv->display.crtc_compute_clock &&
  10543. !WARN_ON(pipe_config->shared_dpll)) {
  10544. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  10545. pipe_config);
  10546. if (ret)
  10547. return ret;
  10548. }
  10549. if (crtc_state->color_mgmt_changed) {
  10550. ret = intel_color_check(crtc, crtc_state);
  10551. if (ret)
  10552. return ret;
  10553. /*
  10554. * Changing color management on Intel hardware is
  10555. * handled as part of planes update.
  10556. */
  10557. crtc_state->planes_changed = true;
  10558. }
  10559. ret = 0;
  10560. if (dev_priv->display.compute_pipe_wm) {
  10561. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  10562. if (ret) {
  10563. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  10564. return ret;
  10565. }
  10566. }
  10567. if (dev_priv->display.compute_intermediate_wm &&
  10568. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  10569. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  10570. return 0;
  10571. /*
  10572. * Calculate 'intermediate' watermarks that satisfy both the
  10573. * old state and the new state. We can program these
  10574. * immediately.
  10575. */
  10576. ret = dev_priv->display.compute_intermediate_wm(dev,
  10577. intel_crtc,
  10578. pipe_config);
  10579. if (ret) {
  10580. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  10581. return ret;
  10582. }
  10583. } else if (dev_priv->display.compute_intermediate_wm) {
  10584. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  10585. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  10586. }
  10587. if (INTEL_GEN(dev_priv) >= 9) {
  10588. if (mode_changed)
  10589. ret = skl_update_scaler_crtc(pipe_config);
  10590. if (!ret)
  10591. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  10592. pipe_config);
  10593. }
  10594. return ret;
  10595. }
  10596. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  10597. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  10598. .atomic_begin = intel_begin_crtc_commit,
  10599. .atomic_flush = intel_finish_crtc_commit,
  10600. .atomic_check = intel_crtc_atomic_check,
  10601. };
  10602. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  10603. {
  10604. struct intel_connector *connector;
  10605. for_each_intel_connector(dev, connector) {
  10606. if (connector->base.state->crtc)
  10607. drm_connector_unreference(&connector->base);
  10608. if (connector->base.encoder) {
  10609. connector->base.state->best_encoder =
  10610. connector->base.encoder;
  10611. connector->base.state->crtc =
  10612. connector->base.encoder->crtc;
  10613. drm_connector_reference(&connector->base);
  10614. } else {
  10615. connector->base.state->best_encoder = NULL;
  10616. connector->base.state->crtc = NULL;
  10617. }
  10618. }
  10619. }
  10620. static void
  10621. connected_sink_compute_bpp(struct intel_connector *connector,
  10622. struct intel_crtc_state *pipe_config)
  10623. {
  10624. const struct drm_display_info *info = &connector->base.display_info;
  10625. int bpp = pipe_config->pipe_bpp;
  10626. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  10627. connector->base.base.id,
  10628. connector->base.name);
  10629. /* Don't use an invalid EDID bpc value */
  10630. if (info->bpc != 0 && info->bpc * 3 < bpp) {
  10631. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  10632. bpp, info->bpc * 3);
  10633. pipe_config->pipe_bpp = info->bpc * 3;
  10634. }
  10635. /* Clamp bpp to 8 on screens without EDID 1.4 */
  10636. if (info->bpc == 0 && bpp > 24) {
  10637. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  10638. bpp);
  10639. pipe_config->pipe_bpp = 24;
  10640. }
  10641. }
  10642. static int
  10643. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  10644. struct intel_crtc_state *pipe_config)
  10645. {
  10646. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  10647. struct drm_atomic_state *state;
  10648. struct drm_connector *connector;
  10649. struct drm_connector_state *connector_state;
  10650. int bpp, i;
  10651. if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  10652. IS_CHERRYVIEW(dev_priv)))
  10653. bpp = 10*3;
  10654. else if (INTEL_GEN(dev_priv) >= 5)
  10655. bpp = 12*3;
  10656. else
  10657. bpp = 8*3;
  10658. pipe_config->pipe_bpp = bpp;
  10659. state = pipe_config->base.state;
  10660. /* Clamp display bpp to EDID value */
  10661. for_each_connector_in_state(state, connector, connector_state, i) {
  10662. if (connector_state->crtc != &crtc->base)
  10663. continue;
  10664. connected_sink_compute_bpp(to_intel_connector(connector),
  10665. pipe_config);
  10666. }
  10667. return bpp;
  10668. }
  10669. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  10670. {
  10671. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  10672. "type: 0x%x flags: 0x%x\n",
  10673. mode->crtc_clock,
  10674. mode->crtc_hdisplay, mode->crtc_hsync_start,
  10675. mode->crtc_hsync_end, mode->crtc_htotal,
  10676. mode->crtc_vdisplay, mode->crtc_vsync_start,
  10677. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  10678. }
  10679. static inline void
  10680. intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
  10681. unsigned int lane_count, struct intel_link_m_n *m_n)
  10682. {
  10683. DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10684. id, lane_count,
  10685. m_n->gmch_m, m_n->gmch_n,
  10686. m_n->link_m, m_n->link_n, m_n->tu);
  10687. }
  10688. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  10689. struct intel_crtc_state *pipe_config,
  10690. const char *context)
  10691. {
  10692. struct drm_device *dev = crtc->base.dev;
  10693. struct drm_i915_private *dev_priv = to_i915(dev);
  10694. struct drm_plane *plane;
  10695. struct intel_plane *intel_plane;
  10696. struct intel_plane_state *state;
  10697. struct drm_framebuffer *fb;
  10698. DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
  10699. crtc->base.base.id, crtc->base.name, context);
  10700. DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
  10701. transcoder_name(pipe_config->cpu_transcoder),
  10702. pipe_config->pipe_bpp, pipe_config->dither);
  10703. if (pipe_config->has_pch_encoder)
  10704. intel_dump_m_n_config(pipe_config, "fdi",
  10705. pipe_config->fdi_lanes,
  10706. &pipe_config->fdi_m_n);
  10707. if (intel_crtc_has_dp_encoder(pipe_config)) {
  10708. intel_dump_m_n_config(pipe_config, "dp m_n",
  10709. pipe_config->lane_count, &pipe_config->dp_m_n);
  10710. if (pipe_config->has_drrs)
  10711. intel_dump_m_n_config(pipe_config, "dp m2_n2",
  10712. pipe_config->lane_count,
  10713. &pipe_config->dp_m2_n2);
  10714. }
  10715. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  10716. pipe_config->has_audio, pipe_config->has_infoframe);
  10717. DRM_DEBUG_KMS("requested mode:\n");
  10718. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  10719. DRM_DEBUG_KMS("adjusted mode:\n");
  10720. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  10721. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  10722. DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d\n",
  10723. pipe_config->port_clock,
  10724. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  10725. if (INTEL_GEN(dev_priv) >= 9)
  10726. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  10727. crtc->num_scalers,
  10728. pipe_config->scaler_state.scaler_users,
  10729. pipe_config->scaler_state.scaler_id);
  10730. if (HAS_GMCH_DISPLAY(dev_priv))
  10731. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10732. pipe_config->gmch_pfit.control,
  10733. pipe_config->gmch_pfit.pgm_ratios,
  10734. pipe_config->gmch_pfit.lvds_border_bits);
  10735. else
  10736. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10737. pipe_config->pch_pfit.pos,
  10738. pipe_config->pch_pfit.size,
  10739. enableddisabled(pipe_config->pch_pfit.enabled));
  10740. DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
  10741. pipe_config->ips_enabled, pipe_config->double_wide);
  10742. intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
  10743. DRM_DEBUG_KMS("planes on this crtc\n");
  10744. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10745. struct drm_format_name_buf format_name;
  10746. intel_plane = to_intel_plane(plane);
  10747. if (intel_plane->pipe != crtc->pipe)
  10748. continue;
  10749. state = to_intel_plane_state(plane->state);
  10750. fb = state->base.fb;
  10751. if (!fb) {
  10752. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  10753. plane->base.id, plane->name, state->scaler_id);
  10754. continue;
  10755. }
  10756. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
  10757. plane->base.id, plane->name,
  10758. fb->base.id, fb->width, fb->height,
  10759. drm_get_format_name(fb->format->format, &format_name));
  10760. if (INTEL_GEN(dev_priv) >= 9)
  10761. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  10762. state->scaler_id,
  10763. state->base.src.x1 >> 16,
  10764. state->base.src.y1 >> 16,
  10765. drm_rect_width(&state->base.src) >> 16,
  10766. drm_rect_height(&state->base.src) >> 16,
  10767. state->base.dst.x1, state->base.dst.y1,
  10768. drm_rect_width(&state->base.dst),
  10769. drm_rect_height(&state->base.dst));
  10770. }
  10771. }
  10772. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10773. {
  10774. struct drm_device *dev = state->dev;
  10775. struct drm_connector *connector;
  10776. unsigned int used_ports = 0;
  10777. unsigned int used_mst_ports = 0;
  10778. /*
  10779. * Walk the connector list instead of the encoder
  10780. * list to detect the problem on ddi platforms
  10781. * where there's just one encoder per digital port.
  10782. */
  10783. drm_for_each_connector(connector, dev) {
  10784. struct drm_connector_state *connector_state;
  10785. struct intel_encoder *encoder;
  10786. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  10787. if (!connector_state)
  10788. connector_state = connector->state;
  10789. if (!connector_state->best_encoder)
  10790. continue;
  10791. encoder = to_intel_encoder(connector_state->best_encoder);
  10792. WARN_ON(!connector_state->crtc);
  10793. switch (encoder->type) {
  10794. unsigned int port_mask;
  10795. case INTEL_OUTPUT_UNKNOWN:
  10796. if (WARN_ON(!HAS_DDI(to_i915(dev))))
  10797. break;
  10798. case INTEL_OUTPUT_DP:
  10799. case INTEL_OUTPUT_HDMI:
  10800. case INTEL_OUTPUT_EDP:
  10801. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10802. /* the same port mustn't appear more than once */
  10803. if (used_ports & port_mask)
  10804. return false;
  10805. used_ports |= port_mask;
  10806. break;
  10807. case INTEL_OUTPUT_DP_MST:
  10808. used_mst_ports |=
  10809. 1 << enc_to_mst(&encoder->base)->primary->port;
  10810. break;
  10811. default:
  10812. break;
  10813. }
  10814. }
  10815. /* can't mix MST and SST/HDMI on the same port */
  10816. if (used_ports & used_mst_ports)
  10817. return false;
  10818. return true;
  10819. }
  10820. static void
  10821. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10822. {
  10823. struct drm_crtc_state tmp_state;
  10824. struct intel_crtc_scaler_state scaler_state;
  10825. struct intel_dpll_hw_state dpll_hw_state;
  10826. struct intel_shared_dpll *shared_dpll;
  10827. bool force_thru;
  10828. /* FIXME: before the switch to atomic started, a new pipe_config was
  10829. * kzalloc'd. Code that depends on any field being zero should be
  10830. * fixed, so that the crtc_state can be safely duplicated. For now,
  10831. * only fields that are know to not cause problems are preserved. */
  10832. tmp_state = crtc_state->base;
  10833. scaler_state = crtc_state->scaler_state;
  10834. shared_dpll = crtc_state->shared_dpll;
  10835. dpll_hw_state = crtc_state->dpll_hw_state;
  10836. force_thru = crtc_state->pch_pfit.force_thru;
  10837. memset(crtc_state, 0, sizeof *crtc_state);
  10838. crtc_state->base = tmp_state;
  10839. crtc_state->scaler_state = scaler_state;
  10840. crtc_state->shared_dpll = shared_dpll;
  10841. crtc_state->dpll_hw_state = dpll_hw_state;
  10842. crtc_state->pch_pfit.force_thru = force_thru;
  10843. }
  10844. static int
  10845. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10846. struct intel_crtc_state *pipe_config)
  10847. {
  10848. struct drm_atomic_state *state = pipe_config->base.state;
  10849. struct intel_encoder *encoder;
  10850. struct drm_connector *connector;
  10851. struct drm_connector_state *connector_state;
  10852. int base_bpp, ret = -EINVAL;
  10853. int i;
  10854. bool retry = true;
  10855. clear_intel_crtc_state(pipe_config);
  10856. pipe_config->cpu_transcoder =
  10857. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10858. /*
  10859. * Sanitize sync polarity flags based on requested ones. If neither
  10860. * positive or negative polarity is requested, treat this as meaning
  10861. * negative polarity.
  10862. */
  10863. if (!(pipe_config->base.adjusted_mode.flags &
  10864. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10865. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10866. if (!(pipe_config->base.adjusted_mode.flags &
  10867. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10868. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10869. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10870. pipe_config);
  10871. if (base_bpp < 0)
  10872. goto fail;
  10873. /*
  10874. * Determine the real pipe dimensions. Note that stereo modes can
  10875. * increase the actual pipe size due to the frame doubling and
  10876. * insertion of additional space for blanks between the frame. This
  10877. * is stored in the crtc timings. We use the requested mode to do this
  10878. * computation to clearly distinguish it from the adjusted mode, which
  10879. * can be changed by the connectors in the below retry loop.
  10880. */
  10881. drm_mode_get_hv_timing(&pipe_config->base.mode,
  10882. &pipe_config->pipe_src_w,
  10883. &pipe_config->pipe_src_h);
  10884. for_each_connector_in_state(state, connector, connector_state, i) {
  10885. if (connector_state->crtc != crtc)
  10886. continue;
  10887. encoder = to_intel_encoder(connector_state->best_encoder);
  10888. if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
  10889. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  10890. goto fail;
  10891. }
  10892. /*
  10893. * Determine output_types before calling the .compute_config()
  10894. * hooks so that the hooks can use this information safely.
  10895. */
  10896. pipe_config->output_types |= 1 << encoder->type;
  10897. }
  10898. encoder_retry:
  10899. /* Ensure the port clock defaults are reset when retrying. */
  10900. pipe_config->port_clock = 0;
  10901. pipe_config->pixel_multiplier = 1;
  10902. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10903. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10904. CRTC_STEREO_DOUBLE);
  10905. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10906. * adjust it according to limitations or connector properties, and also
  10907. * a chance to reject the mode entirely.
  10908. */
  10909. for_each_connector_in_state(state, connector, connector_state, i) {
  10910. if (connector_state->crtc != crtc)
  10911. continue;
  10912. encoder = to_intel_encoder(connector_state->best_encoder);
  10913. if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
  10914. DRM_DEBUG_KMS("Encoder config failure\n");
  10915. goto fail;
  10916. }
  10917. }
  10918. /* Set default port clock if not overwritten by the encoder. Needs to be
  10919. * done afterwards in case the encoder adjusts the mode. */
  10920. if (!pipe_config->port_clock)
  10921. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10922. * pipe_config->pixel_multiplier;
  10923. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10924. if (ret < 0) {
  10925. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10926. goto fail;
  10927. }
  10928. if (ret == RETRY) {
  10929. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10930. ret = -EINVAL;
  10931. goto fail;
  10932. }
  10933. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10934. retry = false;
  10935. goto encoder_retry;
  10936. }
  10937. /* Dithering seems to not pass-through bits correctly when it should, so
  10938. * only enable it on 6bpc panels. */
  10939. pipe_config->dither = pipe_config->pipe_bpp == 6*3;
  10940. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  10941. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10942. fail:
  10943. return ret;
  10944. }
  10945. static void
  10946. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  10947. {
  10948. struct drm_crtc *crtc;
  10949. struct drm_crtc_state *crtc_state;
  10950. int i;
  10951. /* Double check state. */
  10952. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10953. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10954. /* Update hwmode for vblank functions */
  10955. if (crtc->state->active)
  10956. crtc->hwmode = crtc->state->adjusted_mode;
  10957. else
  10958. crtc->hwmode.crtc_clock = 0;
  10959. /*
  10960. * Update legacy state to satisfy fbc code. This can
  10961. * be removed when fbc uses the atomic state.
  10962. */
  10963. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10964. struct drm_plane_state *plane_state = crtc->primary->state;
  10965. crtc->primary->fb = plane_state->fb;
  10966. crtc->x = plane_state->src_x >> 16;
  10967. crtc->y = plane_state->src_y >> 16;
  10968. }
  10969. }
  10970. }
  10971. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10972. {
  10973. int diff;
  10974. if (clock1 == clock2)
  10975. return true;
  10976. if (!clock1 || !clock2)
  10977. return false;
  10978. diff = abs(clock1 - clock2);
  10979. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10980. return true;
  10981. return false;
  10982. }
  10983. static bool
  10984. intel_compare_m_n(unsigned int m, unsigned int n,
  10985. unsigned int m2, unsigned int n2,
  10986. bool exact)
  10987. {
  10988. if (m == m2 && n == n2)
  10989. return true;
  10990. if (exact || !m || !n || !m2 || !n2)
  10991. return false;
  10992. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  10993. if (n > n2) {
  10994. while (n > n2) {
  10995. m2 <<= 1;
  10996. n2 <<= 1;
  10997. }
  10998. } else if (n < n2) {
  10999. while (n < n2) {
  11000. m <<= 1;
  11001. n <<= 1;
  11002. }
  11003. }
  11004. if (n != n2)
  11005. return false;
  11006. return intel_fuzzy_clock_check(m, m2);
  11007. }
  11008. static bool
  11009. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  11010. struct intel_link_m_n *m2_n2,
  11011. bool adjust)
  11012. {
  11013. if (m_n->tu == m2_n2->tu &&
  11014. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  11015. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  11016. intel_compare_m_n(m_n->link_m, m_n->link_n,
  11017. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  11018. if (adjust)
  11019. *m2_n2 = *m_n;
  11020. return true;
  11021. }
  11022. return false;
  11023. }
  11024. static void __printf(3, 4)
  11025. pipe_config_err(bool adjust, const char *name, const char *format, ...)
  11026. {
  11027. char *level;
  11028. unsigned int category;
  11029. struct va_format vaf;
  11030. va_list args;
  11031. if (adjust) {
  11032. level = KERN_DEBUG;
  11033. category = DRM_UT_KMS;
  11034. } else {
  11035. level = KERN_ERR;
  11036. category = DRM_UT_NONE;
  11037. }
  11038. va_start(args, format);
  11039. vaf.fmt = format;
  11040. vaf.va = &args;
  11041. drm_printk(level, category, "mismatch in %s %pV", name, &vaf);
  11042. va_end(args);
  11043. }
  11044. static bool
  11045. intel_pipe_config_compare(struct drm_i915_private *dev_priv,
  11046. struct intel_crtc_state *current_config,
  11047. struct intel_crtc_state *pipe_config,
  11048. bool adjust)
  11049. {
  11050. bool ret = true;
  11051. #define PIPE_CONF_CHECK_X(name) \
  11052. if (current_config->name != pipe_config->name) { \
  11053. pipe_config_err(adjust, __stringify(name), \
  11054. "(expected 0x%08x, found 0x%08x)\n", \
  11055. current_config->name, \
  11056. pipe_config->name); \
  11057. ret = false; \
  11058. }
  11059. #define PIPE_CONF_CHECK_I(name) \
  11060. if (current_config->name != pipe_config->name) { \
  11061. pipe_config_err(adjust, __stringify(name), \
  11062. "(expected %i, found %i)\n", \
  11063. current_config->name, \
  11064. pipe_config->name); \
  11065. ret = false; \
  11066. }
  11067. #define PIPE_CONF_CHECK_P(name) \
  11068. if (current_config->name != pipe_config->name) { \
  11069. pipe_config_err(adjust, __stringify(name), \
  11070. "(expected %p, found %p)\n", \
  11071. current_config->name, \
  11072. pipe_config->name); \
  11073. ret = false; \
  11074. }
  11075. #define PIPE_CONF_CHECK_M_N(name) \
  11076. if (!intel_compare_link_m_n(&current_config->name, \
  11077. &pipe_config->name,\
  11078. adjust)) { \
  11079. pipe_config_err(adjust, __stringify(name), \
  11080. "(expected tu %i gmch %i/%i link %i/%i, " \
  11081. "found tu %i, gmch %i/%i link %i/%i)\n", \
  11082. current_config->name.tu, \
  11083. current_config->name.gmch_m, \
  11084. current_config->name.gmch_n, \
  11085. current_config->name.link_m, \
  11086. current_config->name.link_n, \
  11087. pipe_config->name.tu, \
  11088. pipe_config->name.gmch_m, \
  11089. pipe_config->name.gmch_n, \
  11090. pipe_config->name.link_m, \
  11091. pipe_config->name.link_n); \
  11092. ret = false; \
  11093. }
  11094. /* This is required for BDW+ where there is only one set of registers for
  11095. * switching between high and low RR.
  11096. * This macro can be used whenever a comparison has to be made between one
  11097. * hw state and multiple sw state variables.
  11098. */
  11099. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  11100. if (!intel_compare_link_m_n(&current_config->name, \
  11101. &pipe_config->name, adjust) && \
  11102. !intel_compare_link_m_n(&current_config->alt_name, \
  11103. &pipe_config->name, adjust)) { \
  11104. pipe_config_err(adjust, __stringify(name), \
  11105. "(expected tu %i gmch %i/%i link %i/%i, " \
  11106. "or tu %i gmch %i/%i link %i/%i, " \
  11107. "found tu %i, gmch %i/%i link %i/%i)\n", \
  11108. current_config->name.tu, \
  11109. current_config->name.gmch_m, \
  11110. current_config->name.gmch_n, \
  11111. current_config->name.link_m, \
  11112. current_config->name.link_n, \
  11113. current_config->alt_name.tu, \
  11114. current_config->alt_name.gmch_m, \
  11115. current_config->alt_name.gmch_n, \
  11116. current_config->alt_name.link_m, \
  11117. current_config->alt_name.link_n, \
  11118. pipe_config->name.tu, \
  11119. pipe_config->name.gmch_m, \
  11120. pipe_config->name.gmch_n, \
  11121. pipe_config->name.link_m, \
  11122. pipe_config->name.link_n); \
  11123. ret = false; \
  11124. }
  11125. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  11126. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  11127. pipe_config_err(adjust, __stringify(name), \
  11128. "(%x) (expected %i, found %i)\n", \
  11129. (mask), \
  11130. current_config->name & (mask), \
  11131. pipe_config->name & (mask)); \
  11132. ret = false; \
  11133. }
  11134. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  11135. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  11136. pipe_config_err(adjust, __stringify(name), \
  11137. "(expected %i, found %i)\n", \
  11138. current_config->name, \
  11139. pipe_config->name); \
  11140. ret = false; \
  11141. }
  11142. #define PIPE_CONF_QUIRK(quirk) \
  11143. ((current_config->quirks | pipe_config->quirks) & (quirk))
  11144. PIPE_CONF_CHECK_I(cpu_transcoder);
  11145. PIPE_CONF_CHECK_I(has_pch_encoder);
  11146. PIPE_CONF_CHECK_I(fdi_lanes);
  11147. PIPE_CONF_CHECK_M_N(fdi_m_n);
  11148. PIPE_CONF_CHECK_I(lane_count);
  11149. PIPE_CONF_CHECK_X(lane_lat_optim_mask);
  11150. if (INTEL_GEN(dev_priv) < 8) {
  11151. PIPE_CONF_CHECK_M_N(dp_m_n);
  11152. if (current_config->has_drrs)
  11153. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  11154. } else
  11155. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  11156. PIPE_CONF_CHECK_X(output_types);
  11157. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  11158. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  11159. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  11160. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  11161. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  11162. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  11163. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  11164. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  11165. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  11166. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  11167. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  11168. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  11169. PIPE_CONF_CHECK_I(pixel_multiplier);
  11170. PIPE_CONF_CHECK_I(has_hdmi_sink);
  11171. if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
  11172. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11173. PIPE_CONF_CHECK_I(limited_color_range);
  11174. PIPE_CONF_CHECK_I(has_infoframe);
  11175. PIPE_CONF_CHECK_I(has_audio);
  11176. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11177. DRM_MODE_FLAG_INTERLACE);
  11178. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  11179. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11180. DRM_MODE_FLAG_PHSYNC);
  11181. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11182. DRM_MODE_FLAG_NHSYNC);
  11183. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11184. DRM_MODE_FLAG_PVSYNC);
  11185. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11186. DRM_MODE_FLAG_NVSYNC);
  11187. }
  11188. PIPE_CONF_CHECK_X(gmch_pfit.control);
  11189. /* pfit ratios are autocomputed by the hw on gen4+ */
  11190. if (INTEL_GEN(dev_priv) < 4)
  11191. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  11192. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  11193. if (!adjust) {
  11194. PIPE_CONF_CHECK_I(pipe_src_w);
  11195. PIPE_CONF_CHECK_I(pipe_src_h);
  11196. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  11197. if (current_config->pch_pfit.enabled) {
  11198. PIPE_CONF_CHECK_X(pch_pfit.pos);
  11199. PIPE_CONF_CHECK_X(pch_pfit.size);
  11200. }
  11201. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  11202. }
  11203. /* BDW+ don't expose a synchronous way to read the state */
  11204. if (IS_HASWELL(dev_priv))
  11205. PIPE_CONF_CHECK_I(ips_enabled);
  11206. PIPE_CONF_CHECK_I(double_wide);
  11207. PIPE_CONF_CHECK_P(shared_dpll);
  11208. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  11209. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  11210. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  11211. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  11212. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  11213. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  11214. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  11215. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  11216. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  11217. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  11218. PIPE_CONF_CHECK_X(dsi_pll.div);
  11219. if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
  11220. PIPE_CONF_CHECK_I(pipe_bpp);
  11221. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  11222. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  11223. #undef PIPE_CONF_CHECK_X
  11224. #undef PIPE_CONF_CHECK_I
  11225. #undef PIPE_CONF_CHECK_P
  11226. #undef PIPE_CONF_CHECK_FLAGS
  11227. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  11228. #undef PIPE_CONF_QUIRK
  11229. return ret;
  11230. }
  11231. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  11232. const struct intel_crtc_state *pipe_config)
  11233. {
  11234. if (pipe_config->has_pch_encoder) {
  11235. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  11236. &pipe_config->fdi_m_n);
  11237. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  11238. /*
  11239. * FDI already provided one idea for the dotclock.
  11240. * Yell if the encoder disagrees.
  11241. */
  11242. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  11243. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  11244. fdi_dotclock, dotclock);
  11245. }
  11246. }
  11247. static void verify_wm_state(struct drm_crtc *crtc,
  11248. struct drm_crtc_state *new_state)
  11249. {
  11250. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  11251. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  11252. struct skl_pipe_wm hw_wm, *sw_wm;
  11253. struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
  11254. struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
  11255. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11256. const enum pipe pipe = intel_crtc->pipe;
  11257. int plane, level, max_level = ilk_wm_max_level(dev_priv);
  11258. if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
  11259. return;
  11260. skl_pipe_wm_get_hw_state(crtc, &hw_wm);
  11261. sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
  11262. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  11263. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  11264. /* planes */
  11265. for_each_universal_plane(dev_priv, pipe, plane) {
  11266. hw_plane_wm = &hw_wm.planes[plane];
  11267. sw_plane_wm = &sw_wm->planes[plane];
  11268. /* Watermarks */
  11269. for (level = 0; level <= max_level; level++) {
  11270. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  11271. &sw_plane_wm->wm[level]))
  11272. continue;
  11273. DRM_ERROR("mismatch in WM pipe %c plane %d level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  11274. pipe_name(pipe), plane + 1, level,
  11275. sw_plane_wm->wm[level].plane_en,
  11276. sw_plane_wm->wm[level].plane_res_b,
  11277. sw_plane_wm->wm[level].plane_res_l,
  11278. hw_plane_wm->wm[level].plane_en,
  11279. hw_plane_wm->wm[level].plane_res_b,
  11280. hw_plane_wm->wm[level].plane_res_l);
  11281. }
  11282. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  11283. &sw_plane_wm->trans_wm)) {
  11284. DRM_ERROR("mismatch in trans WM pipe %c plane %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  11285. pipe_name(pipe), plane + 1,
  11286. sw_plane_wm->trans_wm.plane_en,
  11287. sw_plane_wm->trans_wm.plane_res_b,
  11288. sw_plane_wm->trans_wm.plane_res_l,
  11289. hw_plane_wm->trans_wm.plane_en,
  11290. hw_plane_wm->trans_wm.plane_res_b,
  11291. hw_plane_wm->trans_wm.plane_res_l);
  11292. }
  11293. /* DDB */
  11294. hw_ddb_entry = &hw_ddb.plane[pipe][plane];
  11295. sw_ddb_entry = &sw_ddb->plane[pipe][plane];
  11296. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  11297. DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
  11298. pipe_name(pipe), plane + 1,
  11299. sw_ddb_entry->start, sw_ddb_entry->end,
  11300. hw_ddb_entry->start, hw_ddb_entry->end);
  11301. }
  11302. }
  11303. /*
  11304. * cursor
  11305. * If the cursor plane isn't active, we may not have updated it's ddb
  11306. * allocation. In that case since the ddb allocation will be updated
  11307. * once the plane becomes visible, we can skip this check
  11308. */
  11309. if (intel_crtc->cursor_addr) {
  11310. hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
  11311. sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
  11312. /* Watermarks */
  11313. for (level = 0; level <= max_level; level++) {
  11314. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  11315. &sw_plane_wm->wm[level]))
  11316. continue;
  11317. DRM_ERROR("mismatch in WM pipe %c cursor level %d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  11318. pipe_name(pipe), level,
  11319. sw_plane_wm->wm[level].plane_en,
  11320. sw_plane_wm->wm[level].plane_res_b,
  11321. sw_plane_wm->wm[level].plane_res_l,
  11322. hw_plane_wm->wm[level].plane_en,
  11323. hw_plane_wm->wm[level].plane_res_b,
  11324. hw_plane_wm->wm[level].plane_res_l);
  11325. }
  11326. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  11327. &sw_plane_wm->trans_wm)) {
  11328. DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  11329. pipe_name(pipe),
  11330. sw_plane_wm->trans_wm.plane_en,
  11331. sw_plane_wm->trans_wm.plane_res_b,
  11332. sw_plane_wm->trans_wm.plane_res_l,
  11333. hw_plane_wm->trans_wm.plane_en,
  11334. hw_plane_wm->trans_wm.plane_res_b,
  11335. hw_plane_wm->trans_wm.plane_res_l);
  11336. }
  11337. /* DDB */
  11338. hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  11339. sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  11340. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  11341. DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
  11342. pipe_name(pipe),
  11343. sw_ddb_entry->start, sw_ddb_entry->end,
  11344. hw_ddb_entry->start, hw_ddb_entry->end);
  11345. }
  11346. }
  11347. }
  11348. static void
  11349. verify_connector_state(struct drm_device *dev,
  11350. struct drm_atomic_state *state,
  11351. struct drm_crtc *crtc)
  11352. {
  11353. struct drm_connector *connector;
  11354. struct drm_connector_state *old_conn_state;
  11355. int i;
  11356. for_each_connector_in_state(state, connector, old_conn_state, i) {
  11357. struct drm_encoder *encoder = connector->encoder;
  11358. struct drm_connector_state *state = connector->state;
  11359. if (state->crtc != crtc)
  11360. continue;
  11361. intel_connector_verify_state(to_intel_connector(connector));
  11362. I915_STATE_WARN(state->best_encoder != encoder,
  11363. "connector's atomic encoder doesn't match legacy encoder\n");
  11364. }
  11365. }
  11366. static void
  11367. verify_encoder_state(struct drm_device *dev)
  11368. {
  11369. struct intel_encoder *encoder;
  11370. struct intel_connector *connector;
  11371. for_each_intel_encoder(dev, encoder) {
  11372. bool enabled = false;
  11373. enum pipe pipe;
  11374. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  11375. encoder->base.base.id,
  11376. encoder->base.name);
  11377. for_each_intel_connector(dev, connector) {
  11378. if (connector->base.state->best_encoder != &encoder->base)
  11379. continue;
  11380. enabled = true;
  11381. I915_STATE_WARN(connector->base.state->crtc !=
  11382. encoder->base.crtc,
  11383. "connector's crtc doesn't match encoder crtc\n");
  11384. }
  11385. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  11386. "encoder's enabled state mismatch "
  11387. "(expected %i, found %i)\n",
  11388. !!encoder->base.crtc, enabled);
  11389. if (!encoder->base.crtc) {
  11390. bool active;
  11391. active = encoder->get_hw_state(encoder, &pipe);
  11392. I915_STATE_WARN(active,
  11393. "encoder detached but still enabled on pipe %c.\n",
  11394. pipe_name(pipe));
  11395. }
  11396. }
  11397. }
  11398. static void
  11399. verify_crtc_state(struct drm_crtc *crtc,
  11400. struct drm_crtc_state *old_crtc_state,
  11401. struct drm_crtc_state *new_crtc_state)
  11402. {
  11403. struct drm_device *dev = crtc->dev;
  11404. struct drm_i915_private *dev_priv = to_i915(dev);
  11405. struct intel_encoder *encoder;
  11406. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11407. struct intel_crtc_state *pipe_config, *sw_config;
  11408. struct drm_atomic_state *old_state;
  11409. bool active;
  11410. old_state = old_crtc_state->state;
  11411. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  11412. pipe_config = to_intel_crtc_state(old_crtc_state);
  11413. memset(pipe_config, 0, sizeof(*pipe_config));
  11414. pipe_config->base.crtc = crtc;
  11415. pipe_config->base.state = old_state;
  11416. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  11417. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  11418. /* hw state is inconsistent with the pipe quirk */
  11419. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  11420. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  11421. active = new_crtc_state->active;
  11422. I915_STATE_WARN(new_crtc_state->active != active,
  11423. "crtc active state doesn't match with hw state "
  11424. "(expected %i, found %i)\n", new_crtc_state->active, active);
  11425. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  11426. "transitional active state does not match atomic hw state "
  11427. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  11428. for_each_encoder_on_crtc(dev, crtc, encoder) {
  11429. enum pipe pipe;
  11430. active = encoder->get_hw_state(encoder, &pipe);
  11431. I915_STATE_WARN(active != new_crtc_state->active,
  11432. "[ENCODER:%i] active %i with crtc active %i\n",
  11433. encoder->base.base.id, active, new_crtc_state->active);
  11434. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  11435. "Encoder connected to wrong pipe %c\n",
  11436. pipe_name(pipe));
  11437. if (active) {
  11438. pipe_config->output_types |= 1 << encoder->type;
  11439. encoder->get_config(encoder, pipe_config);
  11440. }
  11441. }
  11442. if (!new_crtc_state->active)
  11443. return;
  11444. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  11445. sw_config = to_intel_crtc_state(crtc->state);
  11446. if (!intel_pipe_config_compare(dev_priv, sw_config,
  11447. pipe_config, false)) {
  11448. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  11449. intel_dump_pipe_config(intel_crtc, pipe_config,
  11450. "[hw state]");
  11451. intel_dump_pipe_config(intel_crtc, sw_config,
  11452. "[sw state]");
  11453. }
  11454. }
  11455. static void
  11456. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  11457. struct intel_shared_dpll *pll,
  11458. struct drm_crtc *crtc,
  11459. struct drm_crtc_state *new_state)
  11460. {
  11461. struct intel_dpll_hw_state dpll_hw_state;
  11462. unsigned crtc_mask;
  11463. bool active;
  11464. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  11465. DRM_DEBUG_KMS("%s\n", pll->name);
  11466. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  11467. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  11468. I915_STATE_WARN(!pll->on && pll->active_mask,
  11469. "pll in active use but not on in sw tracking\n");
  11470. I915_STATE_WARN(pll->on && !pll->active_mask,
  11471. "pll is on but not used by any active crtc\n");
  11472. I915_STATE_WARN(pll->on != active,
  11473. "pll on state mismatch (expected %i, found %i)\n",
  11474. pll->on, active);
  11475. }
  11476. if (!crtc) {
  11477. I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
  11478. "more active pll users than references: %x vs %x\n",
  11479. pll->active_mask, pll->state.crtc_mask);
  11480. return;
  11481. }
  11482. crtc_mask = 1 << drm_crtc_index(crtc);
  11483. if (new_state->active)
  11484. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  11485. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  11486. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  11487. else
  11488. I915_STATE_WARN(pll->active_mask & crtc_mask,
  11489. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  11490. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  11491. I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
  11492. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  11493. crtc_mask, pll->state.crtc_mask);
  11494. I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
  11495. &dpll_hw_state,
  11496. sizeof(dpll_hw_state)),
  11497. "pll hw state mismatch\n");
  11498. }
  11499. static void
  11500. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  11501. struct drm_crtc_state *old_crtc_state,
  11502. struct drm_crtc_state *new_crtc_state)
  11503. {
  11504. struct drm_i915_private *dev_priv = to_i915(dev);
  11505. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  11506. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  11507. if (new_state->shared_dpll)
  11508. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  11509. if (old_state->shared_dpll &&
  11510. old_state->shared_dpll != new_state->shared_dpll) {
  11511. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  11512. struct intel_shared_dpll *pll = old_state->shared_dpll;
  11513. I915_STATE_WARN(pll->active_mask & crtc_mask,
  11514. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  11515. pipe_name(drm_crtc_index(crtc)));
  11516. I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
  11517. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  11518. pipe_name(drm_crtc_index(crtc)));
  11519. }
  11520. }
  11521. static void
  11522. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  11523. struct drm_atomic_state *state,
  11524. struct drm_crtc_state *old_state,
  11525. struct drm_crtc_state *new_state)
  11526. {
  11527. if (!needs_modeset(new_state) &&
  11528. !to_intel_crtc_state(new_state)->update_pipe)
  11529. return;
  11530. verify_wm_state(crtc, new_state);
  11531. verify_connector_state(crtc->dev, state, crtc);
  11532. verify_crtc_state(crtc, old_state, new_state);
  11533. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  11534. }
  11535. static void
  11536. verify_disabled_dpll_state(struct drm_device *dev)
  11537. {
  11538. struct drm_i915_private *dev_priv = to_i915(dev);
  11539. int i;
  11540. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  11541. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  11542. }
  11543. static void
  11544. intel_modeset_verify_disabled(struct drm_device *dev,
  11545. struct drm_atomic_state *state)
  11546. {
  11547. verify_encoder_state(dev);
  11548. verify_connector_state(dev, state, NULL);
  11549. verify_disabled_dpll_state(dev);
  11550. }
  11551. static void update_scanline_offset(struct intel_crtc *crtc)
  11552. {
  11553. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  11554. /*
  11555. * The scanline counter increments at the leading edge of hsync.
  11556. *
  11557. * On most platforms it starts counting from vtotal-1 on the
  11558. * first active line. That means the scanline counter value is
  11559. * always one less than what we would expect. Ie. just after
  11560. * start of vblank, which also occurs at start of hsync (on the
  11561. * last active line), the scanline counter will read vblank_start-1.
  11562. *
  11563. * On gen2 the scanline counter starts counting from 1 instead
  11564. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  11565. * to keep the value positive), instead of adding one.
  11566. *
  11567. * On HSW+ the behaviour of the scanline counter depends on the output
  11568. * type. For DP ports it behaves like most other platforms, but on HDMI
  11569. * there's an extra 1 line difference. So we need to add two instead of
  11570. * one to the value.
  11571. */
  11572. if (IS_GEN2(dev_priv)) {
  11573. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  11574. int vtotal;
  11575. vtotal = adjusted_mode->crtc_vtotal;
  11576. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  11577. vtotal /= 2;
  11578. crtc->scanline_offset = vtotal - 1;
  11579. } else if (HAS_DDI(dev_priv) &&
  11580. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
  11581. crtc->scanline_offset = 2;
  11582. } else
  11583. crtc->scanline_offset = 1;
  11584. }
  11585. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  11586. {
  11587. struct drm_device *dev = state->dev;
  11588. struct drm_i915_private *dev_priv = to_i915(dev);
  11589. struct drm_crtc *crtc;
  11590. struct drm_crtc_state *crtc_state;
  11591. int i;
  11592. if (!dev_priv->display.crtc_compute_clock)
  11593. return;
  11594. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11595. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11596. struct intel_shared_dpll *old_dpll =
  11597. to_intel_crtc_state(crtc->state)->shared_dpll;
  11598. if (!needs_modeset(crtc_state))
  11599. continue;
  11600. to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
  11601. if (!old_dpll)
  11602. continue;
  11603. intel_release_shared_dpll(old_dpll, intel_crtc, state);
  11604. }
  11605. }
  11606. /*
  11607. * This implements the workaround described in the "notes" section of the mode
  11608. * set sequence documentation. When going from no pipes or single pipe to
  11609. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  11610. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  11611. */
  11612. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  11613. {
  11614. struct drm_crtc_state *crtc_state;
  11615. struct intel_crtc *intel_crtc;
  11616. struct drm_crtc *crtc;
  11617. struct intel_crtc_state *first_crtc_state = NULL;
  11618. struct intel_crtc_state *other_crtc_state = NULL;
  11619. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  11620. int i;
  11621. /* look at all crtc's that are going to be enabled in during modeset */
  11622. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11623. intel_crtc = to_intel_crtc(crtc);
  11624. if (!crtc_state->active || !needs_modeset(crtc_state))
  11625. continue;
  11626. if (first_crtc_state) {
  11627. other_crtc_state = to_intel_crtc_state(crtc_state);
  11628. break;
  11629. } else {
  11630. first_crtc_state = to_intel_crtc_state(crtc_state);
  11631. first_pipe = intel_crtc->pipe;
  11632. }
  11633. }
  11634. /* No workaround needed? */
  11635. if (!first_crtc_state)
  11636. return 0;
  11637. /* w/a possibly needed, check how many crtc's are already enabled. */
  11638. for_each_intel_crtc(state->dev, intel_crtc) {
  11639. struct intel_crtc_state *pipe_config;
  11640. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  11641. if (IS_ERR(pipe_config))
  11642. return PTR_ERR(pipe_config);
  11643. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  11644. if (!pipe_config->base.active ||
  11645. needs_modeset(&pipe_config->base))
  11646. continue;
  11647. /* 2 or more enabled crtcs means no need for w/a */
  11648. if (enabled_pipe != INVALID_PIPE)
  11649. return 0;
  11650. enabled_pipe = intel_crtc->pipe;
  11651. }
  11652. if (enabled_pipe != INVALID_PIPE)
  11653. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  11654. else if (other_crtc_state)
  11655. other_crtc_state->hsw_workaround_pipe = first_pipe;
  11656. return 0;
  11657. }
  11658. static int intel_lock_all_pipes(struct drm_atomic_state *state)
  11659. {
  11660. struct drm_crtc *crtc;
  11661. /* Add all pipes to the state */
  11662. for_each_crtc(state->dev, crtc) {
  11663. struct drm_crtc_state *crtc_state;
  11664. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11665. if (IS_ERR(crtc_state))
  11666. return PTR_ERR(crtc_state);
  11667. }
  11668. return 0;
  11669. }
  11670. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  11671. {
  11672. struct drm_crtc *crtc;
  11673. /*
  11674. * Add all pipes to the state, and force
  11675. * a modeset on all the active ones.
  11676. */
  11677. for_each_crtc(state->dev, crtc) {
  11678. struct drm_crtc_state *crtc_state;
  11679. int ret;
  11680. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11681. if (IS_ERR(crtc_state))
  11682. return PTR_ERR(crtc_state);
  11683. if (!crtc_state->active || needs_modeset(crtc_state))
  11684. continue;
  11685. crtc_state->mode_changed = true;
  11686. ret = drm_atomic_add_affected_connectors(state, crtc);
  11687. if (ret)
  11688. return ret;
  11689. ret = drm_atomic_add_affected_planes(state, crtc);
  11690. if (ret)
  11691. return ret;
  11692. }
  11693. return 0;
  11694. }
  11695. static int intel_modeset_checks(struct drm_atomic_state *state)
  11696. {
  11697. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11698. struct drm_i915_private *dev_priv = to_i915(state->dev);
  11699. struct drm_crtc *crtc;
  11700. struct drm_crtc_state *crtc_state;
  11701. int ret = 0, i;
  11702. if (!check_digital_port_conflicts(state)) {
  11703. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  11704. return -EINVAL;
  11705. }
  11706. intel_state->modeset = true;
  11707. intel_state->active_crtcs = dev_priv->active_crtcs;
  11708. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11709. if (crtc_state->active)
  11710. intel_state->active_crtcs |= 1 << i;
  11711. else
  11712. intel_state->active_crtcs &= ~(1 << i);
  11713. if (crtc_state->active != crtc->state->active)
  11714. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  11715. }
  11716. /*
  11717. * See if the config requires any additional preparation, e.g.
  11718. * to adjust global state with pipes off. We need to do this
  11719. * here so we can get the modeset_pipe updated config for the new
  11720. * mode set on this crtc. For other crtcs we need to use the
  11721. * adjusted_mode bits in the crtc directly.
  11722. */
  11723. if (dev_priv->display.modeset_calc_cdclk) {
  11724. if (!intel_state->cdclk_pll_vco)
  11725. intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
  11726. if (!intel_state->cdclk_pll_vco)
  11727. intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
  11728. ret = dev_priv->display.modeset_calc_cdclk(state);
  11729. if (ret < 0)
  11730. return ret;
  11731. /*
  11732. * Writes to dev_priv->atomic_cdclk_freq must protected by
  11733. * holding all the crtc locks, even if we don't end up
  11734. * touching the hardware
  11735. */
  11736. if (intel_state->cdclk != dev_priv->atomic_cdclk_freq) {
  11737. ret = intel_lock_all_pipes(state);
  11738. if (ret < 0)
  11739. return ret;
  11740. }
  11741. /* All pipes must be switched off while we change the cdclk. */
  11742. if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
  11743. intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco) {
  11744. ret = intel_modeset_all_pipes(state);
  11745. if (ret < 0)
  11746. return ret;
  11747. }
  11748. DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
  11749. intel_state->cdclk, intel_state->dev_cdclk);
  11750. } else {
  11751. to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
  11752. }
  11753. intel_modeset_clear_plls(state);
  11754. if (IS_HASWELL(dev_priv))
  11755. return haswell_mode_set_planes_workaround(state);
  11756. return 0;
  11757. }
  11758. /*
  11759. * Handle calculation of various watermark data at the end of the atomic check
  11760. * phase. The code here should be run after the per-crtc and per-plane 'check'
  11761. * handlers to ensure that all derived state has been updated.
  11762. */
  11763. static int calc_watermark_data(struct drm_atomic_state *state)
  11764. {
  11765. struct drm_device *dev = state->dev;
  11766. struct drm_i915_private *dev_priv = to_i915(dev);
  11767. /* Is there platform-specific watermark information to calculate? */
  11768. if (dev_priv->display.compute_global_watermarks)
  11769. return dev_priv->display.compute_global_watermarks(state);
  11770. return 0;
  11771. }
  11772. /**
  11773. * intel_atomic_check - validate state object
  11774. * @dev: drm device
  11775. * @state: state to validate
  11776. */
  11777. static int intel_atomic_check(struct drm_device *dev,
  11778. struct drm_atomic_state *state)
  11779. {
  11780. struct drm_i915_private *dev_priv = to_i915(dev);
  11781. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11782. struct drm_crtc *crtc;
  11783. struct drm_crtc_state *crtc_state;
  11784. int ret, i;
  11785. bool any_ms = false;
  11786. ret = drm_atomic_helper_check_modeset(dev, state);
  11787. if (ret)
  11788. return ret;
  11789. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11790. struct intel_crtc_state *pipe_config =
  11791. to_intel_crtc_state(crtc_state);
  11792. /* Catch I915_MODE_FLAG_INHERITED */
  11793. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  11794. crtc_state->mode_changed = true;
  11795. if (!needs_modeset(crtc_state))
  11796. continue;
  11797. if (!crtc_state->enable) {
  11798. any_ms = true;
  11799. continue;
  11800. }
  11801. /* FIXME: For only active_changed we shouldn't need to do any
  11802. * state recomputation at all. */
  11803. ret = drm_atomic_add_affected_connectors(state, crtc);
  11804. if (ret)
  11805. return ret;
  11806. ret = intel_modeset_pipe_config(crtc, pipe_config);
  11807. if (ret) {
  11808. intel_dump_pipe_config(to_intel_crtc(crtc),
  11809. pipe_config, "[failed]");
  11810. return ret;
  11811. }
  11812. if (i915.fastboot &&
  11813. intel_pipe_config_compare(dev_priv,
  11814. to_intel_crtc_state(crtc->state),
  11815. pipe_config, true)) {
  11816. crtc_state->mode_changed = false;
  11817. to_intel_crtc_state(crtc_state)->update_pipe = true;
  11818. }
  11819. if (needs_modeset(crtc_state))
  11820. any_ms = true;
  11821. ret = drm_atomic_add_affected_planes(state, crtc);
  11822. if (ret)
  11823. return ret;
  11824. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  11825. needs_modeset(crtc_state) ?
  11826. "[modeset]" : "[fastset]");
  11827. }
  11828. if (any_ms) {
  11829. ret = intel_modeset_checks(state);
  11830. if (ret)
  11831. return ret;
  11832. } else {
  11833. intel_state->cdclk = dev_priv->atomic_cdclk_freq;
  11834. }
  11835. ret = drm_atomic_helper_check_planes(dev, state);
  11836. if (ret)
  11837. return ret;
  11838. intel_fbc_choose_crtc(dev_priv, state);
  11839. return calc_watermark_data(state);
  11840. }
  11841. static int intel_atomic_prepare_commit(struct drm_device *dev,
  11842. struct drm_atomic_state *state)
  11843. {
  11844. struct drm_i915_private *dev_priv = to_i915(dev);
  11845. struct drm_crtc_state *crtc_state;
  11846. struct drm_crtc *crtc;
  11847. int i, ret;
  11848. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11849. if (state->legacy_cursor_update)
  11850. continue;
  11851. ret = intel_crtc_wait_for_pending_flips(crtc);
  11852. if (ret)
  11853. return ret;
  11854. if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
  11855. flush_workqueue(dev_priv->wq);
  11856. }
  11857. ret = mutex_lock_interruptible(&dev->struct_mutex);
  11858. if (ret)
  11859. return ret;
  11860. ret = drm_atomic_helper_prepare_planes(dev, state);
  11861. mutex_unlock(&dev->struct_mutex);
  11862. return ret;
  11863. }
  11864. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  11865. {
  11866. struct drm_device *dev = crtc->base.dev;
  11867. if (!dev->max_vblank_count)
  11868. return drm_accurate_vblank_count(&crtc->base);
  11869. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  11870. }
  11871. static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
  11872. struct drm_i915_private *dev_priv,
  11873. unsigned crtc_mask)
  11874. {
  11875. unsigned last_vblank_count[I915_MAX_PIPES];
  11876. enum pipe pipe;
  11877. int ret;
  11878. if (!crtc_mask)
  11879. return;
  11880. for_each_pipe(dev_priv, pipe) {
  11881. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  11882. pipe);
  11883. if (!((1 << pipe) & crtc_mask))
  11884. continue;
  11885. ret = drm_crtc_vblank_get(&crtc->base);
  11886. if (WARN_ON(ret != 0)) {
  11887. crtc_mask &= ~(1 << pipe);
  11888. continue;
  11889. }
  11890. last_vblank_count[pipe] = drm_crtc_vblank_count(&crtc->base);
  11891. }
  11892. for_each_pipe(dev_priv, pipe) {
  11893. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  11894. pipe);
  11895. long lret;
  11896. if (!((1 << pipe) & crtc_mask))
  11897. continue;
  11898. lret = wait_event_timeout(dev->vblank[pipe].queue,
  11899. last_vblank_count[pipe] !=
  11900. drm_crtc_vblank_count(&crtc->base),
  11901. msecs_to_jiffies(50));
  11902. WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
  11903. drm_crtc_vblank_put(&crtc->base);
  11904. }
  11905. }
  11906. static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
  11907. {
  11908. /* fb updated, need to unpin old fb */
  11909. if (crtc_state->fb_changed)
  11910. return true;
  11911. /* wm changes, need vblank before final wm's */
  11912. if (crtc_state->update_wm_post)
  11913. return true;
  11914. /*
  11915. * cxsr is re-enabled after vblank.
  11916. * This is already handled by crtc_state->update_wm_post,
  11917. * but added for clarity.
  11918. */
  11919. if (crtc_state->disable_cxsr)
  11920. return true;
  11921. return false;
  11922. }
  11923. static void intel_update_crtc(struct drm_crtc *crtc,
  11924. struct drm_atomic_state *state,
  11925. struct drm_crtc_state *old_crtc_state,
  11926. unsigned int *crtc_vblank_mask)
  11927. {
  11928. struct drm_device *dev = crtc->dev;
  11929. struct drm_i915_private *dev_priv = to_i915(dev);
  11930. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11931. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc->state);
  11932. bool modeset = needs_modeset(crtc->state);
  11933. if (modeset) {
  11934. update_scanline_offset(intel_crtc);
  11935. dev_priv->display.crtc_enable(pipe_config, state);
  11936. } else {
  11937. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  11938. }
  11939. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  11940. intel_fbc_enable(
  11941. intel_crtc, pipe_config,
  11942. to_intel_plane_state(crtc->primary->state));
  11943. }
  11944. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  11945. if (needs_vblank_wait(pipe_config))
  11946. *crtc_vblank_mask |= drm_crtc_mask(crtc);
  11947. }
  11948. static void intel_update_crtcs(struct drm_atomic_state *state,
  11949. unsigned int *crtc_vblank_mask)
  11950. {
  11951. struct drm_crtc *crtc;
  11952. struct drm_crtc_state *old_crtc_state;
  11953. int i;
  11954. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11955. if (!crtc->state->active)
  11956. continue;
  11957. intel_update_crtc(crtc, state, old_crtc_state,
  11958. crtc_vblank_mask);
  11959. }
  11960. }
  11961. static void skl_update_crtcs(struct drm_atomic_state *state,
  11962. unsigned int *crtc_vblank_mask)
  11963. {
  11964. struct drm_i915_private *dev_priv = to_i915(state->dev);
  11965. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11966. struct drm_crtc *crtc;
  11967. struct intel_crtc *intel_crtc;
  11968. struct drm_crtc_state *old_crtc_state;
  11969. struct intel_crtc_state *cstate;
  11970. unsigned int updated = 0;
  11971. bool progress;
  11972. enum pipe pipe;
  11973. int i;
  11974. const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
  11975. for_each_crtc_in_state(state, crtc, old_crtc_state, i)
  11976. /* ignore allocations for crtc's that have been turned off. */
  11977. if (crtc->state->active)
  11978. entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
  11979. /*
  11980. * Whenever the number of active pipes changes, we need to make sure we
  11981. * update the pipes in the right order so that their ddb allocations
  11982. * never overlap with eachother inbetween CRTC updates. Otherwise we'll
  11983. * cause pipe underruns and other bad stuff.
  11984. */
  11985. do {
  11986. progress = false;
  11987. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11988. bool vbl_wait = false;
  11989. unsigned int cmask = drm_crtc_mask(crtc);
  11990. intel_crtc = to_intel_crtc(crtc);
  11991. cstate = to_intel_crtc_state(crtc->state);
  11992. pipe = intel_crtc->pipe;
  11993. if (updated & cmask || !cstate->base.active)
  11994. continue;
  11995. if (skl_ddb_allocation_overlaps(entries, &cstate->wm.skl.ddb, i))
  11996. continue;
  11997. updated |= cmask;
  11998. entries[i] = &cstate->wm.skl.ddb;
  11999. /*
  12000. * If this is an already active pipe, it's DDB changed,
  12001. * and this isn't the last pipe that needs updating
  12002. * then we need to wait for a vblank to pass for the
  12003. * new ddb allocation to take effect.
  12004. */
  12005. if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
  12006. &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
  12007. !crtc->state->active_changed &&
  12008. intel_state->wm_results.dirty_pipes != updated)
  12009. vbl_wait = true;
  12010. intel_update_crtc(crtc, state, old_crtc_state,
  12011. crtc_vblank_mask);
  12012. if (vbl_wait)
  12013. intel_wait_for_vblank(dev_priv, pipe);
  12014. progress = true;
  12015. }
  12016. } while (progress);
  12017. }
  12018. static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
  12019. {
  12020. struct intel_atomic_state *state, *next;
  12021. struct llist_node *freed;
  12022. freed = llist_del_all(&dev_priv->atomic_helper.free_list);
  12023. llist_for_each_entry_safe(state, next, freed, freed)
  12024. drm_atomic_state_put(&state->base);
  12025. }
  12026. static void intel_atomic_helper_free_state_worker(struct work_struct *work)
  12027. {
  12028. struct drm_i915_private *dev_priv =
  12029. container_of(work, typeof(*dev_priv), atomic_helper.free_work);
  12030. intel_atomic_helper_free_state(dev_priv);
  12031. }
  12032. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  12033. {
  12034. struct drm_device *dev = state->dev;
  12035. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  12036. struct drm_i915_private *dev_priv = to_i915(dev);
  12037. struct drm_crtc_state *old_crtc_state;
  12038. struct drm_crtc *crtc;
  12039. struct intel_crtc_state *intel_cstate;
  12040. bool hw_check = intel_state->modeset;
  12041. unsigned long put_domains[I915_MAX_PIPES] = {};
  12042. unsigned crtc_vblank_mask = 0;
  12043. int i;
  12044. drm_atomic_helper_wait_for_dependencies(state);
  12045. if (intel_state->modeset)
  12046. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  12047. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12048. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12049. if (needs_modeset(crtc->state) ||
  12050. to_intel_crtc_state(crtc->state)->update_pipe) {
  12051. hw_check = true;
  12052. put_domains[to_intel_crtc(crtc)->pipe] =
  12053. modeset_get_crtc_power_domains(crtc,
  12054. to_intel_crtc_state(crtc->state));
  12055. }
  12056. if (!needs_modeset(crtc->state))
  12057. continue;
  12058. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  12059. if (old_crtc_state->active) {
  12060. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  12061. dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
  12062. intel_crtc->active = false;
  12063. intel_fbc_disable(intel_crtc);
  12064. intel_disable_shared_dpll(intel_crtc);
  12065. /*
  12066. * Underruns don't always raise
  12067. * interrupts, so check manually.
  12068. */
  12069. intel_check_cpu_fifo_underruns(dev_priv);
  12070. intel_check_pch_fifo_underruns(dev_priv);
  12071. if (!crtc->state->active) {
  12072. /*
  12073. * Make sure we don't call initial_watermarks
  12074. * for ILK-style watermark updates.
  12075. */
  12076. if (dev_priv->display.atomic_update_watermarks)
  12077. dev_priv->display.initial_watermarks(intel_state,
  12078. to_intel_crtc_state(crtc->state));
  12079. else
  12080. intel_update_watermarks(intel_crtc);
  12081. }
  12082. }
  12083. }
  12084. /* Only after disabling all output pipelines that will be changed can we
  12085. * update the the output configuration. */
  12086. intel_modeset_update_crtc_state(state);
  12087. if (intel_state->modeset) {
  12088. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  12089. if (dev_priv->display.modeset_commit_cdclk &&
  12090. (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
  12091. intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
  12092. dev_priv->display.modeset_commit_cdclk(state);
  12093. /*
  12094. * SKL workaround: bspec recommends we disable the SAGV when we
  12095. * have more then one pipe enabled
  12096. */
  12097. if (!intel_can_enable_sagv(state))
  12098. intel_disable_sagv(dev_priv);
  12099. intel_modeset_verify_disabled(dev, state);
  12100. }
  12101. /* Complete the events for pipes that have now been disabled */
  12102. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12103. bool modeset = needs_modeset(crtc->state);
  12104. /* Complete events for now disable pipes here. */
  12105. if (modeset && !crtc->state->active && crtc->state->event) {
  12106. spin_lock_irq(&dev->event_lock);
  12107. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  12108. spin_unlock_irq(&dev->event_lock);
  12109. crtc->state->event = NULL;
  12110. }
  12111. }
  12112. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  12113. dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
  12114. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  12115. * already, but still need the state for the delayed optimization. To
  12116. * fix this:
  12117. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  12118. * - schedule that vblank worker _before_ calling hw_done
  12119. * - at the start of commit_tail, cancel it _synchrously
  12120. * - switch over to the vblank wait helper in the core after that since
  12121. * we don't need out special handling any more.
  12122. */
  12123. if (!state->legacy_cursor_update)
  12124. intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
  12125. /*
  12126. * Now that the vblank has passed, we can go ahead and program the
  12127. * optimal watermarks on platforms that need two-step watermark
  12128. * programming.
  12129. *
  12130. * TODO: Move this (and other cleanup) to an async worker eventually.
  12131. */
  12132. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12133. intel_cstate = to_intel_crtc_state(crtc->state);
  12134. if (dev_priv->display.optimize_watermarks)
  12135. dev_priv->display.optimize_watermarks(intel_state,
  12136. intel_cstate);
  12137. }
  12138. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12139. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  12140. if (put_domains[i])
  12141. modeset_put_power_domains(dev_priv, put_domains[i]);
  12142. intel_modeset_verify_crtc(crtc, state, old_crtc_state, crtc->state);
  12143. }
  12144. if (intel_state->modeset && intel_can_enable_sagv(state))
  12145. intel_enable_sagv(dev_priv);
  12146. drm_atomic_helper_commit_hw_done(state);
  12147. if (intel_state->modeset)
  12148. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  12149. mutex_lock(&dev->struct_mutex);
  12150. drm_atomic_helper_cleanup_planes(dev, state);
  12151. mutex_unlock(&dev->struct_mutex);
  12152. drm_atomic_helper_commit_cleanup_done(state);
  12153. drm_atomic_state_put(state);
  12154. /* As one of the primary mmio accessors, KMS has a high likelihood
  12155. * of triggering bugs in unclaimed access. After we finish
  12156. * modesetting, see if an error has been flagged, and if so
  12157. * enable debugging for the next modeset - and hope we catch
  12158. * the culprit.
  12159. *
  12160. * XXX note that we assume display power is on at this point.
  12161. * This might hold true now but we need to add pm helper to check
  12162. * unclaimed only when the hardware is on, as atomic commits
  12163. * can happen also when the device is completely off.
  12164. */
  12165. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  12166. intel_atomic_helper_free_state(dev_priv);
  12167. }
  12168. static void intel_atomic_commit_work(struct work_struct *work)
  12169. {
  12170. struct drm_atomic_state *state =
  12171. container_of(work, struct drm_atomic_state, commit_work);
  12172. intel_atomic_commit_tail(state);
  12173. }
  12174. static int __i915_sw_fence_call
  12175. intel_atomic_commit_ready(struct i915_sw_fence *fence,
  12176. enum i915_sw_fence_notify notify)
  12177. {
  12178. struct intel_atomic_state *state =
  12179. container_of(fence, struct intel_atomic_state, commit_ready);
  12180. switch (notify) {
  12181. case FENCE_COMPLETE:
  12182. if (state->base.commit_work.func)
  12183. queue_work(system_unbound_wq, &state->base.commit_work);
  12184. break;
  12185. case FENCE_FREE:
  12186. {
  12187. struct intel_atomic_helper *helper =
  12188. &to_i915(state->base.dev)->atomic_helper;
  12189. if (llist_add(&state->freed, &helper->free_list))
  12190. schedule_work(&helper->free_work);
  12191. break;
  12192. }
  12193. }
  12194. return NOTIFY_DONE;
  12195. }
  12196. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  12197. {
  12198. struct drm_plane_state *old_plane_state;
  12199. struct drm_plane *plane;
  12200. int i;
  12201. for_each_plane_in_state(state, plane, old_plane_state, i)
  12202. i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
  12203. intel_fb_obj(plane->state->fb),
  12204. to_intel_plane(plane)->frontbuffer_bit);
  12205. }
  12206. /**
  12207. * intel_atomic_commit - commit validated state object
  12208. * @dev: DRM device
  12209. * @state: the top-level driver state object
  12210. * @nonblock: nonblocking commit
  12211. *
  12212. * This function commits a top-level state object that has been validated
  12213. * with drm_atomic_helper_check().
  12214. *
  12215. * RETURNS
  12216. * Zero for success or -errno.
  12217. */
  12218. static int intel_atomic_commit(struct drm_device *dev,
  12219. struct drm_atomic_state *state,
  12220. bool nonblock)
  12221. {
  12222. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  12223. struct drm_i915_private *dev_priv = to_i915(dev);
  12224. int ret = 0;
  12225. ret = drm_atomic_helper_setup_commit(state, nonblock);
  12226. if (ret)
  12227. return ret;
  12228. drm_atomic_state_get(state);
  12229. i915_sw_fence_init(&intel_state->commit_ready,
  12230. intel_atomic_commit_ready);
  12231. ret = intel_atomic_prepare_commit(dev, state);
  12232. if (ret) {
  12233. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  12234. i915_sw_fence_commit(&intel_state->commit_ready);
  12235. return ret;
  12236. }
  12237. drm_atomic_helper_swap_state(state, true);
  12238. dev_priv->wm.distrust_bios_wm = false;
  12239. intel_shared_dpll_swap_state(state);
  12240. intel_atomic_track_fbs(state);
  12241. if (intel_state->modeset) {
  12242. memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
  12243. sizeof(intel_state->min_pixclk));
  12244. dev_priv->active_crtcs = intel_state->active_crtcs;
  12245. dev_priv->atomic_cdclk_freq = intel_state->cdclk;
  12246. }
  12247. drm_atomic_state_get(state);
  12248. INIT_WORK(&state->commit_work,
  12249. nonblock ? intel_atomic_commit_work : NULL);
  12250. i915_sw_fence_commit(&intel_state->commit_ready);
  12251. if (!nonblock) {
  12252. i915_sw_fence_wait(&intel_state->commit_ready);
  12253. intel_atomic_commit_tail(state);
  12254. }
  12255. return 0;
  12256. }
  12257. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  12258. {
  12259. struct drm_device *dev = crtc->dev;
  12260. struct drm_atomic_state *state;
  12261. struct drm_crtc_state *crtc_state;
  12262. int ret;
  12263. state = drm_atomic_state_alloc(dev);
  12264. if (!state) {
  12265. DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
  12266. crtc->base.id, crtc->name);
  12267. return;
  12268. }
  12269. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  12270. retry:
  12271. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  12272. ret = PTR_ERR_OR_ZERO(crtc_state);
  12273. if (!ret) {
  12274. if (!crtc_state->active)
  12275. goto out;
  12276. crtc_state->mode_changed = true;
  12277. ret = drm_atomic_commit(state);
  12278. }
  12279. if (ret == -EDEADLK) {
  12280. drm_atomic_state_clear(state);
  12281. drm_modeset_backoff(state->acquire_ctx);
  12282. goto retry;
  12283. }
  12284. out:
  12285. drm_atomic_state_put(state);
  12286. }
  12287. /*
  12288. * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
  12289. * drm_atomic_helper_legacy_gamma_set() directly.
  12290. */
  12291. static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
  12292. u16 *red, u16 *green, u16 *blue,
  12293. uint32_t size)
  12294. {
  12295. struct drm_device *dev = crtc->dev;
  12296. struct drm_mode_config *config = &dev->mode_config;
  12297. struct drm_crtc_state *state;
  12298. int ret;
  12299. ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
  12300. if (ret)
  12301. return ret;
  12302. /*
  12303. * Make sure we update the legacy properties so this works when
  12304. * atomic is not enabled.
  12305. */
  12306. state = crtc->state;
  12307. drm_object_property_set_value(&crtc->base,
  12308. config->degamma_lut_property,
  12309. (state->degamma_lut) ?
  12310. state->degamma_lut->base.id : 0);
  12311. drm_object_property_set_value(&crtc->base,
  12312. config->ctm_property,
  12313. (state->ctm) ?
  12314. state->ctm->base.id : 0);
  12315. drm_object_property_set_value(&crtc->base,
  12316. config->gamma_lut_property,
  12317. (state->gamma_lut) ?
  12318. state->gamma_lut->base.id : 0);
  12319. return 0;
  12320. }
  12321. static const struct drm_crtc_funcs intel_crtc_funcs = {
  12322. .gamma_set = intel_atomic_legacy_gamma_set,
  12323. .set_config = drm_atomic_helper_set_config,
  12324. .set_property = drm_atomic_helper_crtc_set_property,
  12325. .destroy = intel_crtc_destroy,
  12326. .page_flip = intel_crtc_page_flip,
  12327. .atomic_duplicate_state = intel_crtc_duplicate_state,
  12328. .atomic_destroy_state = intel_crtc_destroy_state,
  12329. .set_crc_source = intel_crtc_set_crc_source,
  12330. };
  12331. /**
  12332. * intel_prepare_plane_fb - Prepare fb for usage on plane
  12333. * @plane: drm plane to prepare for
  12334. * @fb: framebuffer to prepare for presentation
  12335. *
  12336. * Prepares a framebuffer for usage on a display plane. Generally this
  12337. * involves pinning the underlying object and updating the frontbuffer tracking
  12338. * bits. Some older platforms need special physical address handling for
  12339. * cursor planes.
  12340. *
  12341. * Must be called with struct_mutex held.
  12342. *
  12343. * Returns 0 on success, negative error code on failure.
  12344. */
  12345. int
  12346. intel_prepare_plane_fb(struct drm_plane *plane,
  12347. struct drm_plane_state *new_state)
  12348. {
  12349. struct intel_atomic_state *intel_state =
  12350. to_intel_atomic_state(new_state->state);
  12351. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  12352. struct drm_framebuffer *fb = new_state->fb;
  12353. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  12354. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  12355. int ret;
  12356. if (!obj && !old_obj)
  12357. return 0;
  12358. if (old_obj) {
  12359. struct drm_crtc_state *crtc_state =
  12360. drm_atomic_get_existing_crtc_state(new_state->state,
  12361. plane->state->crtc);
  12362. /* Big Hammer, we also need to ensure that any pending
  12363. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  12364. * current scanout is retired before unpinning the old
  12365. * framebuffer. Note that we rely on userspace rendering
  12366. * into the buffer attached to the pipe they are waiting
  12367. * on. If not, userspace generates a GPU hang with IPEHR
  12368. * point to the MI_WAIT_FOR_EVENT.
  12369. *
  12370. * This should only fail upon a hung GPU, in which case we
  12371. * can safely continue.
  12372. */
  12373. if (needs_modeset(crtc_state)) {
  12374. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  12375. old_obj->resv, NULL,
  12376. false, 0,
  12377. GFP_KERNEL);
  12378. if (ret < 0)
  12379. return ret;
  12380. }
  12381. }
  12382. if (new_state->fence) { /* explicit fencing */
  12383. ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
  12384. new_state->fence,
  12385. I915_FENCE_TIMEOUT,
  12386. GFP_KERNEL);
  12387. if (ret < 0)
  12388. return ret;
  12389. }
  12390. if (!obj)
  12391. return 0;
  12392. if (!new_state->fence) { /* implicit fencing */
  12393. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  12394. obj->resv, NULL,
  12395. false, I915_FENCE_TIMEOUT,
  12396. GFP_KERNEL);
  12397. if (ret < 0)
  12398. return ret;
  12399. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  12400. }
  12401. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  12402. INTEL_INFO(dev_priv)->cursor_needs_physical) {
  12403. int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
  12404. ret = i915_gem_object_attach_phys(obj, align);
  12405. if (ret) {
  12406. DRM_DEBUG_KMS("failed to attach phys object\n");
  12407. return ret;
  12408. }
  12409. } else {
  12410. struct i915_vma *vma;
  12411. vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  12412. if (IS_ERR(vma)) {
  12413. DRM_DEBUG_KMS("failed to pin object\n");
  12414. return PTR_ERR(vma);
  12415. }
  12416. to_intel_plane_state(new_state)->vma = vma;
  12417. }
  12418. return 0;
  12419. }
  12420. /**
  12421. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  12422. * @plane: drm plane to clean up for
  12423. * @fb: old framebuffer that was on plane
  12424. *
  12425. * Cleans up a framebuffer that has just been removed from a plane.
  12426. *
  12427. * Must be called with struct_mutex held.
  12428. */
  12429. void
  12430. intel_cleanup_plane_fb(struct drm_plane *plane,
  12431. struct drm_plane_state *old_state)
  12432. {
  12433. struct i915_vma *vma;
  12434. /* Should only be called after a successful intel_prepare_plane_fb()! */
  12435. vma = fetch_and_zero(&to_intel_plane_state(old_state)->vma);
  12436. if (vma)
  12437. intel_unpin_fb_vma(vma);
  12438. }
  12439. int
  12440. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  12441. {
  12442. int max_scale;
  12443. int crtc_clock, cdclk;
  12444. if (!intel_crtc || !crtc_state->base.enable)
  12445. return DRM_PLANE_HELPER_NO_SCALING;
  12446. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  12447. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  12448. if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
  12449. return DRM_PLANE_HELPER_NO_SCALING;
  12450. /*
  12451. * skl max scale is lower of:
  12452. * close to 3 but not 3, -1 is for that purpose
  12453. * or
  12454. * cdclk/crtc_clock
  12455. */
  12456. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  12457. return max_scale;
  12458. }
  12459. static int
  12460. intel_check_primary_plane(struct drm_plane *plane,
  12461. struct intel_crtc_state *crtc_state,
  12462. struct intel_plane_state *state)
  12463. {
  12464. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  12465. struct drm_crtc *crtc = state->base.crtc;
  12466. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  12467. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  12468. bool can_position = false;
  12469. int ret;
  12470. if (INTEL_GEN(dev_priv) >= 9) {
  12471. /* use scaler when colorkey is not required */
  12472. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  12473. min_scale = 1;
  12474. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  12475. }
  12476. can_position = true;
  12477. }
  12478. ret = drm_plane_helper_check_state(&state->base,
  12479. &state->clip,
  12480. min_scale, max_scale,
  12481. can_position, true);
  12482. if (ret)
  12483. return ret;
  12484. if (!state->base.fb)
  12485. return 0;
  12486. if (INTEL_GEN(dev_priv) >= 9) {
  12487. ret = skl_check_plane_surface(state);
  12488. if (ret)
  12489. return ret;
  12490. }
  12491. return 0;
  12492. }
  12493. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  12494. struct drm_crtc_state *old_crtc_state)
  12495. {
  12496. struct drm_device *dev = crtc->dev;
  12497. struct drm_i915_private *dev_priv = to_i915(dev);
  12498. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12499. struct intel_crtc_state *intel_cstate =
  12500. to_intel_crtc_state(crtc->state);
  12501. struct intel_crtc_state *old_intel_cstate =
  12502. to_intel_crtc_state(old_crtc_state);
  12503. struct intel_atomic_state *old_intel_state =
  12504. to_intel_atomic_state(old_crtc_state->state);
  12505. bool modeset = needs_modeset(crtc->state);
  12506. if (!modeset &&
  12507. (intel_cstate->base.color_mgmt_changed ||
  12508. intel_cstate->update_pipe)) {
  12509. intel_color_set_csc(crtc->state);
  12510. intel_color_load_luts(crtc->state);
  12511. }
  12512. /* Perform vblank evasion around commit operation */
  12513. intel_pipe_update_start(intel_crtc);
  12514. if (modeset)
  12515. goto out;
  12516. if (intel_cstate->update_pipe)
  12517. intel_update_pipe_config(intel_crtc, old_intel_cstate);
  12518. else if (INTEL_GEN(dev_priv) >= 9)
  12519. skl_detach_scalers(intel_crtc);
  12520. out:
  12521. if (dev_priv->display.atomic_update_watermarks)
  12522. dev_priv->display.atomic_update_watermarks(old_intel_state,
  12523. intel_cstate);
  12524. }
  12525. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  12526. struct drm_crtc_state *old_crtc_state)
  12527. {
  12528. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12529. intel_pipe_update_end(intel_crtc, NULL);
  12530. }
  12531. /**
  12532. * intel_plane_destroy - destroy a plane
  12533. * @plane: plane to destroy
  12534. *
  12535. * Common destruction function for all types of planes (primary, cursor,
  12536. * sprite).
  12537. */
  12538. void intel_plane_destroy(struct drm_plane *plane)
  12539. {
  12540. drm_plane_cleanup(plane);
  12541. kfree(to_intel_plane(plane));
  12542. }
  12543. const struct drm_plane_funcs intel_plane_funcs = {
  12544. .update_plane = drm_atomic_helper_update_plane,
  12545. .disable_plane = drm_atomic_helper_disable_plane,
  12546. .destroy = intel_plane_destroy,
  12547. .set_property = drm_atomic_helper_plane_set_property,
  12548. .atomic_get_property = intel_plane_atomic_get_property,
  12549. .atomic_set_property = intel_plane_atomic_set_property,
  12550. .atomic_duplicate_state = intel_plane_duplicate_state,
  12551. .atomic_destroy_state = intel_plane_destroy_state,
  12552. };
  12553. static int
  12554. intel_legacy_cursor_update(struct drm_plane *plane,
  12555. struct drm_crtc *crtc,
  12556. struct drm_framebuffer *fb,
  12557. int crtc_x, int crtc_y,
  12558. unsigned int crtc_w, unsigned int crtc_h,
  12559. uint32_t src_x, uint32_t src_y,
  12560. uint32_t src_w, uint32_t src_h)
  12561. {
  12562. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  12563. int ret;
  12564. struct drm_plane_state *old_plane_state, *new_plane_state;
  12565. struct intel_plane *intel_plane = to_intel_plane(plane);
  12566. struct drm_framebuffer *old_fb;
  12567. struct drm_crtc_state *crtc_state = crtc->state;
  12568. struct i915_vma *old_vma;
  12569. /*
  12570. * When crtc is inactive or there is a modeset pending,
  12571. * wait for it to complete in the slowpath
  12572. */
  12573. if (!crtc_state->active || needs_modeset(crtc_state) ||
  12574. to_intel_crtc_state(crtc_state)->update_pipe)
  12575. goto slow;
  12576. old_plane_state = plane->state;
  12577. /*
  12578. * If any parameters change that may affect watermarks,
  12579. * take the slowpath. Only changing fb or position should be
  12580. * in the fastpath.
  12581. */
  12582. if (old_plane_state->crtc != crtc ||
  12583. old_plane_state->src_w != src_w ||
  12584. old_plane_state->src_h != src_h ||
  12585. old_plane_state->crtc_w != crtc_w ||
  12586. old_plane_state->crtc_h != crtc_h ||
  12587. !old_plane_state->visible ||
  12588. old_plane_state->fb->modifier != fb->modifier)
  12589. goto slow;
  12590. new_plane_state = intel_plane_duplicate_state(plane);
  12591. if (!new_plane_state)
  12592. return -ENOMEM;
  12593. drm_atomic_set_fb_for_plane(new_plane_state, fb);
  12594. new_plane_state->src_x = src_x;
  12595. new_plane_state->src_y = src_y;
  12596. new_plane_state->src_w = src_w;
  12597. new_plane_state->src_h = src_h;
  12598. new_plane_state->crtc_x = crtc_x;
  12599. new_plane_state->crtc_y = crtc_y;
  12600. new_plane_state->crtc_w = crtc_w;
  12601. new_plane_state->crtc_h = crtc_h;
  12602. ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
  12603. to_intel_plane_state(new_plane_state));
  12604. if (ret)
  12605. goto out_free;
  12606. /* Visibility changed, must take slowpath. */
  12607. if (!new_plane_state->visible)
  12608. goto slow_free;
  12609. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  12610. if (ret)
  12611. goto out_free;
  12612. if (INTEL_INFO(dev_priv)->cursor_needs_physical) {
  12613. int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
  12614. ret = i915_gem_object_attach_phys(intel_fb_obj(fb), align);
  12615. if (ret) {
  12616. DRM_DEBUG_KMS("failed to attach phys object\n");
  12617. goto out_unlock;
  12618. }
  12619. } else {
  12620. struct i915_vma *vma;
  12621. vma = intel_pin_and_fence_fb_obj(fb, new_plane_state->rotation);
  12622. if (IS_ERR(vma)) {
  12623. DRM_DEBUG_KMS("failed to pin object\n");
  12624. ret = PTR_ERR(vma);
  12625. goto out_unlock;
  12626. }
  12627. to_intel_plane_state(new_plane_state)->vma = vma;
  12628. }
  12629. old_fb = old_plane_state->fb;
  12630. old_vma = to_intel_plane_state(old_plane_state)->vma;
  12631. i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
  12632. intel_plane->frontbuffer_bit);
  12633. /* Swap plane state */
  12634. new_plane_state->fence = old_plane_state->fence;
  12635. *to_intel_plane_state(old_plane_state) = *to_intel_plane_state(new_plane_state);
  12636. new_plane_state->fence = NULL;
  12637. new_plane_state->fb = old_fb;
  12638. to_intel_plane_state(new_plane_state)->vma = old_vma;
  12639. intel_plane->update_plane(plane,
  12640. to_intel_crtc_state(crtc->state),
  12641. to_intel_plane_state(plane->state));
  12642. intel_cleanup_plane_fb(plane, new_plane_state);
  12643. out_unlock:
  12644. mutex_unlock(&dev_priv->drm.struct_mutex);
  12645. out_free:
  12646. intel_plane_destroy_state(plane, new_plane_state);
  12647. return ret;
  12648. slow_free:
  12649. intel_plane_destroy_state(plane, new_plane_state);
  12650. slow:
  12651. return drm_atomic_helper_update_plane(plane, crtc, fb,
  12652. crtc_x, crtc_y, crtc_w, crtc_h,
  12653. src_x, src_y, src_w, src_h);
  12654. }
  12655. static const struct drm_plane_funcs intel_cursor_plane_funcs = {
  12656. .update_plane = intel_legacy_cursor_update,
  12657. .disable_plane = drm_atomic_helper_disable_plane,
  12658. .destroy = intel_plane_destroy,
  12659. .set_property = drm_atomic_helper_plane_set_property,
  12660. .atomic_get_property = intel_plane_atomic_get_property,
  12661. .atomic_set_property = intel_plane_atomic_set_property,
  12662. .atomic_duplicate_state = intel_plane_duplicate_state,
  12663. .atomic_destroy_state = intel_plane_destroy_state,
  12664. };
  12665. static struct intel_plane *
  12666. intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  12667. {
  12668. struct intel_plane *primary = NULL;
  12669. struct intel_plane_state *state = NULL;
  12670. const uint32_t *intel_primary_formats;
  12671. unsigned int supported_rotations;
  12672. unsigned int num_formats;
  12673. int ret;
  12674. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  12675. if (!primary) {
  12676. ret = -ENOMEM;
  12677. goto fail;
  12678. }
  12679. state = intel_create_plane_state(&primary->base);
  12680. if (!state) {
  12681. ret = -ENOMEM;
  12682. goto fail;
  12683. }
  12684. primary->base.state = &state->base;
  12685. primary->can_scale = false;
  12686. primary->max_downscale = 1;
  12687. if (INTEL_GEN(dev_priv) >= 9) {
  12688. primary->can_scale = true;
  12689. state->scaler_id = -1;
  12690. }
  12691. primary->pipe = pipe;
  12692. /*
  12693. * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
  12694. * port is hooked to pipe B. Hence we want plane A feeding pipe B.
  12695. */
  12696. if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
  12697. primary->plane = (enum plane) !pipe;
  12698. else
  12699. primary->plane = (enum plane) pipe;
  12700. primary->id = PLANE_PRIMARY;
  12701. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  12702. primary->check_plane = intel_check_primary_plane;
  12703. if (INTEL_GEN(dev_priv) >= 9) {
  12704. intel_primary_formats = skl_primary_formats;
  12705. num_formats = ARRAY_SIZE(skl_primary_formats);
  12706. primary->update_plane = skylake_update_primary_plane;
  12707. primary->disable_plane = skylake_disable_primary_plane;
  12708. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12709. intel_primary_formats = i965_primary_formats;
  12710. num_formats = ARRAY_SIZE(i965_primary_formats);
  12711. primary->update_plane = ironlake_update_primary_plane;
  12712. primary->disable_plane = i9xx_disable_primary_plane;
  12713. } else if (INTEL_GEN(dev_priv) >= 4) {
  12714. intel_primary_formats = i965_primary_formats;
  12715. num_formats = ARRAY_SIZE(i965_primary_formats);
  12716. primary->update_plane = i9xx_update_primary_plane;
  12717. primary->disable_plane = i9xx_disable_primary_plane;
  12718. } else {
  12719. intel_primary_formats = i8xx_primary_formats;
  12720. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  12721. primary->update_plane = i9xx_update_primary_plane;
  12722. primary->disable_plane = i9xx_disable_primary_plane;
  12723. }
  12724. if (INTEL_GEN(dev_priv) >= 9)
  12725. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  12726. 0, &intel_plane_funcs,
  12727. intel_primary_formats, num_formats,
  12728. DRM_PLANE_TYPE_PRIMARY,
  12729. "plane 1%c", pipe_name(pipe));
  12730. else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  12731. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  12732. 0, &intel_plane_funcs,
  12733. intel_primary_formats, num_formats,
  12734. DRM_PLANE_TYPE_PRIMARY,
  12735. "primary %c", pipe_name(pipe));
  12736. else
  12737. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  12738. 0, &intel_plane_funcs,
  12739. intel_primary_formats, num_formats,
  12740. DRM_PLANE_TYPE_PRIMARY,
  12741. "plane %c", plane_name(primary->plane));
  12742. if (ret)
  12743. goto fail;
  12744. if (INTEL_GEN(dev_priv) >= 9) {
  12745. supported_rotations =
  12746. DRM_ROTATE_0 | DRM_ROTATE_90 |
  12747. DRM_ROTATE_180 | DRM_ROTATE_270;
  12748. } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  12749. supported_rotations =
  12750. DRM_ROTATE_0 | DRM_ROTATE_180 |
  12751. DRM_REFLECT_X;
  12752. } else if (INTEL_GEN(dev_priv) >= 4) {
  12753. supported_rotations =
  12754. DRM_ROTATE_0 | DRM_ROTATE_180;
  12755. } else {
  12756. supported_rotations = DRM_ROTATE_0;
  12757. }
  12758. if (INTEL_GEN(dev_priv) >= 4)
  12759. drm_plane_create_rotation_property(&primary->base,
  12760. DRM_ROTATE_0,
  12761. supported_rotations);
  12762. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  12763. return primary;
  12764. fail:
  12765. kfree(state);
  12766. kfree(primary);
  12767. return ERR_PTR(ret);
  12768. }
  12769. static int
  12770. intel_check_cursor_plane(struct drm_plane *plane,
  12771. struct intel_crtc_state *crtc_state,
  12772. struct intel_plane_state *state)
  12773. {
  12774. struct drm_framebuffer *fb = state->base.fb;
  12775. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  12776. enum pipe pipe = to_intel_plane(plane)->pipe;
  12777. unsigned stride;
  12778. int ret;
  12779. ret = drm_plane_helper_check_state(&state->base,
  12780. &state->clip,
  12781. DRM_PLANE_HELPER_NO_SCALING,
  12782. DRM_PLANE_HELPER_NO_SCALING,
  12783. true, true);
  12784. if (ret)
  12785. return ret;
  12786. /* if we want to turn off the cursor ignore width and height */
  12787. if (!obj)
  12788. return 0;
  12789. /* Check for which cursor types we support */
  12790. if (!cursor_size_ok(to_i915(plane->dev), state->base.crtc_w,
  12791. state->base.crtc_h)) {
  12792. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  12793. state->base.crtc_w, state->base.crtc_h);
  12794. return -EINVAL;
  12795. }
  12796. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  12797. if (obj->base.size < stride * state->base.crtc_h) {
  12798. DRM_DEBUG_KMS("buffer is too small\n");
  12799. return -ENOMEM;
  12800. }
  12801. if (fb->modifier != DRM_FORMAT_MOD_NONE) {
  12802. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  12803. return -EINVAL;
  12804. }
  12805. /*
  12806. * There's something wrong with the cursor on CHV pipe C.
  12807. * If it straddles the left edge of the screen then
  12808. * moving it away from the edge or disabling it often
  12809. * results in a pipe underrun, and often that can lead to
  12810. * dead pipe (constant underrun reported, and it scans
  12811. * out just a solid color). To recover from that, the
  12812. * display power well must be turned off and on again.
  12813. * Refuse the put the cursor into that compromised position.
  12814. */
  12815. if (IS_CHERRYVIEW(to_i915(plane->dev)) && pipe == PIPE_C &&
  12816. state->base.visible && state->base.crtc_x < 0) {
  12817. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  12818. return -EINVAL;
  12819. }
  12820. return 0;
  12821. }
  12822. static void
  12823. intel_disable_cursor_plane(struct drm_plane *plane,
  12824. struct drm_crtc *crtc)
  12825. {
  12826. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12827. intel_crtc->cursor_addr = 0;
  12828. intel_crtc_update_cursor(crtc, NULL);
  12829. }
  12830. static void
  12831. intel_update_cursor_plane(struct drm_plane *plane,
  12832. const struct intel_crtc_state *crtc_state,
  12833. const struct intel_plane_state *state)
  12834. {
  12835. struct drm_crtc *crtc = crtc_state->base.crtc;
  12836. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12837. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  12838. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  12839. uint32_t addr;
  12840. if (!obj)
  12841. addr = 0;
  12842. else if (!INTEL_INFO(dev_priv)->cursor_needs_physical)
  12843. addr = intel_plane_ggtt_offset(state);
  12844. else
  12845. addr = obj->phys_handle->busaddr;
  12846. intel_crtc->cursor_addr = addr;
  12847. intel_crtc_update_cursor(crtc, state);
  12848. }
  12849. static struct intel_plane *
  12850. intel_cursor_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  12851. {
  12852. struct intel_plane *cursor = NULL;
  12853. struct intel_plane_state *state = NULL;
  12854. int ret;
  12855. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  12856. if (!cursor) {
  12857. ret = -ENOMEM;
  12858. goto fail;
  12859. }
  12860. state = intel_create_plane_state(&cursor->base);
  12861. if (!state) {
  12862. ret = -ENOMEM;
  12863. goto fail;
  12864. }
  12865. cursor->base.state = &state->base;
  12866. cursor->can_scale = false;
  12867. cursor->max_downscale = 1;
  12868. cursor->pipe = pipe;
  12869. cursor->plane = pipe;
  12870. cursor->id = PLANE_CURSOR;
  12871. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  12872. cursor->check_plane = intel_check_cursor_plane;
  12873. cursor->update_plane = intel_update_cursor_plane;
  12874. cursor->disable_plane = intel_disable_cursor_plane;
  12875. ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
  12876. 0, &intel_cursor_plane_funcs,
  12877. intel_cursor_formats,
  12878. ARRAY_SIZE(intel_cursor_formats),
  12879. DRM_PLANE_TYPE_CURSOR,
  12880. "cursor %c", pipe_name(pipe));
  12881. if (ret)
  12882. goto fail;
  12883. if (INTEL_GEN(dev_priv) >= 4)
  12884. drm_plane_create_rotation_property(&cursor->base,
  12885. DRM_ROTATE_0,
  12886. DRM_ROTATE_0 |
  12887. DRM_ROTATE_180);
  12888. if (INTEL_GEN(dev_priv) >= 9)
  12889. state->scaler_id = -1;
  12890. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  12891. return cursor;
  12892. fail:
  12893. kfree(state);
  12894. kfree(cursor);
  12895. return ERR_PTR(ret);
  12896. }
  12897. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  12898. struct intel_crtc_state *crtc_state)
  12899. {
  12900. struct intel_crtc_scaler_state *scaler_state =
  12901. &crtc_state->scaler_state;
  12902. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  12903. int i;
  12904. crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
  12905. if (!crtc->num_scalers)
  12906. return;
  12907. for (i = 0; i < crtc->num_scalers; i++) {
  12908. struct intel_scaler *scaler = &scaler_state->scalers[i];
  12909. scaler->in_use = 0;
  12910. scaler->mode = PS_SCALER_MODE_DYN;
  12911. }
  12912. scaler_state->scaler_id = -1;
  12913. }
  12914. static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
  12915. {
  12916. struct intel_crtc *intel_crtc;
  12917. struct intel_crtc_state *crtc_state = NULL;
  12918. struct intel_plane *primary = NULL;
  12919. struct intel_plane *cursor = NULL;
  12920. int sprite, ret;
  12921. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  12922. if (!intel_crtc)
  12923. return -ENOMEM;
  12924. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  12925. if (!crtc_state) {
  12926. ret = -ENOMEM;
  12927. goto fail;
  12928. }
  12929. intel_crtc->config = crtc_state;
  12930. intel_crtc->base.state = &crtc_state->base;
  12931. crtc_state->base.crtc = &intel_crtc->base;
  12932. primary = intel_primary_plane_create(dev_priv, pipe);
  12933. if (IS_ERR(primary)) {
  12934. ret = PTR_ERR(primary);
  12935. goto fail;
  12936. }
  12937. intel_crtc->plane_ids_mask |= BIT(primary->id);
  12938. for_each_sprite(dev_priv, pipe, sprite) {
  12939. struct intel_plane *plane;
  12940. plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
  12941. if (IS_ERR(plane)) {
  12942. ret = PTR_ERR(plane);
  12943. goto fail;
  12944. }
  12945. intel_crtc->plane_ids_mask |= BIT(plane->id);
  12946. }
  12947. cursor = intel_cursor_plane_create(dev_priv, pipe);
  12948. if (IS_ERR(cursor)) {
  12949. ret = PTR_ERR(cursor);
  12950. goto fail;
  12951. }
  12952. intel_crtc->plane_ids_mask |= BIT(cursor->id);
  12953. ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
  12954. &primary->base, &cursor->base,
  12955. &intel_crtc_funcs,
  12956. "pipe %c", pipe_name(pipe));
  12957. if (ret)
  12958. goto fail;
  12959. intel_crtc->pipe = pipe;
  12960. intel_crtc->plane = primary->plane;
  12961. intel_crtc->cursor_base = ~0;
  12962. intel_crtc->cursor_cntl = ~0;
  12963. intel_crtc->cursor_size = ~0;
  12964. intel_crtc->wm.cxsr_allowed = true;
  12965. /* initialize shared scalers */
  12966. intel_crtc_init_scalers(intel_crtc, crtc_state);
  12967. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  12968. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  12969. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
  12970. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
  12971. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  12972. intel_color_init(&intel_crtc->base);
  12973. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  12974. return 0;
  12975. fail:
  12976. /*
  12977. * drm_mode_config_cleanup() will free up any
  12978. * crtcs/planes already initialized.
  12979. */
  12980. kfree(crtc_state);
  12981. kfree(intel_crtc);
  12982. return ret;
  12983. }
  12984. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  12985. {
  12986. struct drm_encoder *encoder = connector->base.encoder;
  12987. struct drm_device *dev = connector->base.dev;
  12988. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  12989. if (!encoder || WARN_ON(!encoder->crtc))
  12990. return INVALID_PIPE;
  12991. return to_intel_crtc(encoder->crtc)->pipe;
  12992. }
  12993. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  12994. struct drm_file *file)
  12995. {
  12996. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  12997. struct drm_crtc *drmmode_crtc;
  12998. struct intel_crtc *crtc;
  12999. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  13000. if (!drmmode_crtc)
  13001. return -ENOENT;
  13002. crtc = to_intel_crtc(drmmode_crtc);
  13003. pipe_from_crtc_id->pipe = crtc->pipe;
  13004. return 0;
  13005. }
  13006. static int intel_encoder_clones(struct intel_encoder *encoder)
  13007. {
  13008. struct drm_device *dev = encoder->base.dev;
  13009. struct intel_encoder *source_encoder;
  13010. int index_mask = 0;
  13011. int entry = 0;
  13012. for_each_intel_encoder(dev, source_encoder) {
  13013. if (encoders_cloneable(encoder, source_encoder))
  13014. index_mask |= (1 << entry);
  13015. entry++;
  13016. }
  13017. return index_mask;
  13018. }
  13019. static bool has_edp_a(struct drm_i915_private *dev_priv)
  13020. {
  13021. if (!IS_MOBILE(dev_priv))
  13022. return false;
  13023. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  13024. return false;
  13025. if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  13026. return false;
  13027. return true;
  13028. }
  13029. static bool intel_crt_present(struct drm_i915_private *dev_priv)
  13030. {
  13031. if (INTEL_GEN(dev_priv) >= 9)
  13032. return false;
  13033. if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
  13034. return false;
  13035. if (IS_CHERRYVIEW(dev_priv))
  13036. return false;
  13037. if (HAS_PCH_LPT_H(dev_priv) &&
  13038. I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  13039. return false;
  13040. /* DDI E can't be used if DDI A requires 4 lanes */
  13041. if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  13042. return false;
  13043. if (!dev_priv->vbt.int_crt_support)
  13044. return false;
  13045. return true;
  13046. }
  13047. void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
  13048. {
  13049. int pps_num;
  13050. int pps_idx;
  13051. if (HAS_DDI(dev_priv))
  13052. return;
  13053. /*
  13054. * This w/a is needed at least on CPT/PPT, but to be sure apply it
  13055. * everywhere where registers can be write protected.
  13056. */
  13057. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  13058. pps_num = 2;
  13059. else
  13060. pps_num = 1;
  13061. for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
  13062. u32 val = I915_READ(PP_CONTROL(pps_idx));
  13063. val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
  13064. I915_WRITE(PP_CONTROL(pps_idx), val);
  13065. }
  13066. }
  13067. static void intel_pps_init(struct drm_i915_private *dev_priv)
  13068. {
  13069. if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
  13070. dev_priv->pps_mmio_base = PCH_PPS_BASE;
  13071. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  13072. dev_priv->pps_mmio_base = VLV_PPS_BASE;
  13073. else
  13074. dev_priv->pps_mmio_base = PPS_BASE;
  13075. intel_pps_unlock_regs_wa(dev_priv);
  13076. }
  13077. static void intel_setup_outputs(struct drm_i915_private *dev_priv)
  13078. {
  13079. struct intel_encoder *encoder;
  13080. bool dpd_is_edp = false;
  13081. intel_pps_init(dev_priv);
  13082. /*
  13083. * intel_edp_init_connector() depends on this completing first, to
  13084. * prevent the registeration of both eDP and LVDS and the incorrect
  13085. * sharing of the PPS.
  13086. */
  13087. intel_lvds_init(dev_priv);
  13088. if (intel_crt_present(dev_priv))
  13089. intel_crt_init(dev_priv);
  13090. if (IS_GEN9_LP(dev_priv)) {
  13091. /*
  13092. * FIXME: Broxton doesn't support port detection via the
  13093. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  13094. * detect the ports.
  13095. */
  13096. intel_ddi_init(dev_priv, PORT_A);
  13097. intel_ddi_init(dev_priv, PORT_B);
  13098. intel_ddi_init(dev_priv, PORT_C);
  13099. intel_dsi_init(dev_priv);
  13100. } else if (HAS_DDI(dev_priv)) {
  13101. int found;
  13102. /*
  13103. * Haswell uses DDI functions to detect digital outputs.
  13104. * On SKL pre-D0 the strap isn't connected, so we assume
  13105. * it's there.
  13106. */
  13107. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  13108. /* WaIgnoreDDIAStrap: skl */
  13109. if (found || IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  13110. intel_ddi_init(dev_priv, PORT_A);
  13111. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  13112. * register */
  13113. found = I915_READ(SFUSE_STRAP);
  13114. if (found & SFUSE_STRAP_DDIB_DETECTED)
  13115. intel_ddi_init(dev_priv, PORT_B);
  13116. if (found & SFUSE_STRAP_DDIC_DETECTED)
  13117. intel_ddi_init(dev_priv, PORT_C);
  13118. if (found & SFUSE_STRAP_DDID_DETECTED)
  13119. intel_ddi_init(dev_priv, PORT_D);
  13120. /*
  13121. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  13122. */
  13123. if ((IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) &&
  13124. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  13125. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  13126. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  13127. intel_ddi_init(dev_priv, PORT_E);
  13128. } else if (HAS_PCH_SPLIT(dev_priv)) {
  13129. int found;
  13130. dpd_is_edp = intel_dp_is_edp(dev_priv, PORT_D);
  13131. if (has_edp_a(dev_priv))
  13132. intel_dp_init(dev_priv, DP_A, PORT_A);
  13133. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  13134. /* PCH SDVOB multiplex with HDMIB */
  13135. found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
  13136. if (!found)
  13137. intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
  13138. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  13139. intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
  13140. }
  13141. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  13142. intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
  13143. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  13144. intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
  13145. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  13146. intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
  13147. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  13148. intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
  13149. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  13150. bool has_edp, has_port;
  13151. /*
  13152. * The DP_DETECTED bit is the latched state of the DDC
  13153. * SDA pin at boot. However since eDP doesn't require DDC
  13154. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  13155. * eDP ports may have been muxed to an alternate function.
  13156. * Thus we can't rely on the DP_DETECTED bit alone to detect
  13157. * eDP ports. Consult the VBT as well as DP_DETECTED to
  13158. * detect eDP ports.
  13159. *
  13160. * Sadly the straps seem to be missing sometimes even for HDMI
  13161. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  13162. * and VBT for the presence of the port. Additionally we can't
  13163. * trust the port type the VBT declares as we've seen at least
  13164. * HDMI ports that the VBT claim are DP or eDP.
  13165. */
  13166. has_edp = intel_dp_is_edp(dev_priv, PORT_B);
  13167. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  13168. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  13169. has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
  13170. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  13171. intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
  13172. has_edp = intel_dp_is_edp(dev_priv, PORT_C);
  13173. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  13174. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  13175. has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
  13176. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  13177. intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
  13178. if (IS_CHERRYVIEW(dev_priv)) {
  13179. /*
  13180. * eDP not supported on port D,
  13181. * so no need to worry about it
  13182. */
  13183. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  13184. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  13185. intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
  13186. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  13187. intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
  13188. }
  13189. intel_dsi_init(dev_priv);
  13190. } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
  13191. bool found = false;
  13192. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  13193. DRM_DEBUG_KMS("probing SDVOB\n");
  13194. found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
  13195. if (!found && IS_G4X(dev_priv)) {
  13196. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  13197. intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
  13198. }
  13199. if (!found && IS_G4X(dev_priv))
  13200. intel_dp_init(dev_priv, DP_B, PORT_B);
  13201. }
  13202. /* Before G4X SDVOC doesn't have its own detect register */
  13203. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  13204. DRM_DEBUG_KMS("probing SDVOC\n");
  13205. found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
  13206. }
  13207. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  13208. if (IS_G4X(dev_priv)) {
  13209. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  13210. intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
  13211. }
  13212. if (IS_G4X(dev_priv))
  13213. intel_dp_init(dev_priv, DP_C, PORT_C);
  13214. }
  13215. if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
  13216. intel_dp_init(dev_priv, DP_D, PORT_D);
  13217. } else if (IS_GEN2(dev_priv))
  13218. intel_dvo_init(dev_priv);
  13219. if (SUPPORTS_TV(dev_priv))
  13220. intel_tv_init(dev_priv);
  13221. intel_psr_init(dev_priv);
  13222. for_each_intel_encoder(&dev_priv->drm, encoder) {
  13223. encoder->base.possible_crtcs = encoder->crtc_mask;
  13224. encoder->base.possible_clones =
  13225. intel_encoder_clones(encoder);
  13226. }
  13227. intel_init_pch_refclk(dev_priv);
  13228. drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
  13229. }
  13230. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  13231. {
  13232. struct drm_device *dev = fb->dev;
  13233. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  13234. drm_framebuffer_cleanup(fb);
  13235. mutex_lock(&dev->struct_mutex);
  13236. WARN_ON(!intel_fb->obj->framebuffer_references--);
  13237. i915_gem_object_put(intel_fb->obj);
  13238. mutex_unlock(&dev->struct_mutex);
  13239. kfree(intel_fb);
  13240. }
  13241. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  13242. struct drm_file *file,
  13243. unsigned int *handle)
  13244. {
  13245. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  13246. struct drm_i915_gem_object *obj = intel_fb->obj;
  13247. if (obj->userptr.mm) {
  13248. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  13249. return -EINVAL;
  13250. }
  13251. return drm_gem_handle_create(file, &obj->base, handle);
  13252. }
  13253. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  13254. struct drm_file *file,
  13255. unsigned flags, unsigned color,
  13256. struct drm_clip_rect *clips,
  13257. unsigned num_clips)
  13258. {
  13259. struct drm_device *dev = fb->dev;
  13260. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  13261. struct drm_i915_gem_object *obj = intel_fb->obj;
  13262. mutex_lock(&dev->struct_mutex);
  13263. if (obj->pin_display && obj->cache_dirty)
  13264. i915_gem_clflush_object(obj, true);
  13265. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  13266. mutex_unlock(&dev->struct_mutex);
  13267. return 0;
  13268. }
  13269. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  13270. .destroy = intel_user_framebuffer_destroy,
  13271. .create_handle = intel_user_framebuffer_create_handle,
  13272. .dirty = intel_user_framebuffer_dirty,
  13273. };
  13274. static
  13275. u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
  13276. uint64_t fb_modifier, uint32_t pixel_format)
  13277. {
  13278. u32 gen = INTEL_INFO(dev_priv)->gen;
  13279. if (gen >= 9) {
  13280. int cpp = drm_format_plane_cpp(pixel_format, 0);
  13281. /* "The stride in bytes must not exceed the of the size of 8K
  13282. * pixels and 32K bytes."
  13283. */
  13284. return min(8192 * cpp, 32768);
  13285. } else if (gen >= 5 && !IS_VALLEYVIEW(dev_priv) &&
  13286. !IS_CHERRYVIEW(dev_priv)) {
  13287. return 32*1024;
  13288. } else if (gen >= 4) {
  13289. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  13290. return 16*1024;
  13291. else
  13292. return 32*1024;
  13293. } else if (gen >= 3) {
  13294. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  13295. return 8*1024;
  13296. else
  13297. return 16*1024;
  13298. } else {
  13299. /* XXX DSPC is limited to 4k tiled */
  13300. return 8*1024;
  13301. }
  13302. }
  13303. static int intel_framebuffer_init(struct drm_device *dev,
  13304. struct intel_framebuffer *intel_fb,
  13305. struct drm_mode_fb_cmd2 *mode_cmd,
  13306. struct drm_i915_gem_object *obj)
  13307. {
  13308. struct drm_i915_private *dev_priv = to_i915(dev);
  13309. unsigned int tiling = i915_gem_object_get_tiling(obj);
  13310. int ret;
  13311. u32 pitch_limit, stride_alignment;
  13312. struct drm_format_name_buf format_name;
  13313. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  13314. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  13315. /*
  13316. * If there's a fence, enforce that
  13317. * the fb modifier and tiling mode match.
  13318. */
  13319. if (tiling != I915_TILING_NONE &&
  13320. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  13321. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  13322. return -EINVAL;
  13323. }
  13324. } else {
  13325. if (tiling == I915_TILING_X) {
  13326. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  13327. } else if (tiling == I915_TILING_Y) {
  13328. DRM_DEBUG("No Y tiling for legacy addfb\n");
  13329. return -EINVAL;
  13330. }
  13331. }
  13332. /* Passed in modifier sanity checking. */
  13333. switch (mode_cmd->modifier[0]) {
  13334. case I915_FORMAT_MOD_Y_TILED:
  13335. case I915_FORMAT_MOD_Yf_TILED:
  13336. if (INTEL_GEN(dev_priv) < 9) {
  13337. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  13338. mode_cmd->modifier[0]);
  13339. return -EINVAL;
  13340. }
  13341. case DRM_FORMAT_MOD_NONE:
  13342. case I915_FORMAT_MOD_X_TILED:
  13343. break;
  13344. default:
  13345. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  13346. mode_cmd->modifier[0]);
  13347. return -EINVAL;
  13348. }
  13349. /*
  13350. * gen2/3 display engine uses the fence if present,
  13351. * so the tiling mode must match the fb modifier exactly.
  13352. */
  13353. if (INTEL_INFO(dev_priv)->gen < 4 &&
  13354. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  13355. DRM_DEBUG("tiling_mode must match fb modifier exactly on gen2/3\n");
  13356. return -EINVAL;
  13357. }
  13358. stride_alignment = intel_fb_stride_alignment(dev_priv,
  13359. mode_cmd->modifier[0],
  13360. mode_cmd->pixel_format);
  13361. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  13362. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  13363. mode_cmd->pitches[0], stride_alignment);
  13364. return -EINVAL;
  13365. }
  13366. pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
  13367. mode_cmd->pixel_format);
  13368. if (mode_cmd->pitches[0] > pitch_limit) {
  13369. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  13370. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  13371. "tiled" : "linear",
  13372. mode_cmd->pitches[0], pitch_limit);
  13373. return -EINVAL;
  13374. }
  13375. /*
  13376. * If there's a fence, enforce that
  13377. * the fb pitch and fence stride match.
  13378. */
  13379. if (tiling != I915_TILING_NONE &&
  13380. mode_cmd->pitches[0] != i915_gem_object_get_stride(obj)) {
  13381. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  13382. mode_cmd->pitches[0],
  13383. i915_gem_object_get_stride(obj));
  13384. return -EINVAL;
  13385. }
  13386. /* Reject formats not supported by any plane early. */
  13387. switch (mode_cmd->pixel_format) {
  13388. case DRM_FORMAT_C8:
  13389. case DRM_FORMAT_RGB565:
  13390. case DRM_FORMAT_XRGB8888:
  13391. case DRM_FORMAT_ARGB8888:
  13392. break;
  13393. case DRM_FORMAT_XRGB1555:
  13394. if (INTEL_GEN(dev_priv) > 3) {
  13395. DRM_DEBUG("unsupported pixel format: %s\n",
  13396. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  13397. return -EINVAL;
  13398. }
  13399. break;
  13400. case DRM_FORMAT_ABGR8888:
  13401. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  13402. INTEL_GEN(dev_priv) < 9) {
  13403. DRM_DEBUG("unsupported pixel format: %s\n",
  13404. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  13405. return -EINVAL;
  13406. }
  13407. break;
  13408. case DRM_FORMAT_XBGR8888:
  13409. case DRM_FORMAT_XRGB2101010:
  13410. case DRM_FORMAT_XBGR2101010:
  13411. if (INTEL_GEN(dev_priv) < 4) {
  13412. DRM_DEBUG("unsupported pixel format: %s\n",
  13413. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  13414. return -EINVAL;
  13415. }
  13416. break;
  13417. case DRM_FORMAT_ABGR2101010:
  13418. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  13419. DRM_DEBUG("unsupported pixel format: %s\n",
  13420. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  13421. return -EINVAL;
  13422. }
  13423. break;
  13424. case DRM_FORMAT_YUYV:
  13425. case DRM_FORMAT_UYVY:
  13426. case DRM_FORMAT_YVYU:
  13427. case DRM_FORMAT_VYUY:
  13428. if (INTEL_GEN(dev_priv) < 5) {
  13429. DRM_DEBUG("unsupported pixel format: %s\n",
  13430. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  13431. return -EINVAL;
  13432. }
  13433. break;
  13434. default:
  13435. DRM_DEBUG("unsupported pixel format: %s\n",
  13436. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  13437. return -EINVAL;
  13438. }
  13439. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  13440. if (mode_cmd->offsets[0] != 0)
  13441. return -EINVAL;
  13442. drm_helper_mode_fill_fb_struct(dev, &intel_fb->base, mode_cmd);
  13443. intel_fb->obj = obj;
  13444. ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
  13445. if (ret)
  13446. return ret;
  13447. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  13448. if (ret) {
  13449. DRM_ERROR("framebuffer init failed %d\n", ret);
  13450. return ret;
  13451. }
  13452. intel_fb->obj->framebuffer_references++;
  13453. return 0;
  13454. }
  13455. static struct drm_framebuffer *
  13456. intel_user_framebuffer_create(struct drm_device *dev,
  13457. struct drm_file *filp,
  13458. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  13459. {
  13460. struct drm_framebuffer *fb;
  13461. struct drm_i915_gem_object *obj;
  13462. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  13463. obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
  13464. if (!obj)
  13465. return ERR_PTR(-ENOENT);
  13466. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  13467. if (IS_ERR(fb))
  13468. i915_gem_object_put(obj);
  13469. return fb;
  13470. }
  13471. static void intel_atomic_state_free(struct drm_atomic_state *state)
  13472. {
  13473. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  13474. drm_atomic_state_default_release(state);
  13475. i915_sw_fence_fini(&intel_state->commit_ready);
  13476. kfree(state);
  13477. }
  13478. static const struct drm_mode_config_funcs intel_mode_funcs = {
  13479. .fb_create = intel_user_framebuffer_create,
  13480. .output_poll_changed = intel_fbdev_output_poll_changed,
  13481. .atomic_check = intel_atomic_check,
  13482. .atomic_commit = intel_atomic_commit,
  13483. .atomic_state_alloc = intel_atomic_state_alloc,
  13484. .atomic_state_clear = intel_atomic_state_clear,
  13485. .atomic_state_free = intel_atomic_state_free,
  13486. };
  13487. /**
  13488. * intel_init_display_hooks - initialize the display modesetting hooks
  13489. * @dev_priv: device private
  13490. */
  13491. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  13492. {
  13493. if (INTEL_INFO(dev_priv)->gen >= 9) {
  13494. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  13495. dev_priv->display.get_initial_plane_config =
  13496. skylake_get_initial_plane_config;
  13497. dev_priv->display.crtc_compute_clock =
  13498. haswell_crtc_compute_clock;
  13499. dev_priv->display.crtc_enable = haswell_crtc_enable;
  13500. dev_priv->display.crtc_disable = haswell_crtc_disable;
  13501. } else if (HAS_DDI(dev_priv)) {
  13502. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  13503. dev_priv->display.get_initial_plane_config =
  13504. ironlake_get_initial_plane_config;
  13505. dev_priv->display.crtc_compute_clock =
  13506. haswell_crtc_compute_clock;
  13507. dev_priv->display.crtc_enable = haswell_crtc_enable;
  13508. dev_priv->display.crtc_disable = haswell_crtc_disable;
  13509. } else if (HAS_PCH_SPLIT(dev_priv)) {
  13510. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  13511. dev_priv->display.get_initial_plane_config =
  13512. ironlake_get_initial_plane_config;
  13513. dev_priv->display.crtc_compute_clock =
  13514. ironlake_crtc_compute_clock;
  13515. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  13516. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  13517. } else if (IS_CHERRYVIEW(dev_priv)) {
  13518. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13519. dev_priv->display.get_initial_plane_config =
  13520. i9xx_get_initial_plane_config;
  13521. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  13522. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  13523. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13524. } else if (IS_VALLEYVIEW(dev_priv)) {
  13525. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13526. dev_priv->display.get_initial_plane_config =
  13527. i9xx_get_initial_plane_config;
  13528. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  13529. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  13530. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13531. } else if (IS_G4X(dev_priv)) {
  13532. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13533. dev_priv->display.get_initial_plane_config =
  13534. i9xx_get_initial_plane_config;
  13535. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  13536. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13537. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13538. } else if (IS_PINEVIEW(dev_priv)) {
  13539. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13540. dev_priv->display.get_initial_plane_config =
  13541. i9xx_get_initial_plane_config;
  13542. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  13543. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13544. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13545. } else if (!IS_GEN2(dev_priv)) {
  13546. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13547. dev_priv->display.get_initial_plane_config =
  13548. i9xx_get_initial_plane_config;
  13549. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  13550. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13551. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13552. } else {
  13553. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13554. dev_priv->display.get_initial_plane_config =
  13555. i9xx_get_initial_plane_config;
  13556. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  13557. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13558. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13559. }
  13560. /* Returns the core display clock speed */
  13561. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  13562. dev_priv->display.get_display_clock_speed =
  13563. skylake_get_display_clock_speed;
  13564. else if (IS_GEN9_LP(dev_priv))
  13565. dev_priv->display.get_display_clock_speed =
  13566. broxton_get_display_clock_speed;
  13567. else if (IS_BROADWELL(dev_priv))
  13568. dev_priv->display.get_display_clock_speed =
  13569. broadwell_get_display_clock_speed;
  13570. else if (IS_HASWELL(dev_priv))
  13571. dev_priv->display.get_display_clock_speed =
  13572. haswell_get_display_clock_speed;
  13573. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  13574. dev_priv->display.get_display_clock_speed =
  13575. valleyview_get_display_clock_speed;
  13576. else if (IS_GEN5(dev_priv))
  13577. dev_priv->display.get_display_clock_speed =
  13578. ilk_get_display_clock_speed;
  13579. else if (IS_I945G(dev_priv) || IS_I965G(dev_priv) ||
  13580. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  13581. dev_priv->display.get_display_clock_speed =
  13582. i945_get_display_clock_speed;
  13583. else if (IS_GM45(dev_priv))
  13584. dev_priv->display.get_display_clock_speed =
  13585. gm45_get_display_clock_speed;
  13586. else if (IS_I965GM(dev_priv))
  13587. dev_priv->display.get_display_clock_speed =
  13588. i965gm_get_display_clock_speed;
  13589. else if (IS_PINEVIEW(dev_priv))
  13590. dev_priv->display.get_display_clock_speed =
  13591. pnv_get_display_clock_speed;
  13592. else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
  13593. dev_priv->display.get_display_clock_speed =
  13594. g33_get_display_clock_speed;
  13595. else if (IS_I915G(dev_priv))
  13596. dev_priv->display.get_display_clock_speed =
  13597. i915_get_display_clock_speed;
  13598. else if (IS_I945GM(dev_priv) || IS_I845G(dev_priv))
  13599. dev_priv->display.get_display_clock_speed =
  13600. i9xx_misc_get_display_clock_speed;
  13601. else if (IS_I915GM(dev_priv))
  13602. dev_priv->display.get_display_clock_speed =
  13603. i915gm_get_display_clock_speed;
  13604. else if (IS_I865G(dev_priv))
  13605. dev_priv->display.get_display_clock_speed =
  13606. i865_get_display_clock_speed;
  13607. else if (IS_I85X(dev_priv))
  13608. dev_priv->display.get_display_clock_speed =
  13609. i85x_get_display_clock_speed;
  13610. else { /* 830 */
  13611. WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
  13612. dev_priv->display.get_display_clock_speed =
  13613. i830_get_display_clock_speed;
  13614. }
  13615. if (IS_GEN5(dev_priv)) {
  13616. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  13617. } else if (IS_GEN6(dev_priv)) {
  13618. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  13619. } else if (IS_IVYBRIDGE(dev_priv)) {
  13620. /* FIXME: detect B0+ stepping and use auto training */
  13621. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  13622. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  13623. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  13624. }
  13625. if (IS_BROADWELL(dev_priv)) {
  13626. dev_priv->display.modeset_commit_cdclk =
  13627. broadwell_modeset_commit_cdclk;
  13628. dev_priv->display.modeset_calc_cdclk =
  13629. broadwell_modeset_calc_cdclk;
  13630. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  13631. dev_priv->display.modeset_commit_cdclk =
  13632. valleyview_modeset_commit_cdclk;
  13633. dev_priv->display.modeset_calc_cdclk =
  13634. valleyview_modeset_calc_cdclk;
  13635. } else if (IS_GEN9_LP(dev_priv)) {
  13636. dev_priv->display.modeset_commit_cdclk =
  13637. bxt_modeset_commit_cdclk;
  13638. dev_priv->display.modeset_calc_cdclk =
  13639. bxt_modeset_calc_cdclk;
  13640. } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
  13641. dev_priv->display.modeset_commit_cdclk =
  13642. skl_modeset_commit_cdclk;
  13643. dev_priv->display.modeset_calc_cdclk =
  13644. skl_modeset_calc_cdclk;
  13645. }
  13646. if (dev_priv->info.gen >= 9)
  13647. dev_priv->display.update_crtcs = skl_update_crtcs;
  13648. else
  13649. dev_priv->display.update_crtcs = intel_update_crtcs;
  13650. switch (INTEL_INFO(dev_priv)->gen) {
  13651. case 2:
  13652. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  13653. break;
  13654. case 3:
  13655. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  13656. break;
  13657. case 4:
  13658. case 5:
  13659. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  13660. break;
  13661. case 6:
  13662. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  13663. break;
  13664. case 7:
  13665. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  13666. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  13667. break;
  13668. case 9:
  13669. /* Drop through - unsupported since execlist only. */
  13670. default:
  13671. /* Default just returns -ENODEV to indicate unsupported */
  13672. dev_priv->display.queue_flip = intel_default_queue_flip;
  13673. }
  13674. }
  13675. /*
  13676. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  13677. * resume, or other times. This quirk makes sure that's the case for
  13678. * affected systems.
  13679. */
  13680. static void quirk_pipea_force(struct drm_device *dev)
  13681. {
  13682. struct drm_i915_private *dev_priv = to_i915(dev);
  13683. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  13684. DRM_INFO("applying pipe a force quirk\n");
  13685. }
  13686. static void quirk_pipeb_force(struct drm_device *dev)
  13687. {
  13688. struct drm_i915_private *dev_priv = to_i915(dev);
  13689. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  13690. DRM_INFO("applying pipe b force quirk\n");
  13691. }
  13692. /*
  13693. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  13694. */
  13695. static void quirk_ssc_force_disable(struct drm_device *dev)
  13696. {
  13697. struct drm_i915_private *dev_priv = to_i915(dev);
  13698. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  13699. DRM_INFO("applying lvds SSC disable quirk\n");
  13700. }
  13701. /*
  13702. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  13703. * brightness value
  13704. */
  13705. static void quirk_invert_brightness(struct drm_device *dev)
  13706. {
  13707. struct drm_i915_private *dev_priv = to_i915(dev);
  13708. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  13709. DRM_INFO("applying inverted panel brightness quirk\n");
  13710. }
  13711. /* Some VBT's incorrectly indicate no backlight is present */
  13712. static void quirk_backlight_present(struct drm_device *dev)
  13713. {
  13714. struct drm_i915_private *dev_priv = to_i915(dev);
  13715. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  13716. DRM_INFO("applying backlight present quirk\n");
  13717. }
  13718. struct intel_quirk {
  13719. int device;
  13720. int subsystem_vendor;
  13721. int subsystem_device;
  13722. void (*hook)(struct drm_device *dev);
  13723. };
  13724. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  13725. struct intel_dmi_quirk {
  13726. void (*hook)(struct drm_device *dev);
  13727. const struct dmi_system_id (*dmi_id_list)[];
  13728. };
  13729. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  13730. {
  13731. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  13732. return 1;
  13733. }
  13734. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  13735. {
  13736. .dmi_id_list = &(const struct dmi_system_id[]) {
  13737. {
  13738. .callback = intel_dmi_reverse_brightness,
  13739. .ident = "NCR Corporation",
  13740. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  13741. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  13742. },
  13743. },
  13744. { } /* terminating entry */
  13745. },
  13746. .hook = quirk_invert_brightness,
  13747. },
  13748. };
  13749. static struct intel_quirk intel_quirks[] = {
  13750. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  13751. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  13752. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  13753. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  13754. /* 830 needs to leave pipe A & dpll A up */
  13755. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  13756. /* 830 needs to leave pipe B & dpll B up */
  13757. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  13758. /* Lenovo U160 cannot use SSC on LVDS */
  13759. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  13760. /* Sony Vaio Y cannot use SSC on LVDS */
  13761. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  13762. /* Acer Aspire 5734Z must invert backlight brightness */
  13763. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  13764. /* Acer/eMachines G725 */
  13765. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  13766. /* Acer/eMachines e725 */
  13767. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  13768. /* Acer/Packard Bell NCL20 */
  13769. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  13770. /* Acer Aspire 4736Z */
  13771. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  13772. /* Acer Aspire 5336 */
  13773. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  13774. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  13775. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  13776. /* Acer C720 Chromebook (Core i3 4005U) */
  13777. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  13778. /* Apple Macbook 2,1 (Core 2 T7400) */
  13779. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  13780. /* Apple Macbook 4,1 */
  13781. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  13782. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  13783. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  13784. /* HP Chromebook 14 (Celeron 2955U) */
  13785. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  13786. /* Dell Chromebook 11 */
  13787. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  13788. /* Dell Chromebook 11 (2015 version) */
  13789. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  13790. };
  13791. static void intel_init_quirks(struct drm_device *dev)
  13792. {
  13793. struct pci_dev *d = dev->pdev;
  13794. int i;
  13795. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  13796. struct intel_quirk *q = &intel_quirks[i];
  13797. if (d->device == q->device &&
  13798. (d->subsystem_vendor == q->subsystem_vendor ||
  13799. q->subsystem_vendor == PCI_ANY_ID) &&
  13800. (d->subsystem_device == q->subsystem_device ||
  13801. q->subsystem_device == PCI_ANY_ID))
  13802. q->hook(dev);
  13803. }
  13804. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  13805. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  13806. intel_dmi_quirks[i].hook(dev);
  13807. }
  13808. }
  13809. /* Disable the VGA plane that we never use */
  13810. static void i915_disable_vga(struct drm_i915_private *dev_priv)
  13811. {
  13812. struct pci_dev *pdev = dev_priv->drm.pdev;
  13813. u8 sr1;
  13814. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  13815. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  13816. vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
  13817. outb(SR01, VGA_SR_INDEX);
  13818. sr1 = inb(VGA_SR_DATA);
  13819. outb(sr1 | 1<<5, VGA_SR_DATA);
  13820. vga_put(pdev, VGA_RSRC_LEGACY_IO);
  13821. udelay(300);
  13822. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  13823. POSTING_READ(vga_reg);
  13824. }
  13825. void intel_modeset_init_hw(struct drm_device *dev)
  13826. {
  13827. struct drm_i915_private *dev_priv = to_i915(dev);
  13828. intel_update_cdclk(dev_priv);
  13829. dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
  13830. intel_init_clock_gating(dev_priv);
  13831. }
  13832. /*
  13833. * Calculate what we think the watermarks should be for the state we've read
  13834. * out of the hardware and then immediately program those watermarks so that
  13835. * we ensure the hardware settings match our internal state.
  13836. *
  13837. * We can calculate what we think WM's should be by creating a duplicate of the
  13838. * current state (which was constructed during hardware readout) and running it
  13839. * through the atomic check code to calculate new watermark values in the
  13840. * state object.
  13841. */
  13842. static void sanitize_watermarks(struct drm_device *dev)
  13843. {
  13844. struct drm_i915_private *dev_priv = to_i915(dev);
  13845. struct drm_atomic_state *state;
  13846. struct intel_atomic_state *intel_state;
  13847. struct drm_crtc *crtc;
  13848. struct drm_crtc_state *cstate;
  13849. struct drm_modeset_acquire_ctx ctx;
  13850. int ret;
  13851. int i;
  13852. /* Only supported on platforms that use atomic watermark design */
  13853. if (!dev_priv->display.optimize_watermarks)
  13854. return;
  13855. /*
  13856. * We need to hold connection_mutex before calling duplicate_state so
  13857. * that the connector loop is protected.
  13858. */
  13859. drm_modeset_acquire_init(&ctx, 0);
  13860. retry:
  13861. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  13862. if (ret == -EDEADLK) {
  13863. drm_modeset_backoff(&ctx);
  13864. goto retry;
  13865. } else if (WARN_ON(ret)) {
  13866. goto fail;
  13867. }
  13868. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  13869. if (WARN_ON(IS_ERR(state)))
  13870. goto fail;
  13871. intel_state = to_intel_atomic_state(state);
  13872. /*
  13873. * Hardware readout is the only time we don't want to calculate
  13874. * intermediate watermarks (since we don't trust the current
  13875. * watermarks).
  13876. */
  13877. intel_state->skip_intermediate_wm = true;
  13878. ret = intel_atomic_check(dev, state);
  13879. if (ret) {
  13880. /*
  13881. * If we fail here, it means that the hardware appears to be
  13882. * programmed in a way that shouldn't be possible, given our
  13883. * understanding of watermark requirements. This might mean a
  13884. * mistake in the hardware readout code or a mistake in the
  13885. * watermark calculations for a given platform. Raise a WARN
  13886. * so that this is noticeable.
  13887. *
  13888. * If this actually happens, we'll have to just leave the
  13889. * BIOS-programmed watermarks untouched and hope for the best.
  13890. */
  13891. WARN(true, "Could not determine valid watermarks for inherited state\n");
  13892. goto put_state;
  13893. }
  13894. /* Write calculated watermark values back */
  13895. for_each_crtc_in_state(state, crtc, cstate, i) {
  13896. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  13897. cs->wm.need_postvbl_update = true;
  13898. dev_priv->display.optimize_watermarks(intel_state, cs);
  13899. }
  13900. put_state:
  13901. drm_atomic_state_put(state);
  13902. fail:
  13903. drm_modeset_drop_locks(&ctx);
  13904. drm_modeset_acquire_fini(&ctx);
  13905. }
  13906. int intel_modeset_init(struct drm_device *dev)
  13907. {
  13908. struct drm_i915_private *dev_priv = to_i915(dev);
  13909. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  13910. enum pipe pipe;
  13911. struct intel_crtc *crtc;
  13912. drm_mode_config_init(dev);
  13913. dev->mode_config.min_width = 0;
  13914. dev->mode_config.min_height = 0;
  13915. dev->mode_config.preferred_depth = 24;
  13916. dev->mode_config.prefer_shadow = 1;
  13917. dev->mode_config.allow_fb_modifiers = true;
  13918. dev->mode_config.funcs = &intel_mode_funcs;
  13919. INIT_WORK(&dev_priv->atomic_helper.free_work,
  13920. intel_atomic_helper_free_state_worker);
  13921. intel_init_quirks(dev);
  13922. intel_init_pm(dev_priv);
  13923. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  13924. return 0;
  13925. /*
  13926. * There may be no VBT; and if the BIOS enabled SSC we can
  13927. * just keep using it to avoid unnecessary flicker. Whereas if the
  13928. * BIOS isn't using it, don't assume it will work even if the VBT
  13929. * indicates as much.
  13930. */
  13931. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
  13932. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  13933. DREF_SSC1_ENABLE);
  13934. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  13935. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  13936. bios_lvds_use_ssc ? "en" : "dis",
  13937. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  13938. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  13939. }
  13940. }
  13941. if (IS_GEN2(dev_priv)) {
  13942. dev->mode_config.max_width = 2048;
  13943. dev->mode_config.max_height = 2048;
  13944. } else if (IS_GEN3(dev_priv)) {
  13945. dev->mode_config.max_width = 4096;
  13946. dev->mode_config.max_height = 4096;
  13947. } else {
  13948. dev->mode_config.max_width = 8192;
  13949. dev->mode_config.max_height = 8192;
  13950. }
  13951. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  13952. dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
  13953. dev->mode_config.cursor_height = 1023;
  13954. } else if (IS_GEN2(dev_priv)) {
  13955. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  13956. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  13957. } else {
  13958. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  13959. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  13960. }
  13961. dev->mode_config.fb_base = ggtt->mappable_base;
  13962. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  13963. INTEL_INFO(dev_priv)->num_pipes,
  13964. INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
  13965. for_each_pipe(dev_priv, pipe) {
  13966. int ret;
  13967. ret = intel_crtc_init(dev_priv, pipe);
  13968. if (ret) {
  13969. drm_mode_config_cleanup(dev);
  13970. return ret;
  13971. }
  13972. }
  13973. intel_shared_dpll_init(dev);
  13974. intel_update_czclk(dev_priv);
  13975. intel_modeset_init_hw(dev);
  13976. if (dev_priv->max_cdclk_freq == 0)
  13977. intel_update_max_cdclk(dev_priv);
  13978. /* Just disable it once at startup */
  13979. i915_disable_vga(dev_priv);
  13980. intel_setup_outputs(dev_priv);
  13981. drm_modeset_lock_all(dev);
  13982. intel_modeset_setup_hw_state(dev);
  13983. drm_modeset_unlock_all(dev);
  13984. for_each_intel_crtc(dev, crtc) {
  13985. struct intel_initial_plane_config plane_config = {};
  13986. if (!crtc->active)
  13987. continue;
  13988. /*
  13989. * Note that reserving the BIOS fb up front prevents us
  13990. * from stuffing other stolen allocations like the ring
  13991. * on top. This prevents some ugliness at boot time, and
  13992. * can even allow for smooth boot transitions if the BIOS
  13993. * fb is large enough for the active pipe configuration.
  13994. */
  13995. dev_priv->display.get_initial_plane_config(crtc,
  13996. &plane_config);
  13997. /*
  13998. * If the fb is shared between multiple heads, we'll
  13999. * just get the first one.
  14000. */
  14001. intel_find_initial_plane_obj(crtc, &plane_config);
  14002. }
  14003. /*
  14004. * Make sure hardware watermarks really match the state we read out.
  14005. * Note that we need to do this after reconstructing the BIOS fb's
  14006. * since the watermark calculation done here will use pstate->fb.
  14007. */
  14008. sanitize_watermarks(dev);
  14009. return 0;
  14010. }
  14011. static void intel_enable_pipe_a(struct drm_device *dev)
  14012. {
  14013. struct intel_connector *connector;
  14014. struct drm_connector *crt = NULL;
  14015. struct intel_load_detect_pipe load_detect_temp;
  14016. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  14017. /* We can't just switch on the pipe A, we need to set things up with a
  14018. * proper mode and output configuration. As a gross hack, enable pipe A
  14019. * by enabling the load detect pipe once. */
  14020. for_each_intel_connector(dev, connector) {
  14021. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  14022. crt = &connector->base;
  14023. break;
  14024. }
  14025. }
  14026. if (!crt)
  14027. return;
  14028. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  14029. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  14030. }
  14031. static bool
  14032. intel_check_plane_mapping(struct intel_crtc *crtc)
  14033. {
  14034. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  14035. u32 val;
  14036. if (INTEL_INFO(dev_priv)->num_pipes == 1)
  14037. return true;
  14038. val = I915_READ(DSPCNTR(!crtc->plane));
  14039. if ((val & DISPLAY_PLANE_ENABLE) &&
  14040. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  14041. return false;
  14042. return true;
  14043. }
  14044. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  14045. {
  14046. struct drm_device *dev = crtc->base.dev;
  14047. struct intel_encoder *encoder;
  14048. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  14049. return true;
  14050. return false;
  14051. }
  14052. static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
  14053. {
  14054. struct drm_device *dev = encoder->base.dev;
  14055. struct intel_connector *connector;
  14056. for_each_connector_on_encoder(dev, &encoder->base, connector)
  14057. return connector;
  14058. return NULL;
  14059. }
  14060. static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
  14061. enum transcoder pch_transcoder)
  14062. {
  14063. return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
  14064. (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
  14065. }
  14066. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  14067. {
  14068. struct drm_device *dev = crtc->base.dev;
  14069. struct drm_i915_private *dev_priv = to_i915(dev);
  14070. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  14071. /* Clear any frame start delays used for debugging left by the BIOS */
  14072. if (!transcoder_is_dsi(cpu_transcoder)) {
  14073. i915_reg_t reg = PIPECONF(cpu_transcoder);
  14074. I915_WRITE(reg,
  14075. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  14076. }
  14077. /* restore vblank interrupts to correct state */
  14078. drm_crtc_vblank_reset(&crtc->base);
  14079. if (crtc->active) {
  14080. struct intel_plane *plane;
  14081. drm_crtc_vblank_on(&crtc->base);
  14082. /* Disable everything but the primary plane */
  14083. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  14084. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  14085. continue;
  14086. plane->disable_plane(&plane->base, &crtc->base);
  14087. }
  14088. }
  14089. /* We need to sanitize the plane -> pipe mapping first because this will
  14090. * disable the crtc (and hence change the state) if it is wrong. Note
  14091. * that gen4+ has a fixed plane -> pipe mapping. */
  14092. if (INTEL_GEN(dev_priv) < 4 && !intel_check_plane_mapping(crtc)) {
  14093. bool plane;
  14094. DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
  14095. crtc->base.base.id, crtc->base.name);
  14096. /* Pipe has the wrong plane attached and the plane is active.
  14097. * Temporarily change the plane mapping and disable everything
  14098. * ... */
  14099. plane = crtc->plane;
  14100. crtc->base.primary->state->visible = true;
  14101. crtc->plane = !plane;
  14102. intel_crtc_disable_noatomic(&crtc->base);
  14103. crtc->plane = plane;
  14104. }
  14105. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  14106. crtc->pipe == PIPE_A && !crtc->active) {
  14107. /* BIOS forgot to enable pipe A, this mostly happens after
  14108. * resume. Force-enable the pipe to fix this, the update_dpms
  14109. * call below we restore the pipe to the right state, but leave
  14110. * the required bits on. */
  14111. intel_enable_pipe_a(dev);
  14112. }
  14113. /* Adjust the state of the output pipe according to whether we
  14114. * have active connectors/encoders. */
  14115. if (crtc->active && !intel_crtc_has_encoders(crtc))
  14116. intel_crtc_disable_noatomic(&crtc->base);
  14117. if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
  14118. /*
  14119. * We start out with underrun reporting disabled to avoid races.
  14120. * For correct bookkeeping mark this on active crtcs.
  14121. *
  14122. * Also on gmch platforms we dont have any hardware bits to
  14123. * disable the underrun reporting. Which means we need to start
  14124. * out with underrun reporting disabled also on inactive pipes,
  14125. * since otherwise we'll complain about the garbage we read when
  14126. * e.g. coming up after runtime pm.
  14127. *
  14128. * No protection against concurrent access is required - at
  14129. * worst a fifo underrun happens which also sets this to false.
  14130. */
  14131. crtc->cpu_fifo_underrun_disabled = true;
  14132. /*
  14133. * We track the PCH trancoder underrun reporting state
  14134. * within the crtc. With crtc for pipe A housing the underrun
  14135. * reporting state for PCH transcoder A, crtc for pipe B housing
  14136. * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
  14137. * and marking underrun reporting as disabled for the non-existing
  14138. * PCH transcoders B and C would prevent enabling the south
  14139. * error interrupt (see cpt_can_enable_serr_int()).
  14140. */
  14141. if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
  14142. crtc->pch_fifo_underrun_disabled = true;
  14143. }
  14144. }
  14145. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  14146. {
  14147. struct intel_connector *connector;
  14148. /* We need to check both for a crtc link (meaning that the
  14149. * encoder is active and trying to read from a pipe) and the
  14150. * pipe itself being active. */
  14151. bool has_active_crtc = encoder->base.crtc &&
  14152. to_intel_crtc(encoder->base.crtc)->active;
  14153. connector = intel_encoder_find_connector(encoder);
  14154. if (connector && !has_active_crtc) {
  14155. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  14156. encoder->base.base.id,
  14157. encoder->base.name);
  14158. /* Connector is active, but has no active pipe. This is
  14159. * fallout from our resume register restoring. Disable
  14160. * the encoder manually again. */
  14161. if (encoder->base.crtc) {
  14162. struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
  14163. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  14164. encoder->base.base.id,
  14165. encoder->base.name);
  14166. encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  14167. if (encoder->post_disable)
  14168. encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  14169. }
  14170. encoder->base.crtc = NULL;
  14171. /* Inconsistent output/port/pipe state happens presumably due to
  14172. * a bug in one of the get_hw_state functions. Or someplace else
  14173. * in our code, like the register restore mess on resume. Clamp
  14174. * things to off as a safer default. */
  14175. connector->base.dpms = DRM_MODE_DPMS_OFF;
  14176. connector->base.encoder = NULL;
  14177. }
  14178. /* Enabled encoders without active connectors will be fixed in
  14179. * the crtc fixup. */
  14180. }
  14181. void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
  14182. {
  14183. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  14184. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  14185. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  14186. i915_disable_vga(dev_priv);
  14187. }
  14188. }
  14189. void i915_redisable_vga(struct drm_i915_private *dev_priv)
  14190. {
  14191. /* This function can be called both from intel_modeset_setup_hw_state or
  14192. * at a very early point in our resume sequence, where the power well
  14193. * structures are not yet restored. Since this function is at a very
  14194. * paranoid "someone might have enabled VGA while we were not looking"
  14195. * level, just check if the power well is enabled instead of trying to
  14196. * follow the "don't touch the power well if we don't need it" policy
  14197. * the rest of the driver uses. */
  14198. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  14199. return;
  14200. i915_redisable_vga_power_on(dev_priv);
  14201. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  14202. }
  14203. static bool primary_get_hw_state(struct intel_plane *plane)
  14204. {
  14205. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  14206. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  14207. }
  14208. /* FIXME read out full plane state for all planes */
  14209. static void readout_plane_state(struct intel_crtc *crtc)
  14210. {
  14211. struct drm_plane *primary = crtc->base.primary;
  14212. struct intel_plane_state *plane_state =
  14213. to_intel_plane_state(primary->state);
  14214. plane_state->base.visible = crtc->active &&
  14215. primary_get_hw_state(to_intel_plane(primary));
  14216. if (plane_state->base.visible)
  14217. crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
  14218. }
  14219. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  14220. {
  14221. struct drm_i915_private *dev_priv = to_i915(dev);
  14222. enum pipe pipe;
  14223. struct intel_crtc *crtc;
  14224. struct intel_encoder *encoder;
  14225. struct intel_connector *connector;
  14226. int i;
  14227. dev_priv->active_crtcs = 0;
  14228. for_each_intel_crtc(dev, crtc) {
  14229. struct intel_crtc_state *crtc_state =
  14230. to_intel_crtc_state(crtc->base.state);
  14231. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  14232. memset(crtc_state, 0, sizeof(*crtc_state));
  14233. crtc_state->base.crtc = &crtc->base;
  14234. crtc_state->base.active = crtc_state->base.enable =
  14235. dev_priv->display.get_pipe_config(crtc, crtc_state);
  14236. crtc->base.enabled = crtc_state->base.enable;
  14237. crtc->active = crtc_state->base.active;
  14238. if (crtc_state->base.active)
  14239. dev_priv->active_crtcs |= 1 << crtc->pipe;
  14240. readout_plane_state(crtc);
  14241. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  14242. crtc->base.base.id, crtc->base.name,
  14243. enableddisabled(crtc_state->base.active));
  14244. }
  14245. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  14246. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  14247. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  14248. &pll->state.hw_state);
  14249. pll->state.crtc_mask = 0;
  14250. for_each_intel_crtc(dev, crtc) {
  14251. struct intel_crtc_state *crtc_state =
  14252. to_intel_crtc_state(crtc->base.state);
  14253. if (crtc_state->base.active &&
  14254. crtc_state->shared_dpll == pll)
  14255. pll->state.crtc_mask |= 1 << crtc->pipe;
  14256. }
  14257. pll->active_mask = pll->state.crtc_mask;
  14258. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  14259. pll->name, pll->state.crtc_mask, pll->on);
  14260. }
  14261. for_each_intel_encoder(dev, encoder) {
  14262. pipe = 0;
  14263. if (encoder->get_hw_state(encoder, &pipe)) {
  14264. struct intel_crtc_state *crtc_state;
  14265. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  14266. crtc_state = to_intel_crtc_state(crtc->base.state);
  14267. encoder->base.crtc = &crtc->base;
  14268. crtc_state->output_types |= 1 << encoder->type;
  14269. encoder->get_config(encoder, crtc_state);
  14270. } else {
  14271. encoder->base.crtc = NULL;
  14272. }
  14273. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  14274. encoder->base.base.id, encoder->base.name,
  14275. enableddisabled(encoder->base.crtc),
  14276. pipe_name(pipe));
  14277. }
  14278. for_each_intel_connector(dev, connector) {
  14279. if (connector->get_hw_state(connector)) {
  14280. connector->base.dpms = DRM_MODE_DPMS_ON;
  14281. encoder = connector->encoder;
  14282. connector->base.encoder = &encoder->base;
  14283. if (encoder->base.crtc &&
  14284. encoder->base.crtc->state->active) {
  14285. /*
  14286. * This has to be done during hardware readout
  14287. * because anything calling .crtc_disable may
  14288. * rely on the connector_mask being accurate.
  14289. */
  14290. encoder->base.crtc->state->connector_mask |=
  14291. 1 << drm_connector_index(&connector->base);
  14292. encoder->base.crtc->state->encoder_mask |=
  14293. 1 << drm_encoder_index(&encoder->base);
  14294. }
  14295. } else {
  14296. connector->base.dpms = DRM_MODE_DPMS_OFF;
  14297. connector->base.encoder = NULL;
  14298. }
  14299. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  14300. connector->base.base.id, connector->base.name,
  14301. enableddisabled(connector->base.encoder));
  14302. }
  14303. for_each_intel_crtc(dev, crtc) {
  14304. struct intel_crtc_state *crtc_state =
  14305. to_intel_crtc_state(crtc->base.state);
  14306. int pixclk = 0;
  14307. crtc->base.hwmode = crtc_state->base.adjusted_mode;
  14308. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  14309. if (crtc_state->base.active) {
  14310. intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
  14311. intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
  14312. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  14313. /*
  14314. * The initial mode needs to be set in order to keep
  14315. * the atomic core happy. It wants a valid mode if the
  14316. * crtc's enabled, so we do the above call.
  14317. *
  14318. * But we don't set all the derived state fully, hence
  14319. * set a flag to indicate that a full recalculation is
  14320. * needed on the next commit.
  14321. */
  14322. crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
  14323. if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
  14324. pixclk = ilk_pipe_pixel_rate(crtc_state);
  14325. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  14326. pixclk = crtc_state->base.adjusted_mode.crtc_clock;
  14327. else
  14328. WARN_ON(dev_priv->display.modeset_calc_cdclk);
  14329. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  14330. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  14331. pixclk = DIV_ROUND_UP(pixclk * 100, 95);
  14332. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  14333. update_scanline_offset(crtc);
  14334. }
  14335. dev_priv->min_pixclk[crtc->pipe] = pixclk;
  14336. intel_pipe_config_sanity_check(dev_priv, crtc_state);
  14337. }
  14338. }
  14339. /* Scan out the current hw modeset state,
  14340. * and sanitizes it to the current state
  14341. */
  14342. static void
  14343. intel_modeset_setup_hw_state(struct drm_device *dev)
  14344. {
  14345. struct drm_i915_private *dev_priv = to_i915(dev);
  14346. enum pipe pipe;
  14347. struct intel_crtc *crtc;
  14348. struct intel_encoder *encoder;
  14349. int i;
  14350. intel_modeset_readout_hw_state(dev);
  14351. /* HW state is read out, now we need to sanitize this mess. */
  14352. for_each_intel_encoder(dev, encoder) {
  14353. intel_sanitize_encoder(encoder);
  14354. }
  14355. for_each_pipe(dev_priv, pipe) {
  14356. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  14357. intel_sanitize_crtc(crtc);
  14358. intel_dump_pipe_config(crtc, crtc->config,
  14359. "[setup_hw_state]");
  14360. }
  14361. intel_modeset_update_connector_atomic_state(dev);
  14362. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  14363. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  14364. if (!pll->on || pll->active_mask)
  14365. continue;
  14366. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  14367. pll->funcs.disable(dev_priv, pll);
  14368. pll->on = false;
  14369. }
  14370. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  14371. vlv_wm_get_hw_state(dev);
  14372. else if (IS_GEN9(dev_priv))
  14373. skl_wm_get_hw_state(dev);
  14374. else if (HAS_PCH_SPLIT(dev_priv))
  14375. ilk_wm_get_hw_state(dev);
  14376. for_each_intel_crtc(dev, crtc) {
  14377. unsigned long put_domains;
  14378. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  14379. if (WARN_ON(put_domains))
  14380. modeset_put_power_domains(dev_priv, put_domains);
  14381. }
  14382. intel_display_set_init_power(dev_priv, false);
  14383. intel_fbc_init_pipe_state(dev_priv);
  14384. }
  14385. void intel_display_resume(struct drm_device *dev)
  14386. {
  14387. struct drm_i915_private *dev_priv = to_i915(dev);
  14388. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  14389. struct drm_modeset_acquire_ctx ctx;
  14390. int ret;
  14391. dev_priv->modeset_restore_state = NULL;
  14392. if (state)
  14393. state->acquire_ctx = &ctx;
  14394. /*
  14395. * This is a cludge because with real atomic modeset mode_config.mutex
  14396. * won't be taken. Unfortunately some probed state like
  14397. * audio_codec_enable is still protected by mode_config.mutex, so lock
  14398. * it here for now.
  14399. */
  14400. mutex_lock(&dev->mode_config.mutex);
  14401. drm_modeset_acquire_init(&ctx, 0);
  14402. while (1) {
  14403. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  14404. if (ret != -EDEADLK)
  14405. break;
  14406. drm_modeset_backoff(&ctx);
  14407. }
  14408. if (!ret)
  14409. ret = __intel_display_resume(dev, state);
  14410. drm_modeset_drop_locks(&ctx);
  14411. drm_modeset_acquire_fini(&ctx);
  14412. mutex_unlock(&dev->mode_config.mutex);
  14413. if (ret)
  14414. DRM_ERROR("Restoring old state failed with %i\n", ret);
  14415. if (state)
  14416. drm_atomic_state_put(state);
  14417. }
  14418. void intel_modeset_gem_init(struct drm_device *dev)
  14419. {
  14420. struct drm_i915_private *dev_priv = to_i915(dev);
  14421. intel_init_gt_powersave(dev_priv);
  14422. intel_setup_overlay(dev_priv);
  14423. }
  14424. int intel_connector_register(struct drm_connector *connector)
  14425. {
  14426. struct intel_connector *intel_connector = to_intel_connector(connector);
  14427. int ret;
  14428. ret = intel_backlight_device_register(intel_connector);
  14429. if (ret)
  14430. goto err;
  14431. return 0;
  14432. err:
  14433. return ret;
  14434. }
  14435. void intel_connector_unregister(struct drm_connector *connector)
  14436. {
  14437. struct intel_connector *intel_connector = to_intel_connector(connector);
  14438. intel_backlight_device_unregister(intel_connector);
  14439. intel_panel_destroy_backlight(connector);
  14440. }
  14441. void intel_modeset_cleanup(struct drm_device *dev)
  14442. {
  14443. struct drm_i915_private *dev_priv = to_i915(dev);
  14444. flush_work(&dev_priv->atomic_helper.free_work);
  14445. WARN_ON(!llist_empty(&dev_priv->atomic_helper.free_list));
  14446. intel_disable_gt_powersave(dev_priv);
  14447. /*
  14448. * Interrupts and polling as the first thing to avoid creating havoc.
  14449. * Too much stuff here (turning of connectors, ...) would
  14450. * experience fancy races otherwise.
  14451. */
  14452. intel_irq_uninstall(dev_priv);
  14453. /*
  14454. * Due to the hpd irq storm handling the hotplug work can re-arm the
  14455. * poll handlers. Hence disable polling after hpd handling is shut down.
  14456. */
  14457. drm_kms_helper_poll_fini(dev);
  14458. intel_unregister_dsm_handler();
  14459. intel_fbc_global_disable(dev_priv);
  14460. /* flush any delayed tasks or pending work */
  14461. flush_scheduled_work();
  14462. drm_mode_config_cleanup(dev);
  14463. intel_cleanup_overlay(dev_priv);
  14464. intel_cleanup_gt_powersave(dev_priv);
  14465. intel_teardown_gmbus(dev_priv);
  14466. }
  14467. void intel_connector_attach_encoder(struct intel_connector *connector,
  14468. struct intel_encoder *encoder)
  14469. {
  14470. connector->encoder = encoder;
  14471. drm_mode_connector_attach_encoder(&connector->base,
  14472. &encoder->base);
  14473. }
  14474. /*
  14475. * set vga decode state - true == enable VGA decode
  14476. */
  14477. int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
  14478. {
  14479. unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  14480. u16 gmch_ctrl;
  14481. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  14482. DRM_ERROR("failed to read control word\n");
  14483. return -EIO;
  14484. }
  14485. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  14486. return 0;
  14487. if (state)
  14488. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  14489. else
  14490. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  14491. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  14492. DRM_ERROR("failed to write control word\n");
  14493. return -EIO;
  14494. }
  14495. return 0;
  14496. }
  14497. #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
  14498. struct intel_display_error_state {
  14499. u32 power_well_driver;
  14500. int num_transcoders;
  14501. struct intel_cursor_error_state {
  14502. u32 control;
  14503. u32 position;
  14504. u32 base;
  14505. u32 size;
  14506. } cursor[I915_MAX_PIPES];
  14507. struct intel_pipe_error_state {
  14508. bool power_domain_on;
  14509. u32 source;
  14510. u32 stat;
  14511. } pipe[I915_MAX_PIPES];
  14512. struct intel_plane_error_state {
  14513. u32 control;
  14514. u32 stride;
  14515. u32 size;
  14516. u32 pos;
  14517. u32 addr;
  14518. u32 surface;
  14519. u32 tile_offset;
  14520. } plane[I915_MAX_PIPES];
  14521. struct intel_transcoder_error_state {
  14522. bool power_domain_on;
  14523. enum transcoder cpu_transcoder;
  14524. u32 conf;
  14525. u32 htotal;
  14526. u32 hblank;
  14527. u32 hsync;
  14528. u32 vtotal;
  14529. u32 vblank;
  14530. u32 vsync;
  14531. } transcoder[4];
  14532. };
  14533. struct intel_display_error_state *
  14534. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  14535. {
  14536. struct intel_display_error_state *error;
  14537. int transcoders[] = {
  14538. TRANSCODER_A,
  14539. TRANSCODER_B,
  14540. TRANSCODER_C,
  14541. TRANSCODER_EDP,
  14542. };
  14543. int i;
  14544. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  14545. return NULL;
  14546. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  14547. if (error == NULL)
  14548. return NULL;
  14549. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  14550. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  14551. for_each_pipe(dev_priv, i) {
  14552. error->pipe[i].power_domain_on =
  14553. __intel_display_power_is_enabled(dev_priv,
  14554. POWER_DOMAIN_PIPE(i));
  14555. if (!error->pipe[i].power_domain_on)
  14556. continue;
  14557. error->cursor[i].control = I915_READ(CURCNTR(i));
  14558. error->cursor[i].position = I915_READ(CURPOS(i));
  14559. error->cursor[i].base = I915_READ(CURBASE(i));
  14560. error->plane[i].control = I915_READ(DSPCNTR(i));
  14561. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  14562. if (INTEL_GEN(dev_priv) <= 3) {
  14563. error->plane[i].size = I915_READ(DSPSIZE(i));
  14564. error->plane[i].pos = I915_READ(DSPPOS(i));
  14565. }
  14566. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  14567. error->plane[i].addr = I915_READ(DSPADDR(i));
  14568. if (INTEL_GEN(dev_priv) >= 4) {
  14569. error->plane[i].surface = I915_READ(DSPSURF(i));
  14570. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  14571. }
  14572. error->pipe[i].source = I915_READ(PIPESRC(i));
  14573. if (HAS_GMCH_DISPLAY(dev_priv))
  14574. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  14575. }
  14576. /* Note: this does not include DSI transcoders. */
  14577. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  14578. if (HAS_DDI(dev_priv))
  14579. error->num_transcoders++; /* Account for eDP. */
  14580. for (i = 0; i < error->num_transcoders; i++) {
  14581. enum transcoder cpu_transcoder = transcoders[i];
  14582. error->transcoder[i].power_domain_on =
  14583. __intel_display_power_is_enabled(dev_priv,
  14584. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  14585. if (!error->transcoder[i].power_domain_on)
  14586. continue;
  14587. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  14588. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  14589. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  14590. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  14591. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  14592. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  14593. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  14594. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  14595. }
  14596. return error;
  14597. }
  14598. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  14599. void
  14600. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  14601. struct drm_i915_private *dev_priv,
  14602. struct intel_display_error_state *error)
  14603. {
  14604. int i;
  14605. if (!error)
  14606. return;
  14607. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
  14608. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  14609. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  14610. error->power_well_driver);
  14611. for_each_pipe(dev_priv, i) {
  14612. err_printf(m, "Pipe [%d]:\n", i);
  14613. err_printf(m, " Power: %s\n",
  14614. onoff(error->pipe[i].power_domain_on));
  14615. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  14616. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  14617. err_printf(m, "Plane [%d]:\n", i);
  14618. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  14619. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  14620. if (INTEL_GEN(dev_priv) <= 3) {
  14621. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  14622. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  14623. }
  14624. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  14625. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  14626. if (INTEL_GEN(dev_priv) >= 4) {
  14627. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  14628. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  14629. }
  14630. err_printf(m, "Cursor [%d]:\n", i);
  14631. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  14632. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  14633. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  14634. }
  14635. for (i = 0; i < error->num_transcoders; i++) {
  14636. err_printf(m, "CPU transcoder: %s\n",
  14637. transcoder_name(error->transcoder[i].cpu_transcoder));
  14638. err_printf(m, " Power: %s\n",
  14639. onoff(error->transcoder[i].power_domain_on));
  14640. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  14641. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  14642. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  14643. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  14644. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  14645. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  14646. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  14647. }
  14648. }
  14649. #endif