intel_display.c 487 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include "intel_frontbuffer.h"
  37. #include <drm/i915_drm.h>
  38. #include "i915_drv.h"
  39. #include "intel_dsi.h"
  40. #include "i915_trace.h"
  41. #include <drm/drm_atomic.h>
  42. #include <drm/drm_atomic_helper.h>
  43. #include <drm/drm_dp_helper.h>
  44. #include <drm/drm_crtc_helper.h>
  45. #include <drm/drm_plane_helper.h>
  46. #include <drm/drm_rect.h>
  47. #include <linux/dma_remapping.h>
  48. #include <linux/reservation.h>
  49. static bool is_mmio_work(struct intel_flip_work *work)
  50. {
  51. return work->mmio_work.func;
  52. }
  53. /* Primary plane formats for gen <= 3 */
  54. static const uint32_t i8xx_primary_formats[] = {
  55. DRM_FORMAT_C8,
  56. DRM_FORMAT_RGB565,
  57. DRM_FORMAT_XRGB1555,
  58. DRM_FORMAT_XRGB8888,
  59. };
  60. /* Primary plane formats for gen >= 4 */
  61. static const uint32_t i965_primary_formats[] = {
  62. DRM_FORMAT_C8,
  63. DRM_FORMAT_RGB565,
  64. DRM_FORMAT_XRGB8888,
  65. DRM_FORMAT_XBGR8888,
  66. DRM_FORMAT_XRGB2101010,
  67. DRM_FORMAT_XBGR2101010,
  68. };
  69. static const uint32_t skl_primary_formats[] = {
  70. DRM_FORMAT_C8,
  71. DRM_FORMAT_RGB565,
  72. DRM_FORMAT_XRGB8888,
  73. DRM_FORMAT_XBGR8888,
  74. DRM_FORMAT_ARGB8888,
  75. DRM_FORMAT_ABGR8888,
  76. DRM_FORMAT_XRGB2101010,
  77. DRM_FORMAT_XBGR2101010,
  78. DRM_FORMAT_YUYV,
  79. DRM_FORMAT_YVYU,
  80. DRM_FORMAT_UYVY,
  81. DRM_FORMAT_VYUY,
  82. };
  83. /* Cursor formats */
  84. static const uint32_t intel_cursor_formats[] = {
  85. DRM_FORMAT_ARGB8888,
  86. };
  87. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  88. struct intel_crtc_state *pipe_config);
  89. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  90. struct intel_crtc_state *pipe_config);
  91. static int intel_framebuffer_init(struct drm_device *dev,
  92. struct intel_framebuffer *ifb,
  93. struct drm_mode_fb_cmd2 *mode_cmd,
  94. struct drm_i915_gem_object *obj);
  95. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  96. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  97. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
  98. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  99. struct intel_link_m_n *m_n,
  100. struct intel_link_m_n *m2_n2);
  101. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  102. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  103. static void haswell_set_pipemisc(struct drm_crtc *crtc);
  104. static void vlv_prepare_pll(struct intel_crtc *crtc,
  105. const struct intel_crtc_state *pipe_config);
  106. static void chv_prepare_pll(struct intel_crtc *crtc,
  107. const struct intel_crtc_state *pipe_config);
  108. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  109. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  110. static void skl_init_scalers(struct drm_i915_private *dev_priv,
  111. struct intel_crtc *crtc,
  112. struct intel_crtc_state *crtc_state);
  113. static void skylake_pfit_enable(struct intel_crtc *crtc);
  114. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
  115. static void ironlake_pfit_enable(struct intel_crtc *crtc);
  116. static void intel_modeset_setup_hw_state(struct drm_device *dev);
  117. static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
  118. static int ilk_max_pixel_rate(struct drm_atomic_state *state);
  119. static int bxt_calc_cdclk(int max_pixclk);
  120. struct intel_limit {
  121. struct {
  122. int min, max;
  123. } dot, vco, n, m, m1, m2, p, p1;
  124. struct {
  125. int dot_limit;
  126. int p2_slow, p2_fast;
  127. } p2;
  128. };
  129. /* returns HPLL frequency in kHz */
  130. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  131. {
  132. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  133. /* Obtain SKU information */
  134. mutex_lock(&dev_priv->sb_lock);
  135. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  136. CCK_FUSE_HPLL_FREQ_MASK;
  137. mutex_unlock(&dev_priv->sb_lock);
  138. return vco_freq[hpll_freq] * 1000;
  139. }
  140. int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
  141. const char *name, u32 reg, int ref_freq)
  142. {
  143. u32 val;
  144. int divider;
  145. mutex_lock(&dev_priv->sb_lock);
  146. val = vlv_cck_read(dev_priv, reg);
  147. mutex_unlock(&dev_priv->sb_lock);
  148. divider = val & CCK_FREQUENCY_VALUES;
  149. WARN((val & CCK_FREQUENCY_STATUS) !=
  150. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  151. "%s change in progress\n", name);
  152. return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
  153. }
  154. static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  155. const char *name, u32 reg)
  156. {
  157. if (dev_priv->hpll_freq == 0)
  158. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  159. return vlv_get_cck_clock(dev_priv, name, reg,
  160. dev_priv->hpll_freq);
  161. }
  162. static int
  163. intel_pch_rawclk(struct drm_i915_private *dev_priv)
  164. {
  165. return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
  166. }
  167. static int
  168. intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
  169. {
  170. /* RAWCLK_FREQ_VLV register updated from power well code */
  171. return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
  172. CCK_DISPLAY_REF_CLOCK_CONTROL);
  173. }
  174. static int
  175. intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
  176. {
  177. uint32_t clkcfg;
  178. /* hrawclock is 1/4 the FSB frequency */
  179. clkcfg = I915_READ(CLKCFG);
  180. switch (clkcfg & CLKCFG_FSB_MASK) {
  181. case CLKCFG_FSB_400:
  182. return 100000;
  183. case CLKCFG_FSB_533:
  184. return 133333;
  185. case CLKCFG_FSB_667:
  186. return 166667;
  187. case CLKCFG_FSB_800:
  188. return 200000;
  189. case CLKCFG_FSB_1067:
  190. return 266667;
  191. case CLKCFG_FSB_1333:
  192. return 333333;
  193. /* these two are just a guess; one of them might be right */
  194. case CLKCFG_FSB_1600:
  195. case CLKCFG_FSB_1600_ALT:
  196. return 400000;
  197. default:
  198. return 133333;
  199. }
  200. }
  201. void intel_update_rawclk(struct drm_i915_private *dev_priv)
  202. {
  203. if (HAS_PCH_SPLIT(dev_priv))
  204. dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
  205. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  206. dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
  207. else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
  208. dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
  209. else
  210. return; /* no rawclk on other platforms, or no need to know it */
  211. DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
  212. }
  213. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  214. {
  215. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  216. return;
  217. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  218. CCK_CZ_CLOCK_CONTROL);
  219. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  220. }
  221. static inline u32 /* units of 100MHz */
  222. intel_fdi_link_freq(struct drm_i915_private *dev_priv,
  223. const struct intel_crtc_state *pipe_config)
  224. {
  225. if (HAS_DDI(dev_priv))
  226. return pipe_config->port_clock; /* SPLL */
  227. else if (IS_GEN5(dev_priv))
  228. return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
  229. else
  230. return 270000;
  231. }
  232. static const struct intel_limit intel_limits_i8xx_dac = {
  233. .dot = { .min = 25000, .max = 350000 },
  234. .vco = { .min = 908000, .max = 1512000 },
  235. .n = { .min = 2, .max = 16 },
  236. .m = { .min = 96, .max = 140 },
  237. .m1 = { .min = 18, .max = 26 },
  238. .m2 = { .min = 6, .max = 16 },
  239. .p = { .min = 4, .max = 128 },
  240. .p1 = { .min = 2, .max = 33 },
  241. .p2 = { .dot_limit = 165000,
  242. .p2_slow = 4, .p2_fast = 2 },
  243. };
  244. static const struct intel_limit intel_limits_i8xx_dvo = {
  245. .dot = { .min = 25000, .max = 350000 },
  246. .vco = { .min = 908000, .max = 1512000 },
  247. .n = { .min = 2, .max = 16 },
  248. .m = { .min = 96, .max = 140 },
  249. .m1 = { .min = 18, .max = 26 },
  250. .m2 = { .min = 6, .max = 16 },
  251. .p = { .min = 4, .max = 128 },
  252. .p1 = { .min = 2, .max = 33 },
  253. .p2 = { .dot_limit = 165000,
  254. .p2_slow = 4, .p2_fast = 4 },
  255. };
  256. static const struct intel_limit intel_limits_i8xx_lvds = {
  257. .dot = { .min = 25000, .max = 350000 },
  258. .vco = { .min = 908000, .max = 1512000 },
  259. .n = { .min = 2, .max = 16 },
  260. .m = { .min = 96, .max = 140 },
  261. .m1 = { .min = 18, .max = 26 },
  262. .m2 = { .min = 6, .max = 16 },
  263. .p = { .min = 4, .max = 128 },
  264. .p1 = { .min = 1, .max = 6 },
  265. .p2 = { .dot_limit = 165000,
  266. .p2_slow = 14, .p2_fast = 7 },
  267. };
  268. static const struct intel_limit intel_limits_i9xx_sdvo = {
  269. .dot = { .min = 20000, .max = 400000 },
  270. .vco = { .min = 1400000, .max = 2800000 },
  271. .n = { .min = 1, .max = 6 },
  272. .m = { .min = 70, .max = 120 },
  273. .m1 = { .min = 8, .max = 18 },
  274. .m2 = { .min = 3, .max = 7 },
  275. .p = { .min = 5, .max = 80 },
  276. .p1 = { .min = 1, .max = 8 },
  277. .p2 = { .dot_limit = 200000,
  278. .p2_slow = 10, .p2_fast = 5 },
  279. };
  280. static const struct intel_limit intel_limits_i9xx_lvds = {
  281. .dot = { .min = 20000, .max = 400000 },
  282. .vco = { .min = 1400000, .max = 2800000 },
  283. .n = { .min = 1, .max = 6 },
  284. .m = { .min = 70, .max = 120 },
  285. .m1 = { .min = 8, .max = 18 },
  286. .m2 = { .min = 3, .max = 7 },
  287. .p = { .min = 7, .max = 98 },
  288. .p1 = { .min = 1, .max = 8 },
  289. .p2 = { .dot_limit = 112000,
  290. .p2_slow = 14, .p2_fast = 7 },
  291. };
  292. static const struct intel_limit intel_limits_g4x_sdvo = {
  293. .dot = { .min = 25000, .max = 270000 },
  294. .vco = { .min = 1750000, .max = 3500000},
  295. .n = { .min = 1, .max = 4 },
  296. .m = { .min = 104, .max = 138 },
  297. .m1 = { .min = 17, .max = 23 },
  298. .m2 = { .min = 5, .max = 11 },
  299. .p = { .min = 10, .max = 30 },
  300. .p1 = { .min = 1, .max = 3},
  301. .p2 = { .dot_limit = 270000,
  302. .p2_slow = 10,
  303. .p2_fast = 10
  304. },
  305. };
  306. static const struct intel_limit intel_limits_g4x_hdmi = {
  307. .dot = { .min = 22000, .max = 400000 },
  308. .vco = { .min = 1750000, .max = 3500000},
  309. .n = { .min = 1, .max = 4 },
  310. .m = { .min = 104, .max = 138 },
  311. .m1 = { .min = 16, .max = 23 },
  312. .m2 = { .min = 5, .max = 11 },
  313. .p = { .min = 5, .max = 80 },
  314. .p1 = { .min = 1, .max = 8},
  315. .p2 = { .dot_limit = 165000,
  316. .p2_slow = 10, .p2_fast = 5 },
  317. };
  318. static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
  319. .dot = { .min = 20000, .max = 115000 },
  320. .vco = { .min = 1750000, .max = 3500000 },
  321. .n = { .min = 1, .max = 3 },
  322. .m = { .min = 104, .max = 138 },
  323. .m1 = { .min = 17, .max = 23 },
  324. .m2 = { .min = 5, .max = 11 },
  325. .p = { .min = 28, .max = 112 },
  326. .p1 = { .min = 2, .max = 8 },
  327. .p2 = { .dot_limit = 0,
  328. .p2_slow = 14, .p2_fast = 14
  329. },
  330. };
  331. static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
  332. .dot = { .min = 80000, .max = 224000 },
  333. .vco = { .min = 1750000, .max = 3500000 },
  334. .n = { .min = 1, .max = 3 },
  335. .m = { .min = 104, .max = 138 },
  336. .m1 = { .min = 17, .max = 23 },
  337. .m2 = { .min = 5, .max = 11 },
  338. .p = { .min = 14, .max = 42 },
  339. .p1 = { .min = 2, .max = 6 },
  340. .p2 = { .dot_limit = 0,
  341. .p2_slow = 7, .p2_fast = 7
  342. },
  343. };
  344. static const struct intel_limit intel_limits_pineview_sdvo = {
  345. .dot = { .min = 20000, .max = 400000},
  346. .vco = { .min = 1700000, .max = 3500000 },
  347. /* Pineview's Ncounter is a ring counter */
  348. .n = { .min = 3, .max = 6 },
  349. .m = { .min = 2, .max = 256 },
  350. /* Pineview only has one combined m divider, which we treat as m2. */
  351. .m1 = { .min = 0, .max = 0 },
  352. .m2 = { .min = 0, .max = 254 },
  353. .p = { .min = 5, .max = 80 },
  354. .p1 = { .min = 1, .max = 8 },
  355. .p2 = { .dot_limit = 200000,
  356. .p2_slow = 10, .p2_fast = 5 },
  357. };
  358. static const struct intel_limit intel_limits_pineview_lvds = {
  359. .dot = { .min = 20000, .max = 400000 },
  360. .vco = { .min = 1700000, .max = 3500000 },
  361. .n = { .min = 3, .max = 6 },
  362. .m = { .min = 2, .max = 256 },
  363. .m1 = { .min = 0, .max = 0 },
  364. .m2 = { .min = 0, .max = 254 },
  365. .p = { .min = 7, .max = 112 },
  366. .p1 = { .min = 1, .max = 8 },
  367. .p2 = { .dot_limit = 112000,
  368. .p2_slow = 14, .p2_fast = 14 },
  369. };
  370. /* Ironlake / Sandybridge
  371. *
  372. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  373. * the range value for them is (actual_value - 2).
  374. */
  375. static const struct intel_limit intel_limits_ironlake_dac = {
  376. .dot = { .min = 25000, .max = 350000 },
  377. .vco = { .min = 1760000, .max = 3510000 },
  378. .n = { .min = 1, .max = 5 },
  379. .m = { .min = 79, .max = 127 },
  380. .m1 = { .min = 12, .max = 22 },
  381. .m2 = { .min = 5, .max = 9 },
  382. .p = { .min = 5, .max = 80 },
  383. .p1 = { .min = 1, .max = 8 },
  384. .p2 = { .dot_limit = 225000,
  385. .p2_slow = 10, .p2_fast = 5 },
  386. };
  387. static const struct intel_limit intel_limits_ironlake_single_lvds = {
  388. .dot = { .min = 25000, .max = 350000 },
  389. .vco = { .min = 1760000, .max = 3510000 },
  390. .n = { .min = 1, .max = 3 },
  391. .m = { .min = 79, .max = 118 },
  392. .m1 = { .min = 12, .max = 22 },
  393. .m2 = { .min = 5, .max = 9 },
  394. .p = { .min = 28, .max = 112 },
  395. .p1 = { .min = 2, .max = 8 },
  396. .p2 = { .dot_limit = 225000,
  397. .p2_slow = 14, .p2_fast = 14 },
  398. };
  399. static const struct intel_limit intel_limits_ironlake_dual_lvds = {
  400. .dot = { .min = 25000, .max = 350000 },
  401. .vco = { .min = 1760000, .max = 3510000 },
  402. .n = { .min = 1, .max = 3 },
  403. .m = { .min = 79, .max = 127 },
  404. .m1 = { .min = 12, .max = 22 },
  405. .m2 = { .min = 5, .max = 9 },
  406. .p = { .min = 14, .max = 56 },
  407. .p1 = { .min = 2, .max = 8 },
  408. .p2 = { .dot_limit = 225000,
  409. .p2_slow = 7, .p2_fast = 7 },
  410. };
  411. /* LVDS 100mhz refclk limits. */
  412. static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
  413. .dot = { .min = 25000, .max = 350000 },
  414. .vco = { .min = 1760000, .max = 3510000 },
  415. .n = { .min = 1, .max = 2 },
  416. .m = { .min = 79, .max = 126 },
  417. .m1 = { .min = 12, .max = 22 },
  418. .m2 = { .min = 5, .max = 9 },
  419. .p = { .min = 28, .max = 112 },
  420. .p1 = { .min = 2, .max = 8 },
  421. .p2 = { .dot_limit = 225000,
  422. .p2_slow = 14, .p2_fast = 14 },
  423. };
  424. static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
  425. .dot = { .min = 25000, .max = 350000 },
  426. .vco = { .min = 1760000, .max = 3510000 },
  427. .n = { .min = 1, .max = 3 },
  428. .m = { .min = 79, .max = 126 },
  429. .m1 = { .min = 12, .max = 22 },
  430. .m2 = { .min = 5, .max = 9 },
  431. .p = { .min = 14, .max = 42 },
  432. .p1 = { .min = 2, .max = 6 },
  433. .p2 = { .dot_limit = 225000,
  434. .p2_slow = 7, .p2_fast = 7 },
  435. };
  436. static const struct intel_limit intel_limits_vlv = {
  437. /*
  438. * These are the data rate limits (measured in fast clocks)
  439. * since those are the strictest limits we have. The fast
  440. * clock and actual rate limits are more relaxed, so checking
  441. * them would make no difference.
  442. */
  443. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  444. .vco = { .min = 4000000, .max = 6000000 },
  445. .n = { .min = 1, .max = 7 },
  446. .m1 = { .min = 2, .max = 3 },
  447. .m2 = { .min = 11, .max = 156 },
  448. .p1 = { .min = 2, .max = 3 },
  449. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  450. };
  451. static const struct intel_limit intel_limits_chv = {
  452. /*
  453. * These are the data rate limits (measured in fast clocks)
  454. * since those are the strictest limits we have. The fast
  455. * clock and actual rate limits are more relaxed, so checking
  456. * them would make no difference.
  457. */
  458. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  459. .vco = { .min = 4800000, .max = 6480000 },
  460. .n = { .min = 1, .max = 1 },
  461. .m1 = { .min = 2, .max = 2 },
  462. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  463. .p1 = { .min = 2, .max = 4 },
  464. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  465. };
  466. static const struct intel_limit intel_limits_bxt = {
  467. /* FIXME: find real dot limits */
  468. .dot = { .min = 0, .max = INT_MAX },
  469. .vco = { .min = 4800000, .max = 6700000 },
  470. .n = { .min = 1, .max = 1 },
  471. .m1 = { .min = 2, .max = 2 },
  472. /* FIXME: find real m2 limits */
  473. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  474. .p1 = { .min = 2, .max = 4 },
  475. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  476. };
  477. static bool
  478. needs_modeset(struct drm_crtc_state *state)
  479. {
  480. return drm_atomic_crtc_needs_modeset(state);
  481. }
  482. /*
  483. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  484. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  485. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  486. * The helpers' return value is the rate of the clock that is fed to the
  487. * display engine's pipe which can be the above fast dot clock rate or a
  488. * divided-down version of it.
  489. */
  490. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  491. static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
  492. {
  493. clock->m = clock->m2 + 2;
  494. clock->p = clock->p1 * clock->p2;
  495. if (WARN_ON(clock->n == 0 || clock->p == 0))
  496. return 0;
  497. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  498. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  499. return clock->dot;
  500. }
  501. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  502. {
  503. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  504. }
  505. static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
  506. {
  507. clock->m = i9xx_dpll_compute_m(clock);
  508. clock->p = clock->p1 * clock->p2;
  509. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  510. return 0;
  511. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  512. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  513. return clock->dot;
  514. }
  515. static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
  516. {
  517. clock->m = clock->m1 * clock->m2;
  518. clock->p = clock->p1 * clock->p2;
  519. if (WARN_ON(clock->n == 0 || clock->p == 0))
  520. return 0;
  521. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  522. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  523. return clock->dot / 5;
  524. }
  525. int chv_calc_dpll_params(int refclk, struct dpll *clock)
  526. {
  527. clock->m = clock->m1 * clock->m2;
  528. clock->p = clock->p1 * clock->p2;
  529. if (WARN_ON(clock->n == 0 || clock->p == 0))
  530. return 0;
  531. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  532. clock->n << 22);
  533. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  534. return clock->dot / 5;
  535. }
  536. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  537. /**
  538. * Returns whether the given set of divisors are valid for a given refclk with
  539. * the given connectors.
  540. */
  541. static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
  542. const struct intel_limit *limit,
  543. const struct dpll *clock)
  544. {
  545. if (clock->n < limit->n.min || limit->n.max < clock->n)
  546. INTELPllInvalid("n out of range\n");
  547. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  548. INTELPllInvalid("p1 out of range\n");
  549. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  550. INTELPllInvalid("m2 out of range\n");
  551. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  552. INTELPllInvalid("m1 out of range\n");
  553. if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
  554. !IS_CHERRYVIEW(dev_priv) && !IS_BROXTON(dev_priv))
  555. if (clock->m1 <= clock->m2)
  556. INTELPllInvalid("m1 <= m2\n");
  557. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  558. !IS_BROXTON(dev_priv)) {
  559. if (clock->p < limit->p.min || limit->p.max < clock->p)
  560. INTELPllInvalid("p out of range\n");
  561. if (clock->m < limit->m.min || limit->m.max < clock->m)
  562. INTELPllInvalid("m out of range\n");
  563. }
  564. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  565. INTELPllInvalid("vco out of range\n");
  566. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  567. * connector, etc., rather than just a single range.
  568. */
  569. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  570. INTELPllInvalid("dot out of range\n");
  571. return true;
  572. }
  573. static int
  574. i9xx_select_p2_div(const struct intel_limit *limit,
  575. const struct intel_crtc_state *crtc_state,
  576. int target)
  577. {
  578. struct drm_device *dev = crtc_state->base.crtc->dev;
  579. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  580. /*
  581. * For LVDS just rely on its current settings for dual-channel.
  582. * We haven't figured out how to reliably set up different
  583. * single/dual channel state, if we even can.
  584. */
  585. if (intel_is_dual_link_lvds(dev))
  586. return limit->p2.p2_fast;
  587. else
  588. return limit->p2.p2_slow;
  589. } else {
  590. if (target < limit->p2.dot_limit)
  591. return limit->p2.p2_slow;
  592. else
  593. return limit->p2.p2_fast;
  594. }
  595. }
  596. /*
  597. * Returns a set of divisors for the desired target clock with the given
  598. * refclk, or FALSE. The returned values represent the clock equation:
  599. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  600. *
  601. * Target and reference clocks are specified in kHz.
  602. *
  603. * If match_clock is provided, then best_clock P divider must match the P
  604. * divider from @match_clock used for LVDS downclocking.
  605. */
  606. static bool
  607. i9xx_find_best_dpll(const struct intel_limit *limit,
  608. struct intel_crtc_state *crtc_state,
  609. int target, int refclk, struct dpll *match_clock,
  610. struct dpll *best_clock)
  611. {
  612. struct drm_device *dev = crtc_state->base.crtc->dev;
  613. struct dpll clock;
  614. int err = target;
  615. memset(best_clock, 0, sizeof(*best_clock));
  616. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  617. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  618. clock.m1++) {
  619. for (clock.m2 = limit->m2.min;
  620. clock.m2 <= limit->m2.max; clock.m2++) {
  621. if (clock.m2 >= clock.m1)
  622. break;
  623. for (clock.n = limit->n.min;
  624. clock.n <= limit->n.max; clock.n++) {
  625. for (clock.p1 = limit->p1.min;
  626. clock.p1 <= limit->p1.max; clock.p1++) {
  627. int this_err;
  628. i9xx_calc_dpll_params(refclk, &clock);
  629. if (!intel_PLL_is_valid(to_i915(dev),
  630. limit,
  631. &clock))
  632. continue;
  633. if (match_clock &&
  634. clock.p != match_clock->p)
  635. continue;
  636. this_err = abs(clock.dot - target);
  637. if (this_err < err) {
  638. *best_clock = clock;
  639. err = this_err;
  640. }
  641. }
  642. }
  643. }
  644. }
  645. return (err != target);
  646. }
  647. /*
  648. * Returns a set of divisors for the desired target clock with the given
  649. * refclk, or FALSE. The returned values represent the clock equation:
  650. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  651. *
  652. * Target and reference clocks are specified in kHz.
  653. *
  654. * If match_clock is provided, then best_clock P divider must match the P
  655. * divider from @match_clock used for LVDS downclocking.
  656. */
  657. static bool
  658. pnv_find_best_dpll(const struct intel_limit *limit,
  659. struct intel_crtc_state *crtc_state,
  660. int target, int refclk, struct dpll *match_clock,
  661. struct dpll *best_clock)
  662. {
  663. struct drm_device *dev = crtc_state->base.crtc->dev;
  664. struct dpll clock;
  665. int err = target;
  666. memset(best_clock, 0, sizeof(*best_clock));
  667. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  668. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  669. clock.m1++) {
  670. for (clock.m2 = limit->m2.min;
  671. clock.m2 <= limit->m2.max; clock.m2++) {
  672. for (clock.n = limit->n.min;
  673. clock.n <= limit->n.max; clock.n++) {
  674. for (clock.p1 = limit->p1.min;
  675. clock.p1 <= limit->p1.max; clock.p1++) {
  676. int this_err;
  677. pnv_calc_dpll_params(refclk, &clock);
  678. if (!intel_PLL_is_valid(to_i915(dev),
  679. limit,
  680. &clock))
  681. continue;
  682. if (match_clock &&
  683. clock.p != match_clock->p)
  684. continue;
  685. this_err = abs(clock.dot - target);
  686. if (this_err < err) {
  687. *best_clock = clock;
  688. err = this_err;
  689. }
  690. }
  691. }
  692. }
  693. }
  694. return (err != target);
  695. }
  696. /*
  697. * Returns a set of divisors for the desired target clock with the given
  698. * refclk, or FALSE. The returned values represent the clock equation:
  699. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  700. *
  701. * Target and reference clocks are specified in kHz.
  702. *
  703. * If match_clock is provided, then best_clock P divider must match the P
  704. * divider from @match_clock used for LVDS downclocking.
  705. */
  706. static bool
  707. g4x_find_best_dpll(const struct intel_limit *limit,
  708. struct intel_crtc_state *crtc_state,
  709. int target, int refclk, struct dpll *match_clock,
  710. struct dpll *best_clock)
  711. {
  712. struct drm_device *dev = crtc_state->base.crtc->dev;
  713. struct dpll clock;
  714. int max_n;
  715. bool found = false;
  716. /* approximately equals target * 0.00585 */
  717. int err_most = (target >> 8) + (target >> 9);
  718. memset(best_clock, 0, sizeof(*best_clock));
  719. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  720. max_n = limit->n.max;
  721. /* based on hardware requirement, prefer smaller n to precision */
  722. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  723. /* based on hardware requirement, prefere larger m1,m2 */
  724. for (clock.m1 = limit->m1.max;
  725. clock.m1 >= limit->m1.min; clock.m1--) {
  726. for (clock.m2 = limit->m2.max;
  727. clock.m2 >= limit->m2.min; clock.m2--) {
  728. for (clock.p1 = limit->p1.max;
  729. clock.p1 >= limit->p1.min; clock.p1--) {
  730. int this_err;
  731. i9xx_calc_dpll_params(refclk, &clock);
  732. if (!intel_PLL_is_valid(to_i915(dev),
  733. limit,
  734. &clock))
  735. continue;
  736. this_err = abs(clock.dot - target);
  737. if (this_err < err_most) {
  738. *best_clock = clock;
  739. err_most = this_err;
  740. max_n = clock.n;
  741. found = true;
  742. }
  743. }
  744. }
  745. }
  746. }
  747. return found;
  748. }
  749. /*
  750. * Check if the calculated PLL configuration is more optimal compared to the
  751. * best configuration and error found so far. Return the calculated error.
  752. */
  753. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  754. const struct dpll *calculated_clock,
  755. const struct dpll *best_clock,
  756. unsigned int best_error_ppm,
  757. unsigned int *error_ppm)
  758. {
  759. /*
  760. * For CHV ignore the error and consider only the P value.
  761. * Prefer a bigger P value based on HW requirements.
  762. */
  763. if (IS_CHERRYVIEW(to_i915(dev))) {
  764. *error_ppm = 0;
  765. return calculated_clock->p > best_clock->p;
  766. }
  767. if (WARN_ON_ONCE(!target_freq))
  768. return false;
  769. *error_ppm = div_u64(1000000ULL *
  770. abs(target_freq - calculated_clock->dot),
  771. target_freq);
  772. /*
  773. * Prefer a better P value over a better (smaller) error if the error
  774. * is small. Ensure this preference for future configurations too by
  775. * setting the error to 0.
  776. */
  777. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  778. *error_ppm = 0;
  779. return true;
  780. }
  781. return *error_ppm + 10 < best_error_ppm;
  782. }
  783. /*
  784. * Returns a set of divisors for the desired target clock with the given
  785. * refclk, or FALSE. The returned values represent the clock equation:
  786. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  787. */
  788. static bool
  789. vlv_find_best_dpll(const struct intel_limit *limit,
  790. struct intel_crtc_state *crtc_state,
  791. int target, int refclk, struct dpll *match_clock,
  792. struct dpll *best_clock)
  793. {
  794. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  795. struct drm_device *dev = crtc->base.dev;
  796. struct dpll clock;
  797. unsigned int bestppm = 1000000;
  798. /* min update 19.2 MHz */
  799. int max_n = min(limit->n.max, refclk / 19200);
  800. bool found = false;
  801. target *= 5; /* fast clock */
  802. memset(best_clock, 0, sizeof(*best_clock));
  803. /* based on hardware requirement, prefer smaller n to precision */
  804. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  805. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  806. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  807. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  808. clock.p = clock.p1 * clock.p2;
  809. /* based on hardware requirement, prefer bigger m1,m2 values */
  810. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  811. unsigned int ppm;
  812. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  813. refclk * clock.m1);
  814. vlv_calc_dpll_params(refclk, &clock);
  815. if (!intel_PLL_is_valid(to_i915(dev),
  816. limit,
  817. &clock))
  818. continue;
  819. if (!vlv_PLL_is_optimal(dev, target,
  820. &clock,
  821. best_clock,
  822. bestppm, &ppm))
  823. continue;
  824. *best_clock = clock;
  825. bestppm = ppm;
  826. found = true;
  827. }
  828. }
  829. }
  830. }
  831. return found;
  832. }
  833. /*
  834. * Returns a set of divisors for the desired target clock with the given
  835. * refclk, or FALSE. The returned values represent the clock equation:
  836. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  837. */
  838. static bool
  839. chv_find_best_dpll(const struct intel_limit *limit,
  840. struct intel_crtc_state *crtc_state,
  841. int target, int refclk, struct dpll *match_clock,
  842. struct dpll *best_clock)
  843. {
  844. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  845. struct drm_device *dev = crtc->base.dev;
  846. unsigned int best_error_ppm;
  847. struct dpll clock;
  848. uint64_t m2;
  849. int found = false;
  850. memset(best_clock, 0, sizeof(*best_clock));
  851. best_error_ppm = 1000000;
  852. /*
  853. * Based on hardware doc, the n always set to 1, and m1 always
  854. * set to 2. If requires to support 200Mhz refclk, we need to
  855. * revisit this because n may not 1 anymore.
  856. */
  857. clock.n = 1, clock.m1 = 2;
  858. target *= 5; /* fast clock */
  859. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  860. for (clock.p2 = limit->p2.p2_fast;
  861. clock.p2 >= limit->p2.p2_slow;
  862. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  863. unsigned int error_ppm;
  864. clock.p = clock.p1 * clock.p2;
  865. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  866. clock.n) << 22, refclk * clock.m1);
  867. if (m2 > INT_MAX/clock.m1)
  868. continue;
  869. clock.m2 = m2;
  870. chv_calc_dpll_params(refclk, &clock);
  871. if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
  872. continue;
  873. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  874. best_error_ppm, &error_ppm))
  875. continue;
  876. *best_clock = clock;
  877. best_error_ppm = error_ppm;
  878. found = true;
  879. }
  880. }
  881. return found;
  882. }
  883. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  884. struct dpll *best_clock)
  885. {
  886. int refclk = 100000;
  887. const struct intel_limit *limit = &intel_limits_bxt;
  888. return chv_find_best_dpll(limit, crtc_state,
  889. target_clock, refclk, NULL, best_clock);
  890. }
  891. bool intel_crtc_active(struct intel_crtc *crtc)
  892. {
  893. /* Be paranoid as we can arrive here with only partial
  894. * state retrieved from the hardware during setup.
  895. *
  896. * We can ditch the adjusted_mode.crtc_clock check as soon
  897. * as Haswell has gained clock readout/fastboot support.
  898. *
  899. * We can ditch the crtc->primary->fb check as soon as we can
  900. * properly reconstruct framebuffers.
  901. *
  902. * FIXME: The intel_crtc->active here should be switched to
  903. * crtc->state->active once we have proper CRTC states wired up
  904. * for atomic.
  905. */
  906. return crtc->active && crtc->base.primary->state->fb &&
  907. crtc->config->base.adjusted_mode.crtc_clock;
  908. }
  909. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  910. enum pipe pipe)
  911. {
  912. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  913. return crtc->config->cpu_transcoder;
  914. }
  915. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  916. {
  917. struct drm_i915_private *dev_priv = to_i915(dev);
  918. i915_reg_t reg = PIPEDSL(pipe);
  919. u32 line1, line2;
  920. u32 line_mask;
  921. if (IS_GEN2(dev_priv))
  922. line_mask = DSL_LINEMASK_GEN2;
  923. else
  924. line_mask = DSL_LINEMASK_GEN3;
  925. line1 = I915_READ(reg) & line_mask;
  926. msleep(5);
  927. line2 = I915_READ(reg) & line_mask;
  928. return line1 == line2;
  929. }
  930. /*
  931. * intel_wait_for_pipe_off - wait for pipe to turn off
  932. * @crtc: crtc whose pipe to wait for
  933. *
  934. * After disabling a pipe, we can't wait for vblank in the usual way,
  935. * spinning on the vblank interrupt status bit, since we won't actually
  936. * see an interrupt when the pipe is disabled.
  937. *
  938. * On Gen4 and above:
  939. * wait for the pipe register state bit to turn off
  940. *
  941. * Otherwise:
  942. * wait for the display line value to settle (it usually
  943. * ends up stopping at the start of the next frame).
  944. *
  945. */
  946. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  947. {
  948. struct drm_device *dev = crtc->base.dev;
  949. struct drm_i915_private *dev_priv = to_i915(dev);
  950. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  951. enum pipe pipe = crtc->pipe;
  952. if (INTEL_INFO(dev)->gen >= 4) {
  953. i915_reg_t reg = PIPECONF(cpu_transcoder);
  954. /* Wait for the Pipe State to go off */
  955. if (intel_wait_for_register(dev_priv,
  956. reg, I965_PIPECONF_ACTIVE, 0,
  957. 100))
  958. WARN(1, "pipe_off wait timed out\n");
  959. } else {
  960. /* Wait for the display line to settle */
  961. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  962. WARN(1, "pipe_off wait timed out\n");
  963. }
  964. }
  965. /* Only for pre-ILK configs */
  966. void assert_pll(struct drm_i915_private *dev_priv,
  967. enum pipe pipe, bool state)
  968. {
  969. u32 val;
  970. bool cur_state;
  971. val = I915_READ(DPLL(pipe));
  972. cur_state = !!(val & DPLL_VCO_ENABLE);
  973. I915_STATE_WARN(cur_state != state,
  974. "PLL state assertion failure (expected %s, current %s)\n",
  975. onoff(state), onoff(cur_state));
  976. }
  977. /* XXX: the dsi pll is shared between MIPI DSI ports */
  978. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  979. {
  980. u32 val;
  981. bool cur_state;
  982. mutex_lock(&dev_priv->sb_lock);
  983. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  984. mutex_unlock(&dev_priv->sb_lock);
  985. cur_state = val & DSI_PLL_VCO_EN;
  986. I915_STATE_WARN(cur_state != state,
  987. "DSI PLL state assertion failure (expected %s, current %s)\n",
  988. onoff(state), onoff(cur_state));
  989. }
  990. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  991. enum pipe pipe, bool state)
  992. {
  993. bool cur_state;
  994. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  995. pipe);
  996. if (HAS_DDI(dev_priv)) {
  997. /* DDI does not have a specific FDI_TX register */
  998. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  999. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1000. } else {
  1001. u32 val = I915_READ(FDI_TX_CTL(pipe));
  1002. cur_state = !!(val & FDI_TX_ENABLE);
  1003. }
  1004. I915_STATE_WARN(cur_state != state,
  1005. "FDI TX state assertion failure (expected %s, current %s)\n",
  1006. onoff(state), onoff(cur_state));
  1007. }
  1008. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1009. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1010. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1011. enum pipe pipe, bool state)
  1012. {
  1013. u32 val;
  1014. bool cur_state;
  1015. val = I915_READ(FDI_RX_CTL(pipe));
  1016. cur_state = !!(val & FDI_RX_ENABLE);
  1017. I915_STATE_WARN(cur_state != state,
  1018. "FDI RX state assertion failure (expected %s, current %s)\n",
  1019. onoff(state), onoff(cur_state));
  1020. }
  1021. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1022. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1023. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1024. enum pipe pipe)
  1025. {
  1026. u32 val;
  1027. /* ILK FDI PLL is always enabled */
  1028. if (IS_GEN5(dev_priv))
  1029. return;
  1030. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1031. if (HAS_DDI(dev_priv))
  1032. return;
  1033. val = I915_READ(FDI_TX_CTL(pipe));
  1034. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1035. }
  1036. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1037. enum pipe pipe, bool state)
  1038. {
  1039. u32 val;
  1040. bool cur_state;
  1041. val = I915_READ(FDI_RX_CTL(pipe));
  1042. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1043. I915_STATE_WARN(cur_state != state,
  1044. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1045. onoff(state), onoff(cur_state));
  1046. }
  1047. void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
  1048. {
  1049. i915_reg_t pp_reg;
  1050. u32 val;
  1051. enum pipe panel_pipe = PIPE_A;
  1052. bool locked = true;
  1053. if (WARN_ON(HAS_DDI(dev_priv)))
  1054. return;
  1055. if (HAS_PCH_SPLIT(dev_priv)) {
  1056. u32 port_sel;
  1057. pp_reg = PP_CONTROL(0);
  1058. port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
  1059. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1060. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1061. panel_pipe = PIPE_B;
  1062. /* XXX: else fix for eDP */
  1063. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1064. /* presumably write lock depends on pipe, not port select */
  1065. pp_reg = PP_CONTROL(pipe);
  1066. panel_pipe = pipe;
  1067. } else {
  1068. pp_reg = PP_CONTROL(0);
  1069. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1070. panel_pipe = PIPE_B;
  1071. }
  1072. val = I915_READ(pp_reg);
  1073. if (!(val & PANEL_POWER_ON) ||
  1074. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1075. locked = false;
  1076. I915_STATE_WARN(panel_pipe == pipe && locked,
  1077. "panel assertion failure, pipe %c regs locked\n",
  1078. pipe_name(pipe));
  1079. }
  1080. static void assert_cursor(struct drm_i915_private *dev_priv,
  1081. enum pipe pipe, bool state)
  1082. {
  1083. bool cur_state;
  1084. if (IS_845G(dev_priv) || IS_I865G(dev_priv))
  1085. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1086. else
  1087. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1088. I915_STATE_WARN(cur_state != state,
  1089. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1090. pipe_name(pipe), onoff(state), onoff(cur_state));
  1091. }
  1092. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1093. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1094. void assert_pipe(struct drm_i915_private *dev_priv,
  1095. enum pipe pipe, bool state)
  1096. {
  1097. bool cur_state;
  1098. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1099. pipe);
  1100. enum intel_display_power_domain power_domain;
  1101. /* if we need the pipe quirk it must be always on */
  1102. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1103. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1104. state = true;
  1105. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1106. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1107. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1108. cur_state = !!(val & PIPECONF_ENABLE);
  1109. intel_display_power_put(dev_priv, power_domain);
  1110. } else {
  1111. cur_state = false;
  1112. }
  1113. I915_STATE_WARN(cur_state != state,
  1114. "pipe %c assertion failure (expected %s, current %s)\n",
  1115. pipe_name(pipe), onoff(state), onoff(cur_state));
  1116. }
  1117. static void assert_plane(struct drm_i915_private *dev_priv,
  1118. enum plane plane, bool state)
  1119. {
  1120. u32 val;
  1121. bool cur_state;
  1122. val = I915_READ(DSPCNTR(plane));
  1123. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1124. I915_STATE_WARN(cur_state != state,
  1125. "plane %c assertion failure (expected %s, current %s)\n",
  1126. plane_name(plane), onoff(state), onoff(cur_state));
  1127. }
  1128. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1129. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1130. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1131. enum pipe pipe)
  1132. {
  1133. struct drm_device *dev = &dev_priv->drm;
  1134. int i;
  1135. /* Primary planes are fixed to pipes on gen4+ */
  1136. if (INTEL_INFO(dev)->gen >= 4) {
  1137. u32 val = I915_READ(DSPCNTR(pipe));
  1138. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1139. "plane %c assertion failure, should be disabled but not\n",
  1140. plane_name(pipe));
  1141. return;
  1142. }
  1143. /* Need to check both planes against the pipe */
  1144. for_each_pipe(dev_priv, i) {
  1145. u32 val = I915_READ(DSPCNTR(i));
  1146. enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1147. DISPPLANE_SEL_PIPE_SHIFT;
  1148. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1149. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1150. plane_name(i), pipe_name(pipe));
  1151. }
  1152. }
  1153. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1154. enum pipe pipe)
  1155. {
  1156. struct drm_device *dev = &dev_priv->drm;
  1157. int sprite;
  1158. if (INTEL_INFO(dev)->gen >= 9) {
  1159. for_each_sprite(dev_priv, pipe, sprite) {
  1160. u32 val = I915_READ(PLANE_CTL(pipe, sprite));
  1161. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1162. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1163. sprite, pipe_name(pipe));
  1164. }
  1165. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1166. for_each_sprite(dev_priv, pipe, sprite) {
  1167. u32 val = I915_READ(SPCNTR(pipe, sprite));
  1168. I915_STATE_WARN(val & SP_ENABLE,
  1169. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1170. sprite_name(pipe, sprite), pipe_name(pipe));
  1171. }
  1172. } else if (INTEL_INFO(dev)->gen >= 7) {
  1173. u32 val = I915_READ(SPRCTL(pipe));
  1174. I915_STATE_WARN(val & SPRITE_ENABLE,
  1175. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1176. plane_name(pipe), pipe_name(pipe));
  1177. } else if (INTEL_INFO(dev)->gen >= 5) {
  1178. u32 val = I915_READ(DVSCNTR(pipe));
  1179. I915_STATE_WARN(val & DVS_ENABLE,
  1180. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1181. plane_name(pipe), pipe_name(pipe));
  1182. }
  1183. }
  1184. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1185. {
  1186. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1187. drm_crtc_vblank_put(crtc);
  1188. }
  1189. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1190. enum pipe pipe)
  1191. {
  1192. u32 val;
  1193. bool enabled;
  1194. val = I915_READ(PCH_TRANSCONF(pipe));
  1195. enabled = !!(val & TRANS_ENABLE);
  1196. I915_STATE_WARN(enabled,
  1197. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1198. pipe_name(pipe));
  1199. }
  1200. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1201. enum pipe pipe, u32 port_sel, u32 val)
  1202. {
  1203. if ((val & DP_PORT_EN) == 0)
  1204. return false;
  1205. if (HAS_PCH_CPT(dev_priv)) {
  1206. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1207. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1208. return false;
  1209. } else if (IS_CHERRYVIEW(dev_priv)) {
  1210. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1211. return false;
  1212. } else {
  1213. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1214. return false;
  1215. }
  1216. return true;
  1217. }
  1218. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1219. enum pipe pipe, u32 val)
  1220. {
  1221. if ((val & SDVO_ENABLE) == 0)
  1222. return false;
  1223. if (HAS_PCH_CPT(dev_priv)) {
  1224. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1225. return false;
  1226. } else if (IS_CHERRYVIEW(dev_priv)) {
  1227. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1228. return false;
  1229. } else {
  1230. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1231. return false;
  1232. }
  1233. return true;
  1234. }
  1235. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1236. enum pipe pipe, u32 val)
  1237. {
  1238. if ((val & LVDS_PORT_EN) == 0)
  1239. return false;
  1240. if (HAS_PCH_CPT(dev_priv)) {
  1241. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1242. return false;
  1243. } else {
  1244. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1245. return false;
  1246. }
  1247. return true;
  1248. }
  1249. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1250. enum pipe pipe, u32 val)
  1251. {
  1252. if ((val & ADPA_DAC_ENABLE) == 0)
  1253. return false;
  1254. if (HAS_PCH_CPT(dev_priv)) {
  1255. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1256. return false;
  1257. } else {
  1258. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1259. return false;
  1260. }
  1261. return true;
  1262. }
  1263. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1264. enum pipe pipe, i915_reg_t reg,
  1265. u32 port_sel)
  1266. {
  1267. u32 val = I915_READ(reg);
  1268. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1269. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1270. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1271. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
  1272. && (val & DP_PIPEB_SELECT),
  1273. "IBX PCH dp port still using transcoder B\n");
  1274. }
  1275. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1276. enum pipe pipe, i915_reg_t reg)
  1277. {
  1278. u32 val = I915_READ(reg);
  1279. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1280. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1281. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1282. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
  1283. && (val & SDVO_PIPE_B_SELECT),
  1284. "IBX PCH hdmi port still using transcoder B\n");
  1285. }
  1286. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1287. enum pipe pipe)
  1288. {
  1289. u32 val;
  1290. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1291. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1292. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1293. val = I915_READ(PCH_ADPA);
  1294. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1295. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1296. pipe_name(pipe));
  1297. val = I915_READ(PCH_LVDS);
  1298. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1299. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1300. pipe_name(pipe));
  1301. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1302. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1303. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1304. }
  1305. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1306. const struct intel_crtc_state *pipe_config)
  1307. {
  1308. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1309. enum pipe pipe = crtc->pipe;
  1310. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1311. POSTING_READ(DPLL(pipe));
  1312. udelay(150);
  1313. if (intel_wait_for_register(dev_priv,
  1314. DPLL(pipe),
  1315. DPLL_LOCK_VLV,
  1316. DPLL_LOCK_VLV,
  1317. 1))
  1318. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1319. }
  1320. static void vlv_enable_pll(struct intel_crtc *crtc,
  1321. const struct intel_crtc_state *pipe_config)
  1322. {
  1323. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1324. enum pipe pipe = crtc->pipe;
  1325. assert_pipe_disabled(dev_priv, pipe);
  1326. /* PLL is protected by panel, make sure we can write it */
  1327. assert_panel_unlocked(dev_priv, pipe);
  1328. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1329. _vlv_enable_pll(crtc, pipe_config);
  1330. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1331. POSTING_READ(DPLL_MD(pipe));
  1332. }
  1333. static void _chv_enable_pll(struct intel_crtc *crtc,
  1334. const struct intel_crtc_state *pipe_config)
  1335. {
  1336. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1337. enum pipe pipe = crtc->pipe;
  1338. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1339. u32 tmp;
  1340. mutex_lock(&dev_priv->sb_lock);
  1341. /* Enable back the 10bit clock to display controller */
  1342. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1343. tmp |= DPIO_DCLKP_EN;
  1344. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1345. mutex_unlock(&dev_priv->sb_lock);
  1346. /*
  1347. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1348. */
  1349. udelay(1);
  1350. /* Enable PLL */
  1351. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1352. /* Check PLL is locked */
  1353. if (intel_wait_for_register(dev_priv,
  1354. DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
  1355. 1))
  1356. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1357. }
  1358. static void chv_enable_pll(struct intel_crtc *crtc,
  1359. const struct intel_crtc_state *pipe_config)
  1360. {
  1361. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1362. enum pipe pipe = crtc->pipe;
  1363. assert_pipe_disabled(dev_priv, pipe);
  1364. /* PLL is protected by panel, make sure we can write it */
  1365. assert_panel_unlocked(dev_priv, pipe);
  1366. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1367. _chv_enable_pll(crtc, pipe_config);
  1368. if (pipe != PIPE_A) {
  1369. /*
  1370. * WaPixelRepeatModeFixForC0:chv
  1371. *
  1372. * DPLLCMD is AWOL. Use chicken bits to propagate
  1373. * the value from DPLLBMD to either pipe B or C.
  1374. */
  1375. I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
  1376. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1377. I915_WRITE(CBR4_VLV, 0);
  1378. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1379. /*
  1380. * DPLLB VGA mode also seems to cause problems.
  1381. * We should always have it disabled.
  1382. */
  1383. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1384. } else {
  1385. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1386. POSTING_READ(DPLL_MD(pipe));
  1387. }
  1388. }
  1389. static int intel_num_dvo_pipes(struct drm_device *dev)
  1390. {
  1391. struct intel_crtc *crtc;
  1392. int count = 0;
  1393. for_each_intel_crtc(dev, crtc) {
  1394. count += crtc->base.state->active &&
  1395. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
  1396. }
  1397. return count;
  1398. }
  1399. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1400. {
  1401. struct drm_device *dev = crtc->base.dev;
  1402. struct drm_i915_private *dev_priv = to_i915(dev);
  1403. i915_reg_t reg = DPLL(crtc->pipe);
  1404. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1405. assert_pipe_disabled(dev_priv, crtc->pipe);
  1406. /* PLL is protected by panel, make sure we can write it */
  1407. if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
  1408. assert_panel_unlocked(dev_priv, crtc->pipe);
  1409. /* Enable DVO 2x clock on both PLLs if necessary */
  1410. if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev) > 0) {
  1411. /*
  1412. * It appears to be important that we don't enable this
  1413. * for the current pipe before otherwise configuring the
  1414. * PLL. No idea how this should be handled if multiple
  1415. * DVO outputs are enabled simultaneosly.
  1416. */
  1417. dpll |= DPLL_DVO_2X_MODE;
  1418. I915_WRITE(DPLL(!crtc->pipe),
  1419. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1420. }
  1421. /*
  1422. * Apparently we need to have VGA mode enabled prior to changing
  1423. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1424. * dividers, even though the register value does change.
  1425. */
  1426. I915_WRITE(reg, 0);
  1427. I915_WRITE(reg, dpll);
  1428. /* Wait for the clocks to stabilize. */
  1429. POSTING_READ(reg);
  1430. udelay(150);
  1431. if (INTEL_INFO(dev)->gen >= 4) {
  1432. I915_WRITE(DPLL_MD(crtc->pipe),
  1433. crtc->config->dpll_hw_state.dpll_md);
  1434. } else {
  1435. /* The pixel multiplier can only be updated once the
  1436. * DPLL is enabled and the clocks are stable.
  1437. *
  1438. * So write it again.
  1439. */
  1440. I915_WRITE(reg, dpll);
  1441. }
  1442. /* We do this three times for luck */
  1443. I915_WRITE(reg, dpll);
  1444. POSTING_READ(reg);
  1445. udelay(150); /* wait for warmup */
  1446. I915_WRITE(reg, dpll);
  1447. POSTING_READ(reg);
  1448. udelay(150); /* wait for warmup */
  1449. I915_WRITE(reg, dpll);
  1450. POSTING_READ(reg);
  1451. udelay(150); /* wait for warmup */
  1452. }
  1453. /**
  1454. * i9xx_disable_pll - disable a PLL
  1455. * @dev_priv: i915 private structure
  1456. * @pipe: pipe PLL to disable
  1457. *
  1458. * Disable the PLL for @pipe, making sure the pipe is off first.
  1459. *
  1460. * Note! This is for pre-ILK only.
  1461. */
  1462. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1463. {
  1464. struct drm_device *dev = crtc->base.dev;
  1465. struct drm_i915_private *dev_priv = to_i915(dev);
  1466. enum pipe pipe = crtc->pipe;
  1467. /* Disable DVO 2x clock on both PLLs if necessary */
  1468. if (IS_I830(dev_priv) &&
  1469. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
  1470. !intel_num_dvo_pipes(dev)) {
  1471. I915_WRITE(DPLL(PIPE_B),
  1472. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1473. I915_WRITE(DPLL(PIPE_A),
  1474. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1475. }
  1476. /* Don't disable pipe or pipe PLLs if needed */
  1477. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1478. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1479. return;
  1480. /* Make sure the pipe isn't still relying on us */
  1481. assert_pipe_disabled(dev_priv, pipe);
  1482. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1483. POSTING_READ(DPLL(pipe));
  1484. }
  1485. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1486. {
  1487. u32 val;
  1488. /* Make sure the pipe isn't still relying on us */
  1489. assert_pipe_disabled(dev_priv, pipe);
  1490. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1491. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1492. if (pipe != PIPE_A)
  1493. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1494. I915_WRITE(DPLL(pipe), val);
  1495. POSTING_READ(DPLL(pipe));
  1496. }
  1497. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1498. {
  1499. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1500. u32 val;
  1501. /* Make sure the pipe isn't still relying on us */
  1502. assert_pipe_disabled(dev_priv, pipe);
  1503. val = DPLL_SSC_REF_CLK_CHV |
  1504. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1505. if (pipe != PIPE_A)
  1506. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1507. I915_WRITE(DPLL(pipe), val);
  1508. POSTING_READ(DPLL(pipe));
  1509. mutex_lock(&dev_priv->sb_lock);
  1510. /* Disable 10bit clock to display controller */
  1511. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1512. val &= ~DPIO_DCLKP_EN;
  1513. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1514. mutex_unlock(&dev_priv->sb_lock);
  1515. }
  1516. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1517. struct intel_digital_port *dport,
  1518. unsigned int expected_mask)
  1519. {
  1520. u32 port_mask;
  1521. i915_reg_t dpll_reg;
  1522. switch (dport->port) {
  1523. case PORT_B:
  1524. port_mask = DPLL_PORTB_READY_MASK;
  1525. dpll_reg = DPLL(0);
  1526. break;
  1527. case PORT_C:
  1528. port_mask = DPLL_PORTC_READY_MASK;
  1529. dpll_reg = DPLL(0);
  1530. expected_mask <<= 4;
  1531. break;
  1532. case PORT_D:
  1533. port_mask = DPLL_PORTD_READY_MASK;
  1534. dpll_reg = DPIO_PHY_STATUS;
  1535. break;
  1536. default:
  1537. BUG();
  1538. }
  1539. if (intel_wait_for_register(dev_priv,
  1540. dpll_reg, port_mask, expected_mask,
  1541. 1000))
  1542. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1543. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1544. }
  1545. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1546. enum pipe pipe)
  1547. {
  1548. struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
  1549. pipe);
  1550. i915_reg_t reg;
  1551. uint32_t val, pipeconf_val;
  1552. /* Make sure PCH DPLL is enabled */
  1553. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1554. /* FDI must be feeding us bits for PCH ports */
  1555. assert_fdi_tx_enabled(dev_priv, pipe);
  1556. assert_fdi_rx_enabled(dev_priv, pipe);
  1557. if (HAS_PCH_CPT(dev_priv)) {
  1558. /* Workaround: Set the timing override bit before enabling the
  1559. * pch transcoder. */
  1560. reg = TRANS_CHICKEN2(pipe);
  1561. val = I915_READ(reg);
  1562. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1563. I915_WRITE(reg, val);
  1564. }
  1565. reg = PCH_TRANSCONF(pipe);
  1566. val = I915_READ(reg);
  1567. pipeconf_val = I915_READ(PIPECONF(pipe));
  1568. if (HAS_PCH_IBX(dev_priv)) {
  1569. /*
  1570. * Make the BPC in transcoder be consistent with
  1571. * that in pipeconf reg. For HDMI we must use 8bpc
  1572. * here for both 8bpc and 12bpc.
  1573. */
  1574. val &= ~PIPECONF_BPC_MASK;
  1575. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
  1576. val |= PIPECONF_8BPC;
  1577. else
  1578. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1579. }
  1580. val &= ~TRANS_INTERLACE_MASK;
  1581. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1582. if (HAS_PCH_IBX(dev_priv) &&
  1583. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  1584. val |= TRANS_LEGACY_INTERLACED_ILK;
  1585. else
  1586. val |= TRANS_INTERLACED;
  1587. else
  1588. val |= TRANS_PROGRESSIVE;
  1589. I915_WRITE(reg, val | TRANS_ENABLE);
  1590. if (intel_wait_for_register(dev_priv,
  1591. reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
  1592. 100))
  1593. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1594. }
  1595. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1596. enum transcoder cpu_transcoder)
  1597. {
  1598. u32 val, pipeconf_val;
  1599. /* FDI must be feeding us bits for PCH ports */
  1600. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1601. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1602. /* Workaround: set timing override bit. */
  1603. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1604. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1605. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1606. val = TRANS_ENABLE;
  1607. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1608. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1609. PIPECONF_INTERLACED_ILK)
  1610. val |= TRANS_INTERLACED;
  1611. else
  1612. val |= TRANS_PROGRESSIVE;
  1613. I915_WRITE(LPT_TRANSCONF, val);
  1614. if (intel_wait_for_register(dev_priv,
  1615. LPT_TRANSCONF,
  1616. TRANS_STATE_ENABLE,
  1617. TRANS_STATE_ENABLE,
  1618. 100))
  1619. DRM_ERROR("Failed to enable PCH transcoder\n");
  1620. }
  1621. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1622. enum pipe pipe)
  1623. {
  1624. i915_reg_t reg;
  1625. uint32_t val;
  1626. /* FDI relies on the transcoder */
  1627. assert_fdi_tx_disabled(dev_priv, pipe);
  1628. assert_fdi_rx_disabled(dev_priv, pipe);
  1629. /* Ports must be off as well */
  1630. assert_pch_ports_disabled(dev_priv, pipe);
  1631. reg = PCH_TRANSCONF(pipe);
  1632. val = I915_READ(reg);
  1633. val &= ~TRANS_ENABLE;
  1634. I915_WRITE(reg, val);
  1635. /* wait for PCH transcoder off, transcoder state */
  1636. if (intel_wait_for_register(dev_priv,
  1637. reg, TRANS_STATE_ENABLE, 0,
  1638. 50))
  1639. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1640. if (HAS_PCH_CPT(dev_priv)) {
  1641. /* Workaround: Clear the timing override chicken bit again. */
  1642. reg = TRANS_CHICKEN2(pipe);
  1643. val = I915_READ(reg);
  1644. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1645. I915_WRITE(reg, val);
  1646. }
  1647. }
  1648. void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1649. {
  1650. u32 val;
  1651. val = I915_READ(LPT_TRANSCONF);
  1652. val &= ~TRANS_ENABLE;
  1653. I915_WRITE(LPT_TRANSCONF, val);
  1654. /* wait for PCH transcoder off, transcoder state */
  1655. if (intel_wait_for_register(dev_priv,
  1656. LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
  1657. 50))
  1658. DRM_ERROR("Failed to disable PCH transcoder\n");
  1659. /* Workaround: clear timing override bit. */
  1660. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1661. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1662. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1663. }
  1664. enum transcoder intel_crtc_pch_transcoder(struct intel_crtc *crtc)
  1665. {
  1666. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1667. WARN_ON(!crtc->config->has_pch_encoder);
  1668. if (HAS_PCH_LPT(dev_priv))
  1669. return TRANSCODER_A;
  1670. else
  1671. return (enum transcoder) crtc->pipe;
  1672. }
  1673. /**
  1674. * intel_enable_pipe - enable a pipe, asserting requirements
  1675. * @crtc: crtc responsible for the pipe
  1676. *
  1677. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1678. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1679. */
  1680. static void intel_enable_pipe(struct intel_crtc *crtc)
  1681. {
  1682. struct drm_device *dev = crtc->base.dev;
  1683. struct drm_i915_private *dev_priv = to_i915(dev);
  1684. enum pipe pipe = crtc->pipe;
  1685. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1686. i915_reg_t reg;
  1687. u32 val;
  1688. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1689. assert_planes_disabled(dev_priv, pipe);
  1690. assert_cursor_disabled(dev_priv, pipe);
  1691. assert_sprites_disabled(dev_priv, pipe);
  1692. /*
  1693. * A pipe without a PLL won't actually be able to drive bits from
  1694. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1695. * need the check.
  1696. */
  1697. if (HAS_GMCH_DISPLAY(dev_priv)) {
  1698. if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
  1699. assert_dsi_pll_enabled(dev_priv);
  1700. else
  1701. assert_pll_enabled(dev_priv, pipe);
  1702. } else {
  1703. if (crtc->config->has_pch_encoder) {
  1704. /* if driving the PCH, we need FDI enabled */
  1705. assert_fdi_rx_pll_enabled(dev_priv,
  1706. (enum pipe) intel_crtc_pch_transcoder(crtc));
  1707. assert_fdi_tx_pll_enabled(dev_priv,
  1708. (enum pipe) cpu_transcoder);
  1709. }
  1710. /* FIXME: assert CPU port conditions for SNB+ */
  1711. }
  1712. reg = PIPECONF(cpu_transcoder);
  1713. val = I915_READ(reg);
  1714. if (val & PIPECONF_ENABLE) {
  1715. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1716. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1717. return;
  1718. }
  1719. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1720. POSTING_READ(reg);
  1721. /*
  1722. * Until the pipe starts DSL will read as 0, which would cause
  1723. * an apparent vblank timestamp jump, which messes up also the
  1724. * frame count when it's derived from the timestamps. So let's
  1725. * wait for the pipe to start properly before we call
  1726. * drm_crtc_vblank_on()
  1727. */
  1728. if (dev->max_vblank_count == 0 &&
  1729. wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
  1730. DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
  1731. }
  1732. /**
  1733. * intel_disable_pipe - disable a pipe, asserting requirements
  1734. * @crtc: crtc whose pipes is to be disabled
  1735. *
  1736. * Disable the pipe of @crtc, making sure that various hardware
  1737. * specific requirements are met, if applicable, e.g. plane
  1738. * disabled, panel fitter off, etc.
  1739. *
  1740. * Will wait until the pipe has shut down before returning.
  1741. */
  1742. static void intel_disable_pipe(struct intel_crtc *crtc)
  1743. {
  1744. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1745. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1746. enum pipe pipe = crtc->pipe;
  1747. i915_reg_t reg;
  1748. u32 val;
  1749. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1750. /*
  1751. * Make sure planes won't keep trying to pump pixels to us,
  1752. * or we might hang the display.
  1753. */
  1754. assert_planes_disabled(dev_priv, pipe);
  1755. assert_cursor_disabled(dev_priv, pipe);
  1756. assert_sprites_disabled(dev_priv, pipe);
  1757. reg = PIPECONF(cpu_transcoder);
  1758. val = I915_READ(reg);
  1759. if ((val & PIPECONF_ENABLE) == 0)
  1760. return;
  1761. /*
  1762. * Double wide has implications for planes
  1763. * so best keep it disabled when not needed.
  1764. */
  1765. if (crtc->config->double_wide)
  1766. val &= ~PIPECONF_DOUBLE_WIDE;
  1767. /* Don't disable pipe or pipe PLLs if needed */
  1768. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1769. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1770. val &= ~PIPECONF_ENABLE;
  1771. I915_WRITE(reg, val);
  1772. if ((val & PIPECONF_ENABLE) == 0)
  1773. intel_wait_for_pipe_off(crtc);
  1774. }
  1775. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1776. {
  1777. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1778. }
  1779. static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
  1780. uint64_t fb_modifier, unsigned int cpp)
  1781. {
  1782. switch (fb_modifier) {
  1783. case DRM_FORMAT_MOD_NONE:
  1784. return cpp;
  1785. case I915_FORMAT_MOD_X_TILED:
  1786. if (IS_GEN2(dev_priv))
  1787. return 128;
  1788. else
  1789. return 512;
  1790. case I915_FORMAT_MOD_Y_TILED:
  1791. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1792. return 128;
  1793. else
  1794. return 512;
  1795. case I915_FORMAT_MOD_Yf_TILED:
  1796. switch (cpp) {
  1797. case 1:
  1798. return 64;
  1799. case 2:
  1800. case 4:
  1801. return 128;
  1802. case 8:
  1803. case 16:
  1804. return 256;
  1805. default:
  1806. MISSING_CASE(cpp);
  1807. return cpp;
  1808. }
  1809. break;
  1810. default:
  1811. MISSING_CASE(fb_modifier);
  1812. return cpp;
  1813. }
  1814. }
  1815. unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
  1816. uint64_t fb_modifier, unsigned int cpp)
  1817. {
  1818. if (fb_modifier == DRM_FORMAT_MOD_NONE)
  1819. return 1;
  1820. else
  1821. return intel_tile_size(dev_priv) /
  1822. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1823. }
  1824. /* Return the tile dimensions in pixel units */
  1825. static void intel_tile_dims(const struct drm_i915_private *dev_priv,
  1826. unsigned int *tile_width,
  1827. unsigned int *tile_height,
  1828. uint64_t fb_modifier,
  1829. unsigned int cpp)
  1830. {
  1831. unsigned int tile_width_bytes =
  1832. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1833. *tile_width = tile_width_bytes / cpp;
  1834. *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
  1835. }
  1836. unsigned int
  1837. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1838. uint32_t pixel_format, uint64_t fb_modifier)
  1839. {
  1840. unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
  1841. unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
  1842. return ALIGN(height, tile_height);
  1843. }
  1844. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1845. {
  1846. unsigned int size = 0;
  1847. int i;
  1848. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1849. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1850. return size;
  1851. }
  1852. static void
  1853. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1854. const struct drm_framebuffer *fb,
  1855. unsigned int rotation)
  1856. {
  1857. if (drm_rotation_90_or_270(rotation)) {
  1858. *view = i915_ggtt_view_rotated;
  1859. view->params.rotated = to_intel_framebuffer(fb)->rot_info;
  1860. } else {
  1861. *view = i915_ggtt_view_normal;
  1862. }
  1863. }
  1864. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1865. {
  1866. if (INTEL_INFO(dev_priv)->gen >= 9)
  1867. return 256 * 1024;
  1868. else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
  1869. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1870. return 128 * 1024;
  1871. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1872. return 4 * 1024;
  1873. else
  1874. return 0;
  1875. }
  1876. static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
  1877. uint64_t fb_modifier)
  1878. {
  1879. switch (fb_modifier) {
  1880. case DRM_FORMAT_MOD_NONE:
  1881. return intel_linear_alignment(dev_priv);
  1882. case I915_FORMAT_MOD_X_TILED:
  1883. if (INTEL_INFO(dev_priv)->gen >= 9)
  1884. return 256 * 1024;
  1885. return 0;
  1886. case I915_FORMAT_MOD_Y_TILED:
  1887. case I915_FORMAT_MOD_Yf_TILED:
  1888. return 1 * 1024 * 1024;
  1889. default:
  1890. MISSING_CASE(fb_modifier);
  1891. return 0;
  1892. }
  1893. }
  1894. struct i915_vma *
  1895. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1896. {
  1897. struct drm_device *dev = fb->dev;
  1898. struct drm_i915_private *dev_priv = to_i915(dev);
  1899. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1900. struct i915_ggtt_view view;
  1901. struct i915_vma *vma;
  1902. u32 alignment;
  1903. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1904. alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
  1905. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1906. /* Note that the w/a also requires 64 PTE of padding following the
  1907. * bo. We currently fill all unused PTE with the shadow page and so
  1908. * we should always have valid PTE following the scanout preventing
  1909. * the VT-d warning.
  1910. */
  1911. if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
  1912. alignment = 256 * 1024;
  1913. /*
  1914. * Global gtt pte registers are special registers which actually forward
  1915. * writes to a chunk of system memory. Which means that there is no risk
  1916. * that the register values disappear as soon as we call
  1917. * intel_runtime_pm_put(), so it is correct to wrap only the
  1918. * pin/unpin/fence and not more.
  1919. */
  1920. intel_runtime_pm_get(dev_priv);
  1921. vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
  1922. if (IS_ERR(vma))
  1923. goto err;
  1924. if (i915_vma_is_map_and_fenceable(vma)) {
  1925. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1926. * fence, whereas 965+ only requires a fence if using
  1927. * framebuffer compression. For simplicity, we always, when
  1928. * possible, install a fence as the cost is not that onerous.
  1929. *
  1930. * If we fail to fence the tiled scanout, then either the
  1931. * modeset will reject the change (which is highly unlikely as
  1932. * the affected systems, all but one, do not have unmappable
  1933. * space) or we will not be able to enable full powersaving
  1934. * techniques (also likely not to apply due to various limits
  1935. * FBC and the like impose on the size of the buffer, which
  1936. * presumably we violated anyway with this unmappable buffer).
  1937. * Anyway, it is presumably better to stumble onwards with
  1938. * something and try to run the system in a "less than optimal"
  1939. * mode that matches the user configuration.
  1940. */
  1941. if (i915_vma_get_fence(vma) == 0)
  1942. i915_vma_pin_fence(vma);
  1943. }
  1944. err:
  1945. intel_runtime_pm_put(dev_priv);
  1946. return vma;
  1947. }
  1948. void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1949. {
  1950. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1951. struct i915_ggtt_view view;
  1952. struct i915_vma *vma;
  1953. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  1954. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1955. vma = i915_gem_object_to_ggtt(obj, &view);
  1956. i915_vma_unpin_fence(vma);
  1957. i915_gem_object_unpin_from_display_plane(vma);
  1958. }
  1959. static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
  1960. unsigned int rotation)
  1961. {
  1962. if (drm_rotation_90_or_270(rotation))
  1963. return to_intel_framebuffer(fb)->rotated[plane].pitch;
  1964. else
  1965. return fb->pitches[plane];
  1966. }
  1967. /*
  1968. * Convert the x/y offsets into a linear offset.
  1969. * Only valid with 0/180 degree rotation, which is fine since linear
  1970. * offset is only used with linear buffers on pre-hsw and tiled buffers
  1971. * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
  1972. */
  1973. u32 intel_fb_xy_to_linear(int x, int y,
  1974. const struct intel_plane_state *state,
  1975. int plane)
  1976. {
  1977. const struct drm_framebuffer *fb = state->base.fb;
  1978. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  1979. unsigned int pitch = fb->pitches[plane];
  1980. return y * pitch + x * cpp;
  1981. }
  1982. /*
  1983. * Add the x/y offsets derived from fb->offsets[] to the user
  1984. * specified plane src x/y offsets. The resulting x/y offsets
  1985. * specify the start of scanout from the beginning of the gtt mapping.
  1986. */
  1987. void intel_add_fb_offsets(int *x, int *y,
  1988. const struct intel_plane_state *state,
  1989. int plane)
  1990. {
  1991. const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
  1992. unsigned int rotation = state->base.rotation;
  1993. if (drm_rotation_90_or_270(rotation)) {
  1994. *x += intel_fb->rotated[plane].x;
  1995. *y += intel_fb->rotated[plane].y;
  1996. } else {
  1997. *x += intel_fb->normal[plane].x;
  1998. *y += intel_fb->normal[plane].y;
  1999. }
  2000. }
  2001. /*
  2002. * Input tile dimensions and pitch must already be
  2003. * rotated to match x and y, and in pixel units.
  2004. */
  2005. static u32 _intel_adjust_tile_offset(int *x, int *y,
  2006. unsigned int tile_width,
  2007. unsigned int tile_height,
  2008. unsigned int tile_size,
  2009. unsigned int pitch_tiles,
  2010. u32 old_offset,
  2011. u32 new_offset)
  2012. {
  2013. unsigned int pitch_pixels = pitch_tiles * tile_width;
  2014. unsigned int tiles;
  2015. WARN_ON(old_offset & (tile_size - 1));
  2016. WARN_ON(new_offset & (tile_size - 1));
  2017. WARN_ON(new_offset > old_offset);
  2018. tiles = (old_offset - new_offset) / tile_size;
  2019. *y += tiles / pitch_tiles * tile_height;
  2020. *x += tiles % pitch_tiles * tile_width;
  2021. /* minimize x in case it got needlessly big */
  2022. *y += *x / pitch_pixels * tile_height;
  2023. *x %= pitch_pixels;
  2024. return new_offset;
  2025. }
  2026. /*
  2027. * Adjust the tile offset by moving the difference into
  2028. * the x/y offsets.
  2029. */
  2030. static u32 intel_adjust_tile_offset(int *x, int *y,
  2031. const struct intel_plane_state *state, int plane,
  2032. u32 old_offset, u32 new_offset)
  2033. {
  2034. const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
  2035. const struct drm_framebuffer *fb = state->base.fb;
  2036. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2037. unsigned int rotation = state->base.rotation;
  2038. unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
  2039. WARN_ON(new_offset > old_offset);
  2040. if (fb->modifier[plane] != DRM_FORMAT_MOD_NONE) {
  2041. unsigned int tile_size, tile_width, tile_height;
  2042. unsigned int pitch_tiles;
  2043. tile_size = intel_tile_size(dev_priv);
  2044. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2045. fb->modifier[plane], cpp);
  2046. if (drm_rotation_90_or_270(rotation)) {
  2047. pitch_tiles = pitch / tile_height;
  2048. swap(tile_width, tile_height);
  2049. } else {
  2050. pitch_tiles = pitch / (tile_width * cpp);
  2051. }
  2052. _intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2053. tile_size, pitch_tiles,
  2054. old_offset, new_offset);
  2055. } else {
  2056. old_offset += *y * pitch + *x * cpp;
  2057. *y = (old_offset - new_offset) / pitch;
  2058. *x = ((old_offset - new_offset) - *y * pitch) / cpp;
  2059. }
  2060. return new_offset;
  2061. }
  2062. /*
  2063. * Computes the linear offset to the base tile and adjusts
  2064. * x, y. bytes per pixel is assumed to be a power-of-two.
  2065. *
  2066. * In the 90/270 rotated case, x and y are assumed
  2067. * to be already rotated to match the rotated GTT view, and
  2068. * pitch is the tile_height aligned framebuffer height.
  2069. *
  2070. * This function is used when computing the derived information
  2071. * under intel_framebuffer, so using any of that information
  2072. * here is not allowed. Anything under drm_framebuffer can be
  2073. * used. This is why the user has to pass in the pitch since it
  2074. * is specified in the rotated orientation.
  2075. */
  2076. static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
  2077. int *x, int *y,
  2078. const struct drm_framebuffer *fb, int plane,
  2079. unsigned int pitch,
  2080. unsigned int rotation,
  2081. u32 alignment)
  2082. {
  2083. uint64_t fb_modifier = fb->modifier[plane];
  2084. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2085. u32 offset, offset_aligned;
  2086. if (alignment)
  2087. alignment--;
  2088. if (fb_modifier != DRM_FORMAT_MOD_NONE) {
  2089. unsigned int tile_size, tile_width, tile_height;
  2090. unsigned int tile_rows, tiles, pitch_tiles;
  2091. tile_size = intel_tile_size(dev_priv);
  2092. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2093. fb_modifier, cpp);
  2094. if (drm_rotation_90_or_270(rotation)) {
  2095. pitch_tiles = pitch / tile_height;
  2096. swap(tile_width, tile_height);
  2097. } else {
  2098. pitch_tiles = pitch / (tile_width * cpp);
  2099. }
  2100. tile_rows = *y / tile_height;
  2101. *y %= tile_height;
  2102. tiles = *x / tile_width;
  2103. *x %= tile_width;
  2104. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  2105. offset_aligned = offset & ~alignment;
  2106. _intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2107. tile_size, pitch_tiles,
  2108. offset, offset_aligned);
  2109. } else {
  2110. offset = *y * pitch + *x * cpp;
  2111. offset_aligned = offset & ~alignment;
  2112. *y = (offset & alignment) / pitch;
  2113. *x = ((offset & alignment) - *y * pitch) / cpp;
  2114. }
  2115. return offset_aligned;
  2116. }
  2117. u32 intel_compute_tile_offset(int *x, int *y,
  2118. const struct intel_plane_state *state,
  2119. int plane)
  2120. {
  2121. const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
  2122. const struct drm_framebuffer *fb = state->base.fb;
  2123. unsigned int rotation = state->base.rotation;
  2124. int pitch = intel_fb_pitch(fb, plane, rotation);
  2125. u32 alignment;
  2126. /* AUX_DIST needs only 4K alignment */
  2127. if (fb->pixel_format == DRM_FORMAT_NV12 && plane == 1)
  2128. alignment = 4096;
  2129. else
  2130. alignment = intel_surf_alignment(dev_priv, fb->modifier[plane]);
  2131. return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
  2132. rotation, alignment);
  2133. }
  2134. /* Convert the fb->offset[] linear offset into x/y offsets */
  2135. static void intel_fb_offset_to_xy(int *x, int *y,
  2136. const struct drm_framebuffer *fb, int plane)
  2137. {
  2138. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2139. unsigned int pitch = fb->pitches[plane];
  2140. u32 linear_offset = fb->offsets[plane];
  2141. *y = linear_offset / pitch;
  2142. *x = linear_offset % pitch / cpp;
  2143. }
  2144. static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
  2145. {
  2146. switch (fb_modifier) {
  2147. case I915_FORMAT_MOD_X_TILED:
  2148. return I915_TILING_X;
  2149. case I915_FORMAT_MOD_Y_TILED:
  2150. return I915_TILING_Y;
  2151. default:
  2152. return I915_TILING_NONE;
  2153. }
  2154. }
  2155. static int
  2156. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  2157. struct drm_framebuffer *fb)
  2158. {
  2159. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  2160. struct intel_rotation_info *rot_info = &intel_fb->rot_info;
  2161. u32 gtt_offset_rotated = 0;
  2162. unsigned int max_size = 0;
  2163. uint32_t format = fb->pixel_format;
  2164. int i, num_planes = drm_format_num_planes(format);
  2165. unsigned int tile_size = intel_tile_size(dev_priv);
  2166. for (i = 0; i < num_planes; i++) {
  2167. unsigned int width, height;
  2168. unsigned int cpp, size;
  2169. u32 offset;
  2170. int x, y;
  2171. cpp = drm_format_plane_cpp(format, i);
  2172. width = drm_format_plane_width(fb->width, format, i);
  2173. height = drm_format_plane_height(fb->height, format, i);
  2174. intel_fb_offset_to_xy(&x, &y, fb, i);
  2175. /*
  2176. * The fence (if used) is aligned to the start of the object
  2177. * so having the framebuffer wrap around across the edge of the
  2178. * fenced region doesn't really work. We have no API to configure
  2179. * the fence start offset within the object (nor could we probably
  2180. * on gen2/3). So it's just easier if we just require that the
  2181. * fb layout agrees with the fence layout. We already check that the
  2182. * fb stride matches the fence stride elsewhere.
  2183. */
  2184. if (i915_gem_object_is_tiled(intel_fb->obj) &&
  2185. (x + width) * cpp > fb->pitches[i]) {
  2186. DRM_DEBUG("bad fb plane %d offset: 0x%x\n",
  2187. i, fb->offsets[i]);
  2188. return -EINVAL;
  2189. }
  2190. /*
  2191. * First pixel of the framebuffer from
  2192. * the start of the normal gtt mapping.
  2193. */
  2194. intel_fb->normal[i].x = x;
  2195. intel_fb->normal[i].y = y;
  2196. offset = _intel_compute_tile_offset(dev_priv, &x, &y,
  2197. fb, 0, fb->pitches[i],
  2198. DRM_ROTATE_0, tile_size);
  2199. offset /= tile_size;
  2200. if (fb->modifier[i] != DRM_FORMAT_MOD_NONE) {
  2201. unsigned int tile_width, tile_height;
  2202. unsigned int pitch_tiles;
  2203. struct drm_rect r;
  2204. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2205. fb->modifier[i], cpp);
  2206. rot_info->plane[i].offset = offset;
  2207. rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
  2208. rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
  2209. rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
  2210. intel_fb->rotated[i].pitch =
  2211. rot_info->plane[i].height * tile_height;
  2212. /* how many tiles does this plane need */
  2213. size = rot_info->plane[i].stride * rot_info->plane[i].height;
  2214. /*
  2215. * If the plane isn't horizontally tile aligned,
  2216. * we need one more tile.
  2217. */
  2218. if (x != 0)
  2219. size++;
  2220. /* rotate the x/y offsets to match the GTT view */
  2221. r.x1 = x;
  2222. r.y1 = y;
  2223. r.x2 = x + width;
  2224. r.y2 = y + height;
  2225. drm_rect_rotate(&r,
  2226. rot_info->plane[i].width * tile_width,
  2227. rot_info->plane[i].height * tile_height,
  2228. DRM_ROTATE_270);
  2229. x = r.x1;
  2230. y = r.y1;
  2231. /* rotate the tile dimensions to match the GTT view */
  2232. pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
  2233. swap(tile_width, tile_height);
  2234. /*
  2235. * We only keep the x/y offsets, so push all of the
  2236. * gtt offset into the x/y offsets.
  2237. */
  2238. _intel_adjust_tile_offset(&x, &y, tile_size,
  2239. tile_width, tile_height, pitch_tiles,
  2240. gtt_offset_rotated * tile_size, 0);
  2241. gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
  2242. /*
  2243. * First pixel of the framebuffer from
  2244. * the start of the rotated gtt mapping.
  2245. */
  2246. intel_fb->rotated[i].x = x;
  2247. intel_fb->rotated[i].y = y;
  2248. } else {
  2249. size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
  2250. x * cpp, tile_size);
  2251. }
  2252. /* how many tiles in total needed in the bo */
  2253. max_size = max(max_size, offset + size);
  2254. }
  2255. if (max_size * tile_size > to_intel_framebuffer(fb)->obj->base.size) {
  2256. DRM_DEBUG("fb too big for bo (need %u bytes, have %zu bytes)\n",
  2257. max_size * tile_size, to_intel_framebuffer(fb)->obj->base.size);
  2258. return -EINVAL;
  2259. }
  2260. return 0;
  2261. }
  2262. static int i9xx_format_to_fourcc(int format)
  2263. {
  2264. switch (format) {
  2265. case DISPPLANE_8BPP:
  2266. return DRM_FORMAT_C8;
  2267. case DISPPLANE_BGRX555:
  2268. return DRM_FORMAT_XRGB1555;
  2269. case DISPPLANE_BGRX565:
  2270. return DRM_FORMAT_RGB565;
  2271. default:
  2272. case DISPPLANE_BGRX888:
  2273. return DRM_FORMAT_XRGB8888;
  2274. case DISPPLANE_RGBX888:
  2275. return DRM_FORMAT_XBGR8888;
  2276. case DISPPLANE_BGRX101010:
  2277. return DRM_FORMAT_XRGB2101010;
  2278. case DISPPLANE_RGBX101010:
  2279. return DRM_FORMAT_XBGR2101010;
  2280. }
  2281. }
  2282. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2283. {
  2284. switch (format) {
  2285. case PLANE_CTL_FORMAT_RGB_565:
  2286. return DRM_FORMAT_RGB565;
  2287. default:
  2288. case PLANE_CTL_FORMAT_XRGB_8888:
  2289. if (rgb_order) {
  2290. if (alpha)
  2291. return DRM_FORMAT_ABGR8888;
  2292. else
  2293. return DRM_FORMAT_XBGR8888;
  2294. } else {
  2295. if (alpha)
  2296. return DRM_FORMAT_ARGB8888;
  2297. else
  2298. return DRM_FORMAT_XRGB8888;
  2299. }
  2300. case PLANE_CTL_FORMAT_XRGB_2101010:
  2301. if (rgb_order)
  2302. return DRM_FORMAT_XBGR2101010;
  2303. else
  2304. return DRM_FORMAT_XRGB2101010;
  2305. }
  2306. }
  2307. static bool
  2308. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2309. struct intel_initial_plane_config *plane_config)
  2310. {
  2311. struct drm_device *dev = crtc->base.dev;
  2312. struct drm_i915_private *dev_priv = to_i915(dev);
  2313. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2314. struct drm_i915_gem_object *obj = NULL;
  2315. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2316. struct drm_framebuffer *fb = &plane_config->fb->base;
  2317. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2318. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2319. PAGE_SIZE);
  2320. size_aligned -= base_aligned;
  2321. if (plane_config->size == 0)
  2322. return false;
  2323. /* If the FB is too big, just don't use it since fbdev is not very
  2324. * important and we should probably use that space with FBC or other
  2325. * features. */
  2326. if (size_aligned * 2 > ggtt->stolen_usable_size)
  2327. return false;
  2328. mutex_lock(&dev->struct_mutex);
  2329. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2330. base_aligned,
  2331. base_aligned,
  2332. size_aligned);
  2333. if (!obj) {
  2334. mutex_unlock(&dev->struct_mutex);
  2335. return false;
  2336. }
  2337. if (plane_config->tiling == I915_TILING_X)
  2338. obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
  2339. mode_cmd.pixel_format = fb->pixel_format;
  2340. mode_cmd.width = fb->width;
  2341. mode_cmd.height = fb->height;
  2342. mode_cmd.pitches[0] = fb->pitches[0];
  2343. mode_cmd.modifier[0] = fb->modifier[0];
  2344. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2345. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2346. &mode_cmd, obj)) {
  2347. DRM_DEBUG_KMS("intel fb init failed\n");
  2348. goto out_unref_obj;
  2349. }
  2350. mutex_unlock(&dev->struct_mutex);
  2351. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2352. return true;
  2353. out_unref_obj:
  2354. i915_gem_object_put(obj);
  2355. mutex_unlock(&dev->struct_mutex);
  2356. return false;
  2357. }
  2358. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2359. static void
  2360. update_state_fb(struct drm_plane *plane)
  2361. {
  2362. if (plane->fb == plane->state->fb)
  2363. return;
  2364. if (plane->state->fb)
  2365. drm_framebuffer_unreference(plane->state->fb);
  2366. plane->state->fb = plane->fb;
  2367. if (plane->state->fb)
  2368. drm_framebuffer_reference(plane->state->fb);
  2369. }
  2370. static void
  2371. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2372. struct intel_initial_plane_config *plane_config)
  2373. {
  2374. struct drm_device *dev = intel_crtc->base.dev;
  2375. struct drm_i915_private *dev_priv = to_i915(dev);
  2376. struct drm_crtc *c;
  2377. struct intel_crtc *i;
  2378. struct drm_i915_gem_object *obj;
  2379. struct drm_plane *primary = intel_crtc->base.primary;
  2380. struct drm_plane_state *plane_state = primary->state;
  2381. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2382. struct intel_plane *intel_plane = to_intel_plane(primary);
  2383. struct intel_plane_state *intel_state =
  2384. to_intel_plane_state(plane_state);
  2385. struct drm_framebuffer *fb;
  2386. if (!plane_config->fb)
  2387. return;
  2388. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2389. fb = &plane_config->fb->base;
  2390. goto valid_fb;
  2391. }
  2392. kfree(plane_config->fb);
  2393. /*
  2394. * Failed to alloc the obj, check to see if we should share
  2395. * an fb with another CRTC instead
  2396. */
  2397. for_each_crtc(dev, c) {
  2398. i = to_intel_crtc(c);
  2399. if (c == &intel_crtc->base)
  2400. continue;
  2401. if (!i->active)
  2402. continue;
  2403. fb = c->primary->fb;
  2404. if (!fb)
  2405. continue;
  2406. obj = intel_fb_obj(fb);
  2407. if (i915_gem_object_ggtt_offset(obj, NULL) == plane_config->base) {
  2408. drm_framebuffer_reference(fb);
  2409. goto valid_fb;
  2410. }
  2411. }
  2412. /*
  2413. * We've failed to reconstruct the BIOS FB. Current display state
  2414. * indicates that the primary plane is visible, but has a NULL FB,
  2415. * which will lead to problems later if we don't fix it up. The
  2416. * simplest solution is to just disable the primary plane now and
  2417. * pretend the BIOS never had it enabled.
  2418. */
  2419. to_intel_plane_state(plane_state)->base.visible = false;
  2420. crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
  2421. intel_pre_disable_primary_noatomic(&intel_crtc->base);
  2422. intel_plane->disable_plane(primary, &intel_crtc->base);
  2423. return;
  2424. valid_fb:
  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.x1 = plane_state->src_x;
  2434. intel_state->base.src.y1 = plane_state->src_y;
  2435. intel_state->base.src.x2 = plane_state->src_x + plane_state->src_w;
  2436. intel_state->base.src.y2 = plane_state->src_y + plane_state->src_h;
  2437. intel_state->base.dst.x1 = plane_state->crtc_x;
  2438. intel_state->base.dst.y1 = plane_state->crtc_y;
  2439. intel_state->base.dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
  2440. intel_state->base.dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
  2441. obj = intel_fb_obj(fb);
  2442. if (i915_gem_object_is_tiled(obj))
  2443. dev_priv->preserve_bios_swizzle = true;
  2444. drm_framebuffer_reference(fb);
  2445. primary->fb = primary->state->fb = fb;
  2446. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2447. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2448. atomic_or(to_intel_plane(primary)->frontbuffer_bit,
  2449. &obj->frontbuffer_bits);
  2450. }
  2451. static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
  2452. unsigned int rotation)
  2453. {
  2454. int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2455. switch (fb->modifier[plane]) {
  2456. case DRM_FORMAT_MOD_NONE:
  2457. case I915_FORMAT_MOD_X_TILED:
  2458. switch (cpp) {
  2459. case 8:
  2460. return 4096;
  2461. case 4:
  2462. case 2:
  2463. case 1:
  2464. return 8192;
  2465. default:
  2466. MISSING_CASE(cpp);
  2467. break;
  2468. }
  2469. break;
  2470. case I915_FORMAT_MOD_Y_TILED:
  2471. case I915_FORMAT_MOD_Yf_TILED:
  2472. switch (cpp) {
  2473. case 8:
  2474. return 2048;
  2475. case 4:
  2476. return 4096;
  2477. case 2:
  2478. case 1:
  2479. return 8192;
  2480. default:
  2481. MISSING_CASE(cpp);
  2482. break;
  2483. }
  2484. break;
  2485. default:
  2486. MISSING_CASE(fb->modifier[plane]);
  2487. }
  2488. return 2048;
  2489. }
  2490. static int skl_check_main_surface(struct intel_plane_state *plane_state)
  2491. {
  2492. const struct drm_i915_private *dev_priv = to_i915(plane_state->base.plane->dev);
  2493. const struct drm_framebuffer *fb = plane_state->base.fb;
  2494. unsigned int rotation = plane_state->base.rotation;
  2495. int x = plane_state->base.src.x1 >> 16;
  2496. int y = plane_state->base.src.y1 >> 16;
  2497. int w = drm_rect_width(&plane_state->base.src) >> 16;
  2498. int h = drm_rect_height(&plane_state->base.src) >> 16;
  2499. int max_width = skl_max_plane_width(fb, 0, rotation);
  2500. int max_height = 4096;
  2501. u32 alignment, offset, aux_offset = plane_state->aux.offset;
  2502. if (w > max_width || h > max_height) {
  2503. DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
  2504. w, h, max_width, max_height);
  2505. return -EINVAL;
  2506. }
  2507. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2508. offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
  2509. alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
  2510. /*
  2511. * AUX surface offset is specified as the distance from the
  2512. * main surface offset, and it must be non-negative. Make
  2513. * sure that is what we will get.
  2514. */
  2515. if (offset > aux_offset)
  2516. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2517. offset, aux_offset & ~(alignment - 1));
  2518. /*
  2519. * When using an X-tiled surface, the plane blows up
  2520. * if the x offset + width exceed the stride.
  2521. *
  2522. * TODO: linear and Y-tiled seem fine, Yf untested,
  2523. */
  2524. if (fb->modifier[0] == I915_FORMAT_MOD_X_TILED) {
  2525. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2526. while ((x + w) * cpp > fb->pitches[0]) {
  2527. if (offset == 0) {
  2528. DRM_DEBUG_KMS("Unable to find suitable display surface offset\n");
  2529. return -EINVAL;
  2530. }
  2531. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2532. offset, offset - alignment);
  2533. }
  2534. }
  2535. plane_state->main.offset = offset;
  2536. plane_state->main.x = x;
  2537. plane_state->main.y = y;
  2538. return 0;
  2539. }
  2540. static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
  2541. {
  2542. const struct drm_framebuffer *fb = plane_state->base.fb;
  2543. unsigned int rotation = plane_state->base.rotation;
  2544. int max_width = skl_max_plane_width(fb, 1, rotation);
  2545. int max_height = 4096;
  2546. int x = plane_state->base.src.x1 >> 17;
  2547. int y = plane_state->base.src.y1 >> 17;
  2548. int w = drm_rect_width(&plane_state->base.src) >> 17;
  2549. int h = drm_rect_height(&plane_state->base.src) >> 17;
  2550. u32 offset;
  2551. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2552. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2553. /* FIXME not quite sure how/if these apply to the chroma plane */
  2554. if (w > max_width || h > max_height) {
  2555. DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
  2556. w, h, max_width, max_height);
  2557. return -EINVAL;
  2558. }
  2559. plane_state->aux.offset = offset;
  2560. plane_state->aux.x = x;
  2561. plane_state->aux.y = y;
  2562. return 0;
  2563. }
  2564. int skl_check_plane_surface(struct intel_plane_state *plane_state)
  2565. {
  2566. const struct drm_framebuffer *fb = plane_state->base.fb;
  2567. unsigned int rotation = plane_state->base.rotation;
  2568. int ret;
  2569. /* Rotate src coordinates to match rotated GTT view */
  2570. if (drm_rotation_90_or_270(rotation))
  2571. drm_rect_rotate(&plane_state->base.src,
  2572. fb->width << 16, fb->height << 16,
  2573. DRM_ROTATE_270);
  2574. /*
  2575. * Handle the AUX surface first since
  2576. * the main surface setup depends on it.
  2577. */
  2578. if (fb->pixel_format == DRM_FORMAT_NV12) {
  2579. ret = skl_check_nv12_aux_surface(plane_state);
  2580. if (ret)
  2581. return ret;
  2582. } else {
  2583. plane_state->aux.offset = ~0xfff;
  2584. plane_state->aux.x = 0;
  2585. plane_state->aux.y = 0;
  2586. }
  2587. ret = skl_check_main_surface(plane_state);
  2588. if (ret)
  2589. return ret;
  2590. return 0;
  2591. }
  2592. static void i9xx_update_primary_plane(struct drm_plane *primary,
  2593. const struct intel_crtc_state *crtc_state,
  2594. const struct intel_plane_state *plane_state)
  2595. {
  2596. struct drm_device *dev = primary->dev;
  2597. struct drm_i915_private *dev_priv = to_i915(dev);
  2598. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2599. struct drm_framebuffer *fb = plane_state->base.fb;
  2600. int plane = intel_crtc->plane;
  2601. u32 linear_offset;
  2602. u32 dspcntr;
  2603. i915_reg_t reg = DSPCNTR(plane);
  2604. unsigned int rotation = plane_state->base.rotation;
  2605. int x = plane_state->base.src.x1 >> 16;
  2606. int y = plane_state->base.src.y1 >> 16;
  2607. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2608. dspcntr |= DISPLAY_PLANE_ENABLE;
  2609. if (INTEL_INFO(dev)->gen < 4) {
  2610. if (intel_crtc->pipe == PIPE_B)
  2611. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2612. /* pipesrc and dspsize control the size that is scaled from,
  2613. * which should always be the user's requested size.
  2614. */
  2615. I915_WRITE(DSPSIZE(plane),
  2616. ((crtc_state->pipe_src_h - 1) << 16) |
  2617. (crtc_state->pipe_src_w - 1));
  2618. I915_WRITE(DSPPOS(plane), 0);
  2619. } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
  2620. I915_WRITE(PRIMSIZE(plane),
  2621. ((crtc_state->pipe_src_h - 1) << 16) |
  2622. (crtc_state->pipe_src_w - 1));
  2623. I915_WRITE(PRIMPOS(plane), 0);
  2624. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2625. }
  2626. switch (fb->pixel_format) {
  2627. case DRM_FORMAT_C8:
  2628. dspcntr |= DISPPLANE_8BPP;
  2629. break;
  2630. case DRM_FORMAT_XRGB1555:
  2631. dspcntr |= DISPPLANE_BGRX555;
  2632. break;
  2633. case DRM_FORMAT_RGB565:
  2634. dspcntr |= DISPPLANE_BGRX565;
  2635. break;
  2636. case DRM_FORMAT_XRGB8888:
  2637. dspcntr |= DISPPLANE_BGRX888;
  2638. break;
  2639. case DRM_FORMAT_XBGR8888:
  2640. dspcntr |= DISPPLANE_RGBX888;
  2641. break;
  2642. case DRM_FORMAT_XRGB2101010:
  2643. dspcntr |= DISPPLANE_BGRX101010;
  2644. break;
  2645. case DRM_FORMAT_XBGR2101010:
  2646. dspcntr |= DISPPLANE_RGBX101010;
  2647. break;
  2648. default:
  2649. BUG();
  2650. }
  2651. if (INTEL_GEN(dev_priv) >= 4 &&
  2652. fb->modifier[0] == I915_FORMAT_MOD_X_TILED)
  2653. dspcntr |= DISPPLANE_TILED;
  2654. if (rotation & DRM_ROTATE_180)
  2655. dspcntr |= DISPPLANE_ROTATE_180;
  2656. if (rotation & DRM_REFLECT_X)
  2657. dspcntr |= DISPPLANE_MIRROR;
  2658. if (IS_G4X(dev_priv))
  2659. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2660. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2661. if (INTEL_INFO(dev)->gen >= 4)
  2662. intel_crtc->dspaddr_offset =
  2663. intel_compute_tile_offset(&x, &y, plane_state, 0);
  2664. if (rotation & DRM_ROTATE_180) {
  2665. x += crtc_state->pipe_src_w - 1;
  2666. y += crtc_state->pipe_src_h - 1;
  2667. } else if (rotation & DRM_REFLECT_X) {
  2668. x += crtc_state->pipe_src_w - 1;
  2669. }
  2670. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2671. if (INTEL_INFO(dev)->gen < 4)
  2672. intel_crtc->dspaddr_offset = linear_offset;
  2673. intel_crtc->adjusted_x = x;
  2674. intel_crtc->adjusted_y = y;
  2675. I915_WRITE(reg, dspcntr);
  2676. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2677. if (INTEL_INFO(dev)->gen >= 4) {
  2678. I915_WRITE(DSPSURF(plane),
  2679. intel_fb_gtt_offset(fb, rotation) +
  2680. intel_crtc->dspaddr_offset);
  2681. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2682. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2683. } else {
  2684. I915_WRITE(DSPADDR(plane),
  2685. intel_fb_gtt_offset(fb, rotation) +
  2686. intel_crtc->dspaddr_offset);
  2687. }
  2688. POSTING_READ(reg);
  2689. }
  2690. static void i9xx_disable_primary_plane(struct drm_plane *primary,
  2691. struct drm_crtc *crtc)
  2692. {
  2693. struct drm_device *dev = crtc->dev;
  2694. struct drm_i915_private *dev_priv = to_i915(dev);
  2695. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2696. int plane = intel_crtc->plane;
  2697. I915_WRITE(DSPCNTR(plane), 0);
  2698. if (INTEL_INFO(dev_priv)->gen >= 4)
  2699. I915_WRITE(DSPSURF(plane), 0);
  2700. else
  2701. I915_WRITE(DSPADDR(plane), 0);
  2702. POSTING_READ(DSPCNTR(plane));
  2703. }
  2704. static void ironlake_update_primary_plane(struct drm_plane *primary,
  2705. const struct intel_crtc_state *crtc_state,
  2706. const struct intel_plane_state *plane_state)
  2707. {
  2708. struct drm_device *dev = primary->dev;
  2709. struct drm_i915_private *dev_priv = to_i915(dev);
  2710. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2711. struct drm_framebuffer *fb = plane_state->base.fb;
  2712. int plane = intel_crtc->plane;
  2713. u32 linear_offset;
  2714. u32 dspcntr;
  2715. i915_reg_t reg = DSPCNTR(plane);
  2716. unsigned int rotation = plane_state->base.rotation;
  2717. int x = plane_state->base.src.x1 >> 16;
  2718. int y = plane_state->base.src.y1 >> 16;
  2719. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2720. dspcntr |= DISPLAY_PLANE_ENABLE;
  2721. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  2722. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2723. switch (fb->pixel_format) {
  2724. case DRM_FORMAT_C8:
  2725. dspcntr |= DISPPLANE_8BPP;
  2726. break;
  2727. case DRM_FORMAT_RGB565:
  2728. dspcntr |= DISPPLANE_BGRX565;
  2729. break;
  2730. case DRM_FORMAT_XRGB8888:
  2731. dspcntr |= DISPPLANE_BGRX888;
  2732. break;
  2733. case DRM_FORMAT_XBGR8888:
  2734. dspcntr |= DISPPLANE_RGBX888;
  2735. break;
  2736. case DRM_FORMAT_XRGB2101010:
  2737. dspcntr |= DISPPLANE_BGRX101010;
  2738. break;
  2739. case DRM_FORMAT_XBGR2101010:
  2740. dspcntr |= DISPPLANE_RGBX101010;
  2741. break;
  2742. default:
  2743. BUG();
  2744. }
  2745. if (fb->modifier[0] == I915_FORMAT_MOD_X_TILED)
  2746. dspcntr |= DISPPLANE_TILED;
  2747. if (rotation & DRM_ROTATE_180)
  2748. dspcntr |= DISPPLANE_ROTATE_180;
  2749. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv))
  2750. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2751. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2752. intel_crtc->dspaddr_offset =
  2753. intel_compute_tile_offset(&x, &y, plane_state, 0);
  2754. /* HSW+ does this automagically in hardware */
  2755. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv) &&
  2756. rotation & DRM_ROTATE_180) {
  2757. x += crtc_state->pipe_src_w - 1;
  2758. y += crtc_state->pipe_src_h - 1;
  2759. }
  2760. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2761. intel_crtc->adjusted_x = x;
  2762. intel_crtc->adjusted_y = y;
  2763. I915_WRITE(reg, dspcntr);
  2764. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2765. I915_WRITE(DSPSURF(plane),
  2766. intel_fb_gtt_offset(fb, rotation) +
  2767. intel_crtc->dspaddr_offset);
  2768. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  2769. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2770. } else {
  2771. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2772. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2773. }
  2774. POSTING_READ(reg);
  2775. }
  2776. u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
  2777. uint64_t fb_modifier, uint32_t pixel_format)
  2778. {
  2779. if (fb_modifier == DRM_FORMAT_MOD_NONE) {
  2780. return 64;
  2781. } else {
  2782. int cpp = drm_format_plane_cpp(pixel_format, 0);
  2783. return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  2784. }
  2785. }
  2786. u32 intel_fb_gtt_offset(struct drm_framebuffer *fb,
  2787. unsigned int rotation)
  2788. {
  2789. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2790. struct i915_ggtt_view view;
  2791. struct i915_vma *vma;
  2792. intel_fill_fb_ggtt_view(&view, fb, rotation);
  2793. vma = i915_gem_object_to_ggtt(obj, &view);
  2794. if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
  2795. view.type))
  2796. return -1;
  2797. return i915_ggtt_offset(vma);
  2798. }
  2799. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2800. {
  2801. struct drm_device *dev = intel_crtc->base.dev;
  2802. struct drm_i915_private *dev_priv = to_i915(dev);
  2803. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2804. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2805. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2806. }
  2807. /*
  2808. * This function detaches (aka. unbinds) unused scalers in hardware
  2809. */
  2810. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2811. {
  2812. struct intel_crtc_scaler_state *scaler_state;
  2813. int i;
  2814. scaler_state = &intel_crtc->config->scaler_state;
  2815. /* loop through and disable scalers that aren't in use */
  2816. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2817. if (!scaler_state->scalers[i].in_use)
  2818. skl_detach_scaler(intel_crtc, i);
  2819. }
  2820. }
  2821. u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
  2822. unsigned int rotation)
  2823. {
  2824. const struct drm_i915_private *dev_priv = to_i915(fb->dev);
  2825. u32 stride = intel_fb_pitch(fb, plane, rotation);
  2826. /*
  2827. * The stride is either expressed as a multiple of 64 bytes chunks for
  2828. * linear buffers or in number of tiles for tiled buffers.
  2829. */
  2830. if (drm_rotation_90_or_270(rotation)) {
  2831. int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2832. stride /= intel_tile_height(dev_priv, fb->modifier[0], cpp);
  2833. } else {
  2834. stride /= intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  2835. fb->pixel_format);
  2836. }
  2837. return stride;
  2838. }
  2839. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2840. {
  2841. switch (pixel_format) {
  2842. case DRM_FORMAT_C8:
  2843. return PLANE_CTL_FORMAT_INDEXED;
  2844. case DRM_FORMAT_RGB565:
  2845. return PLANE_CTL_FORMAT_RGB_565;
  2846. case DRM_FORMAT_XBGR8888:
  2847. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2848. case DRM_FORMAT_XRGB8888:
  2849. return PLANE_CTL_FORMAT_XRGB_8888;
  2850. /*
  2851. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2852. * to be already pre-multiplied. We need to add a knob (or a different
  2853. * DRM_FORMAT) for user-space to configure that.
  2854. */
  2855. case DRM_FORMAT_ABGR8888:
  2856. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2857. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2858. case DRM_FORMAT_ARGB8888:
  2859. return PLANE_CTL_FORMAT_XRGB_8888 |
  2860. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2861. case DRM_FORMAT_XRGB2101010:
  2862. return PLANE_CTL_FORMAT_XRGB_2101010;
  2863. case DRM_FORMAT_XBGR2101010:
  2864. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2865. case DRM_FORMAT_YUYV:
  2866. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2867. case DRM_FORMAT_YVYU:
  2868. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2869. case DRM_FORMAT_UYVY:
  2870. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2871. case DRM_FORMAT_VYUY:
  2872. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2873. default:
  2874. MISSING_CASE(pixel_format);
  2875. }
  2876. return 0;
  2877. }
  2878. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2879. {
  2880. switch (fb_modifier) {
  2881. case DRM_FORMAT_MOD_NONE:
  2882. break;
  2883. case I915_FORMAT_MOD_X_TILED:
  2884. return PLANE_CTL_TILED_X;
  2885. case I915_FORMAT_MOD_Y_TILED:
  2886. return PLANE_CTL_TILED_Y;
  2887. case I915_FORMAT_MOD_Yf_TILED:
  2888. return PLANE_CTL_TILED_YF;
  2889. default:
  2890. MISSING_CASE(fb_modifier);
  2891. }
  2892. return 0;
  2893. }
  2894. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2895. {
  2896. switch (rotation) {
  2897. case DRM_ROTATE_0:
  2898. break;
  2899. /*
  2900. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2901. * while i915 HW rotation is clockwise, thats why this swapping.
  2902. */
  2903. case DRM_ROTATE_90:
  2904. return PLANE_CTL_ROTATE_270;
  2905. case DRM_ROTATE_180:
  2906. return PLANE_CTL_ROTATE_180;
  2907. case DRM_ROTATE_270:
  2908. return PLANE_CTL_ROTATE_90;
  2909. default:
  2910. MISSING_CASE(rotation);
  2911. }
  2912. return 0;
  2913. }
  2914. static void skylake_update_primary_plane(struct drm_plane *plane,
  2915. const struct intel_crtc_state *crtc_state,
  2916. const struct intel_plane_state *plane_state)
  2917. {
  2918. struct drm_device *dev = plane->dev;
  2919. struct drm_i915_private *dev_priv = to_i915(dev);
  2920. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2921. struct drm_framebuffer *fb = plane_state->base.fb;
  2922. const struct skl_wm_values *wm = &dev_priv->wm.skl_results;
  2923. const struct skl_plane_wm *p_wm =
  2924. &crtc_state->wm.skl.optimal.planes[0];
  2925. int pipe = intel_crtc->pipe;
  2926. u32 plane_ctl;
  2927. unsigned int rotation = plane_state->base.rotation;
  2928. u32 stride = skl_plane_stride(fb, 0, rotation);
  2929. u32 surf_addr = plane_state->main.offset;
  2930. int scaler_id = plane_state->scaler_id;
  2931. int src_x = plane_state->main.x;
  2932. int src_y = plane_state->main.y;
  2933. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  2934. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  2935. int dst_x = plane_state->base.dst.x1;
  2936. int dst_y = plane_state->base.dst.y1;
  2937. int dst_w = drm_rect_width(&plane_state->base.dst);
  2938. int dst_h = drm_rect_height(&plane_state->base.dst);
  2939. plane_ctl = PLANE_CTL_ENABLE |
  2940. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2941. PLANE_CTL_PIPE_CSC_ENABLE;
  2942. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2943. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2944. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2945. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2946. /* Sizes are 0 based */
  2947. src_w--;
  2948. src_h--;
  2949. dst_w--;
  2950. dst_h--;
  2951. intel_crtc->dspaddr_offset = surf_addr;
  2952. intel_crtc->adjusted_x = src_x;
  2953. intel_crtc->adjusted_y = src_y;
  2954. if (wm->dirty_pipes & drm_crtc_mask(&intel_crtc->base))
  2955. skl_write_plane_wm(intel_crtc, p_wm, &wm->ddb, 0);
  2956. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2957. I915_WRITE(PLANE_OFFSET(pipe, 0), (src_y << 16) | src_x);
  2958. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2959. I915_WRITE(PLANE_SIZE(pipe, 0), (src_h << 16) | src_w);
  2960. if (scaler_id >= 0) {
  2961. uint32_t ps_ctrl = 0;
  2962. WARN_ON(!dst_w || !dst_h);
  2963. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2964. crtc_state->scaler_state.scalers[scaler_id].mode;
  2965. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2966. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2967. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2968. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2969. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2970. } else {
  2971. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2972. }
  2973. I915_WRITE(PLANE_SURF(pipe, 0),
  2974. intel_fb_gtt_offset(fb, rotation) + surf_addr);
  2975. POSTING_READ(PLANE_SURF(pipe, 0));
  2976. }
  2977. static void skylake_disable_primary_plane(struct drm_plane *primary,
  2978. struct drm_crtc *crtc)
  2979. {
  2980. struct drm_device *dev = crtc->dev;
  2981. struct drm_i915_private *dev_priv = to_i915(dev);
  2982. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2983. struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
  2984. const struct skl_plane_wm *p_wm = &cstate->wm.skl.optimal.planes[0];
  2985. int pipe = intel_crtc->pipe;
  2986. /*
  2987. * We only populate skl_results on watermark updates, and if the
  2988. * plane's visiblity isn't actually changing neither is its watermarks.
  2989. */
  2990. if (!crtc->primary->state->visible)
  2991. skl_write_plane_wm(intel_crtc, p_wm,
  2992. &dev_priv->wm.skl_results.ddb, 0);
  2993. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2994. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2995. POSTING_READ(PLANE_SURF(pipe, 0));
  2996. }
  2997. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2998. static int
  2999. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  3000. int x, int y, enum mode_set_atomic state)
  3001. {
  3002. /* Support for kgdboc is disabled, this needs a major rework. */
  3003. DRM_ERROR("legacy panic handler not supported any more.\n");
  3004. return -ENODEV;
  3005. }
  3006. static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
  3007. {
  3008. struct intel_crtc *crtc;
  3009. for_each_intel_crtc(&dev_priv->drm, crtc)
  3010. intel_finish_page_flip_cs(dev_priv, crtc->pipe);
  3011. }
  3012. static void intel_update_primary_planes(struct drm_device *dev)
  3013. {
  3014. struct drm_crtc *crtc;
  3015. for_each_crtc(dev, crtc) {
  3016. struct intel_plane *plane = to_intel_plane(crtc->primary);
  3017. struct intel_plane_state *plane_state =
  3018. to_intel_plane_state(plane->base.state);
  3019. if (plane_state->base.visible)
  3020. plane->update_plane(&plane->base,
  3021. to_intel_crtc_state(crtc->state),
  3022. plane_state);
  3023. }
  3024. }
  3025. static int
  3026. __intel_display_resume(struct drm_device *dev,
  3027. struct drm_atomic_state *state)
  3028. {
  3029. struct drm_crtc_state *crtc_state;
  3030. struct drm_crtc *crtc;
  3031. int i, ret;
  3032. intel_modeset_setup_hw_state(dev);
  3033. i915_redisable_vga(dev);
  3034. if (!state)
  3035. return 0;
  3036. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  3037. /*
  3038. * Force recalculation even if we restore
  3039. * current state. With fast modeset this may not result
  3040. * in a modeset when the state is compatible.
  3041. */
  3042. crtc_state->mode_changed = true;
  3043. }
  3044. /* ignore any reset values/BIOS leftovers in the WM registers */
  3045. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  3046. ret = drm_atomic_commit(state);
  3047. WARN_ON(ret == -EDEADLK);
  3048. return ret;
  3049. }
  3050. static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
  3051. {
  3052. return intel_has_gpu_reset(dev_priv) &&
  3053. INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
  3054. }
  3055. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  3056. {
  3057. struct drm_device *dev = &dev_priv->drm;
  3058. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3059. struct drm_atomic_state *state;
  3060. int ret;
  3061. /*
  3062. * Need mode_config.mutex so that we don't
  3063. * trample ongoing ->detect() and whatnot.
  3064. */
  3065. mutex_lock(&dev->mode_config.mutex);
  3066. drm_modeset_acquire_init(ctx, 0);
  3067. while (1) {
  3068. ret = drm_modeset_lock_all_ctx(dev, ctx);
  3069. if (ret != -EDEADLK)
  3070. break;
  3071. drm_modeset_backoff(ctx);
  3072. }
  3073. /* reset doesn't touch the display, but flips might get nuked anyway, */
  3074. if (!i915.force_reset_modeset_test &&
  3075. !gpu_reset_clobbers_display(dev_priv))
  3076. return;
  3077. /*
  3078. * Disabling the crtcs gracefully seems nicer. Also the
  3079. * g33 docs say we should at least disable all the planes.
  3080. */
  3081. state = drm_atomic_helper_duplicate_state(dev, ctx);
  3082. if (IS_ERR(state)) {
  3083. ret = PTR_ERR(state);
  3084. state = NULL;
  3085. DRM_ERROR("Duplicating state failed with %i\n", ret);
  3086. goto err;
  3087. }
  3088. ret = drm_atomic_helper_disable_all(dev, ctx);
  3089. if (ret) {
  3090. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  3091. goto err;
  3092. }
  3093. dev_priv->modeset_restore_state = state;
  3094. state->acquire_ctx = ctx;
  3095. return;
  3096. err:
  3097. drm_atomic_state_put(state);
  3098. }
  3099. void intel_finish_reset(struct drm_i915_private *dev_priv)
  3100. {
  3101. struct drm_device *dev = &dev_priv->drm;
  3102. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3103. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  3104. int ret;
  3105. /*
  3106. * Flips in the rings will be nuked by the reset,
  3107. * so complete all pending flips so that user space
  3108. * will get its events and not get stuck.
  3109. */
  3110. intel_complete_page_flips(dev_priv);
  3111. dev_priv->modeset_restore_state = NULL;
  3112. /* reset doesn't touch the display */
  3113. if (!gpu_reset_clobbers_display(dev_priv)) {
  3114. if (!state) {
  3115. /*
  3116. * Flips in the rings have been nuked by the reset,
  3117. * so update the base address of all primary
  3118. * planes to the the last fb to make sure we're
  3119. * showing the correct fb after a reset.
  3120. *
  3121. * FIXME: Atomic will make this obsolete since we won't schedule
  3122. * CS-based flips (which might get lost in gpu resets) any more.
  3123. */
  3124. intel_update_primary_planes(dev);
  3125. } else {
  3126. ret = __intel_display_resume(dev, state);
  3127. if (ret)
  3128. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3129. }
  3130. } else {
  3131. /*
  3132. * The display has been reset as well,
  3133. * so need a full re-initialization.
  3134. */
  3135. intel_runtime_pm_disable_interrupts(dev_priv);
  3136. intel_runtime_pm_enable_interrupts(dev_priv);
  3137. intel_pps_unlock_regs_wa(dev_priv);
  3138. intel_modeset_init_hw(dev);
  3139. spin_lock_irq(&dev_priv->irq_lock);
  3140. if (dev_priv->display.hpd_irq_setup)
  3141. dev_priv->display.hpd_irq_setup(dev_priv);
  3142. spin_unlock_irq(&dev_priv->irq_lock);
  3143. ret = __intel_display_resume(dev, state);
  3144. if (ret)
  3145. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3146. intel_hpd_init(dev_priv);
  3147. }
  3148. if (state)
  3149. drm_atomic_state_put(state);
  3150. drm_modeset_drop_locks(ctx);
  3151. drm_modeset_acquire_fini(ctx);
  3152. mutex_unlock(&dev->mode_config.mutex);
  3153. }
  3154. static bool abort_flip_on_reset(struct intel_crtc *crtc)
  3155. {
  3156. struct i915_gpu_error *error = &to_i915(crtc->base.dev)->gpu_error;
  3157. if (i915_reset_in_progress(error))
  3158. return true;
  3159. if (crtc->reset_count != i915_reset_count(error))
  3160. return true;
  3161. return false;
  3162. }
  3163. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  3164. {
  3165. struct drm_device *dev = crtc->dev;
  3166. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3167. bool pending;
  3168. if (abort_flip_on_reset(intel_crtc))
  3169. return false;
  3170. spin_lock_irq(&dev->event_lock);
  3171. pending = to_intel_crtc(crtc)->flip_work != NULL;
  3172. spin_unlock_irq(&dev->event_lock);
  3173. return pending;
  3174. }
  3175. static void intel_update_pipe_config(struct intel_crtc *crtc,
  3176. struct intel_crtc_state *old_crtc_state)
  3177. {
  3178. struct drm_device *dev = crtc->base.dev;
  3179. struct drm_i915_private *dev_priv = to_i915(dev);
  3180. struct intel_crtc_state *pipe_config =
  3181. to_intel_crtc_state(crtc->base.state);
  3182. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  3183. crtc->base.mode = crtc->base.state->mode;
  3184. DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
  3185. old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
  3186. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  3187. /*
  3188. * Update pipe size and adjust fitter if needed: the reason for this is
  3189. * that in compute_mode_changes we check the native mode (not the pfit
  3190. * mode) to see if we can flip rather than do a full mode set. In the
  3191. * fastboot case, we'll flip, but if we don't update the pipesrc and
  3192. * pfit state, we'll end up with a big fb scanned out into the wrong
  3193. * sized surface.
  3194. */
  3195. I915_WRITE(PIPESRC(crtc->pipe),
  3196. ((pipe_config->pipe_src_w - 1) << 16) |
  3197. (pipe_config->pipe_src_h - 1));
  3198. /* on skylake this is done by detaching scalers */
  3199. if (INTEL_INFO(dev)->gen >= 9) {
  3200. skl_detach_scalers(crtc);
  3201. if (pipe_config->pch_pfit.enabled)
  3202. skylake_pfit_enable(crtc);
  3203. } else if (HAS_PCH_SPLIT(dev_priv)) {
  3204. if (pipe_config->pch_pfit.enabled)
  3205. ironlake_pfit_enable(crtc);
  3206. else if (old_crtc_state->pch_pfit.enabled)
  3207. ironlake_pfit_disable(crtc, true);
  3208. }
  3209. }
  3210. static void intel_fdi_normal_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;
  3218. /* enable normal train */
  3219. reg = FDI_TX_CTL(pipe);
  3220. temp = I915_READ(reg);
  3221. if (IS_IVYBRIDGE(dev_priv)) {
  3222. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3223. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  3224. } else {
  3225. temp &= ~FDI_LINK_TRAIN_NONE;
  3226. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  3227. }
  3228. I915_WRITE(reg, temp);
  3229. reg = FDI_RX_CTL(pipe);
  3230. temp = I915_READ(reg);
  3231. if (HAS_PCH_CPT(dev_priv)) {
  3232. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3233. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  3234. } else {
  3235. temp &= ~FDI_LINK_TRAIN_NONE;
  3236. temp |= FDI_LINK_TRAIN_NONE;
  3237. }
  3238. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  3239. /* wait one idle pattern time */
  3240. POSTING_READ(reg);
  3241. udelay(1000);
  3242. /* IVB wants error correction enabled */
  3243. if (IS_IVYBRIDGE(dev_priv))
  3244. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  3245. FDI_FE_ERRC_ENABLE);
  3246. }
  3247. /* The FDI link training functions for ILK/Ibexpeak. */
  3248. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  3249. {
  3250. struct drm_device *dev = crtc->dev;
  3251. struct drm_i915_private *dev_priv = to_i915(dev);
  3252. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3253. int pipe = intel_crtc->pipe;
  3254. i915_reg_t reg;
  3255. u32 temp, tries;
  3256. /* FDI needs bits from pipe first */
  3257. assert_pipe_enabled(dev_priv, pipe);
  3258. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3259. for train result */
  3260. reg = FDI_RX_IMR(pipe);
  3261. temp = I915_READ(reg);
  3262. temp &= ~FDI_RX_SYMBOL_LOCK;
  3263. temp &= ~FDI_RX_BIT_LOCK;
  3264. I915_WRITE(reg, temp);
  3265. I915_READ(reg);
  3266. udelay(150);
  3267. /* enable CPU FDI TX and PCH FDI RX */
  3268. reg = FDI_TX_CTL(pipe);
  3269. temp = I915_READ(reg);
  3270. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3271. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3272. temp &= ~FDI_LINK_TRAIN_NONE;
  3273. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3274. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3275. reg = FDI_RX_CTL(pipe);
  3276. temp = I915_READ(reg);
  3277. temp &= ~FDI_LINK_TRAIN_NONE;
  3278. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3279. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3280. POSTING_READ(reg);
  3281. udelay(150);
  3282. /* Ironlake workaround, enable clock pointer after FDI enable*/
  3283. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3284. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  3285. FDI_RX_PHASE_SYNC_POINTER_EN);
  3286. reg = FDI_RX_IIR(pipe);
  3287. for (tries = 0; tries < 5; tries++) {
  3288. temp = I915_READ(reg);
  3289. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3290. if ((temp & FDI_RX_BIT_LOCK)) {
  3291. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3292. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3293. break;
  3294. }
  3295. }
  3296. if (tries == 5)
  3297. DRM_ERROR("FDI train 1 fail!\n");
  3298. /* Train 2 */
  3299. reg = FDI_TX_CTL(pipe);
  3300. temp = I915_READ(reg);
  3301. temp &= ~FDI_LINK_TRAIN_NONE;
  3302. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3303. I915_WRITE(reg, temp);
  3304. reg = FDI_RX_CTL(pipe);
  3305. temp = I915_READ(reg);
  3306. temp &= ~FDI_LINK_TRAIN_NONE;
  3307. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3308. I915_WRITE(reg, temp);
  3309. POSTING_READ(reg);
  3310. udelay(150);
  3311. reg = FDI_RX_IIR(pipe);
  3312. for (tries = 0; tries < 5; tries++) {
  3313. temp = I915_READ(reg);
  3314. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3315. if (temp & FDI_RX_SYMBOL_LOCK) {
  3316. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3317. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3318. break;
  3319. }
  3320. }
  3321. if (tries == 5)
  3322. DRM_ERROR("FDI train 2 fail!\n");
  3323. DRM_DEBUG_KMS("FDI train done\n");
  3324. }
  3325. static const int snb_b_fdi_train_param[] = {
  3326. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3327. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3328. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3329. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3330. };
  3331. /* The FDI link training functions for SNB/Cougarpoint. */
  3332. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  3333. {
  3334. struct drm_device *dev = crtc->dev;
  3335. struct drm_i915_private *dev_priv = to_i915(dev);
  3336. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3337. int pipe = intel_crtc->pipe;
  3338. i915_reg_t reg;
  3339. u32 temp, i, retry;
  3340. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3341. for train result */
  3342. reg = FDI_RX_IMR(pipe);
  3343. temp = I915_READ(reg);
  3344. temp &= ~FDI_RX_SYMBOL_LOCK;
  3345. temp &= ~FDI_RX_BIT_LOCK;
  3346. I915_WRITE(reg, temp);
  3347. POSTING_READ(reg);
  3348. udelay(150);
  3349. /* enable CPU FDI TX and PCH FDI RX */
  3350. reg = FDI_TX_CTL(pipe);
  3351. temp = I915_READ(reg);
  3352. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3353. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3354. temp &= ~FDI_LINK_TRAIN_NONE;
  3355. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3356. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3357. /* SNB-B */
  3358. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3359. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3360. I915_WRITE(FDI_RX_MISC(pipe),
  3361. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3362. reg = FDI_RX_CTL(pipe);
  3363. temp = I915_READ(reg);
  3364. if (HAS_PCH_CPT(dev_priv)) {
  3365. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3366. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3367. } else {
  3368. temp &= ~FDI_LINK_TRAIN_NONE;
  3369. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3370. }
  3371. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3372. POSTING_READ(reg);
  3373. udelay(150);
  3374. for (i = 0; i < 4; i++) {
  3375. reg = FDI_TX_CTL(pipe);
  3376. temp = I915_READ(reg);
  3377. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3378. temp |= snb_b_fdi_train_param[i];
  3379. I915_WRITE(reg, temp);
  3380. POSTING_READ(reg);
  3381. udelay(500);
  3382. for (retry = 0; retry < 5; retry++) {
  3383. reg = FDI_RX_IIR(pipe);
  3384. temp = I915_READ(reg);
  3385. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3386. if (temp & FDI_RX_BIT_LOCK) {
  3387. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3388. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3389. break;
  3390. }
  3391. udelay(50);
  3392. }
  3393. if (retry < 5)
  3394. break;
  3395. }
  3396. if (i == 4)
  3397. DRM_ERROR("FDI train 1 fail!\n");
  3398. /* Train 2 */
  3399. reg = FDI_TX_CTL(pipe);
  3400. temp = I915_READ(reg);
  3401. temp &= ~FDI_LINK_TRAIN_NONE;
  3402. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3403. if (IS_GEN6(dev_priv)) {
  3404. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3405. /* SNB-B */
  3406. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3407. }
  3408. I915_WRITE(reg, temp);
  3409. reg = FDI_RX_CTL(pipe);
  3410. temp = I915_READ(reg);
  3411. if (HAS_PCH_CPT(dev_priv)) {
  3412. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3413. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3414. } else {
  3415. temp &= ~FDI_LINK_TRAIN_NONE;
  3416. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3417. }
  3418. I915_WRITE(reg, temp);
  3419. POSTING_READ(reg);
  3420. udelay(150);
  3421. for (i = 0; i < 4; i++) {
  3422. reg = FDI_TX_CTL(pipe);
  3423. temp = I915_READ(reg);
  3424. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3425. temp |= snb_b_fdi_train_param[i];
  3426. I915_WRITE(reg, temp);
  3427. POSTING_READ(reg);
  3428. udelay(500);
  3429. for (retry = 0; retry < 5; retry++) {
  3430. reg = FDI_RX_IIR(pipe);
  3431. temp = I915_READ(reg);
  3432. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3433. if (temp & FDI_RX_SYMBOL_LOCK) {
  3434. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3435. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3436. break;
  3437. }
  3438. udelay(50);
  3439. }
  3440. if (retry < 5)
  3441. break;
  3442. }
  3443. if (i == 4)
  3444. DRM_ERROR("FDI train 2 fail!\n");
  3445. DRM_DEBUG_KMS("FDI train done.\n");
  3446. }
  3447. /* Manual link training for Ivy Bridge A0 parts */
  3448. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3449. {
  3450. struct drm_device *dev = crtc->dev;
  3451. struct drm_i915_private *dev_priv = to_i915(dev);
  3452. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3453. int pipe = intel_crtc->pipe;
  3454. i915_reg_t reg;
  3455. u32 temp, i, j;
  3456. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3457. for train result */
  3458. reg = FDI_RX_IMR(pipe);
  3459. temp = I915_READ(reg);
  3460. temp &= ~FDI_RX_SYMBOL_LOCK;
  3461. temp &= ~FDI_RX_BIT_LOCK;
  3462. I915_WRITE(reg, temp);
  3463. POSTING_READ(reg);
  3464. udelay(150);
  3465. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3466. I915_READ(FDI_RX_IIR(pipe)));
  3467. /* Try each vswing and preemphasis setting twice before moving on */
  3468. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3469. /* disable first in case we need to retry */
  3470. reg = FDI_TX_CTL(pipe);
  3471. temp = I915_READ(reg);
  3472. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3473. temp &= ~FDI_TX_ENABLE;
  3474. I915_WRITE(reg, temp);
  3475. reg = FDI_RX_CTL(pipe);
  3476. temp = I915_READ(reg);
  3477. temp &= ~FDI_LINK_TRAIN_AUTO;
  3478. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3479. temp &= ~FDI_RX_ENABLE;
  3480. I915_WRITE(reg, temp);
  3481. /* enable CPU FDI TX and PCH FDI RX */
  3482. reg = FDI_TX_CTL(pipe);
  3483. temp = I915_READ(reg);
  3484. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3485. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3486. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3487. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3488. temp |= snb_b_fdi_train_param[j/2];
  3489. temp |= FDI_COMPOSITE_SYNC;
  3490. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3491. I915_WRITE(FDI_RX_MISC(pipe),
  3492. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3493. reg = FDI_RX_CTL(pipe);
  3494. temp = I915_READ(reg);
  3495. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3496. temp |= FDI_COMPOSITE_SYNC;
  3497. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3498. POSTING_READ(reg);
  3499. udelay(1); /* should be 0.5us */
  3500. for (i = 0; i < 4; i++) {
  3501. reg = FDI_RX_IIR(pipe);
  3502. temp = I915_READ(reg);
  3503. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3504. if (temp & FDI_RX_BIT_LOCK ||
  3505. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3506. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3507. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3508. i);
  3509. break;
  3510. }
  3511. udelay(1); /* should be 0.5us */
  3512. }
  3513. if (i == 4) {
  3514. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3515. continue;
  3516. }
  3517. /* Train 2 */
  3518. reg = FDI_TX_CTL(pipe);
  3519. temp = I915_READ(reg);
  3520. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3521. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3522. I915_WRITE(reg, temp);
  3523. reg = FDI_RX_CTL(pipe);
  3524. temp = I915_READ(reg);
  3525. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3526. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3527. I915_WRITE(reg, temp);
  3528. POSTING_READ(reg);
  3529. udelay(2); /* should be 1.5us */
  3530. for (i = 0; i < 4; i++) {
  3531. reg = FDI_RX_IIR(pipe);
  3532. temp = I915_READ(reg);
  3533. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3534. if (temp & FDI_RX_SYMBOL_LOCK ||
  3535. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3536. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3537. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3538. i);
  3539. goto train_done;
  3540. }
  3541. udelay(2); /* should be 1.5us */
  3542. }
  3543. if (i == 4)
  3544. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3545. }
  3546. train_done:
  3547. DRM_DEBUG_KMS("FDI train done.\n");
  3548. }
  3549. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3550. {
  3551. struct drm_device *dev = intel_crtc->base.dev;
  3552. struct drm_i915_private *dev_priv = to_i915(dev);
  3553. int pipe = intel_crtc->pipe;
  3554. i915_reg_t reg;
  3555. u32 temp;
  3556. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3557. reg = FDI_RX_CTL(pipe);
  3558. temp = I915_READ(reg);
  3559. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3560. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3561. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3562. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3563. POSTING_READ(reg);
  3564. udelay(200);
  3565. /* Switch from Rawclk to PCDclk */
  3566. temp = I915_READ(reg);
  3567. I915_WRITE(reg, temp | FDI_PCDCLK);
  3568. POSTING_READ(reg);
  3569. udelay(200);
  3570. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3571. reg = FDI_TX_CTL(pipe);
  3572. temp = I915_READ(reg);
  3573. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3574. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3575. POSTING_READ(reg);
  3576. udelay(100);
  3577. }
  3578. }
  3579. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3580. {
  3581. struct drm_device *dev = intel_crtc->base.dev;
  3582. struct drm_i915_private *dev_priv = to_i915(dev);
  3583. int pipe = intel_crtc->pipe;
  3584. i915_reg_t reg;
  3585. u32 temp;
  3586. /* Switch from PCDclk to Rawclk */
  3587. reg = FDI_RX_CTL(pipe);
  3588. temp = I915_READ(reg);
  3589. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3590. /* Disable CPU FDI TX PLL */
  3591. reg = FDI_TX_CTL(pipe);
  3592. temp = I915_READ(reg);
  3593. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3594. POSTING_READ(reg);
  3595. udelay(100);
  3596. reg = FDI_RX_CTL(pipe);
  3597. temp = I915_READ(reg);
  3598. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3599. /* Wait for the clocks to turn off. */
  3600. POSTING_READ(reg);
  3601. udelay(100);
  3602. }
  3603. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3604. {
  3605. struct drm_device *dev = crtc->dev;
  3606. struct drm_i915_private *dev_priv = to_i915(dev);
  3607. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3608. int pipe = intel_crtc->pipe;
  3609. i915_reg_t reg;
  3610. u32 temp;
  3611. /* disable CPU FDI tx and PCH FDI rx */
  3612. reg = FDI_TX_CTL(pipe);
  3613. temp = I915_READ(reg);
  3614. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3615. POSTING_READ(reg);
  3616. reg = FDI_RX_CTL(pipe);
  3617. temp = I915_READ(reg);
  3618. temp &= ~(0x7 << 16);
  3619. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3620. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3621. POSTING_READ(reg);
  3622. udelay(100);
  3623. /* Ironlake workaround, disable clock pointer after downing FDI */
  3624. if (HAS_PCH_IBX(dev_priv))
  3625. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3626. /* still set train pattern 1 */
  3627. reg = FDI_TX_CTL(pipe);
  3628. temp = I915_READ(reg);
  3629. temp &= ~FDI_LINK_TRAIN_NONE;
  3630. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3631. I915_WRITE(reg, temp);
  3632. reg = FDI_RX_CTL(pipe);
  3633. temp = I915_READ(reg);
  3634. if (HAS_PCH_CPT(dev_priv)) {
  3635. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3636. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3637. } else {
  3638. temp &= ~FDI_LINK_TRAIN_NONE;
  3639. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3640. }
  3641. /* BPC in FDI rx is consistent with that in PIPECONF */
  3642. temp &= ~(0x07 << 16);
  3643. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3644. I915_WRITE(reg, temp);
  3645. POSTING_READ(reg);
  3646. udelay(100);
  3647. }
  3648. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3649. {
  3650. struct drm_i915_private *dev_priv = to_i915(dev);
  3651. struct intel_crtc *crtc;
  3652. /* Note that we don't need to be called with mode_config.lock here
  3653. * as our list of CRTC objects is static for the lifetime of the
  3654. * device and so cannot disappear as we iterate. Similarly, we can
  3655. * happily treat the predicates as racy, atomic checks as userspace
  3656. * cannot claim and pin a new fb without at least acquring the
  3657. * struct_mutex and so serialising with us.
  3658. */
  3659. for_each_intel_crtc(dev, crtc) {
  3660. if (atomic_read(&crtc->unpin_work_count) == 0)
  3661. continue;
  3662. if (crtc->flip_work)
  3663. intel_wait_for_vblank(dev_priv, crtc->pipe);
  3664. return true;
  3665. }
  3666. return false;
  3667. }
  3668. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3669. {
  3670. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3671. struct intel_flip_work *work = intel_crtc->flip_work;
  3672. intel_crtc->flip_work = NULL;
  3673. if (work->event)
  3674. drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
  3675. drm_crtc_vblank_put(&intel_crtc->base);
  3676. wake_up_all(&dev_priv->pending_flip_queue);
  3677. queue_work(dev_priv->wq, &work->unpin_work);
  3678. trace_i915_flip_complete(intel_crtc->plane,
  3679. work->pending_flip_obj);
  3680. }
  3681. static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3682. {
  3683. struct drm_device *dev = crtc->dev;
  3684. struct drm_i915_private *dev_priv = to_i915(dev);
  3685. long ret;
  3686. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3687. ret = wait_event_interruptible_timeout(
  3688. dev_priv->pending_flip_queue,
  3689. !intel_crtc_has_pending_flip(crtc),
  3690. 60*HZ);
  3691. if (ret < 0)
  3692. return ret;
  3693. if (ret == 0) {
  3694. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3695. struct intel_flip_work *work;
  3696. spin_lock_irq(&dev->event_lock);
  3697. work = intel_crtc->flip_work;
  3698. if (work && !is_mmio_work(work)) {
  3699. WARN_ONCE(1, "Removing stuck page flip\n");
  3700. page_flip_completed(intel_crtc);
  3701. }
  3702. spin_unlock_irq(&dev->event_lock);
  3703. }
  3704. return 0;
  3705. }
  3706. void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3707. {
  3708. u32 temp;
  3709. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3710. mutex_lock(&dev_priv->sb_lock);
  3711. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3712. temp |= SBI_SSCCTL_DISABLE;
  3713. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3714. mutex_unlock(&dev_priv->sb_lock);
  3715. }
  3716. /* Program iCLKIP clock to the desired frequency */
  3717. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3718. {
  3719. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  3720. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3721. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3722. u32 temp;
  3723. lpt_disable_iclkip(dev_priv);
  3724. /* The iCLK virtual clock root frequency is in MHz,
  3725. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3726. * divisors, it is necessary to divide one by another, so we
  3727. * convert the virtual clock precision to KHz here for higher
  3728. * precision.
  3729. */
  3730. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3731. u32 iclk_virtual_root_freq = 172800 * 1000;
  3732. u32 iclk_pi_range = 64;
  3733. u32 desired_divisor;
  3734. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3735. clock << auxdiv);
  3736. divsel = (desired_divisor / iclk_pi_range) - 2;
  3737. phaseinc = desired_divisor % iclk_pi_range;
  3738. /*
  3739. * Near 20MHz is a corner case which is
  3740. * out of range for the 7-bit divisor
  3741. */
  3742. if (divsel <= 0x7f)
  3743. break;
  3744. }
  3745. /* This should not happen with any sane values */
  3746. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3747. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3748. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3749. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3750. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3751. clock,
  3752. auxdiv,
  3753. divsel,
  3754. phasedir,
  3755. phaseinc);
  3756. mutex_lock(&dev_priv->sb_lock);
  3757. /* Program SSCDIVINTPHASE6 */
  3758. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3759. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3760. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3761. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3762. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3763. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3764. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3765. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3766. /* Program SSCAUXDIV */
  3767. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3768. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3769. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3770. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3771. /* Enable modulator and associated divider */
  3772. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3773. temp &= ~SBI_SSCCTL_DISABLE;
  3774. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3775. mutex_unlock(&dev_priv->sb_lock);
  3776. /* Wait for initialization time */
  3777. udelay(24);
  3778. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3779. }
  3780. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3781. {
  3782. u32 divsel, phaseinc, auxdiv;
  3783. u32 iclk_virtual_root_freq = 172800 * 1000;
  3784. u32 iclk_pi_range = 64;
  3785. u32 desired_divisor;
  3786. u32 temp;
  3787. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3788. return 0;
  3789. mutex_lock(&dev_priv->sb_lock);
  3790. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3791. if (temp & SBI_SSCCTL_DISABLE) {
  3792. mutex_unlock(&dev_priv->sb_lock);
  3793. return 0;
  3794. }
  3795. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3796. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3797. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3798. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3799. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3800. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3801. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3802. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3803. mutex_unlock(&dev_priv->sb_lock);
  3804. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3805. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3806. desired_divisor << auxdiv);
  3807. }
  3808. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3809. enum pipe pch_transcoder)
  3810. {
  3811. struct drm_device *dev = crtc->base.dev;
  3812. struct drm_i915_private *dev_priv = to_i915(dev);
  3813. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3814. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3815. I915_READ(HTOTAL(cpu_transcoder)));
  3816. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3817. I915_READ(HBLANK(cpu_transcoder)));
  3818. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3819. I915_READ(HSYNC(cpu_transcoder)));
  3820. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3821. I915_READ(VTOTAL(cpu_transcoder)));
  3822. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3823. I915_READ(VBLANK(cpu_transcoder)));
  3824. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3825. I915_READ(VSYNC(cpu_transcoder)));
  3826. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3827. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3828. }
  3829. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3830. {
  3831. struct drm_i915_private *dev_priv = to_i915(dev);
  3832. uint32_t temp;
  3833. temp = I915_READ(SOUTH_CHICKEN1);
  3834. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3835. return;
  3836. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3837. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3838. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3839. if (enable)
  3840. temp |= FDI_BC_BIFURCATION_SELECT;
  3841. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3842. I915_WRITE(SOUTH_CHICKEN1, temp);
  3843. POSTING_READ(SOUTH_CHICKEN1);
  3844. }
  3845. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3846. {
  3847. struct drm_device *dev = intel_crtc->base.dev;
  3848. switch (intel_crtc->pipe) {
  3849. case PIPE_A:
  3850. break;
  3851. case PIPE_B:
  3852. if (intel_crtc->config->fdi_lanes > 2)
  3853. cpt_set_fdi_bc_bifurcation(dev, false);
  3854. else
  3855. cpt_set_fdi_bc_bifurcation(dev, true);
  3856. break;
  3857. case PIPE_C:
  3858. cpt_set_fdi_bc_bifurcation(dev, true);
  3859. break;
  3860. default:
  3861. BUG();
  3862. }
  3863. }
  3864. /* Return which DP Port should be selected for Transcoder DP control */
  3865. static enum port
  3866. intel_trans_dp_port_sel(struct drm_crtc *crtc)
  3867. {
  3868. struct drm_device *dev = crtc->dev;
  3869. struct intel_encoder *encoder;
  3870. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3871. if (encoder->type == INTEL_OUTPUT_DP ||
  3872. encoder->type == INTEL_OUTPUT_EDP)
  3873. return enc_to_dig_port(&encoder->base)->port;
  3874. }
  3875. return -1;
  3876. }
  3877. /*
  3878. * Enable PCH resources required for PCH ports:
  3879. * - PCH PLLs
  3880. * - FDI training & RX/TX
  3881. * - update transcoder timings
  3882. * - DP transcoding bits
  3883. * - transcoder
  3884. */
  3885. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3886. {
  3887. struct drm_device *dev = crtc->dev;
  3888. struct drm_i915_private *dev_priv = to_i915(dev);
  3889. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3890. int pipe = intel_crtc->pipe;
  3891. u32 temp;
  3892. assert_pch_transcoder_disabled(dev_priv, pipe);
  3893. if (IS_IVYBRIDGE(dev_priv))
  3894. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3895. /* Write the TU size bits before fdi link training, so that error
  3896. * detection works. */
  3897. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3898. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3899. /* For PCH output, training FDI link */
  3900. dev_priv->display.fdi_link_train(crtc);
  3901. /* We need to program the right clock selection before writing the pixel
  3902. * mutliplier into the DPLL. */
  3903. if (HAS_PCH_CPT(dev_priv)) {
  3904. u32 sel;
  3905. temp = I915_READ(PCH_DPLL_SEL);
  3906. temp |= TRANS_DPLL_ENABLE(pipe);
  3907. sel = TRANS_DPLLB_SEL(pipe);
  3908. if (intel_crtc->config->shared_dpll ==
  3909. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3910. temp |= sel;
  3911. else
  3912. temp &= ~sel;
  3913. I915_WRITE(PCH_DPLL_SEL, temp);
  3914. }
  3915. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3916. * transcoder, and we actually should do this to not upset any PCH
  3917. * transcoder that already use the clock when we share it.
  3918. *
  3919. * Note that enable_shared_dpll tries to do the right thing, but
  3920. * get_shared_dpll unconditionally resets the pll - we need that to have
  3921. * the right LVDS enable sequence. */
  3922. intel_enable_shared_dpll(intel_crtc);
  3923. /* set transcoder timing, panel must allow it */
  3924. assert_panel_unlocked(dev_priv, pipe);
  3925. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3926. intel_fdi_normal_train(crtc);
  3927. /* For PCH DP, enable TRANS_DP_CTL */
  3928. if (HAS_PCH_CPT(dev_priv) &&
  3929. intel_crtc_has_dp_encoder(intel_crtc->config)) {
  3930. const struct drm_display_mode *adjusted_mode =
  3931. &intel_crtc->config->base.adjusted_mode;
  3932. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3933. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3934. temp = I915_READ(reg);
  3935. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3936. TRANS_DP_SYNC_MASK |
  3937. TRANS_DP_BPC_MASK);
  3938. temp |= TRANS_DP_OUTPUT_ENABLE;
  3939. temp |= bpc << 9; /* same format but at 11:9 */
  3940. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3941. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3942. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3943. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3944. switch (intel_trans_dp_port_sel(crtc)) {
  3945. case PORT_B:
  3946. temp |= TRANS_DP_PORT_SEL_B;
  3947. break;
  3948. case PORT_C:
  3949. temp |= TRANS_DP_PORT_SEL_C;
  3950. break;
  3951. case PORT_D:
  3952. temp |= TRANS_DP_PORT_SEL_D;
  3953. break;
  3954. default:
  3955. BUG();
  3956. }
  3957. I915_WRITE(reg, temp);
  3958. }
  3959. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3960. }
  3961. static void lpt_pch_enable(struct drm_crtc *crtc)
  3962. {
  3963. struct drm_device *dev = crtc->dev;
  3964. struct drm_i915_private *dev_priv = to_i915(dev);
  3965. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3966. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3967. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3968. lpt_program_iclkip(crtc);
  3969. /* Set transcoder timing. */
  3970. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3971. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3972. }
  3973. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3974. {
  3975. struct drm_i915_private *dev_priv = to_i915(dev);
  3976. i915_reg_t dslreg = PIPEDSL(pipe);
  3977. u32 temp;
  3978. temp = I915_READ(dslreg);
  3979. udelay(500);
  3980. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3981. if (wait_for(I915_READ(dslreg) != temp, 5))
  3982. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3983. }
  3984. }
  3985. static int
  3986. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3987. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3988. int src_w, int src_h, int dst_w, int dst_h)
  3989. {
  3990. struct intel_crtc_scaler_state *scaler_state =
  3991. &crtc_state->scaler_state;
  3992. struct intel_crtc *intel_crtc =
  3993. to_intel_crtc(crtc_state->base.crtc);
  3994. int need_scaling;
  3995. need_scaling = drm_rotation_90_or_270(rotation) ?
  3996. (src_h != dst_w || src_w != dst_h):
  3997. (src_w != dst_w || src_h != dst_h);
  3998. /*
  3999. * if plane is being disabled or scaler is no more required or force detach
  4000. * - free scaler binded to this plane/crtc
  4001. * - in order to do this, update crtc->scaler_usage
  4002. *
  4003. * Here scaler state in crtc_state is set free so that
  4004. * scaler can be assigned to other user. Actual register
  4005. * update to free the scaler is done in plane/panel-fit programming.
  4006. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  4007. */
  4008. if (force_detach || !need_scaling) {
  4009. if (*scaler_id >= 0) {
  4010. scaler_state->scaler_users &= ~(1 << scaler_user);
  4011. scaler_state->scalers[*scaler_id].in_use = 0;
  4012. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  4013. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  4014. intel_crtc->pipe, scaler_user, *scaler_id,
  4015. scaler_state->scaler_users);
  4016. *scaler_id = -1;
  4017. }
  4018. return 0;
  4019. }
  4020. /* range checks */
  4021. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  4022. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  4023. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  4024. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  4025. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  4026. "size is out of scaler range\n",
  4027. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  4028. return -EINVAL;
  4029. }
  4030. /* mark this plane as a scaler user in crtc_state */
  4031. scaler_state->scaler_users |= (1 << scaler_user);
  4032. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  4033. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  4034. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  4035. scaler_state->scaler_users);
  4036. return 0;
  4037. }
  4038. /**
  4039. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  4040. *
  4041. * @state: crtc's scaler state
  4042. *
  4043. * Return
  4044. * 0 - scaler_usage updated successfully
  4045. * error - requested scaling cannot be supported or other error condition
  4046. */
  4047. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  4048. {
  4049. struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
  4050. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  4051. DRM_DEBUG_KMS("Updating scaler for [CRTC:%d:%s] scaler_user index %u.%u\n",
  4052. intel_crtc->base.base.id, intel_crtc->base.name,
  4053. intel_crtc->pipe, SKL_CRTC_INDEX);
  4054. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  4055. &state->scaler_state.scaler_id, DRM_ROTATE_0,
  4056. state->pipe_src_w, state->pipe_src_h,
  4057. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  4058. }
  4059. /**
  4060. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  4061. *
  4062. * @state: crtc's scaler state
  4063. * @plane_state: atomic plane state to update
  4064. *
  4065. * Return
  4066. * 0 - scaler_usage updated successfully
  4067. * error - requested scaling cannot be supported or other error condition
  4068. */
  4069. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  4070. struct intel_plane_state *plane_state)
  4071. {
  4072. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  4073. struct intel_plane *intel_plane =
  4074. to_intel_plane(plane_state->base.plane);
  4075. struct drm_framebuffer *fb = plane_state->base.fb;
  4076. int ret;
  4077. bool force_detach = !fb || !plane_state->base.visible;
  4078. DRM_DEBUG_KMS("Updating scaler for [PLANE:%d:%s] scaler_user index %u.%u\n",
  4079. intel_plane->base.base.id, intel_plane->base.name,
  4080. intel_crtc->pipe, drm_plane_index(&intel_plane->base));
  4081. ret = skl_update_scaler(crtc_state, force_detach,
  4082. drm_plane_index(&intel_plane->base),
  4083. &plane_state->scaler_id,
  4084. plane_state->base.rotation,
  4085. drm_rect_width(&plane_state->base.src) >> 16,
  4086. drm_rect_height(&plane_state->base.src) >> 16,
  4087. drm_rect_width(&plane_state->base.dst),
  4088. drm_rect_height(&plane_state->base.dst));
  4089. if (ret || plane_state->scaler_id < 0)
  4090. return ret;
  4091. /* check colorkey */
  4092. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  4093. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  4094. intel_plane->base.base.id,
  4095. intel_plane->base.name);
  4096. return -EINVAL;
  4097. }
  4098. /* Check src format */
  4099. switch (fb->pixel_format) {
  4100. case DRM_FORMAT_RGB565:
  4101. case DRM_FORMAT_XBGR8888:
  4102. case DRM_FORMAT_XRGB8888:
  4103. case DRM_FORMAT_ABGR8888:
  4104. case DRM_FORMAT_ARGB8888:
  4105. case DRM_FORMAT_XRGB2101010:
  4106. case DRM_FORMAT_XBGR2101010:
  4107. case DRM_FORMAT_YUYV:
  4108. case DRM_FORMAT_YVYU:
  4109. case DRM_FORMAT_UYVY:
  4110. case DRM_FORMAT_VYUY:
  4111. break;
  4112. default:
  4113. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  4114. intel_plane->base.base.id, intel_plane->base.name,
  4115. fb->base.id, fb->pixel_format);
  4116. return -EINVAL;
  4117. }
  4118. return 0;
  4119. }
  4120. static void skylake_scaler_disable(struct intel_crtc *crtc)
  4121. {
  4122. int i;
  4123. for (i = 0; i < crtc->num_scalers; i++)
  4124. skl_detach_scaler(crtc, i);
  4125. }
  4126. static void skylake_pfit_enable(struct intel_crtc *crtc)
  4127. {
  4128. struct drm_device *dev = crtc->base.dev;
  4129. struct drm_i915_private *dev_priv = to_i915(dev);
  4130. int pipe = crtc->pipe;
  4131. struct intel_crtc_scaler_state *scaler_state =
  4132. &crtc->config->scaler_state;
  4133. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  4134. if (crtc->config->pch_pfit.enabled) {
  4135. int id;
  4136. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  4137. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  4138. return;
  4139. }
  4140. id = scaler_state->scaler_id;
  4141. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  4142. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  4143. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  4144. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  4145. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  4146. }
  4147. }
  4148. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  4149. {
  4150. struct drm_device *dev = crtc->base.dev;
  4151. struct drm_i915_private *dev_priv = to_i915(dev);
  4152. int pipe = crtc->pipe;
  4153. if (crtc->config->pch_pfit.enabled) {
  4154. /* Force use of hard-coded filter coefficients
  4155. * as some pre-programmed values are broken,
  4156. * e.g. x201.
  4157. */
  4158. if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
  4159. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  4160. PF_PIPE_SEL_IVB(pipe));
  4161. else
  4162. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  4163. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  4164. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  4165. }
  4166. }
  4167. void hsw_enable_ips(struct intel_crtc *crtc)
  4168. {
  4169. struct drm_device *dev = crtc->base.dev;
  4170. struct drm_i915_private *dev_priv = to_i915(dev);
  4171. if (!crtc->config->ips_enabled)
  4172. return;
  4173. /*
  4174. * We can only enable IPS after we enable a plane and wait for a vblank
  4175. * This function is called from post_plane_update, which is run after
  4176. * a vblank wait.
  4177. */
  4178. assert_plane_enabled(dev_priv, crtc->plane);
  4179. if (IS_BROADWELL(dev_priv)) {
  4180. mutex_lock(&dev_priv->rps.hw_lock);
  4181. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  4182. mutex_unlock(&dev_priv->rps.hw_lock);
  4183. /* Quoting Art Runyan: "its not safe to expect any particular
  4184. * value in IPS_CTL bit 31 after enabling IPS through the
  4185. * mailbox." Moreover, the mailbox may return a bogus state,
  4186. * so we need to just enable it and continue on.
  4187. */
  4188. } else {
  4189. I915_WRITE(IPS_CTL, IPS_ENABLE);
  4190. /* The bit only becomes 1 in the next vblank, so this wait here
  4191. * is essentially intel_wait_for_vblank. If we don't have this
  4192. * and don't wait for vblanks until the end of crtc_enable, then
  4193. * the HW state readout code will complain that the expected
  4194. * IPS_CTL value is not the one we read. */
  4195. if (intel_wait_for_register(dev_priv,
  4196. IPS_CTL, IPS_ENABLE, IPS_ENABLE,
  4197. 50))
  4198. DRM_ERROR("Timed out waiting for IPS enable\n");
  4199. }
  4200. }
  4201. void hsw_disable_ips(struct intel_crtc *crtc)
  4202. {
  4203. struct drm_device *dev = crtc->base.dev;
  4204. struct drm_i915_private *dev_priv = to_i915(dev);
  4205. if (!crtc->config->ips_enabled)
  4206. return;
  4207. assert_plane_enabled(dev_priv, crtc->plane);
  4208. if (IS_BROADWELL(dev_priv)) {
  4209. mutex_lock(&dev_priv->rps.hw_lock);
  4210. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  4211. mutex_unlock(&dev_priv->rps.hw_lock);
  4212. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  4213. if (intel_wait_for_register(dev_priv,
  4214. IPS_CTL, IPS_ENABLE, 0,
  4215. 42))
  4216. DRM_ERROR("Timed out waiting for IPS disable\n");
  4217. } else {
  4218. I915_WRITE(IPS_CTL, 0);
  4219. POSTING_READ(IPS_CTL);
  4220. }
  4221. /* We need to wait for a vblank before we can disable the plane. */
  4222. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4223. }
  4224. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  4225. {
  4226. if (intel_crtc->overlay) {
  4227. struct drm_device *dev = intel_crtc->base.dev;
  4228. struct drm_i915_private *dev_priv = to_i915(dev);
  4229. mutex_lock(&dev->struct_mutex);
  4230. dev_priv->mm.interruptible = false;
  4231. (void) intel_overlay_switch_off(intel_crtc->overlay);
  4232. dev_priv->mm.interruptible = true;
  4233. mutex_unlock(&dev->struct_mutex);
  4234. }
  4235. /* Let userspace switch the overlay on again. In most cases userspace
  4236. * has to recompute where to put it anyway.
  4237. */
  4238. }
  4239. /**
  4240. * intel_post_enable_primary - Perform operations after enabling primary plane
  4241. * @crtc: the CRTC whose primary plane was just enabled
  4242. *
  4243. * Performs potentially sleeping operations that must be done after the primary
  4244. * plane is enabled, such as updating FBC and IPS. Note that this may be
  4245. * called due to an explicit primary plane update, or due to an implicit
  4246. * re-enable that is caused when a sprite plane is updated to no longer
  4247. * completely hide the primary plane.
  4248. */
  4249. static void
  4250. intel_post_enable_primary(struct drm_crtc *crtc)
  4251. {
  4252. struct drm_device *dev = crtc->dev;
  4253. struct drm_i915_private *dev_priv = to_i915(dev);
  4254. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4255. int pipe = intel_crtc->pipe;
  4256. /*
  4257. * FIXME IPS should be fine as long as one plane is
  4258. * enabled, but in practice it seems to have problems
  4259. * when going from primary only to sprite only and vice
  4260. * versa.
  4261. */
  4262. hsw_enable_ips(intel_crtc);
  4263. /*
  4264. * Gen2 reports pipe underruns whenever all planes are disabled.
  4265. * So don't enable underrun reporting before at least some planes
  4266. * are enabled.
  4267. * FIXME: Need to fix the logic to work when we turn off all planes
  4268. * but leave the pipe running.
  4269. */
  4270. if (IS_GEN2(dev_priv))
  4271. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4272. /* Underruns don't always raise interrupts, so check manually. */
  4273. intel_check_cpu_fifo_underruns(dev_priv);
  4274. intel_check_pch_fifo_underruns(dev_priv);
  4275. }
  4276. /* FIXME move all this to pre_plane_update() with proper state tracking */
  4277. static void
  4278. intel_pre_disable_primary(struct drm_crtc *crtc)
  4279. {
  4280. struct drm_device *dev = crtc->dev;
  4281. struct drm_i915_private *dev_priv = to_i915(dev);
  4282. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4283. int pipe = intel_crtc->pipe;
  4284. /*
  4285. * Gen2 reports pipe underruns whenever all planes are disabled.
  4286. * So diasble underrun reporting before all the planes get disabled.
  4287. * FIXME: Need to fix the logic to work when we turn off all planes
  4288. * but leave the pipe running.
  4289. */
  4290. if (IS_GEN2(dev_priv))
  4291. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4292. /*
  4293. * FIXME IPS should be fine as long as one plane is
  4294. * enabled, but in practice it seems to have problems
  4295. * when going from primary only to sprite only and vice
  4296. * versa.
  4297. */
  4298. hsw_disable_ips(intel_crtc);
  4299. }
  4300. /* FIXME get rid of this and use pre_plane_update */
  4301. static void
  4302. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  4303. {
  4304. struct drm_device *dev = crtc->dev;
  4305. struct drm_i915_private *dev_priv = to_i915(dev);
  4306. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4307. int pipe = intel_crtc->pipe;
  4308. intel_pre_disable_primary(crtc);
  4309. /*
  4310. * Vblank time updates from the shadow to live plane control register
  4311. * are blocked if the memory self-refresh mode is active at that
  4312. * moment. So to make sure the plane gets truly disabled, disable
  4313. * first the self-refresh mode. The self-refresh enable bit in turn
  4314. * will be checked/applied by the HW only at the next frame start
  4315. * event which is after the vblank start event, so we need to have a
  4316. * wait-for-vblank between disabling the plane and the pipe.
  4317. */
  4318. if (HAS_GMCH_DISPLAY(dev_priv)) {
  4319. intel_set_memory_cxsr(dev_priv, false);
  4320. dev_priv->wm.vlv.cxsr = false;
  4321. intel_wait_for_vblank(dev_priv, pipe);
  4322. }
  4323. }
  4324. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  4325. {
  4326. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4327. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4328. struct intel_crtc_state *pipe_config =
  4329. to_intel_crtc_state(crtc->base.state);
  4330. struct drm_plane *primary = crtc->base.primary;
  4331. struct drm_plane_state *old_pri_state =
  4332. drm_atomic_get_existing_plane_state(old_state, primary);
  4333. intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
  4334. crtc->wm.cxsr_allowed = true;
  4335. if (pipe_config->update_wm_post && pipe_config->base.active)
  4336. intel_update_watermarks(crtc);
  4337. if (old_pri_state) {
  4338. struct intel_plane_state *primary_state =
  4339. to_intel_plane_state(primary->state);
  4340. struct intel_plane_state *old_primary_state =
  4341. to_intel_plane_state(old_pri_state);
  4342. intel_fbc_post_update(crtc);
  4343. if (primary_state->base.visible &&
  4344. (needs_modeset(&pipe_config->base) ||
  4345. !old_primary_state->base.visible))
  4346. intel_post_enable_primary(&crtc->base);
  4347. }
  4348. }
  4349. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
  4350. {
  4351. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4352. struct drm_device *dev = crtc->base.dev;
  4353. struct drm_i915_private *dev_priv = to_i915(dev);
  4354. struct intel_crtc_state *pipe_config =
  4355. to_intel_crtc_state(crtc->base.state);
  4356. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4357. struct drm_plane *primary = crtc->base.primary;
  4358. struct drm_plane_state *old_pri_state =
  4359. drm_atomic_get_existing_plane_state(old_state, primary);
  4360. bool modeset = needs_modeset(&pipe_config->base);
  4361. if (old_pri_state) {
  4362. struct intel_plane_state *primary_state =
  4363. to_intel_plane_state(primary->state);
  4364. struct intel_plane_state *old_primary_state =
  4365. to_intel_plane_state(old_pri_state);
  4366. intel_fbc_pre_update(crtc, pipe_config, primary_state);
  4367. if (old_primary_state->base.visible &&
  4368. (modeset || !primary_state->base.visible))
  4369. intel_pre_disable_primary(&crtc->base);
  4370. }
  4371. if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev_priv)) {
  4372. crtc->wm.cxsr_allowed = false;
  4373. /*
  4374. * Vblank time updates from the shadow to live plane control register
  4375. * are blocked if the memory self-refresh mode is active at that
  4376. * moment. So to make sure the plane gets truly disabled, disable
  4377. * first the self-refresh mode. The self-refresh enable bit in turn
  4378. * will be checked/applied by the HW only at the next frame start
  4379. * event which is after the vblank start event, so we need to have a
  4380. * wait-for-vblank between disabling the plane and the pipe.
  4381. */
  4382. if (old_crtc_state->base.active) {
  4383. intel_set_memory_cxsr(dev_priv, false);
  4384. dev_priv->wm.vlv.cxsr = false;
  4385. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4386. }
  4387. }
  4388. /*
  4389. * IVB workaround: must disable low power watermarks for at least
  4390. * one frame before enabling scaling. LP watermarks can be re-enabled
  4391. * when scaling is disabled.
  4392. *
  4393. * WaCxSRDisabledForSpriteScaling:ivb
  4394. */
  4395. if (pipe_config->disable_lp_wm) {
  4396. ilk_disable_lp_wm(dev);
  4397. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4398. }
  4399. /*
  4400. * If we're doing a modeset, we're done. No need to do any pre-vblank
  4401. * watermark programming here.
  4402. */
  4403. if (needs_modeset(&pipe_config->base))
  4404. return;
  4405. /*
  4406. * For platforms that support atomic watermarks, program the
  4407. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  4408. * will be the intermediate values that are safe for both pre- and
  4409. * post- vblank; when vblank happens, the 'active' values will be set
  4410. * to the final 'target' values and we'll do this again to get the
  4411. * optimal watermarks. For gen9+ platforms, the values we program here
  4412. * will be the final target values which will get automatically latched
  4413. * at vblank time; no further programming will be necessary.
  4414. *
  4415. * If a platform hasn't been transitioned to atomic watermarks yet,
  4416. * we'll continue to update watermarks the old way, if flags tell
  4417. * us to.
  4418. */
  4419. if (dev_priv->display.initial_watermarks != NULL)
  4420. dev_priv->display.initial_watermarks(pipe_config);
  4421. else if (pipe_config->update_wm_pre)
  4422. intel_update_watermarks(crtc);
  4423. }
  4424. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4425. {
  4426. struct drm_device *dev = crtc->dev;
  4427. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4428. struct drm_plane *p;
  4429. int pipe = intel_crtc->pipe;
  4430. intel_crtc_dpms_overlay_disable(intel_crtc);
  4431. drm_for_each_plane_mask(p, dev, plane_mask)
  4432. to_intel_plane(p)->disable_plane(p, crtc);
  4433. /*
  4434. * FIXME: Once we grow proper nuclear flip support out of this we need
  4435. * to compute the mask of flip planes precisely. For the time being
  4436. * consider this a flip to a NULL plane.
  4437. */
  4438. intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4439. }
  4440. static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
  4441. struct intel_crtc_state *crtc_state,
  4442. struct drm_atomic_state *old_state)
  4443. {
  4444. struct drm_connector_state *old_conn_state;
  4445. struct drm_connector *conn;
  4446. int i;
  4447. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4448. struct drm_connector_state *conn_state = conn->state;
  4449. struct intel_encoder *encoder =
  4450. to_intel_encoder(conn_state->best_encoder);
  4451. if (conn_state->crtc != crtc)
  4452. continue;
  4453. if (encoder->pre_pll_enable)
  4454. encoder->pre_pll_enable(encoder, crtc_state, conn_state);
  4455. }
  4456. }
  4457. static void intel_encoders_pre_enable(struct drm_crtc *crtc,
  4458. struct intel_crtc_state *crtc_state,
  4459. struct drm_atomic_state *old_state)
  4460. {
  4461. struct drm_connector_state *old_conn_state;
  4462. struct drm_connector *conn;
  4463. int i;
  4464. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4465. struct drm_connector_state *conn_state = conn->state;
  4466. struct intel_encoder *encoder =
  4467. to_intel_encoder(conn_state->best_encoder);
  4468. if (conn_state->crtc != crtc)
  4469. continue;
  4470. if (encoder->pre_enable)
  4471. encoder->pre_enable(encoder, crtc_state, conn_state);
  4472. }
  4473. }
  4474. static void intel_encoders_enable(struct drm_crtc *crtc,
  4475. struct intel_crtc_state *crtc_state,
  4476. struct drm_atomic_state *old_state)
  4477. {
  4478. struct drm_connector_state *old_conn_state;
  4479. struct drm_connector *conn;
  4480. int i;
  4481. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4482. struct drm_connector_state *conn_state = conn->state;
  4483. struct intel_encoder *encoder =
  4484. to_intel_encoder(conn_state->best_encoder);
  4485. if (conn_state->crtc != crtc)
  4486. continue;
  4487. encoder->enable(encoder, crtc_state, conn_state);
  4488. intel_opregion_notify_encoder(encoder, true);
  4489. }
  4490. }
  4491. static void intel_encoders_disable(struct drm_crtc *crtc,
  4492. struct intel_crtc_state *old_crtc_state,
  4493. struct drm_atomic_state *old_state)
  4494. {
  4495. struct drm_connector_state *old_conn_state;
  4496. struct drm_connector *conn;
  4497. int i;
  4498. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4499. struct intel_encoder *encoder =
  4500. to_intel_encoder(old_conn_state->best_encoder);
  4501. if (old_conn_state->crtc != crtc)
  4502. continue;
  4503. intel_opregion_notify_encoder(encoder, false);
  4504. encoder->disable(encoder, old_crtc_state, old_conn_state);
  4505. }
  4506. }
  4507. static void intel_encoders_post_disable(struct drm_crtc *crtc,
  4508. struct intel_crtc_state *old_crtc_state,
  4509. struct drm_atomic_state *old_state)
  4510. {
  4511. struct drm_connector_state *old_conn_state;
  4512. struct drm_connector *conn;
  4513. int i;
  4514. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4515. struct intel_encoder *encoder =
  4516. to_intel_encoder(old_conn_state->best_encoder);
  4517. if (old_conn_state->crtc != crtc)
  4518. continue;
  4519. if (encoder->post_disable)
  4520. encoder->post_disable(encoder, old_crtc_state, old_conn_state);
  4521. }
  4522. }
  4523. static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
  4524. struct intel_crtc_state *old_crtc_state,
  4525. struct drm_atomic_state *old_state)
  4526. {
  4527. struct drm_connector_state *old_conn_state;
  4528. struct drm_connector *conn;
  4529. int i;
  4530. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4531. struct intel_encoder *encoder =
  4532. to_intel_encoder(old_conn_state->best_encoder);
  4533. if (old_conn_state->crtc != crtc)
  4534. continue;
  4535. if (encoder->post_pll_disable)
  4536. encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
  4537. }
  4538. }
  4539. static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
  4540. struct drm_atomic_state *old_state)
  4541. {
  4542. struct drm_crtc *crtc = pipe_config->base.crtc;
  4543. struct drm_device *dev = crtc->dev;
  4544. struct drm_i915_private *dev_priv = to_i915(dev);
  4545. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4546. int pipe = intel_crtc->pipe;
  4547. if (WARN_ON(intel_crtc->active))
  4548. return;
  4549. /*
  4550. * Sometimes spurious CPU pipe underruns happen during FDI
  4551. * training, at least with VGA+HDMI cloning. Suppress them.
  4552. *
  4553. * On ILK we get an occasional spurious CPU pipe underruns
  4554. * between eDP port A enable and vdd enable. Also PCH port
  4555. * enable seems to result in the occasional CPU pipe underrun.
  4556. *
  4557. * Spurious PCH underruns also occur during PCH enabling.
  4558. */
  4559. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4560. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4561. if (intel_crtc->config->has_pch_encoder)
  4562. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4563. if (intel_crtc->config->has_pch_encoder)
  4564. intel_prepare_shared_dpll(intel_crtc);
  4565. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4566. intel_dp_set_m_n(intel_crtc, M1_N1);
  4567. intel_set_pipe_timings(intel_crtc);
  4568. intel_set_pipe_src_size(intel_crtc);
  4569. if (intel_crtc->config->has_pch_encoder) {
  4570. intel_cpu_transcoder_set_m_n(intel_crtc,
  4571. &intel_crtc->config->fdi_m_n, NULL);
  4572. }
  4573. ironlake_set_pipeconf(crtc);
  4574. intel_crtc->active = true;
  4575. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4576. if (intel_crtc->config->has_pch_encoder) {
  4577. /* Note: FDI PLL enabling _must_ be done before we enable the
  4578. * cpu pipes, hence this is separate from all the other fdi/pch
  4579. * enabling. */
  4580. ironlake_fdi_pll_enable(intel_crtc);
  4581. } else {
  4582. assert_fdi_tx_disabled(dev_priv, pipe);
  4583. assert_fdi_rx_disabled(dev_priv, pipe);
  4584. }
  4585. ironlake_pfit_enable(intel_crtc);
  4586. /*
  4587. * On ILK+ LUT must be loaded before the pipe is running but with
  4588. * clocks enabled
  4589. */
  4590. intel_color_load_luts(&pipe_config->base);
  4591. if (dev_priv->display.initial_watermarks != NULL)
  4592. dev_priv->display.initial_watermarks(intel_crtc->config);
  4593. intel_enable_pipe(intel_crtc);
  4594. if (intel_crtc->config->has_pch_encoder)
  4595. ironlake_pch_enable(crtc);
  4596. assert_vblank_disabled(crtc);
  4597. drm_crtc_vblank_on(crtc);
  4598. intel_encoders_enable(crtc, pipe_config, old_state);
  4599. if (HAS_PCH_CPT(dev_priv))
  4600. cpt_verify_modeset(dev, intel_crtc->pipe);
  4601. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4602. if (intel_crtc->config->has_pch_encoder)
  4603. intel_wait_for_vblank(dev_priv, pipe);
  4604. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4605. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4606. }
  4607. /* IPS only exists on ULT machines and is tied to pipe A. */
  4608. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4609. {
  4610. return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
  4611. }
  4612. static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
  4613. struct drm_atomic_state *old_state)
  4614. {
  4615. struct drm_crtc *crtc = pipe_config->base.crtc;
  4616. struct drm_device *dev = crtc->dev;
  4617. struct drm_i915_private *dev_priv = to_i915(dev);
  4618. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4619. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4620. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4621. if (WARN_ON(intel_crtc->active))
  4622. return;
  4623. if (intel_crtc->config->has_pch_encoder)
  4624. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4625. false);
  4626. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4627. if (intel_crtc->config->shared_dpll)
  4628. intel_enable_shared_dpll(intel_crtc);
  4629. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4630. intel_dp_set_m_n(intel_crtc, M1_N1);
  4631. if (!transcoder_is_dsi(cpu_transcoder))
  4632. intel_set_pipe_timings(intel_crtc);
  4633. intel_set_pipe_src_size(intel_crtc);
  4634. if (cpu_transcoder != TRANSCODER_EDP &&
  4635. !transcoder_is_dsi(cpu_transcoder)) {
  4636. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4637. intel_crtc->config->pixel_multiplier - 1);
  4638. }
  4639. if (intel_crtc->config->has_pch_encoder) {
  4640. intel_cpu_transcoder_set_m_n(intel_crtc,
  4641. &intel_crtc->config->fdi_m_n, NULL);
  4642. }
  4643. if (!transcoder_is_dsi(cpu_transcoder))
  4644. haswell_set_pipeconf(crtc);
  4645. haswell_set_pipemisc(crtc);
  4646. intel_color_set_csc(&pipe_config->base);
  4647. intel_crtc->active = true;
  4648. if (intel_crtc->config->has_pch_encoder)
  4649. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4650. else
  4651. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4652. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4653. if (intel_crtc->config->has_pch_encoder)
  4654. dev_priv->display.fdi_link_train(crtc);
  4655. if (!transcoder_is_dsi(cpu_transcoder))
  4656. intel_ddi_enable_pipe_clock(intel_crtc);
  4657. if (INTEL_INFO(dev)->gen >= 9)
  4658. skylake_pfit_enable(intel_crtc);
  4659. else
  4660. ironlake_pfit_enable(intel_crtc);
  4661. /*
  4662. * On ILK+ LUT must be loaded before the pipe is running but with
  4663. * clocks enabled
  4664. */
  4665. intel_color_load_luts(&pipe_config->base);
  4666. intel_ddi_set_pipe_settings(crtc);
  4667. if (!transcoder_is_dsi(cpu_transcoder))
  4668. intel_ddi_enable_transcoder_func(crtc);
  4669. if (dev_priv->display.initial_watermarks != NULL)
  4670. dev_priv->display.initial_watermarks(pipe_config);
  4671. else
  4672. intel_update_watermarks(intel_crtc);
  4673. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4674. if (!transcoder_is_dsi(cpu_transcoder))
  4675. intel_enable_pipe(intel_crtc);
  4676. if (intel_crtc->config->has_pch_encoder)
  4677. lpt_pch_enable(crtc);
  4678. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4679. intel_ddi_set_vc_payload_alloc(crtc, true);
  4680. assert_vblank_disabled(crtc);
  4681. drm_crtc_vblank_on(crtc);
  4682. intel_encoders_enable(crtc, pipe_config, old_state);
  4683. if (intel_crtc->config->has_pch_encoder) {
  4684. intel_wait_for_vblank(dev_priv, pipe);
  4685. intel_wait_for_vblank(dev_priv, pipe);
  4686. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4687. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4688. true);
  4689. }
  4690. /* If we change the relative order between pipe/planes enabling, we need
  4691. * to change the workaround. */
  4692. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4693. if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
  4694. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4695. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4696. }
  4697. }
  4698. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4699. {
  4700. struct drm_device *dev = crtc->base.dev;
  4701. struct drm_i915_private *dev_priv = to_i915(dev);
  4702. int pipe = crtc->pipe;
  4703. /* To avoid upsetting the power well on haswell only disable the pfit if
  4704. * it's in use. The hw state code will make sure we get this right. */
  4705. if (force || crtc->config->pch_pfit.enabled) {
  4706. I915_WRITE(PF_CTL(pipe), 0);
  4707. I915_WRITE(PF_WIN_POS(pipe), 0);
  4708. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4709. }
  4710. }
  4711. static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4712. struct drm_atomic_state *old_state)
  4713. {
  4714. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4715. struct drm_device *dev = crtc->dev;
  4716. struct drm_i915_private *dev_priv = to_i915(dev);
  4717. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4718. int pipe = intel_crtc->pipe;
  4719. /*
  4720. * Sometimes spurious CPU pipe underruns happen when the
  4721. * pipe is already disabled, but FDI RX/TX is still enabled.
  4722. * Happens at least with VGA+HDMI cloning. Suppress them.
  4723. */
  4724. if (intel_crtc->config->has_pch_encoder) {
  4725. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4726. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4727. }
  4728. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4729. drm_crtc_vblank_off(crtc);
  4730. assert_vblank_disabled(crtc);
  4731. intel_disable_pipe(intel_crtc);
  4732. ironlake_pfit_disable(intel_crtc, false);
  4733. if (intel_crtc->config->has_pch_encoder)
  4734. ironlake_fdi_disable(crtc);
  4735. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4736. if (intel_crtc->config->has_pch_encoder) {
  4737. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4738. if (HAS_PCH_CPT(dev_priv)) {
  4739. i915_reg_t reg;
  4740. u32 temp;
  4741. /* disable TRANS_DP_CTL */
  4742. reg = TRANS_DP_CTL(pipe);
  4743. temp = I915_READ(reg);
  4744. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4745. TRANS_DP_PORT_SEL_MASK);
  4746. temp |= TRANS_DP_PORT_SEL_NONE;
  4747. I915_WRITE(reg, temp);
  4748. /* disable DPLL_SEL */
  4749. temp = I915_READ(PCH_DPLL_SEL);
  4750. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4751. I915_WRITE(PCH_DPLL_SEL, temp);
  4752. }
  4753. ironlake_fdi_pll_disable(intel_crtc);
  4754. }
  4755. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4756. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4757. }
  4758. static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4759. struct drm_atomic_state *old_state)
  4760. {
  4761. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4762. struct drm_device *dev = crtc->dev;
  4763. struct drm_i915_private *dev_priv = to_i915(dev);
  4764. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4765. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4766. if (intel_crtc->config->has_pch_encoder)
  4767. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4768. false);
  4769. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4770. drm_crtc_vblank_off(crtc);
  4771. assert_vblank_disabled(crtc);
  4772. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4773. if (!transcoder_is_dsi(cpu_transcoder))
  4774. intel_disable_pipe(intel_crtc);
  4775. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4776. intel_ddi_set_vc_payload_alloc(crtc, false);
  4777. if (!transcoder_is_dsi(cpu_transcoder))
  4778. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4779. if (INTEL_INFO(dev)->gen >= 9)
  4780. skylake_scaler_disable(intel_crtc);
  4781. else
  4782. ironlake_pfit_disable(intel_crtc, false);
  4783. if (!transcoder_is_dsi(cpu_transcoder))
  4784. intel_ddi_disable_pipe_clock(intel_crtc);
  4785. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4786. if (old_crtc_state->has_pch_encoder)
  4787. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4788. true);
  4789. }
  4790. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4791. {
  4792. struct drm_device *dev = crtc->base.dev;
  4793. struct drm_i915_private *dev_priv = to_i915(dev);
  4794. struct intel_crtc_state *pipe_config = crtc->config;
  4795. if (!pipe_config->gmch_pfit.control)
  4796. return;
  4797. /*
  4798. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4799. * according to register description and PRM.
  4800. */
  4801. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4802. assert_pipe_disabled(dev_priv, crtc->pipe);
  4803. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4804. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4805. /* Border color in case we don't scale up to the full screen. Black by
  4806. * default, change to something else for debugging. */
  4807. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4808. }
  4809. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4810. {
  4811. switch (port) {
  4812. case PORT_A:
  4813. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4814. case PORT_B:
  4815. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4816. case PORT_C:
  4817. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4818. case PORT_D:
  4819. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4820. case PORT_E:
  4821. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4822. default:
  4823. MISSING_CASE(port);
  4824. return POWER_DOMAIN_PORT_OTHER;
  4825. }
  4826. }
  4827. static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
  4828. {
  4829. switch (port) {
  4830. case PORT_A:
  4831. return POWER_DOMAIN_AUX_A;
  4832. case PORT_B:
  4833. return POWER_DOMAIN_AUX_B;
  4834. case PORT_C:
  4835. return POWER_DOMAIN_AUX_C;
  4836. case PORT_D:
  4837. return POWER_DOMAIN_AUX_D;
  4838. case PORT_E:
  4839. /* FIXME: Check VBT for actual wiring of PORT E */
  4840. return POWER_DOMAIN_AUX_D;
  4841. default:
  4842. MISSING_CASE(port);
  4843. return POWER_DOMAIN_AUX_A;
  4844. }
  4845. }
  4846. enum intel_display_power_domain
  4847. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4848. {
  4849. struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
  4850. struct intel_digital_port *intel_dig_port;
  4851. switch (intel_encoder->type) {
  4852. case INTEL_OUTPUT_UNKNOWN:
  4853. /* Only DDI platforms should ever use this output type */
  4854. WARN_ON_ONCE(!HAS_DDI(dev_priv));
  4855. case INTEL_OUTPUT_DP:
  4856. case INTEL_OUTPUT_HDMI:
  4857. case INTEL_OUTPUT_EDP:
  4858. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4859. return port_to_power_domain(intel_dig_port->port);
  4860. case INTEL_OUTPUT_DP_MST:
  4861. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4862. return port_to_power_domain(intel_dig_port->port);
  4863. case INTEL_OUTPUT_ANALOG:
  4864. return POWER_DOMAIN_PORT_CRT;
  4865. case INTEL_OUTPUT_DSI:
  4866. return POWER_DOMAIN_PORT_DSI;
  4867. default:
  4868. return POWER_DOMAIN_PORT_OTHER;
  4869. }
  4870. }
  4871. enum intel_display_power_domain
  4872. intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
  4873. {
  4874. struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
  4875. struct intel_digital_port *intel_dig_port;
  4876. switch (intel_encoder->type) {
  4877. case INTEL_OUTPUT_UNKNOWN:
  4878. case INTEL_OUTPUT_HDMI:
  4879. /*
  4880. * Only DDI platforms should ever use these output types.
  4881. * We can get here after the HDMI detect code has already set
  4882. * the type of the shared encoder. Since we can't be sure
  4883. * what's the status of the given connectors, play safe and
  4884. * run the DP detection too.
  4885. */
  4886. WARN_ON_ONCE(!HAS_DDI(dev_priv));
  4887. case INTEL_OUTPUT_DP:
  4888. case INTEL_OUTPUT_EDP:
  4889. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4890. return port_to_aux_power_domain(intel_dig_port->port);
  4891. case INTEL_OUTPUT_DP_MST:
  4892. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4893. return port_to_aux_power_domain(intel_dig_port->port);
  4894. default:
  4895. MISSING_CASE(intel_encoder->type);
  4896. return POWER_DOMAIN_AUX_A;
  4897. }
  4898. }
  4899. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
  4900. struct intel_crtc_state *crtc_state)
  4901. {
  4902. struct drm_device *dev = crtc->dev;
  4903. struct drm_encoder *encoder;
  4904. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4905. enum pipe pipe = intel_crtc->pipe;
  4906. unsigned long mask;
  4907. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4908. if (!crtc_state->base.active)
  4909. return 0;
  4910. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4911. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4912. if (crtc_state->pch_pfit.enabled ||
  4913. crtc_state->pch_pfit.force_thru)
  4914. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4915. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4916. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4917. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4918. }
  4919. if (crtc_state->shared_dpll)
  4920. mask |= BIT(POWER_DOMAIN_PLLS);
  4921. return mask;
  4922. }
  4923. static unsigned long
  4924. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4925. struct intel_crtc_state *crtc_state)
  4926. {
  4927. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4928. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4929. enum intel_display_power_domain domain;
  4930. unsigned long domains, new_domains, old_domains;
  4931. old_domains = intel_crtc->enabled_power_domains;
  4932. intel_crtc->enabled_power_domains = new_domains =
  4933. get_crtc_power_domains(crtc, crtc_state);
  4934. domains = new_domains & ~old_domains;
  4935. for_each_power_domain(domain, domains)
  4936. intel_display_power_get(dev_priv, domain);
  4937. return old_domains & ~new_domains;
  4938. }
  4939. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4940. unsigned long domains)
  4941. {
  4942. enum intel_display_power_domain domain;
  4943. for_each_power_domain(domain, domains)
  4944. intel_display_power_put(dev_priv, domain);
  4945. }
  4946. static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
  4947. {
  4948. int max_cdclk_freq = dev_priv->max_cdclk_freq;
  4949. if (INTEL_INFO(dev_priv)->gen >= 9 ||
  4950. IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  4951. return max_cdclk_freq;
  4952. else if (IS_CHERRYVIEW(dev_priv))
  4953. return max_cdclk_freq*95/100;
  4954. else if (INTEL_INFO(dev_priv)->gen < 4)
  4955. return 2*max_cdclk_freq*90/100;
  4956. else
  4957. return max_cdclk_freq*90/100;
  4958. }
  4959. static int skl_calc_cdclk(int max_pixclk, int vco);
  4960. static void intel_update_max_cdclk(struct drm_i915_private *dev_priv)
  4961. {
  4962. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
  4963. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4964. int max_cdclk, vco;
  4965. vco = dev_priv->skl_preferred_vco_freq;
  4966. WARN_ON(vco != 8100000 && vco != 8640000);
  4967. /*
  4968. * Use the lower (vco 8640) cdclk values as a
  4969. * first guess. skl_calc_cdclk() will correct it
  4970. * if the preferred vco is 8100 instead.
  4971. */
  4972. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4973. max_cdclk = 617143;
  4974. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4975. max_cdclk = 540000;
  4976. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4977. max_cdclk = 432000;
  4978. else
  4979. max_cdclk = 308571;
  4980. dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
  4981. } else if (IS_BROXTON(dev_priv)) {
  4982. dev_priv->max_cdclk_freq = 624000;
  4983. } else if (IS_BROADWELL(dev_priv)) {
  4984. /*
  4985. * FIXME with extra cooling we can allow
  4986. * 540 MHz for ULX and 675 Mhz for ULT.
  4987. * How can we know if extra cooling is
  4988. * available? PCI ID, VTB, something else?
  4989. */
  4990. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4991. dev_priv->max_cdclk_freq = 450000;
  4992. else if (IS_BDW_ULX(dev_priv))
  4993. dev_priv->max_cdclk_freq = 450000;
  4994. else if (IS_BDW_ULT(dev_priv))
  4995. dev_priv->max_cdclk_freq = 540000;
  4996. else
  4997. dev_priv->max_cdclk_freq = 675000;
  4998. } else if (IS_CHERRYVIEW(dev_priv)) {
  4999. dev_priv->max_cdclk_freq = 320000;
  5000. } else if (IS_VALLEYVIEW(dev_priv)) {
  5001. dev_priv->max_cdclk_freq = 400000;
  5002. } else {
  5003. /* otherwise assume cdclk is fixed */
  5004. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  5005. }
  5006. dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
  5007. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  5008. dev_priv->max_cdclk_freq);
  5009. DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
  5010. dev_priv->max_dotclk_freq);
  5011. }
  5012. static void intel_update_cdclk(struct drm_i915_private *dev_priv)
  5013. {
  5014. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev_priv);
  5015. if (INTEL_GEN(dev_priv) >= 9)
  5016. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
  5017. dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
  5018. dev_priv->cdclk_pll.ref);
  5019. else
  5020. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  5021. dev_priv->cdclk_freq);
  5022. /*
  5023. * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
  5024. * Programmng [sic] note: bit[9:2] should be programmed to the number
  5025. * of cdclk that generates 4MHz reference clock freq which is used to
  5026. * generate GMBus clock. This will vary with the cdclk freq.
  5027. */
  5028. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  5029. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  5030. }
  5031. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  5032. static int skl_cdclk_decimal(int cdclk)
  5033. {
  5034. return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
  5035. }
  5036. static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
  5037. {
  5038. int ratio;
  5039. if (cdclk == dev_priv->cdclk_pll.ref)
  5040. return 0;
  5041. switch (cdclk) {
  5042. default:
  5043. MISSING_CASE(cdclk);
  5044. case 144000:
  5045. case 288000:
  5046. case 384000:
  5047. case 576000:
  5048. ratio = 60;
  5049. break;
  5050. case 624000:
  5051. ratio = 65;
  5052. break;
  5053. }
  5054. return dev_priv->cdclk_pll.ref * ratio;
  5055. }
  5056. static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
  5057. {
  5058. I915_WRITE(BXT_DE_PLL_ENABLE, 0);
  5059. /* Timeout 200us */
  5060. if (intel_wait_for_register(dev_priv,
  5061. BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
  5062. 1))
  5063. DRM_ERROR("timeout waiting for DE PLL unlock\n");
  5064. dev_priv->cdclk_pll.vco = 0;
  5065. }
  5066. static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
  5067. {
  5068. int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
  5069. u32 val;
  5070. val = I915_READ(BXT_DE_PLL_CTL);
  5071. val &= ~BXT_DE_PLL_RATIO_MASK;
  5072. val |= BXT_DE_PLL_RATIO(ratio);
  5073. I915_WRITE(BXT_DE_PLL_CTL, val);
  5074. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  5075. /* Timeout 200us */
  5076. if (intel_wait_for_register(dev_priv,
  5077. BXT_DE_PLL_ENABLE,
  5078. BXT_DE_PLL_LOCK,
  5079. BXT_DE_PLL_LOCK,
  5080. 1))
  5081. DRM_ERROR("timeout waiting for DE PLL lock\n");
  5082. dev_priv->cdclk_pll.vco = vco;
  5083. }
  5084. static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
  5085. {
  5086. u32 val, divider;
  5087. int vco, ret;
  5088. vco = bxt_de_pll_vco(dev_priv, cdclk);
  5089. DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
  5090. /* cdclk = vco / 2 / div{1,1.5,2,4} */
  5091. switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
  5092. case 8:
  5093. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  5094. break;
  5095. case 4:
  5096. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  5097. break;
  5098. case 3:
  5099. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  5100. break;
  5101. case 2:
  5102. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  5103. break;
  5104. default:
  5105. WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
  5106. WARN_ON(vco != 0);
  5107. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  5108. break;
  5109. }
  5110. /* Inform power controller of upcoming frequency change */
  5111. mutex_lock(&dev_priv->rps.hw_lock);
  5112. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  5113. 0x80000000);
  5114. mutex_unlock(&dev_priv->rps.hw_lock);
  5115. if (ret) {
  5116. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  5117. ret, cdclk);
  5118. return;
  5119. }
  5120. if (dev_priv->cdclk_pll.vco != 0 &&
  5121. dev_priv->cdclk_pll.vco != vco)
  5122. bxt_de_pll_disable(dev_priv);
  5123. if (dev_priv->cdclk_pll.vco != vco)
  5124. bxt_de_pll_enable(dev_priv, vco);
  5125. val = divider | skl_cdclk_decimal(cdclk);
  5126. /*
  5127. * FIXME if only the cd2x divider needs changing, it could be done
  5128. * without shutting off the pipe (if only one pipe is active).
  5129. */
  5130. val |= BXT_CDCLK_CD2X_PIPE_NONE;
  5131. /*
  5132. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  5133. * enable otherwise.
  5134. */
  5135. if (cdclk >= 500000)
  5136. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  5137. I915_WRITE(CDCLK_CTL, val);
  5138. mutex_lock(&dev_priv->rps.hw_lock);
  5139. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  5140. DIV_ROUND_UP(cdclk, 25000));
  5141. mutex_unlock(&dev_priv->rps.hw_lock);
  5142. if (ret) {
  5143. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  5144. ret, cdclk);
  5145. return;
  5146. }
  5147. intel_update_cdclk(dev_priv);
  5148. }
  5149. static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
  5150. {
  5151. u32 cdctl, expected;
  5152. intel_update_cdclk(dev_priv);
  5153. if (dev_priv->cdclk_pll.vco == 0 ||
  5154. dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
  5155. goto sanitize;
  5156. /* DPLL okay; verify the cdclock
  5157. *
  5158. * Some BIOS versions leave an incorrect decimal frequency value and
  5159. * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
  5160. * so sanitize this register.
  5161. */
  5162. cdctl = I915_READ(CDCLK_CTL);
  5163. /*
  5164. * Let's ignore the pipe field, since BIOS could have configured the
  5165. * dividers both synching to an active pipe, or asynchronously
  5166. * (PIPE_NONE).
  5167. */
  5168. cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
  5169. expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
  5170. skl_cdclk_decimal(dev_priv->cdclk_freq);
  5171. /*
  5172. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  5173. * enable otherwise.
  5174. */
  5175. if (dev_priv->cdclk_freq >= 500000)
  5176. expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  5177. if (cdctl == expected)
  5178. /* All well; nothing to sanitize */
  5179. return;
  5180. sanitize:
  5181. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  5182. /* force cdclk programming */
  5183. dev_priv->cdclk_freq = 0;
  5184. /* force full PLL disable + enable */
  5185. dev_priv->cdclk_pll.vco = -1;
  5186. }
  5187. void bxt_init_cdclk(struct drm_i915_private *dev_priv)
  5188. {
  5189. bxt_sanitize_cdclk(dev_priv);
  5190. if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
  5191. return;
  5192. /*
  5193. * FIXME:
  5194. * - The initial CDCLK needs to be read from VBT.
  5195. * Need to make this change after VBT has changes for BXT.
  5196. */
  5197. bxt_set_cdclk(dev_priv, bxt_calc_cdclk(0));
  5198. }
  5199. void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
  5200. {
  5201. bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
  5202. }
  5203. static int skl_calc_cdclk(int max_pixclk, int vco)
  5204. {
  5205. if (vco == 8640000) {
  5206. if (max_pixclk > 540000)
  5207. return 617143;
  5208. else if (max_pixclk > 432000)
  5209. return 540000;
  5210. else if (max_pixclk > 308571)
  5211. return 432000;
  5212. else
  5213. return 308571;
  5214. } else {
  5215. if (max_pixclk > 540000)
  5216. return 675000;
  5217. else if (max_pixclk > 450000)
  5218. return 540000;
  5219. else if (max_pixclk > 337500)
  5220. return 450000;
  5221. else
  5222. return 337500;
  5223. }
  5224. }
  5225. static void
  5226. skl_dpll0_update(struct drm_i915_private *dev_priv)
  5227. {
  5228. u32 val;
  5229. dev_priv->cdclk_pll.ref = 24000;
  5230. dev_priv->cdclk_pll.vco = 0;
  5231. val = I915_READ(LCPLL1_CTL);
  5232. if ((val & LCPLL_PLL_ENABLE) == 0)
  5233. return;
  5234. if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
  5235. return;
  5236. val = I915_READ(DPLL_CTRL1);
  5237. if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
  5238. DPLL_CTRL1_SSC(SKL_DPLL0) |
  5239. DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
  5240. DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
  5241. return;
  5242. switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
  5243. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
  5244. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
  5245. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
  5246. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
  5247. dev_priv->cdclk_pll.vco = 8100000;
  5248. break;
  5249. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
  5250. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
  5251. dev_priv->cdclk_pll.vco = 8640000;
  5252. break;
  5253. default:
  5254. MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  5255. break;
  5256. }
  5257. }
  5258. void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
  5259. {
  5260. bool changed = dev_priv->skl_preferred_vco_freq != vco;
  5261. dev_priv->skl_preferred_vco_freq = vco;
  5262. if (changed)
  5263. intel_update_max_cdclk(dev_priv);
  5264. }
  5265. static void
  5266. skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
  5267. {
  5268. int min_cdclk = skl_calc_cdclk(0, vco);
  5269. u32 val;
  5270. WARN_ON(vco != 8100000 && vco != 8640000);
  5271. /* select the minimum CDCLK before enabling DPLL 0 */
  5272. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
  5273. I915_WRITE(CDCLK_CTL, val);
  5274. POSTING_READ(CDCLK_CTL);
  5275. /*
  5276. * We always enable DPLL0 with the lowest link rate possible, but still
  5277. * taking into account the VCO required to operate the eDP panel at the
  5278. * desired frequency. The usual DP link rates operate with a VCO of
  5279. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  5280. * The modeset code is responsible for the selection of the exact link
  5281. * rate later on, with the constraint of choosing a frequency that
  5282. * works with vco.
  5283. */
  5284. val = I915_READ(DPLL_CTRL1);
  5285. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  5286. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  5287. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  5288. if (vco == 8640000)
  5289. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  5290. SKL_DPLL0);
  5291. else
  5292. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  5293. SKL_DPLL0);
  5294. I915_WRITE(DPLL_CTRL1, val);
  5295. POSTING_READ(DPLL_CTRL1);
  5296. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  5297. if (intel_wait_for_register(dev_priv,
  5298. LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  5299. 5))
  5300. DRM_ERROR("DPLL0 not locked\n");
  5301. dev_priv->cdclk_pll.vco = vco;
  5302. /* We'll want to keep using the current vco from now on. */
  5303. skl_set_preferred_cdclk_vco(dev_priv, vco);
  5304. }
  5305. static void
  5306. skl_dpll0_disable(struct drm_i915_private *dev_priv)
  5307. {
  5308. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  5309. if (intel_wait_for_register(dev_priv,
  5310. LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
  5311. 1))
  5312. DRM_ERROR("Couldn't disable DPLL0\n");
  5313. dev_priv->cdclk_pll.vco = 0;
  5314. }
  5315. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  5316. {
  5317. int ret;
  5318. u32 val;
  5319. /* inform PCU we want to change CDCLK */
  5320. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  5321. mutex_lock(&dev_priv->rps.hw_lock);
  5322. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  5323. mutex_unlock(&dev_priv->rps.hw_lock);
  5324. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  5325. }
  5326. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  5327. {
  5328. return _wait_for(skl_cdclk_pcu_ready(dev_priv), 3000, 10) == 0;
  5329. }
  5330. static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
  5331. {
  5332. u32 freq_select, pcu_ack;
  5333. WARN_ON((cdclk == 24000) != (vco == 0));
  5334. DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
  5335. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  5336. DRM_ERROR("failed to inform PCU about cdclk change\n");
  5337. return;
  5338. }
  5339. /* set CDCLK_CTL */
  5340. switch (cdclk) {
  5341. case 450000:
  5342. case 432000:
  5343. freq_select = CDCLK_FREQ_450_432;
  5344. pcu_ack = 1;
  5345. break;
  5346. case 540000:
  5347. freq_select = CDCLK_FREQ_540;
  5348. pcu_ack = 2;
  5349. break;
  5350. case 308571:
  5351. case 337500:
  5352. default:
  5353. freq_select = CDCLK_FREQ_337_308;
  5354. pcu_ack = 0;
  5355. break;
  5356. case 617143:
  5357. case 675000:
  5358. freq_select = CDCLK_FREQ_675_617;
  5359. pcu_ack = 3;
  5360. break;
  5361. }
  5362. if (dev_priv->cdclk_pll.vco != 0 &&
  5363. dev_priv->cdclk_pll.vco != vco)
  5364. skl_dpll0_disable(dev_priv);
  5365. if (dev_priv->cdclk_pll.vco != vco)
  5366. skl_dpll0_enable(dev_priv, vco);
  5367. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
  5368. POSTING_READ(CDCLK_CTL);
  5369. /* inform PCU of the change */
  5370. mutex_lock(&dev_priv->rps.hw_lock);
  5371. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  5372. mutex_unlock(&dev_priv->rps.hw_lock);
  5373. intel_update_cdclk(dev_priv);
  5374. }
  5375. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
  5376. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  5377. {
  5378. skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
  5379. }
  5380. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  5381. {
  5382. int cdclk, vco;
  5383. skl_sanitize_cdclk(dev_priv);
  5384. if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
  5385. /*
  5386. * Use the current vco as our initial
  5387. * guess as to what the preferred vco is.
  5388. */
  5389. if (dev_priv->skl_preferred_vco_freq == 0)
  5390. skl_set_preferred_cdclk_vco(dev_priv,
  5391. dev_priv->cdclk_pll.vco);
  5392. return;
  5393. }
  5394. vco = dev_priv->skl_preferred_vco_freq;
  5395. if (vco == 0)
  5396. vco = 8100000;
  5397. cdclk = skl_calc_cdclk(0, vco);
  5398. skl_set_cdclk(dev_priv, cdclk, vco);
  5399. }
  5400. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
  5401. {
  5402. uint32_t cdctl, expected;
  5403. /*
  5404. * check if the pre-os intialized the display
  5405. * There is SWF18 scratchpad register defined which is set by the
  5406. * pre-os which can be used by the OS drivers to check the status
  5407. */
  5408. if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
  5409. goto sanitize;
  5410. intel_update_cdclk(dev_priv);
  5411. /* Is PLL enabled and locked ? */
  5412. if (dev_priv->cdclk_pll.vco == 0 ||
  5413. dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
  5414. goto sanitize;
  5415. /* DPLL okay; verify the cdclock
  5416. *
  5417. * Noticed in some instances that the freq selection is correct but
  5418. * decimal part is programmed wrong from BIOS where pre-os does not
  5419. * enable display. Verify the same as well.
  5420. */
  5421. cdctl = I915_READ(CDCLK_CTL);
  5422. expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
  5423. skl_cdclk_decimal(dev_priv->cdclk_freq);
  5424. if (cdctl == expected)
  5425. /* All well; nothing to sanitize */
  5426. return;
  5427. sanitize:
  5428. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  5429. /* force cdclk programming */
  5430. dev_priv->cdclk_freq = 0;
  5431. /* force full PLL disable + enable */
  5432. dev_priv->cdclk_pll.vco = -1;
  5433. }
  5434. /* Adjust CDclk dividers to allow high res or save power if possible */
  5435. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  5436. {
  5437. struct drm_i915_private *dev_priv = to_i915(dev);
  5438. u32 val, cmd;
  5439. WARN_ON(dev_priv->display.get_display_clock_speed(dev_priv)
  5440. != dev_priv->cdclk_freq);
  5441. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  5442. cmd = 2;
  5443. else if (cdclk == 266667)
  5444. cmd = 1;
  5445. else
  5446. cmd = 0;
  5447. mutex_lock(&dev_priv->rps.hw_lock);
  5448. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  5449. val &= ~DSPFREQGUAR_MASK;
  5450. val |= (cmd << DSPFREQGUAR_SHIFT);
  5451. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  5452. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  5453. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  5454. 50)) {
  5455. DRM_ERROR("timed out waiting for CDclk change\n");
  5456. }
  5457. mutex_unlock(&dev_priv->rps.hw_lock);
  5458. mutex_lock(&dev_priv->sb_lock);
  5459. if (cdclk == 400000) {
  5460. u32 divider;
  5461. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  5462. /* adjust cdclk divider */
  5463. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  5464. val &= ~CCK_FREQUENCY_VALUES;
  5465. val |= divider;
  5466. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  5467. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  5468. CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
  5469. 50))
  5470. DRM_ERROR("timed out waiting for CDclk change\n");
  5471. }
  5472. /* adjust self-refresh exit latency value */
  5473. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  5474. val &= ~0x7f;
  5475. /*
  5476. * For high bandwidth configs, we set a higher latency in the bunit
  5477. * so that the core display fetch happens in time to avoid underruns.
  5478. */
  5479. if (cdclk == 400000)
  5480. val |= 4500 / 250; /* 4.5 usec */
  5481. else
  5482. val |= 3000 / 250; /* 3.0 usec */
  5483. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  5484. mutex_unlock(&dev_priv->sb_lock);
  5485. intel_update_cdclk(dev_priv);
  5486. }
  5487. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  5488. {
  5489. struct drm_i915_private *dev_priv = to_i915(dev);
  5490. u32 val, cmd;
  5491. WARN_ON(dev_priv->display.get_display_clock_speed(dev_priv)
  5492. != dev_priv->cdclk_freq);
  5493. switch (cdclk) {
  5494. case 333333:
  5495. case 320000:
  5496. case 266667:
  5497. case 200000:
  5498. break;
  5499. default:
  5500. MISSING_CASE(cdclk);
  5501. return;
  5502. }
  5503. /*
  5504. * Specs are full of misinformation, but testing on actual
  5505. * hardware has shown that we just need to write the desired
  5506. * CCK divider into the Punit register.
  5507. */
  5508. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  5509. mutex_lock(&dev_priv->rps.hw_lock);
  5510. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  5511. val &= ~DSPFREQGUAR_MASK_CHV;
  5512. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  5513. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  5514. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  5515. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  5516. 50)) {
  5517. DRM_ERROR("timed out waiting for CDclk change\n");
  5518. }
  5519. mutex_unlock(&dev_priv->rps.hw_lock);
  5520. intel_update_cdclk(dev_priv);
  5521. }
  5522. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  5523. int max_pixclk)
  5524. {
  5525. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  5526. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  5527. /*
  5528. * Really only a few cases to deal with, as only 4 CDclks are supported:
  5529. * 200MHz
  5530. * 267MHz
  5531. * 320/333MHz (depends on HPLL freq)
  5532. * 400MHz (VLV only)
  5533. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  5534. * of the lower bin and adjust if needed.
  5535. *
  5536. * We seem to get an unstable or solid color picture at 200MHz.
  5537. * Not sure what's wrong. For now use 200MHz only when all pipes
  5538. * are off.
  5539. */
  5540. if (!IS_CHERRYVIEW(dev_priv) &&
  5541. max_pixclk > freq_320*limit/100)
  5542. return 400000;
  5543. else if (max_pixclk > 266667*limit/100)
  5544. return freq_320;
  5545. else if (max_pixclk > 0)
  5546. return 266667;
  5547. else
  5548. return 200000;
  5549. }
  5550. static int bxt_calc_cdclk(int max_pixclk)
  5551. {
  5552. if (max_pixclk > 576000)
  5553. return 624000;
  5554. else if (max_pixclk > 384000)
  5555. return 576000;
  5556. else if (max_pixclk > 288000)
  5557. return 384000;
  5558. else if (max_pixclk > 144000)
  5559. return 288000;
  5560. else
  5561. return 144000;
  5562. }
  5563. /* Compute the max pixel clock for new configuration. */
  5564. static int intel_mode_max_pixclk(struct drm_device *dev,
  5565. struct drm_atomic_state *state)
  5566. {
  5567. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  5568. struct drm_i915_private *dev_priv = to_i915(dev);
  5569. struct drm_crtc *crtc;
  5570. struct drm_crtc_state *crtc_state;
  5571. unsigned max_pixclk = 0, i;
  5572. enum pipe pipe;
  5573. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  5574. sizeof(intel_state->min_pixclk));
  5575. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  5576. int pixclk = 0;
  5577. if (crtc_state->enable)
  5578. pixclk = crtc_state->adjusted_mode.crtc_clock;
  5579. intel_state->min_pixclk[i] = pixclk;
  5580. }
  5581. for_each_pipe(dev_priv, pipe)
  5582. max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
  5583. return max_pixclk;
  5584. }
  5585. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  5586. {
  5587. struct drm_device *dev = state->dev;
  5588. struct drm_i915_private *dev_priv = to_i915(dev);
  5589. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5590. struct intel_atomic_state *intel_state =
  5591. to_intel_atomic_state(state);
  5592. intel_state->cdclk = intel_state->dev_cdclk =
  5593. valleyview_calc_cdclk(dev_priv, max_pixclk);
  5594. if (!intel_state->active_crtcs)
  5595. intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
  5596. return 0;
  5597. }
  5598. static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
  5599. {
  5600. int max_pixclk = ilk_max_pixel_rate(state);
  5601. struct intel_atomic_state *intel_state =
  5602. to_intel_atomic_state(state);
  5603. intel_state->cdclk = intel_state->dev_cdclk =
  5604. bxt_calc_cdclk(max_pixclk);
  5605. if (!intel_state->active_crtcs)
  5606. intel_state->dev_cdclk = bxt_calc_cdclk(0);
  5607. return 0;
  5608. }
  5609. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5610. {
  5611. unsigned int credits, default_credits;
  5612. if (IS_CHERRYVIEW(dev_priv))
  5613. default_credits = PFI_CREDIT(12);
  5614. else
  5615. default_credits = PFI_CREDIT(8);
  5616. if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
  5617. /* CHV suggested value is 31 or 63 */
  5618. if (IS_CHERRYVIEW(dev_priv))
  5619. credits = PFI_CREDIT_63;
  5620. else
  5621. credits = PFI_CREDIT(15);
  5622. } else {
  5623. credits = default_credits;
  5624. }
  5625. /*
  5626. * WA - write default credits before re-programming
  5627. * FIXME: should we also set the resend bit here?
  5628. */
  5629. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5630. default_credits);
  5631. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5632. credits | PFI_CREDIT_RESEND);
  5633. /*
  5634. * FIXME is this guaranteed to clear
  5635. * immediately or should we poll for it?
  5636. */
  5637. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5638. }
  5639. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5640. {
  5641. struct drm_device *dev = old_state->dev;
  5642. struct drm_i915_private *dev_priv = to_i915(dev);
  5643. struct intel_atomic_state *old_intel_state =
  5644. to_intel_atomic_state(old_state);
  5645. unsigned req_cdclk = old_intel_state->dev_cdclk;
  5646. /*
  5647. * FIXME: We can end up here with all power domains off, yet
  5648. * with a CDCLK frequency other than the minimum. To account
  5649. * for this take the PIPE-A power domain, which covers the HW
  5650. * blocks needed for the following programming. This can be
  5651. * removed once it's guaranteed that we get here either with
  5652. * the minimum CDCLK set, or the required power domains
  5653. * enabled.
  5654. */
  5655. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5656. if (IS_CHERRYVIEW(dev_priv))
  5657. cherryview_set_cdclk(dev, req_cdclk);
  5658. else
  5659. valleyview_set_cdclk(dev, req_cdclk);
  5660. vlv_program_pfi_credits(dev_priv);
  5661. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5662. }
  5663. static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
  5664. struct drm_atomic_state *old_state)
  5665. {
  5666. struct drm_crtc *crtc = pipe_config->base.crtc;
  5667. struct drm_device *dev = crtc->dev;
  5668. struct drm_i915_private *dev_priv = to_i915(dev);
  5669. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5670. int pipe = intel_crtc->pipe;
  5671. if (WARN_ON(intel_crtc->active))
  5672. return;
  5673. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5674. intel_dp_set_m_n(intel_crtc, M1_N1);
  5675. intel_set_pipe_timings(intel_crtc);
  5676. intel_set_pipe_src_size(intel_crtc);
  5677. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  5678. struct drm_i915_private *dev_priv = to_i915(dev);
  5679. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5680. I915_WRITE(CHV_CANVAS(pipe), 0);
  5681. }
  5682. i9xx_set_pipeconf(intel_crtc);
  5683. intel_crtc->active = true;
  5684. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5685. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  5686. if (IS_CHERRYVIEW(dev_priv)) {
  5687. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5688. chv_enable_pll(intel_crtc, intel_crtc->config);
  5689. } else {
  5690. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5691. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5692. }
  5693. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  5694. i9xx_pfit_enable(intel_crtc);
  5695. intel_color_load_luts(&pipe_config->base);
  5696. intel_update_watermarks(intel_crtc);
  5697. intel_enable_pipe(intel_crtc);
  5698. assert_vblank_disabled(crtc);
  5699. drm_crtc_vblank_on(crtc);
  5700. intel_encoders_enable(crtc, pipe_config, old_state);
  5701. }
  5702. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5703. {
  5704. struct drm_device *dev = crtc->base.dev;
  5705. struct drm_i915_private *dev_priv = to_i915(dev);
  5706. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5707. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5708. }
  5709. static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
  5710. struct drm_atomic_state *old_state)
  5711. {
  5712. struct drm_crtc *crtc = pipe_config->base.crtc;
  5713. struct drm_device *dev = crtc->dev;
  5714. struct drm_i915_private *dev_priv = to_i915(dev);
  5715. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5716. enum pipe pipe = intel_crtc->pipe;
  5717. if (WARN_ON(intel_crtc->active))
  5718. return;
  5719. i9xx_set_pll_dividers(intel_crtc);
  5720. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5721. intel_dp_set_m_n(intel_crtc, M1_N1);
  5722. intel_set_pipe_timings(intel_crtc);
  5723. intel_set_pipe_src_size(intel_crtc);
  5724. i9xx_set_pipeconf(intel_crtc);
  5725. intel_crtc->active = true;
  5726. if (!IS_GEN2(dev_priv))
  5727. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5728. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  5729. i9xx_enable_pll(intel_crtc);
  5730. i9xx_pfit_enable(intel_crtc);
  5731. intel_color_load_luts(&pipe_config->base);
  5732. intel_update_watermarks(intel_crtc);
  5733. intel_enable_pipe(intel_crtc);
  5734. assert_vblank_disabled(crtc);
  5735. drm_crtc_vblank_on(crtc);
  5736. intel_encoders_enable(crtc, pipe_config, old_state);
  5737. }
  5738. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5739. {
  5740. struct drm_device *dev = crtc->base.dev;
  5741. struct drm_i915_private *dev_priv = to_i915(dev);
  5742. if (!crtc->config->gmch_pfit.control)
  5743. return;
  5744. assert_pipe_disabled(dev_priv, crtc->pipe);
  5745. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5746. I915_READ(PFIT_CONTROL));
  5747. I915_WRITE(PFIT_CONTROL, 0);
  5748. }
  5749. static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
  5750. struct drm_atomic_state *old_state)
  5751. {
  5752. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  5753. struct drm_device *dev = crtc->dev;
  5754. struct drm_i915_private *dev_priv = to_i915(dev);
  5755. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5756. int pipe = intel_crtc->pipe;
  5757. /*
  5758. * On gen2 planes are double buffered but the pipe isn't, so we must
  5759. * wait for planes to fully turn off before disabling the pipe.
  5760. */
  5761. if (IS_GEN2(dev_priv))
  5762. intel_wait_for_vblank(dev_priv, pipe);
  5763. intel_encoders_disable(crtc, old_crtc_state, old_state);
  5764. drm_crtc_vblank_off(crtc);
  5765. assert_vblank_disabled(crtc);
  5766. intel_disable_pipe(intel_crtc);
  5767. i9xx_pfit_disable(intel_crtc);
  5768. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  5769. if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
  5770. if (IS_CHERRYVIEW(dev_priv))
  5771. chv_disable_pll(dev_priv, pipe);
  5772. else if (IS_VALLEYVIEW(dev_priv))
  5773. vlv_disable_pll(dev_priv, pipe);
  5774. else
  5775. i9xx_disable_pll(intel_crtc);
  5776. }
  5777. intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
  5778. if (!IS_GEN2(dev_priv))
  5779. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5780. }
  5781. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5782. {
  5783. struct intel_encoder *encoder;
  5784. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5785. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5786. enum intel_display_power_domain domain;
  5787. unsigned long domains;
  5788. struct drm_atomic_state *state;
  5789. struct intel_crtc_state *crtc_state;
  5790. int ret;
  5791. if (!intel_crtc->active)
  5792. return;
  5793. if (to_intel_plane_state(crtc->primary->state)->base.visible) {
  5794. WARN_ON(intel_crtc->flip_work);
  5795. intel_pre_disable_primary_noatomic(crtc);
  5796. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  5797. to_intel_plane_state(crtc->primary->state)->base.visible = false;
  5798. }
  5799. state = drm_atomic_state_alloc(crtc->dev);
  5800. state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
  5801. /* Everything's already locked, -EDEADLK can't happen. */
  5802. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  5803. ret = drm_atomic_add_affected_connectors(state, crtc);
  5804. WARN_ON(IS_ERR(crtc_state) || ret);
  5805. dev_priv->display.crtc_disable(crtc_state, state);
  5806. drm_atomic_state_put(state);
  5807. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  5808. crtc->base.id, crtc->name);
  5809. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  5810. crtc->state->active = false;
  5811. intel_crtc->active = false;
  5812. crtc->enabled = false;
  5813. crtc->state->connector_mask = 0;
  5814. crtc->state->encoder_mask = 0;
  5815. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  5816. encoder->base.crtc = NULL;
  5817. intel_fbc_disable(intel_crtc);
  5818. intel_update_watermarks(intel_crtc);
  5819. intel_disable_shared_dpll(intel_crtc);
  5820. domains = intel_crtc->enabled_power_domains;
  5821. for_each_power_domain(domain, domains)
  5822. intel_display_power_put(dev_priv, domain);
  5823. intel_crtc->enabled_power_domains = 0;
  5824. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5825. dev_priv->min_pixclk[intel_crtc->pipe] = 0;
  5826. }
  5827. /*
  5828. * turn all crtc's off, but do not adjust state
  5829. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5830. */
  5831. int intel_display_suspend(struct drm_device *dev)
  5832. {
  5833. struct drm_i915_private *dev_priv = to_i915(dev);
  5834. struct drm_atomic_state *state;
  5835. int ret;
  5836. state = drm_atomic_helper_suspend(dev);
  5837. ret = PTR_ERR_OR_ZERO(state);
  5838. if (ret)
  5839. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5840. else
  5841. dev_priv->modeset_restore_state = state;
  5842. return ret;
  5843. }
  5844. void intel_encoder_destroy(struct drm_encoder *encoder)
  5845. {
  5846. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5847. drm_encoder_cleanup(encoder);
  5848. kfree(intel_encoder);
  5849. }
  5850. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5851. * internal consistency). */
  5852. static void intel_connector_verify_state(struct intel_connector *connector)
  5853. {
  5854. struct drm_crtc *crtc = connector->base.state->crtc;
  5855. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5856. connector->base.base.id,
  5857. connector->base.name);
  5858. if (connector->get_hw_state(connector)) {
  5859. struct intel_encoder *encoder = connector->encoder;
  5860. struct drm_connector_state *conn_state = connector->base.state;
  5861. I915_STATE_WARN(!crtc,
  5862. "connector enabled without attached crtc\n");
  5863. if (!crtc)
  5864. return;
  5865. I915_STATE_WARN(!crtc->state->active,
  5866. "connector is active, but attached crtc isn't\n");
  5867. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5868. return;
  5869. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5870. "atomic encoder doesn't match attached encoder\n");
  5871. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5872. "attached encoder crtc differs from connector crtc\n");
  5873. } else {
  5874. I915_STATE_WARN(crtc && crtc->state->active,
  5875. "attached crtc is active, but connector isn't\n");
  5876. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5877. "best encoder set without crtc!\n");
  5878. }
  5879. }
  5880. int intel_connector_init(struct intel_connector *connector)
  5881. {
  5882. drm_atomic_helper_connector_reset(&connector->base);
  5883. if (!connector->base.state)
  5884. return -ENOMEM;
  5885. return 0;
  5886. }
  5887. struct intel_connector *intel_connector_alloc(void)
  5888. {
  5889. struct intel_connector *connector;
  5890. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5891. if (!connector)
  5892. return NULL;
  5893. if (intel_connector_init(connector) < 0) {
  5894. kfree(connector);
  5895. return NULL;
  5896. }
  5897. return connector;
  5898. }
  5899. /* Simple connector->get_hw_state implementation for encoders that support only
  5900. * one connector and no cloning and hence the encoder state determines the state
  5901. * of the connector. */
  5902. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5903. {
  5904. enum pipe pipe = 0;
  5905. struct intel_encoder *encoder = connector->encoder;
  5906. return encoder->get_hw_state(encoder, &pipe);
  5907. }
  5908. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5909. {
  5910. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5911. return crtc_state->fdi_lanes;
  5912. return 0;
  5913. }
  5914. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5915. struct intel_crtc_state *pipe_config)
  5916. {
  5917. struct drm_i915_private *dev_priv = to_i915(dev);
  5918. struct drm_atomic_state *state = pipe_config->base.state;
  5919. struct intel_crtc *other_crtc;
  5920. struct intel_crtc_state *other_crtc_state;
  5921. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5922. pipe_name(pipe), pipe_config->fdi_lanes);
  5923. if (pipe_config->fdi_lanes > 4) {
  5924. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5925. pipe_name(pipe), pipe_config->fdi_lanes);
  5926. return -EINVAL;
  5927. }
  5928. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  5929. if (pipe_config->fdi_lanes > 2) {
  5930. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5931. pipe_config->fdi_lanes);
  5932. return -EINVAL;
  5933. } else {
  5934. return 0;
  5935. }
  5936. }
  5937. if (INTEL_INFO(dev_priv)->num_pipes == 2)
  5938. return 0;
  5939. /* Ivybridge 3 pipe is really complicated */
  5940. switch (pipe) {
  5941. case PIPE_A:
  5942. return 0;
  5943. case PIPE_B:
  5944. if (pipe_config->fdi_lanes <= 2)
  5945. return 0;
  5946. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
  5947. other_crtc_state =
  5948. intel_atomic_get_crtc_state(state, other_crtc);
  5949. if (IS_ERR(other_crtc_state))
  5950. return PTR_ERR(other_crtc_state);
  5951. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5952. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5953. pipe_name(pipe), pipe_config->fdi_lanes);
  5954. return -EINVAL;
  5955. }
  5956. return 0;
  5957. case PIPE_C:
  5958. if (pipe_config->fdi_lanes > 2) {
  5959. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5960. pipe_name(pipe), pipe_config->fdi_lanes);
  5961. return -EINVAL;
  5962. }
  5963. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
  5964. other_crtc_state =
  5965. intel_atomic_get_crtc_state(state, other_crtc);
  5966. if (IS_ERR(other_crtc_state))
  5967. return PTR_ERR(other_crtc_state);
  5968. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5969. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5970. return -EINVAL;
  5971. }
  5972. return 0;
  5973. default:
  5974. BUG();
  5975. }
  5976. }
  5977. #define RETRY 1
  5978. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5979. struct intel_crtc_state *pipe_config)
  5980. {
  5981. struct drm_device *dev = intel_crtc->base.dev;
  5982. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5983. int lane, link_bw, fdi_dotclock, ret;
  5984. bool needs_recompute = false;
  5985. retry:
  5986. /* FDI is a binary signal running at ~2.7GHz, encoding
  5987. * each output octet as 10 bits. The actual frequency
  5988. * is stored as a divider into a 100MHz clock, and the
  5989. * mode pixel clock is stored in units of 1KHz.
  5990. * Hence the bw of each lane in terms of the mode signal
  5991. * is:
  5992. */
  5993. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5994. fdi_dotclock = adjusted_mode->crtc_clock;
  5995. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5996. pipe_config->pipe_bpp);
  5997. pipe_config->fdi_lanes = lane;
  5998. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5999. link_bw, &pipe_config->fdi_m_n);
  6000. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  6001. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  6002. pipe_config->pipe_bpp -= 2*3;
  6003. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  6004. pipe_config->pipe_bpp);
  6005. needs_recompute = true;
  6006. pipe_config->bw_constrained = true;
  6007. goto retry;
  6008. }
  6009. if (needs_recompute)
  6010. return RETRY;
  6011. return ret;
  6012. }
  6013. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  6014. struct intel_crtc_state *pipe_config)
  6015. {
  6016. if (pipe_config->pipe_bpp > 24)
  6017. return false;
  6018. /* HSW can handle pixel rate up to cdclk? */
  6019. if (IS_HASWELL(dev_priv))
  6020. return true;
  6021. /*
  6022. * We compare against max which means we must take
  6023. * the increased cdclk requirement into account when
  6024. * calculating the new cdclk.
  6025. *
  6026. * Should measure whether using a lower cdclk w/o IPS
  6027. */
  6028. return ilk_pipe_pixel_rate(pipe_config) <=
  6029. dev_priv->max_cdclk_freq * 95 / 100;
  6030. }
  6031. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  6032. struct intel_crtc_state *pipe_config)
  6033. {
  6034. struct drm_device *dev = crtc->base.dev;
  6035. struct drm_i915_private *dev_priv = to_i915(dev);
  6036. pipe_config->ips_enabled = i915.enable_ips &&
  6037. hsw_crtc_supports_ips(crtc) &&
  6038. pipe_config_supports_ips(dev_priv, pipe_config);
  6039. }
  6040. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  6041. {
  6042. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6043. /* GDG double wide on either pipe, otherwise pipe A only */
  6044. return INTEL_INFO(dev_priv)->gen < 4 &&
  6045. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  6046. }
  6047. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  6048. struct intel_crtc_state *pipe_config)
  6049. {
  6050. struct drm_device *dev = crtc->base.dev;
  6051. struct drm_i915_private *dev_priv = to_i915(dev);
  6052. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  6053. int clock_limit = dev_priv->max_dotclk_freq;
  6054. if (INTEL_INFO(dev)->gen < 4) {
  6055. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  6056. /*
  6057. * Enable double wide mode when the dot clock
  6058. * is > 90% of the (display) core speed.
  6059. */
  6060. if (intel_crtc_supports_double_wide(crtc) &&
  6061. adjusted_mode->crtc_clock > clock_limit) {
  6062. clock_limit = dev_priv->max_dotclk_freq;
  6063. pipe_config->double_wide = true;
  6064. }
  6065. }
  6066. if (adjusted_mode->crtc_clock > clock_limit) {
  6067. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  6068. adjusted_mode->crtc_clock, clock_limit,
  6069. yesno(pipe_config->double_wide));
  6070. return -EINVAL;
  6071. }
  6072. /*
  6073. * Pipe horizontal size must be even in:
  6074. * - DVO ganged mode
  6075. * - LVDS dual channel mode
  6076. * - Double wide pipe
  6077. */
  6078. if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  6079. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  6080. pipe_config->pipe_src_w &= ~1;
  6081. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  6082. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  6083. */
  6084. if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
  6085. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  6086. return -EINVAL;
  6087. if (HAS_IPS(dev_priv))
  6088. hsw_compute_ips_config(crtc, pipe_config);
  6089. if (pipe_config->has_pch_encoder)
  6090. return ironlake_fdi_compute_config(crtc, pipe_config);
  6091. return 0;
  6092. }
  6093. static int skylake_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6094. {
  6095. u32 cdctl;
  6096. skl_dpll0_update(dev_priv);
  6097. if (dev_priv->cdclk_pll.vco == 0)
  6098. return dev_priv->cdclk_pll.ref;
  6099. cdctl = I915_READ(CDCLK_CTL);
  6100. if (dev_priv->cdclk_pll.vco == 8640000) {
  6101. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  6102. case CDCLK_FREQ_450_432:
  6103. return 432000;
  6104. case CDCLK_FREQ_337_308:
  6105. return 308571;
  6106. case CDCLK_FREQ_540:
  6107. return 540000;
  6108. case CDCLK_FREQ_675_617:
  6109. return 617143;
  6110. default:
  6111. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  6112. }
  6113. } else {
  6114. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  6115. case CDCLK_FREQ_450_432:
  6116. return 450000;
  6117. case CDCLK_FREQ_337_308:
  6118. return 337500;
  6119. case CDCLK_FREQ_540:
  6120. return 540000;
  6121. case CDCLK_FREQ_675_617:
  6122. return 675000;
  6123. default:
  6124. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  6125. }
  6126. }
  6127. return dev_priv->cdclk_pll.ref;
  6128. }
  6129. static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
  6130. {
  6131. u32 val;
  6132. dev_priv->cdclk_pll.ref = 19200;
  6133. dev_priv->cdclk_pll.vco = 0;
  6134. val = I915_READ(BXT_DE_PLL_ENABLE);
  6135. if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
  6136. return;
  6137. if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
  6138. return;
  6139. val = I915_READ(BXT_DE_PLL_CTL);
  6140. dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
  6141. dev_priv->cdclk_pll.ref;
  6142. }
  6143. static int broxton_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6144. {
  6145. u32 divider;
  6146. int div, vco;
  6147. bxt_de_pll_update(dev_priv);
  6148. vco = dev_priv->cdclk_pll.vco;
  6149. if (vco == 0)
  6150. return dev_priv->cdclk_pll.ref;
  6151. divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
  6152. switch (divider) {
  6153. case BXT_CDCLK_CD2X_DIV_SEL_1:
  6154. div = 2;
  6155. break;
  6156. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  6157. div = 3;
  6158. break;
  6159. case BXT_CDCLK_CD2X_DIV_SEL_2:
  6160. div = 4;
  6161. break;
  6162. case BXT_CDCLK_CD2X_DIV_SEL_4:
  6163. div = 8;
  6164. break;
  6165. default:
  6166. MISSING_CASE(divider);
  6167. return dev_priv->cdclk_pll.ref;
  6168. }
  6169. return DIV_ROUND_CLOSEST(vco, div);
  6170. }
  6171. static int broadwell_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6172. {
  6173. uint32_t lcpll = I915_READ(LCPLL_CTL);
  6174. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  6175. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  6176. return 800000;
  6177. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  6178. return 450000;
  6179. else if (freq == LCPLL_CLK_FREQ_450)
  6180. return 450000;
  6181. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  6182. return 540000;
  6183. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  6184. return 337500;
  6185. else
  6186. return 675000;
  6187. }
  6188. static int haswell_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6189. {
  6190. uint32_t lcpll = I915_READ(LCPLL_CTL);
  6191. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  6192. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  6193. return 800000;
  6194. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  6195. return 450000;
  6196. else if (freq == LCPLL_CLK_FREQ_450)
  6197. return 450000;
  6198. else if (IS_HSW_ULT(dev_priv))
  6199. return 337500;
  6200. else
  6201. return 540000;
  6202. }
  6203. static int valleyview_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6204. {
  6205. return vlv_get_cck_clock_hpll(dev_priv, "cdclk",
  6206. CCK_DISPLAY_CLOCK_CONTROL);
  6207. }
  6208. static int ilk_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6209. {
  6210. return 450000;
  6211. }
  6212. static int i945_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6213. {
  6214. return 400000;
  6215. }
  6216. static int i915_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6217. {
  6218. return 333333;
  6219. }
  6220. static int i9xx_misc_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6221. {
  6222. return 200000;
  6223. }
  6224. static int pnv_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6225. {
  6226. struct pci_dev *pdev = dev_priv->drm.pdev;
  6227. u16 gcfgc = 0;
  6228. pci_read_config_word(pdev, GCFGC, &gcfgc);
  6229. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  6230. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  6231. return 266667;
  6232. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  6233. return 333333;
  6234. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  6235. return 444444;
  6236. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  6237. return 200000;
  6238. default:
  6239. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  6240. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  6241. return 133333;
  6242. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  6243. return 166667;
  6244. }
  6245. }
  6246. static int i915gm_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6247. {
  6248. struct pci_dev *pdev = dev_priv->drm.pdev;
  6249. u16 gcfgc = 0;
  6250. pci_read_config_word(pdev, GCFGC, &gcfgc);
  6251. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  6252. return 133333;
  6253. else {
  6254. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  6255. case GC_DISPLAY_CLOCK_333_MHZ:
  6256. return 333333;
  6257. default:
  6258. case GC_DISPLAY_CLOCK_190_200_MHZ:
  6259. return 190000;
  6260. }
  6261. }
  6262. }
  6263. static int i865_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6264. {
  6265. return 266667;
  6266. }
  6267. static int i85x_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6268. {
  6269. struct pci_dev *pdev = dev_priv->drm.pdev;
  6270. u16 hpllcc = 0;
  6271. /*
  6272. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  6273. * encoding is different :(
  6274. * FIXME is this the right way to detect 852GM/852GMV?
  6275. */
  6276. if (pdev->revision == 0x1)
  6277. return 133333;
  6278. pci_bus_read_config_word(pdev->bus,
  6279. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  6280. /* Assume that the hardware is in the high speed state. This
  6281. * should be the default.
  6282. */
  6283. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  6284. case GC_CLOCK_133_200:
  6285. case GC_CLOCK_133_200_2:
  6286. case GC_CLOCK_100_200:
  6287. return 200000;
  6288. case GC_CLOCK_166_250:
  6289. return 250000;
  6290. case GC_CLOCK_100_133:
  6291. return 133333;
  6292. case GC_CLOCK_133_266:
  6293. case GC_CLOCK_133_266_2:
  6294. case GC_CLOCK_166_266:
  6295. return 266667;
  6296. }
  6297. /* Shouldn't happen */
  6298. return 0;
  6299. }
  6300. static int i830_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6301. {
  6302. return 133333;
  6303. }
  6304. static unsigned int intel_hpll_vco(struct drm_i915_private *dev_priv)
  6305. {
  6306. static const unsigned int blb_vco[8] = {
  6307. [0] = 3200000,
  6308. [1] = 4000000,
  6309. [2] = 5333333,
  6310. [3] = 4800000,
  6311. [4] = 6400000,
  6312. };
  6313. static const unsigned int pnv_vco[8] = {
  6314. [0] = 3200000,
  6315. [1] = 4000000,
  6316. [2] = 5333333,
  6317. [3] = 4800000,
  6318. [4] = 2666667,
  6319. };
  6320. static const unsigned int cl_vco[8] = {
  6321. [0] = 3200000,
  6322. [1] = 4000000,
  6323. [2] = 5333333,
  6324. [3] = 6400000,
  6325. [4] = 3333333,
  6326. [5] = 3566667,
  6327. [6] = 4266667,
  6328. };
  6329. static const unsigned int elk_vco[8] = {
  6330. [0] = 3200000,
  6331. [1] = 4000000,
  6332. [2] = 5333333,
  6333. [3] = 4800000,
  6334. };
  6335. static const unsigned int ctg_vco[8] = {
  6336. [0] = 3200000,
  6337. [1] = 4000000,
  6338. [2] = 5333333,
  6339. [3] = 6400000,
  6340. [4] = 2666667,
  6341. [5] = 4266667,
  6342. };
  6343. const unsigned int *vco_table;
  6344. unsigned int vco;
  6345. uint8_t tmp = 0;
  6346. /* FIXME other chipsets? */
  6347. if (IS_GM45(dev_priv))
  6348. vco_table = ctg_vco;
  6349. else if (IS_G4X(dev_priv))
  6350. vco_table = elk_vco;
  6351. else if (IS_CRESTLINE(dev_priv))
  6352. vco_table = cl_vco;
  6353. else if (IS_PINEVIEW(dev_priv))
  6354. vco_table = pnv_vco;
  6355. else if (IS_G33(dev_priv))
  6356. vco_table = blb_vco;
  6357. else
  6358. return 0;
  6359. tmp = I915_READ(IS_MOBILE(dev_priv) ? HPLLVCO_MOBILE : HPLLVCO);
  6360. vco = vco_table[tmp & 0x7];
  6361. if (vco == 0)
  6362. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  6363. else
  6364. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  6365. return vco;
  6366. }
  6367. static int gm45_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6368. {
  6369. struct pci_dev *pdev = dev_priv->drm.pdev;
  6370. unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
  6371. uint16_t tmp = 0;
  6372. pci_read_config_word(pdev, GCFGC, &tmp);
  6373. cdclk_sel = (tmp >> 12) & 0x1;
  6374. switch (vco) {
  6375. case 2666667:
  6376. case 4000000:
  6377. case 5333333:
  6378. return cdclk_sel ? 333333 : 222222;
  6379. case 3200000:
  6380. return cdclk_sel ? 320000 : 228571;
  6381. default:
  6382. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  6383. return 222222;
  6384. }
  6385. }
  6386. static int i965gm_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6387. {
  6388. struct pci_dev *pdev = dev_priv->drm.pdev;
  6389. static const uint8_t div_3200[] = { 16, 10, 8 };
  6390. static const uint8_t div_4000[] = { 20, 12, 10 };
  6391. static const uint8_t div_5333[] = { 24, 16, 14 };
  6392. const uint8_t *div_table;
  6393. unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
  6394. uint16_t tmp = 0;
  6395. pci_read_config_word(pdev, GCFGC, &tmp);
  6396. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  6397. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  6398. goto fail;
  6399. switch (vco) {
  6400. case 3200000:
  6401. div_table = div_3200;
  6402. break;
  6403. case 4000000:
  6404. div_table = div_4000;
  6405. break;
  6406. case 5333333:
  6407. div_table = div_5333;
  6408. break;
  6409. default:
  6410. goto fail;
  6411. }
  6412. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  6413. fail:
  6414. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  6415. return 200000;
  6416. }
  6417. static int g33_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6418. {
  6419. struct pci_dev *pdev = dev_priv->drm.pdev;
  6420. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  6421. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  6422. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  6423. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  6424. const uint8_t *div_table;
  6425. unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
  6426. uint16_t tmp = 0;
  6427. pci_read_config_word(pdev, GCFGC, &tmp);
  6428. cdclk_sel = (tmp >> 4) & 0x7;
  6429. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  6430. goto fail;
  6431. switch (vco) {
  6432. case 3200000:
  6433. div_table = div_3200;
  6434. break;
  6435. case 4000000:
  6436. div_table = div_4000;
  6437. break;
  6438. case 4800000:
  6439. div_table = div_4800;
  6440. break;
  6441. case 5333333:
  6442. div_table = div_5333;
  6443. break;
  6444. default:
  6445. goto fail;
  6446. }
  6447. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  6448. fail:
  6449. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  6450. return 190476;
  6451. }
  6452. static void
  6453. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  6454. {
  6455. while (*num > DATA_LINK_M_N_MASK ||
  6456. *den > DATA_LINK_M_N_MASK) {
  6457. *num >>= 1;
  6458. *den >>= 1;
  6459. }
  6460. }
  6461. static void compute_m_n(unsigned int m, unsigned int n,
  6462. uint32_t *ret_m, uint32_t *ret_n)
  6463. {
  6464. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  6465. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  6466. intel_reduce_m_n_ratio(ret_m, ret_n);
  6467. }
  6468. void
  6469. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  6470. int pixel_clock, int link_clock,
  6471. struct intel_link_m_n *m_n)
  6472. {
  6473. m_n->tu = 64;
  6474. compute_m_n(bits_per_pixel * pixel_clock,
  6475. link_clock * nlanes * 8,
  6476. &m_n->gmch_m, &m_n->gmch_n);
  6477. compute_m_n(pixel_clock, link_clock,
  6478. &m_n->link_m, &m_n->link_n);
  6479. }
  6480. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  6481. {
  6482. if (i915.panel_use_ssc >= 0)
  6483. return i915.panel_use_ssc != 0;
  6484. return dev_priv->vbt.lvds_use_ssc
  6485. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  6486. }
  6487. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  6488. {
  6489. return (1 << dpll->n) << 16 | dpll->m2;
  6490. }
  6491. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  6492. {
  6493. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  6494. }
  6495. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  6496. struct intel_crtc_state *crtc_state,
  6497. struct dpll *reduced_clock)
  6498. {
  6499. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6500. u32 fp, fp2 = 0;
  6501. if (IS_PINEVIEW(dev_priv)) {
  6502. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  6503. if (reduced_clock)
  6504. fp2 = pnv_dpll_compute_fp(reduced_clock);
  6505. } else {
  6506. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6507. if (reduced_clock)
  6508. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6509. }
  6510. crtc_state->dpll_hw_state.fp0 = fp;
  6511. crtc->lowfreq_avail = false;
  6512. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6513. reduced_clock) {
  6514. crtc_state->dpll_hw_state.fp1 = fp2;
  6515. crtc->lowfreq_avail = true;
  6516. } else {
  6517. crtc_state->dpll_hw_state.fp1 = fp;
  6518. }
  6519. }
  6520. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  6521. pipe)
  6522. {
  6523. u32 reg_val;
  6524. /*
  6525. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  6526. * and set it to a reasonable value instead.
  6527. */
  6528. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6529. reg_val &= 0xffffff00;
  6530. reg_val |= 0x00000030;
  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 &= 0x8cffffff;
  6534. reg_val = 0x8c000000;
  6535. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6536. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6537. reg_val &= 0xffffff00;
  6538. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6539. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6540. reg_val &= 0x00ffffff;
  6541. reg_val |= 0xb0000000;
  6542. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6543. }
  6544. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  6545. struct intel_link_m_n *m_n)
  6546. {
  6547. struct drm_device *dev = crtc->base.dev;
  6548. struct drm_i915_private *dev_priv = to_i915(dev);
  6549. int pipe = crtc->pipe;
  6550. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6551. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  6552. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  6553. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  6554. }
  6555. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6556. struct intel_link_m_n *m_n,
  6557. struct intel_link_m_n *m2_n2)
  6558. {
  6559. struct drm_device *dev = crtc->base.dev;
  6560. struct drm_i915_private *dev_priv = to_i915(dev);
  6561. int pipe = crtc->pipe;
  6562. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6563. if (INTEL_INFO(dev)->gen >= 5) {
  6564. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6565. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6566. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6567. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6568. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6569. * for gen < 8) and if DRRS is supported (to make sure the
  6570. * registers are not unnecessarily accessed).
  6571. */
  6572. if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
  6573. INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
  6574. I915_WRITE(PIPE_DATA_M2(transcoder),
  6575. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6576. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6577. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6578. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6579. }
  6580. } else {
  6581. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6582. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6583. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6584. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6585. }
  6586. }
  6587. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6588. {
  6589. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6590. if (m_n == M1_N1) {
  6591. dp_m_n = &crtc->config->dp_m_n;
  6592. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6593. } else if (m_n == M2_N2) {
  6594. /*
  6595. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6596. * needs to be programmed into M1_N1.
  6597. */
  6598. dp_m_n = &crtc->config->dp_m2_n2;
  6599. } else {
  6600. DRM_ERROR("Unsupported divider value\n");
  6601. return;
  6602. }
  6603. if (crtc->config->has_pch_encoder)
  6604. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6605. else
  6606. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6607. }
  6608. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6609. struct intel_crtc_state *pipe_config)
  6610. {
  6611. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  6612. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6613. if (crtc->pipe != PIPE_A)
  6614. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6615. /* DPLL not used with DSI, but still need the rest set up */
  6616. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  6617. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  6618. DPLL_EXT_BUFFER_ENABLE_VLV;
  6619. pipe_config->dpll_hw_state.dpll_md =
  6620. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6621. }
  6622. static void chv_compute_dpll(struct intel_crtc *crtc,
  6623. struct intel_crtc_state *pipe_config)
  6624. {
  6625. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  6626. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6627. if (crtc->pipe != PIPE_A)
  6628. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6629. /* DPLL not used with DSI, but still need the rest set up */
  6630. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  6631. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  6632. pipe_config->dpll_hw_state.dpll_md =
  6633. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6634. }
  6635. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6636. const struct intel_crtc_state *pipe_config)
  6637. {
  6638. struct drm_device *dev = crtc->base.dev;
  6639. struct drm_i915_private *dev_priv = to_i915(dev);
  6640. enum pipe pipe = crtc->pipe;
  6641. u32 mdiv;
  6642. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6643. u32 coreclk, reg_val;
  6644. /* Enable Refclk */
  6645. I915_WRITE(DPLL(pipe),
  6646. pipe_config->dpll_hw_state.dpll &
  6647. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  6648. /* No need to actually set up the DPLL with DSI */
  6649. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6650. return;
  6651. mutex_lock(&dev_priv->sb_lock);
  6652. bestn = pipe_config->dpll.n;
  6653. bestm1 = pipe_config->dpll.m1;
  6654. bestm2 = pipe_config->dpll.m2;
  6655. bestp1 = pipe_config->dpll.p1;
  6656. bestp2 = pipe_config->dpll.p2;
  6657. /* See eDP HDMI DPIO driver vbios notes doc */
  6658. /* PLL B needs special handling */
  6659. if (pipe == PIPE_B)
  6660. vlv_pllb_recal_opamp(dev_priv, pipe);
  6661. /* Set up Tx target for periodic Rcomp update */
  6662. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6663. /* Disable target IRef on PLL */
  6664. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6665. reg_val &= 0x00ffffff;
  6666. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6667. /* Disable fast lock */
  6668. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6669. /* Set idtafcrecal before PLL is enabled */
  6670. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6671. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6672. mdiv |= ((bestn << DPIO_N_SHIFT));
  6673. mdiv |= (1 << DPIO_K_SHIFT);
  6674. /*
  6675. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6676. * but we don't support that).
  6677. * Note: don't use the DAC post divider as it seems unstable.
  6678. */
  6679. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6680. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6681. mdiv |= DPIO_ENABLE_CALIBRATION;
  6682. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6683. /* Set HBR and RBR LPF coefficients */
  6684. if (pipe_config->port_clock == 162000 ||
  6685. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
  6686. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
  6687. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6688. 0x009f0003);
  6689. else
  6690. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6691. 0x00d0000f);
  6692. if (intel_crtc_has_dp_encoder(pipe_config)) {
  6693. /* Use SSC source */
  6694. if (pipe == PIPE_A)
  6695. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6696. 0x0df40000);
  6697. else
  6698. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6699. 0x0df70000);
  6700. } else { /* HDMI or VGA */
  6701. /* Use bend source */
  6702. if (pipe == PIPE_A)
  6703. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6704. 0x0df70000);
  6705. else
  6706. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6707. 0x0df40000);
  6708. }
  6709. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6710. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6711. if (intel_crtc_has_dp_encoder(crtc->config))
  6712. coreclk |= 0x01000000;
  6713. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6714. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6715. mutex_unlock(&dev_priv->sb_lock);
  6716. }
  6717. static void chv_prepare_pll(struct intel_crtc *crtc,
  6718. const struct intel_crtc_state *pipe_config)
  6719. {
  6720. struct drm_device *dev = crtc->base.dev;
  6721. struct drm_i915_private *dev_priv = to_i915(dev);
  6722. enum pipe pipe = crtc->pipe;
  6723. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6724. u32 loopfilter, tribuf_calcntr;
  6725. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6726. u32 dpio_val;
  6727. int vco;
  6728. /* Enable Refclk and SSC */
  6729. I915_WRITE(DPLL(pipe),
  6730. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6731. /* No need to actually set up the DPLL with DSI */
  6732. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6733. return;
  6734. bestn = pipe_config->dpll.n;
  6735. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6736. bestm1 = pipe_config->dpll.m1;
  6737. bestm2 = pipe_config->dpll.m2 >> 22;
  6738. bestp1 = pipe_config->dpll.p1;
  6739. bestp2 = pipe_config->dpll.p2;
  6740. vco = pipe_config->dpll.vco;
  6741. dpio_val = 0;
  6742. loopfilter = 0;
  6743. mutex_lock(&dev_priv->sb_lock);
  6744. /* p1 and p2 divider */
  6745. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6746. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6747. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6748. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6749. 1 << DPIO_CHV_K_DIV_SHIFT);
  6750. /* Feedback post-divider - m2 */
  6751. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6752. /* Feedback refclk divider - n and m1 */
  6753. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6754. DPIO_CHV_M1_DIV_BY_2 |
  6755. 1 << DPIO_CHV_N_DIV_SHIFT);
  6756. /* M2 fraction division */
  6757. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6758. /* M2 fraction division enable */
  6759. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6760. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6761. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6762. if (bestm2_frac)
  6763. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6764. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6765. /* Program digital lock detect threshold */
  6766. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6767. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6768. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6769. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6770. if (!bestm2_frac)
  6771. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6772. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6773. /* Loop filter */
  6774. if (vco == 5400000) {
  6775. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6776. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6777. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6778. tribuf_calcntr = 0x9;
  6779. } else if (vco <= 6200000) {
  6780. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6781. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6782. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6783. tribuf_calcntr = 0x9;
  6784. } else if (vco <= 6480000) {
  6785. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6786. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6787. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6788. tribuf_calcntr = 0x8;
  6789. } else {
  6790. /* Not supported. Apply the same limits as in the max case */
  6791. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6792. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6793. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6794. tribuf_calcntr = 0;
  6795. }
  6796. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6797. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6798. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6799. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6800. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6801. /* AFC Recal */
  6802. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6803. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6804. DPIO_AFC_RECAL);
  6805. mutex_unlock(&dev_priv->sb_lock);
  6806. }
  6807. /**
  6808. * vlv_force_pll_on - forcibly enable just the PLL
  6809. * @dev_priv: i915 private structure
  6810. * @pipe: pipe PLL to enable
  6811. * @dpll: PLL configuration
  6812. *
  6813. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6814. * in cases where we need the PLL enabled even when @pipe is not going to
  6815. * be enabled.
  6816. */
  6817. int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
  6818. const struct dpll *dpll)
  6819. {
  6820. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  6821. struct intel_crtc_state *pipe_config;
  6822. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  6823. if (!pipe_config)
  6824. return -ENOMEM;
  6825. pipe_config->base.crtc = &crtc->base;
  6826. pipe_config->pixel_multiplier = 1;
  6827. pipe_config->dpll = *dpll;
  6828. if (IS_CHERRYVIEW(dev_priv)) {
  6829. chv_compute_dpll(crtc, pipe_config);
  6830. chv_prepare_pll(crtc, pipe_config);
  6831. chv_enable_pll(crtc, pipe_config);
  6832. } else {
  6833. vlv_compute_dpll(crtc, pipe_config);
  6834. vlv_prepare_pll(crtc, pipe_config);
  6835. vlv_enable_pll(crtc, pipe_config);
  6836. }
  6837. kfree(pipe_config);
  6838. return 0;
  6839. }
  6840. /**
  6841. * vlv_force_pll_off - forcibly disable just the PLL
  6842. * @dev_priv: i915 private structure
  6843. * @pipe: pipe PLL to disable
  6844. *
  6845. * Disable the PLL for @pipe. To be used in cases where we need
  6846. * the PLL enabled even when @pipe is not going to be enabled.
  6847. */
  6848. void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
  6849. {
  6850. if (IS_CHERRYVIEW(dev_priv))
  6851. chv_disable_pll(dev_priv, pipe);
  6852. else
  6853. vlv_disable_pll(dev_priv, pipe);
  6854. }
  6855. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6856. struct intel_crtc_state *crtc_state,
  6857. struct dpll *reduced_clock)
  6858. {
  6859. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6860. u32 dpll;
  6861. struct dpll *clock = &crtc_state->dpll;
  6862. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6863. dpll = DPLL_VGA_MODE_DIS;
  6864. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  6865. dpll |= DPLLB_MODE_LVDS;
  6866. else
  6867. dpll |= DPLLB_MODE_DAC_SERIAL;
  6868. if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) || IS_G33(dev_priv)) {
  6869. dpll |= (crtc_state->pixel_multiplier - 1)
  6870. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6871. }
  6872. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6873. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  6874. dpll |= DPLL_SDVO_HIGH_SPEED;
  6875. if (intel_crtc_has_dp_encoder(crtc_state))
  6876. dpll |= DPLL_SDVO_HIGH_SPEED;
  6877. /* compute bitmask from p1 value */
  6878. if (IS_PINEVIEW(dev_priv))
  6879. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6880. else {
  6881. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6882. if (IS_G4X(dev_priv) && reduced_clock)
  6883. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6884. }
  6885. switch (clock->p2) {
  6886. case 5:
  6887. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6888. break;
  6889. case 7:
  6890. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6891. break;
  6892. case 10:
  6893. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6894. break;
  6895. case 14:
  6896. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6897. break;
  6898. }
  6899. if (INTEL_GEN(dev_priv) >= 4)
  6900. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6901. if (crtc_state->sdvo_tv_clock)
  6902. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6903. else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6904. intel_panel_use_ssc(dev_priv))
  6905. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6906. else
  6907. dpll |= PLL_REF_INPUT_DREFCLK;
  6908. dpll |= DPLL_VCO_ENABLE;
  6909. crtc_state->dpll_hw_state.dpll = dpll;
  6910. if (INTEL_GEN(dev_priv) >= 4) {
  6911. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6912. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6913. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6914. }
  6915. }
  6916. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6917. struct intel_crtc_state *crtc_state,
  6918. struct dpll *reduced_clock)
  6919. {
  6920. struct drm_device *dev = crtc->base.dev;
  6921. struct drm_i915_private *dev_priv = to_i915(dev);
  6922. u32 dpll;
  6923. struct dpll *clock = &crtc_state->dpll;
  6924. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6925. dpll = DPLL_VGA_MODE_DIS;
  6926. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6927. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6928. } else {
  6929. if (clock->p1 == 2)
  6930. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6931. else
  6932. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6933. if (clock->p2 == 4)
  6934. dpll |= PLL_P2_DIVIDE_BY_4;
  6935. }
  6936. if (!IS_I830(dev_priv) &&
  6937. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
  6938. dpll |= DPLL_DVO_2X_MODE;
  6939. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6940. intel_panel_use_ssc(dev_priv))
  6941. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6942. else
  6943. dpll |= PLL_REF_INPUT_DREFCLK;
  6944. dpll |= DPLL_VCO_ENABLE;
  6945. crtc_state->dpll_hw_state.dpll = dpll;
  6946. }
  6947. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6948. {
  6949. struct drm_device *dev = intel_crtc->base.dev;
  6950. struct drm_i915_private *dev_priv = to_i915(dev);
  6951. enum pipe pipe = intel_crtc->pipe;
  6952. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6953. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  6954. uint32_t crtc_vtotal, crtc_vblank_end;
  6955. int vsyncshift = 0;
  6956. /* We need to be careful not to changed the adjusted mode, for otherwise
  6957. * the hw state checker will get angry at the mismatch. */
  6958. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6959. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6960. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6961. /* the chip adds 2 halflines automatically */
  6962. crtc_vtotal -= 1;
  6963. crtc_vblank_end -= 1;
  6964. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6965. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6966. else
  6967. vsyncshift = adjusted_mode->crtc_hsync_start -
  6968. adjusted_mode->crtc_htotal / 2;
  6969. if (vsyncshift < 0)
  6970. vsyncshift += adjusted_mode->crtc_htotal;
  6971. }
  6972. if (INTEL_INFO(dev)->gen > 3)
  6973. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6974. I915_WRITE(HTOTAL(cpu_transcoder),
  6975. (adjusted_mode->crtc_hdisplay - 1) |
  6976. ((adjusted_mode->crtc_htotal - 1) << 16));
  6977. I915_WRITE(HBLANK(cpu_transcoder),
  6978. (adjusted_mode->crtc_hblank_start - 1) |
  6979. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6980. I915_WRITE(HSYNC(cpu_transcoder),
  6981. (adjusted_mode->crtc_hsync_start - 1) |
  6982. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6983. I915_WRITE(VTOTAL(cpu_transcoder),
  6984. (adjusted_mode->crtc_vdisplay - 1) |
  6985. ((crtc_vtotal - 1) << 16));
  6986. I915_WRITE(VBLANK(cpu_transcoder),
  6987. (adjusted_mode->crtc_vblank_start - 1) |
  6988. ((crtc_vblank_end - 1) << 16));
  6989. I915_WRITE(VSYNC(cpu_transcoder),
  6990. (adjusted_mode->crtc_vsync_start - 1) |
  6991. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6992. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6993. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6994. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6995. * bits. */
  6996. if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
  6997. (pipe == PIPE_B || pipe == PIPE_C))
  6998. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6999. }
  7000. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  7001. {
  7002. struct drm_device *dev = intel_crtc->base.dev;
  7003. struct drm_i915_private *dev_priv = to_i915(dev);
  7004. enum pipe pipe = intel_crtc->pipe;
  7005. /* pipesrc controls the size that is scaled from, which should
  7006. * always be the user's requested size.
  7007. */
  7008. I915_WRITE(PIPESRC(pipe),
  7009. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  7010. (intel_crtc->config->pipe_src_h - 1));
  7011. }
  7012. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  7013. struct intel_crtc_state *pipe_config)
  7014. {
  7015. struct drm_device *dev = crtc->base.dev;
  7016. struct drm_i915_private *dev_priv = to_i915(dev);
  7017. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  7018. uint32_t tmp;
  7019. tmp = I915_READ(HTOTAL(cpu_transcoder));
  7020. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  7021. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  7022. tmp = I915_READ(HBLANK(cpu_transcoder));
  7023. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  7024. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  7025. tmp = I915_READ(HSYNC(cpu_transcoder));
  7026. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  7027. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  7028. tmp = I915_READ(VTOTAL(cpu_transcoder));
  7029. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  7030. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  7031. tmp = I915_READ(VBLANK(cpu_transcoder));
  7032. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  7033. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  7034. tmp = I915_READ(VSYNC(cpu_transcoder));
  7035. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  7036. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  7037. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  7038. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  7039. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  7040. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  7041. }
  7042. }
  7043. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  7044. struct intel_crtc_state *pipe_config)
  7045. {
  7046. struct drm_device *dev = crtc->base.dev;
  7047. struct drm_i915_private *dev_priv = to_i915(dev);
  7048. u32 tmp;
  7049. tmp = I915_READ(PIPESRC(crtc->pipe));
  7050. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  7051. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  7052. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  7053. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  7054. }
  7055. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  7056. struct intel_crtc_state *pipe_config)
  7057. {
  7058. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  7059. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  7060. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  7061. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  7062. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  7063. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  7064. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  7065. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  7066. mode->flags = pipe_config->base.adjusted_mode.flags;
  7067. mode->type = DRM_MODE_TYPE_DRIVER;
  7068. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  7069. mode->flags |= pipe_config->base.adjusted_mode.flags;
  7070. mode->hsync = drm_mode_hsync(mode);
  7071. mode->vrefresh = drm_mode_vrefresh(mode);
  7072. drm_mode_set_name(mode);
  7073. }
  7074. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  7075. {
  7076. struct drm_device *dev = intel_crtc->base.dev;
  7077. struct drm_i915_private *dev_priv = to_i915(dev);
  7078. uint32_t pipeconf;
  7079. pipeconf = 0;
  7080. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  7081. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  7082. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  7083. if (intel_crtc->config->double_wide)
  7084. pipeconf |= PIPECONF_DOUBLE_WIDE;
  7085. /* only g4x and later have fancy bpc/dither controls */
  7086. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  7087. IS_CHERRYVIEW(dev_priv)) {
  7088. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  7089. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  7090. pipeconf |= PIPECONF_DITHER_EN |
  7091. PIPECONF_DITHER_TYPE_SP;
  7092. switch (intel_crtc->config->pipe_bpp) {
  7093. case 18:
  7094. pipeconf |= PIPECONF_6BPC;
  7095. break;
  7096. case 24:
  7097. pipeconf |= PIPECONF_8BPC;
  7098. break;
  7099. case 30:
  7100. pipeconf |= PIPECONF_10BPC;
  7101. break;
  7102. default:
  7103. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7104. BUG();
  7105. }
  7106. }
  7107. if (HAS_PIPE_CXSR(dev_priv)) {
  7108. if (intel_crtc->lowfreq_avail) {
  7109. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  7110. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  7111. } else {
  7112. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  7113. }
  7114. }
  7115. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  7116. if (INTEL_INFO(dev)->gen < 4 ||
  7117. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  7118. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  7119. else
  7120. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  7121. } else
  7122. pipeconf |= PIPECONF_PROGRESSIVE;
  7123. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  7124. intel_crtc->config->limited_color_range)
  7125. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  7126. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  7127. POSTING_READ(PIPECONF(intel_crtc->pipe));
  7128. }
  7129. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  7130. struct intel_crtc_state *crtc_state)
  7131. {
  7132. struct drm_device *dev = crtc->base.dev;
  7133. struct drm_i915_private *dev_priv = to_i915(dev);
  7134. const struct intel_limit *limit;
  7135. int refclk = 48000;
  7136. memset(&crtc_state->dpll_hw_state, 0,
  7137. sizeof(crtc_state->dpll_hw_state));
  7138. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7139. if (intel_panel_use_ssc(dev_priv)) {
  7140. refclk = dev_priv->vbt.lvds_ssc_freq;
  7141. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7142. }
  7143. limit = &intel_limits_i8xx_lvds;
  7144. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
  7145. limit = &intel_limits_i8xx_dvo;
  7146. } else {
  7147. limit = &intel_limits_i8xx_dac;
  7148. }
  7149. if (!crtc_state->clock_set &&
  7150. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7151. refclk, NULL, &crtc_state->dpll)) {
  7152. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7153. return -EINVAL;
  7154. }
  7155. i8xx_compute_dpll(crtc, crtc_state, NULL);
  7156. return 0;
  7157. }
  7158. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  7159. struct intel_crtc_state *crtc_state)
  7160. {
  7161. struct drm_device *dev = crtc->base.dev;
  7162. struct drm_i915_private *dev_priv = to_i915(dev);
  7163. const struct intel_limit *limit;
  7164. int refclk = 96000;
  7165. memset(&crtc_state->dpll_hw_state, 0,
  7166. sizeof(crtc_state->dpll_hw_state));
  7167. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7168. if (intel_panel_use_ssc(dev_priv)) {
  7169. refclk = dev_priv->vbt.lvds_ssc_freq;
  7170. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7171. }
  7172. if (intel_is_dual_link_lvds(dev))
  7173. limit = &intel_limits_g4x_dual_channel_lvds;
  7174. else
  7175. limit = &intel_limits_g4x_single_channel_lvds;
  7176. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  7177. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  7178. limit = &intel_limits_g4x_hdmi;
  7179. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  7180. limit = &intel_limits_g4x_sdvo;
  7181. } else {
  7182. /* The option is for other outputs */
  7183. limit = &intel_limits_i9xx_sdvo;
  7184. }
  7185. if (!crtc_state->clock_set &&
  7186. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7187. refclk, NULL, &crtc_state->dpll)) {
  7188. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7189. return -EINVAL;
  7190. }
  7191. i9xx_compute_dpll(crtc, crtc_state, NULL);
  7192. return 0;
  7193. }
  7194. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  7195. struct intel_crtc_state *crtc_state)
  7196. {
  7197. struct drm_device *dev = crtc->base.dev;
  7198. struct drm_i915_private *dev_priv = to_i915(dev);
  7199. const struct intel_limit *limit;
  7200. int refclk = 96000;
  7201. memset(&crtc_state->dpll_hw_state, 0,
  7202. sizeof(crtc_state->dpll_hw_state));
  7203. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7204. if (intel_panel_use_ssc(dev_priv)) {
  7205. refclk = dev_priv->vbt.lvds_ssc_freq;
  7206. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7207. }
  7208. limit = &intel_limits_pineview_lvds;
  7209. } else {
  7210. limit = &intel_limits_pineview_sdvo;
  7211. }
  7212. if (!crtc_state->clock_set &&
  7213. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7214. refclk, NULL, &crtc_state->dpll)) {
  7215. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7216. return -EINVAL;
  7217. }
  7218. i9xx_compute_dpll(crtc, crtc_state, NULL);
  7219. return 0;
  7220. }
  7221. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  7222. struct intel_crtc_state *crtc_state)
  7223. {
  7224. struct drm_device *dev = crtc->base.dev;
  7225. struct drm_i915_private *dev_priv = to_i915(dev);
  7226. const struct intel_limit *limit;
  7227. int refclk = 96000;
  7228. memset(&crtc_state->dpll_hw_state, 0,
  7229. sizeof(crtc_state->dpll_hw_state));
  7230. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7231. if (intel_panel_use_ssc(dev_priv)) {
  7232. refclk = dev_priv->vbt.lvds_ssc_freq;
  7233. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7234. }
  7235. limit = &intel_limits_i9xx_lvds;
  7236. } else {
  7237. limit = &intel_limits_i9xx_sdvo;
  7238. }
  7239. if (!crtc_state->clock_set &&
  7240. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7241. refclk, NULL, &crtc_state->dpll)) {
  7242. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7243. return -EINVAL;
  7244. }
  7245. i9xx_compute_dpll(crtc, crtc_state, NULL);
  7246. return 0;
  7247. }
  7248. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  7249. struct intel_crtc_state *crtc_state)
  7250. {
  7251. int refclk = 100000;
  7252. const struct intel_limit *limit = &intel_limits_chv;
  7253. memset(&crtc_state->dpll_hw_state, 0,
  7254. sizeof(crtc_state->dpll_hw_state));
  7255. if (!crtc_state->clock_set &&
  7256. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7257. refclk, NULL, &crtc_state->dpll)) {
  7258. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7259. return -EINVAL;
  7260. }
  7261. chv_compute_dpll(crtc, crtc_state);
  7262. return 0;
  7263. }
  7264. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  7265. struct intel_crtc_state *crtc_state)
  7266. {
  7267. int refclk = 100000;
  7268. const struct intel_limit *limit = &intel_limits_vlv;
  7269. memset(&crtc_state->dpll_hw_state, 0,
  7270. sizeof(crtc_state->dpll_hw_state));
  7271. if (!crtc_state->clock_set &&
  7272. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7273. refclk, NULL, &crtc_state->dpll)) {
  7274. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7275. return -EINVAL;
  7276. }
  7277. vlv_compute_dpll(crtc, crtc_state);
  7278. return 0;
  7279. }
  7280. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  7281. struct intel_crtc_state *pipe_config)
  7282. {
  7283. struct drm_device *dev = crtc->base.dev;
  7284. struct drm_i915_private *dev_priv = to_i915(dev);
  7285. uint32_t tmp;
  7286. if (INTEL_GEN(dev_priv) <= 3 &&
  7287. (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
  7288. return;
  7289. tmp = I915_READ(PFIT_CONTROL);
  7290. if (!(tmp & PFIT_ENABLE))
  7291. return;
  7292. /* Check whether the pfit is attached to our pipe. */
  7293. if (INTEL_INFO(dev)->gen < 4) {
  7294. if (crtc->pipe != PIPE_B)
  7295. return;
  7296. } else {
  7297. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  7298. return;
  7299. }
  7300. pipe_config->gmch_pfit.control = tmp;
  7301. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  7302. }
  7303. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  7304. struct intel_crtc_state *pipe_config)
  7305. {
  7306. struct drm_device *dev = crtc->base.dev;
  7307. struct drm_i915_private *dev_priv = to_i915(dev);
  7308. int pipe = pipe_config->cpu_transcoder;
  7309. struct dpll clock;
  7310. u32 mdiv;
  7311. int refclk = 100000;
  7312. /* In case of DSI, DPLL will not be used */
  7313. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  7314. return;
  7315. mutex_lock(&dev_priv->sb_lock);
  7316. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  7317. mutex_unlock(&dev_priv->sb_lock);
  7318. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  7319. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  7320. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  7321. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  7322. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  7323. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  7324. }
  7325. static void
  7326. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  7327. struct intel_initial_plane_config *plane_config)
  7328. {
  7329. struct drm_device *dev = crtc->base.dev;
  7330. struct drm_i915_private *dev_priv = to_i915(dev);
  7331. u32 val, base, offset;
  7332. int pipe = crtc->pipe, plane = crtc->plane;
  7333. int fourcc, pixel_format;
  7334. unsigned int aligned_height;
  7335. struct drm_framebuffer *fb;
  7336. struct intel_framebuffer *intel_fb;
  7337. val = I915_READ(DSPCNTR(plane));
  7338. if (!(val & DISPLAY_PLANE_ENABLE))
  7339. return;
  7340. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7341. if (!intel_fb) {
  7342. DRM_DEBUG_KMS("failed to alloc fb\n");
  7343. return;
  7344. }
  7345. fb = &intel_fb->base;
  7346. if (INTEL_INFO(dev)->gen >= 4) {
  7347. if (val & DISPPLANE_TILED) {
  7348. plane_config->tiling = I915_TILING_X;
  7349. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7350. }
  7351. }
  7352. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7353. fourcc = i9xx_format_to_fourcc(pixel_format);
  7354. fb->pixel_format = fourcc;
  7355. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7356. if (INTEL_INFO(dev)->gen >= 4) {
  7357. if (plane_config->tiling)
  7358. offset = I915_READ(DSPTILEOFF(plane));
  7359. else
  7360. offset = I915_READ(DSPLINOFF(plane));
  7361. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  7362. } else {
  7363. base = I915_READ(DSPADDR(plane));
  7364. }
  7365. plane_config->base = base;
  7366. val = I915_READ(PIPESRC(pipe));
  7367. fb->width = ((val >> 16) & 0xfff) + 1;
  7368. fb->height = ((val >> 0) & 0xfff) + 1;
  7369. val = I915_READ(DSPSTRIDE(pipe));
  7370. fb->pitches[0] = val & 0xffffffc0;
  7371. aligned_height = intel_fb_align_height(dev, fb->height,
  7372. fb->pixel_format,
  7373. fb->modifier[0]);
  7374. plane_config->size = fb->pitches[0] * aligned_height;
  7375. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7376. pipe_name(pipe), plane, fb->width, fb->height,
  7377. fb->bits_per_pixel, base, fb->pitches[0],
  7378. plane_config->size);
  7379. plane_config->fb = intel_fb;
  7380. }
  7381. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  7382. struct intel_crtc_state *pipe_config)
  7383. {
  7384. struct drm_device *dev = crtc->base.dev;
  7385. struct drm_i915_private *dev_priv = to_i915(dev);
  7386. int pipe = pipe_config->cpu_transcoder;
  7387. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  7388. struct dpll clock;
  7389. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  7390. int refclk = 100000;
  7391. /* In case of DSI, DPLL will not be used */
  7392. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  7393. return;
  7394. mutex_lock(&dev_priv->sb_lock);
  7395. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  7396. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  7397. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  7398. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  7399. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  7400. mutex_unlock(&dev_priv->sb_lock);
  7401. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  7402. clock.m2 = (pll_dw0 & 0xff) << 22;
  7403. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  7404. clock.m2 |= pll_dw2 & 0x3fffff;
  7405. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  7406. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  7407. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  7408. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  7409. }
  7410. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  7411. struct intel_crtc_state *pipe_config)
  7412. {
  7413. struct drm_device *dev = crtc->base.dev;
  7414. struct drm_i915_private *dev_priv = to_i915(dev);
  7415. enum intel_display_power_domain power_domain;
  7416. uint32_t tmp;
  7417. bool ret;
  7418. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7419. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7420. return false;
  7421. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7422. pipe_config->shared_dpll = NULL;
  7423. ret = false;
  7424. tmp = I915_READ(PIPECONF(crtc->pipe));
  7425. if (!(tmp & PIPECONF_ENABLE))
  7426. goto out;
  7427. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  7428. IS_CHERRYVIEW(dev_priv)) {
  7429. switch (tmp & PIPECONF_BPC_MASK) {
  7430. case PIPECONF_6BPC:
  7431. pipe_config->pipe_bpp = 18;
  7432. break;
  7433. case PIPECONF_8BPC:
  7434. pipe_config->pipe_bpp = 24;
  7435. break;
  7436. case PIPECONF_10BPC:
  7437. pipe_config->pipe_bpp = 30;
  7438. break;
  7439. default:
  7440. break;
  7441. }
  7442. }
  7443. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  7444. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  7445. pipe_config->limited_color_range = true;
  7446. if (INTEL_INFO(dev)->gen < 4)
  7447. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  7448. intel_get_pipe_timings(crtc, pipe_config);
  7449. intel_get_pipe_src_size(crtc, pipe_config);
  7450. i9xx_get_pfit_config(crtc, pipe_config);
  7451. if (INTEL_INFO(dev)->gen >= 4) {
  7452. /* No way to read it out on pipes B and C */
  7453. if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
  7454. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  7455. else
  7456. tmp = I915_READ(DPLL_MD(crtc->pipe));
  7457. pipe_config->pixel_multiplier =
  7458. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  7459. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  7460. pipe_config->dpll_hw_state.dpll_md = tmp;
  7461. } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  7462. IS_G33(dev_priv)) {
  7463. tmp = I915_READ(DPLL(crtc->pipe));
  7464. pipe_config->pixel_multiplier =
  7465. ((tmp & SDVO_MULTIPLIER_MASK)
  7466. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  7467. } else {
  7468. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  7469. * port and will be fixed up in the encoder->get_config
  7470. * function. */
  7471. pipe_config->pixel_multiplier = 1;
  7472. }
  7473. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  7474. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  7475. /*
  7476. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  7477. * on 830. Filter it out here so that we don't
  7478. * report errors due to that.
  7479. */
  7480. if (IS_I830(dev_priv))
  7481. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  7482. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  7483. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  7484. } else {
  7485. /* Mask out read-only status bits. */
  7486. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  7487. DPLL_PORTC_READY_MASK |
  7488. DPLL_PORTB_READY_MASK);
  7489. }
  7490. if (IS_CHERRYVIEW(dev_priv))
  7491. chv_crtc_clock_get(crtc, pipe_config);
  7492. else if (IS_VALLEYVIEW(dev_priv))
  7493. vlv_crtc_clock_get(crtc, pipe_config);
  7494. else
  7495. i9xx_crtc_clock_get(crtc, pipe_config);
  7496. /*
  7497. * Normally the dotclock is filled in by the encoder .get_config()
  7498. * but in case the pipe is enabled w/o any ports we need a sane
  7499. * default.
  7500. */
  7501. pipe_config->base.adjusted_mode.crtc_clock =
  7502. pipe_config->port_clock / pipe_config->pixel_multiplier;
  7503. ret = true;
  7504. out:
  7505. intel_display_power_put(dev_priv, power_domain);
  7506. return ret;
  7507. }
  7508. static void ironlake_init_pch_refclk(struct drm_device *dev)
  7509. {
  7510. struct drm_i915_private *dev_priv = to_i915(dev);
  7511. struct intel_encoder *encoder;
  7512. int i;
  7513. u32 val, final;
  7514. bool has_lvds = false;
  7515. bool has_cpu_edp = false;
  7516. bool has_panel = false;
  7517. bool has_ck505 = false;
  7518. bool can_ssc = false;
  7519. bool using_ssc_source = false;
  7520. /* We need to take the global config into account */
  7521. for_each_intel_encoder(dev, encoder) {
  7522. switch (encoder->type) {
  7523. case INTEL_OUTPUT_LVDS:
  7524. has_panel = true;
  7525. has_lvds = true;
  7526. break;
  7527. case INTEL_OUTPUT_EDP:
  7528. has_panel = true;
  7529. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  7530. has_cpu_edp = true;
  7531. break;
  7532. default:
  7533. break;
  7534. }
  7535. }
  7536. if (HAS_PCH_IBX(dev_priv)) {
  7537. has_ck505 = dev_priv->vbt.display_clock_mode;
  7538. can_ssc = has_ck505;
  7539. } else {
  7540. has_ck505 = false;
  7541. can_ssc = true;
  7542. }
  7543. /* Check if any DPLLs are using the SSC source */
  7544. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  7545. u32 temp = I915_READ(PCH_DPLL(i));
  7546. if (!(temp & DPLL_VCO_ENABLE))
  7547. continue;
  7548. if ((temp & PLL_REF_INPUT_MASK) ==
  7549. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  7550. using_ssc_source = true;
  7551. break;
  7552. }
  7553. }
  7554. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  7555. has_panel, has_lvds, has_ck505, using_ssc_source);
  7556. /* Ironlake: try to setup display ref clock before DPLL
  7557. * enabling. This is only under driver's control after
  7558. * PCH B stepping, previous chipset stepping should be
  7559. * ignoring this setting.
  7560. */
  7561. val = I915_READ(PCH_DREF_CONTROL);
  7562. /* As we must carefully and slowly disable/enable each source in turn,
  7563. * compute the final state we want first and check if we need to
  7564. * make any changes at all.
  7565. */
  7566. final = val;
  7567. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  7568. if (has_ck505)
  7569. final |= DREF_NONSPREAD_CK505_ENABLE;
  7570. else
  7571. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  7572. final &= ~DREF_SSC_SOURCE_MASK;
  7573. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7574. final &= ~DREF_SSC1_ENABLE;
  7575. if (has_panel) {
  7576. final |= DREF_SSC_SOURCE_ENABLE;
  7577. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7578. final |= DREF_SSC1_ENABLE;
  7579. if (has_cpu_edp) {
  7580. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7581. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7582. else
  7583. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7584. } else
  7585. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7586. } else if (using_ssc_source) {
  7587. final |= DREF_SSC_SOURCE_ENABLE;
  7588. final |= DREF_SSC1_ENABLE;
  7589. }
  7590. if (final == val)
  7591. return;
  7592. /* Always enable nonspread source */
  7593. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  7594. if (has_ck505)
  7595. val |= DREF_NONSPREAD_CK505_ENABLE;
  7596. else
  7597. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  7598. if (has_panel) {
  7599. val &= ~DREF_SSC_SOURCE_MASK;
  7600. val |= DREF_SSC_SOURCE_ENABLE;
  7601. /* SSC must be turned on before enabling the CPU output */
  7602. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7603. DRM_DEBUG_KMS("Using SSC on panel\n");
  7604. val |= DREF_SSC1_ENABLE;
  7605. } else
  7606. val &= ~DREF_SSC1_ENABLE;
  7607. /* Get SSC going before enabling the outputs */
  7608. I915_WRITE(PCH_DREF_CONTROL, val);
  7609. POSTING_READ(PCH_DREF_CONTROL);
  7610. udelay(200);
  7611. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7612. /* Enable CPU source on CPU attached eDP */
  7613. if (has_cpu_edp) {
  7614. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7615. DRM_DEBUG_KMS("Using SSC on eDP\n");
  7616. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7617. } else
  7618. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7619. } else
  7620. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7621. I915_WRITE(PCH_DREF_CONTROL, val);
  7622. POSTING_READ(PCH_DREF_CONTROL);
  7623. udelay(200);
  7624. } else {
  7625. DRM_DEBUG_KMS("Disabling CPU source output\n");
  7626. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7627. /* Turn off CPU output */
  7628. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7629. I915_WRITE(PCH_DREF_CONTROL, val);
  7630. POSTING_READ(PCH_DREF_CONTROL);
  7631. udelay(200);
  7632. if (!using_ssc_source) {
  7633. DRM_DEBUG_KMS("Disabling SSC source\n");
  7634. /* Turn off the SSC source */
  7635. val &= ~DREF_SSC_SOURCE_MASK;
  7636. val |= DREF_SSC_SOURCE_DISABLE;
  7637. /* Turn off SSC1 */
  7638. val &= ~DREF_SSC1_ENABLE;
  7639. I915_WRITE(PCH_DREF_CONTROL, val);
  7640. POSTING_READ(PCH_DREF_CONTROL);
  7641. udelay(200);
  7642. }
  7643. }
  7644. BUG_ON(val != final);
  7645. }
  7646. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  7647. {
  7648. uint32_t tmp;
  7649. tmp = I915_READ(SOUTH_CHICKEN2);
  7650. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  7651. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7652. if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
  7653. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  7654. DRM_ERROR("FDI mPHY reset assert timeout\n");
  7655. tmp = I915_READ(SOUTH_CHICKEN2);
  7656. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  7657. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7658. if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
  7659. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  7660. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  7661. }
  7662. /* WaMPhyProgramming:hsw */
  7663. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  7664. {
  7665. uint32_t tmp;
  7666. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  7667. tmp &= ~(0xFF << 24);
  7668. tmp |= (0x12 << 24);
  7669. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  7670. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  7671. tmp |= (1 << 11);
  7672. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  7673. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  7674. tmp |= (1 << 11);
  7675. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  7676. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  7677. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7678. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7679. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7680. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7681. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7682. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7683. tmp &= ~(7 << 13);
  7684. tmp |= (5 << 13);
  7685. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7686. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7687. tmp &= ~(7 << 13);
  7688. tmp |= (5 << 13);
  7689. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7690. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7691. tmp &= ~0xFF;
  7692. tmp |= 0x1C;
  7693. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7694. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7695. tmp &= ~0xFF;
  7696. tmp |= 0x1C;
  7697. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7698. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7699. tmp &= ~(0xFF << 16);
  7700. tmp |= (0x1C << 16);
  7701. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7702. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7703. tmp &= ~(0xFF << 16);
  7704. tmp |= (0x1C << 16);
  7705. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7706. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7707. tmp |= (1 << 27);
  7708. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7709. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7710. tmp |= (1 << 27);
  7711. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7712. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7713. tmp &= ~(0xF << 28);
  7714. tmp |= (4 << 28);
  7715. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7716. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7717. tmp &= ~(0xF << 28);
  7718. tmp |= (4 << 28);
  7719. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7720. }
  7721. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7722. * Programming" based on the parameters passed:
  7723. * - Sequence to enable CLKOUT_DP
  7724. * - Sequence to enable CLKOUT_DP without spread
  7725. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7726. */
  7727. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7728. bool with_fdi)
  7729. {
  7730. struct drm_i915_private *dev_priv = to_i915(dev);
  7731. uint32_t reg, tmp;
  7732. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7733. with_spread = true;
  7734. if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
  7735. with_fdi, "LP PCH doesn't have FDI\n"))
  7736. with_fdi = false;
  7737. mutex_lock(&dev_priv->sb_lock);
  7738. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7739. tmp &= ~SBI_SSCCTL_DISABLE;
  7740. tmp |= SBI_SSCCTL_PATHALT;
  7741. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7742. udelay(24);
  7743. if (with_spread) {
  7744. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7745. tmp &= ~SBI_SSCCTL_PATHALT;
  7746. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7747. if (with_fdi) {
  7748. lpt_reset_fdi_mphy(dev_priv);
  7749. lpt_program_fdi_mphy(dev_priv);
  7750. }
  7751. }
  7752. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  7753. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7754. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7755. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7756. mutex_unlock(&dev_priv->sb_lock);
  7757. }
  7758. /* Sequence to disable CLKOUT_DP */
  7759. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7760. {
  7761. struct drm_i915_private *dev_priv = to_i915(dev);
  7762. uint32_t reg, tmp;
  7763. mutex_lock(&dev_priv->sb_lock);
  7764. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  7765. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7766. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7767. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7768. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7769. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7770. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7771. tmp |= SBI_SSCCTL_PATHALT;
  7772. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7773. udelay(32);
  7774. }
  7775. tmp |= SBI_SSCCTL_DISABLE;
  7776. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7777. }
  7778. mutex_unlock(&dev_priv->sb_lock);
  7779. }
  7780. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  7781. static const uint16_t sscdivintphase[] = {
  7782. [BEND_IDX( 50)] = 0x3B23,
  7783. [BEND_IDX( 45)] = 0x3B23,
  7784. [BEND_IDX( 40)] = 0x3C23,
  7785. [BEND_IDX( 35)] = 0x3C23,
  7786. [BEND_IDX( 30)] = 0x3D23,
  7787. [BEND_IDX( 25)] = 0x3D23,
  7788. [BEND_IDX( 20)] = 0x3E23,
  7789. [BEND_IDX( 15)] = 0x3E23,
  7790. [BEND_IDX( 10)] = 0x3F23,
  7791. [BEND_IDX( 5)] = 0x3F23,
  7792. [BEND_IDX( 0)] = 0x0025,
  7793. [BEND_IDX( -5)] = 0x0025,
  7794. [BEND_IDX(-10)] = 0x0125,
  7795. [BEND_IDX(-15)] = 0x0125,
  7796. [BEND_IDX(-20)] = 0x0225,
  7797. [BEND_IDX(-25)] = 0x0225,
  7798. [BEND_IDX(-30)] = 0x0325,
  7799. [BEND_IDX(-35)] = 0x0325,
  7800. [BEND_IDX(-40)] = 0x0425,
  7801. [BEND_IDX(-45)] = 0x0425,
  7802. [BEND_IDX(-50)] = 0x0525,
  7803. };
  7804. /*
  7805. * Bend CLKOUT_DP
  7806. * steps -50 to 50 inclusive, in steps of 5
  7807. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  7808. * change in clock period = -(steps / 10) * 5.787 ps
  7809. */
  7810. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  7811. {
  7812. uint32_t tmp;
  7813. int idx = BEND_IDX(steps);
  7814. if (WARN_ON(steps % 5 != 0))
  7815. return;
  7816. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  7817. return;
  7818. mutex_lock(&dev_priv->sb_lock);
  7819. if (steps % 10 != 0)
  7820. tmp = 0xAAAAAAAB;
  7821. else
  7822. tmp = 0x00000000;
  7823. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  7824. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  7825. tmp &= 0xffff0000;
  7826. tmp |= sscdivintphase[idx];
  7827. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  7828. mutex_unlock(&dev_priv->sb_lock);
  7829. }
  7830. #undef BEND_IDX
  7831. static void lpt_init_pch_refclk(struct drm_device *dev)
  7832. {
  7833. struct intel_encoder *encoder;
  7834. bool has_vga = false;
  7835. for_each_intel_encoder(dev, encoder) {
  7836. switch (encoder->type) {
  7837. case INTEL_OUTPUT_ANALOG:
  7838. has_vga = true;
  7839. break;
  7840. default:
  7841. break;
  7842. }
  7843. }
  7844. if (has_vga) {
  7845. lpt_bend_clkout_dp(to_i915(dev), 0);
  7846. lpt_enable_clkout_dp(dev, true, true);
  7847. } else {
  7848. lpt_disable_clkout_dp(dev);
  7849. }
  7850. }
  7851. /*
  7852. * Initialize reference clocks when the driver loads
  7853. */
  7854. void intel_init_pch_refclk(struct drm_device *dev)
  7855. {
  7856. struct drm_i915_private *dev_priv = to_i915(dev);
  7857. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
  7858. ironlake_init_pch_refclk(dev);
  7859. else if (HAS_PCH_LPT(dev_priv))
  7860. lpt_init_pch_refclk(dev);
  7861. }
  7862. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7863. {
  7864. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7865. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7866. int pipe = intel_crtc->pipe;
  7867. uint32_t val;
  7868. val = 0;
  7869. switch (intel_crtc->config->pipe_bpp) {
  7870. case 18:
  7871. val |= PIPECONF_6BPC;
  7872. break;
  7873. case 24:
  7874. val |= PIPECONF_8BPC;
  7875. break;
  7876. case 30:
  7877. val |= PIPECONF_10BPC;
  7878. break;
  7879. case 36:
  7880. val |= PIPECONF_12BPC;
  7881. break;
  7882. default:
  7883. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7884. BUG();
  7885. }
  7886. if (intel_crtc->config->dither)
  7887. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7888. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7889. val |= PIPECONF_INTERLACED_ILK;
  7890. else
  7891. val |= PIPECONF_PROGRESSIVE;
  7892. if (intel_crtc->config->limited_color_range)
  7893. val |= PIPECONF_COLOR_RANGE_SELECT;
  7894. I915_WRITE(PIPECONF(pipe), val);
  7895. POSTING_READ(PIPECONF(pipe));
  7896. }
  7897. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7898. {
  7899. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7900. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7901. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7902. u32 val = 0;
  7903. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  7904. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7905. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7906. val |= PIPECONF_INTERLACED_ILK;
  7907. else
  7908. val |= PIPECONF_PROGRESSIVE;
  7909. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7910. POSTING_READ(PIPECONF(cpu_transcoder));
  7911. }
  7912. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  7913. {
  7914. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7915. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7916. if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
  7917. u32 val = 0;
  7918. switch (intel_crtc->config->pipe_bpp) {
  7919. case 18:
  7920. val |= PIPEMISC_DITHER_6_BPC;
  7921. break;
  7922. case 24:
  7923. val |= PIPEMISC_DITHER_8_BPC;
  7924. break;
  7925. case 30:
  7926. val |= PIPEMISC_DITHER_10_BPC;
  7927. break;
  7928. case 36:
  7929. val |= PIPEMISC_DITHER_12_BPC;
  7930. break;
  7931. default:
  7932. /* Case prevented by pipe_config_set_bpp. */
  7933. BUG();
  7934. }
  7935. if (intel_crtc->config->dither)
  7936. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7937. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  7938. }
  7939. }
  7940. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7941. {
  7942. /*
  7943. * Account for spread spectrum to avoid
  7944. * oversubscribing the link. Max center spread
  7945. * is 2.5%; use 5% for safety's sake.
  7946. */
  7947. u32 bps = target_clock * bpp * 21 / 20;
  7948. return DIV_ROUND_UP(bps, link_bw * 8);
  7949. }
  7950. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7951. {
  7952. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7953. }
  7954. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7955. struct intel_crtc_state *crtc_state,
  7956. struct dpll *reduced_clock)
  7957. {
  7958. struct drm_crtc *crtc = &intel_crtc->base;
  7959. struct drm_device *dev = crtc->dev;
  7960. struct drm_i915_private *dev_priv = to_i915(dev);
  7961. u32 dpll, fp, fp2;
  7962. int factor;
  7963. /* Enable autotuning of the PLL clock (if permissible) */
  7964. factor = 21;
  7965. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7966. if ((intel_panel_use_ssc(dev_priv) &&
  7967. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7968. (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
  7969. factor = 25;
  7970. } else if (crtc_state->sdvo_tv_clock)
  7971. factor = 20;
  7972. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7973. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7974. fp |= FP_CB_TUNE;
  7975. if (reduced_clock) {
  7976. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  7977. if (reduced_clock->m < factor * reduced_clock->n)
  7978. fp2 |= FP_CB_TUNE;
  7979. } else {
  7980. fp2 = fp;
  7981. }
  7982. dpll = 0;
  7983. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  7984. dpll |= DPLLB_MODE_LVDS;
  7985. else
  7986. dpll |= DPLLB_MODE_DAC_SERIAL;
  7987. dpll |= (crtc_state->pixel_multiplier - 1)
  7988. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7989. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  7990. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  7991. dpll |= DPLL_SDVO_HIGH_SPEED;
  7992. if (intel_crtc_has_dp_encoder(crtc_state))
  7993. dpll |= DPLL_SDVO_HIGH_SPEED;
  7994. /*
  7995. * The high speed IO clock is only really required for
  7996. * SDVO/HDMI/DP, but we also enable it for CRT to make it
  7997. * possible to share the DPLL between CRT and HDMI. Enabling
  7998. * the clock needlessly does no real harm, except use up a
  7999. * bit of power potentially.
  8000. *
  8001. * We'll limit this to IVB with 3 pipes, since it has only two
  8002. * DPLLs and so DPLL sharing is the only way to get three pipes
  8003. * driving PCH ports at the same time. On SNB we could do this,
  8004. * and potentially avoid enabling the second DPLL, but it's not
  8005. * clear if it''s a win or loss power wise. No point in doing
  8006. * this on ILK at all since it has a fixed DPLL<->pipe mapping.
  8007. */
  8008. if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
  8009. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
  8010. dpll |= DPLL_SDVO_HIGH_SPEED;
  8011. /* compute bitmask from p1 value */
  8012. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  8013. /* also FPA1 */
  8014. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  8015. switch (crtc_state->dpll.p2) {
  8016. case 5:
  8017. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  8018. break;
  8019. case 7:
  8020. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  8021. break;
  8022. case 10:
  8023. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  8024. break;
  8025. case 14:
  8026. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  8027. break;
  8028. }
  8029. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  8030. intel_panel_use_ssc(dev_priv))
  8031. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  8032. else
  8033. dpll |= PLL_REF_INPUT_DREFCLK;
  8034. dpll |= DPLL_VCO_ENABLE;
  8035. crtc_state->dpll_hw_state.dpll = dpll;
  8036. crtc_state->dpll_hw_state.fp0 = fp;
  8037. crtc_state->dpll_hw_state.fp1 = fp2;
  8038. }
  8039. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  8040. struct intel_crtc_state *crtc_state)
  8041. {
  8042. struct drm_device *dev = crtc->base.dev;
  8043. struct drm_i915_private *dev_priv = to_i915(dev);
  8044. struct dpll reduced_clock;
  8045. bool has_reduced_clock = false;
  8046. struct intel_shared_dpll *pll;
  8047. const struct intel_limit *limit;
  8048. int refclk = 120000;
  8049. memset(&crtc_state->dpll_hw_state, 0,
  8050. sizeof(crtc_state->dpll_hw_state));
  8051. crtc->lowfreq_avail = false;
  8052. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  8053. if (!crtc_state->has_pch_encoder)
  8054. return 0;
  8055. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  8056. if (intel_panel_use_ssc(dev_priv)) {
  8057. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  8058. dev_priv->vbt.lvds_ssc_freq);
  8059. refclk = dev_priv->vbt.lvds_ssc_freq;
  8060. }
  8061. if (intel_is_dual_link_lvds(dev)) {
  8062. if (refclk == 100000)
  8063. limit = &intel_limits_ironlake_dual_lvds_100m;
  8064. else
  8065. limit = &intel_limits_ironlake_dual_lvds;
  8066. } else {
  8067. if (refclk == 100000)
  8068. limit = &intel_limits_ironlake_single_lvds_100m;
  8069. else
  8070. limit = &intel_limits_ironlake_single_lvds;
  8071. }
  8072. } else {
  8073. limit = &intel_limits_ironlake_dac;
  8074. }
  8075. if (!crtc_state->clock_set &&
  8076. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  8077. refclk, NULL, &crtc_state->dpll)) {
  8078. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  8079. return -EINVAL;
  8080. }
  8081. ironlake_compute_dpll(crtc, crtc_state,
  8082. has_reduced_clock ? &reduced_clock : NULL);
  8083. pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
  8084. if (pll == NULL) {
  8085. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  8086. pipe_name(crtc->pipe));
  8087. return -EINVAL;
  8088. }
  8089. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  8090. has_reduced_clock)
  8091. crtc->lowfreq_avail = true;
  8092. return 0;
  8093. }
  8094. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  8095. struct intel_link_m_n *m_n)
  8096. {
  8097. struct drm_device *dev = crtc->base.dev;
  8098. struct drm_i915_private *dev_priv = to_i915(dev);
  8099. enum pipe pipe = crtc->pipe;
  8100. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  8101. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  8102. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  8103. & ~TU_SIZE_MASK;
  8104. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  8105. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  8106. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8107. }
  8108. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  8109. enum transcoder transcoder,
  8110. struct intel_link_m_n *m_n,
  8111. struct intel_link_m_n *m2_n2)
  8112. {
  8113. struct drm_device *dev = crtc->base.dev;
  8114. struct drm_i915_private *dev_priv = to_i915(dev);
  8115. enum pipe pipe = crtc->pipe;
  8116. if (INTEL_INFO(dev)->gen >= 5) {
  8117. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  8118. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  8119. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  8120. & ~TU_SIZE_MASK;
  8121. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  8122. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  8123. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8124. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  8125. * gen < 8) and if DRRS is supported (to make sure the
  8126. * registers are not unnecessarily read).
  8127. */
  8128. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  8129. crtc->config->has_drrs) {
  8130. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  8131. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  8132. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  8133. & ~TU_SIZE_MASK;
  8134. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  8135. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  8136. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8137. }
  8138. } else {
  8139. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  8140. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  8141. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  8142. & ~TU_SIZE_MASK;
  8143. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  8144. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  8145. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8146. }
  8147. }
  8148. void intel_dp_get_m_n(struct intel_crtc *crtc,
  8149. struct intel_crtc_state *pipe_config)
  8150. {
  8151. if (pipe_config->has_pch_encoder)
  8152. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  8153. else
  8154. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  8155. &pipe_config->dp_m_n,
  8156. &pipe_config->dp_m2_n2);
  8157. }
  8158. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  8159. struct intel_crtc_state *pipe_config)
  8160. {
  8161. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  8162. &pipe_config->fdi_m_n, NULL);
  8163. }
  8164. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  8165. struct intel_crtc_state *pipe_config)
  8166. {
  8167. struct drm_device *dev = crtc->base.dev;
  8168. struct drm_i915_private *dev_priv = to_i915(dev);
  8169. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  8170. uint32_t ps_ctrl = 0;
  8171. int id = -1;
  8172. int i;
  8173. /* find scaler attached to this pipe */
  8174. for (i = 0; i < crtc->num_scalers; i++) {
  8175. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  8176. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  8177. id = i;
  8178. pipe_config->pch_pfit.enabled = true;
  8179. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  8180. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  8181. break;
  8182. }
  8183. }
  8184. scaler_state->scaler_id = id;
  8185. if (id >= 0) {
  8186. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  8187. } else {
  8188. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8189. }
  8190. }
  8191. static void
  8192. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  8193. struct intel_initial_plane_config *plane_config)
  8194. {
  8195. struct drm_device *dev = crtc->base.dev;
  8196. struct drm_i915_private *dev_priv = to_i915(dev);
  8197. u32 val, base, offset, stride_mult, tiling;
  8198. int pipe = crtc->pipe;
  8199. int fourcc, pixel_format;
  8200. unsigned int aligned_height;
  8201. struct drm_framebuffer *fb;
  8202. struct intel_framebuffer *intel_fb;
  8203. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8204. if (!intel_fb) {
  8205. DRM_DEBUG_KMS("failed to alloc fb\n");
  8206. return;
  8207. }
  8208. fb = &intel_fb->base;
  8209. val = I915_READ(PLANE_CTL(pipe, 0));
  8210. if (!(val & PLANE_CTL_ENABLE))
  8211. goto error;
  8212. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  8213. fourcc = skl_format_to_fourcc(pixel_format,
  8214. val & PLANE_CTL_ORDER_RGBX,
  8215. val & PLANE_CTL_ALPHA_MASK);
  8216. fb->pixel_format = fourcc;
  8217. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  8218. tiling = val & PLANE_CTL_TILED_MASK;
  8219. switch (tiling) {
  8220. case PLANE_CTL_TILED_LINEAR:
  8221. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  8222. break;
  8223. case PLANE_CTL_TILED_X:
  8224. plane_config->tiling = I915_TILING_X;
  8225. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  8226. break;
  8227. case PLANE_CTL_TILED_Y:
  8228. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  8229. break;
  8230. case PLANE_CTL_TILED_YF:
  8231. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  8232. break;
  8233. default:
  8234. MISSING_CASE(tiling);
  8235. goto error;
  8236. }
  8237. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  8238. plane_config->base = base;
  8239. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  8240. val = I915_READ(PLANE_SIZE(pipe, 0));
  8241. fb->height = ((val >> 16) & 0xfff) + 1;
  8242. fb->width = ((val >> 0) & 0x1fff) + 1;
  8243. val = I915_READ(PLANE_STRIDE(pipe, 0));
  8244. stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  8245. fb->pixel_format);
  8246. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  8247. aligned_height = intel_fb_align_height(dev, fb->height,
  8248. fb->pixel_format,
  8249. fb->modifier[0]);
  8250. plane_config->size = fb->pitches[0] * aligned_height;
  8251. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  8252. pipe_name(pipe), fb->width, fb->height,
  8253. fb->bits_per_pixel, base, fb->pitches[0],
  8254. plane_config->size);
  8255. plane_config->fb = intel_fb;
  8256. return;
  8257. error:
  8258. kfree(intel_fb);
  8259. }
  8260. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  8261. struct intel_crtc_state *pipe_config)
  8262. {
  8263. struct drm_device *dev = crtc->base.dev;
  8264. struct drm_i915_private *dev_priv = to_i915(dev);
  8265. uint32_t tmp;
  8266. tmp = I915_READ(PF_CTL(crtc->pipe));
  8267. if (tmp & PF_ENABLE) {
  8268. pipe_config->pch_pfit.enabled = true;
  8269. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  8270. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  8271. /* We currently do not free assignements of panel fitters on
  8272. * ivb/hsw (since we don't use the higher upscaling modes which
  8273. * differentiates them) so just WARN about this case for now. */
  8274. if (IS_GEN7(dev_priv)) {
  8275. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  8276. PF_PIPE_SEL_IVB(crtc->pipe));
  8277. }
  8278. }
  8279. }
  8280. static void
  8281. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  8282. struct intel_initial_plane_config *plane_config)
  8283. {
  8284. struct drm_device *dev = crtc->base.dev;
  8285. struct drm_i915_private *dev_priv = to_i915(dev);
  8286. u32 val, base, offset;
  8287. int pipe = crtc->pipe;
  8288. int fourcc, pixel_format;
  8289. unsigned int aligned_height;
  8290. struct drm_framebuffer *fb;
  8291. struct intel_framebuffer *intel_fb;
  8292. val = I915_READ(DSPCNTR(pipe));
  8293. if (!(val & DISPLAY_PLANE_ENABLE))
  8294. return;
  8295. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8296. if (!intel_fb) {
  8297. DRM_DEBUG_KMS("failed to alloc fb\n");
  8298. return;
  8299. }
  8300. fb = &intel_fb->base;
  8301. if (INTEL_INFO(dev)->gen >= 4) {
  8302. if (val & DISPPLANE_TILED) {
  8303. plane_config->tiling = I915_TILING_X;
  8304. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  8305. }
  8306. }
  8307. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  8308. fourcc = i9xx_format_to_fourcc(pixel_format);
  8309. fb->pixel_format = fourcc;
  8310. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  8311. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  8312. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  8313. offset = I915_READ(DSPOFFSET(pipe));
  8314. } else {
  8315. if (plane_config->tiling)
  8316. offset = I915_READ(DSPTILEOFF(pipe));
  8317. else
  8318. offset = I915_READ(DSPLINOFF(pipe));
  8319. }
  8320. plane_config->base = base;
  8321. val = I915_READ(PIPESRC(pipe));
  8322. fb->width = ((val >> 16) & 0xfff) + 1;
  8323. fb->height = ((val >> 0) & 0xfff) + 1;
  8324. val = I915_READ(DSPSTRIDE(pipe));
  8325. fb->pitches[0] = val & 0xffffffc0;
  8326. aligned_height = intel_fb_align_height(dev, fb->height,
  8327. fb->pixel_format,
  8328. fb->modifier[0]);
  8329. plane_config->size = fb->pitches[0] * aligned_height;
  8330. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  8331. pipe_name(pipe), fb->width, fb->height,
  8332. fb->bits_per_pixel, base, fb->pitches[0],
  8333. plane_config->size);
  8334. plane_config->fb = intel_fb;
  8335. }
  8336. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  8337. struct intel_crtc_state *pipe_config)
  8338. {
  8339. struct drm_device *dev = crtc->base.dev;
  8340. struct drm_i915_private *dev_priv = to_i915(dev);
  8341. enum intel_display_power_domain power_domain;
  8342. uint32_t tmp;
  8343. bool ret;
  8344. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  8345. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8346. return false;
  8347. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8348. pipe_config->shared_dpll = NULL;
  8349. ret = false;
  8350. tmp = I915_READ(PIPECONF(crtc->pipe));
  8351. if (!(tmp & PIPECONF_ENABLE))
  8352. goto out;
  8353. switch (tmp & PIPECONF_BPC_MASK) {
  8354. case PIPECONF_6BPC:
  8355. pipe_config->pipe_bpp = 18;
  8356. break;
  8357. case PIPECONF_8BPC:
  8358. pipe_config->pipe_bpp = 24;
  8359. break;
  8360. case PIPECONF_10BPC:
  8361. pipe_config->pipe_bpp = 30;
  8362. break;
  8363. case PIPECONF_12BPC:
  8364. pipe_config->pipe_bpp = 36;
  8365. break;
  8366. default:
  8367. break;
  8368. }
  8369. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  8370. pipe_config->limited_color_range = true;
  8371. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  8372. struct intel_shared_dpll *pll;
  8373. enum intel_dpll_id pll_id;
  8374. pipe_config->has_pch_encoder = true;
  8375. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  8376. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8377. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8378. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8379. if (HAS_PCH_IBX(dev_priv)) {
  8380. /*
  8381. * The pipe->pch transcoder and pch transcoder->pll
  8382. * mapping is fixed.
  8383. */
  8384. pll_id = (enum intel_dpll_id) crtc->pipe;
  8385. } else {
  8386. tmp = I915_READ(PCH_DPLL_SEL);
  8387. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  8388. pll_id = DPLL_ID_PCH_PLL_B;
  8389. else
  8390. pll_id= DPLL_ID_PCH_PLL_A;
  8391. }
  8392. pipe_config->shared_dpll =
  8393. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  8394. pll = pipe_config->shared_dpll;
  8395. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  8396. &pipe_config->dpll_hw_state));
  8397. tmp = pipe_config->dpll_hw_state.dpll;
  8398. pipe_config->pixel_multiplier =
  8399. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  8400. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  8401. ironlake_pch_clock_get(crtc, pipe_config);
  8402. } else {
  8403. pipe_config->pixel_multiplier = 1;
  8404. }
  8405. intel_get_pipe_timings(crtc, pipe_config);
  8406. intel_get_pipe_src_size(crtc, pipe_config);
  8407. ironlake_get_pfit_config(crtc, pipe_config);
  8408. ret = true;
  8409. out:
  8410. intel_display_power_put(dev_priv, power_domain);
  8411. return ret;
  8412. }
  8413. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  8414. {
  8415. struct drm_device *dev = &dev_priv->drm;
  8416. struct intel_crtc *crtc;
  8417. for_each_intel_crtc(dev, crtc)
  8418. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  8419. pipe_name(crtc->pipe));
  8420. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  8421. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  8422. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  8423. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  8424. I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
  8425. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  8426. "CPU PWM1 enabled\n");
  8427. if (IS_HASWELL(dev_priv))
  8428. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  8429. "CPU PWM2 enabled\n");
  8430. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  8431. "PCH PWM1 enabled\n");
  8432. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  8433. "Utility pin enabled\n");
  8434. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  8435. /*
  8436. * In theory we can still leave IRQs enabled, as long as only the HPD
  8437. * interrupts remain enabled. We used to check for that, but since it's
  8438. * gen-specific and since we only disable LCPLL after we fully disable
  8439. * the interrupts, the check below should be enough.
  8440. */
  8441. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  8442. }
  8443. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  8444. {
  8445. if (IS_HASWELL(dev_priv))
  8446. return I915_READ(D_COMP_HSW);
  8447. else
  8448. return I915_READ(D_COMP_BDW);
  8449. }
  8450. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  8451. {
  8452. if (IS_HASWELL(dev_priv)) {
  8453. mutex_lock(&dev_priv->rps.hw_lock);
  8454. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  8455. val))
  8456. DRM_DEBUG_KMS("Failed to write to D_COMP\n");
  8457. mutex_unlock(&dev_priv->rps.hw_lock);
  8458. } else {
  8459. I915_WRITE(D_COMP_BDW, val);
  8460. POSTING_READ(D_COMP_BDW);
  8461. }
  8462. }
  8463. /*
  8464. * This function implements pieces of two sequences from BSpec:
  8465. * - Sequence for display software to disable LCPLL
  8466. * - Sequence for display software to allow package C8+
  8467. * The steps implemented here are just the steps that actually touch the LCPLL
  8468. * register. Callers should take care of disabling all the display engine
  8469. * functions, doing the mode unset, fixing interrupts, etc.
  8470. */
  8471. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  8472. bool switch_to_fclk, bool allow_power_down)
  8473. {
  8474. uint32_t val;
  8475. assert_can_disable_lcpll(dev_priv);
  8476. val = I915_READ(LCPLL_CTL);
  8477. if (switch_to_fclk) {
  8478. val |= LCPLL_CD_SOURCE_FCLK;
  8479. I915_WRITE(LCPLL_CTL, val);
  8480. if (wait_for_us(I915_READ(LCPLL_CTL) &
  8481. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8482. DRM_ERROR("Switching to FCLK failed\n");
  8483. val = I915_READ(LCPLL_CTL);
  8484. }
  8485. val |= LCPLL_PLL_DISABLE;
  8486. I915_WRITE(LCPLL_CTL, val);
  8487. POSTING_READ(LCPLL_CTL);
  8488. if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
  8489. DRM_ERROR("LCPLL still locked\n");
  8490. val = hsw_read_dcomp(dev_priv);
  8491. val |= D_COMP_COMP_DISABLE;
  8492. hsw_write_dcomp(dev_priv, val);
  8493. ndelay(100);
  8494. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  8495. 1))
  8496. DRM_ERROR("D_COMP RCOMP still in progress\n");
  8497. if (allow_power_down) {
  8498. val = I915_READ(LCPLL_CTL);
  8499. val |= LCPLL_POWER_DOWN_ALLOW;
  8500. I915_WRITE(LCPLL_CTL, val);
  8501. POSTING_READ(LCPLL_CTL);
  8502. }
  8503. }
  8504. /*
  8505. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  8506. * source.
  8507. */
  8508. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  8509. {
  8510. uint32_t val;
  8511. val = I915_READ(LCPLL_CTL);
  8512. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  8513. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  8514. return;
  8515. /*
  8516. * Make sure we're not on PC8 state before disabling PC8, otherwise
  8517. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  8518. */
  8519. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  8520. if (val & LCPLL_POWER_DOWN_ALLOW) {
  8521. val &= ~LCPLL_POWER_DOWN_ALLOW;
  8522. I915_WRITE(LCPLL_CTL, val);
  8523. POSTING_READ(LCPLL_CTL);
  8524. }
  8525. val = hsw_read_dcomp(dev_priv);
  8526. val |= D_COMP_COMP_FORCE;
  8527. val &= ~D_COMP_COMP_DISABLE;
  8528. hsw_write_dcomp(dev_priv, val);
  8529. val = I915_READ(LCPLL_CTL);
  8530. val &= ~LCPLL_PLL_DISABLE;
  8531. I915_WRITE(LCPLL_CTL, val);
  8532. if (intel_wait_for_register(dev_priv,
  8533. LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  8534. 5))
  8535. DRM_ERROR("LCPLL not locked yet\n");
  8536. if (val & LCPLL_CD_SOURCE_FCLK) {
  8537. val = I915_READ(LCPLL_CTL);
  8538. val &= ~LCPLL_CD_SOURCE_FCLK;
  8539. I915_WRITE(LCPLL_CTL, val);
  8540. if (wait_for_us((I915_READ(LCPLL_CTL) &
  8541. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8542. DRM_ERROR("Switching back to LCPLL failed\n");
  8543. }
  8544. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  8545. intel_update_cdclk(dev_priv);
  8546. }
  8547. /*
  8548. * Package states C8 and deeper are really deep PC states that can only be
  8549. * reached when all the devices on the system allow it, so even if the graphics
  8550. * device allows PC8+, it doesn't mean the system will actually get to these
  8551. * states. Our driver only allows PC8+ when going into runtime PM.
  8552. *
  8553. * The requirements for PC8+ are that all the outputs are disabled, the power
  8554. * well is disabled and most interrupts are disabled, and these are also
  8555. * requirements for runtime PM. When these conditions are met, we manually do
  8556. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  8557. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  8558. * hang the machine.
  8559. *
  8560. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  8561. * the state of some registers, so when we come back from PC8+ we need to
  8562. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  8563. * need to take care of the registers kept by RC6. Notice that this happens even
  8564. * if we don't put the device in PCI D3 state (which is what currently happens
  8565. * because of the runtime PM support).
  8566. *
  8567. * For more, read "Display Sequences for Package C8" on the hardware
  8568. * documentation.
  8569. */
  8570. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  8571. {
  8572. struct drm_device *dev = &dev_priv->drm;
  8573. uint32_t val;
  8574. DRM_DEBUG_KMS("Enabling package C8+\n");
  8575. if (HAS_PCH_LPT_LP(dev_priv)) {
  8576. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8577. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  8578. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8579. }
  8580. lpt_disable_clkout_dp(dev);
  8581. hsw_disable_lcpll(dev_priv, true, true);
  8582. }
  8583. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  8584. {
  8585. struct drm_device *dev = &dev_priv->drm;
  8586. uint32_t val;
  8587. DRM_DEBUG_KMS("Disabling package C8+\n");
  8588. hsw_restore_lcpll(dev_priv);
  8589. lpt_init_pch_refclk(dev);
  8590. if (HAS_PCH_LPT_LP(dev_priv)) {
  8591. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8592. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  8593. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8594. }
  8595. }
  8596. static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8597. {
  8598. struct drm_device *dev = old_state->dev;
  8599. struct intel_atomic_state *old_intel_state =
  8600. to_intel_atomic_state(old_state);
  8601. unsigned int req_cdclk = old_intel_state->dev_cdclk;
  8602. bxt_set_cdclk(to_i915(dev), req_cdclk);
  8603. }
  8604. static int bdw_adjust_min_pipe_pixel_rate(struct intel_crtc_state *crtc_state,
  8605. int pixel_rate)
  8606. {
  8607. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  8608. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  8609. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  8610. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  8611. /* BSpec says "Do not use DisplayPort with CDCLK less than
  8612. * 432 MHz, audio enabled, port width x4, and link rate
  8613. * HBR2 (5.4 GHz), or else there may be audio corruption or
  8614. * screen corruption."
  8615. */
  8616. if (intel_crtc_has_dp_encoder(crtc_state) &&
  8617. crtc_state->has_audio &&
  8618. crtc_state->port_clock >= 540000 &&
  8619. crtc_state->lane_count == 4)
  8620. pixel_rate = max(432000, pixel_rate);
  8621. return pixel_rate;
  8622. }
  8623. /* compute the max rate for new configuration */
  8624. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  8625. {
  8626. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8627. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8628. struct drm_crtc *crtc;
  8629. struct drm_crtc_state *cstate;
  8630. struct intel_crtc_state *crtc_state;
  8631. unsigned max_pixel_rate = 0, i;
  8632. enum pipe pipe;
  8633. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  8634. sizeof(intel_state->min_pixclk));
  8635. for_each_crtc_in_state(state, crtc, cstate, i) {
  8636. int pixel_rate;
  8637. crtc_state = to_intel_crtc_state(cstate);
  8638. if (!crtc_state->base.enable) {
  8639. intel_state->min_pixclk[i] = 0;
  8640. continue;
  8641. }
  8642. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  8643. if (IS_BROADWELL(dev_priv) || IS_GEN9(dev_priv))
  8644. pixel_rate = bdw_adjust_min_pipe_pixel_rate(crtc_state,
  8645. pixel_rate);
  8646. intel_state->min_pixclk[i] = pixel_rate;
  8647. }
  8648. for_each_pipe(dev_priv, pipe)
  8649. max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
  8650. return max_pixel_rate;
  8651. }
  8652. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  8653. {
  8654. struct drm_i915_private *dev_priv = to_i915(dev);
  8655. uint32_t val, data;
  8656. int ret;
  8657. if (WARN((I915_READ(LCPLL_CTL) &
  8658. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  8659. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  8660. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  8661. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  8662. "trying to change cdclk frequency with cdclk not enabled\n"))
  8663. return;
  8664. mutex_lock(&dev_priv->rps.hw_lock);
  8665. ret = sandybridge_pcode_write(dev_priv,
  8666. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  8667. mutex_unlock(&dev_priv->rps.hw_lock);
  8668. if (ret) {
  8669. DRM_ERROR("failed to inform pcode about cdclk change\n");
  8670. return;
  8671. }
  8672. val = I915_READ(LCPLL_CTL);
  8673. val |= LCPLL_CD_SOURCE_FCLK;
  8674. I915_WRITE(LCPLL_CTL, val);
  8675. if (wait_for_us(I915_READ(LCPLL_CTL) &
  8676. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8677. DRM_ERROR("Switching to FCLK failed\n");
  8678. val = I915_READ(LCPLL_CTL);
  8679. val &= ~LCPLL_CLK_FREQ_MASK;
  8680. switch (cdclk) {
  8681. case 450000:
  8682. val |= LCPLL_CLK_FREQ_450;
  8683. data = 0;
  8684. break;
  8685. case 540000:
  8686. val |= LCPLL_CLK_FREQ_54O_BDW;
  8687. data = 1;
  8688. break;
  8689. case 337500:
  8690. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8691. data = 2;
  8692. break;
  8693. case 675000:
  8694. val |= LCPLL_CLK_FREQ_675_BDW;
  8695. data = 3;
  8696. break;
  8697. default:
  8698. WARN(1, "invalid cdclk frequency\n");
  8699. return;
  8700. }
  8701. I915_WRITE(LCPLL_CTL, val);
  8702. val = I915_READ(LCPLL_CTL);
  8703. val &= ~LCPLL_CD_SOURCE_FCLK;
  8704. I915_WRITE(LCPLL_CTL, val);
  8705. if (wait_for_us((I915_READ(LCPLL_CTL) &
  8706. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8707. DRM_ERROR("Switching back to LCPLL failed\n");
  8708. mutex_lock(&dev_priv->rps.hw_lock);
  8709. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8710. mutex_unlock(&dev_priv->rps.hw_lock);
  8711. I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
  8712. intel_update_cdclk(dev_priv);
  8713. WARN(cdclk != dev_priv->cdclk_freq,
  8714. "cdclk requested %d kHz but got %d kHz\n",
  8715. cdclk, dev_priv->cdclk_freq);
  8716. }
  8717. static int broadwell_calc_cdclk(int max_pixclk)
  8718. {
  8719. if (max_pixclk > 540000)
  8720. return 675000;
  8721. else if (max_pixclk > 450000)
  8722. return 540000;
  8723. else if (max_pixclk > 337500)
  8724. return 450000;
  8725. else
  8726. return 337500;
  8727. }
  8728. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8729. {
  8730. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8731. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8732. int max_pixclk = ilk_max_pixel_rate(state);
  8733. int cdclk;
  8734. /*
  8735. * FIXME should also account for plane ratio
  8736. * once 64bpp pixel formats are supported.
  8737. */
  8738. cdclk = broadwell_calc_cdclk(max_pixclk);
  8739. if (cdclk > dev_priv->max_cdclk_freq) {
  8740. DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8741. cdclk, dev_priv->max_cdclk_freq);
  8742. return -EINVAL;
  8743. }
  8744. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8745. if (!intel_state->active_crtcs)
  8746. intel_state->dev_cdclk = broadwell_calc_cdclk(0);
  8747. return 0;
  8748. }
  8749. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8750. {
  8751. struct drm_device *dev = old_state->dev;
  8752. struct intel_atomic_state *old_intel_state =
  8753. to_intel_atomic_state(old_state);
  8754. unsigned req_cdclk = old_intel_state->dev_cdclk;
  8755. broadwell_set_cdclk(dev, req_cdclk);
  8756. }
  8757. static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
  8758. {
  8759. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8760. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8761. const int max_pixclk = ilk_max_pixel_rate(state);
  8762. int vco = intel_state->cdclk_pll_vco;
  8763. int cdclk;
  8764. /*
  8765. * FIXME should also account for plane ratio
  8766. * once 64bpp pixel formats are supported.
  8767. */
  8768. cdclk = skl_calc_cdclk(max_pixclk, vco);
  8769. /*
  8770. * FIXME move the cdclk caclulation to
  8771. * compute_config() so we can fail gracegully.
  8772. */
  8773. if (cdclk > dev_priv->max_cdclk_freq) {
  8774. DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8775. cdclk, dev_priv->max_cdclk_freq);
  8776. cdclk = dev_priv->max_cdclk_freq;
  8777. }
  8778. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8779. if (!intel_state->active_crtcs)
  8780. intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
  8781. return 0;
  8782. }
  8783. static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8784. {
  8785. struct drm_i915_private *dev_priv = to_i915(old_state->dev);
  8786. struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
  8787. unsigned int req_cdclk = intel_state->dev_cdclk;
  8788. unsigned int req_vco = intel_state->cdclk_pll_vco;
  8789. skl_set_cdclk(dev_priv, req_cdclk, req_vco);
  8790. }
  8791. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8792. struct intel_crtc_state *crtc_state)
  8793. {
  8794. if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
  8795. if (!intel_ddi_pll_select(crtc, crtc_state))
  8796. return -EINVAL;
  8797. }
  8798. crtc->lowfreq_avail = false;
  8799. return 0;
  8800. }
  8801. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8802. enum port port,
  8803. struct intel_crtc_state *pipe_config)
  8804. {
  8805. enum intel_dpll_id id;
  8806. switch (port) {
  8807. case PORT_A:
  8808. id = DPLL_ID_SKL_DPLL0;
  8809. break;
  8810. case PORT_B:
  8811. id = DPLL_ID_SKL_DPLL1;
  8812. break;
  8813. case PORT_C:
  8814. id = DPLL_ID_SKL_DPLL2;
  8815. break;
  8816. default:
  8817. DRM_ERROR("Incorrect port type\n");
  8818. return;
  8819. }
  8820. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8821. }
  8822. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8823. enum port port,
  8824. struct intel_crtc_state *pipe_config)
  8825. {
  8826. enum intel_dpll_id id;
  8827. u32 temp;
  8828. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8829. id = temp >> (port * 3 + 1);
  8830. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
  8831. return;
  8832. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8833. }
  8834. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8835. enum port port,
  8836. struct intel_crtc_state *pipe_config)
  8837. {
  8838. enum intel_dpll_id id;
  8839. uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8840. switch (ddi_pll_sel) {
  8841. case PORT_CLK_SEL_WRPLL1:
  8842. id = DPLL_ID_WRPLL1;
  8843. break;
  8844. case PORT_CLK_SEL_WRPLL2:
  8845. id = DPLL_ID_WRPLL2;
  8846. break;
  8847. case PORT_CLK_SEL_SPLL:
  8848. id = DPLL_ID_SPLL;
  8849. break;
  8850. case PORT_CLK_SEL_LCPLL_810:
  8851. id = DPLL_ID_LCPLL_810;
  8852. break;
  8853. case PORT_CLK_SEL_LCPLL_1350:
  8854. id = DPLL_ID_LCPLL_1350;
  8855. break;
  8856. case PORT_CLK_SEL_LCPLL_2700:
  8857. id = DPLL_ID_LCPLL_2700;
  8858. break;
  8859. default:
  8860. MISSING_CASE(ddi_pll_sel);
  8861. /* fall through */
  8862. case PORT_CLK_SEL_NONE:
  8863. return;
  8864. }
  8865. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8866. }
  8867. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  8868. struct intel_crtc_state *pipe_config,
  8869. unsigned long *power_domain_mask)
  8870. {
  8871. struct drm_device *dev = crtc->base.dev;
  8872. struct drm_i915_private *dev_priv = to_i915(dev);
  8873. enum intel_display_power_domain power_domain;
  8874. u32 tmp;
  8875. /*
  8876. * The pipe->transcoder mapping is fixed with the exception of the eDP
  8877. * transcoder handled below.
  8878. */
  8879. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8880. /*
  8881. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  8882. * consistency and less surprising code; it's in always on power).
  8883. */
  8884. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8885. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8886. enum pipe trans_edp_pipe;
  8887. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8888. default:
  8889. WARN(1, "unknown pipe linked to edp transcoder\n");
  8890. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8891. case TRANS_DDI_EDP_INPUT_A_ON:
  8892. trans_edp_pipe = PIPE_A;
  8893. break;
  8894. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8895. trans_edp_pipe = PIPE_B;
  8896. break;
  8897. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8898. trans_edp_pipe = PIPE_C;
  8899. break;
  8900. }
  8901. if (trans_edp_pipe == crtc->pipe)
  8902. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8903. }
  8904. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  8905. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8906. return false;
  8907. *power_domain_mask |= BIT(power_domain);
  8908. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8909. return tmp & PIPECONF_ENABLE;
  8910. }
  8911. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  8912. struct intel_crtc_state *pipe_config,
  8913. unsigned long *power_domain_mask)
  8914. {
  8915. struct drm_device *dev = crtc->base.dev;
  8916. struct drm_i915_private *dev_priv = to_i915(dev);
  8917. enum intel_display_power_domain power_domain;
  8918. enum port port;
  8919. enum transcoder cpu_transcoder;
  8920. u32 tmp;
  8921. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  8922. if (port == PORT_A)
  8923. cpu_transcoder = TRANSCODER_DSI_A;
  8924. else
  8925. cpu_transcoder = TRANSCODER_DSI_C;
  8926. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  8927. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8928. continue;
  8929. *power_domain_mask |= BIT(power_domain);
  8930. /*
  8931. * The PLL needs to be enabled with a valid divider
  8932. * configuration, otherwise accessing DSI registers will hang
  8933. * the machine. See BSpec North Display Engine
  8934. * registers/MIPI[BXT]. We can break out here early, since we
  8935. * need the same DSI PLL to be enabled for both DSI ports.
  8936. */
  8937. if (!intel_dsi_pll_is_enabled(dev_priv))
  8938. break;
  8939. /* XXX: this works for video mode only */
  8940. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  8941. if (!(tmp & DPI_ENABLE))
  8942. continue;
  8943. tmp = I915_READ(MIPI_CTRL(port));
  8944. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  8945. continue;
  8946. pipe_config->cpu_transcoder = cpu_transcoder;
  8947. break;
  8948. }
  8949. return transcoder_is_dsi(pipe_config->cpu_transcoder);
  8950. }
  8951. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8952. struct intel_crtc_state *pipe_config)
  8953. {
  8954. struct drm_device *dev = crtc->base.dev;
  8955. struct drm_i915_private *dev_priv = to_i915(dev);
  8956. struct intel_shared_dpll *pll;
  8957. enum port port;
  8958. uint32_t tmp;
  8959. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8960. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8961. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  8962. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8963. else if (IS_BROXTON(dev_priv))
  8964. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8965. else
  8966. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8967. pll = pipe_config->shared_dpll;
  8968. if (pll) {
  8969. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  8970. &pipe_config->dpll_hw_state));
  8971. }
  8972. /*
  8973. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8974. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8975. * the PCH transcoder is on.
  8976. */
  8977. if (INTEL_INFO(dev)->gen < 9 &&
  8978. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8979. pipe_config->has_pch_encoder = true;
  8980. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8981. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8982. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8983. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8984. }
  8985. }
  8986. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8987. struct intel_crtc_state *pipe_config)
  8988. {
  8989. struct drm_device *dev = crtc->base.dev;
  8990. struct drm_i915_private *dev_priv = to_i915(dev);
  8991. enum intel_display_power_domain power_domain;
  8992. unsigned long power_domain_mask;
  8993. bool active;
  8994. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  8995. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8996. return false;
  8997. power_domain_mask = BIT(power_domain);
  8998. pipe_config->shared_dpll = NULL;
  8999. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  9000. if (IS_BROXTON(dev_priv) &&
  9001. bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
  9002. WARN_ON(active);
  9003. active = true;
  9004. }
  9005. if (!active)
  9006. goto out;
  9007. if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  9008. haswell_get_ddi_port_state(crtc, pipe_config);
  9009. intel_get_pipe_timings(crtc, pipe_config);
  9010. }
  9011. intel_get_pipe_src_size(crtc, pipe_config);
  9012. pipe_config->gamma_mode =
  9013. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  9014. if (INTEL_INFO(dev)->gen >= 9) {
  9015. skl_init_scalers(dev_priv, crtc, pipe_config);
  9016. pipe_config->scaler_state.scaler_id = -1;
  9017. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  9018. }
  9019. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  9020. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  9021. power_domain_mask |= BIT(power_domain);
  9022. if (INTEL_INFO(dev)->gen >= 9)
  9023. skylake_get_pfit_config(crtc, pipe_config);
  9024. else
  9025. ironlake_get_pfit_config(crtc, pipe_config);
  9026. }
  9027. if (IS_HASWELL(dev_priv))
  9028. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  9029. (I915_READ(IPS_CTL) & IPS_ENABLE);
  9030. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  9031. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  9032. pipe_config->pixel_multiplier =
  9033. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  9034. } else {
  9035. pipe_config->pixel_multiplier = 1;
  9036. }
  9037. out:
  9038. for_each_power_domain(power_domain, power_domain_mask)
  9039. intel_display_power_put(dev_priv, power_domain);
  9040. return active;
  9041. }
  9042. static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
  9043. const struct intel_plane_state *plane_state)
  9044. {
  9045. struct drm_device *dev = crtc->dev;
  9046. struct drm_i915_private *dev_priv = to_i915(dev);
  9047. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9048. uint32_t cntl = 0, size = 0;
  9049. if (plane_state && plane_state->base.visible) {
  9050. unsigned int width = plane_state->base.crtc_w;
  9051. unsigned int height = plane_state->base.crtc_h;
  9052. unsigned int stride = roundup_pow_of_two(width) * 4;
  9053. switch (stride) {
  9054. default:
  9055. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  9056. width, stride);
  9057. stride = 256;
  9058. /* fallthrough */
  9059. case 256:
  9060. case 512:
  9061. case 1024:
  9062. case 2048:
  9063. break;
  9064. }
  9065. cntl |= CURSOR_ENABLE |
  9066. CURSOR_GAMMA_ENABLE |
  9067. CURSOR_FORMAT_ARGB |
  9068. CURSOR_STRIDE(stride);
  9069. size = (height << 12) | width;
  9070. }
  9071. if (intel_crtc->cursor_cntl != 0 &&
  9072. (intel_crtc->cursor_base != base ||
  9073. intel_crtc->cursor_size != size ||
  9074. intel_crtc->cursor_cntl != cntl)) {
  9075. /* On these chipsets we can only modify the base/size/stride
  9076. * whilst the cursor is disabled.
  9077. */
  9078. I915_WRITE(CURCNTR(PIPE_A), 0);
  9079. POSTING_READ(CURCNTR(PIPE_A));
  9080. intel_crtc->cursor_cntl = 0;
  9081. }
  9082. if (intel_crtc->cursor_base != base) {
  9083. I915_WRITE(CURBASE(PIPE_A), base);
  9084. intel_crtc->cursor_base = base;
  9085. }
  9086. if (intel_crtc->cursor_size != size) {
  9087. I915_WRITE(CURSIZE, size);
  9088. intel_crtc->cursor_size = size;
  9089. }
  9090. if (intel_crtc->cursor_cntl != cntl) {
  9091. I915_WRITE(CURCNTR(PIPE_A), cntl);
  9092. POSTING_READ(CURCNTR(PIPE_A));
  9093. intel_crtc->cursor_cntl = cntl;
  9094. }
  9095. }
  9096. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
  9097. const struct intel_plane_state *plane_state)
  9098. {
  9099. struct drm_device *dev = crtc->dev;
  9100. struct drm_i915_private *dev_priv = to_i915(dev);
  9101. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9102. struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
  9103. const struct skl_wm_values *wm = &dev_priv->wm.skl_results;
  9104. const struct skl_plane_wm *p_wm =
  9105. &cstate->wm.skl.optimal.planes[PLANE_CURSOR];
  9106. int pipe = intel_crtc->pipe;
  9107. uint32_t cntl = 0;
  9108. if (INTEL_GEN(dev_priv) >= 9 && wm->dirty_pipes & drm_crtc_mask(crtc))
  9109. skl_write_cursor_wm(intel_crtc, p_wm, &wm->ddb);
  9110. if (plane_state && plane_state->base.visible) {
  9111. cntl = MCURSOR_GAMMA_ENABLE;
  9112. switch (plane_state->base.crtc_w) {
  9113. case 64:
  9114. cntl |= CURSOR_MODE_64_ARGB_AX;
  9115. break;
  9116. case 128:
  9117. cntl |= CURSOR_MODE_128_ARGB_AX;
  9118. break;
  9119. case 256:
  9120. cntl |= CURSOR_MODE_256_ARGB_AX;
  9121. break;
  9122. default:
  9123. MISSING_CASE(plane_state->base.crtc_w);
  9124. return;
  9125. }
  9126. cntl |= pipe << 28; /* Connect to correct pipe */
  9127. if (HAS_DDI(dev_priv))
  9128. cntl |= CURSOR_PIPE_CSC_ENABLE;
  9129. if (plane_state->base.rotation & DRM_ROTATE_180)
  9130. cntl |= CURSOR_ROTATE_180;
  9131. }
  9132. if (intel_crtc->cursor_cntl != cntl) {
  9133. I915_WRITE(CURCNTR(pipe), cntl);
  9134. POSTING_READ(CURCNTR(pipe));
  9135. intel_crtc->cursor_cntl = cntl;
  9136. }
  9137. /* and commit changes on next vblank */
  9138. I915_WRITE(CURBASE(pipe), base);
  9139. POSTING_READ(CURBASE(pipe));
  9140. intel_crtc->cursor_base = base;
  9141. }
  9142. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  9143. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  9144. const struct intel_plane_state *plane_state)
  9145. {
  9146. struct drm_device *dev = crtc->dev;
  9147. struct drm_i915_private *dev_priv = to_i915(dev);
  9148. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9149. int pipe = intel_crtc->pipe;
  9150. u32 base = intel_crtc->cursor_addr;
  9151. u32 pos = 0;
  9152. if (plane_state) {
  9153. int x = plane_state->base.crtc_x;
  9154. int y = plane_state->base.crtc_y;
  9155. if (x < 0) {
  9156. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  9157. x = -x;
  9158. }
  9159. pos |= x << CURSOR_X_SHIFT;
  9160. if (y < 0) {
  9161. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  9162. y = -y;
  9163. }
  9164. pos |= y << CURSOR_Y_SHIFT;
  9165. /* ILK+ do this automagically */
  9166. if (HAS_GMCH_DISPLAY(dev_priv) &&
  9167. plane_state->base.rotation & DRM_ROTATE_180) {
  9168. base += (plane_state->base.crtc_h *
  9169. plane_state->base.crtc_w - 1) * 4;
  9170. }
  9171. }
  9172. I915_WRITE(CURPOS(pipe), pos);
  9173. if (IS_845G(dev_priv) || IS_I865G(dev_priv))
  9174. i845_update_cursor(crtc, base, plane_state);
  9175. else
  9176. i9xx_update_cursor(crtc, base, plane_state);
  9177. }
  9178. static bool cursor_size_ok(struct drm_i915_private *dev_priv,
  9179. uint32_t width, uint32_t height)
  9180. {
  9181. if (width == 0 || height == 0)
  9182. return false;
  9183. /*
  9184. * 845g/865g are special in that they are only limited by
  9185. * the width of their cursors, the height is arbitrary up to
  9186. * the precision of the register. Everything else requires
  9187. * square cursors, limited to a few power-of-two sizes.
  9188. */
  9189. if (IS_845G(dev_priv) || IS_I865G(dev_priv)) {
  9190. if ((width & 63) != 0)
  9191. return false;
  9192. if (width > (IS_845G(dev_priv) ? 64 : 512))
  9193. return false;
  9194. if (height > 1023)
  9195. return false;
  9196. } else {
  9197. switch (width | height) {
  9198. case 256:
  9199. case 128:
  9200. if (IS_GEN2(dev_priv))
  9201. return false;
  9202. case 64:
  9203. break;
  9204. default:
  9205. return false;
  9206. }
  9207. }
  9208. return true;
  9209. }
  9210. /* VESA 640x480x72Hz mode to set on the pipe */
  9211. static struct drm_display_mode load_detect_mode = {
  9212. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  9213. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  9214. };
  9215. struct drm_framebuffer *
  9216. __intel_framebuffer_create(struct drm_device *dev,
  9217. struct drm_mode_fb_cmd2 *mode_cmd,
  9218. struct drm_i915_gem_object *obj)
  9219. {
  9220. struct intel_framebuffer *intel_fb;
  9221. int ret;
  9222. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  9223. if (!intel_fb)
  9224. return ERR_PTR(-ENOMEM);
  9225. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  9226. if (ret)
  9227. goto err;
  9228. return &intel_fb->base;
  9229. err:
  9230. kfree(intel_fb);
  9231. return ERR_PTR(ret);
  9232. }
  9233. static struct drm_framebuffer *
  9234. intel_framebuffer_create(struct drm_device *dev,
  9235. struct drm_mode_fb_cmd2 *mode_cmd,
  9236. struct drm_i915_gem_object *obj)
  9237. {
  9238. struct drm_framebuffer *fb;
  9239. int ret;
  9240. ret = i915_mutex_lock_interruptible(dev);
  9241. if (ret)
  9242. return ERR_PTR(ret);
  9243. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  9244. mutex_unlock(&dev->struct_mutex);
  9245. return fb;
  9246. }
  9247. static u32
  9248. intel_framebuffer_pitch_for_width(int width, int bpp)
  9249. {
  9250. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  9251. return ALIGN(pitch, 64);
  9252. }
  9253. static u32
  9254. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  9255. {
  9256. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  9257. return PAGE_ALIGN(pitch * mode->vdisplay);
  9258. }
  9259. static struct drm_framebuffer *
  9260. intel_framebuffer_create_for_mode(struct drm_device *dev,
  9261. struct drm_display_mode *mode,
  9262. int depth, int bpp)
  9263. {
  9264. struct drm_framebuffer *fb;
  9265. struct drm_i915_gem_object *obj;
  9266. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  9267. obj = i915_gem_object_create(dev,
  9268. intel_framebuffer_size_for_mode(mode, bpp));
  9269. if (IS_ERR(obj))
  9270. return ERR_CAST(obj);
  9271. mode_cmd.width = mode->hdisplay;
  9272. mode_cmd.height = mode->vdisplay;
  9273. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  9274. bpp);
  9275. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  9276. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  9277. if (IS_ERR(fb))
  9278. i915_gem_object_put(obj);
  9279. return fb;
  9280. }
  9281. static struct drm_framebuffer *
  9282. mode_fits_in_fbdev(struct drm_device *dev,
  9283. struct drm_display_mode *mode)
  9284. {
  9285. #ifdef CONFIG_DRM_FBDEV_EMULATION
  9286. struct drm_i915_private *dev_priv = to_i915(dev);
  9287. struct drm_i915_gem_object *obj;
  9288. struct drm_framebuffer *fb;
  9289. if (!dev_priv->fbdev)
  9290. return NULL;
  9291. if (!dev_priv->fbdev->fb)
  9292. return NULL;
  9293. obj = dev_priv->fbdev->fb->obj;
  9294. BUG_ON(!obj);
  9295. fb = &dev_priv->fbdev->fb->base;
  9296. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  9297. fb->bits_per_pixel))
  9298. return NULL;
  9299. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  9300. return NULL;
  9301. drm_framebuffer_reference(fb);
  9302. return fb;
  9303. #else
  9304. return NULL;
  9305. #endif
  9306. }
  9307. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  9308. struct drm_crtc *crtc,
  9309. struct drm_display_mode *mode,
  9310. struct drm_framebuffer *fb,
  9311. int x, int y)
  9312. {
  9313. struct drm_plane_state *plane_state;
  9314. int hdisplay, vdisplay;
  9315. int ret;
  9316. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  9317. if (IS_ERR(plane_state))
  9318. return PTR_ERR(plane_state);
  9319. if (mode)
  9320. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  9321. else
  9322. hdisplay = vdisplay = 0;
  9323. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  9324. if (ret)
  9325. return ret;
  9326. drm_atomic_set_fb_for_plane(plane_state, fb);
  9327. plane_state->crtc_x = 0;
  9328. plane_state->crtc_y = 0;
  9329. plane_state->crtc_w = hdisplay;
  9330. plane_state->crtc_h = vdisplay;
  9331. plane_state->src_x = x << 16;
  9332. plane_state->src_y = y << 16;
  9333. plane_state->src_w = hdisplay << 16;
  9334. plane_state->src_h = vdisplay << 16;
  9335. return 0;
  9336. }
  9337. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  9338. struct drm_display_mode *mode,
  9339. struct intel_load_detect_pipe *old,
  9340. struct drm_modeset_acquire_ctx *ctx)
  9341. {
  9342. struct intel_crtc *intel_crtc;
  9343. struct intel_encoder *intel_encoder =
  9344. intel_attached_encoder(connector);
  9345. struct drm_crtc *possible_crtc;
  9346. struct drm_encoder *encoder = &intel_encoder->base;
  9347. struct drm_crtc *crtc = NULL;
  9348. struct drm_device *dev = encoder->dev;
  9349. struct drm_i915_private *dev_priv = to_i915(dev);
  9350. struct drm_framebuffer *fb;
  9351. struct drm_mode_config *config = &dev->mode_config;
  9352. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  9353. struct drm_connector_state *connector_state;
  9354. struct intel_crtc_state *crtc_state;
  9355. int ret, i = -1;
  9356. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  9357. connector->base.id, connector->name,
  9358. encoder->base.id, encoder->name);
  9359. old->restore_state = NULL;
  9360. retry:
  9361. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  9362. if (ret)
  9363. goto fail;
  9364. /*
  9365. * Algorithm gets a little messy:
  9366. *
  9367. * - if the connector already has an assigned crtc, use it (but make
  9368. * sure it's on first)
  9369. *
  9370. * - try to find the first unused crtc that can drive this connector,
  9371. * and use that if we find one
  9372. */
  9373. /* See if we already have a CRTC for this connector */
  9374. if (connector->state->crtc) {
  9375. crtc = connector->state->crtc;
  9376. ret = drm_modeset_lock(&crtc->mutex, ctx);
  9377. if (ret)
  9378. goto fail;
  9379. /* Make sure the crtc and connector are running */
  9380. goto found;
  9381. }
  9382. /* Find an unused one (if possible) */
  9383. for_each_crtc(dev, possible_crtc) {
  9384. i++;
  9385. if (!(encoder->possible_crtcs & (1 << i)))
  9386. continue;
  9387. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  9388. if (ret)
  9389. goto fail;
  9390. if (possible_crtc->state->enable) {
  9391. drm_modeset_unlock(&possible_crtc->mutex);
  9392. continue;
  9393. }
  9394. crtc = possible_crtc;
  9395. break;
  9396. }
  9397. /*
  9398. * If we didn't find an unused CRTC, don't use any.
  9399. */
  9400. if (!crtc) {
  9401. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  9402. goto fail;
  9403. }
  9404. found:
  9405. intel_crtc = to_intel_crtc(crtc);
  9406. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  9407. if (ret)
  9408. goto fail;
  9409. state = drm_atomic_state_alloc(dev);
  9410. restore_state = drm_atomic_state_alloc(dev);
  9411. if (!state || !restore_state) {
  9412. ret = -ENOMEM;
  9413. goto fail;
  9414. }
  9415. state->acquire_ctx = ctx;
  9416. restore_state->acquire_ctx = ctx;
  9417. connector_state = drm_atomic_get_connector_state(state, connector);
  9418. if (IS_ERR(connector_state)) {
  9419. ret = PTR_ERR(connector_state);
  9420. goto fail;
  9421. }
  9422. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  9423. if (ret)
  9424. goto fail;
  9425. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  9426. if (IS_ERR(crtc_state)) {
  9427. ret = PTR_ERR(crtc_state);
  9428. goto fail;
  9429. }
  9430. crtc_state->base.active = crtc_state->base.enable = true;
  9431. if (!mode)
  9432. mode = &load_detect_mode;
  9433. /* We need a framebuffer large enough to accommodate all accesses
  9434. * that the plane may generate whilst we perform load detection.
  9435. * We can not rely on the fbcon either being present (we get called
  9436. * during its initialisation to detect all boot displays, or it may
  9437. * not even exist) or that it is large enough to satisfy the
  9438. * requested mode.
  9439. */
  9440. fb = mode_fits_in_fbdev(dev, mode);
  9441. if (fb == NULL) {
  9442. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  9443. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  9444. } else
  9445. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  9446. if (IS_ERR(fb)) {
  9447. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  9448. goto fail;
  9449. }
  9450. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  9451. if (ret)
  9452. goto fail;
  9453. drm_framebuffer_unreference(fb);
  9454. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  9455. if (ret)
  9456. goto fail;
  9457. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  9458. if (!ret)
  9459. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  9460. if (!ret)
  9461. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
  9462. if (ret) {
  9463. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  9464. goto fail;
  9465. }
  9466. ret = drm_atomic_commit(state);
  9467. if (ret) {
  9468. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  9469. goto fail;
  9470. }
  9471. old->restore_state = restore_state;
  9472. /* let the connector get through one full cycle before testing */
  9473. intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
  9474. return true;
  9475. fail:
  9476. if (state) {
  9477. drm_atomic_state_put(state);
  9478. state = NULL;
  9479. }
  9480. if (restore_state) {
  9481. drm_atomic_state_put(restore_state);
  9482. restore_state = NULL;
  9483. }
  9484. if (ret == -EDEADLK) {
  9485. drm_modeset_backoff(ctx);
  9486. goto retry;
  9487. }
  9488. return false;
  9489. }
  9490. void intel_release_load_detect_pipe(struct drm_connector *connector,
  9491. struct intel_load_detect_pipe *old,
  9492. struct drm_modeset_acquire_ctx *ctx)
  9493. {
  9494. struct intel_encoder *intel_encoder =
  9495. intel_attached_encoder(connector);
  9496. struct drm_encoder *encoder = &intel_encoder->base;
  9497. struct drm_atomic_state *state = old->restore_state;
  9498. int ret;
  9499. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  9500. connector->base.id, connector->name,
  9501. encoder->base.id, encoder->name);
  9502. if (!state)
  9503. return;
  9504. ret = drm_atomic_commit(state);
  9505. if (ret)
  9506. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  9507. drm_atomic_state_put(state);
  9508. }
  9509. static int i9xx_pll_refclk(struct drm_device *dev,
  9510. const struct intel_crtc_state *pipe_config)
  9511. {
  9512. struct drm_i915_private *dev_priv = to_i915(dev);
  9513. u32 dpll = pipe_config->dpll_hw_state.dpll;
  9514. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  9515. return dev_priv->vbt.lvds_ssc_freq;
  9516. else if (HAS_PCH_SPLIT(dev_priv))
  9517. return 120000;
  9518. else if (!IS_GEN2(dev_priv))
  9519. return 96000;
  9520. else
  9521. return 48000;
  9522. }
  9523. /* Returns the clock of the currently programmed mode of the given pipe. */
  9524. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  9525. struct intel_crtc_state *pipe_config)
  9526. {
  9527. struct drm_device *dev = crtc->base.dev;
  9528. struct drm_i915_private *dev_priv = to_i915(dev);
  9529. int pipe = pipe_config->cpu_transcoder;
  9530. u32 dpll = pipe_config->dpll_hw_state.dpll;
  9531. u32 fp;
  9532. struct dpll clock;
  9533. int port_clock;
  9534. int refclk = i9xx_pll_refclk(dev, pipe_config);
  9535. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  9536. fp = pipe_config->dpll_hw_state.fp0;
  9537. else
  9538. fp = pipe_config->dpll_hw_state.fp1;
  9539. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  9540. if (IS_PINEVIEW(dev_priv)) {
  9541. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  9542. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  9543. } else {
  9544. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  9545. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  9546. }
  9547. if (!IS_GEN2(dev_priv)) {
  9548. if (IS_PINEVIEW(dev_priv))
  9549. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  9550. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  9551. else
  9552. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  9553. DPLL_FPA01_P1_POST_DIV_SHIFT);
  9554. switch (dpll & DPLL_MODE_MASK) {
  9555. case DPLLB_MODE_DAC_SERIAL:
  9556. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  9557. 5 : 10;
  9558. break;
  9559. case DPLLB_MODE_LVDS:
  9560. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  9561. 7 : 14;
  9562. break;
  9563. default:
  9564. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  9565. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  9566. return;
  9567. }
  9568. if (IS_PINEVIEW(dev_priv))
  9569. port_clock = pnv_calc_dpll_params(refclk, &clock);
  9570. else
  9571. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  9572. } else {
  9573. u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
  9574. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  9575. if (is_lvds) {
  9576. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  9577. DPLL_FPA01_P1_POST_DIV_SHIFT);
  9578. if (lvds & LVDS_CLKB_POWER_UP)
  9579. clock.p2 = 7;
  9580. else
  9581. clock.p2 = 14;
  9582. } else {
  9583. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  9584. clock.p1 = 2;
  9585. else {
  9586. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  9587. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  9588. }
  9589. if (dpll & PLL_P2_DIVIDE_BY_4)
  9590. clock.p2 = 4;
  9591. else
  9592. clock.p2 = 2;
  9593. }
  9594. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  9595. }
  9596. /*
  9597. * This value includes pixel_multiplier. We will use
  9598. * port_clock to compute adjusted_mode.crtc_clock in the
  9599. * encoder's get_config() function.
  9600. */
  9601. pipe_config->port_clock = port_clock;
  9602. }
  9603. int intel_dotclock_calculate(int link_freq,
  9604. const struct intel_link_m_n *m_n)
  9605. {
  9606. /*
  9607. * The calculation for the data clock is:
  9608. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  9609. * But we want to avoid losing precison if possible, so:
  9610. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  9611. *
  9612. * and the link clock is simpler:
  9613. * link_clock = (m * link_clock) / n
  9614. */
  9615. if (!m_n->link_n)
  9616. return 0;
  9617. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  9618. }
  9619. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  9620. struct intel_crtc_state *pipe_config)
  9621. {
  9622. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9623. /* read out port_clock from the DPLL */
  9624. i9xx_crtc_clock_get(crtc, pipe_config);
  9625. /*
  9626. * In case there is an active pipe without active ports,
  9627. * we may need some idea for the dotclock anyway.
  9628. * Calculate one based on the FDI configuration.
  9629. */
  9630. pipe_config->base.adjusted_mode.crtc_clock =
  9631. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9632. &pipe_config->fdi_m_n);
  9633. }
  9634. /** Returns the currently programmed mode of the given pipe. */
  9635. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  9636. struct drm_crtc *crtc)
  9637. {
  9638. struct drm_i915_private *dev_priv = to_i915(dev);
  9639. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9640. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  9641. struct drm_display_mode *mode;
  9642. struct intel_crtc_state *pipe_config;
  9643. int htot = I915_READ(HTOTAL(cpu_transcoder));
  9644. int hsync = I915_READ(HSYNC(cpu_transcoder));
  9645. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  9646. int vsync = I915_READ(VSYNC(cpu_transcoder));
  9647. enum pipe pipe = intel_crtc->pipe;
  9648. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  9649. if (!mode)
  9650. return NULL;
  9651. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  9652. if (!pipe_config) {
  9653. kfree(mode);
  9654. return NULL;
  9655. }
  9656. /*
  9657. * Construct a pipe_config sufficient for getting the clock info
  9658. * back out of crtc_clock_get.
  9659. *
  9660. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  9661. * to use a real value here instead.
  9662. */
  9663. pipe_config->cpu_transcoder = (enum transcoder) pipe;
  9664. pipe_config->pixel_multiplier = 1;
  9665. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  9666. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  9667. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  9668. i9xx_crtc_clock_get(intel_crtc, pipe_config);
  9669. mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
  9670. mode->hdisplay = (htot & 0xffff) + 1;
  9671. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  9672. mode->hsync_start = (hsync & 0xffff) + 1;
  9673. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  9674. mode->vdisplay = (vtot & 0xffff) + 1;
  9675. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  9676. mode->vsync_start = (vsync & 0xffff) + 1;
  9677. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  9678. drm_mode_set_name(mode);
  9679. kfree(pipe_config);
  9680. return mode;
  9681. }
  9682. static void intel_crtc_destroy(struct drm_crtc *crtc)
  9683. {
  9684. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9685. struct drm_device *dev = crtc->dev;
  9686. struct intel_flip_work *work;
  9687. spin_lock_irq(&dev->event_lock);
  9688. work = intel_crtc->flip_work;
  9689. intel_crtc->flip_work = NULL;
  9690. spin_unlock_irq(&dev->event_lock);
  9691. if (work) {
  9692. cancel_work_sync(&work->mmio_work);
  9693. cancel_work_sync(&work->unpin_work);
  9694. kfree(work);
  9695. }
  9696. drm_crtc_cleanup(crtc);
  9697. kfree(intel_crtc);
  9698. }
  9699. static void intel_unpin_work_fn(struct work_struct *__work)
  9700. {
  9701. struct intel_flip_work *work =
  9702. container_of(__work, struct intel_flip_work, unpin_work);
  9703. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  9704. struct drm_device *dev = crtc->base.dev;
  9705. struct drm_plane *primary = crtc->base.primary;
  9706. if (is_mmio_work(work))
  9707. flush_work(&work->mmio_work);
  9708. mutex_lock(&dev->struct_mutex);
  9709. intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
  9710. i915_gem_object_put(work->pending_flip_obj);
  9711. mutex_unlock(&dev->struct_mutex);
  9712. i915_gem_request_put(work->flip_queued_req);
  9713. intel_frontbuffer_flip_complete(to_i915(dev),
  9714. to_intel_plane(primary)->frontbuffer_bit);
  9715. intel_fbc_post_update(crtc);
  9716. drm_framebuffer_unreference(work->old_fb);
  9717. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  9718. atomic_dec(&crtc->unpin_work_count);
  9719. kfree(work);
  9720. }
  9721. /* Is 'a' after or equal to 'b'? */
  9722. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9723. {
  9724. return !((a - b) & 0x80000000);
  9725. }
  9726. static bool __pageflip_finished_cs(struct intel_crtc *crtc,
  9727. struct intel_flip_work *work)
  9728. {
  9729. struct drm_device *dev = crtc->base.dev;
  9730. struct drm_i915_private *dev_priv = to_i915(dev);
  9731. if (abort_flip_on_reset(crtc))
  9732. return true;
  9733. /*
  9734. * The relevant registers doen't exist on pre-ctg.
  9735. * As the flip done interrupt doesn't trigger for mmio
  9736. * flips on gmch platforms, a flip count check isn't
  9737. * really needed there. But since ctg has the registers,
  9738. * include it in the check anyway.
  9739. */
  9740. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  9741. return true;
  9742. /*
  9743. * BDW signals flip done immediately if the plane
  9744. * is disabled, even if the plane enable is already
  9745. * armed to occur at the next vblank :(
  9746. */
  9747. /*
  9748. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9749. * used the same base address. In that case the mmio flip might
  9750. * have completed, but the CS hasn't even executed the flip yet.
  9751. *
  9752. * A flip count check isn't enough as the CS might have updated
  9753. * the base address just after start of vblank, but before we
  9754. * managed to process the interrupt. This means we'd complete the
  9755. * CS flip too soon.
  9756. *
  9757. * Combining both checks should get us a good enough result. It may
  9758. * still happen that the CS flip has been executed, but has not
  9759. * yet actually completed. But in case the base address is the same
  9760. * anyway, we don't really care.
  9761. */
  9762. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9763. crtc->flip_work->gtt_offset &&
  9764. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
  9765. crtc->flip_work->flip_count);
  9766. }
  9767. static bool
  9768. __pageflip_finished_mmio(struct intel_crtc *crtc,
  9769. struct intel_flip_work *work)
  9770. {
  9771. /*
  9772. * MMIO work completes when vblank is different from
  9773. * flip_queued_vblank.
  9774. *
  9775. * Reset counter value doesn't matter, this is handled by
  9776. * i915_wait_request finishing early, so no need to handle
  9777. * reset here.
  9778. */
  9779. return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
  9780. }
  9781. static bool pageflip_finished(struct intel_crtc *crtc,
  9782. struct intel_flip_work *work)
  9783. {
  9784. if (!atomic_read(&work->pending))
  9785. return false;
  9786. smp_rmb();
  9787. if (is_mmio_work(work))
  9788. return __pageflip_finished_mmio(crtc, work);
  9789. else
  9790. return __pageflip_finished_cs(crtc, work);
  9791. }
  9792. void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
  9793. {
  9794. struct drm_device *dev = &dev_priv->drm;
  9795. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  9796. struct intel_flip_work *work;
  9797. unsigned long flags;
  9798. /* Ignore early vblank irqs */
  9799. if (!crtc)
  9800. return;
  9801. /*
  9802. * This is called both by irq handlers and the reset code (to complete
  9803. * lost pageflips) so needs the full irqsave spinlocks.
  9804. */
  9805. spin_lock_irqsave(&dev->event_lock, flags);
  9806. work = crtc->flip_work;
  9807. if (work != NULL &&
  9808. !is_mmio_work(work) &&
  9809. pageflip_finished(crtc, work))
  9810. page_flip_completed(crtc);
  9811. spin_unlock_irqrestore(&dev->event_lock, flags);
  9812. }
  9813. void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
  9814. {
  9815. struct drm_device *dev = &dev_priv->drm;
  9816. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  9817. struct intel_flip_work *work;
  9818. unsigned long flags;
  9819. /* Ignore early vblank irqs */
  9820. if (!crtc)
  9821. return;
  9822. /*
  9823. * This is called both by irq handlers and the reset code (to complete
  9824. * lost pageflips) so needs the full irqsave spinlocks.
  9825. */
  9826. spin_lock_irqsave(&dev->event_lock, flags);
  9827. work = crtc->flip_work;
  9828. if (work != NULL &&
  9829. is_mmio_work(work) &&
  9830. pageflip_finished(crtc, work))
  9831. page_flip_completed(crtc);
  9832. spin_unlock_irqrestore(&dev->event_lock, flags);
  9833. }
  9834. static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
  9835. struct intel_flip_work *work)
  9836. {
  9837. work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
  9838. /* Ensure that the work item is consistent when activating it ... */
  9839. smp_mb__before_atomic();
  9840. atomic_set(&work->pending, 1);
  9841. }
  9842. static int intel_gen2_queue_flip(struct drm_device *dev,
  9843. struct drm_crtc *crtc,
  9844. struct drm_framebuffer *fb,
  9845. struct drm_i915_gem_object *obj,
  9846. struct drm_i915_gem_request *req,
  9847. uint32_t flags)
  9848. {
  9849. struct intel_ring *ring = req->ring;
  9850. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9851. u32 flip_mask;
  9852. int ret;
  9853. ret = intel_ring_begin(req, 6);
  9854. if (ret)
  9855. return ret;
  9856. /* Can't queue multiple flips, so wait for the previous
  9857. * one to finish before executing the next.
  9858. */
  9859. if (intel_crtc->plane)
  9860. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9861. else
  9862. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9863. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9864. intel_ring_emit(ring, MI_NOOP);
  9865. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9866. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9867. intel_ring_emit(ring, fb->pitches[0]);
  9868. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  9869. intel_ring_emit(ring, 0); /* aux display base address, unused */
  9870. return 0;
  9871. }
  9872. static int intel_gen3_queue_flip(struct drm_device *dev,
  9873. struct drm_crtc *crtc,
  9874. struct drm_framebuffer *fb,
  9875. struct drm_i915_gem_object *obj,
  9876. struct drm_i915_gem_request *req,
  9877. uint32_t flags)
  9878. {
  9879. struct intel_ring *ring = req->ring;
  9880. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9881. u32 flip_mask;
  9882. int ret;
  9883. ret = intel_ring_begin(req, 6);
  9884. if (ret)
  9885. return ret;
  9886. if (intel_crtc->plane)
  9887. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9888. else
  9889. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9890. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9891. intel_ring_emit(ring, MI_NOOP);
  9892. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  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_ring_emit(ring, MI_NOOP);
  9897. return 0;
  9898. }
  9899. static int intel_gen4_queue_flip(struct drm_device *dev,
  9900. struct drm_crtc *crtc,
  9901. struct drm_framebuffer *fb,
  9902. struct drm_i915_gem_object *obj,
  9903. struct drm_i915_gem_request *req,
  9904. uint32_t flags)
  9905. {
  9906. struct intel_ring *ring = req->ring;
  9907. struct drm_i915_private *dev_priv = to_i915(dev);
  9908. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9909. uint32_t pf, pipesrc;
  9910. int ret;
  9911. ret = intel_ring_begin(req, 4);
  9912. if (ret)
  9913. return ret;
  9914. /* i965+ uses the linear or tiled offsets from the
  9915. * Display Registers (which do not change across a page-flip)
  9916. * so we need only reprogram the base address.
  9917. */
  9918. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9919. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9920. intel_ring_emit(ring, fb->pitches[0]);
  9921. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset |
  9922. intel_fb_modifier_to_tiling(fb->modifier[0]));
  9923. /* XXX Enabling the panel-fitter across page-flip is so far
  9924. * untested on non-native modes, so ignore it for now.
  9925. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9926. */
  9927. pf = 0;
  9928. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9929. intel_ring_emit(ring, pf | pipesrc);
  9930. return 0;
  9931. }
  9932. static int intel_gen6_queue_flip(struct drm_device *dev,
  9933. struct drm_crtc *crtc,
  9934. struct drm_framebuffer *fb,
  9935. struct drm_i915_gem_object *obj,
  9936. struct drm_i915_gem_request *req,
  9937. uint32_t flags)
  9938. {
  9939. struct intel_ring *ring = req->ring;
  9940. struct drm_i915_private *dev_priv = to_i915(dev);
  9941. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9942. uint32_t pf, pipesrc;
  9943. int ret;
  9944. ret = intel_ring_begin(req, 4);
  9945. if (ret)
  9946. return ret;
  9947. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9948. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9949. intel_ring_emit(ring, fb->pitches[0] |
  9950. intel_fb_modifier_to_tiling(fb->modifier[0]));
  9951. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  9952. /* Contrary to the suggestions in the documentation,
  9953. * "Enable Panel Fitter" does not seem to be required when page
  9954. * flipping with a non-native mode, and worse causes a normal
  9955. * modeset to fail.
  9956. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9957. */
  9958. pf = 0;
  9959. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9960. intel_ring_emit(ring, pf | pipesrc);
  9961. return 0;
  9962. }
  9963. static int intel_gen7_queue_flip(struct drm_device *dev,
  9964. struct drm_crtc *crtc,
  9965. struct drm_framebuffer *fb,
  9966. struct drm_i915_gem_object *obj,
  9967. struct drm_i915_gem_request *req,
  9968. uint32_t flags)
  9969. {
  9970. struct drm_i915_private *dev_priv = to_i915(dev);
  9971. struct intel_ring *ring = req->ring;
  9972. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9973. uint32_t plane_bit = 0;
  9974. int len, ret;
  9975. switch (intel_crtc->plane) {
  9976. case PLANE_A:
  9977. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9978. break;
  9979. case PLANE_B:
  9980. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9981. break;
  9982. case PLANE_C:
  9983. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9984. break;
  9985. default:
  9986. WARN_ONCE(1, "unknown plane in flip command\n");
  9987. return -ENODEV;
  9988. }
  9989. len = 4;
  9990. if (req->engine->id == RCS) {
  9991. len += 6;
  9992. /*
  9993. * On Gen 8, SRM is now taking an extra dword to accommodate
  9994. * 48bits addresses, and we need a NOOP for the batch size to
  9995. * stay even.
  9996. */
  9997. if (IS_GEN8(dev_priv))
  9998. len += 2;
  9999. }
  10000. /*
  10001. * BSpec MI_DISPLAY_FLIP for IVB:
  10002. * "The full packet must be contained within the same cache line."
  10003. *
  10004. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  10005. * cacheline, if we ever start emitting more commands before
  10006. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  10007. * then do the cacheline alignment, and finally emit the
  10008. * MI_DISPLAY_FLIP.
  10009. */
  10010. ret = intel_ring_cacheline_align(req);
  10011. if (ret)
  10012. return ret;
  10013. ret = intel_ring_begin(req, len);
  10014. if (ret)
  10015. return ret;
  10016. /* Unmask the flip-done completion message. Note that the bspec says that
  10017. * we should do this for both the BCS and RCS, and that we must not unmask
  10018. * more than one flip event at any time (or ensure that one flip message
  10019. * can be sent by waiting for flip-done prior to queueing new flips).
  10020. * Experimentation says that BCS works despite DERRMR masking all
  10021. * flip-done completion events and that unmasking all planes at once
  10022. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  10023. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  10024. */
  10025. if (req->engine->id == RCS) {
  10026. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  10027. intel_ring_emit_reg(ring, DERRMR);
  10028. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  10029. DERRMR_PIPEB_PRI_FLIP_DONE |
  10030. DERRMR_PIPEC_PRI_FLIP_DONE));
  10031. if (IS_GEN8(dev_priv))
  10032. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
  10033. MI_SRM_LRM_GLOBAL_GTT);
  10034. else
  10035. intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
  10036. MI_SRM_LRM_GLOBAL_GTT);
  10037. intel_ring_emit_reg(ring, DERRMR);
  10038. intel_ring_emit(ring,
  10039. i915_ggtt_offset(req->engine->scratch) + 256);
  10040. if (IS_GEN8(dev_priv)) {
  10041. intel_ring_emit(ring, 0);
  10042. intel_ring_emit(ring, MI_NOOP);
  10043. }
  10044. }
  10045. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  10046. intel_ring_emit(ring, fb->pitches[0] |
  10047. intel_fb_modifier_to_tiling(fb->modifier[0]));
  10048. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  10049. intel_ring_emit(ring, (MI_NOOP));
  10050. return 0;
  10051. }
  10052. static bool use_mmio_flip(struct intel_engine_cs *engine,
  10053. struct drm_i915_gem_object *obj)
  10054. {
  10055. /*
  10056. * This is not being used for older platforms, because
  10057. * non-availability of flip done interrupt forces us to use
  10058. * CS flips. Older platforms derive flip done using some clever
  10059. * tricks involving the flip_pending status bits and vblank irqs.
  10060. * So using MMIO flips there would disrupt this mechanism.
  10061. */
  10062. if (engine == NULL)
  10063. return true;
  10064. if (INTEL_GEN(engine->i915) < 5)
  10065. return false;
  10066. if (i915.use_mmio_flip < 0)
  10067. return false;
  10068. else if (i915.use_mmio_flip > 0)
  10069. return true;
  10070. else if (i915.enable_execlists)
  10071. return true;
  10072. return engine != i915_gem_object_last_write_engine(obj);
  10073. }
  10074. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
  10075. unsigned int rotation,
  10076. struct intel_flip_work *work)
  10077. {
  10078. struct drm_device *dev = intel_crtc->base.dev;
  10079. struct drm_i915_private *dev_priv = to_i915(dev);
  10080. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  10081. const enum pipe pipe = intel_crtc->pipe;
  10082. u32 ctl, stride = skl_plane_stride(fb, 0, rotation);
  10083. ctl = I915_READ(PLANE_CTL(pipe, 0));
  10084. ctl &= ~PLANE_CTL_TILED_MASK;
  10085. switch (fb->modifier[0]) {
  10086. case DRM_FORMAT_MOD_NONE:
  10087. break;
  10088. case I915_FORMAT_MOD_X_TILED:
  10089. ctl |= PLANE_CTL_TILED_X;
  10090. break;
  10091. case I915_FORMAT_MOD_Y_TILED:
  10092. ctl |= PLANE_CTL_TILED_Y;
  10093. break;
  10094. case I915_FORMAT_MOD_Yf_TILED:
  10095. ctl |= PLANE_CTL_TILED_YF;
  10096. break;
  10097. default:
  10098. MISSING_CASE(fb->modifier[0]);
  10099. }
  10100. /*
  10101. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  10102. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  10103. */
  10104. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  10105. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  10106. I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
  10107. POSTING_READ(PLANE_SURF(pipe, 0));
  10108. }
  10109. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
  10110. struct intel_flip_work *work)
  10111. {
  10112. struct drm_device *dev = intel_crtc->base.dev;
  10113. struct drm_i915_private *dev_priv = to_i915(dev);
  10114. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  10115. i915_reg_t reg = DSPCNTR(intel_crtc->plane);
  10116. u32 dspcntr;
  10117. dspcntr = I915_READ(reg);
  10118. if (fb->modifier[0] == I915_FORMAT_MOD_X_TILED)
  10119. dspcntr |= DISPPLANE_TILED;
  10120. else
  10121. dspcntr &= ~DISPPLANE_TILED;
  10122. I915_WRITE(reg, dspcntr);
  10123. I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
  10124. POSTING_READ(DSPSURF(intel_crtc->plane));
  10125. }
  10126. static void intel_mmio_flip_work_func(struct work_struct *w)
  10127. {
  10128. struct intel_flip_work *work =
  10129. container_of(w, struct intel_flip_work, mmio_work);
  10130. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  10131. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  10132. struct intel_framebuffer *intel_fb =
  10133. to_intel_framebuffer(crtc->base.primary->fb);
  10134. struct drm_i915_gem_object *obj = intel_fb->obj;
  10135. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  10136. WARN_ON(i915_gem_object_wait(obj, 0, MAX_SCHEDULE_TIMEOUT, NULL) < 0);
  10137. intel_pipe_update_start(crtc);
  10138. if (INTEL_GEN(dev_priv) >= 9)
  10139. skl_do_mmio_flip(crtc, work->rotation, work);
  10140. else
  10141. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  10142. ilk_do_mmio_flip(crtc, work);
  10143. intel_pipe_update_end(crtc, work);
  10144. }
  10145. static int intel_default_queue_flip(struct drm_device *dev,
  10146. struct drm_crtc *crtc,
  10147. struct drm_framebuffer *fb,
  10148. struct drm_i915_gem_object *obj,
  10149. struct drm_i915_gem_request *req,
  10150. uint32_t flags)
  10151. {
  10152. return -ENODEV;
  10153. }
  10154. static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
  10155. struct intel_crtc *intel_crtc,
  10156. struct intel_flip_work *work)
  10157. {
  10158. u32 addr, vblank;
  10159. if (!atomic_read(&work->pending))
  10160. return false;
  10161. smp_rmb();
  10162. vblank = intel_crtc_get_vblank_counter(intel_crtc);
  10163. if (work->flip_ready_vblank == 0) {
  10164. if (work->flip_queued_req &&
  10165. !i915_gem_request_completed(work->flip_queued_req))
  10166. return false;
  10167. work->flip_ready_vblank = vblank;
  10168. }
  10169. if (vblank - work->flip_ready_vblank < 3)
  10170. return false;
  10171. /* Potential stall - if we see that the flip has happened,
  10172. * assume a missed interrupt. */
  10173. if (INTEL_GEN(dev_priv) >= 4)
  10174. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  10175. else
  10176. addr = I915_READ(DSPADDR(intel_crtc->plane));
  10177. /* There is a potential issue here with a false positive after a flip
  10178. * to the same address. We could address this by checking for a
  10179. * non-incrementing frame counter.
  10180. */
  10181. return addr == work->gtt_offset;
  10182. }
  10183. void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
  10184. {
  10185. struct drm_device *dev = &dev_priv->drm;
  10186. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  10187. struct intel_flip_work *work;
  10188. WARN_ON(!in_interrupt());
  10189. if (crtc == NULL)
  10190. return;
  10191. spin_lock(&dev->event_lock);
  10192. work = crtc->flip_work;
  10193. if (work != NULL && !is_mmio_work(work) &&
  10194. __pageflip_stall_check_cs(dev_priv, crtc, work)) {
  10195. WARN_ONCE(1,
  10196. "Kicking stuck page flip: queued at %d, now %d\n",
  10197. work->flip_queued_vblank, intel_crtc_get_vblank_counter(crtc));
  10198. page_flip_completed(crtc);
  10199. work = NULL;
  10200. }
  10201. if (work != NULL && !is_mmio_work(work) &&
  10202. intel_crtc_get_vblank_counter(crtc) - work->flip_queued_vblank > 1)
  10203. intel_queue_rps_boost_for_request(work->flip_queued_req);
  10204. spin_unlock(&dev->event_lock);
  10205. }
  10206. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  10207. struct drm_framebuffer *fb,
  10208. struct drm_pending_vblank_event *event,
  10209. uint32_t page_flip_flags)
  10210. {
  10211. struct drm_device *dev = crtc->dev;
  10212. struct drm_i915_private *dev_priv = to_i915(dev);
  10213. struct drm_framebuffer *old_fb = crtc->primary->fb;
  10214. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10215. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10216. struct drm_plane *primary = crtc->primary;
  10217. enum pipe pipe = intel_crtc->pipe;
  10218. struct intel_flip_work *work;
  10219. struct intel_engine_cs *engine;
  10220. bool mmio_flip;
  10221. struct drm_i915_gem_request *request;
  10222. struct i915_vma *vma;
  10223. int ret;
  10224. /*
  10225. * drm_mode_page_flip_ioctl() should already catch this, but double
  10226. * check to be safe. In the future we may enable pageflipping from
  10227. * a disabled primary plane.
  10228. */
  10229. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  10230. return -EBUSY;
  10231. /* Can't change pixel format via MI display flips. */
  10232. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  10233. return -EINVAL;
  10234. /*
  10235. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  10236. * Note that pitch changes could also affect these register.
  10237. */
  10238. if (INTEL_INFO(dev)->gen > 3 &&
  10239. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  10240. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  10241. return -EINVAL;
  10242. if (i915_terminally_wedged(&dev_priv->gpu_error))
  10243. goto out_hang;
  10244. work = kzalloc(sizeof(*work), GFP_KERNEL);
  10245. if (work == NULL)
  10246. return -ENOMEM;
  10247. work->event = event;
  10248. work->crtc = crtc;
  10249. work->old_fb = old_fb;
  10250. INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
  10251. ret = drm_crtc_vblank_get(crtc);
  10252. if (ret)
  10253. goto free_work;
  10254. /* We borrow the event spin lock for protecting flip_work */
  10255. spin_lock_irq(&dev->event_lock);
  10256. if (intel_crtc->flip_work) {
  10257. /* Before declaring the flip queue wedged, check if
  10258. * the hardware completed the operation behind our backs.
  10259. */
  10260. if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
  10261. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  10262. page_flip_completed(intel_crtc);
  10263. } else {
  10264. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  10265. spin_unlock_irq(&dev->event_lock);
  10266. drm_crtc_vblank_put(crtc);
  10267. kfree(work);
  10268. return -EBUSY;
  10269. }
  10270. }
  10271. intel_crtc->flip_work = work;
  10272. spin_unlock_irq(&dev->event_lock);
  10273. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  10274. flush_workqueue(dev_priv->wq);
  10275. /* Reference the objects for the scheduled work. */
  10276. drm_framebuffer_reference(work->old_fb);
  10277. crtc->primary->fb = fb;
  10278. update_state_fb(crtc->primary);
  10279. work->pending_flip_obj = i915_gem_object_get(obj);
  10280. ret = i915_mutex_lock_interruptible(dev);
  10281. if (ret)
  10282. goto cleanup;
  10283. intel_crtc->reset_count = i915_reset_count(&dev_priv->gpu_error);
  10284. if (i915_reset_in_progress_or_wedged(&dev_priv->gpu_error)) {
  10285. ret = -EIO;
  10286. goto cleanup;
  10287. }
  10288. atomic_inc(&intel_crtc->unpin_work_count);
  10289. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  10290. work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
  10291. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  10292. engine = dev_priv->engine[BCS];
  10293. if (fb->modifier[0] != old_fb->modifier[0])
  10294. /* vlv: DISPLAY_FLIP fails to change tiling */
  10295. engine = NULL;
  10296. } else if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
  10297. engine = dev_priv->engine[BCS];
  10298. } else if (INTEL_INFO(dev)->gen >= 7) {
  10299. engine = i915_gem_object_last_write_engine(obj);
  10300. if (engine == NULL || engine->id != RCS)
  10301. engine = dev_priv->engine[BCS];
  10302. } else {
  10303. engine = dev_priv->engine[RCS];
  10304. }
  10305. mmio_flip = use_mmio_flip(engine, obj);
  10306. vma = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  10307. if (IS_ERR(vma)) {
  10308. ret = PTR_ERR(vma);
  10309. goto cleanup_pending;
  10310. }
  10311. work->gtt_offset = intel_fb_gtt_offset(fb, primary->state->rotation);
  10312. work->gtt_offset += intel_crtc->dspaddr_offset;
  10313. work->rotation = crtc->primary->state->rotation;
  10314. /*
  10315. * There's the potential that the next frame will not be compatible with
  10316. * FBC, so we want to call pre_update() before the actual page flip.
  10317. * The problem is that pre_update() caches some information about the fb
  10318. * object, so we want to do this only after the object is pinned. Let's
  10319. * be on the safe side and do this immediately before scheduling the
  10320. * flip.
  10321. */
  10322. intel_fbc_pre_update(intel_crtc, intel_crtc->config,
  10323. to_intel_plane_state(primary->state));
  10324. if (mmio_flip) {
  10325. INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
  10326. queue_work(system_unbound_wq, &work->mmio_work);
  10327. } else {
  10328. request = i915_gem_request_alloc(engine, engine->last_context);
  10329. if (IS_ERR(request)) {
  10330. ret = PTR_ERR(request);
  10331. goto cleanup_unpin;
  10332. }
  10333. ret = i915_gem_request_await_object(request, obj, false);
  10334. if (ret)
  10335. goto cleanup_request;
  10336. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  10337. page_flip_flags);
  10338. if (ret)
  10339. goto cleanup_request;
  10340. intel_mark_page_flip_active(intel_crtc, work);
  10341. work->flip_queued_req = i915_gem_request_get(request);
  10342. i915_add_request_no_flush(request);
  10343. }
  10344. i915_gem_track_fb(intel_fb_obj(old_fb), obj,
  10345. to_intel_plane(primary)->frontbuffer_bit);
  10346. mutex_unlock(&dev->struct_mutex);
  10347. intel_frontbuffer_flip_prepare(to_i915(dev),
  10348. to_intel_plane(primary)->frontbuffer_bit);
  10349. trace_i915_flip_request(intel_crtc->plane, obj);
  10350. return 0;
  10351. cleanup_request:
  10352. i915_add_request_no_flush(request);
  10353. cleanup_unpin:
  10354. intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
  10355. cleanup_pending:
  10356. atomic_dec(&intel_crtc->unpin_work_count);
  10357. mutex_unlock(&dev->struct_mutex);
  10358. cleanup:
  10359. crtc->primary->fb = old_fb;
  10360. update_state_fb(crtc->primary);
  10361. i915_gem_object_put(obj);
  10362. drm_framebuffer_unreference(work->old_fb);
  10363. spin_lock_irq(&dev->event_lock);
  10364. intel_crtc->flip_work = NULL;
  10365. spin_unlock_irq(&dev->event_lock);
  10366. drm_crtc_vblank_put(crtc);
  10367. free_work:
  10368. kfree(work);
  10369. if (ret == -EIO) {
  10370. struct drm_atomic_state *state;
  10371. struct drm_plane_state *plane_state;
  10372. out_hang:
  10373. state = drm_atomic_state_alloc(dev);
  10374. if (!state)
  10375. return -ENOMEM;
  10376. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  10377. retry:
  10378. plane_state = drm_atomic_get_plane_state(state, primary);
  10379. ret = PTR_ERR_OR_ZERO(plane_state);
  10380. if (!ret) {
  10381. drm_atomic_set_fb_for_plane(plane_state, fb);
  10382. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  10383. if (!ret)
  10384. ret = drm_atomic_commit(state);
  10385. }
  10386. if (ret == -EDEADLK) {
  10387. drm_modeset_backoff(state->acquire_ctx);
  10388. drm_atomic_state_clear(state);
  10389. goto retry;
  10390. }
  10391. drm_atomic_state_put(state);
  10392. if (ret == 0 && event) {
  10393. spin_lock_irq(&dev->event_lock);
  10394. drm_crtc_send_vblank_event(crtc, event);
  10395. spin_unlock_irq(&dev->event_lock);
  10396. }
  10397. }
  10398. return ret;
  10399. }
  10400. /**
  10401. * intel_wm_need_update - Check whether watermarks need updating
  10402. * @plane: drm plane
  10403. * @state: new plane state
  10404. *
  10405. * Check current plane state versus the new one to determine whether
  10406. * watermarks need to be recalculated.
  10407. *
  10408. * Returns true or false.
  10409. */
  10410. static bool intel_wm_need_update(struct drm_plane *plane,
  10411. struct drm_plane_state *state)
  10412. {
  10413. struct intel_plane_state *new = to_intel_plane_state(state);
  10414. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  10415. /* Update watermarks on tiling or size changes. */
  10416. if (new->base.visible != cur->base.visible)
  10417. return true;
  10418. if (!cur->base.fb || !new->base.fb)
  10419. return false;
  10420. if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
  10421. cur->base.rotation != new->base.rotation ||
  10422. drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
  10423. drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
  10424. drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
  10425. drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
  10426. return true;
  10427. return false;
  10428. }
  10429. static bool needs_scaling(struct intel_plane_state *state)
  10430. {
  10431. int src_w = drm_rect_width(&state->base.src) >> 16;
  10432. int src_h = drm_rect_height(&state->base.src) >> 16;
  10433. int dst_w = drm_rect_width(&state->base.dst);
  10434. int dst_h = drm_rect_height(&state->base.dst);
  10435. return (src_w != dst_w || src_h != dst_h);
  10436. }
  10437. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  10438. struct drm_plane_state *plane_state)
  10439. {
  10440. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  10441. struct drm_crtc *crtc = crtc_state->crtc;
  10442. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10443. struct drm_plane *plane = plane_state->plane;
  10444. struct drm_device *dev = crtc->dev;
  10445. struct drm_i915_private *dev_priv = to_i915(dev);
  10446. struct intel_plane_state *old_plane_state =
  10447. to_intel_plane_state(plane->state);
  10448. bool mode_changed = needs_modeset(crtc_state);
  10449. bool was_crtc_enabled = crtc->state->active;
  10450. bool is_crtc_enabled = crtc_state->active;
  10451. bool turn_off, turn_on, visible, was_visible;
  10452. struct drm_framebuffer *fb = plane_state->fb;
  10453. int ret;
  10454. if (INTEL_GEN(dev_priv) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
  10455. ret = skl_update_scaler_plane(
  10456. to_intel_crtc_state(crtc_state),
  10457. to_intel_plane_state(plane_state));
  10458. if (ret)
  10459. return ret;
  10460. }
  10461. was_visible = old_plane_state->base.visible;
  10462. visible = to_intel_plane_state(plane_state)->base.visible;
  10463. if (!was_crtc_enabled && WARN_ON(was_visible))
  10464. was_visible = false;
  10465. /*
  10466. * Visibility is calculated as if the crtc was on, but
  10467. * after scaler setup everything depends on it being off
  10468. * when the crtc isn't active.
  10469. *
  10470. * FIXME this is wrong for watermarks. Watermarks should also
  10471. * be computed as if the pipe would be active. Perhaps move
  10472. * per-plane wm computation to the .check_plane() hook, and
  10473. * only combine the results from all planes in the current place?
  10474. */
  10475. if (!is_crtc_enabled)
  10476. to_intel_plane_state(plane_state)->base.visible = visible = false;
  10477. if (!was_visible && !visible)
  10478. return 0;
  10479. if (fb != old_plane_state->base.fb)
  10480. pipe_config->fb_changed = true;
  10481. turn_off = was_visible && (!visible || mode_changed);
  10482. turn_on = visible && (!was_visible || mode_changed);
  10483. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  10484. intel_crtc->base.base.id,
  10485. intel_crtc->base.name,
  10486. plane->base.id, plane->name,
  10487. fb ? fb->base.id : -1);
  10488. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  10489. plane->base.id, plane->name,
  10490. was_visible, visible,
  10491. turn_off, turn_on, mode_changed);
  10492. if (turn_on) {
  10493. pipe_config->update_wm_pre = true;
  10494. /* must disable cxsr around plane enable/disable */
  10495. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  10496. pipe_config->disable_cxsr = true;
  10497. } else if (turn_off) {
  10498. pipe_config->update_wm_post = true;
  10499. /* must disable cxsr around plane enable/disable */
  10500. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  10501. pipe_config->disable_cxsr = true;
  10502. } else if (intel_wm_need_update(plane, plane_state)) {
  10503. /* FIXME bollocks */
  10504. pipe_config->update_wm_pre = true;
  10505. pipe_config->update_wm_post = true;
  10506. }
  10507. /* Pre-gen9 platforms need two-step watermark updates */
  10508. if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
  10509. INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
  10510. to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
  10511. if (visible || was_visible)
  10512. pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
  10513. /*
  10514. * WaCxSRDisabledForSpriteScaling:ivb
  10515. *
  10516. * cstate->update_wm was already set above, so this flag will
  10517. * take effect when we commit and program watermarks.
  10518. */
  10519. if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev_priv) &&
  10520. needs_scaling(to_intel_plane_state(plane_state)) &&
  10521. !needs_scaling(old_plane_state))
  10522. pipe_config->disable_lp_wm = true;
  10523. return 0;
  10524. }
  10525. static bool encoders_cloneable(const struct intel_encoder *a,
  10526. const struct intel_encoder *b)
  10527. {
  10528. /* masks could be asymmetric, so check both ways */
  10529. return a == b || (a->cloneable & (1 << b->type) &&
  10530. b->cloneable & (1 << a->type));
  10531. }
  10532. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  10533. struct intel_crtc *crtc,
  10534. struct intel_encoder *encoder)
  10535. {
  10536. struct intel_encoder *source_encoder;
  10537. struct drm_connector *connector;
  10538. struct drm_connector_state *connector_state;
  10539. int i;
  10540. for_each_connector_in_state(state, connector, connector_state, i) {
  10541. if (connector_state->crtc != &crtc->base)
  10542. continue;
  10543. source_encoder =
  10544. to_intel_encoder(connector_state->best_encoder);
  10545. if (!encoders_cloneable(encoder, source_encoder))
  10546. return false;
  10547. }
  10548. return true;
  10549. }
  10550. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  10551. struct drm_crtc_state *crtc_state)
  10552. {
  10553. struct drm_device *dev = crtc->dev;
  10554. struct drm_i915_private *dev_priv = to_i915(dev);
  10555. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10556. struct intel_crtc_state *pipe_config =
  10557. to_intel_crtc_state(crtc_state);
  10558. struct drm_atomic_state *state = crtc_state->state;
  10559. int ret;
  10560. bool mode_changed = needs_modeset(crtc_state);
  10561. if (mode_changed && !crtc_state->active)
  10562. pipe_config->update_wm_post = true;
  10563. if (mode_changed && crtc_state->enable &&
  10564. dev_priv->display.crtc_compute_clock &&
  10565. !WARN_ON(pipe_config->shared_dpll)) {
  10566. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  10567. pipe_config);
  10568. if (ret)
  10569. return ret;
  10570. }
  10571. if (crtc_state->color_mgmt_changed) {
  10572. ret = intel_color_check(crtc, crtc_state);
  10573. if (ret)
  10574. return ret;
  10575. /*
  10576. * Changing color management on Intel hardware is
  10577. * handled as part of planes update.
  10578. */
  10579. crtc_state->planes_changed = true;
  10580. }
  10581. ret = 0;
  10582. if (dev_priv->display.compute_pipe_wm) {
  10583. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  10584. if (ret) {
  10585. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  10586. return ret;
  10587. }
  10588. }
  10589. if (dev_priv->display.compute_intermediate_wm &&
  10590. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  10591. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  10592. return 0;
  10593. /*
  10594. * Calculate 'intermediate' watermarks that satisfy both the
  10595. * old state and the new state. We can program these
  10596. * immediately.
  10597. */
  10598. ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
  10599. intel_crtc,
  10600. pipe_config);
  10601. if (ret) {
  10602. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  10603. return ret;
  10604. }
  10605. } else if (dev_priv->display.compute_intermediate_wm) {
  10606. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  10607. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  10608. }
  10609. if (INTEL_INFO(dev)->gen >= 9) {
  10610. if (mode_changed)
  10611. ret = skl_update_scaler_crtc(pipe_config);
  10612. if (!ret)
  10613. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  10614. pipe_config);
  10615. }
  10616. return ret;
  10617. }
  10618. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  10619. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  10620. .atomic_begin = intel_begin_crtc_commit,
  10621. .atomic_flush = intel_finish_crtc_commit,
  10622. .atomic_check = intel_crtc_atomic_check,
  10623. };
  10624. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  10625. {
  10626. struct intel_connector *connector;
  10627. for_each_intel_connector(dev, connector) {
  10628. if (connector->base.state->crtc)
  10629. drm_connector_unreference(&connector->base);
  10630. if (connector->base.encoder) {
  10631. connector->base.state->best_encoder =
  10632. connector->base.encoder;
  10633. connector->base.state->crtc =
  10634. connector->base.encoder->crtc;
  10635. drm_connector_reference(&connector->base);
  10636. } else {
  10637. connector->base.state->best_encoder = NULL;
  10638. connector->base.state->crtc = NULL;
  10639. }
  10640. }
  10641. }
  10642. static void
  10643. connected_sink_compute_bpp(struct intel_connector *connector,
  10644. struct intel_crtc_state *pipe_config)
  10645. {
  10646. const struct drm_display_info *info = &connector->base.display_info;
  10647. int bpp = pipe_config->pipe_bpp;
  10648. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  10649. connector->base.base.id,
  10650. connector->base.name);
  10651. /* Don't use an invalid EDID bpc value */
  10652. if (info->bpc != 0 && info->bpc * 3 < bpp) {
  10653. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  10654. bpp, info->bpc * 3);
  10655. pipe_config->pipe_bpp = info->bpc * 3;
  10656. }
  10657. /* Clamp bpp to 8 on screens without EDID 1.4 */
  10658. if (info->bpc == 0 && bpp > 24) {
  10659. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  10660. bpp);
  10661. pipe_config->pipe_bpp = 24;
  10662. }
  10663. }
  10664. static int
  10665. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  10666. struct intel_crtc_state *pipe_config)
  10667. {
  10668. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  10669. struct drm_atomic_state *state;
  10670. struct drm_connector *connector;
  10671. struct drm_connector_state *connector_state;
  10672. int bpp, i;
  10673. if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  10674. IS_CHERRYVIEW(dev_priv)))
  10675. bpp = 10*3;
  10676. else if (INTEL_GEN(dev_priv) >= 5)
  10677. bpp = 12*3;
  10678. else
  10679. bpp = 8*3;
  10680. pipe_config->pipe_bpp = bpp;
  10681. state = pipe_config->base.state;
  10682. /* Clamp display bpp to EDID value */
  10683. for_each_connector_in_state(state, connector, connector_state, i) {
  10684. if (connector_state->crtc != &crtc->base)
  10685. continue;
  10686. connected_sink_compute_bpp(to_intel_connector(connector),
  10687. pipe_config);
  10688. }
  10689. return bpp;
  10690. }
  10691. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  10692. {
  10693. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  10694. "type: 0x%x flags: 0x%x\n",
  10695. mode->crtc_clock,
  10696. mode->crtc_hdisplay, mode->crtc_hsync_start,
  10697. mode->crtc_hsync_end, mode->crtc_htotal,
  10698. mode->crtc_vdisplay, mode->crtc_vsync_start,
  10699. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  10700. }
  10701. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  10702. struct intel_crtc_state *pipe_config,
  10703. const char *context)
  10704. {
  10705. struct drm_device *dev = crtc->base.dev;
  10706. struct drm_i915_private *dev_priv = to_i915(dev);
  10707. struct drm_plane *plane;
  10708. struct intel_plane *intel_plane;
  10709. struct intel_plane_state *state;
  10710. struct drm_framebuffer *fb;
  10711. DRM_DEBUG_KMS("[CRTC:%d:%s]%s config %p for pipe %c\n",
  10712. crtc->base.base.id, crtc->base.name,
  10713. context, pipe_config, pipe_name(crtc->pipe));
  10714. DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
  10715. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  10716. pipe_config->pipe_bpp, pipe_config->dither);
  10717. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10718. pipe_config->has_pch_encoder,
  10719. pipe_config->fdi_lanes,
  10720. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  10721. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  10722. pipe_config->fdi_m_n.tu);
  10723. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10724. intel_crtc_has_dp_encoder(pipe_config),
  10725. pipe_config->lane_count,
  10726. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  10727. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  10728. pipe_config->dp_m_n.tu);
  10729. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  10730. intel_crtc_has_dp_encoder(pipe_config),
  10731. pipe_config->lane_count,
  10732. pipe_config->dp_m2_n2.gmch_m,
  10733. pipe_config->dp_m2_n2.gmch_n,
  10734. pipe_config->dp_m2_n2.link_m,
  10735. pipe_config->dp_m2_n2.link_n,
  10736. pipe_config->dp_m2_n2.tu);
  10737. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  10738. pipe_config->has_audio,
  10739. pipe_config->has_infoframe);
  10740. DRM_DEBUG_KMS("requested mode:\n");
  10741. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  10742. DRM_DEBUG_KMS("adjusted mode:\n");
  10743. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  10744. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  10745. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  10746. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  10747. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  10748. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  10749. crtc->num_scalers,
  10750. pipe_config->scaler_state.scaler_users,
  10751. pipe_config->scaler_state.scaler_id);
  10752. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10753. pipe_config->gmch_pfit.control,
  10754. pipe_config->gmch_pfit.pgm_ratios,
  10755. pipe_config->gmch_pfit.lvds_border_bits);
  10756. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10757. pipe_config->pch_pfit.pos,
  10758. pipe_config->pch_pfit.size,
  10759. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  10760. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  10761. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  10762. if (IS_BROXTON(dev_priv)) {
  10763. DRM_DEBUG_KMS("dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
  10764. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10765. "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
  10766. pipe_config->dpll_hw_state.ebb0,
  10767. pipe_config->dpll_hw_state.ebb4,
  10768. pipe_config->dpll_hw_state.pll0,
  10769. pipe_config->dpll_hw_state.pll1,
  10770. pipe_config->dpll_hw_state.pll2,
  10771. pipe_config->dpll_hw_state.pll3,
  10772. pipe_config->dpll_hw_state.pll6,
  10773. pipe_config->dpll_hw_state.pll8,
  10774. pipe_config->dpll_hw_state.pll9,
  10775. pipe_config->dpll_hw_state.pll10,
  10776. pipe_config->dpll_hw_state.pcsdw12);
  10777. } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
  10778. DRM_DEBUG_KMS("dpll_hw_state: "
  10779. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10780. pipe_config->dpll_hw_state.ctrl1,
  10781. pipe_config->dpll_hw_state.cfgcr1,
  10782. pipe_config->dpll_hw_state.cfgcr2);
  10783. } else if (HAS_DDI(dev_priv)) {
  10784. DRM_DEBUG_KMS("dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
  10785. pipe_config->dpll_hw_state.wrpll,
  10786. pipe_config->dpll_hw_state.spll);
  10787. } else {
  10788. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10789. "fp0: 0x%x, fp1: 0x%x\n",
  10790. pipe_config->dpll_hw_state.dpll,
  10791. pipe_config->dpll_hw_state.dpll_md,
  10792. pipe_config->dpll_hw_state.fp0,
  10793. pipe_config->dpll_hw_state.fp1);
  10794. }
  10795. DRM_DEBUG_KMS("planes on this crtc\n");
  10796. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10797. char *format_name;
  10798. intel_plane = to_intel_plane(plane);
  10799. if (intel_plane->pipe != crtc->pipe)
  10800. continue;
  10801. state = to_intel_plane_state(plane->state);
  10802. fb = state->base.fb;
  10803. if (!fb) {
  10804. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  10805. plane->base.id, plane->name, state->scaler_id);
  10806. continue;
  10807. }
  10808. format_name = drm_get_format_name(fb->pixel_format);
  10809. DRM_DEBUG_KMS("[PLANE:%d:%s] enabled",
  10810. plane->base.id, plane->name);
  10811. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = %s",
  10812. fb->base.id, fb->width, fb->height, format_name);
  10813. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  10814. state->scaler_id,
  10815. state->base.src.x1 >> 16,
  10816. state->base.src.y1 >> 16,
  10817. drm_rect_width(&state->base.src) >> 16,
  10818. drm_rect_height(&state->base.src) >> 16,
  10819. state->base.dst.x1, state->base.dst.y1,
  10820. drm_rect_width(&state->base.dst),
  10821. drm_rect_height(&state->base.dst));
  10822. kfree(format_name);
  10823. }
  10824. }
  10825. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10826. {
  10827. struct drm_device *dev = state->dev;
  10828. struct drm_connector *connector;
  10829. unsigned int used_ports = 0;
  10830. unsigned int used_mst_ports = 0;
  10831. /*
  10832. * Walk the connector list instead of the encoder
  10833. * list to detect the problem on ddi platforms
  10834. * where there's just one encoder per digital port.
  10835. */
  10836. drm_for_each_connector(connector, dev) {
  10837. struct drm_connector_state *connector_state;
  10838. struct intel_encoder *encoder;
  10839. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  10840. if (!connector_state)
  10841. connector_state = connector->state;
  10842. if (!connector_state->best_encoder)
  10843. continue;
  10844. encoder = to_intel_encoder(connector_state->best_encoder);
  10845. WARN_ON(!connector_state->crtc);
  10846. switch (encoder->type) {
  10847. unsigned int port_mask;
  10848. case INTEL_OUTPUT_UNKNOWN:
  10849. if (WARN_ON(!HAS_DDI(to_i915(dev))))
  10850. break;
  10851. case INTEL_OUTPUT_DP:
  10852. case INTEL_OUTPUT_HDMI:
  10853. case INTEL_OUTPUT_EDP:
  10854. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10855. /* the same port mustn't appear more than once */
  10856. if (used_ports & port_mask)
  10857. return false;
  10858. used_ports |= port_mask;
  10859. break;
  10860. case INTEL_OUTPUT_DP_MST:
  10861. used_mst_ports |=
  10862. 1 << enc_to_mst(&encoder->base)->primary->port;
  10863. break;
  10864. default:
  10865. break;
  10866. }
  10867. }
  10868. /* can't mix MST and SST/HDMI on the same port */
  10869. if (used_ports & used_mst_ports)
  10870. return false;
  10871. return true;
  10872. }
  10873. static void
  10874. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10875. {
  10876. struct drm_crtc_state tmp_state;
  10877. struct intel_crtc_scaler_state scaler_state;
  10878. struct intel_dpll_hw_state dpll_hw_state;
  10879. struct intel_shared_dpll *shared_dpll;
  10880. bool force_thru;
  10881. /* FIXME: before the switch to atomic started, a new pipe_config was
  10882. * kzalloc'd. Code that depends on any field being zero should be
  10883. * fixed, so that the crtc_state can be safely duplicated. For now,
  10884. * only fields that are know to not cause problems are preserved. */
  10885. tmp_state = crtc_state->base;
  10886. scaler_state = crtc_state->scaler_state;
  10887. shared_dpll = crtc_state->shared_dpll;
  10888. dpll_hw_state = crtc_state->dpll_hw_state;
  10889. force_thru = crtc_state->pch_pfit.force_thru;
  10890. memset(crtc_state, 0, sizeof *crtc_state);
  10891. crtc_state->base = tmp_state;
  10892. crtc_state->scaler_state = scaler_state;
  10893. crtc_state->shared_dpll = shared_dpll;
  10894. crtc_state->dpll_hw_state = dpll_hw_state;
  10895. crtc_state->pch_pfit.force_thru = force_thru;
  10896. }
  10897. static int
  10898. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10899. struct intel_crtc_state *pipe_config)
  10900. {
  10901. struct drm_atomic_state *state = pipe_config->base.state;
  10902. struct intel_encoder *encoder;
  10903. struct drm_connector *connector;
  10904. struct drm_connector_state *connector_state;
  10905. int base_bpp, ret = -EINVAL;
  10906. int i;
  10907. bool retry = true;
  10908. clear_intel_crtc_state(pipe_config);
  10909. pipe_config->cpu_transcoder =
  10910. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10911. /*
  10912. * Sanitize sync polarity flags based on requested ones. If neither
  10913. * positive or negative polarity is requested, treat this as meaning
  10914. * negative polarity.
  10915. */
  10916. if (!(pipe_config->base.adjusted_mode.flags &
  10917. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10918. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10919. if (!(pipe_config->base.adjusted_mode.flags &
  10920. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10921. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10922. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10923. pipe_config);
  10924. if (base_bpp < 0)
  10925. goto fail;
  10926. /*
  10927. * Determine the real pipe dimensions. Note that stereo modes can
  10928. * increase the actual pipe size due to the frame doubling and
  10929. * insertion of additional space for blanks between the frame. This
  10930. * is stored in the crtc timings. We use the requested mode to do this
  10931. * computation to clearly distinguish it from the adjusted mode, which
  10932. * can be changed by the connectors in the below retry loop.
  10933. */
  10934. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10935. &pipe_config->pipe_src_w,
  10936. &pipe_config->pipe_src_h);
  10937. for_each_connector_in_state(state, connector, connector_state, i) {
  10938. if (connector_state->crtc != crtc)
  10939. continue;
  10940. encoder = to_intel_encoder(connector_state->best_encoder);
  10941. if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
  10942. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  10943. goto fail;
  10944. }
  10945. /*
  10946. * Determine output_types before calling the .compute_config()
  10947. * hooks so that the hooks can use this information safely.
  10948. */
  10949. pipe_config->output_types |= 1 << encoder->type;
  10950. }
  10951. encoder_retry:
  10952. /* Ensure the port clock defaults are reset when retrying. */
  10953. pipe_config->port_clock = 0;
  10954. pipe_config->pixel_multiplier = 1;
  10955. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10956. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10957. CRTC_STEREO_DOUBLE);
  10958. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10959. * adjust it according to limitations or connector properties, and also
  10960. * a chance to reject the mode entirely.
  10961. */
  10962. for_each_connector_in_state(state, connector, connector_state, i) {
  10963. if (connector_state->crtc != crtc)
  10964. continue;
  10965. encoder = to_intel_encoder(connector_state->best_encoder);
  10966. if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
  10967. DRM_DEBUG_KMS("Encoder config failure\n");
  10968. goto fail;
  10969. }
  10970. }
  10971. /* Set default port clock if not overwritten by the encoder. Needs to be
  10972. * done afterwards in case the encoder adjusts the mode. */
  10973. if (!pipe_config->port_clock)
  10974. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10975. * pipe_config->pixel_multiplier;
  10976. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10977. if (ret < 0) {
  10978. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10979. goto fail;
  10980. }
  10981. if (ret == RETRY) {
  10982. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10983. ret = -EINVAL;
  10984. goto fail;
  10985. }
  10986. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10987. retry = false;
  10988. goto encoder_retry;
  10989. }
  10990. /* Dithering seems to not pass-through bits correctly when it should, so
  10991. * only enable it on 6bpc panels. */
  10992. pipe_config->dither = pipe_config->pipe_bpp == 6*3;
  10993. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  10994. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10995. fail:
  10996. return ret;
  10997. }
  10998. static void
  10999. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  11000. {
  11001. struct drm_crtc *crtc;
  11002. struct drm_crtc_state *crtc_state;
  11003. int i;
  11004. /* Double check state. */
  11005. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11006. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  11007. /* Update hwmode for vblank functions */
  11008. if (crtc->state->active)
  11009. crtc->hwmode = crtc->state->adjusted_mode;
  11010. else
  11011. crtc->hwmode.crtc_clock = 0;
  11012. /*
  11013. * Update legacy state to satisfy fbc code. This can
  11014. * be removed when fbc uses the atomic state.
  11015. */
  11016. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  11017. struct drm_plane_state *plane_state = crtc->primary->state;
  11018. crtc->primary->fb = plane_state->fb;
  11019. crtc->x = plane_state->src_x >> 16;
  11020. crtc->y = plane_state->src_y >> 16;
  11021. }
  11022. }
  11023. }
  11024. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  11025. {
  11026. int diff;
  11027. if (clock1 == clock2)
  11028. return true;
  11029. if (!clock1 || !clock2)
  11030. return false;
  11031. diff = abs(clock1 - clock2);
  11032. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  11033. return true;
  11034. return false;
  11035. }
  11036. static bool
  11037. intel_compare_m_n(unsigned int m, unsigned int n,
  11038. unsigned int m2, unsigned int n2,
  11039. bool exact)
  11040. {
  11041. if (m == m2 && n == n2)
  11042. return true;
  11043. if (exact || !m || !n || !m2 || !n2)
  11044. return false;
  11045. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  11046. if (n > n2) {
  11047. while (n > n2) {
  11048. m2 <<= 1;
  11049. n2 <<= 1;
  11050. }
  11051. } else if (n < n2) {
  11052. while (n < n2) {
  11053. m <<= 1;
  11054. n <<= 1;
  11055. }
  11056. }
  11057. if (n != n2)
  11058. return false;
  11059. return intel_fuzzy_clock_check(m, m2);
  11060. }
  11061. static bool
  11062. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  11063. struct intel_link_m_n *m2_n2,
  11064. bool adjust)
  11065. {
  11066. if (m_n->tu == m2_n2->tu &&
  11067. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  11068. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  11069. intel_compare_m_n(m_n->link_m, m_n->link_n,
  11070. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  11071. if (adjust)
  11072. *m2_n2 = *m_n;
  11073. return true;
  11074. }
  11075. return false;
  11076. }
  11077. static bool
  11078. intel_pipe_config_compare(struct drm_device *dev,
  11079. struct intel_crtc_state *current_config,
  11080. struct intel_crtc_state *pipe_config,
  11081. bool adjust)
  11082. {
  11083. struct drm_i915_private *dev_priv = to_i915(dev);
  11084. bool ret = true;
  11085. #define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
  11086. do { \
  11087. if (!adjust) \
  11088. DRM_ERROR(fmt, ##__VA_ARGS__); \
  11089. else \
  11090. DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
  11091. } while (0)
  11092. #define PIPE_CONF_CHECK_X(name) \
  11093. if (current_config->name != pipe_config->name) { \
  11094. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  11095. "(expected 0x%08x, found 0x%08x)\n", \
  11096. current_config->name, \
  11097. pipe_config->name); \
  11098. ret = false; \
  11099. }
  11100. #define PIPE_CONF_CHECK_I(name) \
  11101. if (current_config->name != pipe_config->name) { \
  11102. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  11103. "(expected %i, found %i)\n", \
  11104. current_config->name, \
  11105. pipe_config->name); \
  11106. ret = false; \
  11107. }
  11108. #define PIPE_CONF_CHECK_P(name) \
  11109. if (current_config->name != pipe_config->name) { \
  11110. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  11111. "(expected %p, found %p)\n", \
  11112. current_config->name, \
  11113. pipe_config->name); \
  11114. ret = false; \
  11115. }
  11116. #define PIPE_CONF_CHECK_M_N(name) \
  11117. if (!intel_compare_link_m_n(&current_config->name, \
  11118. &pipe_config->name,\
  11119. adjust)) { \
  11120. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  11121. "(expected tu %i gmch %i/%i link %i/%i, " \
  11122. "found tu %i, gmch %i/%i link %i/%i)\n", \
  11123. current_config->name.tu, \
  11124. current_config->name.gmch_m, \
  11125. current_config->name.gmch_n, \
  11126. current_config->name.link_m, \
  11127. current_config->name.link_n, \
  11128. pipe_config->name.tu, \
  11129. pipe_config->name.gmch_m, \
  11130. pipe_config->name.gmch_n, \
  11131. pipe_config->name.link_m, \
  11132. pipe_config->name.link_n); \
  11133. ret = false; \
  11134. }
  11135. /* This is required for BDW+ where there is only one set of registers for
  11136. * switching between high and low RR.
  11137. * This macro can be used whenever a comparison has to be made between one
  11138. * hw state and multiple sw state variables.
  11139. */
  11140. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  11141. if (!intel_compare_link_m_n(&current_config->name, \
  11142. &pipe_config->name, adjust) && \
  11143. !intel_compare_link_m_n(&current_config->alt_name, \
  11144. &pipe_config->name, adjust)) { \
  11145. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  11146. "(expected tu %i gmch %i/%i link %i/%i, " \
  11147. "or tu %i gmch %i/%i link %i/%i, " \
  11148. "found tu %i, gmch %i/%i link %i/%i)\n", \
  11149. current_config->name.tu, \
  11150. current_config->name.gmch_m, \
  11151. current_config->name.gmch_n, \
  11152. current_config->name.link_m, \
  11153. current_config->name.link_n, \
  11154. current_config->alt_name.tu, \
  11155. current_config->alt_name.gmch_m, \
  11156. current_config->alt_name.gmch_n, \
  11157. current_config->alt_name.link_m, \
  11158. current_config->alt_name.link_n, \
  11159. pipe_config->name.tu, \
  11160. pipe_config->name.gmch_m, \
  11161. pipe_config->name.gmch_n, \
  11162. pipe_config->name.link_m, \
  11163. pipe_config->name.link_n); \
  11164. ret = false; \
  11165. }
  11166. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  11167. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  11168. INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
  11169. "(expected %i, found %i)\n", \
  11170. current_config->name & (mask), \
  11171. pipe_config->name & (mask)); \
  11172. ret = false; \
  11173. }
  11174. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  11175. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  11176. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  11177. "(expected %i, found %i)\n", \
  11178. current_config->name, \
  11179. pipe_config->name); \
  11180. ret = false; \
  11181. }
  11182. #define PIPE_CONF_QUIRK(quirk) \
  11183. ((current_config->quirks | pipe_config->quirks) & (quirk))
  11184. PIPE_CONF_CHECK_I(cpu_transcoder);
  11185. PIPE_CONF_CHECK_I(has_pch_encoder);
  11186. PIPE_CONF_CHECK_I(fdi_lanes);
  11187. PIPE_CONF_CHECK_M_N(fdi_m_n);
  11188. PIPE_CONF_CHECK_I(lane_count);
  11189. PIPE_CONF_CHECK_X(lane_lat_optim_mask);
  11190. if (INTEL_INFO(dev)->gen < 8) {
  11191. PIPE_CONF_CHECK_M_N(dp_m_n);
  11192. if (current_config->has_drrs)
  11193. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  11194. } else
  11195. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  11196. PIPE_CONF_CHECK_X(output_types);
  11197. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  11198. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  11199. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  11200. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  11201. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  11202. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  11203. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  11204. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  11205. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  11206. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  11207. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  11208. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  11209. PIPE_CONF_CHECK_I(pixel_multiplier);
  11210. PIPE_CONF_CHECK_I(has_hdmi_sink);
  11211. if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
  11212. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11213. PIPE_CONF_CHECK_I(limited_color_range);
  11214. PIPE_CONF_CHECK_I(has_infoframe);
  11215. PIPE_CONF_CHECK_I(has_audio);
  11216. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11217. DRM_MODE_FLAG_INTERLACE);
  11218. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  11219. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11220. DRM_MODE_FLAG_PHSYNC);
  11221. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11222. DRM_MODE_FLAG_NHSYNC);
  11223. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11224. DRM_MODE_FLAG_PVSYNC);
  11225. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11226. DRM_MODE_FLAG_NVSYNC);
  11227. }
  11228. PIPE_CONF_CHECK_X(gmch_pfit.control);
  11229. /* pfit ratios are autocomputed by the hw on gen4+ */
  11230. if (INTEL_INFO(dev)->gen < 4)
  11231. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  11232. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  11233. if (!adjust) {
  11234. PIPE_CONF_CHECK_I(pipe_src_w);
  11235. PIPE_CONF_CHECK_I(pipe_src_h);
  11236. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  11237. if (current_config->pch_pfit.enabled) {
  11238. PIPE_CONF_CHECK_X(pch_pfit.pos);
  11239. PIPE_CONF_CHECK_X(pch_pfit.size);
  11240. }
  11241. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  11242. }
  11243. /* BDW+ don't expose a synchronous way to read the state */
  11244. if (IS_HASWELL(dev_priv))
  11245. PIPE_CONF_CHECK_I(ips_enabled);
  11246. PIPE_CONF_CHECK_I(double_wide);
  11247. PIPE_CONF_CHECK_P(shared_dpll);
  11248. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  11249. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  11250. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  11251. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  11252. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  11253. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  11254. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  11255. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  11256. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  11257. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  11258. PIPE_CONF_CHECK_X(dsi_pll.div);
  11259. if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
  11260. PIPE_CONF_CHECK_I(pipe_bpp);
  11261. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  11262. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  11263. #undef PIPE_CONF_CHECK_X
  11264. #undef PIPE_CONF_CHECK_I
  11265. #undef PIPE_CONF_CHECK_P
  11266. #undef PIPE_CONF_CHECK_FLAGS
  11267. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  11268. #undef PIPE_CONF_QUIRK
  11269. #undef INTEL_ERR_OR_DBG_KMS
  11270. return ret;
  11271. }
  11272. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  11273. const struct intel_crtc_state *pipe_config)
  11274. {
  11275. if (pipe_config->has_pch_encoder) {
  11276. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  11277. &pipe_config->fdi_m_n);
  11278. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  11279. /*
  11280. * FDI already provided one idea for the dotclock.
  11281. * Yell if the encoder disagrees.
  11282. */
  11283. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  11284. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  11285. fdi_dotclock, dotclock);
  11286. }
  11287. }
  11288. static void verify_wm_state(struct drm_crtc *crtc,
  11289. struct drm_crtc_state *new_state)
  11290. {
  11291. struct drm_device *dev = crtc->dev;
  11292. struct drm_i915_private *dev_priv = to_i915(dev);
  11293. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  11294. struct skl_pipe_wm hw_wm, *sw_wm;
  11295. struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
  11296. struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
  11297. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11298. const enum pipe pipe = intel_crtc->pipe;
  11299. int plane, level, max_level = ilk_wm_max_level(dev_priv);
  11300. if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
  11301. return;
  11302. skl_pipe_wm_get_hw_state(crtc, &hw_wm);
  11303. sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
  11304. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  11305. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  11306. /* planes */
  11307. for_each_universal_plane(dev_priv, pipe, plane) {
  11308. hw_plane_wm = &hw_wm.planes[plane];
  11309. sw_plane_wm = &sw_wm->planes[plane];
  11310. /* Watermarks */
  11311. for (level = 0; level <= max_level; level++) {
  11312. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  11313. &sw_plane_wm->wm[level]))
  11314. continue;
  11315. 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",
  11316. pipe_name(pipe), plane + 1, level,
  11317. sw_plane_wm->wm[level].plane_en,
  11318. sw_plane_wm->wm[level].plane_res_b,
  11319. sw_plane_wm->wm[level].plane_res_l,
  11320. hw_plane_wm->wm[level].plane_en,
  11321. hw_plane_wm->wm[level].plane_res_b,
  11322. hw_plane_wm->wm[level].plane_res_l);
  11323. }
  11324. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  11325. &sw_plane_wm->trans_wm)) {
  11326. 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",
  11327. pipe_name(pipe), plane + 1,
  11328. sw_plane_wm->trans_wm.plane_en,
  11329. sw_plane_wm->trans_wm.plane_res_b,
  11330. sw_plane_wm->trans_wm.plane_res_l,
  11331. hw_plane_wm->trans_wm.plane_en,
  11332. hw_plane_wm->trans_wm.plane_res_b,
  11333. hw_plane_wm->trans_wm.plane_res_l);
  11334. }
  11335. /* DDB */
  11336. hw_ddb_entry = &hw_ddb.plane[pipe][plane];
  11337. sw_ddb_entry = &sw_ddb->plane[pipe][plane];
  11338. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  11339. DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
  11340. pipe_name(pipe), plane + 1,
  11341. sw_ddb_entry->start, sw_ddb_entry->end,
  11342. hw_ddb_entry->start, hw_ddb_entry->end);
  11343. }
  11344. }
  11345. /*
  11346. * cursor
  11347. * If the cursor plane isn't active, we may not have updated it's ddb
  11348. * allocation. In that case since the ddb allocation will be updated
  11349. * once the plane becomes visible, we can skip this check
  11350. */
  11351. if (intel_crtc->cursor_addr) {
  11352. hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
  11353. sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
  11354. /* Watermarks */
  11355. for (level = 0; level <= max_level; level++) {
  11356. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  11357. &sw_plane_wm->wm[level]))
  11358. continue;
  11359. 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",
  11360. pipe_name(pipe), level,
  11361. sw_plane_wm->wm[level].plane_en,
  11362. sw_plane_wm->wm[level].plane_res_b,
  11363. sw_plane_wm->wm[level].plane_res_l,
  11364. hw_plane_wm->wm[level].plane_en,
  11365. hw_plane_wm->wm[level].plane_res_b,
  11366. hw_plane_wm->wm[level].plane_res_l);
  11367. }
  11368. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  11369. &sw_plane_wm->trans_wm)) {
  11370. DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  11371. pipe_name(pipe),
  11372. sw_plane_wm->trans_wm.plane_en,
  11373. sw_plane_wm->trans_wm.plane_res_b,
  11374. sw_plane_wm->trans_wm.plane_res_l,
  11375. hw_plane_wm->trans_wm.plane_en,
  11376. hw_plane_wm->trans_wm.plane_res_b,
  11377. hw_plane_wm->trans_wm.plane_res_l);
  11378. }
  11379. /* DDB */
  11380. hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  11381. sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  11382. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  11383. DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
  11384. pipe_name(pipe),
  11385. sw_ddb_entry->start, sw_ddb_entry->end,
  11386. hw_ddb_entry->start, hw_ddb_entry->end);
  11387. }
  11388. }
  11389. }
  11390. static void
  11391. verify_connector_state(struct drm_device *dev,
  11392. struct drm_atomic_state *state,
  11393. struct drm_crtc *crtc)
  11394. {
  11395. struct drm_connector *connector;
  11396. struct drm_connector_state *old_conn_state;
  11397. int i;
  11398. for_each_connector_in_state(state, connector, old_conn_state, i) {
  11399. struct drm_encoder *encoder = connector->encoder;
  11400. struct drm_connector_state *state = connector->state;
  11401. if (state->crtc != crtc)
  11402. continue;
  11403. intel_connector_verify_state(to_intel_connector(connector));
  11404. I915_STATE_WARN(state->best_encoder != encoder,
  11405. "connector's atomic encoder doesn't match legacy encoder\n");
  11406. }
  11407. }
  11408. static void
  11409. verify_encoder_state(struct drm_device *dev)
  11410. {
  11411. struct intel_encoder *encoder;
  11412. struct intel_connector *connector;
  11413. for_each_intel_encoder(dev, encoder) {
  11414. bool enabled = false;
  11415. enum pipe pipe;
  11416. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  11417. encoder->base.base.id,
  11418. encoder->base.name);
  11419. for_each_intel_connector(dev, connector) {
  11420. if (connector->base.state->best_encoder != &encoder->base)
  11421. continue;
  11422. enabled = true;
  11423. I915_STATE_WARN(connector->base.state->crtc !=
  11424. encoder->base.crtc,
  11425. "connector's crtc doesn't match encoder crtc\n");
  11426. }
  11427. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  11428. "encoder's enabled state mismatch "
  11429. "(expected %i, found %i)\n",
  11430. !!encoder->base.crtc, enabled);
  11431. if (!encoder->base.crtc) {
  11432. bool active;
  11433. active = encoder->get_hw_state(encoder, &pipe);
  11434. I915_STATE_WARN(active,
  11435. "encoder detached but still enabled on pipe %c.\n",
  11436. pipe_name(pipe));
  11437. }
  11438. }
  11439. }
  11440. static void
  11441. verify_crtc_state(struct drm_crtc *crtc,
  11442. struct drm_crtc_state *old_crtc_state,
  11443. struct drm_crtc_state *new_crtc_state)
  11444. {
  11445. struct drm_device *dev = crtc->dev;
  11446. struct drm_i915_private *dev_priv = to_i915(dev);
  11447. struct intel_encoder *encoder;
  11448. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11449. struct intel_crtc_state *pipe_config, *sw_config;
  11450. struct drm_atomic_state *old_state;
  11451. bool active;
  11452. old_state = old_crtc_state->state;
  11453. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  11454. pipe_config = to_intel_crtc_state(old_crtc_state);
  11455. memset(pipe_config, 0, sizeof(*pipe_config));
  11456. pipe_config->base.crtc = crtc;
  11457. pipe_config->base.state = old_state;
  11458. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  11459. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  11460. /* hw state is inconsistent with the pipe quirk */
  11461. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  11462. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  11463. active = new_crtc_state->active;
  11464. I915_STATE_WARN(new_crtc_state->active != active,
  11465. "crtc active state doesn't match with hw state "
  11466. "(expected %i, found %i)\n", new_crtc_state->active, active);
  11467. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  11468. "transitional active state does not match atomic hw state "
  11469. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  11470. for_each_encoder_on_crtc(dev, crtc, encoder) {
  11471. enum pipe pipe;
  11472. active = encoder->get_hw_state(encoder, &pipe);
  11473. I915_STATE_WARN(active != new_crtc_state->active,
  11474. "[ENCODER:%i] active %i with crtc active %i\n",
  11475. encoder->base.base.id, active, new_crtc_state->active);
  11476. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  11477. "Encoder connected to wrong pipe %c\n",
  11478. pipe_name(pipe));
  11479. if (active) {
  11480. pipe_config->output_types |= 1 << encoder->type;
  11481. encoder->get_config(encoder, pipe_config);
  11482. }
  11483. }
  11484. if (!new_crtc_state->active)
  11485. return;
  11486. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  11487. sw_config = to_intel_crtc_state(crtc->state);
  11488. if (!intel_pipe_config_compare(dev, sw_config,
  11489. pipe_config, false)) {
  11490. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  11491. intel_dump_pipe_config(intel_crtc, pipe_config,
  11492. "[hw state]");
  11493. intel_dump_pipe_config(intel_crtc, sw_config,
  11494. "[sw state]");
  11495. }
  11496. }
  11497. static void
  11498. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  11499. struct intel_shared_dpll *pll,
  11500. struct drm_crtc *crtc,
  11501. struct drm_crtc_state *new_state)
  11502. {
  11503. struct intel_dpll_hw_state dpll_hw_state;
  11504. unsigned crtc_mask;
  11505. bool active;
  11506. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  11507. DRM_DEBUG_KMS("%s\n", pll->name);
  11508. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  11509. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  11510. I915_STATE_WARN(!pll->on && pll->active_mask,
  11511. "pll in active use but not on in sw tracking\n");
  11512. I915_STATE_WARN(pll->on && !pll->active_mask,
  11513. "pll is on but not used by any active crtc\n");
  11514. I915_STATE_WARN(pll->on != active,
  11515. "pll on state mismatch (expected %i, found %i)\n",
  11516. pll->on, active);
  11517. }
  11518. if (!crtc) {
  11519. I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
  11520. "more active pll users than references: %x vs %x\n",
  11521. pll->active_mask, pll->config.crtc_mask);
  11522. return;
  11523. }
  11524. crtc_mask = 1 << drm_crtc_index(crtc);
  11525. if (new_state->active)
  11526. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  11527. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  11528. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  11529. else
  11530. I915_STATE_WARN(pll->active_mask & crtc_mask,
  11531. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  11532. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  11533. I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
  11534. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  11535. crtc_mask, pll->config.crtc_mask);
  11536. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
  11537. &dpll_hw_state,
  11538. sizeof(dpll_hw_state)),
  11539. "pll hw state mismatch\n");
  11540. }
  11541. static void
  11542. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  11543. struct drm_crtc_state *old_crtc_state,
  11544. struct drm_crtc_state *new_crtc_state)
  11545. {
  11546. struct drm_i915_private *dev_priv = to_i915(dev);
  11547. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  11548. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  11549. if (new_state->shared_dpll)
  11550. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  11551. if (old_state->shared_dpll &&
  11552. old_state->shared_dpll != new_state->shared_dpll) {
  11553. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  11554. struct intel_shared_dpll *pll = old_state->shared_dpll;
  11555. I915_STATE_WARN(pll->active_mask & crtc_mask,
  11556. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  11557. pipe_name(drm_crtc_index(crtc)));
  11558. I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
  11559. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  11560. pipe_name(drm_crtc_index(crtc)));
  11561. }
  11562. }
  11563. static void
  11564. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  11565. struct drm_atomic_state *state,
  11566. struct drm_crtc_state *old_state,
  11567. struct drm_crtc_state *new_state)
  11568. {
  11569. if (!needs_modeset(new_state) &&
  11570. !to_intel_crtc_state(new_state)->update_pipe)
  11571. return;
  11572. verify_wm_state(crtc, new_state);
  11573. verify_connector_state(crtc->dev, state, crtc);
  11574. verify_crtc_state(crtc, old_state, new_state);
  11575. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  11576. }
  11577. static void
  11578. verify_disabled_dpll_state(struct drm_device *dev)
  11579. {
  11580. struct drm_i915_private *dev_priv = to_i915(dev);
  11581. int i;
  11582. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  11583. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  11584. }
  11585. static void
  11586. intel_modeset_verify_disabled(struct drm_device *dev,
  11587. struct drm_atomic_state *state)
  11588. {
  11589. verify_encoder_state(dev);
  11590. verify_connector_state(dev, state, NULL);
  11591. verify_disabled_dpll_state(dev);
  11592. }
  11593. static void update_scanline_offset(struct intel_crtc *crtc)
  11594. {
  11595. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  11596. /*
  11597. * The scanline counter increments at the leading edge of hsync.
  11598. *
  11599. * On most platforms it starts counting from vtotal-1 on the
  11600. * first active line. That means the scanline counter value is
  11601. * always one less than what we would expect. Ie. just after
  11602. * start of vblank, which also occurs at start of hsync (on the
  11603. * last active line), the scanline counter will read vblank_start-1.
  11604. *
  11605. * On gen2 the scanline counter starts counting from 1 instead
  11606. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  11607. * to keep the value positive), instead of adding one.
  11608. *
  11609. * On HSW+ the behaviour of the scanline counter depends on the output
  11610. * type. For DP ports it behaves like most other platforms, but on HDMI
  11611. * there's an extra 1 line difference. So we need to add two instead of
  11612. * one to the value.
  11613. */
  11614. if (IS_GEN2(dev_priv)) {
  11615. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  11616. int vtotal;
  11617. vtotal = adjusted_mode->crtc_vtotal;
  11618. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  11619. vtotal /= 2;
  11620. crtc->scanline_offset = vtotal - 1;
  11621. } else if (HAS_DDI(dev_priv) &&
  11622. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
  11623. crtc->scanline_offset = 2;
  11624. } else
  11625. crtc->scanline_offset = 1;
  11626. }
  11627. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  11628. {
  11629. struct drm_device *dev = state->dev;
  11630. struct drm_i915_private *dev_priv = to_i915(dev);
  11631. struct intel_shared_dpll_config *shared_dpll = NULL;
  11632. struct drm_crtc *crtc;
  11633. struct drm_crtc_state *crtc_state;
  11634. int i;
  11635. if (!dev_priv->display.crtc_compute_clock)
  11636. return;
  11637. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11638. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11639. struct intel_shared_dpll *old_dpll =
  11640. to_intel_crtc_state(crtc->state)->shared_dpll;
  11641. if (!needs_modeset(crtc_state))
  11642. continue;
  11643. to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
  11644. if (!old_dpll)
  11645. continue;
  11646. if (!shared_dpll)
  11647. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  11648. intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
  11649. }
  11650. }
  11651. /*
  11652. * This implements the workaround described in the "notes" section of the mode
  11653. * set sequence documentation. When going from no pipes or single pipe to
  11654. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  11655. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  11656. */
  11657. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  11658. {
  11659. struct drm_crtc_state *crtc_state;
  11660. struct intel_crtc *intel_crtc;
  11661. struct drm_crtc *crtc;
  11662. struct intel_crtc_state *first_crtc_state = NULL;
  11663. struct intel_crtc_state *other_crtc_state = NULL;
  11664. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  11665. int i;
  11666. /* look at all crtc's that are going to be enabled in during modeset */
  11667. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11668. intel_crtc = to_intel_crtc(crtc);
  11669. if (!crtc_state->active || !needs_modeset(crtc_state))
  11670. continue;
  11671. if (first_crtc_state) {
  11672. other_crtc_state = to_intel_crtc_state(crtc_state);
  11673. break;
  11674. } else {
  11675. first_crtc_state = to_intel_crtc_state(crtc_state);
  11676. first_pipe = intel_crtc->pipe;
  11677. }
  11678. }
  11679. /* No workaround needed? */
  11680. if (!first_crtc_state)
  11681. return 0;
  11682. /* w/a possibly needed, check how many crtc's are already enabled. */
  11683. for_each_intel_crtc(state->dev, intel_crtc) {
  11684. struct intel_crtc_state *pipe_config;
  11685. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  11686. if (IS_ERR(pipe_config))
  11687. return PTR_ERR(pipe_config);
  11688. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  11689. if (!pipe_config->base.active ||
  11690. needs_modeset(&pipe_config->base))
  11691. continue;
  11692. /* 2 or more enabled crtcs means no need for w/a */
  11693. if (enabled_pipe != INVALID_PIPE)
  11694. return 0;
  11695. enabled_pipe = intel_crtc->pipe;
  11696. }
  11697. if (enabled_pipe != INVALID_PIPE)
  11698. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  11699. else if (other_crtc_state)
  11700. other_crtc_state->hsw_workaround_pipe = first_pipe;
  11701. return 0;
  11702. }
  11703. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  11704. {
  11705. struct drm_crtc *crtc;
  11706. struct drm_crtc_state *crtc_state;
  11707. int ret = 0;
  11708. /* add all active pipes to the state */
  11709. for_each_crtc(state->dev, crtc) {
  11710. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11711. if (IS_ERR(crtc_state))
  11712. return PTR_ERR(crtc_state);
  11713. if (!crtc_state->active || needs_modeset(crtc_state))
  11714. continue;
  11715. crtc_state->mode_changed = true;
  11716. ret = drm_atomic_add_affected_connectors(state, crtc);
  11717. if (ret)
  11718. break;
  11719. ret = drm_atomic_add_affected_planes(state, crtc);
  11720. if (ret)
  11721. break;
  11722. }
  11723. return ret;
  11724. }
  11725. static int intel_modeset_checks(struct drm_atomic_state *state)
  11726. {
  11727. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11728. struct drm_i915_private *dev_priv = to_i915(state->dev);
  11729. struct drm_crtc *crtc;
  11730. struct drm_crtc_state *crtc_state;
  11731. int ret = 0, i;
  11732. if (!check_digital_port_conflicts(state)) {
  11733. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  11734. return -EINVAL;
  11735. }
  11736. intel_state->modeset = true;
  11737. intel_state->active_crtcs = dev_priv->active_crtcs;
  11738. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11739. if (crtc_state->active)
  11740. intel_state->active_crtcs |= 1 << i;
  11741. else
  11742. intel_state->active_crtcs &= ~(1 << i);
  11743. if (crtc_state->active != crtc->state->active)
  11744. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  11745. }
  11746. /*
  11747. * See if the config requires any additional preparation, e.g.
  11748. * to adjust global state with pipes off. We need to do this
  11749. * here so we can get the modeset_pipe updated config for the new
  11750. * mode set on this crtc. For other crtcs we need to use the
  11751. * adjusted_mode bits in the crtc directly.
  11752. */
  11753. if (dev_priv->display.modeset_calc_cdclk) {
  11754. if (!intel_state->cdclk_pll_vco)
  11755. intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
  11756. if (!intel_state->cdclk_pll_vco)
  11757. intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
  11758. ret = dev_priv->display.modeset_calc_cdclk(state);
  11759. if (ret < 0)
  11760. return ret;
  11761. if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
  11762. intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
  11763. ret = intel_modeset_all_pipes(state);
  11764. if (ret < 0)
  11765. return ret;
  11766. DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
  11767. intel_state->cdclk, intel_state->dev_cdclk);
  11768. } else
  11769. to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
  11770. intel_modeset_clear_plls(state);
  11771. if (IS_HASWELL(dev_priv))
  11772. return haswell_mode_set_planes_workaround(state);
  11773. return 0;
  11774. }
  11775. /*
  11776. * Handle calculation of various watermark data at the end of the atomic check
  11777. * phase. The code here should be run after the per-crtc and per-plane 'check'
  11778. * handlers to ensure that all derived state has been updated.
  11779. */
  11780. static int calc_watermark_data(struct drm_atomic_state *state)
  11781. {
  11782. struct drm_device *dev = state->dev;
  11783. struct drm_i915_private *dev_priv = to_i915(dev);
  11784. /* Is there platform-specific watermark information to calculate? */
  11785. if (dev_priv->display.compute_global_watermarks)
  11786. return dev_priv->display.compute_global_watermarks(state);
  11787. return 0;
  11788. }
  11789. /**
  11790. * intel_atomic_check - validate state object
  11791. * @dev: drm device
  11792. * @state: state to validate
  11793. */
  11794. static int intel_atomic_check(struct drm_device *dev,
  11795. struct drm_atomic_state *state)
  11796. {
  11797. struct drm_i915_private *dev_priv = to_i915(dev);
  11798. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11799. struct drm_crtc *crtc;
  11800. struct drm_crtc_state *crtc_state;
  11801. int ret, i;
  11802. bool any_ms = false;
  11803. ret = drm_atomic_helper_check_modeset(dev, state);
  11804. if (ret)
  11805. return ret;
  11806. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11807. struct intel_crtc_state *pipe_config =
  11808. to_intel_crtc_state(crtc_state);
  11809. /* Catch I915_MODE_FLAG_INHERITED */
  11810. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  11811. crtc_state->mode_changed = true;
  11812. if (!needs_modeset(crtc_state))
  11813. continue;
  11814. if (!crtc_state->enable) {
  11815. any_ms = true;
  11816. continue;
  11817. }
  11818. /* FIXME: For only active_changed we shouldn't need to do any
  11819. * state recomputation at all. */
  11820. ret = drm_atomic_add_affected_connectors(state, crtc);
  11821. if (ret)
  11822. return ret;
  11823. ret = intel_modeset_pipe_config(crtc, pipe_config);
  11824. if (ret) {
  11825. intel_dump_pipe_config(to_intel_crtc(crtc),
  11826. pipe_config, "[failed]");
  11827. return ret;
  11828. }
  11829. if (i915.fastboot &&
  11830. intel_pipe_config_compare(dev,
  11831. to_intel_crtc_state(crtc->state),
  11832. pipe_config, true)) {
  11833. crtc_state->mode_changed = false;
  11834. to_intel_crtc_state(crtc_state)->update_pipe = true;
  11835. }
  11836. if (needs_modeset(crtc_state))
  11837. any_ms = true;
  11838. ret = drm_atomic_add_affected_planes(state, crtc);
  11839. if (ret)
  11840. return ret;
  11841. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  11842. needs_modeset(crtc_state) ?
  11843. "[modeset]" : "[fastset]");
  11844. }
  11845. if (any_ms) {
  11846. ret = intel_modeset_checks(state);
  11847. if (ret)
  11848. return ret;
  11849. } else
  11850. intel_state->cdclk = dev_priv->cdclk_freq;
  11851. ret = drm_atomic_helper_check_planes(dev, state);
  11852. if (ret)
  11853. return ret;
  11854. intel_fbc_choose_crtc(dev_priv, state);
  11855. return calc_watermark_data(state);
  11856. }
  11857. static int intel_atomic_prepare_commit(struct drm_device *dev,
  11858. struct drm_atomic_state *state)
  11859. {
  11860. struct drm_i915_private *dev_priv = to_i915(dev);
  11861. struct drm_crtc_state *crtc_state;
  11862. struct drm_crtc *crtc;
  11863. int i, ret;
  11864. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11865. if (state->legacy_cursor_update)
  11866. continue;
  11867. ret = intel_crtc_wait_for_pending_flips(crtc);
  11868. if (ret)
  11869. return ret;
  11870. if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
  11871. flush_workqueue(dev_priv->wq);
  11872. }
  11873. ret = mutex_lock_interruptible(&dev->struct_mutex);
  11874. if (ret)
  11875. return ret;
  11876. ret = drm_atomic_helper_prepare_planes(dev, state);
  11877. mutex_unlock(&dev->struct_mutex);
  11878. return ret;
  11879. }
  11880. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  11881. {
  11882. struct drm_device *dev = crtc->base.dev;
  11883. if (!dev->max_vblank_count)
  11884. return drm_accurate_vblank_count(&crtc->base);
  11885. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  11886. }
  11887. static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
  11888. struct drm_i915_private *dev_priv,
  11889. unsigned crtc_mask)
  11890. {
  11891. unsigned last_vblank_count[I915_MAX_PIPES];
  11892. enum pipe pipe;
  11893. int ret;
  11894. if (!crtc_mask)
  11895. return;
  11896. for_each_pipe(dev_priv, pipe) {
  11897. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  11898. pipe);
  11899. if (!((1 << pipe) & crtc_mask))
  11900. continue;
  11901. ret = drm_crtc_vblank_get(&crtc->base);
  11902. if (WARN_ON(ret != 0)) {
  11903. crtc_mask &= ~(1 << pipe);
  11904. continue;
  11905. }
  11906. last_vblank_count[pipe] = drm_crtc_vblank_count(&crtc->base);
  11907. }
  11908. for_each_pipe(dev_priv, pipe) {
  11909. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  11910. pipe);
  11911. long lret;
  11912. if (!((1 << pipe) & crtc_mask))
  11913. continue;
  11914. lret = wait_event_timeout(dev->vblank[pipe].queue,
  11915. last_vblank_count[pipe] !=
  11916. drm_crtc_vblank_count(&crtc->base),
  11917. msecs_to_jiffies(50));
  11918. WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
  11919. drm_crtc_vblank_put(&crtc->base);
  11920. }
  11921. }
  11922. static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
  11923. {
  11924. /* fb updated, need to unpin old fb */
  11925. if (crtc_state->fb_changed)
  11926. return true;
  11927. /* wm changes, need vblank before final wm's */
  11928. if (crtc_state->update_wm_post)
  11929. return true;
  11930. /*
  11931. * cxsr is re-enabled after vblank.
  11932. * This is already handled by crtc_state->update_wm_post,
  11933. * but added for clarity.
  11934. */
  11935. if (crtc_state->disable_cxsr)
  11936. return true;
  11937. return false;
  11938. }
  11939. static void intel_update_crtc(struct drm_crtc *crtc,
  11940. struct drm_atomic_state *state,
  11941. struct drm_crtc_state *old_crtc_state,
  11942. unsigned int *crtc_vblank_mask)
  11943. {
  11944. struct drm_device *dev = crtc->dev;
  11945. struct drm_i915_private *dev_priv = to_i915(dev);
  11946. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11947. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc->state);
  11948. bool modeset = needs_modeset(crtc->state);
  11949. if (modeset) {
  11950. update_scanline_offset(intel_crtc);
  11951. dev_priv->display.crtc_enable(pipe_config, state);
  11952. } else {
  11953. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  11954. }
  11955. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  11956. intel_fbc_enable(
  11957. intel_crtc, pipe_config,
  11958. to_intel_plane_state(crtc->primary->state));
  11959. }
  11960. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  11961. if (needs_vblank_wait(pipe_config))
  11962. *crtc_vblank_mask |= drm_crtc_mask(crtc);
  11963. }
  11964. static void intel_update_crtcs(struct drm_atomic_state *state,
  11965. unsigned int *crtc_vblank_mask)
  11966. {
  11967. struct drm_crtc *crtc;
  11968. struct drm_crtc_state *old_crtc_state;
  11969. int i;
  11970. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11971. if (!crtc->state->active)
  11972. continue;
  11973. intel_update_crtc(crtc, state, old_crtc_state,
  11974. crtc_vblank_mask);
  11975. }
  11976. }
  11977. static void skl_update_crtcs(struct drm_atomic_state *state,
  11978. unsigned int *crtc_vblank_mask)
  11979. {
  11980. struct drm_i915_private *dev_priv = to_i915(state->dev);
  11981. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11982. struct drm_crtc *crtc;
  11983. struct intel_crtc *intel_crtc;
  11984. struct drm_crtc_state *old_crtc_state;
  11985. struct intel_crtc_state *cstate;
  11986. unsigned int updated = 0;
  11987. bool progress;
  11988. enum pipe pipe;
  11989. /*
  11990. * Whenever the number of active pipes changes, we need to make sure we
  11991. * update the pipes in the right order so that their ddb allocations
  11992. * never overlap with eachother inbetween CRTC updates. Otherwise we'll
  11993. * cause pipe underruns and other bad stuff.
  11994. */
  11995. do {
  11996. int i;
  11997. progress = false;
  11998. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11999. bool vbl_wait = false;
  12000. unsigned int cmask = drm_crtc_mask(crtc);
  12001. intel_crtc = to_intel_crtc(crtc);
  12002. cstate = to_intel_crtc_state(crtc->state);
  12003. pipe = intel_crtc->pipe;
  12004. if (updated & cmask || !crtc->state->active)
  12005. continue;
  12006. if (skl_ddb_allocation_overlaps(state, intel_crtc))
  12007. continue;
  12008. updated |= cmask;
  12009. /*
  12010. * If this is an already active pipe, it's DDB changed,
  12011. * and this isn't the last pipe that needs updating
  12012. * then we need to wait for a vblank to pass for the
  12013. * new ddb allocation to take effect.
  12014. */
  12015. if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
  12016. &intel_crtc->hw_ddb) &&
  12017. !crtc->state->active_changed &&
  12018. intel_state->wm_results.dirty_pipes != updated)
  12019. vbl_wait = true;
  12020. intel_update_crtc(crtc, state, old_crtc_state,
  12021. crtc_vblank_mask);
  12022. if (vbl_wait)
  12023. intel_wait_for_vblank(dev_priv, pipe);
  12024. progress = true;
  12025. }
  12026. } while (progress);
  12027. }
  12028. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  12029. {
  12030. struct drm_device *dev = state->dev;
  12031. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  12032. struct drm_i915_private *dev_priv = to_i915(dev);
  12033. struct drm_crtc_state *old_crtc_state;
  12034. struct drm_crtc *crtc;
  12035. struct intel_crtc_state *intel_cstate;
  12036. bool hw_check = intel_state->modeset;
  12037. unsigned long put_domains[I915_MAX_PIPES] = {};
  12038. unsigned crtc_vblank_mask = 0;
  12039. int i;
  12040. drm_atomic_helper_wait_for_dependencies(state);
  12041. if (intel_state->modeset)
  12042. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  12043. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12044. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12045. if (needs_modeset(crtc->state) ||
  12046. to_intel_crtc_state(crtc->state)->update_pipe) {
  12047. hw_check = true;
  12048. put_domains[to_intel_crtc(crtc)->pipe] =
  12049. modeset_get_crtc_power_domains(crtc,
  12050. to_intel_crtc_state(crtc->state));
  12051. }
  12052. if (!needs_modeset(crtc->state))
  12053. continue;
  12054. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  12055. if (old_crtc_state->active) {
  12056. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  12057. dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
  12058. intel_crtc->active = false;
  12059. intel_fbc_disable(intel_crtc);
  12060. intel_disable_shared_dpll(intel_crtc);
  12061. /*
  12062. * Underruns don't always raise
  12063. * interrupts, so check manually.
  12064. */
  12065. intel_check_cpu_fifo_underruns(dev_priv);
  12066. intel_check_pch_fifo_underruns(dev_priv);
  12067. if (!crtc->state->active)
  12068. intel_update_watermarks(intel_crtc);
  12069. }
  12070. }
  12071. /* Only after disabling all output pipelines that will be changed can we
  12072. * update the the output configuration. */
  12073. intel_modeset_update_crtc_state(state);
  12074. if (intel_state->modeset) {
  12075. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  12076. if (dev_priv->display.modeset_commit_cdclk &&
  12077. (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
  12078. intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
  12079. dev_priv->display.modeset_commit_cdclk(state);
  12080. /*
  12081. * SKL workaround: bspec recommends we disable the SAGV when we
  12082. * have more then one pipe enabled
  12083. */
  12084. if (!intel_can_enable_sagv(state))
  12085. intel_disable_sagv(dev_priv);
  12086. intel_modeset_verify_disabled(dev, state);
  12087. }
  12088. /* Complete the events for pipes that have now been disabled */
  12089. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12090. bool modeset = needs_modeset(crtc->state);
  12091. /* Complete events for now disable pipes here. */
  12092. if (modeset && !crtc->state->active && crtc->state->event) {
  12093. spin_lock_irq(&dev->event_lock);
  12094. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  12095. spin_unlock_irq(&dev->event_lock);
  12096. crtc->state->event = NULL;
  12097. }
  12098. }
  12099. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  12100. dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
  12101. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  12102. * already, but still need the state for the delayed optimization. To
  12103. * fix this:
  12104. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  12105. * - schedule that vblank worker _before_ calling hw_done
  12106. * - at the start of commit_tail, cancel it _synchrously
  12107. * - switch over to the vblank wait helper in the core after that since
  12108. * we don't need out special handling any more.
  12109. */
  12110. if (!state->legacy_cursor_update)
  12111. intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
  12112. /*
  12113. * Now that the vblank has passed, we can go ahead and program the
  12114. * optimal watermarks on platforms that need two-step watermark
  12115. * programming.
  12116. *
  12117. * TODO: Move this (and other cleanup) to an async worker eventually.
  12118. */
  12119. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12120. intel_cstate = to_intel_crtc_state(crtc->state);
  12121. if (dev_priv->display.optimize_watermarks)
  12122. dev_priv->display.optimize_watermarks(intel_cstate);
  12123. }
  12124. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12125. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  12126. if (put_domains[i])
  12127. modeset_put_power_domains(dev_priv, put_domains[i]);
  12128. intel_modeset_verify_crtc(crtc, state, old_crtc_state, crtc->state);
  12129. }
  12130. if (intel_state->modeset && intel_can_enable_sagv(state))
  12131. intel_enable_sagv(dev_priv);
  12132. drm_atomic_helper_commit_hw_done(state);
  12133. if (intel_state->modeset)
  12134. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  12135. mutex_lock(&dev->struct_mutex);
  12136. drm_atomic_helper_cleanup_planes(dev, state);
  12137. mutex_unlock(&dev->struct_mutex);
  12138. drm_atomic_helper_commit_cleanup_done(state);
  12139. drm_atomic_state_put(state);
  12140. /* As one of the primary mmio accessors, KMS has a high likelihood
  12141. * of triggering bugs in unclaimed access. After we finish
  12142. * modesetting, see if an error has been flagged, and if so
  12143. * enable debugging for the next modeset - and hope we catch
  12144. * the culprit.
  12145. *
  12146. * XXX note that we assume display power is on at this point.
  12147. * This might hold true now but we need to add pm helper to check
  12148. * unclaimed only when the hardware is on, as atomic commits
  12149. * can happen also when the device is completely off.
  12150. */
  12151. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  12152. }
  12153. static void intel_atomic_commit_work(struct work_struct *work)
  12154. {
  12155. struct drm_atomic_state *state =
  12156. container_of(work, struct drm_atomic_state, commit_work);
  12157. intel_atomic_commit_tail(state);
  12158. }
  12159. static int __i915_sw_fence_call
  12160. intel_atomic_commit_ready(struct i915_sw_fence *fence,
  12161. enum i915_sw_fence_notify notify)
  12162. {
  12163. struct intel_atomic_state *state =
  12164. container_of(fence, struct intel_atomic_state, commit_ready);
  12165. switch (notify) {
  12166. case FENCE_COMPLETE:
  12167. if (state->base.commit_work.func)
  12168. queue_work(system_unbound_wq, &state->base.commit_work);
  12169. break;
  12170. case FENCE_FREE:
  12171. drm_atomic_state_put(&state->base);
  12172. break;
  12173. }
  12174. return NOTIFY_DONE;
  12175. }
  12176. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  12177. {
  12178. struct drm_plane_state *old_plane_state;
  12179. struct drm_plane *plane;
  12180. int i;
  12181. for_each_plane_in_state(state, plane, old_plane_state, i)
  12182. i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
  12183. intel_fb_obj(plane->state->fb),
  12184. to_intel_plane(plane)->frontbuffer_bit);
  12185. }
  12186. /**
  12187. * intel_atomic_commit - commit validated state object
  12188. * @dev: DRM device
  12189. * @state: the top-level driver state object
  12190. * @nonblock: nonblocking commit
  12191. *
  12192. * This function commits a top-level state object that has been validated
  12193. * with drm_atomic_helper_check().
  12194. *
  12195. * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
  12196. * nonblocking commits are only safe for pure plane updates. Everything else
  12197. * should work though.
  12198. *
  12199. * RETURNS
  12200. * Zero for success or -errno.
  12201. */
  12202. static int intel_atomic_commit(struct drm_device *dev,
  12203. struct drm_atomic_state *state,
  12204. bool nonblock)
  12205. {
  12206. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  12207. struct drm_i915_private *dev_priv = to_i915(dev);
  12208. int ret = 0;
  12209. if (intel_state->modeset && nonblock) {
  12210. DRM_DEBUG_KMS("nonblocking commit for modeset not yet implemented.\n");
  12211. return -EINVAL;
  12212. }
  12213. ret = drm_atomic_helper_setup_commit(state, nonblock);
  12214. if (ret)
  12215. return ret;
  12216. drm_atomic_state_get(state);
  12217. i915_sw_fence_init(&intel_state->commit_ready,
  12218. intel_atomic_commit_ready);
  12219. ret = intel_atomic_prepare_commit(dev, state);
  12220. if (ret) {
  12221. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  12222. i915_sw_fence_commit(&intel_state->commit_ready);
  12223. return ret;
  12224. }
  12225. drm_atomic_helper_swap_state(state, true);
  12226. dev_priv->wm.distrust_bios_wm = false;
  12227. dev_priv->wm.skl_results = intel_state->wm_results;
  12228. intel_shared_dpll_commit(state);
  12229. intel_atomic_track_fbs(state);
  12230. if (intel_state->modeset) {
  12231. memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
  12232. sizeof(intel_state->min_pixclk));
  12233. dev_priv->active_crtcs = intel_state->active_crtcs;
  12234. dev_priv->atomic_cdclk_freq = intel_state->cdclk;
  12235. }
  12236. drm_atomic_state_get(state);
  12237. INIT_WORK(&state->commit_work,
  12238. nonblock ? intel_atomic_commit_work : NULL);
  12239. i915_sw_fence_commit(&intel_state->commit_ready);
  12240. if (!nonblock) {
  12241. i915_sw_fence_wait(&intel_state->commit_ready);
  12242. intel_atomic_commit_tail(state);
  12243. }
  12244. return 0;
  12245. }
  12246. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  12247. {
  12248. struct drm_device *dev = crtc->dev;
  12249. struct drm_atomic_state *state;
  12250. struct drm_crtc_state *crtc_state;
  12251. int ret;
  12252. state = drm_atomic_state_alloc(dev);
  12253. if (!state) {
  12254. DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
  12255. crtc->base.id, crtc->name);
  12256. return;
  12257. }
  12258. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  12259. retry:
  12260. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  12261. ret = PTR_ERR_OR_ZERO(crtc_state);
  12262. if (!ret) {
  12263. if (!crtc_state->active)
  12264. goto out;
  12265. crtc_state->mode_changed = true;
  12266. ret = drm_atomic_commit(state);
  12267. }
  12268. if (ret == -EDEADLK) {
  12269. drm_atomic_state_clear(state);
  12270. drm_modeset_backoff(state->acquire_ctx);
  12271. goto retry;
  12272. }
  12273. out:
  12274. drm_atomic_state_put(state);
  12275. }
  12276. /*
  12277. * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
  12278. * drm_atomic_helper_legacy_gamma_set() directly.
  12279. */
  12280. static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
  12281. u16 *red, u16 *green, u16 *blue,
  12282. uint32_t size)
  12283. {
  12284. struct drm_device *dev = crtc->dev;
  12285. struct drm_mode_config *config = &dev->mode_config;
  12286. struct drm_crtc_state *state;
  12287. int ret;
  12288. ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
  12289. if (ret)
  12290. return ret;
  12291. /*
  12292. * Make sure we update the legacy properties so this works when
  12293. * atomic is not enabled.
  12294. */
  12295. state = crtc->state;
  12296. drm_object_property_set_value(&crtc->base,
  12297. config->degamma_lut_property,
  12298. (state->degamma_lut) ?
  12299. state->degamma_lut->base.id : 0);
  12300. drm_object_property_set_value(&crtc->base,
  12301. config->ctm_property,
  12302. (state->ctm) ?
  12303. state->ctm->base.id : 0);
  12304. drm_object_property_set_value(&crtc->base,
  12305. config->gamma_lut_property,
  12306. (state->gamma_lut) ?
  12307. state->gamma_lut->base.id : 0);
  12308. return 0;
  12309. }
  12310. static const struct drm_crtc_funcs intel_crtc_funcs = {
  12311. .gamma_set = intel_atomic_legacy_gamma_set,
  12312. .set_config = drm_atomic_helper_set_config,
  12313. .set_property = drm_atomic_helper_crtc_set_property,
  12314. .destroy = intel_crtc_destroy,
  12315. .page_flip = intel_crtc_page_flip,
  12316. .atomic_duplicate_state = intel_crtc_duplicate_state,
  12317. .atomic_destroy_state = intel_crtc_destroy_state,
  12318. };
  12319. /**
  12320. * intel_prepare_plane_fb - Prepare fb for usage on plane
  12321. * @plane: drm plane to prepare for
  12322. * @fb: framebuffer to prepare for presentation
  12323. *
  12324. * Prepares a framebuffer for usage on a display plane. Generally this
  12325. * involves pinning the underlying object and updating the frontbuffer tracking
  12326. * bits. Some older platforms need special physical address handling for
  12327. * cursor planes.
  12328. *
  12329. * Must be called with struct_mutex held.
  12330. *
  12331. * Returns 0 on success, negative error code on failure.
  12332. */
  12333. int
  12334. intel_prepare_plane_fb(struct drm_plane *plane,
  12335. struct drm_plane_state *new_state)
  12336. {
  12337. struct intel_atomic_state *intel_state =
  12338. to_intel_atomic_state(new_state->state);
  12339. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  12340. struct drm_framebuffer *fb = new_state->fb;
  12341. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  12342. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  12343. int ret;
  12344. if (!obj && !old_obj)
  12345. return 0;
  12346. if (old_obj) {
  12347. struct drm_crtc_state *crtc_state =
  12348. drm_atomic_get_existing_crtc_state(new_state->state,
  12349. plane->state->crtc);
  12350. /* Big Hammer, we also need to ensure that any pending
  12351. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  12352. * current scanout is retired before unpinning the old
  12353. * framebuffer. Note that we rely on userspace rendering
  12354. * into the buffer attached to the pipe they are waiting
  12355. * on. If not, userspace generates a GPU hang with IPEHR
  12356. * point to the MI_WAIT_FOR_EVENT.
  12357. *
  12358. * This should only fail upon a hung GPU, in which case we
  12359. * can safely continue.
  12360. */
  12361. if (needs_modeset(crtc_state)) {
  12362. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  12363. old_obj->resv, NULL,
  12364. false, 0,
  12365. GFP_KERNEL);
  12366. if (ret < 0)
  12367. return ret;
  12368. }
  12369. }
  12370. if (new_state->fence) { /* explicit fencing */
  12371. ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
  12372. new_state->fence,
  12373. I915_FENCE_TIMEOUT,
  12374. GFP_KERNEL);
  12375. if (ret < 0)
  12376. return ret;
  12377. }
  12378. if (!obj)
  12379. return 0;
  12380. if (!new_state->fence) { /* implicit fencing */
  12381. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  12382. obj->resv, NULL,
  12383. false, I915_FENCE_TIMEOUT,
  12384. GFP_KERNEL);
  12385. if (ret < 0)
  12386. return ret;
  12387. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  12388. }
  12389. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  12390. INTEL_INFO(dev_priv)->cursor_needs_physical) {
  12391. int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
  12392. ret = i915_gem_object_attach_phys(obj, align);
  12393. if (ret) {
  12394. DRM_DEBUG_KMS("failed to attach phys object\n");
  12395. return ret;
  12396. }
  12397. } else {
  12398. struct i915_vma *vma;
  12399. vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  12400. if (IS_ERR(vma)) {
  12401. DRM_DEBUG_KMS("failed to pin object\n");
  12402. return PTR_ERR(vma);
  12403. }
  12404. }
  12405. return 0;
  12406. }
  12407. /**
  12408. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  12409. * @plane: drm plane to clean up for
  12410. * @fb: old framebuffer that was on plane
  12411. *
  12412. * Cleans up a framebuffer that has just been removed from a plane.
  12413. *
  12414. * Must be called with struct_mutex held.
  12415. */
  12416. void
  12417. intel_cleanup_plane_fb(struct drm_plane *plane,
  12418. struct drm_plane_state *old_state)
  12419. {
  12420. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  12421. struct intel_plane_state *old_intel_state;
  12422. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
  12423. struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
  12424. old_intel_state = to_intel_plane_state(old_state);
  12425. if (!obj && !old_obj)
  12426. return;
  12427. if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
  12428. !INTEL_INFO(dev_priv)->cursor_needs_physical))
  12429. intel_unpin_fb_obj(old_state->fb, old_state->rotation);
  12430. }
  12431. int
  12432. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  12433. {
  12434. int max_scale;
  12435. int crtc_clock, cdclk;
  12436. if (!intel_crtc || !crtc_state->base.enable)
  12437. return DRM_PLANE_HELPER_NO_SCALING;
  12438. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  12439. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  12440. if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
  12441. return DRM_PLANE_HELPER_NO_SCALING;
  12442. /*
  12443. * skl max scale is lower of:
  12444. * close to 3 but not 3, -1 is for that purpose
  12445. * or
  12446. * cdclk/crtc_clock
  12447. */
  12448. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  12449. return max_scale;
  12450. }
  12451. static int
  12452. intel_check_primary_plane(struct drm_plane *plane,
  12453. struct intel_crtc_state *crtc_state,
  12454. struct intel_plane_state *state)
  12455. {
  12456. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  12457. struct drm_crtc *crtc = state->base.crtc;
  12458. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  12459. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  12460. bool can_position = false;
  12461. int ret;
  12462. if (INTEL_GEN(dev_priv) >= 9) {
  12463. /* use scaler when colorkey is not required */
  12464. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  12465. min_scale = 1;
  12466. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  12467. }
  12468. can_position = true;
  12469. }
  12470. ret = drm_plane_helper_check_state(&state->base,
  12471. &state->clip,
  12472. min_scale, max_scale,
  12473. can_position, true);
  12474. if (ret)
  12475. return ret;
  12476. if (!state->base.fb)
  12477. return 0;
  12478. if (INTEL_GEN(dev_priv) >= 9) {
  12479. ret = skl_check_plane_surface(state);
  12480. if (ret)
  12481. return ret;
  12482. }
  12483. return 0;
  12484. }
  12485. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  12486. struct drm_crtc_state *old_crtc_state)
  12487. {
  12488. struct drm_device *dev = crtc->dev;
  12489. struct drm_i915_private *dev_priv = to_i915(dev);
  12490. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12491. struct intel_crtc_state *intel_cstate =
  12492. to_intel_crtc_state(crtc->state);
  12493. struct intel_crtc_state *old_intel_state =
  12494. to_intel_crtc_state(old_crtc_state);
  12495. bool modeset = needs_modeset(crtc->state);
  12496. enum pipe pipe = intel_crtc->pipe;
  12497. /* Perform vblank evasion around commit operation */
  12498. intel_pipe_update_start(intel_crtc);
  12499. if (modeset)
  12500. return;
  12501. if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
  12502. intel_color_set_csc(crtc->state);
  12503. intel_color_load_luts(crtc->state);
  12504. }
  12505. if (intel_cstate->update_pipe) {
  12506. intel_update_pipe_config(intel_crtc, old_intel_state);
  12507. } else if (INTEL_GEN(dev_priv) >= 9) {
  12508. skl_detach_scalers(intel_crtc);
  12509. I915_WRITE(PIPE_WM_LINETIME(pipe),
  12510. intel_cstate->wm.skl.optimal.linetime);
  12511. }
  12512. }
  12513. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  12514. struct drm_crtc_state *old_crtc_state)
  12515. {
  12516. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12517. intel_pipe_update_end(intel_crtc, NULL);
  12518. }
  12519. /**
  12520. * intel_plane_destroy - destroy a plane
  12521. * @plane: plane to destroy
  12522. *
  12523. * Common destruction function for all types of planes (primary, cursor,
  12524. * sprite).
  12525. */
  12526. void intel_plane_destroy(struct drm_plane *plane)
  12527. {
  12528. drm_plane_cleanup(plane);
  12529. kfree(to_intel_plane(plane));
  12530. }
  12531. const struct drm_plane_funcs intel_plane_funcs = {
  12532. .update_plane = drm_atomic_helper_update_plane,
  12533. .disable_plane = drm_atomic_helper_disable_plane,
  12534. .destroy = intel_plane_destroy,
  12535. .set_property = drm_atomic_helper_plane_set_property,
  12536. .atomic_get_property = intel_plane_atomic_get_property,
  12537. .atomic_set_property = intel_plane_atomic_set_property,
  12538. .atomic_duplicate_state = intel_plane_duplicate_state,
  12539. .atomic_destroy_state = intel_plane_destroy_state,
  12540. };
  12541. static struct intel_plane *
  12542. intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  12543. {
  12544. struct intel_plane *primary = NULL;
  12545. struct intel_plane_state *state = NULL;
  12546. const uint32_t *intel_primary_formats;
  12547. unsigned int supported_rotations;
  12548. unsigned int num_formats;
  12549. int ret;
  12550. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  12551. if (!primary) {
  12552. ret = -ENOMEM;
  12553. goto fail;
  12554. }
  12555. state = intel_create_plane_state(&primary->base);
  12556. if (!state) {
  12557. ret = -ENOMEM;
  12558. goto fail;
  12559. }
  12560. primary->base.state = &state->base;
  12561. primary->can_scale = false;
  12562. primary->max_downscale = 1;
  12563. if (INTEL_GEN(dev_priv) >= 9) {
  12564. primary->can_scale = true;
  12565. state->scaler_id = -1;
  12566. }
  12567. primary->pipe = pipe;
  12568. /*
  12569. * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
  12570. * port is hooked to pipe B. Hence we want plane A feeding pipe B.
  12571. */
  12572. if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
  12573. primary->plane = (enum plane) !pipe;
  12574. else
  12575. primary->plane = (enum plane) pipe;
  12576. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  12577. primary->check_plane = intel_check_primary_plane;
  12578. if (INTEL_GEN(dev_priv) >= 9) {
  12579. intel_primary_formats = skl_primary_formats;
  12580. num_formats = ARRAY_SIZE(skl_primary_formats);
  12581. primary->update_plane = skylake_update_primary_plane;
  12582. primary->disable_plane = skylake_disable_primary_plane;
  12583. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12584. intel_primary_formats = i965_primary_formats;
  12585. num_formats = ARRAY_SIZE(i965_primary_formats);
  12586. primary->update_plane = ironlake_update_primary_plane;
  12587. primary->disable_plane = i9xx_disable_primary_plane;
  12588. } else if (INTEL_GEN(dev_priv) >= 4) {
  12589. intel_primary_formats = i965_primary_formats;
  12590. num_formats = ARRAY_SIZE(i965_primary_formats);
  12591. primary->update_plane = i9xx_update_primary_plane;
  12592. primary->disable_plane = i9xx_disable_primary_plane;
  12593. } else {
  12594. intel_primary_formats = i8xx_primary_formats;
  12595. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  12596. primary->update_plane = i9xx_update_primary_plane;
  12597. primary->disable_plane = i9xx_disable_primary_plane;
  12598. }
  12599. if (INTEL_GEN(dev_priv) >= 9)
  12600. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  12601. 0, &intel_plane_funcs,
  12602. intel_primary_formats, num_formats,
  12603. DRM_PLANE_TYPE_PRIMARY,
  12604. "plane 1%c", pipe_name(pipe));
  12605. else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  12606. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  12607. 0, &intel_plane_funcs,
  12608. intel_primary_formats, num_formats,
  12609. DRM_PLANE_TYPE_PRIMARY,
  12610. "primary %c", pipe_name(pipe));
  12611. else
  12612. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  12613. 0, &intel_plane_funcs,
  12614. intel_primary_formats, num_formats,
  12615. DRM_PLANE_TYPE_PRIMARY,
  12616. "plane %c", plane_name(primary->plane));
  12617. if (ret)
  12618. goto fail;
  12619. if (INTEL_GEN(dev_priv) >= 9) {
  12620. supported_rotations =
  12621. DRM_ROTATE_0 | DRM_ROTATE_90 |
  12622. DRM_ROTATE_180 | DRM_ROTATE_270;
  12623. } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  12624. supported_rotations =
  12625. DRM_ROTATE_0 | DRM_ROTATE_180 |
  12626. DRM_REFLECT_X;
  12627. } else if (INTEL_GEN(dev_priv) >= 4) {
  12628. supported_rotations =
  12629. DRM_ROTATE_0 | DRM_ROTATE_180;
  12630. } else {
  12631. supported_rotations = DRM_ROTATE_0;
  12632. }
  12633. if (INTEL_GEN(dev_priv) >= 4)
  12634. drm_plane_create_rotation_property(&primary->base,
  12635. DRM_ROTATE_0,
  12636. supported_rotations);
  12637. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  12638. return primary;
  12639. fail:
  12640. kfree(state);
  12641. kfree(primary);
  12642. return ERR_PTR(ret);
  12643. }
  12644. static int
  12645. intel_check_cursor_plane(struct drm_plane *plane,
  12646. struct intel_crtc_state *crtc_state,
  12647. struct intel_plane_state *state)
  12648. {
  12649. struct drm_framebuffer *fb = state->base.fb;
  12650. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  12651. enum pipe pipe = to_intel_plane(plane)->pipe;
  12652. unsigned stride;
  12653. int ret;
  12654. ret = drm_plane_helper_check_state(&state->base,
  12655. &state->clip,
  12656. DRM_PLANE_HELPER_NO_SCALING,
  12657. DRM_PLANE_HELPER_NO_SCALING,
  12658. true, true);
  12659. if (ret)
  12660. return ret;
  12661. /* if we want to turn off the cursor ignore width and height */
  12662. if (!obj)
  12663. return 0;
  12664. /* Check for which cursor types we support */
  12665. if (!cursor_size_ok(to_i915(plane->dev), state->base.crtc_w,
  12666. state->base.crtc_h)) {
  12667. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  12668. state->base.crtc_w, state->base.crtc_h);
  12669. return -EINVAL;
  12670. }
  12671. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  12672. if (obj->base.size < stride * state->base.crtc_h) {
  12673. DRM_DEBUG_KMS("buffer is too small\n");
  12674. return -ENOMEM;
  12675. }
  12676. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  12677. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  12678. return -EINVAL;
  12679. }
  12680. /*
  12681. * There's something wrong with the cursor on CHV pipe C.
  12682. * If it straddles the left edge of the screen then
  12683. * moving it away from the edge or disabling it often
  12684. * results in a pipe underrun, and often that can lead to
  12685. * dead pipe (constant underrun reported, and it scans
  12686. * out just a solid color). To recover from that, the
  12687. * display power well must be turned off and on again.
  12688. * Refuse the put the cursor into that compromised position.
  12689. */
  12690. if (IS_CHERRYVIEW(to_i915(plane->dev)) && pipe == PIPE_C &&
  12691. state->base.visible && state->base.crtc_x < 0) {
  12692. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  12693. return -EINVAL;
  12694. }
  12695. return 0;
  12696. }
  12697. static void
  12698. intel_disable_cursor_plane(struct drm_plane *plane,
  12699. struct drm_crtc *crtc)
  12700. {
  12701. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12702. intel_crtc->cursor_addr = 0;
  12703. intel_crtc_update_cursor(crtc, NULL);
  12704. }
  12705. static void
  12706. intel_update_cursor_plane(struct drm_plane *plane,
  12707. const struct intel_crtc_state *crtc_state,
  12708. const struct intel_plane_state *state)
  12709. {
  12710. struct drm_crtc *crtc = crtc_state->base.crtc;
  12711. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12712. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  12713. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  12714. uint32_t addr;
  12715. if (!obj)
  12716. addr = 0;
  12717. else if (!INTEL_INFO(dev_priv)->cursor_needs_physical)
  12718. addr = i915_gem_object_ggtt_offset(obj, NULL);
  12719. else
  12720. addr = obj->phys_handle->busaddr;
  12721. intel_crtc->cursor_addr = addr;
  12722. intel_crtc_update_cursor(crtc, state);
  12723. }
  12724. static struct intel_plane *
  12725. intel_cursor_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  12726. {
  12727. struct intel_plane *cursor = NULL;
  12728. struct intel_plane_state *state = NULL;
  12729. int ret;
  12730. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  12731. if (!cursor) {
  12732. ret = -ENOMEM;
  12733. goto fail;
  12734. }
  12735. state = intel_create_plane_state(&cursor->base);
  12736. if (!state) {
  12737. ret = -ENOMEM;
  12738. goto fail;
  12739. }
  12740. cursor->base.state = &state->base;
  12741. cursor->can_scale = false;
  12742. cursor->max_downscale = 1;
  12743. cursor->pipe = pipe;
  12744. cursor->plane = pipe;
  12745. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  12746. cursor->check_plane = intel_check_cursor_plane;
  12747. cursor->update_plane = intel_update_cursor_plane;
  12748. cursor->disable_plane = intel_disable_cursor_plane;
  12749. ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
  12750. 0, &intel_plane_funcs,
  12751. intel_cursor_formats,
  12752. ARRAY_SIZE(intel_cursor_formats),
  12753. DRM_PLANE_TYPE_CURSOR,
  12754. "cursor %c", pipe_name(pipe));
  12755. if (ret)
  12756. goto fail;
  12757. if (INTEL_GEN(dev_priv) >= 4)
  12758. drm_plane_create_rotation_property(&cursor->base,
  12759. DRM_ROTATE_0,
  12760. DRM_ROTATE_0 |
  12761. DRM_ROTATE_180);
  12762. if (INTEL_GEN(dev_priv) >= 9)
  12763. state->scaler_id = -1;
  12764. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  12765. return cursor;
  12766. fail:
  12767. kfree(state);
  12768. kfree(cursor);
  12769. return ERR_PTR(ret);
  12770. }
  12771. static void skl_init_scalers(struct drm_i915_private *dev_priv,
  12772. struct intel_crtc *crtc,
  12773. struct intel_crtc_state *crtc_state)
  12774. {
  12775. struct intel_crtc_scaler_state *scaler_state =
  12776. &crtc_state->scaler_state;
  12777. int i;
  12778. for (i = 0; i < crtc->num_scalers; i++) {
  12779. struct intel_scaler *scaler = &scaler_state->scalers[i];
  12780. scaler->in_use = 0;
  12781. scaler->mode = PS_SCALER_MODE_DYN;
  12782. }
  12783. scaler_state->scaler_id = -1;
  12784. }
  12785. static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
  12786. {
  12787. struct intel_crtc *intel_crtc;
  12788. struct intel_crtc_state *crtc_state = NULL;
  12789. struct intel_plane *primary = NULL;
  12790. struct intel_plane *cursor = NULL;
  12791. int sprite, ret;
  12792. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  12793. if (!intel_crtc)
  12794. return -ENOMEM;
  12795. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  12796. if (!crtc_state) {
  12797. ret = -ENOMEM;
  12798. goto fail;
  12799. }
  12800. intel_crtc->config = crtc_state;
  12801. intel_crtc->base.state = &crtc_state->base;
  12802. crtc_state->base.crtc = &intel_crtc->base;
  12803. /* initialize shared scalers */
  12804. if (INTEL_GEN(dev_priv) >= 9) {
  12805. if (pipe == PIPE_C)
  12806. intel_crtc->num_scalers = 1;
  12807. else
  12808. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  12809. skl_init_scalers(dev_priv, intel_crtc, crtc_state);
  12810. }
  12811. primary = intel_primary_plane_create(dev_priv, pipe);
  12812. if (IS_ERR(primary)) {
  12813. ret = PTR_ERR(primary);
  12814. goto fail;
  12815. }
  12816. for_each_sprite(dev_priv, pipe, sprite) {
  12817. struct intel_plane *plane;
  12818. plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
  12819. if (IS_ERR(plane)) {
  12820. ret = PTR_ERR(plane);
  12821. goto fail;
  12822. }
  12823. }
  12824. cursor = intel_cursor_plane_create(dev_priv, pipe);
  12825. if (IS_ERR(cursor)) {
  12826. ret = PTR_ERR(cursor);
  12827. goto fail;
  12828. }
  12829. ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
  12830. &primary->base, &cursor->base,
  12831. &intel_crtc_funcs,
  12832. "pipe %c", pipe_name(pipe));
  12833. if (ret)
  12834. goto fail;
  12835. intel_crtc->pipe = pipe;
  12836. intel_crtc->plane = primary->plane;
  12837. intel_crtc->cursor_base = ~0;
  12838. intel_crtc->cursor_cntl = ~0;
  12839. intel_crtc->cursor_size = ~0;
  12840. intel_crtc->wm.cxsr_allowed = true;
  12841. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  12842. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  12843. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
  12844. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
  12845. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  12846. intel_color_init(&intel_crtc->base);
  12847. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  12848. return 0;
  12849. fail:
  12850. /*
  12851. * drm_mode_config_cleanup() will free up any
  12852. * crtcs/planes already initialized.
  12853. */
  12854. kfree(crtc_state);
  12855. kfree(intel_crtc);
  12856. return ret;
  12857. }
  12858. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  12859. {
  12860. struct drm_encoder *encoder = connector->base.encoder;
  12861. struct drm_device *dev = connector->base.dev;
  12862. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  12863. if (!encoder || WARN_ON(!encoder->crtc))
  12864. return INVALID_PIPE;
  12865. return to_intel_crtc(encoder->crtc)->pipe;
  12866. }
  12867. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  12868. struct drm_file *file)
  12869. {
  12870. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  12871. struct drm_crtc *drmmode_crtc;
  12872. struct intel_crtc *crtc;
  12873. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  12874. if (!drmmode_crtc)
  12875. return -ENOENT;
  12876. crtc = to_intel_crtc(drmmode_crtc);
  12877. pipe_from_crtc_id->pipe = crtc->pipe;
  12878. return 0;
  12879. }
  12880. static int intel_encoder_clones(struct intel_encoder *encoder)
  12881. {
  12882. struct drm_device *dev = encoder->base.dev;
  12883. struct intel_encoder *source_encoder;
  12884. int index_mask = 0;
  12885. int entry = 0;
  12886. for_each_intel_encoder(dev, source_encoder) {
  12887. if (encoders_cloneable(encoder, source_encoder))
  12888. index_mask |= (1 << entry);
  12889. entry++;
  12890. }
  12891. return index_mask;
  12892. }
  12893. static bool has_edp_a(struct drm_i915_private *dev_priv)
  12894. {
  12895. if (!IS_MOBILE(dev_priv))
  12896. return false;
  12897. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  12898. return false;
  12899. if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  12900. return false;
  12901. return true;
  12902. }
  12903. static bool intel_crt_present(struct drm_device *dev)
  12904. {
  12905. struct drm_i915_private *dev_priv = to_i915(dev);
  12906. if (INTEL_INFO(dev)->gen >= 9)
  12907. return false;
  12908. if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
  12909. return false;
  12910. if (IS_CHERRYVIEW(dev_priv))
  12911. return false;
  12912. if (HAS_PCH_LPT_H(dev_priv) &&
  12913. I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  12914. return false;
  12915. /* DDI E can't be used if DDI A requires 4 lanes */
  12916. if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  12917. return false;
  12918. if (!dev_priv->vbt.int_crt_support)
  12919. return false;
  12920. return true;
  12921. }
  12922. void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
  12923. {
  12924. int pps_num;
  12925. int pps_idx;
  12926. if (HAS_DDI(dev_priv))
  12927. return;
  12928. /*
  12929. * This w/a is needed at least on CPT/PPT, but to be sure apply it
  12930. * everywhere where registers can be write protected.
  12931. */
  12932. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12933. pps_num = 2;
  12934. else
  12935. pps_num = 1;
  12936. for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
  12937. u32 val = I915_READ(PP_CONTROL(pps_idx));
  12938. val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
  12939. I915_WRITE(PP_CONTROL(pps_idx), val);
  12940. }
  12941. }
  12942. static void intel_pps_init(struct drm_i915_private *dev_priv)
  12943. {
  12944. if (HAS_PCH_SPLIT(dev_priv) || IS_BROXTON(dev_priv))
  12945. dev_priv->pps_mmio_base = PCH_PPS_BASE;
  12946. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12947. dev_priv->pps_mmio_base = VLV_PPS_BASE;
  12948. else
  12949. dev_priv->pps_mmio_base = PPS_BASE;
  12950. intel_pps_unlock_regs_wa(dev_priv);
  12951. }
  12952. static void intel_setup_outputs(struct drm_device *dev)
  12953. {
  12954. struct drm_i915_private *dev_priv = to_i915(dev);
  12955. struct intel_encoder *encoder;
  12956. bool dpd_is_edp = false;
  12957. intel_pps_init(dev_priv);
  12958. /*
  12959. * intel_edp_init_connector() depends on this completing first, to
  12960. * prevent the registeration of both eDP and LVDS and the incorrect
  12961. * sharing of the PPS.
  12962. */
  12963. intel_lvds_init(dev);
  12964. if (intel_crt_present(dev))
  12965. intel_crt_init(dev);
  12966. if (IS_BROXTON(dev_priv)) {
  12967. /*
  12968. * FIXME: Broxton doesn't support port detection via the
  12969. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  12970. * detect the ports.
  12971. */
  12972. intel_ddi_init(dev, PORT_A);
  12973. intel_ddi_init(dev, PORT_B);
  12974. intel_ddi_init(dev, PORT_C);
  12975. intel_dsi_init(dev);
  12976. } else if (HAS_DDI(dev_priv)) {
  12977. int found;
  12978. /*
  12979. * Haswell uses DDI functions to detect digital outputs.
  12980. * On SKL pre-D0 the strap isn't connected, so we assume
  12981. * it's there.
  12982. */
  12983. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  12984. /* WaIgnoreDDIAStrap: skl */
  12985. if (found || IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  12986. intel_ddi_init(dev, PORT_A);
  12987. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  12988. * register */
  12989. found = I915_READ(SFUSE_STRAP);
  12990. if (found & SFUSE_STRAP_DDIB_DETECTED)
  12991. intel_ddi_init(dev, PORT_B);
  12992. if (found & SFUSE_STRAP_DDIC_DETECTED)
  12993. intel_ddi_init(dev, PORT_C);
  12994. if (found & SFUSE_STRAP_DDID_DETECTED)
  12995. intel_ddi_init(dev, PORT_D);
  12996. /*
  12997. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  12998. */
  12999. if ((IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) &&
  13000. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  13001. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  13002. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  13003. intel_ddi_init(dev, PORT_E);
  13004. } else if (HAS_PCH_SPLIT(dev_priv)) {
  13005. int found;
  13006. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  13007. if (has_edp_a(dev_priv))
  13008. intel_dp_init(dev, DP_A, PORT_A);
  13009. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  13010. /* PCH SDVOB multiplex with HDMIB */
  13011. found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
  13012. if (!found)
  13013. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  13014. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  13015. intel_dp_init(dev, PCH_DP_B, PORT_B);
  13016. }
  13017. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  13018. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  13019. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  13020. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  13021. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  13022. intel_dp_init(dev, PCH_DP_C, PORT_C);
  13023. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  13024. intel_dp_init(dev, PCH_DP_D, PORT_D);
  13025. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  13026. bool has_edp, has_port;
  13027. /*
  13028. * The DP_DETECTED bit is the latched state of the DDC
  13029. * SDA pin at boot. However since eDP doesn't require DDC
  13030. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  13031. * eDP ports may have been muxed to an alternate function.
  13032. * Thus we can't rely on the DP_DETECTED bit alone to detect
  13033. * eDP ports. Consult the VBT as well as DP_DETECTED to
  13034. * detect eDP ports.
  13035. *
  13036. * Sadly the straps seem to be missing sometimes even for HDMI
  13037. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  13038. * and VBT for the presence of the port. Additionally we can't
  13039. * trust the port type the VBT declares as we've seen at least
  13040. * HDMI ports that the VBT claim are DP or eDP.
  13041. */
  13042. has_edp = intel_dp_is_edp(dev, PORT_B);
  13043. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  13044. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  13045. has_edp &= intel_dp_init(dev, VLV_DP_B, PORT_B);
  13046. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  13047. intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
  13048. has_edp = intel_dp_is_edp(dev, PORT_C);
  13049. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  13050. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  13051. has_edp &= intel_dp_init(dev, VLV_DP_C, PORT_C);
  13052. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  13053. intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
  13054. if (IS_CHERRYVIEW(dev_priv)) {
  13055. /*
  13056. * eDP not supported on port D,
  13057. * so no need to worry about it
  13058. */
  13059. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  13060. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  13061. intel_dp_init(dev, CHV_DP_D, PORT_D);
  13062. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  13063. intel_hdmi_init(dev, CHV_HDMID, PORT_D);
  13064. }
  13065. intel_dsi_init(dev);
  13066. } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
  13067. bool found = false;
  13068. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  13069. DRM_DEBUG_KMS("probing SDVOB\n");
  13070. found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
  13071. if (!found && IS_G4X(dev_priv)) {
  13072. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  13073. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  13074. }
  13075. if (!found && IS_G4X(dev_priv))
  13076. intel_dp_init(dev, DP_B, PORT_B);
  13077. }
  13078. /* Before G4X SDVOC doesn't have its own detect register */
  13079. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  13080. DRM_DEBUG_KMS("probing SDVOC\n");
  13081. found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
  13082. }
  13083. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  13084. if (IS_G4X(dev_priv)) {
  13085. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  13086. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  13087. }
  13088. if (IS_G4X(dev_priv))
  13089. intel_dp_init(dev, DP_C, PORT_C);
  13090. }
  13091. if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
  13092. intel_dp_init(dev, DP_D, PORT_D);
  13093. } else if (IS_GEN2(dev_priv))
  13094. intel_dvo_init(dev);
  13095. if (SUPPORTS_TV(dev_priv))
  13096. intel_tv_init(dev);
  13097. intel_psr_init(dev);
  13098. for_each_intel_encoder(dev, encoder) {
  13099. encoder->base.possible_crtcs = encoder->crtc_mask;
  13100. encoder->base.possible_clones =
  13101. intel_encoder_clones(encoder);
  13102. }
  13103. intel_init_pch_refclk(dev);
  13104. drm_helper_move_panel_connectors_to_head(dev);
  13105. }
  13106. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  13107. {
  13108. struct drm_device *dev = fb->dev;
  13109. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  13110. drm_framebuffer_cleanup(fb);
  13111. mutex_lock(&dev->struct_mutex);
  13112. WARN_ON(!intel_fb->obj->framebuffer_references--);
  13113. i915_gem_object_put(intel_fb->obj);
  13114. mutex_unlock(&dev->struct_mutex);
  13115. kfree(intel_fb);
  13116. }
  13117. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  13118. struct drm_file *file,
  13119. unsigned int *handle)
  13120. {
  13121. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  13122. struct drm_i915_gem_object *obj = intel_fb->obj;
  13123. if (obj->userptr.mm) {
  13124. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  13125. return -EINVAL;
  13126. }
  13127. return drm_gem_handle_create(file, &obj->base, handle);
  13128. }
  13129. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  13130. struct drm_file *file,
  13131. unsigned flags, unsigned color,
  13132. struct drm_clip_rect *clips,
  13133. unsigned num_clips)
  13134. {
  13135. struct drm_device *dev = fb->dev;
  13136. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  13137. struct drm_i915_gem_object *obj = intel_fb->obj;
  13138. mutex_lock(&dev->struct_mutex);
  13139. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  13140. mutex_unlock(&dev->struct_mutex);
  13141. return 0;
  13142. }
  13143. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  13144. .destroy = intel_user_framebuffer_destroy,
  13145. .create_handle = intel_user_framebuffer_create_handle,
  13146. .dirty = intel_user_framebuffer_dirty,
  13147. };
  13148. static
  13149. u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
  13150. uint64_t fb_modifier, uint32_t pixel_format)
  13151. {
  13152. u32 gen = INTEL_INFO(dev_priv)->gen;
  13153. if (gen >= 9) {
  13154. int cpp = drm_format_plane_cpp(pixel_format, 0);
  13155. /* "The stride in bytes must not exceed the of the size of 8K
  13156. * pixels and 32K bytes."
  13157. */
  13158. return min(8192 * cpp, 32768);
  13159. } else if (gen >= 5 && !IS_VALLEYVIEW(dev_priv) &&
  13160. !IS_CHERRYVIEW(dev_priv)) {
  13161. return 32*1024;
  13162. } else if (gen >= 4) {
  13163. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  13164. return 16*1024;
  13165. else
  13166. return 32*1024;
  13167. } else if (gen >= 3) {
  13168. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  13169. return 8*1024;
  13170. else
  13171. return 16*1024;
  13172. } else {
  13173. /* XXX DSPC is limited to 4k tiled */
  13174. return 8*1024;
  13175. }
  13176. }
  13177. static int intel_framebuffer_init(struct drm_device *dev,
  13178. struct intel_framebuffer *intel_fb,
  13179. struct drm_mode_fb_cmd2 *mode_cmd,
  13180. struct drm_i915_gem_object *obj)
  13181. {
  13182. struct drm_i915_private *dev_priv = to_i915(dev);
  13183. unsigned int tiling = i915_gem_object_get_tiling(obj);
  13184. int ret;
  13185. u32 pitch_limit, stride_alignment;
  13186. char *format_name;
  13187. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  13188. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  13189. /*
  13190. * If there's a fence, enforce that
  13191. * the fb modifier and tiling mode match.
  13192. */
  13193. if (tiling != I915_TILING_NONE &&
  13194. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  13195. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  13196. return -EINVAL;
  13197. }
  13198. } else {
  13199. if (tiling == I915_TILING_X) {
  13200. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  13201. } else if (tiling == I915_TILING_Y) {
  13202. DRM_DEBUG("No Y tiling for legacy addfb\n");
  13203. return -EINVAL;
  13204. }
  13205. }
  13206. /* Passed in modifier sanity checking. */
  13207. switch (mode_cmd->modifier[0]) {
  13208. case I915_FORMAT_MOD_Y_TILED:
  13209. case I915_FORMAT_MOD_Yf_TILED:
  13210. if (INTEL_INFO(dev)->gen < 9) {
  13211. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  13212. mode_cmd->modifier[0]);
  13213. return -EINVAL;
  13214. }
  13215. case DRM_FORMAT_MOD_NONE:
  13216. case I915_FORMAT_MOD_X_TILED:
  13217. break;
  13218. default:
  13219. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  13220. mode_cmd->modifier[0]);
  13221. return -EINVAL;
  13222. }
  13223. /*
  13224. * gen2/3 display engine uses the fence if present,
  13225. * so the tiling mode must match the fb modifier exactly.
  13226. */
  13227. if (INTEL_INFO(dev_priv)->gen < 4 &&
  13228. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  13229. DRM_DEBUG("tiling_mode must match fb modifier exactly on gen2/3\n");
  13230. return -EINVAL;
  13231. }
  13232. stride_alignment = intel_fb_stride_alignment(dev_priv,
  13233. mode_cmd->modifier[0],
  13234. mode_cmd->pixel_format);
  13235. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  13236. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  13237. mode_cmd->pitches[0], stride_alignment);
  13238. return -EINVAL;
  13239. }
  13240. pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
  13241. mode_cmd->pixel_format);
  13242. if (mode_cmd->pitches[0] > pitch_limit) {
  13243. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  13244. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  13245. "tiled" : "linear",
  13246. mode_cmd->pitches[0], pitch_limit);
  13247. return -EINVAL;
  13248. }
  13249. /*
  13250. * If there's a fence, enforce that
  13251. * the fb pitch and fence stride match.
  13252. */
  13253. if (tiling != I915_TILING_NONE &&
  13254. mode_cmd->pitches[0] != i915_gem_object_get_stride(obj)) {
  13255. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  13256. mode_cmd->pitches[0],
  13257. i915_gem_object_get_stride(obj));
  13258. return -EINVAL;
  13259. }
  13260. /* Reject formats not supported by any plane early. */
  13261. switch (mode_cmd->pixel_format) {
  13262. case DRM_FORMAT_C8:
  13263. case DRM_FORMAT_RGB565:
  13264. case DRM_FORMAT_XRGB8888:
  13265. case DRM_FORMAT_ARGB8888:
  13266. break;
  13267. case DRM_FORMAT_XRGB1555:
  13268. if (INTEL_INFO(dev)->gen > 3) {
  13269. format_name = drm_get_format_name(mode_cmd->pixel_format);
  13270. DRM_DEBUG("unsupported pixel format: %s\n", format_name);
  13271. kfree(format_name);
  13272. return -EINVAL;
  13273. }
  13274. break;
  13275. case DRM_FORMAT_ABGR8888:
  13276. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  13277. INTEL_INFO(dev)->gen < 9) {
  13278. format_name = drm_get_format_name(mode_cmd->pixel_format);
  13279. DRM_DEBUG("unsupported pixel format: %s\n", format_name);
  13280. kfree(format_name);
  13281. return -EINVAL;
  13282. }
  13283. break;
  13284. case DRM_FORMAT_XBGR8888:
  13285. case DRM_FORMAT_XRGB2101010:
  13286. case DRM_FORMAT_XBGR2101010:
  13287. if (INTEL_INFO(dev)->gen < 4) {
  13288. format_name = drm_get_format_name(mode_cmd->pixel_format);
  13289. DRM_DEBUG("unsupported pixel format: %s\n", format_name);
  13290. kfree(format_name);
  13291. return -EINVAL;
  13292. }
  13293. break;
  13294. case DRM_FORMAT_ABGR2101010:
  13295. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  13296. format_name = drm_get_format_name(mode_cmd->pixel_format);
  13297. DRM_DEBUG("unsupported pixel format: %s\n", format_name);
  13298. kfree(format_name);
  13299. return -EINVAL;
  13300. }
  13301. break;
  13302. case DRM_FORMAT_YUYV:
  13303. case DRM_FORMAT_UYVY:
  13304. case DRM_FORMAT_YVYU:
  13305. case DRM_FORMAT_VYUY:
  13306. if (INTEL_INFO(dev)->gen < 5) {
  13307. format_name = drm_get_format_name(mode_cmd->pixel_format);
  13308. DRM_DEBUG("unsupported pixel format: %s\n", format_name);
  13309. kfree(format_name);
  13310. return -EINVAL;
  13311. }
  13312. break;
  13313. default:
  13314. format_name = drm_get_format_name(mode_cmd->pixel_format);
  13315. DRM_DEBUG("unsupported pixel format: %s\n", format_name);
  13316. kfree(format_name);
  13317. return -EINVAL;
  13318. }
  13319. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  13320. if (mode_cmd->offsets[0] != 0)
  13321. return -EINVAL;
  13322. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  13323. intel_fb->obj = obj;
  13324. ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
  13325. if (ret)
  13326. return ret;
  13327. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  13328. if (ret) {
  13329. DRM_ERROR("framebuffer init failed %d\n", ret);
  13330. return ret;
  13331. }
  13332. intel_fb->obj->framebuffer_references++;
  13333. return 0;
  13334. }
  13335. static struct drm_framebuffer *
  13336. intel_user_framebuffer_create(struct drm_device *dev,
  13337. struct drm_file *filp,
  13338. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  13339. {
  13340. struct drm_framebuffer *fb;
  13341. struct drm_i915_gem_object *obj;
  13342. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  13343. obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
  13344. if (!obj)
  13345. return ERR_PTR(-ENOENT);
  13346. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  13347. if (IS_ERR(fb))
  13348. i915_gem_object_put(obj);
  13349. return fb;
  13350. }
  13351. static const struct drm_mode_config_funcs intel_mode_funcs = {
  13352. .fb_create = intel_user_framebuffer_create,
  13353. .output_poll_changed = intel_fbdev_output_poll_changed,
  13354. .atomic_check = intel_atomic_check,
  13355. .atomic_commit = intel_atomic_commit,
  13356. .atomic_state_alloc = intel_atomic_state_alloc,
  13357. .atomic_state_clear = intel_atomic_state_clear,
  13358. };
  13359. /**
  13360. * intel_init_display_hooks - initialize the display modesetting hooks
  13361. * @dev_priv: device private
  13362. */
  13363. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  13364. {
  13365. if (INTEL_INFO(dev_priv)->gen >= 9) {
  13366. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  13367. dev_priv->display.get_initial_plane_config =
  13368. skylake_get_initial_plane_config;
  13369. dev_priv->display.crtc_compute_clock =
  13370. haswell_crtc_compute_clock;
  13371. dev_priv->display.crtc_enable = haswell_crtc_enable;
  13372. dev_priv->display.crtc_disable = haswell_crtc_disable;
  13373. } else if (HAS_DDI(dev_priv)) {
  13374. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  13375. dev_priv->display.get_initial_plane_config =
  13376. ironlake_get_initial_plane_config;
  13377. dev_priv->display.crtc_compute_clock =
  13378. haswell_crtc_compute_clock;
  13379. dev_priv->display.crtc_enable = haswell_crtc_enable;
  13380. dev_priv->display.crtc_disable = haswell_crtc_disable;
  13381. } else if (HAS_PCH_SPLIT(dev_priv)) {
  13382. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  13383. dev_priv->display.get_initial_plane_config =
  13384. ironlake_get_initial_plane_config;
  13385. dev_priv->display.crtc_compute_clock =
  13386. ironlake_crtc_compute_clock;
  13387. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  13388. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  13389. } else if (IS_CHERRYVIEW(dev_priv)) {
  13390. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13391. dev_priv->display.get_initial_plane_config =
  13392. i9xx_get_initial_plane_config;
  13393. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  13394. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  13395. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13396. } else if (IS_VALLEYVIEW(dev_priv)) {
  13397. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13398. dev_priv->display.get_initial_plane_config =
  13399. i9xx_get_initial_plane_config;
  13400. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  13401. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  13402. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13403. } else if (IS_G4X(dev_priv)) {
  13404. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13405. dev_priv->display.get_initial_plane_config =
  13406. i9xx_get_initial_plane_config;
  13407. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  13408. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13409. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13410. } else if (IS_PINEVIEW(dev_priv)) {
  13411. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13412. dev_priv->display.get_initial_plane_config =
  13413. i9xx_get_initial_plane_config;
  13414. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  13415. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13416. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13417. } else if (!IS_GEN2(dev_priv)) {
  13418. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13419. dev_priv->display.get_initial_plane_config =
  13420. i9xx_get_initial_plane_config;
  13421. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  13422. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13423. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13424. } else {
  13425. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13426. dev_priv->display.get_initial_plane_config =
  13427. i9xx_get_initial_plane_config;
  13428. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  13429. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13430. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13431. }
  13432. /* Returns the core display clock speed */
  13433. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  13434. dev_priv->display.get_display_clock_speed =
  13435. skylake_get_display_clock_speed;
  13436. else if (IS_BROXTON(dev_priv))
  13437. dev_priv->display.get_display_clock_speed =
  13438. broxton_get_display_clock_speed;
  13439. else if (IS_BROADWELL(dev_priv))
  13440. dev_priv->display.get_display_clock_speed =
  13441. broadwell_get_display_clock_speed;
  13442. else if (IS_HASWELL(dev_priv))
  13443. dev_priv->display.get_display_clock_speed =
  13444. haswell_get_display_clock_speed;
  13445. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  13446. dev_priv->display.get_display_clock_speed =
  13447. valleyview_get_display_clock_speed;
  13448. else if (IS_GEN5(dev_priv))
  13449. dev_priv->display.get_display_clock_speed =
  13450. ilk_get_display_clock_speed;
  13451. else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
  13452. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  13453. dev_priv->display.get_display_clock_speed =
  13454. i945_get_display_clock_speed;
  13455. else if (IS_GM45(dev_priv))
  13456. dev_priv->display.get_display_clock_speed =
  13457. gm45_get_display_clock_speed;
  13458. else if (IS_CRESTLINE(dev_priv))
  13459. dev_priv->display.get_display_clock_speed =
  13460. i965gm_get_display_clock_speed;
  13461. else if (IS_PINEVIEW(dev_priv))
  13462. dev_priv->display.get_display_clock_speed =
  13463. pnv_get_display_clock_speed;
  13464. else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
  13465. dev_priv->display.get_display_clock_speed =
  13466. g33_get_display_clock_speed;
  13467. else if (IS_I915G(dev_priv))
  13468. dev_priv->display.get_display_clock_speed =
  13469. i915_get_display_clock_speed;
  13470. else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
  13471. dev_priv->display.get_display_clock_speed =
  13472. i9xx_misc_get_display_clock_speed;
  13473. else if (IS_I915GM(dev_priv))
  13474. dev_priv->display.get_display_clock_speed =
  13475. i915gm_get_display_clock_speed;
  13476. else if (IS_I865G(dev_priv))
  13477. dev_priv->display.get_display_clock_speed =
  13478. i865_get_display_clock_speed;
  13479. else if (IS_I85X(dev_priv))
  13480. dev_priv->display.get_display_clock_speed =
  13481. i85x_get_display_clock_speed;
  13482. else { /* 830 */
  13483. WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
  13484. dev_priv->display.get_display_clock_speed =
  13485. i830_get_display_clock_speed;
  13486. }
  13487. if (IS_GEN5(dev_priv)) {
  13488. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  13489. } else if (IS_GEN6(dev_priv)) {
  13490. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  13491. } else if (IS_IVYBRIDGE(dev_priv)) {
  13492. /* FIXME: detect B0+ stepping and use auto training */
  13493. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  13494. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  13495. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  13496. }
  13497. if (IS_BROADWELL(dev_priv)) {
  13498. dev_priv->display.modeset_commit_cdclk =
  13499. broadwell_modeset_commit_cdclk;
  13500. dev_priv->display.modeset_calc_cdclk =
  13501. broadwell_modeset_calc_cdclk;
  13502. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  13503. dev_priv->display.modeset_commit_cdclk =
  13504. valleyview_modeset_commit_cdclk;
  13505. dev_priv->display.modeset_calc_cdclk =
  13506. valleyview_modeset_calc_cdclk;
  13507. } else if (IS_BROXTON(dev_priv)) {
  13508. dev_priv->display.modeset_commit_cdclk =
  13509. bxt_modeset_commit_cdclk;
  13510. dev_priv->display.modeset_calc_cdclk =
  13511. bxt_modeset_calc_cdclk;
  13512. } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
  13513. dev_priv->display.modeset_commit_cdclk =
  13514. skl_modeset_commit_cdclk;
  13515. dev_priv->display.modeset_calc_cdclk =
  13516. skl_modeset_calc_cdclk;
  13517. }
  13518. if (dev_priv->info.gen >= 9)
  13519. dev_priv->display.update_crtcs = skl_update_crtcs;
  13520. else
  13521. dev_priv->display.update_crtcs = intel_update_crtcs;
  13522. switch (INTEL_INFO(dev_priv)->gen) {
  13523. case 2:
  13524. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  13525. break;
  13526. case 3:
  13527. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  13528. break;
  13529. case 4:
  13530. case 5:
  13531. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  13532. break;
  13533. case 6:
  13534. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  13535. break;
  13536. case 7:
  13537. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  13538. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  13539. break;
  13540. case 9:
  13541. /* Drop through - unsupported since execlist only. */
  13542. default:
  13543. /* Default just returns -ENODEV to indicate unsupported */
  13544. dev_priv->display.queue_flip = intel_default_queue_flip;
  13545. }
  13546. }
  13547. /*
  13548. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  13549. * resume, or other times. This quirk makes sure that's the case for
  13550. * affected systems.
  13551. */
  13552. static void quirk_pipea_force(struct drm_device *dev)
  13553. {
  13554. struct drm_i915_private *dev_priv = to_i915(dev);
  13555. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  13556. DRM_INFO("applying pipe a force quirk\n");
  13557. }
  13558. static void quirk_pipeb_force(struct drm_device *dev)
  13559. {
  13560. struct drm_i915_private *dev_priv = to_i915(dev);
  13561. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  13562. DRM_INFO("applying pipe b force quirk\n");
  13563. }
  13564. /*
  13565. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  13566. */
  13567. static void quirk_ssc_force_disable(struct drm_device *dev)
  13568. {
  13569. struct drm_i915_private *dev_priv = to_i915(dev);
  13570. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  13571. DRM_INFO("applying lvds SSC disable quirk\n");
  13572. }
  13573. /*
  13574. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  13575. * brightness value
  13576. */
  13577. static void quirk_invert_brightness(struct drm_device *dev)
  13578. {
  13579. struct drm_i915_private *dev_priv = to_i915(dev);
  13580. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  13581. DRM_INFO("applying inverted panel brightness quirk\n");
  13582. }
  13583. /* Some VBT's incorrectly indicate no backlight is present */
  13584. static void quirk_backlight_present(struct drm_device *dev)
  13585. {
  13586. struct drm_i915_private *dev_priv = to_i915(dev);
  13587. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  13588. DRM_INFO("applying backlight present quirk\n");
  13589. }
  13590. struct intel_quirk {
  13591. int device;
  13592. int subsystem_vendor;
  13593. int subsystem_device;
  13594. void (*hook)(struct drm_device *dev);
  13595. };
  13596. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  13597. struct intel_dmi_quirk {
  13598. void (*hook)(struct drm_device *dev);
  13599. const struct dmi_system_id (*dmi_id_list)[];
  13600. };
  13601. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  13602. {
  13603. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  13604. return 1;
  13605. }
  13606. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  13607. {
  13608. .dmi_id_list = &(const struct dmi_system_id[]) {
  13609. {
  13610. .callback = intel_dmi_reverse_brightness,
  13611. .ident = "NCR Corporation",
  13612. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  13613. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  13614. },
  13615. },
  13616. { } /* terminating entry */
  13617. },
  13618. .hook = quirk_invert_brightness,
  13619. },
  13620. };
  13621. static struct intel_quirk intel_quirks[] = {
  13622. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  13623. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  13624. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  13625. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  13626. /* 830 needs to leave pipe A & dpll A up */
  13627. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  13628. /* 830 needs to leave pipe B & dpll B up */
  13629. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  13630. /* Lenovo U160 cannot use SSC on LVDS */
  13631. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  13632. /* Sony Vaio Y cannot use SSC on LVDS */
  13633. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  13634. /* Acer Aspire 5734Z must invert backlight brightness */
  13635. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  13636. /* Acer/eMachines G725 */
  13637. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  13638. /* Acer/eMachines e725 */
  13639. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  13640. /* Acer/Packard Bell NCL20 */
  13641. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  13642. /* Acer Aspire 4736Z */
  13643. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  13644. /* Acer Aspire 5336 */
  13645. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  13646. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  13647. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  13648. /* Acer C720 Chromebook (Core i3 4005U) */
  13649. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  13650. /* Apple Macbook 2,1 (Core 2 T7400) */
  13651. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  13652. /* Apple Macbook 4,1 */
  13653. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  13654. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  13655. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  13656. /* HP Chromebook 14 (Celeron 2955U) */
  13657. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  13658. /* Dell Chromebook 11 */
  13659. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  13660. /* Dell Chromebook 11 (2015 version) */
  13661. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  13662. };
  13663. static void intel_init_quirks(struct drm_device *dev)
  13664. {
  13665. struct pci_dev *d = dev->pdev;
  13666. int i;
  13667. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  13668. struct intel_quirk *q = &intel_quirks[i];
  13669. if (d->device == q->device &&
  13670. (d->subsystem_vendor == q->subsystem_vendor ||
  13671. q->subsystem_vendor == PCI_ANY_ID) &&
  13672. (d->subsystem_device == q->subsystem_device ||
  13673. q->subsystem_device == PCI_ANY_ID))
  13674. q->hook(dev);
  13675. }
  13676. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  13677. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  13678. intel_dmi_quirks[i].hook(dev);
  13679. }
  13680. }
  13681. /* Disable the VGA plane that we never use */
  13682. static void i915_disable_vga(struct drm_device *dev)
  13683. {
  13684. struct drm_i915_private *dev_priv = to_i915(dev);
  13685. struct pci_dev *pdev = dev_priv->drm.pdev;
  13686. u8 sr1;
  13687. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  13688. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  13689. vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
  13690. outb(SR01, VGA_SR_INDEX);
  13691. sr1 = inb(VGA_SR_DATA);
  13692. outb(sr1 | 1<<5, VGA_SR_DATA);
  13693. vga_put(pdev, VGA_RSRC_LEGACY_IO);
  13694. udelay(300);
  13695. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  13696. POSTING_READ(vga_reg);
  13697. }
  13698. void intel_modeset_init_hw(struct drm_device *dev)
  13699. {
  13700. struct drm_i915_private *dev_priv = to_i915(dev);
  13701. intel_update_cdclk(dev_priv);
  13702. dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
  13703. intel_init_clock_gating(dev_priv);
  13704. }
  13705. /*
  13706. * Calculate what we think the watermarks should be for the state we've read
  13707. * out of the hardware and then immediately program those watermarks so that
  13708. * we ensure the hardware settings match our internal state.
  13709. *
  13710. * We can calculate what we think WM's should be by creating a duplicate of the
  13711. * current state (which was constructed during hardware readout) and running it
  13712. * through the atomic check code to calculate new watermark values in the
  13713. * state object.
  13714. */
  13715. static void sanitize_watermarks(struct drm_device *dev)
  13716. {
  13717. struct drm_i915_private *dev_priv = to_i915(dev);
  13718. struct drm_atomic_state *state;
  13719. struct drm_crtc *crtc;
  13720. struct drm_crtc_state *cstate;
  13721. struct drm_modeset_acquire_ctx ctx;
  13722. int ret;
  13723. int i;
  13724. /* Only supported on platforms that use atomic watermark design */
  13725. if (!dev_priv->display.optimize_watermarks)
  13726. return;
  13727. /*
  13728. * We need to hold connection_mutex before calling duplicate_state so
  13729. * that the connector loop is protected.
  13730. */
  13731. drm_modeset_acquire_init(&ctx, 0);
  13732. retry:
  13733. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  13734. if (ret == -EDEADLK) {
  13735. drm_modeset_backoff(&ctx);
  13736. goto retry;
  13737. } else if (WARN_ON(ret)) {
  13738. goto fail;
  13739. }
  13740. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  13741. if (WARN_ON(IS_ERR(state)))
  13742. goto fail;
  13743. /*
  13744. * Hardware readout is the only time we don't want to calculate
  13745. * intermediate watermarks (since we don't trust the current
  13746. * watermarks).
  13747. */
  13748. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  13749. ret = intel_atomic_check(dev, state);
  13750. if (ret) {
  13751. /*
  13752. * If we fail here, it means that the hardware appears to be
  13753. * programmed in a way that shouldn't be possible, given our
  13754. * understanding of watermark requirements. This might mean a
  13755. * mistake in the hardware readout code or a mistake in the
  13756. * watermark calculations for a given platform. Raise a WARN
  13757. * so that this is noticeable.
  13758. *
  13759. * If this actually happens, we'll have to just leave the
  13760. * BIOS-programmed watermarks untouched and hope for the best.
  13761. */
  13762. WARN(true, "Could not determine valid watermarks for inherited state\n");
  13763. goto put_state;
  13764. }
  13765. /* Write calculated watermark values back */
  13766. for_each_crtc_in_state(state, crtc, cstate, i) {
  13767. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  13768. cs->wm.need_postvbl_update = true;
  13769. dev_priv->display.optimize_watermarks(cs);
  13770. }
  13771. put_state:
  13772. drm_atomic_state_put(state);
  13773. fail:
  13774. drm_modeset_drop_locks(&ctx);
  13775. drm_modeset_acquire_fini(&ctx);
  13776. }
  13777. int intel_modeset_init(struct drm_device *dev)
  13778. {
  13779. struct drm_i915_private *dev_priv = to_i915(dev);
  13780. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  13781. enum pipe pipe;
  13782. struct intel_crtc *crtc;
  13783. drm_mode_config_init(dev);
  13784. dev->mode_config.min_width = 0;
  13785. dev->mode_config.min_height = 0;
  13786. dev->mode_config.preferred_depth = 24;
  13787. dev->mode_config.prefer_shadow = 1;
  13788. dev->mode_config.allow_fb_modifiers = true;
  13789. dev->mode_config.funcs = &intel_mode_funcs;
  13790. intel_init_quirks(dev);
  13791. intel_init_pm(dev_priv);
  13792. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  13793. return 0;
  13794. /*
  13795. * There may be no VBT; and if the BIOS enabled SSC we can
  13796. * just keep using it to avoid unnecessary flicker. Whereas if the
  13797. * BIOS isn't using it, don't assume it will work even if the VBT
  13798. * indicates as much.
  13799. */
  13800. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
  13801. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  13802. DREF_SSC1_ENABLE);
  13803. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  13804. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  13805. bios_lvds_use_ssc ? "en" : "dis",
  13806. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  13807. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  13808. }
  13809. }
  13810. if (IS_GEN2(dev_priv)) {
  13811. dev->mode_config.max_width = 2048;
  13812. dev->mode_config.max_height = 2048;
  13813. } else if (IS_GEN3(dev_priv)) {
  13814. dev->mode_config.max_width = 4096;
  13815. dev->mode_config.max_height = 4096;
  13816. } else {
  13817. dev->mode_config.max_width = 8192;
  13818. dev->mode_config.max_height = 8192;
  13819. }
  13820. if (IS_845G(dev_priv) || IS_I865G(dev_priv)) {
  13821. dev->mode_config.cursor_width = IS_845G(dev_priv) ? 64 : 512;
  13822. dev->mode_config.cursor_height = 1023;
  13823. } else if (IS_GEN2(dev_priv)) {
  13824. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  13825. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  13826. } else {
  13827. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  13828. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  13829. }
  13830. dev->mode_config.fb_base = ggtt->mappable_base;
  13831. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  13832. INTEL_INFO(dev_priv)->num_pipes,
  13833. INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
  13834. for_each_pipe(dev_priv, pipe) {
  13835. int ret;
  13836. ret = intel_crtc_init(dev_priv, pipe);
  13837. if (ret) {
  13838. drm_mode_config_cleanup(dev);
  13839. return ret;
  13840. }
  13841. }
  13842. intel_update_czclk(dev_priv);
  13843. intel_update_cdclk(dev_priv);
  13844. intel_shared_dpll_init(dev);
  13845. if (dev_priv->max_cdclk_freq == 0)
  13846. intel_update_max_cdclk(dev_priv);
  13847. /* Just disable it once at startup */
  13848. i915_disable_vga(dev);
  13849. intel_setup_outputs(dev);
  13850. drm_modeset_lock_all(dev);
  13851. intel_modeset_setup_hw_state(dev);
  13852. drm_modeset_unlock_all(dev);
  13853. for_each_intel_crtc(dev, crtc) {
  13854. struct intel_initial_plane_config plane_config = {};
  13855. if (!crtc->active)
  13856. continue;
  13857. /*
  13858. * Note that reserving the BIOS fb up front prevents us
  13859. * from stuffing other stolen allocations like the ring
  13860. * on top. This prevents some ugliness at boot time, and
  13861. * can even allow for smooth boot transitions if the BIOS
  13862. * fb is large enough for the active pipe configuration.
  13863. */
  13864. dev_priv->display.get_initial_plane_config(crtc,
  13865. &plane_config);
  13866. /*
  13867. * If the fb is shared between multiple heads, we'll
  13868. * just get the first one.
  13869. */
  13870. intel_find_initial_plane_obj(crtc, &plane_config);
  13871. }
  13872. /*
  13873. * Make sure hardware watermarks really match the state we read out.
  13874. * Note that we need to do this after reconstructing the BIOS fb's
  13875. * since the watermark calculation done here will use pstate->fb.
  13876. */
  13877. sanitize_watermarks(dev);
  13878. return 0;
  13879. }
  13880. static void intel_enable_pipe_a(struct drm_device *dev)
  13881. {
  13882. struct intel_connector *connector;
  13883. struct drm_connector *crt = NULL;
  13884. struct intel_load_detect_pipe load_detect_temp;
  13885. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  13886. /* We can't just switch on the pipe A, we need to set things up with a
  13887. * proper mode and output configuration. As a gross hack, enable pipe A
  13888. * by enabling the load detect pipe once. */
  13889. for_each_intel_connector(dev, connector) {
  13890. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  13891. crt = &connector->base;
  13892. break;
  13893. }
  13894. }
  13895. if (!crt)
  13896. return;
  13897. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  13898. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  13899. }
  13900. static bool
  13901. intel_check_plane_mapping(struct intel_crtc *crtc)
  13902. {
  13903. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  13904. u32 val;
  13905. if (INTEL_INFO(dev_priv)->num_pipes == 1)
  13906. return true;
  13907. val = I915_READ(DSPCNTR(!crtc->plane));
  13908. if ((val & DISPLAY_PLANE_ENABLE) &&
  13909. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  13910. return false;
  13911. return true;
  13912. }
  13913. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  13914. {
  13915. struct drm_device *dev = crtc->base.dev;
  13916. struct intel_encoder *encoder;
  13917. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  13918. return true;
  13919. return false;
  13920. }
  13921. static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
  13922. {
  13923. struct drm_device *dev = encoder->base.dev;
  13924. struct intel_connector *connector;
  13925. for_each_connector_on_encoder(dev, &encoder->base, connector)
  13926. return connector;
  13927. return NULL;
  13928. }
  13929. static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
  13930. enum transcoder pch_transcoder)
  13931. {
  13932. return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
  13933. (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
  13934. }
  13935. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  13936. {
  13937. struct drm_device *dev = crtc->base.dev;
  13938. struct drm_i915_private *dev_priv = to_i915(dev);
  13939. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  13940. /* Clear any frame start delays used for debugging left by the BIOS */
  13941. if (!transcoder_is_dsi(cpu_transcoder)) {
  13942. i915_reg_t reg = PIPECONF(cpu_transcoder);
  13943. I915_WRITE(reg,
  13944. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  13945. }
  13946. /* restore vblank interrupts to correct state */
  13947. drm_crtc_vblank_reset(&crtc->base);
  13948. if (crtc->active) {
  13949. struct intel_plane *plane;
  13950. drm_crtc_vblank_on(&crtc->base);
  13951. /* Disable everything but the primary plane */
  13952. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  13953. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  13954. continue;
  13955. plane->disable_plane(&plane->base, &crtc->base);
  13956. }
  13957. }
  13958. /* We need to sanitize the plane -> pipe mapping first because this will
  13959. * disable the crtc (and hence change the state) if it is wrong. Note
  13960. * that gen4+ has a fixed plane -> pipe mapping. */
  13961. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  13962. bool plane;
  13963. DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
  13964. crtc->base.base.id, crtc->base.name);
  13965. /* Pipe has the wrong plane attached and the plane is active.
  13966. * Temporarily change the plane mapping and disable everything
  13967. * ... */
  13968. plane = crtc->plane;
  13969. to_intel_plane_state(crtc->base.primary->state)->base.visible = true;
  13970. crtc->plane = !plane;
  13971. intel_crtc_disable_noatomic(&crtc->base);
  13972. crtc->plane = plane;
  13973. }
  13974. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  13975. crtc->pipe == PIPE_A && !crtc->active) {
  13976. /* BIOS forgot to enable pipe A, this mostly happens after
  13977. * resume. Force-enable the pipe to fix this, the update_dpms
  13978. * call below we restore the pipe to the right state, but leave
  13979. * the required bits on. */
  13980. intel_enable_pipe_a(dev);
  13981. }
  13982. /* Adjust the state of the output pipe according to whether we
  13983. * have active connectors/encoders. */
  13984. if (crtc->active && !intel_crtc_has_encoders(crtc))
  13985. intel_crtc_disable_noatomic(&crtc->base);
  13986. if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
  13987. /*
  13988. * We start out with underrun reporting disabled to avoid races.
  13989. * For correct bookkeeping mark this on active crtcs.
  13990. *
  13991. * Also on gmch platforms we dont have any hardware bits to
  13992. * disable the underrun reporting. Which means we need to start
  13993. * out with underrun reporting disabled also on inactive pipes,
  13994. * since otherwise we'll complain about the garbage we read when
  13995. * e.g. coming up after runtime pm.
  13996. *
  13997. * No protection against concurrent access is required - at
  13998. * worst a fifo underrun happens which also sets this to false.
  13999. */
  14000. crtc->cpu_fifo_underrun_disabled = true;
  14001. /*
  14002. * We track the PCH trancoder underrun reporting state
  14003. * within the crtc. With crtc for pipe A housing the underrun
  14004. * reporting state for PCH transcoder A, crtc for pipe B housing
  14005. * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
  14006. * and marking underrun reporting as disabled for the non-existing
  14007. * PCH transcoders B and C would prevent enabling the south
  14008. * error interrupt (see cpt_can_enable_serr_int()).
  14009. */
  14010. if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
  14011. crtc->pch_fifo_underrun_disabled = true;
  14012. }
  14013. }
  14014. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  14015. {
  14016. struct intel_connector *connector;
  14017. /* We need to check both for a crtc link (meaning that the
  14018. * encoder is active and trying to read from a pipe) and the
  14019. * pipe itself being active. */
  14020. bool has_active_crtc = encoder->base.crtc &&
  14021. to_intel_crtc(encoder->base.crtc)->active;
  14022. connector = intel_encoder_find_connector(encoder);
  14023. if (connector && !has_active_crtc) {
  14024. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  14025. encoder->base.base.id,
  14026. encoder->base.name);
  14027. /* Connector is active, but has no active pipe. This is
  14028. * fallout from our resume register restoring. Disable
  14029. * the encoder manually again. */
  14030. if (encoder->base.crtc) {
  14031. struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
  14032. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  14033. encoder->base.base.id,
  14034. encoder->base.name);
  14035. encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  14036. if (encoder->post_disable)
  14037. encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  14038. }
  14039. encoder->base.crtc = NULL;
  14040. /* Inconsistent output/port/pipe state happens presumably due to
  14041. * a bug in one of the get_hw_state functions. Or someplace else
  14042. * in our code, like the register restore mess on resume. Clamp
  14043. * things to off as a safer default. */
  14044. connector->base.dpms = DRM_MODE_DPMS_OFF;
  14045. connector->base.encoder = NULL;
  14046. }
  14047. /* Enabled encoders without active connectors will be fixed in
  14048. * the crtc fixup. */
  14049. }
  14050. void i915_redisable_vga_power_on(struct drm_device *dev)
  14051. {
  14052. struct drm_i915_private *dev_priv = to_i915(dev);
  14053. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  14054. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  14055. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  14056. i915_disable_vga(dev);
  14057. }
  14058. }
  14059. void i915_redisable_vga(struct drm_device *dev)
  14060. {
  14061. struct drm_i915_private *dev_priv = to_i915(dev);
  14062. /* This function can be called both from intel_modeset_setup_hw_state or
  14063. * at a very early point in our resume sequence, where the power well
  14064. * structures are not yet restored. Since this function is at a very
  14065. * paranoid "someone might have enabled VGA while we were not looking"
  14066. * level, just check if the power well is enabled instead of trying to
  14067. * follow the "don't touch the power well if we don't need it" policy
  14068. * the rest of the driver uses. */
  14069. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  14070. return;
  14071. i915_redisable_vga_power_on(dev);
  14072. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  14073. }
  14074. static bool primary_get_hw_state(struct intel_plane *plane)
  14075. {
  14076. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  14077. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  14078. }
  14079. /* FIXME read out full plane state for all planes */
  14080. static void readout_plane_state(struct intel_crtc *crtc)
  14081. {
  14082. struct drm_plane *primary = crtc->base.primary;
  14083. struct intel_plane_state *plane_state =
  14084. to_intel_plane_state(primary->state);
  14085. plane_state->base.visible = crtc->active &&
  14086. primary_get_hw_state(to_intel_plane(primary));
  14087. if (plane_state->base.visible)
  14088. crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
  14089. }
  14090. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  14091. {
  14092. struct drm_i915_private *dev_priv = to_i915(dev);
  14093. enum pipe pipe;
  14094. struct intel_crtc *crtc;
  14095. struct intel_encoder *encoder;
  14096. struct intel_connector *connector;
  14097. int i;
  14098. dev_priv->active_crtcs = 0;
  14099. for_each_intel_crtc(dev, crtc) {
  14100. struct intel_crtc_state *crtc_state = crtc->config;
  14101. int pixclk = 0;
  14102. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  14103. memset(crtc_state, 0, sizeof(*crtc_state));
  14104. crtc_state->base.crtc = &crtc->base;
  14105. crtc_state->base.active = crtc_state->base.enable =
  14106. dev_priv->display.get_pipe_config(crtc, crtc_state);
  14107. crtc->base.enabled = crtc_state->base.enable;
  14108. crtc->active = crtc_state->base.active;
  14109. if (crtc_state->base.active) {
  14110. dev_priv->active_crtcs |= 1 << crtc->pipe;
  14111. if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
  14112. pixclk = ilk_pipe_pixel_rate(crtc_state);
  14113. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  14114. pixclk = crtc_state->base.adjusted_mode.crtc_clock;
  14115. else
  14116. WARN_ON(dev_priv->display.modeset_calc_cdclk);
  14117. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  14118. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  14119. pixclk = DIV_ROUND_UP(pixclk * 100, 95);
  14120. }
  14121. dev_priv->min_pixclk[crtc->pipe] = pixclk;
  14122. readout_plane_state(crtc);
  14123. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  14124. crtc->base.base.id, crtc->base.name,
  14125. crtc->active ? "enabled" : "disabled");
  14126. }
  14127. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  14128. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  14129. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  14130. &pll->config.hw_state);
  14131. pll->config.crtc_mask = 0;
  14132. for_each_intel_crtc(dev, crtc) {
  14133. if (crtc->active && crtc->config->shared_dpll == pll)
  14134. pll->config.crtc_mask |= 1 << crtc->pipe;
  14135. }
  14136. pll->active_mask = pll->config.crtc_mask;
  14137. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  14138. pll->name, pll->config.crtc_mask, pll->on);
  14139. }
  14140. for_each_intel_encoder(dev, encoder) {
  14141. pipe = 0;
  14142. if (encoder->get_hw_state(encoder, &pipe)) {
  14143. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  14144. encoder->base.crtc = &crtc->base;
  14145. crtc->config->output_types |= 1 << encoder->type;
  14146. encoder->get_config(encoder, crtc->config);
  14147. } else {
  14148. encoder->base.crtc = NULL;
  14149. }
  14150. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  14151. encoder->base.base.id,
  14152. encoder->base.name,
  14153. encoder->base.crtc ? "enabled" : "disabled",
  14154. pipe_name(pipe));
  14155. }
  14156. for_each_intel_connector(dev, connector) {
  14157. if (connector->get_hw_state(connector)) {
  14158. connector->base.dpms = DRM_MODE_DPMS_ON;
  14159. encoder = connector->encoder;
  14160. connector->base.encoder = &encoder->base;
  14161. if (encoder->base.crtc &&
  14162. encoder->base.crtc->state->active) {
  14163. /*
  14164. * This has to be done during hardware readout
  14165. * because anything calling .crtc_disable may
  14166. * rely on the connector_mask being accurate.
  14167. */
  14168. encoder->base.crtc->state->connector_mask |=
  14169. 1 << drm_connector_index(&connector->base);
  14170. encoder->base.crtc->state->encoder_mask |=
  14171. 1 << drm_encoder_index(&encoder->base);
  14172. }
  14173. } else {
  14174. connector->base.dpms = DRM_MODE_DPMS_OFF;
  14175. connector->base.encoder = NULL;
  14176. }
  14177. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  14178. connector->base.base.id,
  14179. connector->base.name,
  14180. connector->base.encoder ? "enabled" : "disabled");
  14181. }
  14182. for_each_intel_crtc(dev, crtc) {
  14183. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  14184. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  14185. if (crtc->base.state->active) {
  14186. intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
  14187. intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
  14188. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  14189. /*
  14190. * The initial mode needs to be set in order to keep
  14191. * the atomic core happy. It wants a valid mode if the
  14192. * crtc's enabled, so we do the above call.
  14193. *
  14194. * At this point some state updated by the connectors
  14195. * in their ->detect() callback has not run yet, so
  14196. * no recalculation can be done yet.
  14197. *
  14198. * Even if we could do a recalculation and modeset
  14199. * right now it would cause a double modeset if
  14200. * fbdev or userspace chooses a different initial mode.
  14201. *
  14202. * If that happens, someone indicated they wanted a
  14203. * mode change, which means it's safe to do a full
  14204. * recalculation.
  14205. */
  14206. crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
  14207. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  14208. update_scanline_offset(crtc);
  14209. }
  14210. intel_pipe_config_sanity_check(dev_priv, crtc->config);
  14211. }
  14212. }
  14213. /* Scan out the current hw modeset state,
  14214. * and sanitizes it to the current state
  14215. */
  14216. static void
  14217. intel_modeset_setup_hw_state(struct drm_device *dev)
  14218. {
  14219. struct drm_i915_private *dev_priv = to_i915(dev);
  14220. enum pipe pipe;
  14221. struct intel_crtc *crtc;
  14222. struct intel_encoder *encoder;
  14223. int i;
  14224. intel_modeset_readout_hw_state(dev);
  14225. /* HW state is read out, now we need to sanitize this mess. */
  14226. for_each_intel_encoder(dev, encoder) {
  14227. intel_sanitize_encoder(encoder);
  14228. }
  14229. for_each_pipe(dev_priv, pipe) {
  14230. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  14231. intel_sanitize_crtc(crtc);
  14232. intel_dump_pipe_config(crtc, crtc->config,
  14233. "[setup_hw_state]");
  14234. }
  14235. intel_modeset_update_connector_atomic_state(dev);
  14236. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  14237. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  14238. if (!pll->on || pll->active_mask)
  14239. continue;
  14240. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  14241. pll->funcs.disable(dev_priv, pll);
  14242. pll->on = false;
  14243. }
  14244. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  14245. vlv_wm_get_hw_state(dev);
  14246. else if (IS_GEN9(dev_priv))
  14247. skl_wm_get_hw_state(dev);
  14248. else if (HAS_PCH_SPLIT(dev_priv))
  14249. ilk_wm_get_hw_state(dev);
  14250. for_each_intel_crtc(dev, crtc) {
  14251. unsigned long put_domains;
  14252. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  14253. if (WARN_ON(put_domains))
  14254. modeset_put_power_domains(dev_priv, put_domains);
  14255. }
  14256. intel_display_set_init_power(dev_priv, false);
  14257. intel_fbc_init_pipe_state(dev_priv);
  14258. }
  14259. void intel_display_resume(struct drm_device *dev)
  14260. {
  14261. struct drm_i915_private *dev_priv = to_i915(dev);
  14262. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  14263. struct drm_modeset_acquire_ctx ctx;
  14264. int ret;
  14265. dev_priv->modeset_restore_state = NULL;
  14266. if (state)
  14267. state->acquire_ctx = &ctx;
  14268. /*
  14269. * This is a cludge because with real atomic modeset mode_config.mutex
  14270. * won't be taken. Unfortunately some probed state like
  14271. * audio_codec_enable is still protected by mode_config.mutex, so lock
  14272. * it here for now.
  14273. */
  14274. mutex_lock(&dev->mode_config.mutex);
  14275. drm_modeset_acquire_init(&ctx, 0);
  14276. while (1) {
  14277. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  14278. if (ret != -EDEADLK)
  14279. break;
  14280. drm_modeset_backoff(&ctx);
  14281. }
  14282. if (!ret)
  14283. ret = __intel_display_resume(dev, state);
  14284. drm_modeset_drop_locks(&ctx);
  14285. drm_modeset_acquire_fini(&ctx);
  14286. mutex_unlock(&dev->mode_config.mutex);
  14287. if (ret)
  14288. DRM_ERROR("Restoring old state failed with %i\n", ret);
  14289. drm_atomic_state_put(state);
  14290. }
  14291. void intel_modeset_gem_init(struct drm_device *dev)
  14292. {
  14293. struct drm_i915_private *dev_priv = to_i915(dev);
  14294. struct drm_crtc *c;
  14295. struct drm_i915_gem_object *obj;
  14296. intel_init_gt_powersave(dev_priv);
  14297. intel_modeset_init_hw(dev);
  14298. intel_setup_overlay(dev_priv);
  14299. /*
  14300. * Make sure any fbs we allocated at startup are properly
  14301. * pinned & fenced. When we do the allocation it's too early
  14302. * for this.
  14303. */
  14304. for_each_crtc(dev, c) {
  14305. struct i915_vma *vma;
  14306. obj = intel_fb_obj(c->primary->fb);
  14307. if (obj == NULL)
  14308. continue;
  14309. mutex_lock(&dev->struct_mutex);
  14310. vma = intel_pin_and_fence_fb_obj(c->primary->fb,
  14311. c->primary->state->rotation);
  14312. mutex_unlock(&dev->struct_mutex);
  14313. if (IS_ERR(vma)) {
  14314. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  14315. to_intel_crtc(c)->pipe);
  14316. drm_framebuffer_unreference(c->primary->fb);
  14317. c->primary->fb = NULL;
  14318. c->primary->crtc = c->primary->state->crtc = NULL;
  14319. update_state_fb(c->primary);
  14320. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  14321. }
  14322. }
  14323. }
  14324. int intel_connector_register(struct drm_connector *connector)
  14325. {
  14326. struct intel_connector *intel_connector = to_intel_connector(connector);
  14327. int ret;
  14328. ret = intel_backlight_device_register(intel_connector);
  14329. if (ret)
  14330. goto err;
  14331. return 0;
  14332. err:
  14333. return ret;
  14334. }
  14335. void intel_connector_unregister(struct drm_connector *connector)
  14336. {
  14337. struct intel_connector *intel_connector = to_intel_connector(connector);
  14338. intel_backlight_device_unregister(intel_connector);
  14339. intel_panel_destroy_backlight(connector);
  14340. }
  14341. void intel_modeset_cleanup(struct drm_device *dev)
  14342. {
  14343. struct drm_i915_private *dev_priv = to_i915(dev);
  14344. intel_disable_gt_powersave(dev_priv);
  14345. /*
  14346. * Interrupts and polling as the first thing to avoid creating havoc.
  14347. * Too much stuff here (turning of connectors, ...) would
  14348. * experience fancy races otherwise.
  14349. */
  14350. intel_irq_uninstall(dev_priv);
  14351. /*
  14352. * Due to the hpd irq storm handling the hotplug work can re-arm the
  14353. * poll handlers. Hence disable polling after hpd handling is shut down.
  14354. */
  14355. drm_kms_helper_poll_fini(dev);
  14356. intel_unregister_dsm_handler();
  14357. intel_fbc_global_disable(dev_priv);
  14358. /* flush any delayed tasks or pending work */
  14359. flush_scheduled_work();
  14360. drm_mode_config_cleanup(dev);
  14361. intel_cleanup_overlay(dev_priv);
  14362. intel_cleanup_gt_powersave(dev_priv);
  14363. intel_teardown_gmbus(dev);
  14364. }
  14365. void intel_connector_attach_encoder(struct intel_connector *connector,
  14366. struct intel_encoder *encoder)
  14367. {
  14368. connector->encoder = encoder;
  14369. drm_mode_connector_attach_encoder(&connector->base,
  14370. &encoder->base);
  14371. }
  14372. /*
  14373. * set vga decode state - true == enable VGA decode
  14374. */
  14375. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  14376. {
  14377. struct drm_i915_private *dev_priv = to_i915(dev);
  14378. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  14379. u16 gmch_ctrl;
  14380. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  14381. DRM_ERROR("failed to read control word\n");
  14382. return -EIO;
  14383. }
  14384. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  14385. return 0;
  14386. if (state)
  14387. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  14388. else
  14389. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  14390. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  14391. DRM_ERROR("failed to write control word\n");
  14392. return -EIO;
  14393. }
  14394. return 0;
  14395. }
  14396. #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
  14397. struct intel_display_error_state {
  14398. u32 power_well_driver;
  14399. int num_transcoders;
  14400. struct intel_cursor_error_state {
  14401. u32 control;
  14402. u32 position;
  14403. u32 base;
  14404. u32 size;
  14405. } cursor[I915_MAX_PIPES];
  14406. struct intel_pipe_error_state {
  14407. bool power_domain_on;
  14408. u32 source;
  14409. u32 stat;
  14410. } pipe[I915_MAX_PIPES];
  14411. struct intel_plane_error_state {
  14412. u32 control;
  14413. u32 stride;
  14414. u32 size;
  14415. u32 pos;
  14416. u32 addr;
  14417. u32 surface;
  14418. u32 tile_offset;
  14419. } plane[I915_MAX_PIPES];
  14420. struct intel_transcoder_error_state {
  14421. bool power_domain_on;
  14422. enum transcoder cpu_transcoder;
  14423. u32 conf;
  14424. u32 htotal;
  14425. u32 hblank;
  14426. u32 hsync;
  14427. u32 vtotal;
  14428. u32 vblank;
  14429. u32 vsync;
  14430. } transcoder[4];
  14431. };
  14432. struct intel_display_error_state *
  14433. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  14434. {
  14435. struct intel_display_error_state *error;
  14436. int transcoders[] = {
  14437. TRANSCODER_A,
  14438. TRANSCODER_B,
  14439. TRANSCODER_C,
  14440. TRANSCODER_EDP,
  14441. };
  14442. int i;
  14443. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  14444. return NULL;
  14445. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  14446. if (error == NULL)
  14447. return NULL;
  14448. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  14449. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  14450. for_each_pipe(dev_priv, i) {
  14451. error->pipe[i].power_domain_on =
  14452. __intel_display_power_is_enabled(dev_priv,
  14453. POWER_DOMAIN_PIPE(i));
  14454. if (!error->pipe[i].power_domain_on)
  14455. continue;
  14456. error->cursor[i].control = I915_READ(CURCNTR(i));
  14457. error->cursor[i].position = I915_READ(CURPOS(i));
  14458. error->cursor[i].base = I915_READ(CURBASE(i));
  14459. error->plane[i].control = I915_READ(DSPCNTR(i));
  14460. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  14461. if (INTEL_GEN(dev_priv) <= 3) {
  14462. error->plane[i].size = I915_READ(DSPSIZE(i));
  14463. error->plane[i].pos = I915_READ(DSPPOS(i));
  14464. }
  14465. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  14466. error->plane[i].addr = I915_READ(DSPADDR(i));
  14467. if (INTEL_GEN(dev_priv) >= 4) {
  14468. error->plane[i].surface = I915_READ(DSPSURF(i));
  14469. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  14470. }
  14471. error->pipe[i].source = I915_READ(PIPESRC(i));
  14472. if (HAS_GMCH_DISPLAY(dev_priv))
  14473. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  14474. }
  14475. /* Note: this does not include DSI transcoders. */
  14476. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  14477. if (HAS_DDI(dev_priv))
  14478. error->num_transcoders++; /* Account for eDP. */
  14479. for (i = 0; i < error->num_transcoders; i++) {
  14480. enum transcoder cpu_transcoder = transcoders[i];
  14481. error->transcoder[i].power_domain_on =
  14482. __intel_display_power_is_enabled(dev_priv,
  14483. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  14484. if (!error->transcoder[i].power_domain_on)
  14485. continue;
  14486. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  14487. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  14488. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  14489. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  14490. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  14491. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  14492. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  14493. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  14494. }
  14495. return error;
  14496. }
  14497. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  14498. void
  14499. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  14500. struct drm_device *dev,
  14501. struct intel_display_error_state *error)
  14502. {
  14503. struct drm_i915_private *dev_priv = to_i915(dev);
  14504. int i;
  14505. if (!error)
  14506. return;
  14507. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
  14508. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  14509. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  14510. error->power_well_driver);
  14511. for_each_pipe(dev_priv, i) {
  14512. err_printf(m, "Pipe [%d]:\n", i);
  14513. err_printf(m, " Power: %s\n",
  14514. onoff(error->pipe[i].power_domain_on));
  14515. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  14516. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  14517. err_printf(m, "Plane [%d]:\n", i);
  14518. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  14519. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  14520. if (INTEL_INFO(dev)->gen <= 3) {
  14521. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  14522. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  14523. }
  14524. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  14525. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  14526. if (INTEL_INFO(dev)->gen >= 4) {
  14527. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  14528. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  14529. }
  14530. err_printf(m, "Cursor [%d]:\n", i);
  14531. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  14532. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  14533. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  14534. }
  14535. for (i = 0; i < error->num_transcoders; i++) {
  14536. err_printf(m, "CPU transcoder: %s\n",
  14537. transcoder_name(error->transcoder[i].cpu_transcoder));
  14538. err_printf(m, " Power: %s\n",
  14539. onoff(error->transcoder[i].power_domain_on));
  14540. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  14541. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  14542. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  14543. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  14544. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  14545. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  14546. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  14547. }
  14548. }
  14549. #endif