intel_display.c 487 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458
  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 glk_calc_cdclk(int max_pixclk);
  120. static int bxt_calc_cdclk(int max_pixclk);
  121. struct intel_limit {
  122. struct {
  123. int min, max;
  124. } dot, vco, n, m, m1, m2, p, p1;
  125. struct {
  126. int dot_limit;
  127. int p2_slow, p2_fast;
  128. } p2;
  129. };
  130. /* returns HPLL frequency in kHz */
  131. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  132. {
  133. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  134. /* Obtain SKU information */
  135. mutex_lock(&dev_priv->sb_lock);
  136. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  137. CCK_FUSE_HPLL_FREQ_MASK;
  138. mutex_unlock(&dev_priv->sb_lock);
  139. return vco_freq[hpll_freq] * 1000;
  140. }
  141. int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
  142. const char *name, u32 reg, int ref_freq)
  143. {
  144. u32 val;
  145. int divider;
  146. mutex_lock(&dev_priv->sb_lock);
  147. val = vlv_cck_read(dev_priv, reg);
  148. mutex_unlock(&dev_priv->sb_lock);
  149. divider = val & CCK_FREQUENCY_VALUES;
  150. WARN((val & CCK_FREQUENCY_STATUS) !=
  151. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  152. "%s change in progress\n", name);
  153. return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
  154. }
  155. static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  156. const char *name, u32 reg)
  157. {
  158. if (dev_priv->hpll_freq == 0)
  159. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  160. return vlv_get_cck_clock(dev_priv, name, reg,
  161. dev_priv->hpll_freq);
  162. }
  163. static int
  164. intel_pch_rawclk(struct drm_i915_private *dev_priv)
  165. {
  166. return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
  167. }
  168. static int
  169. intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
  170. {
  171. /* RAWCLK_FREQ_VLV register updated from power well code */
  172. return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
  173. CCK_DISPLAY_REF_CLOCK_CONTROL);
  174. }
  175. static int
  176. intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
  177. {
  178. uint32_t clkcfg;
  179. /* hrawclock is 1/4 the FSB frequency */
  180. clkcfg = I915_READ(CLKCFG);
  181. switch (clkcfg & CLKCFG_FSB_MASK) {
  182. case CLKCFG_FSB_400:
  183. return 100000;
  184. case CLKCFG_FSB_533:
  185. return 133333;
  186. case CLKCFG_FSB_667:
  187. return 166667;
  188. case CLKCFG_FSB_800:
  189. return 200000;
  190. case CLKCFG_FSB_1067:
  191. return 266667;
  192. case CLKCFG_FSB_1333:
  193. return 333333;
  194. /* these two are just a guess; one of them might be right */
  195. case CLKCFG_FSB_1600:
  196. case CLKCFG_FSB_1600_ALT:
  197. return 400000;
  198. default:
  199. return 133333;
  200. }
  201. }
  202. void intel_update_rawclk(struct drm_i915_private *dev_priv)
  203. {
  204. if (HAS_PCH_SPLIT(dev_priv))
  205. dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
  206. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  207. dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
  208. else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
  209. dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
  210. else
  211. return; /* no rawclk on other platforms, or no need to know it */
  212. DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
  213. }
  214. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  215. {
  216. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  217. return;
  218. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  219. CCK_CZ_CLOCK_CONTROL);
  220. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  221. }
  222. static inline u32 /* units of 100MHz */
  223. intel_fdi_link_freq(struct drm_i915_private *dev_priv,
  224. const struct intel_crtc_state *pipe_config)
  225. {
  226. if (HAS_DDI(dev_priv))
  227. return pipe_config->port_clock; /* SPLL */
  228. else if (IS_GEN5(dev_priv))
  229. return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
  230. else
  231. return 270000;
  232. }
  233. static const struct intel_limit intel_limits_i8xx_dac = {
  234. .dot = { .min = 25000, .max = 350000 },
  235. .vco = { .min = 908000, .max = 1512000 },
  236. .n = { .min = 2, .max = 16 },
  237. .m = { .min = 96, .max = 140 },
  238. .m1 = { .min = 18, .max = 26 },
  239. .m2 = { .min = 6, .max = 16 },
  240. .p = { .min = 4, .max = 128 },
  241. .p1 = { .min = 2, .max = 33 },
  242. .p2 = { .dot_limit = 165000,
  243. .p2_slow = 4, .p2_fast = 2 },
  244. };
  245. static const struct intel_limit intel_limits_i8xx_dvo = {
  246. .dot = { .min = 25000, .max = 350000 },
  247. .vco = { .min = 908000, .max = 1512000 },
  248. .n = { .min = 2, .max = 16 },
  249. .m = { .min = 96, .max = 140 },
  250. .m1 = { .min = 18, .max = 26 },
  251. .m2 = { .min = 6, .max = 16 },
  252. .p = { .min = 4, .max = 128 },
  253. .p1 = { .min = 2, .max = 33 },
  254. .p2 = { .dot_limit = 165000,
  255. .p2_slow = 4, .p2_fast = 4 },
  256. };
  257. static const struct intel_limit intel_limits_i8xx_lvds = {
  258. .dot = { .min = 25000, .max = 350000 },
  259. .vco = { .min = 908000, .max = 1512000 },
  260. .n = { .min = 2, .max = 16 },
  261. .m = { .min = 96, .max = 140 },
  262. .m1 = { .min = 18, .max = 26 },
  263. .m2 = { .min = 6, .max = 16 },
  264. .p = { .min = 4, .max = 128 },
  265. .p1 = { .min = 1, .max = 6 },
  266. .p2 = { .dot_limit = 165000,
  267. .p2_slow = 14, .p2_fast = 7 },
  268. };
  269. static const struct intel_limit intel_limits_i9xx_sdvo = {
  270. .dot = { .min = 20000, .max = 400000 },
  271. .vco = { .min = 1400000, .max = 2800000 },
  272. .n = { .min = 1, .max = 6 },
  273. .m = { .min = 70, .max = 120 },
  274. .m1 = { .min = 8, .max = 18 },
  275. .m2 = { .min = 3, .max = 7 },
  276. .p = { .min = 5, .max = 80 },
  277. .p1 = { .min = 1, .max = 8 },
  278. .p2 = { .dot_limit = 200000,
  279. .p2_slow = 10, .p2_fast = 5 },
  280. };
  281. static const struct intel_limit intel_limits_i9xx_lvds = {
  282. .dot = { .min = 20000, .max = 400000 },
  283. .vco = { .min = 1400000, .max = 2800000 },
  284. .n = { .min = 1, .max = 6 },
  285. .m = { .min = 70, .max = 120 },
  286. .m1 = { .min = 8, .max = 18 },
  287. .m2 = { .min = 3, .max = 7 },
  288. .p = { .min = 7, .max = 98 },
  289. .p1 = { .min = 1, .max = 8 },
  290. .p2 = { .dot_limit = 112000,
  291. .p2_slow = 14, .p2_fast = 7 },
  292. };
  293. static const struct intel_limit intel_limits_g4x_sdvo = {
  294. .dot = { .min = 25000, .max = 270000 },
  295. .vco = { .min = 1750000, .max = 3500000},
  296. .n = { .min = 1, .max = 4 },
  297. .m = { .min = 104, .max = 138 },
  298. .m1 = { .min = 17, .max = 23 },
  299. .m2 = { .min = 5, .max = 11 },
  300. .p = { .min = 10, .max = 30 },
  301. .p1 = { .min = 1, .max = 3},
  302. .p2 = { .dot_limit = 270000,
  303. .p2_slow = 10,
  304. .p2_fast = 10
  305. },
  306. };
  307. static const struct intel_limit intel_limits_g4x_hdmi = {
  308. .dot = { .min = 22000, .max = 400000 },
  309. .vco = { .min = 1750000, .max = 3500000},
  310. .n = { .min = 1, .max = 4 },
  311. .m = { .min = 104, .max = 138 },
  312. .m1 = { .min = 16, .max = 23 },
  313. .m2 = { .min = 5, .max = 11 },
  314. .p = { .min = 5, .max = 80 },
  315. .p1 = { .min = 1, .max = 8},
  316. .p2 = { .dot_limit = 165000,
  317. .p2_slow = 10, .p2_fast = 5 },
  318. };
  319. static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
  320. .dot = { .min = 20000, .max = 115000 },
  321. .vco = { .min = 1750000, .max = 3500000 },
  322. .n = { .min = 1, .max = 3 },
  323. .m = { .min = 104, .max = 138 },
  324. .m1 = { .min = 17, .max = 23 },
  325. .m2 = { .min = 5, .max = 11 },
  326. .p = { .min = 28, .max = 112 },
  327. .p1 = { .min = 2, .max = 8 },
  328. .p2 = { .dot_limit = 0,
  329. .p2_slow = 14, .p2_fast = 14
  330. },
  331. };
  332. static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
  333. .dot = { .min = 80000, .max = 224000 },
  334. .vco = { .min = 1750000, .max = 3500000 },
  335. .n = { .min = 1, .max = 3 },
  336. .m = { .min = 104, .max = 138 },
  337. .m1 = { .min = 17, .max = 23 },
  338. .m2 = { .min = 5, .max = 11 },
  339. .p = { .min = 14, .max = 42 },
  340. .p1 = { .min = 2, .max = 6 },
  341. .p2 = { .dot_limit = 0,
  342. .p2_slow = 7, .p2_fast = 7
  343. },
  344. };
  345. static const struct intel_limit intel_limits_pineview_sdvo = {
  346. .dot = { .min = 20000, .max = 400000},
  347. .vco = { .min = 1700000, .max = 3500000 },
  348. /* Pineview's Ncounter is a ring counter */
  349. .n = { .min = 3, .max = 6 },
  350. .m = { .min = 2, .max = 256 },
  351. /* Pineview only has one combined m divider, which we treat as m2. */
  352. .m1 = { .min = 0, .max = 0 },
  353. .m2 = { .min = 0, .max = 254 },
  354. .p = { .min = 5, .max = 80 },
  355. .p1 = { .min = 1, .max = 8 },
  356. .p2 = { .dot_limit = 200000,
  357. .p2_slow = 10, .p2_fast = 5 },
  358. };
  359. static const struct intel_limit intel_limits_pineview_lvds = {
  360. .dot = { .min = 20000, .max = 400000 },
  361. .vco = { .min = 1700000, .max = 3500000 },
  362. .n = { .min = 3, .max = 6 },
  363. .m = { .min = 2, .max = 256 },
  364. .m1 = { .min = 0, .max = 0 },
  365. .m2 = { .min = 0, .max = 254 },
  366. .p = { .min = 7, .max = 112 },
  367. .p1 = { .min = 1, .max = 8 },
  368. .p2 = { .dot_limit = 112000,
  369. .p2_slow = 14, .p2_fast = 14 },
  370. };
  371. /* Ironlake / Sandybridge
  372. *
  373. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  374. * the range value for them is (actual_value - 2).
  375. */
  376. static const struct intel_limit intel_limits_ironlake_dac = {
  377. .dot = { .min = 25000, .max = 350000 },
  378. .vco = { .min = 1760000, .max = 3510000 },
  379. .n = { .min = 1, .max = 5 },
  380. .m = { .min = 79, .max = 127 },
  381. .m1 = { .min = 12, .max = 22 },
  382. .m2 = { .min = 5, .max = 9 },
  383. .p = { .min = 5, .max = 80 },
  384. .p1 = { .min = 1, .max = 8 },
  385. .p2 = { .dot_limit = 225000,
  386. .p2_slow = 10, .p2_fast = 5 },
  387. };
  388. static const struct intel_limit intel_limits_ironlake_single_lvds = {
  389. .dot = { .min = 25000, .max = 350000 },
  390. .vco = { .min = 1760000, .max = 3510000 },
  391. .n = { .min = 1, .max = 3 },
  392. .m = { .min = 79, .max = 118 },
  393. .m1 = { .min = 12, .max = 22 },
  394. .m2 = { .min = 5, .max = 9 },
  395. .p = { .min = 28, .max = 112 },
  396. .p1 = { .min = 2, .max = 8 },
  397. .p2 = { .dot_limit = 225000,
  398. .p2_slow = 14, .p2_fast = 14 },
  399. };
  400. static const struct intel_limit intel_limits_ironlake_dual_lvds = {
  401. .dot = { .min = 25000, .max = 350000 },
  402. .vco = { .min = 1760000, .max = 3510000 },
  403. .n = { .min = 1, .max = 3 },
  404. .m = { .min = 79, .max = 127 },
  405. .m1 = { .min = 12, .max = 22 },
  406. .m2 = { .min = 5, .max = 9 },
  407. .p = { .min = 14, .max = 56 },
  408. .p1 = { .min = 2, .max = 8 },
  409. .p2 = { .dot_limit = 225000,
  410. .p2_slow = 7, .p2_fast = 7 },
  411. };
  412. /* LVDS 100mhz refclk limits. */
  413. static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
  414. .dot = { .min = 25000, .max = 350000 },
  415. .vco = { .min = 1760000, .max = 3510000 },
  416. .n = { .min = 1, .max = 2 },
  417. .m = { .min = 79, .max = 126 },
  418. .m1 = { .min = 12, .max = 22 },
  419. .m2 = { .min = 5, .max = 9 },
  420. .p = { .min = 28, .max = 112 },
  421. .p1 = { .min = 2, .max = 8 },
  422. .p2 = { .dot_limit = 225000,
  423. .p2_slow = 14, .p2_fast = 14 },
  424. };
  425. static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
  426. .dot = { .min = 25000, .max = 350000 },
  427. .vco = { .min = 1760000, .max = 3510000 },
  428. .n = { .min = 1, .max = 3 },
  429. .m = { .min = 79, .max = 126 },
  430. .m1 = { .min = 12, .max = 22 },
  431. .m2 = { .min = 5, .max = 9 },
  432. .p = { .min = 14, .max = 42 },
  433. .p1 = { .min = 2, .max = 6 },
  434. .p2 = { .dot_limit = 225000,
  435. .p2_slow = 7, .p2_fast = 7 },
  436. };
  437. static const struct intel_limit intel_limits_vlv = {
  438. /*
  439. * These are the data rate limits (measured in fast clocks)
  440. * since those are the strictest limits we have. The fast
  441. * clock and actual rate limits are more relaxed, so checking
  442. * them would make no difference.
  443. */
  444. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  445. .vco = { .min = 4000000, .max = 6000000 },
  446. .n = { .min = 1, .max = 7 },
  447. .m1 = { .min = 2, .max = 3 },
  448. .m2 = { .min = 11, .max = 156 },
  449. .p1 = { .min = 2, .max = 3 },
  450. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  451. };
  452. static const struct intel_limit intel_limits_chv = {
  453. /*
  454. * These are the data rate limits (measured in fast clocks)
  455. * since those are the strictest limits we have. The fast
  456. * clock and actual rate limits are more relaxed, so checking
  457. * them would make no difference.
  458. */
  459. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  460. .vco = { .min = 4800000, .max = 6480000 },
  461. .n = { .min = 1, .max = 1 },
  462. .m1 = { .min = 2, .max = 2 },
  463. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  464. .p1 = { .min = 2, .max = 4 },
  465. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  466. };
  467. static const struct intel_limit intel_limits_bxt = {
  468. /* FIXME: find real dot limits */
  469. .dot = { .min = 0, .max = INT_MAX },
  470. .vco = { .min = 4800000, .max = 6700000 },
  471. .n = { .min = 1, .max = 1 },
  472. .m1 = { .min = 2, .max = 2 },
  473. /* FIXME: find real m2 limits */
  474. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  475. .p1 = { .min = 2, .max = 4 },
  476. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  477. };
  478. static bool
  479. needs_modeset(struct drm_crtc_state *state)
  480. {
  481. return drm_atomic_crtc_needs_modeset(state);
  482. }
  483. /*
  484. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  485. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  486. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  487. * The helpers' return value is the rate of the clock that is fed to the
  488. * display engine's pipe which can be the above fast dot clock rate or a
  489. * divided-down version of it.
  490. */
  491. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  492. static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
  493. {
  494. clock->m = clock->m2 + 2;
  495. clock->p = clock->p1 * clock->p2;
  496. if (WARN_ON(clock->n == 0 || clock->p == 0))
  497. return 0;
  498. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  499. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  500. return clock->dot;
  501. }
  502. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  503. {
  504. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  505. }
  506. static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
  507. {
  508. clock->m = i9xx_dpll_compute_m(clock);
  509. clock->p = clock->p1 * clock->p2;
  510. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  511. return 0;
  512. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  513. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  514. return clock->dot;
  515. }
  516. static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
  517. {
  518. clock->m = clock->m1 * clock->m2;
  519. clock->p = clock->p1 * clock->p2;
  520. if (WARN_ON(clock->n == 0 || clock->p == 0))
  521. return 0;
  522. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  523. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  524. return clock->dot / 5;
  525. }
  526. int chv_calc_dpll_params(int refclk, struct dpll *clock)
  527. {
  528. clock->m = clock->m1 * clock->m2;
  529. clock->p = clock->p1 * clock->p2;
  530. if (WARN_ON(clock->n == 0 || clock->p == 0))
  531. return 0;
  532. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  533. clock->n << 22);
  534. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  535. return clock->dot / 5;
  536. }
  537. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  538. /**
  539. * Returns whether the given set of divisors are valid for a given refclk with
  540. * the given connectors.
  541. */
  542. static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
  543. const struct intel_limit *limit,
  544. const struct dpll *clock)
  545. {
  546. if (clock->n < limit->n.min || limit->n.max < clock->n)
  547. INTELPllInvalid("n out of range\n");
  548. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  549. INTELPllInvalid("p1 out of range\n");
  550. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  551. INTELPllInvalid("m2 out of range\n");
  552. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  553. INTELPllInvalid("m1 out of range\n");
  554. if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
  555. !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
  556. if (clock->m1 <= clock->m2)
  557. INTELPllInvalid("m1 <= m2\n");
  558. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  559. !IS_GEN9_LP(dev_priv)) {
  560. if (clock->p < limit->p.min || limit->p.max < clock->p)
  561. INTELPllInvalid("p out of range\n");
  562. if (clock->m < limit->m.min || limit->m.max < clock->m)
  563. INTELPllInvalid("m out of range\n");
  564. }
  565. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  566. INTELPllInvalid("vco out of range\n");
  567. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  568. * connector, etc., rather than just a single range.
  569. */
  570. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  571. INTELPllInvalid("dot out of range\n");
  572. return true;
  573. }
  574. static int
  575. i9xx_select_p2_div(const struct intel_limit *limit,
  576. const struct intel_crtc_state *crtc_state,
  577. int target)
  578. {
  579. struct drm_device *dev = crtc_state->base.crtc->dev;
  580. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  581. /*
  582. * For LVDS just rely on its current settings for dual-channel.
  583. * We haven't figured out how to reliably set up different
  584. * single/dual channel state, if we even can.
  585. */
  586. if (intel_is_dual_link_lvds(dev))
  587. return limit->p2.p2_fast;
  588. else
  589. return limit->p2.p2_slow;
  590. } else {
  591. if (target < limit->p2.dot_limit)
  592. return limit->p2.p2_slow;
  593. else
  594. return limit->p2.p2_fast;
  595. }
  596. }
  597. /*
  598. * Returns a set of divisors for the desired target clock with the given
  599. * refclk, or FALSE. The returned values represent the clock equation:
  600. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  601. *
  602. * Target and reference clocks are specified in kHz.
  603. *
  604. * If match_clock is provided, then best_clock P divider must match the P
  605. * divider from @match_clock used for LVDS downclocking.
  606. */
  607. static bool
  608. i9xx_find_best_dpll(const struct intel_limit *limit,
  609. struct intel_crtc_state *crtc_state,
  610. int target, int refclk, struct dpll *match_clock,
  611. struct dpll *best_clock)
  612. {
  613. struct drm_device *dev = crtc_state->base.crtc->dev;
  614. struct dpll clock;
  615. int err = target;
  616. memset(best_clock, 0, sizeof(*best_clock));
  617. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  618. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  619. clock.m1++) {
  620. for (clock.m2 = limit->m2.min;
  621. clock.m2 <= limit->m2.max; clock.m2++) {
  622. if (clock.m2 >= clock.m1)
  623. break;
  624. for (clock.n = limit->n.min;
  625. clock.n <= limit->n.max; clock.n++) {
  626. for (clock.p1 = limit->p1.min;
  627. clock.p1 <= limit->p1.max; clock.p1++) {
  628. int this_err;
  629. i9xx_calc_dpll_params(refclk, &clock);
  630. if (!intel_PLL_is_valid(to_i915(dev),
  631. limit,
  632. &clock))
  633. continue;
  634. if (match_clock &&
  635. clock.p != match_clock->p)
  636. continue;
  637. this_err = abs(clock.dot - target);
  638. if (this_err < err) {
  639. *best_clock = clock;
  640. err = this_err;
  641. }
  642. }
  643. }
  644. }
  645. }
  646. return (err != target);
  647. }
  648. /*
  649. * Returns a set of divisors for the desired target clock with the given
  650. * refclk, or FALSE. The returned values represent the clock equation:
  651. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  652. *
  653. * Target and reference clocks are specified in kHz.
  654. *
  655. * If match_clock is provided, then best_clock P divider must match the P
  656. * divider from @match_clock used for LVDS downclocking.
  657. */
  658. static bool
  659. pnv_find_best_dpll(const struct intel_limit *limit,
  660. struct intel_crtc_state *crtc_state,
  661. int target, int refclk, struct dpll *match_clock,
  662. struct dpll *best_clock)
  663. {
  664. struct drm_device *dev = crtc_state->base.crtc->dev;
  665. struct dpll clock;
  666. int err = target;
  667. memset(best_clock, 0, sizeof(*best_clock));
  668. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  669. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  670. clock.m1++) {
  671. for (clock.m2 = limit->m2.min;
  672. clock.m2 <= limit->m2.max; clock.m2++) {
  673. for (clock.n = limit->n.min;
  674. clock.n <= limit->n.max; clock.n++) {
  675. for (clock.p1 = limit->p1.min;
  676. clock.p1 <= limit->p1.max; clock.p1++) {
  677. int this_err;
  678. pnv_calc_dpll_params(refclk, &clock);
  679. if (!intel_PLL_is_valid(to_i915(dev),
  680. limit,
  681. &clock))
  682. continue;
  683. if (match_clock &&
  684. clock.p != match_clock->p)
  685. continue;
  686. this_err = abs(clock.dot - target);
  687. if (this_err < err) {
  688. *best_clock = clock;
  689. err = this_err;
  690. }
  691. }
  692. }
  693. }
  694. }
  695. return (err != target);
  696. }
  697. /*
  698. * Returns a set of divisors for the desired target clock with the given
  699. * refclk, or FALSE. The returned values represent the clock equation:
  700. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  701. *
  702. * Target and reference clocks are specified in kHz.
  703. *
  704. * If match_clock is provided, then best_clock P divider must match the P
  705. * divider from @match_clock used for LVDS downclocking.
  706. */
  707. static bool
  708. g4x_find_best_dpll(const struct intel_limit *limit,
  709. struct intel_crtc_state *crtc_state,
  710. int target, int refclk, struct dpll *match_clock,
  711. struct dpll *best_clock)
  712. {
  713. struct drm_device *dev = crtc_state->base.crtc->dev;
  714. struct dpll clock;
  715. int max_n;
  716. bool found = false;
  717. /* approximately equals target * 0.00585 */
  718. int err_most = (target >> 8) + (target >> 9);
  719. memset(best_clock, 0, sizeof(*best_clock));
  720. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  721. max_n = limit->n.max;
  722. /* based on hardware requirement, prefer smaller n to precision */
  723. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  724. /* based on hardware requirement, prefere larger m1,m2 */
  725. for (clock.m1 = limit->m1.max;
  726. clock.m1 >= limit->m1.min; clock.m1--) {
  727. for (clock.m2 = limit->m2.max;
  728. clock.m2 >= limit->m2.min; clock.m2--) {
  729. for (clock.p1 = limit->p1.max;
  730. clock.p1 >= limit->p1.min; clock.p1--) {
  731. int this_err;
  732. i9xx_calc_dpll_params(refclk, &clock);
  733. if (!intel_PLL_is_valid(to_i915(dev),
  734. limit,
  735. &clock))
  736. continue;
  737. this_err = abs(clock.dot - target);
  738. if (this_err < err_most) {
  739. *best_clock = clock;
  740. err_most = this_err;
  741. max_n = clock.n;
  742. found = true;
  743. }
  744. }
  745. }
  746. }
  747. }
  748. return found;
  749. }
  750. /*
  751. * Check if the calculated PLL configuration is more optimal compared to the
  752. * best configuration and error found so far. Return the calculated error.
  753. */
  754. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  755. const struct dpll *calculated_clock,
  756. const struct dpll *best_clock,
  757. unsigned int best_error_ppm,
  758. unsigned int *error_ppm)
  759. {
  760. /*
  761. * For CHV ignore the error and consider only the P value.
  762. * Prefer a bigger P value based on HW requirements.
  763. */
  764. if (IS_CHERRYVIEW(to_i915(dev))) {
  765. *error_ppm = 0;
  766. return calculated_clock->p > best_clock->p;
  767. }
  768. if (WARN_ON_ONCE(!target_freq))
  769. return false;
  770. *error_ppm = div_u64(1000000ULL *
  771. abs(target_freq - calculated_clock->dot),
  772. target_freq);
  773. /*
  774. * Prefer a better P value over a better (smaller) error if the error
  775. * is small. Ensure this preference for future configurations too by
  776. * setting the error to 0.
  777. */
  778. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  779. *error_ppm = 0;
  780. return true;
  781. }
  782. return *error_ppm + 10 < best_error_ppm;
  783. }
  784. /*
  785. * Returns a set of divisors for the desired target clock with the given
  786. * refclk, or FALSE. The returned values represent the clock equation:
  787. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  788. */
  789. static bool
  790. vlv_find_best_dpll(const struct intel_limit *limit,
  791. struct intel_crtc_state *crtc_state,
  792. int target, int refclk, struct dpll *match_clock,
  793. struct dpll *best_clock)
  794. {
  795. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  796. struct drm_device *dev = crtc->base.dev;
  797. struct dpll clock;
  798. unsigned int bestppm = 1000000;
  799. /* min update 19.2 MHz */
  800. int max_n = min(limit->n.max, refclk / 19200);
  801. bool found = false;
  802. target *= 5; /* fast clock */
  803. memset(best_clock, 0, sizeof(*best_clock));
  804. /* based on hardware requirement, prefer smaller n to precision */
  805. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  806. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  807. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  808. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  809. clock.p = clock.p1 * clock.p2;
  810. /* based on hardware requirement, prefer bigger m1,m2 values */
  811. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  812. unsigned int ppm;
  813. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  814. refclk * clock.m1);
  815. vlv_calc_dpll_params(refclk, &clock);
  816. if (!intel_PLL_is_valid(to_i915(dev),
  817. limit,
  818. &clock))
  819. continue;
  820. if (!vlv_PLL_is_optimal(dev, target,
  821. &clock,
  822. best_clock,
  823. bestppm, &ppm))
  824. continue;
  825. *best_clock = clock;
  826. bestppm = ppm;
  827. found = true;
  828. }
  829. }
  830. }
  831. }
  832. return found;
  833. }
  834. /*
  835. * Returns a set of divisors for the desired target clock with the given
  836. * refclk, or FALSE. The returned values represent the clock equation:
  837. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  838. */
  839. static bool
  840. chv_find_best_dpll(const struct intel_limit *limit,
  841. struct intel_crtc_state *crtc_state,
  842. int target, int refclk, struct dpll *match_clock,
  843. struct dpll *best_clock)
  844. {
  845. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  846. struct drm_device *dev = crtc->base.dev;
  847. unsigned int best_error_ppm;
  848. struct dpll clock;
  849. uint64_t m2;
  850. int found = false;
  851. memset(best_clock, 0, sizeof(*best_clock));
  852. best_error_ppm = 1000000;
  853. /*
  854. * Based on hardware doc, the n always set to 1, and m1 always
  855. * set to 2. If requires to support 200Mhz refclk, we need to
  856. * revisit this because n may not 1 anymore.
  857. */
  858. clock.n = 1, clock.m1 = 2;
  859. target *= 5; /* fast clock */
  860. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  861. for (clock.p2 = limit->p2.p2_fast;
  862. clock.p2 >= limit->p2.p2_slow;
  863. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  864. unsigned int error_ppm;
  865. clock.p = clock.p1 * clock.p2;
  866. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  867. clock.n) << 22, refclk * clock.m1);
  868. if (m2 > INT_MAX/clock.m1)
  869. continue;
  870. clock.m2 = m2;
  871. chv_calc_dpll_params(refclk, &clock);
  872. if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
  873. continue;
  874. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  875. best_error_ppm, &error_ppm))
  876. continue;
  877. *best_clock = clock;
  878. best_error_ppm = error_ppm;
  879. found = true;
  880. }
  881. }
  882. return found;
  883. }
  884. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  885. struct dpll *best_clock)
  886. {
  887. int refclk = 100000;
  888. const struct intel_limit *limit = &intel_limits_bxt;
  889. return chv_find_best_dpll(limit, crtc_state,
  890. target_clock, refclk, NULL, best_clock);
  891. }
  892. bool intel_crtc_active(struct intel_crtc *crtc)
  893. {
  894. /* Be paranoid as we can arrive here with only partial
  895. * state retrieved from the hardware during setup.
  896. *
  897. * We can ditch the adjusted_mode.crtc_clock check as soon
  898. * as Haswell has gained clock readout/fastboot support.
  899. *
  900. * We can ditch the crtc->primary->fb check as soon as we can
  901. * properly reconstruct framebuffers.
  902. *
  903. * FIXME: The intel_crtc->active here should be switched to
  904. * crtc->state->active once we have proper CRTC states wired up
  905. * for atomic.
  906. */
  907. return crtc->active && crtc->base.primary->state->fb &&
  908. crtc->config->base.adjusted_mode.crtc_clock;
  909. }
  910. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  911. enum pipe pipe)
  912. {
  913. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  914. return crtc->config->cpu_transcoder;
  915. }
  916. static bool pipe_dsl_stopped(struct drm_i915_private *dev_priv, enum pipe pipe)
  917. {
  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_i915_private *dev_priv = to_i915(crtc->base.dev);
  949. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  950. enum pipe pipe = crtc->pipe;
  951. if (INTEL_GEN(dev_priv) >= 4) {
  952. i915_reg_t reg = PIPECONF(cpu_transcoder);
  953. /* Wait for the Pipe State to go off */
  954. if (intel_wait_for_register(dev_priv,
  955. reg, I965_PIPECONF_ACTIVE, 0,
  956. 100))
  957. WARN(1, "pipe_off wait timed out\n");
  958. } else {
  959. /* Wait for the display line to settle */
  960. if (wait_for(pipe_dsl_stopped(dev_priv, pipe), 100))
  961. WARN(1, "pipe_off wait timed out\n");
  962. }
  963. }
  964. /* Only for pre-ILK configs */
  965. void assert_pll(struct drm_i915_private *dev_priv,
  966. enum pipe pipe, bool state)
  967. {
  968. u32 val;
  969. bool cur_state;
  970. val = I915_READ(DPLL(pipe));
  971. cur_state = !!(val & DPLL_VCO_ENABLE);
  972. I915_STATE_WARN(cur_state != state,
  973. "PLL state assertion failure (expected %s, current %s)\n",
  974. onoff(state), onoff(cur_state));
  975. }
  976. /* XXX: the dsi pll is shared between MIPI DSI ports */
  977. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  978. {
  979. u32 val;
  980. bool cur_state;
  981. mutex_lock(&dev_priv->sb_lock);
  982. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  983. mutex_unlock(&dev_priv->sb_lock);
  984. cur_state = val & DSI_PLL_VCO_EN;
  985. I915_STATE_WARN(cur_state != state,
  986. "DSI PLL state assertion failure (expected %s, current %s)\n",
  987. onoff(state), onoff(cur_state));
  988. }
  989. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  990. enum pipe pipe, bool state)
  991. {
  992. bool cur_state;
  993. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  994. pipe);
  995. if (HAS_DDI(dev_priv)) {
  996. /* DDI does not have a specific FDI_TX register */
  997. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  998. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  999. } else {
  1000. u32 val = I915_READ(FDI_TX_CTL(pipe));
  1001. cur_state = !!(val & FDI_TX_ENABLE);
  1002. }
  1003. I915_STATE_WARN(cur_state != state,
  1004. "FDI TX state assertion failure (expected %s, current %s)\n",
  1005. onoff(state), onoff(cur_state));
  1006. }
  1007. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1008. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1009. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1010. enum pipe pipe, bool state)
  1011. {
  1012. u32 val;
  1013. bool cur_state;
  1014. val = I915_READ(FDI_RX_CTL(pipe));
  1015. cur_state = !!(val & FDI_RX_ENABLE);
  1016. I915_STATE_WARN(cur_state != state,
  1017. "FDI RX state assertion failure (expected %s, current %s)\n",
  1018. onoff(state), onoff(cur_state));
  1019. }
  1020. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1021. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1022. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1023. enum pipe pipe)
  1024. {
  1025. u32 val;
  1026. /* ILK FDI PLL is always enabled */
  1027. if (IS_GEN5(dev_priv))
  1028. return;
  1029. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1030. if (HAS_DDI(dev_priv))
  1031. return;
  1032. val = I915_READ(FDI_TX_CTL(pipe));
  1033. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1034. }
  1035. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1036. enum pipe pipe, bool state)
  1037. {
  1038. u32 val;
  1039. bool cur_state;
  1040. val = I915_READ(FDI_RX_CTL(pipe));
  1041. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1042. I915_STATE_WARN(cur_state != state,
  1043. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1044. onoff(state), onoff(cur_state));
  1045. }
  1046. void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
  1047. {
  1048. i915_reg_t pp_reg;
  1049. u32 val;
  1050. enum pipe panel_pipe = PIPE_A;
  1051. bool locked = true;
  1052. if (WARN_ON(HAS_DDI(dev_priv)))
  1053. return;
  1054. if (HAS_PCH_SPLIT(dev_priv)) {
  1055. u32 port_sel;
  1056. pp_reg = PP_CONTROL(0);
  1057. port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
  1058. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1059. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1060. panel_pipe = PIPE_B;
  1061. /* XXX: else fix for eDP */
  1062. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1063. /* presumably write lock depends on pipe, not port select */
  1064. pp_reg = PP_CONTROL(pipe);
  1065. panel_pipe = pipe;
  1066. } else {
  1067. pp_reg = PP_CONTROL(0);
  1068. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1069. panel_pipe = PIPE_B;
  1070. }
  1071. val = I915_READ(pp_reg);
  1072. if (!(val & PANEL_POWER_ON) ||
  1073. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1074. locked = false;
  1075. I915_STATE_WARN(panel_pipe == pipe && locked,
  1076. "panel assertion failure, pipe %c regs locked\n",
  1077. pipe_name(pipe));
  1078. }
  1079. static void assert_cursor(struct drm_i915_private *dev_priv,
  1080. enum pipe pipe, bool state)
  1081. {
  1082. bool cur_state;
  1083. if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  1084. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1085. else
  1086. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1087. I915_STATE_WARN(cur_state != state,
  1088. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1089. pipe_name(pipe), onoff(state), onoff(cur_state));
  1090. }
  1091. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1092. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1093. void assert_pipe(struct drm_i915_private *dev_priv,
  1094. enum pipe pipe, bool state)
  1095. {
  1096. bool cur_state;
  1097. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1098. pipe);
  1099. enum intel_display_power_domain power_domain;
  1100. /* if we need the pipe quirk it must be always on */
  1101. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1102. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1103. state = true;
  1104. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1105. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1106. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1107. cur_state = !!(val & PIPECONF_ENABLE);
  1108. intel_display_power_put(dev_priv, power_domain);
  1109. } else {
  1110. cur_state = false;
  1111. }
  1112. I915_STATE_WARN(cur_state != state,
  1113. "pipe %c assertion failure (expected %s, current %s)\n",
  1114. pipe_name(pipe), onoff(state), onoff(cur_state));
  1115. }
  1116. static void assert_plane(struct drm_i915_private *dev_priv,
  1117. enum plane plane, bool state)
  1118. {
  1119. u32 val;
  1120. bool cur_state;
  1121. val = I915_READ(DSPCNTR(plane));
  1122. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1123. I915_STATE_WARN(cur_state != state,
  1124. "plane %c assertion failure (expected %s, current %s)\n",
  1125. plane_name(plane), onoff(state), onoff(cur_state));
  1126. }
  1127. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1128. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1129. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1130. enum pipe pipe)
  1131. {
  1132. int i;
  1133. /* Primary planes are fixed to pipes on gen4+ */
  1134. if (INTEL_GEN(dev_priv) >= 4) {
  1135. u32 val = I915_READ(DSPCNTR(pipe));
  1136. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1137. "plane %c assertion failure, should be disabled but not\n",
  1138. plane_name(pipe));
  1139. return;
  1140. }
  1141. /* Need to check both planes against the pipe */
  1142. for_each_pipe(dev_priv, i) {
  1143. u32 val = I915_READ(DSPCNTR(i));
  1144. enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1145. DISPPLANE_SEL_PIPE_SHIFT;
  1146. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1147. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1148. plane_name(i), pipe_name(pipe));
  1149. }
  1150. }
  1151. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1152. enum pipe pipe)
  1153. {
  1154. int sprite;
  1155. if (INTEL_GEN(dev_priv) >= 9) {
  1156. for_each_sprite(dev_priv, pipe, sprite) {
  1157. u32 val = I915_READ(PLANE_CTL(pipe, sprite));
  1158. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1159. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1160. sprite, pipe_name(pipe));
  1161. }
  1162. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1163. for_each_sprite(dev_priv, pipe, sprite) {
  1164. u32 val = I915_READ(SPCNTR(pipe, PLANE_SPRITE0 + sprite));
  1165. I915_STATE_WARN(val & SP_ENABLE,
  1166. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1167. sprite_name(pipe, sprite), pipe_name(pipe));
  1168. }
  1169. } else if (INTEL_GEN(dev_priv) >= 7) {
  1170. u32 val = I915_READ(SPRCTL(pipe));
  1171. I915_STATE_WARN(val & SPRITE_ENABLE,
  1172. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1173. plane_name(pipe), pipe_name(pipe));
  1174. } else if (INTEL_GEN(dev_priv) >= 5) {
  1175. u32 val = I915_READ(DVSCNTR(pipe));
  1176. I915_STATE_WARN(val & DVS_ENABLE,
  1177. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1178. plane_name(pipe), pipe_name(pipe));
  1179. }
  1180. }
  1181. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1182. {
  1183. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1184. drm_crtc_vblank_put(crtc);
  1185. }
  1186. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1187. enum pipe pipe)
  1188. {
  1189. u32 val;
  1190. bool enabled;
  1191. val = I915_READ(PCH_TRANSCONF(pipe));
  1192. enabled = !!(val & TRANS_ENABLE);
  1193. I915_STATE_WARN(enabled,
  1194. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1195. pipe_name(pipe));
  1196. }
  1197. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1198. enum pipe pipe, u32 port_sel, u32 val)
  1199. {
  1200. if ((val & DP_PORT_EN) == 0)
  1201. return false;
  1202. if (HAS_PCH_CPT(dev_priv)) {
  1203. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1204. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1205. return false;
  1206. } else if (IS_CHERRYVIEW(dev_priv)) {
  1207. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1208. return false;
  1209. } else {
  1210. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1211. return false;
  1212. }
  1213. return true;
  1214. }
  1215. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1216. enum pipe pipe, u32 val)
  1217. {
  1218. if ((val & SDVO_ENABLE) == 0)
  1219. return false;
  1220. if (HAS_PCH_CPT(dev_priv)) {
  1221. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1222. return false;
  1223. } else if (IS_CHERRYVIEW(dev_priv)) {
  1224. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1225. return false;
  1226. } else {
  1227. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1228. return false;
  1229. }
  1230. return true;
  1231. }
  1232. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1233. enum pipe pipe, u32 val)
  1234. {
  1235. if ((val & LVDS_PORT_EN) == 0)
  1236. return false;
  1237. if (HAS_PCH_CPT(dev_priv)) {
  1238. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1239. return false;
  1240. } else {
  1241. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1242. return false;
  1243. }
  1244. return true;
  1245. }
  1246. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1247. enum pipe pipe, u32 val)
  1248. {
  1249. if ((val & ADPA_DAC_ENABLE) == 0)
  1250. return false;
  1251. if (HAS_PCH_CPT(dev_priv)) {
  1252. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1253. return false;
  1254. } else {
  1255. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1256. return false;
  1257. }
  1258. return true;
  1259. }
  1260. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1261. enum pipe pipe, i915_reg_t reg,
  1262. u32 port_sel)
  1263. {
  1264. u32 val = I915_READ(reg);
  1265. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1266. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1267. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1268. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
  1269. && (val & DP_PIPEB_SELECT),
  1270. "IBX PCH dp port still using transcoder B\n");
  1271. }
  1272. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1273. enum pipe pipe, i915_reg_t reg)
  1274. {
  1275. u32 val = I915_READ(reg);
  1276. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1277. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1278. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1279. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
  1280. && (val & SDVO_PIPE_B_SELECT),
  1281. "IBX PCH hdmi port still using transcoder B\n");
  1282. }
  1283. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1284. enum pipe pipe)
  1285. {
  1286. u32 val;
  1287. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1288. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1289. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1290. val = I915_READ(PCH_ADPA);
  1291. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1292. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1293. pipe_name(pipe));
  1294. val = I915_READ(PCH_LVDS);
  1295. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1296. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1297. pipe_name(pipe));
  1298. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1299. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1300. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1301. }
  1302. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1303. const struct intel_crtc_state *pipe_config)
  1304. {
  1305. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1306. enum pipe pipe = crtc->pipe;
  1307. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1308. POSTING_READ(DPLL(pipe));
  1309. udelay(150);
  1310. if (intel_wait_for_register(dev_priv,
  1311. DPLL(pipe),
  1312. DPLL_LOCK_VLV,
  1313. DPLL_LOCK_VLV,
  1314. 1))
  1315. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1316. }
  1317. static void vlv_enable_pll(struct intel_crtc *crtc,
  1318. const struct intel_crtc_state *pipe_config)
  1319. {
  1320. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1321. enum pipe pipe = crtc->pipe;
  1322. assert_pipe_disabled(dev_priv, pipe);
  1323. /* PLL is protected by panel, make sure we can write it */
  1324. assert_panel_unlocked(dev_priv, pipe);
  1325. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1326. _vlv_enable_pll(crtc, pipe_config);
  1327. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1328. POSTING_READ(DPLL_MD(pipe));
  1329. }
  1330. static void _chv_enable_pll(struct intel_crtc *crtc,
  1331. const struct intel_crtc_state *pipe_config)
  1332. {
  1333. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1334. enum pipe pipe = crtc->pipe;
  1335. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1336. u32 tmp;
  1337. mutex_lock(&dev_priv->sb_lock);
  1338. /* Enable back the 10bit clock to display controller */
  1339. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1340. tmp |= DPIO_DCLKP_EN;
  1341. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1342. mutex_unlock(&dev_priv->sb_lock);
  1343. /*
  1344. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1345. */
  1346. udelay(1);
  1347. /* Enable PLL */
  1348. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1349. /* Check PLL is locked */
  1350. if (intel_wait_for_register(dev_priv,
  1351. DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
  1352. 1))
  1353. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1354. }
  1355. static void chv_enable_pll(struct intel_crtc *crtc,
  1356. const struct intel_crtc_state *pipe_config)
  1357. {
  1358. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1359. enum pipe pipe = crtc->pipe;
  1360. assert_pipe_disabled(dev_priv, pipe);
  1361. /* PLL is protected by panel, make sure we can write it */
  1362. assert_panel_unlocked(dev_priv, pipe);
  1363. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1364. _chv_enable_pll(crtc, pipe_config);
  1365. if (pipe != PIPE_A) {
  1366. /*
  1367. * WaPixelRepeatModeFixForC0:chv
  1368. *
  1369. * DPLLCMD is AWOL. Use chicken bits to propagate
  1370. * the value from DPLLBMD to either pipe B or C.
  1371. */
  1372. I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
  1373. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1374. I915_WRITE(CBR4_VLV, 0);
  1375. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1376. /*
  1377. * DPLLB VGA mode also seems to cause problems.
  1378. * We should always have it disabled.
  1379. */
  1380. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1381. } else {
  1382. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1383. POSTING_READ(DPLL_MD(pipe));
  1384. }
  1385. }
  1386. static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
  1387. {
  1388. struct intel_crtc *crtc;
  1389. int count = 0;
  1390. for_each_intel_crtc(&dev_priv->drm, crtc) {
  1391. count += crtc->base.state->active &&
  1392. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
  1393. }
  1394. return count;
  1395. }
  1396. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1397. {
  1398. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1399. i915_reg_t reg = DPLL(crtc->pipe);
  1400. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1401. assert_pipe_disabled(dev_priv, crtc->pipe);
  1402. /* PLL is protected by panel, make sure we can write it */
  1403. if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
  1404. assert_panel_unlocked(dev_priv, crtc->pipe);
  1405. /* Enable DVO 2x clock on both PLLs if necessary */
  1406. if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
  1407. /*
  1408. * It appears to be important that we don't enable this
  1409. * for the current pipe before otherwise configuring the
  1410. * PLL. No idea how this should be handled if multiple
  1411. * DVO outputs are enabled simultaneosly.
  1412. */
  1413. dpll |= DPLL_DVO_2X_MODE;
  1414. I915_WRITE(DPLL(!crtc->pipe),
  1415. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1416. }
  1417. /*
  1418. * Apparently we need to have VGA mode enabled prior to changing
  1419. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1420. * dividers, even though the register value does change.
  1421. */
  1422. I915_WRITE(reg, 0);
  1423. I915_WRITE(reg, dpll);
  1424. /* Wait for the clocks to stabilize. */
  1425. POSTING_READ(reg);
  1426. udelay(150);
  1427. if (INTEL_GEN(dev_priv) >= 4) {
  1428. I915_WRITE(DPLL_MD(crtc->pipe),
  1429. crtc->config->dpll_hw_state.dpll_md);
  1430. } else {
  1431. /* The pixel multiplier can only be updated once the
  1432. * DPLL is enabled and the clocks are stable.
  1433. *
  1434. * So write it again.
  1435. */
  1436. I915_WRITE(reg, dpll);
  1437. }
  1438. /* We do this three times for luck */
  1439. I915_WRITE(reg, dpll);
  1440. POSTING_READ(reg);
  1441. udelay(150); /* wait for warmup */
  1442. I915_WRITE(reg, dpll);
  1443. POSTING_READ(reg);
  1444. udelay(150); /* wait for warmup */
  1445. I915_WRITE(reg, dpll);
  1446. POSTING_READ(reg);
  1447. udelay(150); /* wait for warmup */
  1448. }
  1449. /**
  1450. * i9xx_disable_pll - disable a PLL
  1451. * @dev_priv: i915 private structure
  1452. * @pipe: pipe PLL to disable
  1453. *
  1454. * Disable the PLL for @pipe, making sure the pipe is off first.
  1455. *
  1456. * Note! This is for pre-ILK only.
  1457. */
  1458. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1459. {
  1460. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1461. enum pipe pipe = crtc->pipe;
  1462. /* Disable DVO 2x clock on both PLLs if necessary */
  1463. if (IS_I830(dev_priv) &&
  1464. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
  1465. !intel_num_dvo_pipes(dev_priv)) {
  1466. I915_WRITE(DPLL(PIPE_B),
  1467. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1468. I915_WRITE(DPLL(PIPE_A),
  1469. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1470. }
  1471. /* Don't disable pipe or pipe PLLs if needed */
  1472. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1473. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1474. return;
  1475. /* Make sure the pipe isn't still relying on us */
  1476. assert_pipe_disabled(dev_priv, pipe);
  1477. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1478. POSTING_READ(DPLL(pipe));
  1479. }
  1480. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1481. {
  1482. u32 val;
  1483. /* Make sure the pipe isn't still relying on us */
  1484. assert_pipe_disabled(dev_priv, pipe);
  1485. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1486. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1487. if (pipe != PIPE_A)
  1488. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1489. I915_WRITE(DPLL(pipe), val);
  1490. POSTING_READ(DPLL(pipe));
  1491. }
  1492. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1493. {
  1494. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1495. u32 val;
  1496. /* Make sure the pipe isn't still relying on us */
  1497. assert_pipe_disabled(dev_priv, pipe);
  1498. val = DPLL_SSC_REF_CLK_CHV |
  1499. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1500. if (pipe != PIPE_A)
  1501. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1502. I915_WRITE(DPLL(pipe), val);
  1503. POSTING_READ(DPLL(pipe));
  1504. mutex_lock(&dev_priv->sb_lock);
  1505. /* Disable 10bit clock to display controller */
  1506. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1507. val &= ~DPIO_DCLKP_EN;
  1508. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1509. mutex_unlock(&dev_priv->sb_lock);
  1510. }
  1511. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1512. struct intel_digital_port *dport,
  1513. unsigned int expected_mask)
  1514. {
  1515. u32 port_mask;
  1516. i915_reg_t dpll_reg;
  1517. switch (dport->port) {
  1518. case PORT_B:
  1519. port_mask = DPLL_PORTB_READY_MASK;
  1520. dpll_reg = DPLL(0);
  1521. break;
  1522. case PORT_C:
  1523. port_mask = DPLL_PORTC_READY_MASK;
  1524. dpll_reg = DPLL(0);
  1525. expected_mask <<= 4;
  1526. break;
  1527. case PORT_D:
  1528. port_mask = DPLL_PORTD_READY_MASK;
  1529. dpll_reg = DPIO_PHY_STATUS;
  1530. break;
  1531. default:
  1532. BUG();
  1533. }
  1534. if (intel_wait_for_register(dev_priv,
  1535. dpll_reg, port_mask, expected_mask,
  1536. 1000))
  1537. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1538. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1539. }
  1540. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1541. enum pipe pipe)
  1542. {
  1543. struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
  1544. pipe);
  1545. i915_reg_t reg;
  1546. uint32_t val, pipeconf_val;
  1547. /* Make sure PCH DPLL is enabled */
  1548. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1549. /* FDI must be feeding us bits for PCH ports */
  1550. assert_fdi_tx_enabled(dev_priv, pipe);
  1551. assert_fdi_rx_enabled(dev_priv, pipe);
  1552. if (HAS_PCH_CPT(dev_priv)) {
  1553. /* Workaround: Set the timing override bit before enabling the
  1554. * pch transcoder. */
  1555. reg = TRANS_CHICKEN2(pipe);
  1556. val = I915_READ(reg);
  1557. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1558. I915_WRITE(reg, val);
  1559. }
  1560. reg = PCH_TRANSCONF(pipe);
  1561. val = I915_READ(reg);
  1562. pipeconf_val = I915_READ(PIPECONF(pipe));
  1563. if (HAS_PCH_IBX(dev_priv)) {
  1564. /*
  1565. * Make the BPC in transcoder be consistent with
  1566. * that in pipeconf reg. For HDMI we must use 8bpc
  1567. * here for both 8bpc and 12bpc.
  1568. */
  1569. val &= ~PIPECONF_BPC_MASK;
  1570. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
  1571. val |= PIPECONF_8BPC;
  1572. else
  1573. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1574. }
  1575. val &= ~TRANS_INTERLACE_MASK;
  1576. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1577. if (HAS_PCH_IBX(dev_priv) &&
  1578. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  1579. val |= TRANS_LEGACY_INTERLACED_ILK;
  1580. else
  1581. val |= TRANS_INTERLACED;
  1582. else
  1583. val |= TRANS_PROGRESSIVE;
  1584. I915_WRITE(reg, val | TRANS_ENABLE);
  1585. if (intel_wait_for_register(dev_priv,
  1586. reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
  1587. 100))
  1588. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1589. }
  1590. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1591. enum transcoder cpu_transcoder)
  1592. {
  1593. u32 val, pipeconf_val;
  1594. /* FDI must be feeding us bits for PCH ports */
  1595. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1596. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1597. /* Workaround: set timing override bit. */
  1598. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1599. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1600. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1601. val = TRANS_ENABLE;
  1602. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1603. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1604. PIPECONF_INTERLACED_ILK)
  1605. val |= TRANS_INTERLACED;
  1606. else
  1607. val |= TRANS_PROGRESSIVE;
  1608. I915_WRITE(LPT_TRANSCONF, val);
  1609. if (intel_wait_for_register(dev_priv,
  1610. LPT_TRANSCONF,
  1611. TRANS_STATE_ENABLE,
  1612. TRANS_STATE_ENABLE,
  1613. 100))
  1614. DRM_ERROR("Failed to enable PCH transcoder\n");
  1615. }
  1616. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1617. enum pipe pipe)
  1618. {
  1619. i915_reg_t reg;
  1620. uint32_t val;
  1621. /* FDI relies on the transcoder */
  1622. assert_fdi_tx_disabled(dev_priv, pipe);
  1623. assert_fdi_rx_disabled(dev_priv, pipe);
  1624. /* Ports must be off as well */
  1625. assert_pch_ports_disabled(dev_priv, pipe);
  1626. reg = PCH_TRANSCONF(pipe);
  1627. val = I915_READ(reg);
  1628. val &= ~TRANS_ENABLE;
  1629. I915_WRITE(reg, val);
  1630. /* wait for PCH transcoder off, transcoder state */
  1631. if (intel_wait_for_register(dev_priv,
  1632. reg, TRANS_STATE_ENABLE, 0,
  1633. 50))
  1634. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1635. if (HAS_PCH_CPT(dev_priv)) {
  1636. /* Workaround: Clear the timing override chicken bit again. */
  1637. reg = TRANS_CHICKEN2(pipe);
  1638. val = I915_READ(reg);
  1639. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1640. I915_WRITE(reg, val);
  1641. }
  1642. }
  1643. void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1644. {
  1645. u32 val;
  1646. val = I915_READ(LPT_TRANSCONF);
  1647. val &= ~TRANS_ENABLE;
  1648. I915_WRITE(LPT_TRANSCONF, val);
  1649. /* wait for PCH transcoder off, transcoder state */
  1650. if (intel_wait_for_register(dev_priv,
  1651. LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
  1652. 50))
  1653. DRM_ERROR("Failed to disable PCH transcoder\n");
  1654. /* Workaround: clear timing override bit. */
  1655. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1656. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1657. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1658. }
  1659. enum transcoder intel_crtc_pch_transcoder(struct intel_crtc *crtc)
  1660. {
  1661. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1662. WARN_ON(!crtc->config->has_pch_encoder);
  1663. if (HAS_PCH_LPT(dev_priv))
  1664. return TRANSCODER_A;
  1665. else
  1666. return (enum transcoder) crtc->pipe;
  1667. }
  1668. /**
  1669. * intel_enable_pipe - enable a pipe, asserting requirements
  1670. * @crtc: crtc responsible for the pipe
  1671. *
  1672. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1673. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1674. */
  1675. static void intel_enable_pipe(struct intel_crtc *crtc)
  1676. {
  1677. struct drm_device *dev = crtc->base.dev;
  1678. struct drm_i915_private *dev_priv = to_i915(dev);
  1679. enum pipe pipe = crtc->pipe;
  1680. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1681. i915_reg_t reg;
  1682. u32 val;
  1683. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1684. assert_planes_disabled(dev_priv, pipe);
  1685. assert_cursor_disabled(dev_priv, pipe);
  1686. assert_sprites_disabled(dev_priv, pipe);
  1687. /*
  1688. * A pipe without a PLL won't actually be able to drive bits from
  1689. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1690. * need the check.
  1691. */
  1692. if (HAS_GMCH_DISPLAY(dev_priv)) {
  1693. if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
  1694. assert_dsi_pll_enabled(dev_priv);
  1695. else
  1696. assert_pll_enabled(dev_priv, pipe);
  1697. } else {
  1698. if (crtc->config->has_pch_encoder) {
  1699. /* if driving the PCH, we need FDI enabled */
  1700. assert_fdi_rx_pll_enabled(dev_priv,
  1701. (enum pipe) intel_crtc_pch_transcoder(crtc));
  1702. assert_fdi_tx_pll_enabled(dev_priv,
  1703. (enum pipe) cpu_transcoder);
  1704. }
  1705. /* FIXME: assert CPU port conditions for SNB+ */
  1706. }
  1707. reg = PIPECONF(cpu_transcoder);
  1708. val = I915_READ(reg);
  1709. if (val & PIPECONF_ENABLE) {
  1710. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1711. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1712. return;
  1713. }
  1714. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1715. POSTING_READ(reg);
  1716. /*
  1717. * Until the pipe starts DSL will read as 0, which would cause
  1718. * an apparent vblank timestamp jump, which messes up also the
  1719. * frame count when it's derived from the timestamps. So let's
  1720. * wait for the pipe to start properly before we call
  1721. * drm_crtc_vblank_on()
  1722. */
  1723. if (dev->max_vblank_count == 0 &&
  1724. wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
  1725. DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
  1726. }
  1727. /**
  1728. * intel_disable_pipe - disable a pipe, asserting requirements
  1729. * @crtc: crtc whose pipes is to be disabled
  1730. *
  1731. * Disable the pipe of @crtc, making sure that various hardware
  1732. * specific requirements are met, if applicable, e.g. plane
  1733. * disabled, panel fitter off, etc.
  1734. *
  1735. * Will wait until the pipe has shut down before returning.
  1736. */
  1737. static void intel_disable_pipe(struct intel_crtc *crtc)
  1738. {
  1739. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1740. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1741. enum pipe pipe = crtc->pipe;
  1742. i915_reg_t reg;
  1743. u32 val;
  1744. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1745. /*
  1746. * Make sure planes won't keep trying to pump pixels to us,
  1747. * or we might hang the display.
  1748. */
  1749. assert_planes_disabled(dev_priv, pipe);
  1750. assert_cursor_disabled(dev_priv, pipe);
  1751. assert_sprites_disabled(dev_priv, pipe);
  1752. reg = PIPECONF(cpu_transcoder);
  1753. val = I915_READ(reg);
  1754. if ((val & PIPECONF_ENABLE) == 0)
  1755. return;
  1756. /*
  1757. * Double wide has implications for planes
  1758. * so best keep it disabled when not needed.
  1759. */
  1760. if (crtc->config->double_wide)
  1761. val &= ~PIPECONF_DOUBLE_WIDE;
  1762. /* Don't disable pipe or pipe PLLs if needed */
  1763. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1764. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1765. val &= ~PIPECONF_ENABLE;
  1766. I915_WRITE(reg, val);
  1767. if ((val & PIPECONF_ENABLE) == 0)
  1768. intel_wait_for_pipe_off(crtc);
  1769. }
  1770. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1771. {
  1772. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1773. }
  1774. static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
  1775. uint64_t fb_modifier, unsigned int cpp)
  1776. {
  1777. switch (fb_modifier) {
  1778. case DRM_FORMAT_MOD_NONE:
  1779. return cpp;
  1780. case I915_FORMAT_MOD_X_TILED:
  1781. if (IS_GEN2(dev_priv))
  1782. return 128;
  1783. else
  1784. return 512;
  1785. case I915_FORMAT_MOD_Y_TILED:
  1786. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1787. return 128;
  1788. else
  1789. return 512;
  1790. case I915_FORMAT_MOD_Yf_TILED:
  1791. switch (cpp) {
  1792. case 1:
  1793. return 64;
  1794. case 2:
  1795. case 4:
  1796. return 128;
  1797. case 8:
  1798. case 16:
  1799. return 256;
  1800. default:
  1801. MISSING_CASE(cpp);
  1802. return cpp;
  1803. }
  1804. break;
  1805. default:
  1806. MISSING_CASE(fb_modifier);
  1807. return cpp;
  1808. }
  1809. }
  1810. unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
  1811. uint64_t fb_modifier, unsigned int cpp)
  1812. {
  1813. if (fb_modifier == DRM_FORMAT_MOD_NONE)
  1814. return 1;
  1815. else
  1816. return intel_tile_size(dev_priv) /
  1817. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1818. }
  1819. /* Return the tile dimensions in pixel units */
  1820. static void intel_tile_dims(const struct drm_i915_private *dev_priv,
  1821. unsigned int *tile_width,
  1822. unsigned int *tile_height,
  1823. uint64_t fb_modifier,
  1824. unsigned int cpp)
  1825. {
  1826. unsigned int tile_width_bytes =
  1827. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1828. *tile_width = tile_width_bytes / cpp;
  1829. *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
  1830. }
  1831. unsigned int
  1832. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1833. uint32_t pixel_format, uint64_t fb_modifier)
  1834. {
  1835. unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
  1836. unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
  1837. return ALIGN(height, tile_height);
  1838. }
  1839. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1840. {
  1841. unsigned int size = 0;
  1842. int i;
  1843. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1844. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1845. return size;
  1846. }
  1847. static void
  1848. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1849. const struct drm_framebuffer *fb,
  1850. unsigned int rotation)
  1851. {
  1852. if (drm_rotation_90_or_270(rotation)) {
  1853. *view = i915_ggtt_view_rotated;
  1854. view->params.rotated = to_intel_framebuffer(fb)->rot_info;
  1855. } else {
  1856. *view = i915_ggtt_view_normal;
  1857. }
  1858. }
  1859. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1860. {
  1861. if (INTEL_INFO(dev_priv)->gen >= 9)
  1862. return 256 * 1024;
  1863. else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
  1864. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1865. return 128 * 1024;
  1866. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1867. return 4 * 1024;
  1868. else
  1869. return 0;
  1870. }
  1871. static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
  1872. uint64_t fb_modifier)
  1873. {
  1874. switch (fb_modifier) {
  1875. case DRM_FORMAT_MOD_NONE:
  1876. return intel_linear_alignment(dev_priv);
  1877. case I915_FORMAT_MOD_X_TILED:
  1878. if (INTEL_INFO(dev_priv)->gen >= 9)
  1879. return 256 * 1024;
  1880. return 0;
  1881. case I915_FORMAT_MOD_Y_TILED:
  1882. case I915_FORMAT_MOD_Yf_TILED:
  1883. return 1 * 1024 * 1024;
  1884. default:
  1885. MISSING_CASE(fb_modifier);
  1886. return 0;
  1887. }
  1888. }
  1889. struct i915_vma *
  1890. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1891. {
  1892. struct drm_device *dev = fb->dev;
  1893. struct drm_i915_private *dev_priv = to_i915(dev);
  1894. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1895. struct i915_ggtt_view view;
  1896. struct i915_vma *vma;
  1897. u32 alignment;
  1898. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1899. alignment = intel_surf_alignment(dev_priv, fb->modifier);
  1900. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1901. /* Note that the w/a also requires 64 PTE of padding following the
  1902. * bo. We currently fill all unused PTE with the shadow page and so
  1903. * we should always have valid PTE following the scanout preventing
  1904. * the VT-d warning.
  1905. */
  1906. if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
  1907. alignment = 256 * 1024;
  1908. /*
  1909. * Global gtt pte registers are special registers which actually forward
  1910. * writes to a chunk of system memory. Which means that there is no risk
  1911. * that the register values disappear as soon as we call
  1912. * intel_runtime_pm_put(), so it is correct to wrap only the
  1913. * pin/unpin/fence and not more.
  1914. */
  1915. intel_runtime_pm_get(dev_priv);
  1916. vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
  1917. if (IS_ERR(vma))
  1918. goto err;
  1919. if (i915_vma_is_map_and_fenceable(vma)) {
  1920. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1921. * fence, whereas 965+ only requires a fence if using
  1922. * framebuffer compression. For simplicity, we always, when
  1923. * possible, install a fence as the cost is not that onerous.
  1924. *
  1925. * If we fail to fence the tiled scanout, then either the
  1926. * modeset will reject the change (which is highly unlikely as
  1927. * the affected systems, all but one, do not have unmappable
  1928. * space) or we will not be able to enable full powersaving
  1929. * techniques (also likely not to apply due to various limits
  1930. * FBC and the like impose on the size of the buffer, which
  1931. * presumably we violated anyway with this unmappable buffer).
  1932. * Anyway, it is presumably better to stumble onwards with
  1933. * something and try to run the system in a "less than optimal"
  1934. * mode that matches the user configuration.
  1935. */
  1936. if (i915_vma_get_fence(vma) == 0)
  1937. i915_vma_pin_fence(vma);
  1938. }
  1939. err:
  1940. intel_runtime_pm_put(dev_priv);
  1941. return vma;
  1942. }
  1943. void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1944. {
  1945. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1946. struct i915_ggtt_view view;
  1947. struct i915_vma *vma;
  1948. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  1949. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1950. vma = i915_gem_object_to_ggtt(obj, &view);
  1951. i915_vma_unpin_fence(vma);
  1952. i915_gem_object_unpin_from_display_plane(vma);
  1953. }
  1954. static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
  1955. unsigned int rotation)
  1956. {
  1957. if (drm_rotation_90_or_270(rotation))
  1958. return to_intel_framebuffer(fb)->rotated[plane].pitch;
  1959. else
  1960. return fb->pitches[plane];
  1961. }
  1962. /*
  1963. * Convert the x/y offsets into a linear offset.
  1964. * Only valid with 0/180 degree rotation, which is fine since linear
  1965. * offset is only used with linear buffers on pre-hsw and tiled buffers
  1966. * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
  1967. */
  1968. u32 intel_fb_xy_to_linear(int x, int y,
  1969. const struct intel_plane_state *state,
  1970. int plane)
  1971. {
  1972. const struct drm_framebuffer *fb = state->base.fb;
  1973. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  1974. unsigned int pitch = fb->pitches[plane];
  1975. return y * pitch + x * cpp;
  1976. }
  1977. /*
  1978. * Add the x/y offsets derived from fb->offsets[] to the user
  1979. * specified plane src x/y offsets. The resulting x/y offsets
  1980. * specify the start of scanout from the beginning of the gtt mapping.
  1981. */
  1982. void intel_add_fb_offsets(int *x, int *y,
  1983. const struct intel_plane_state *state,
  1984. int plane)
  1985. {
  1986. const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
  1987. unsigned int rotation = state->base.rotation;
  1988. if (drm_rotation_90_or_270(rotation)) {
  1989. *x += intel_fb->rotated[plane].x;
  1990. *y += intel_fb->rotated[plane].y;
  1991. } else {
  1992. *x += intel_fb->normal[plane].x;
  1993. *y += intel_fb->normal[plane].y;
  1994. }
  1995. }
  1996. /*
  1997. * Input tile dimensions and pitch must already be
  1998. * rotated to match x and y, and in pixel units.
  1999. */
  2000. static u32 _intel_adjust_tile_offset(int *x, int *y,
  2001. unsigned int tile_width,
  2002. unsigned int tile_height,
  2003. unsigned int tile_size,
  2004. unsigned int pitch_tiles,
  2005. u32 old_offset,
  2006. u32 new_offset)
  2007. {
  2008. unsigned int pitch_pixels = pitch_tiles * tile_width;
  2009. unsigned int tiles;
  2010. WARN_ON(old_offset & (tile_size - 1));
  2011. WARN_ON(new_offset & (tile_size - 1));
  2012. WARN_ON(new_offset > old_offset);
  2013. tiles = (old_offset - new_offset) / tile_size;
  2014. *y += tiles / pitch_tiles * tile_height;
  2015. *x += tiles % pitch_tiles * tile_width;
  2016. /* minimize x in case it got needlessly big */
  2017. *y += *x / pitch_pixels * tile_height;
  2018. *x %= pitch_pixels;
  2019. return new_offset;
  2020. }
  2021. /*
  2022. * Adjust the tile offset by moving the difference into
  2023. * the x/y offsets.
  2024. */
  2025. static u32 intel_adjust_tile_offset(int *x, int *y,
  2026. const struct intel_plane_state *state, int plane,
  2027. u32 old_offset, u32 new_offset)
  2028. {
  2029. const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
  2030. const struct drm_framebuffer *fb = state->base.fb;
  2031. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2032. unsigned int rotation = state->base.rotation;
  2033. unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
  2034. WARN_ON(new_offset > old_offset);
  2035. if (fb->modifier != DRM_FORMAT_MOD_NONE) {
  2036. unsigned int tile_size, tile_width, tile_height;
  2037. unsigned int pitch_tiles;
  2038. tile_size = intel_tile_size(dev_priv);
  2039. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2040. fb->modifier, cpp);
  2041. if (drm_rotation_90_or_270(rotation)) {
  2042. pitch_tiles = pitch / tile_height;
  2043. swap(tile_width, tile_height);
  2044. } else {
  2045. pitch_tiles = pitch / (tile_width * cpp);
  2046. }
  2047. _intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2048. tile_size, pitch_tiles,
  2049. old_offset, new_offset);
  2050. } else {
  2051. old_offset += *y * pitch + *x * cpp;
  2052. *y = (old_offset - new_offset) / pitch;
  2053. *x = ((old_offset - new_offset) - *y * pitch) / cpp;
  2054. }
  2055. return new_offset;
  2056. }
  2057. /*
  2058. * Computes the linear offset to the base tile and adjusts
  2059. * x, y. bytes per pixel is assumed to be a power-of-two.
  2060. *
  2061. * In the 90/270 rotated case, x and y are assumed
  2062. * to be already rotated to match the rotated GTT view, and
  2063. * pitch is the tile_height aligned framebuffer height.
  2064. *
  2065. * This function is used when computing the derived information
  2066. * under intel_framebuffer, so using any of that information
  2067. * here is not allowed. Anything under drm_framebuffer can be
  2068. * used. This is why the user has to pass in the pitch since it
  2069. * is specified in the rotated orientation.
  2070. */
  2071. static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
  2072. int *x, int *y,
  2073. const struct drm_framebuffer *fb, int plane,
  2074. unsigned int pitch,
  2075. unsigned int rotation,
  2076. u32 alignment)
  2077. {
  2078. uint64_t fb_modifier = fb->modifier;
  2079. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2080. u32 offset, offset_aligned;
  2081. if (alignment)
  2082. alignment--;
  2083. if (fb_modifier != DRM_FORMAT_MOD_NONE) {
  2084. unsigned int tile_size, tile_width, tile_height;
  2085. unsigned int tile_rows, tiles, pitch_tiles;
  2086. tile_size = intel_tile_size(dev_priv);
  2087. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2088. fb_modifier, cpp);
  2089. if (drm_rotation_90_or_270(rotation)) {
  2090. pitch_tiles = pitch / tile_height;
  2091. swap(tile_width, tile_height);
  2092. } else {
  2093. pitch_tiles = pitch / (tile_width * cpp);
  2094. }
  2095. tile_rows = *y / tile_height;
  2096. *y %= tile_height;
  2097. tiles = *x / tile_width;
  2098. *x %= tile_width;
  2099. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  2100. offset_aligned = offset & ~alignment;
  2101. _intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2102. tile_size, pitch_tiles,
  2103. offset, offset_aligned);
  2104. } else {
  2105. offset = *y * pitch + *x * cpp;
  2106. offset_aligned = offset & ~alignment;
  2107. *y = (offset & alignment) / pitch;
  2108. *x = ((offset & alignment) - *y * pitch) / cpp;
  2109. }
  2110. return offset_aligned;
  2111. }
  2112. u32 intel_compute_tile_offset(int *x, int *y,
  2113. const struct intel_plane_state *state,
  2114. int plane)
  2115. {
  2116. const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
  2117. const struct drm_framebuffer *fb = state->base.fb;
  2118. unsigned int rotation = state->base.rotation;
  2119. int pitch = intel_fb_pitch(fb, plane, rotation);
  2120. u32 alignment;
  2121. /* AUX_DIST needs only 4K alignment */
  2122. if (fb->pixel_format == DRM_FORMAT_NV12 && plane == 1)
  2123. alignment = 4096;
  2124. else
  2125. alignment = intel_surf_alignment(dev_priv, fb->modifier);
  2126. return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
  2127. rotation, alignment);
  2128. }
  2129. /* Convert the fb->offset[] linear offset into x/y offsets */
  2130. static void intel_fb_offset_to_xy(int *x, int *y,
  2131. const struct drm_framebuffer *fb, int plane)
  2132. {
  2133. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2134. unsigned int pitch = fb->pitches[plane];
  2135. u32 linear_offset = fb->offsets[plane];
  2136. *y = linear_offset / pitch;
  2137. *x = linear_offset % pitch / cpp;
  2138. }
  2139. static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
  2140. {
  2141. switch (fb_modifier) {
  2142. case I915_FORMAT_MOD_X_TILED:
  2143. return I915_TILING_X;
  2144. case I915_FORMAT_MOD_Y_TILED:
  2145. return I915_TILING_Y;
  2146. default:
  2147. return I915_TILING_NONE;
  2148. }
  2149. }
  2150. static int
  2151. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  2152. struct drm_framebuffer *fb)
  2153. {
  2154. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  2155. struct intel_rotation_info *rot_info = &intel_fb->rot_info;
  2156. u32 gtt_offset_rotated = 0;
  2157. unsigned int max_size = 0;
  2158. uint32_t format = fb->pixel_format;
  2159. int i, num_planes = drm_format_num_planes(format);
  2160. unsigned int tile_size = intel_tile_size(dev_priv);
  2161. for (i = 0; i < num_planes; i++) {
  2162. unsigned int width, height;
  2163. unsigned int cpp, size;
  2164. u32 offset;
  2165. int x, y;
  2166. cpp = drm_format_plane_cpp(format, i);
  2167. width = drm_format_plane_width(fb->width, format, i);
  2168. height = drm_format_plane_height(fb->height, format, i);
  2169. intel_fb_offset_to_xy(&x, &y, fb, i);
  2170. /*
  2171. * The fence (if used) is aligned to the start of the object
  2172. * so having the framebuffer wrap around across the edge of the
  2173. * fenced region doesn't really work. We have no API to configure
  2174. * the fence start offset within the object (nor could we probably
  2175. * on gen2/3). So it's just easier if we just require that the
  2176. * fb layout agrees with the fence layout. We already check that the
  2177. * fb stride matches the fence stride elsewhere.
  2178. */
  2179. if (i915_gem_object_is_tiled(intel_fb->obj) &&
  2180. (x + width) * cpp > fb->pitches[i]) {
  2181. DRM_DEBUG("bad fb plane %d offset: 0x%x\n",
  2182. i, fb->offsets[i]);
  2183. return -EINVAL;
  2184. }
  2185. /*
  2186. * First pixel of the framebuffer from
  2187. * the start of the normal gtt mapping.
  2188. */
  2189. intel_fb->normal[i].x = x;
  2190. intel_fb->normal[i].y = y;
  2191. offset = _intel_compute_tile_offset(dev_priv, &x, &y,
  2192. fb, 0, fb->pitches[i],
  2193. DRM_ROTATE_0, tile_size);
  2194. offset /= tile_size;
  2195. if (fb->modifier != DRM_FORMAT_MOD_NONE) {
  2196. unsigned int tile_width, tile_height;
  2197. unsigned int pitch_tiles;
  2198. struct drm_rect r;
  2199. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2200. fb->modifier, cpp);
  2201. rot_info->plane[i].offset = offset;
  2202. rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
  2203. rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
  2204. rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
  2205. intel_fb->rotated[i].pitch =
  2206. rot_info->plane[i].height * tile_height;
  2207. /* how many tiles does this plane need */
  2208. size = rot_info->plane[i].stride * rot_info->plane[i].height;
  2209. /*
  2210. * If the plane isn't horizontally tile aligned,
  2211. * we need one more tile.
  2212. */
  2213. if (x != 0)
  2214. size++;
  2215. /* rotate the x/y offsets to match the GTT view */
  2216. r.x1 = x;
  2217. r.y1 = y;
  2218. r.x2 = x + width;
  2219. r.y2 = y + height;
  2220. drm_rect_rotate(&r,
  2221. rot_info->plane[i].width * tile_width,
  2222. rot_info->plane[i].height * tile_height,
  2223. DRM_ROTATE_270);
  2224. x = r.x1;
  2225. y = r.y1;
  2226. /* rotate the tile dimensions to match the GTT view */
  2227. pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
  2228. swap(tile_width, tile_height);
  2229. /*
  2230. * We only keep the x/y offsets, so push all of the
  2231. * gtt offset into the x/y offsets.
  2232. */
  2233. _intel_adjust_tile_offset(&x, &y, tile_size,
  2234. tile_width, tile_height, pitch_tiles,
  2235. gtt_offset_rotated * tile_size, 0);
  2236. gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
  2237. /*
  2238. * First pixel of the framebuffer from
  2239. * the start of the rotated gtt mapping.
  2240. */
  2241. intel_fb->rotated[i].x = x;
  2242. intel_fb->rotated[i].y = y;
  2243. } else {
  2244. size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
  2245. x * cpp, tile_size);
  2246. }
  2247. /* how many tiles in total needed in the bo */
  2248. max_size = max(max_size, offset + size);
  2249. }
  2250. if (max_size * tile_size > to_intel_framebuffer(fb)->obj->base.size) {
  2251. DRM_DEBUG("fb too big for bo (need %u bytes, have %zu bytes)\n",
  2252. max_size * tile_size, to_intel_framebuffer(fb)->obj->base.size);
  2253. return -EINVAL;
  2254. }
  2255. return 0;
  2256. }
  2257. static int i9xx_format_to_fourcc(int format)
  2258. {
  2259. switch (format) {
  2260. case DISPPLANE_8BPP:
  2261. return DRM_FORMAT_C8;
  2262. case DISPPLANE_BGRX555:
  2263. return DRM_FORMAT_XRGB1555;
  2264. case DISPPLANE_BGRX565:
  2265. return DRM_FORMAT_RGB565;
  2266. default:
  2267. case DISPPLANE_BGRX888:
  2268. return DRM_FORMAT_XRGB8888;
  2269. case DISPPLANE_RGBX888:
  2270. return DRM_FORMAT_XBGR8888;
  2271. case DISPPLANE_BGRX101010:
  2272. return DRM_FORMAT_XRGB2101010;
  2273. case DISPPLANE_RGBX101010:
  2274. return DRM_FORMAT_XBGR2101010;
  2275. }
  2276. }
  2277. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2278. {
  2279. switch (format) {
  2280. case PLANE_CTL_FORMAT_RGB_565:
  2281. return DRM_FORMAT_RGB565;
  2282. default:
  2283. case PLANE_CTL_FORMAT_XRGB_8888:
  2284. if (rgb_order) {
  2285. if (alpha)
  2286. return DRM_FORMAT_ABGR8888;
  2287. else
  2288. return DRM_FORMAT_XBGR8888;
  2289. } else {
  2290. if (alpha)
  2291. return DRM_FORMAT_ARGB8888;
  2292. else
  2293. return DRM_FORMAT_XRGB8888;
  2294. }
  2295. case PLANE_CTL_FORMAT_XRGB_2101010:
  2296. if (rgb_order)
  2297. return DRM_FORMAT_XBGR2101010;
  2298. else
  2299. return DRM_FORMAT_XRGB2101010;
  2300. }
  2301. }
  2302. static bool
  2303. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2304. struct intel_initial_plane_config *plane_config)
  2305. {
  2306. struct drm_device *dev = crtc->base.dev;
  2307. struct drm_i915_private *dev_priv = to_i915(dev);
  2308. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2309. struct drm_i915_gem_object *obj = NULL;
  2310. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2311. struct drm_framebuffer *fb = &plane_config->fb->base;
  2312. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2313. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2314. PAGE_SIZE);
  2315. size_aligned -= base_aligned;
  2316. if (plane_config->size == 0)
  2317. return false;
  2318. /* If the FB is too big, just don't use it since fbdev is not very
  2319. * important and we should probably use that space with FBC or other
  2320. * features. */
  2321. if (size_aligned * 2 > ggtt->stolen_usable_size)
  2322. return false;
  2323. mutex_lock(&dev->struct_mutex);
  2324. obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
  2325. base_aligned,
  2326. base_aligned,
  2327. size_aligned);
  2328. if (!obj) {
  2329. mutex_unlock(&dev->struct_mutex);
  2330. return false;
  2331. }
  2332. if (plane_config->tiling == I915_TILING_X)
  2333. obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
  2334. mode_cmd.pixel_format = fb->pixel_format;
  2335. mode_cmd.width = fb->width;
  2336. mode_cmd.height = fb->height;
  2337. mode_cmd.pitches[0] = fb->pitches[0];
  2338. mode_cmd.modifier[0] = fb->modifier;
  2339. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2340. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2341. &mode_cmd, obj)) {
  2342. DRM_DEBUG_KMS("intel fb init failed\n");
  2343. goto out_unref_obj;
  2344. }
  2345. mutex_unlock(&dev->struct_mutex);
  2346. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2347. return true;
  2348. out_unref_obj:
  2349. i915_gem_object_put(obj);
  2350. mutex_unlock(&dev->struct_mutex);
  2351. return false;
  2352. }
  2353. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2354. static void
  2355. update_state_fb(struct drm_plane *plane)
  2356. {
  2357. if (plane->fb == plane->state->fb)
  2358. return;
  2359. if (plane->state->fb)
  2360. drm_framebuffer_unreference(plane->state->fb);
  2361. plane->state->fb = plane->fb;
  2362. if (plane->state->fb)
  2363. drm_framebuffer_reference(plane->state->fb);
  2364. }
  2365. static void
  2366. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2367. struct intel_initial_plane_config *plane_config)
  2368. {
  2369. struct drm_device *dev = intel_crtc->base.dev;
  2370. struct drm_i915_private *dev_priv = to_i915(dev);
  2371. struct drm_crtc *c;
  2372. struct intel_crtc *i;
  2373. struct drm_i915_gem_object *obj;
  2374. struct drm_plane *primary = intel_crtc->base.primary;
  2375. struct drm_plane_state *plane_state = primary->state;
  2376. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2377. struct intel_plane *intel_plane = to_intel_plane(primary);
  2378. struct intel_plane_state *intel_state =
  2379. to_intel_plane_state(plane_state);
  2380. struct drm_framebuffer *fb;
  2381. if (!plane_config->fb)
  2382. return;
  2383. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2384. fb = &plane_config->fb->base;
  2385. goto valid_fb;
  2386. }
  2387. kfree(plane_config->fb);
  2388. /*
  2389. * Failed to alloc the obj, check to see if we should share
  2390. * an fb with another CRTC instead
  2391. */
  2392. for_each_crtc(dev, c) {
  2393. i = to_intel_crtc(c);
  2394. if (c == &intel_crtc->base)
  2395. continue;
  2396. if (!i->active)
  2397. continue;
  2398. fb = c->primary->fb;
  2399. if (!fb)
  2400. continue;
  2401. obj = intel_fb_obj(fb);
  2402. if (i915_gem_object_ggtt_offset(obj, NULL) == plane_config->base) {
  2403. drm_framebuffer_reference(fb);
  2404. goto valid_fb;
  2405. }
  2406. }
  2407. /*
  2408. * We've failed to reconstruct the BIOS FB. Current display state
  2409. * indicates that the primary plane is visible, but has a NULL FB,
  2410. * which will lead to problems later if we don't fix it up. The
  2411. * simplest solution is to just disable the primary plane now and
  2412. * pretend the BIOS never had it enabled.
  2413. */
  2414. to_intel_plane_state(plane_state)->base.visible = false;
  2415. crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
  2416. intel_pre_disable_primary_noatomic(&intel_crtc->base);
  2417. intel_plane->disable_plane(primary, &intel_crtc->base);
  2418. return;
  2419. valid_fb:
  2420. plane_state->src_x = 0;
  2421. plane_state->src_y = 0;
  2422. plane_state->src_w = fb->width << 16;
  2423. plane_state->src_h = fb->height << 16;
  2424. plane_state->crtc_x = 0;
  2425. plane_state->crtc_y = 0;
  2426. plane_state->crtc_w = fb->width;
  2427. plane_state->crtc_h = fb->height;
  2428. intel_state->base.src = drm_plane_state_src(plane_state);
  2429. intel_state->base.dst = drm_plane_state_dest(plane_state);
  2430. obj = intel_fb_obj(fb);
  2431. if (i915_gem_object_is_tiled(obj))
  2432. dev_priv->preserve_bios_swizzle = true;
  2433. drm_framebuffer_reference(fb);
  2434. primary->fb = primary->state->fb = fb;
  2435. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2436. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2437. atomic_or(to_intel_plane(primary)->frontbuffer_bit,
  2438. &obj->frontbuffer_bits);
  2439. }
  2440. static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
  2441. unsigned int rotation)
  2442. {
  2443. int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2444. switch (fb->modifier) {
  2445. case DRM_FORMAT_MOD_NONE:
  2446. case I915_FORMAT_MOD_X_TILED:
  2447. switch (cpp) {
  2448. case 8:
  2449. return 4096;
  2450. case 4:
  2451. case 2:
  2452. case 1:
  2453. return 8192;
  2454. default:
  2455. MISSING_CASE(cpp);
  2456. break;
  2457. }
  2458. break;
  2459. case I915_FORMAT_MOD_Y_TILED:
  2460. case I915_FORMAT_MOD_Yf_TILED:
  2461. switch (cpp) {
  2462. case 8:
  2463. return 2048;
  2464. case 4:
  2465. return 4096;
  2466. case 2:
  2467. case 1:
  2468. return 8192;
  2469. default:
  2470. MISSING_CASE(cpp);
  2471. break;
  2472. }
  2473. break;
  2474. default:
  2475. MISSING_CASE(fb->modifier);
  2476. }
  2477. return 2048;
  2478. }
  2479. static int skl_check_main_surface(struct intel_plane_state *plane_state)
  2480. {
  2481. const struct drm_i915_private *dev_priv = to_i915(plane_state->base.plane->dev);
  2482. const struct drm_framebuffer *fb = plane_state->base.fb;
  2483. unsigned int rotation = plane_state->base.rotation;
  2484. int x = plane_state->base.src.x1 >> 16;
  2485. int y = plane_state->base.src.y1 >> 16;
  2486. int w = drm_rect_width(&plane_state->base.src) >> 16;
  2487. int h = drm_rect_height(&plane_state->base.src) >> 16;
  2488. int max_width = skl_max_plane_width(fb, 0, rotation);
  2489. int max_height = 4096;
  2490. u32 alignment, offset, aux_offset = plane_state->aux.offset;
  2491. if (w > max_width || h > max_height) {
  2492. DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
  2493. w, h, max_width, max_height);
  2494. return -EINVAL;
  2495. }
  2496. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2497. offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
  2498. alignment = intel_surf_alignment(dev_priv, fb->modifier);
  2499. /*
  2500. * AUX surface offset is specified as the distance from the
  2501. * main surface offset, and it must be non-negative. Make
  2502. * sure that is what we will get.
  2503. */
  2504. if (offset > aux_offset)
  2505. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2506. offset, aux_offset & ~(alignment - 1));
  2507. /*
  2508. * When using an X-tiled surface, the plane blows up
  2509. * if the x offset + width exceed the stride.
  2510. *
  2511. * TODO: linear and Y-tiled seem fine, Yf untested,
  2512. */
  2513. if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
  2514. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2515. while ((x + w) * cpp > fb->pitches[0]) {
  2516. if (offset == 0) {
  2517. DRM_DEBUG_KMS("Unable to find suitable display surface offset\n");
  2518. return -EINVAL;
  2519. }
  2520. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2521. offset, offset - alignment);
  2522. }
  2523. }
  2524. plane_state->main.offset = offset;
  2525. plane_state->main.x = x;
  2526. plane_state->main.y = y;
  2527. return 0;
  2528. }
  2529. static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
  2530. {
  2531. const struct drm_framebuffer *fb = plane_state->base.fb;
  2532. unsigned int rotation = plane_state->base.rotation;
  2533. int max_width = skl_max_plane_width(fb, 1, rotation);
  2534. int max_height = 4096;
  2535. int x = plane_state->base.src.x1 >> 17;
  2536. int y = plane_state->base.src.y1 >> 17;
  2537. int w = drm_rect_width(&plane_state->base.src) >> 17;
  2538. int h = drm_rect_height(&plane_state->base.src) >> 17;
  2539. u32 offset;
  2540. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2541. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2542. /* FIXME not quite sure how/if these apply to the chroma plane */
  2543. if (w > max_width || h > max_height) {
  2544. DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
  2545. w, h, max_width, max_height);
  2546. return -EINVAL;
  2547. }
  2548. plane_state->aux.offset = offset;
  2549. plane_state->aux.x = x;
  2550. plane_state->aux.y = y;
  2551. return 0;
  2552. }
  2553. int skl_check_plane_surface(struct intel_plane_state *plane_state)
  2554. {
  2555. const struct drm_framebuffer *fb = plane_state->base.fb;
  2556. unsigned int rotation = plane_state->base.rotation;
  2557. int ret;
  2558. /* Rotate src coordinates to match rotated GTT view */
  2559. if (drm_rotation_90_or_270(rotation))
  2560. drm_rect_rotate(&plane_state->base.src,
  2561. fb->width << 16, fb->height << 16,
  2562. DRM_ROTATE_270);
  2563. /*
  2564. * Handle the AUX surface first since
  2565. * the main surface setup depends on it.
  2566. */
  2567. if (fb->pixel_format == DRM_FORMAT_NV12) {
  2568. ret = skl_check_nv12_aux_surface(plane_state);
  2569. if (ret)
  2570. return ret;
  2571. } else {
  2572. plane_state->aux.offset = ~0xfff;
  2573. plane_state->aux.x = 0;
  2574. plane_state->aux.y = 0;
  2575. }
  2576. ret = skl_check_main_surface(plane_state);
  2577. if (ret)
  2578. return ret;
  2579. return 0;
  2580. }
  2581. static void i9xx_update_primary_plane(struct drm_plane *primary,
  2582. const struct intel_crtc_state *crtc_state,
  2583. const struct intel_plane_state *plane_state)
  2584. {
  2585. struct drm_i915_private *dev_priv = to_i915(primary->dev);
  2586. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2587. struct drm_framebuffer *fb = plane_state->base.fb;
  2588. int plane = intel_crtc->plane;
  2589. u32 linear_offset;
  2590. u32 dspcntr;
  2591. i915_reg_t reg = DSPCNTR(plane);
  2592. unsigned int rotation = plane_state->base.rotation;
  2593. int x = plane_state->base.src.x1 >> 16;
  2594. int y = plane_state->base.src.y1 >> 16;
  2595. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2596. dspcntr |= DISPLAY_PLANE_ENABLE;
  2597. if (INTEL_GEN(dev_priv) < 4) {
  2598. if (intel_crtc->pipe == PIPE_B)
  2599. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2600. /* pipesrc and dspsize control the size that is scaled from,
  2601. * which should always be the user's requested size.
  2602. */
  2603. I915_WRITE(DSPSIZE(plane),
  2604. ((crtc_state->pipe_src_h - 1) << 16) |
  2605. (crtc_state->pipe_src_w - 1));
  2606. I915_WRITE(DSPPOS(plane), 0);
  2607. } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
  2608. I915_WRITE(PRIMSIZE(plane),
  2609. ((crtc_state->pipe_src_h - 1) << 16) |
  2610. (crtc_state->pipe_src_w - 1));
  2611. I915_WRITE(PRIMPOS(plane), 0);
  2612. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2613. }
  2614. switch (fb->pixel_format) {
  2615. case DRM_FORMAT_C8:
  2616. dspcntr |= DISPPLANE_8BPP;
  2617. break;
  2618. case DRM_FORMAT_XRGB1555:
  2619. dspcntr |= DISPPLANE_BGRX555;
  2620. break;
  2621. case DRM_FORMAT_RGB565:
  2622. dspcntr |= DISPPLANE_BGRX565;
  2623. break;
  2624. case DRM_FORMAT_XRGB8888:
  2625. dspcntr |= DISPPLANE_BGRX888;
  2626. break;
  2627. case DRM_FORMAT_XBGR8888:
  2628. dspcntr |= DISPPLANE_RGBX888;
  2629. break;
  2630. case DRM_FORMAT_XRGB2101010:
  2631. dspcntr |= DISPPLANE_BGRX101010;
  2632. break;
  2633. case DRM_FORMAT_XBGR2101010:
  2634. dspcntr |= DISPPLANE_RGBX101010;
  2635. break;
  2636. default:
  2637. BUG();
  2638. }
  2639. if (INTEL_GEN(dev_priv) >= 4 &&
  2640. fb->modifier == I915_FORMAT_MOD_X_TILED)
  2641. dspcntr |= DISPPLANE_TILED;
  2642. if (rotation & DRM_ROTATE_180)
  2643. dspcntr |= DISPPLANE_ROTATE_180;
  2644. if (rotation & DRM_REFLECT_X)
  2645. dspcntr |= DISPPLANE_MIRROR;
  2646. if (IS_G4X(dev_priv))
  2647. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2648. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2649. if (INTEL_GEN(dev_priv) >= 4)
  2650. intel_crtc->dspaddr_offset =
  2651. intel_compute_tile_offset(&x, &y, plane_state, 0);
  2652. if (rotation & DRM_ROTATE_180) {
  2653. x += crtc_state->pipe_src_w - 1;
  2654. y += crtc_state->pipe_src_h - 1;
  2655. } else if (rotation & DRM_REFLECT_X) {
  2656. x += crtc_state->pipe_src_w - 1;
  2657. }
  2658. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2659. if (INTEL_GEN(dev_priv) < 4)
  2660. intel_crtc->dspaddr_offset = linear_offset;
  2661. intel_crtc->adjusted_x = x;
  2662. intel_crtc->adjusted_y = y;
  2663. I915_WRITE(reg, dspcntr);
  2664. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2665. if (INTEL_GEN(dev_priv) >= 4) {
  2666. I915_WRITE(DSPSURF(plane),
  2667. intel_fb_gtt_offset(fb, rotation) +
  2668. intel_crtc->dspaddr_offset);
  2669. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2670. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2671. } else {
  2672. I915_WRITE(DSPADDR(plane),
  2673. intel_fb_gtt_offset(fb, rotation) +
  2674. intel_crtc->dspaddr_offset);
  2675. }
  2676. POSTING_READ(reg);
  2677. }
  2678. static void i9xx_disable_primary_plane(struct drm_plane *primary,
  2679. struct drm_crtc *crtc)
  2680. {
  2681. struct drm_device *dev = crtc->dev;
  2682. struct drm_i915_private *dev_priv = to_i915(dev);
  2683. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2684. int plane = intel_crtc->plane;
  2685. I915_WRITE(DSPCNTR(plane), 0);
  2686. if (INTEL_INFO(dev_priv)->gen >= 4)
  2687. I915_WRITE(DSPSURF(plane), 0);
  2688. else
  2689. I915_WRITE(DSPADDR(plane), 0);
  2690. POSTING_READ(DSPCNTR(plane));
  2691. }
  2692. static void ironlake_update_primary_plane(struct drm_plane *primary,
  2693. const struct intel_crtc_state *crtc_state,
  2694. const struct intel_plane_state *plane_state)
  2695. {
  2696. struct drm_device *dev = primary->dev;
  2697. struct drm_i915_private *dev_priv = to_i915(dev);
  2698. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2699. struct drm_framebuffer *fb = plane_state->base.fb;
  2700. int plane = intel_crtc->plane;
  2701. u32 linear_offset;
  2702. u32 dspcntr;
  2703. i915_reg_t reg = DSPCNTR(plane);
  2704. unsigned int rotation = plane_state->base.rotation;
  2705. int x = plane_state->base.src.x1 >> 16;
  2706. int y = plane_state->base.src.y1 >> 16;
  2707. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2708. dspcntr |= DISPLAY_PLANE_ENABLE;
  2709. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  2710. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2711. switch (fb->pixel_format) {
  2712. case DRM_FORMAT_C8:
  2713. dspcntr |= DISPPLANE_8BPP;
  2714. break;
  2715. case DRM_FORMAT_RGB565:
  2716. dspcntr |= DISPPLANE_BGRX565;
  2717. break;
  2718. case DRM_FORMAT_XRGB8888:
  2719. dspcntr |= DISPPLANE_BGRX888;
  2720. break;
  2721. case DRM_FORMAT_XBGR8888:
  2722. dspcntr |= DISPPLANE_RGBX888;
  2723. break;
  2724. case DRM_FORMAT_XRGB2101010:
  2725. dspcntr |= DISPPLANE_BGRX101010;
  2726. break;
  2727. case DRM_FORMAT_XBGR2101010:
  2728. dspcntr |= DISPPLANE_RGBX101010;
  2729. break;
  2730. default:
  2731. BUG();
  2732. }
  2733. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  2734. dspcntr |= DISPPLANE_TILED;
  2735. if (rotation & DRM_ROTATE_180)
  2736. dspcntr |= DISPPLANE_ROTATE_180;
  2737. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv))
  2738. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2739. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2740. intel_crtc->dspaddr_offset =
  2741. intel_compute_tile_offset(&x, &y, plane_state, 0);
  2742. /* HSW+ does this automagically in hardware */
  2743. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv) &&
  2744. rotation & DRM_ROTATE_180) {
  2745. x += crtc_state->pipe_src_w - 1;
  2746. y += crtc_state->pipe_src_h - 1;
  2747. }
  2748. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2749. intel_crtc->adjusted_x = x;
  2750. intel_crtc->adjusted_y = y;
  2751. I915_WRITE(reg, dspcntr);
  2752. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2753. I915_WRITE(DSPSURF(plane),
  2754. intel_fb_gtt_offset(fb, rotation) +
  2755. intel_crtc->dspaddr_offset);
  2756. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  2757. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2758. } else {
  2759. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2760. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2761. }
  2762. POSTING_READ(reg);
  2763. }
  2764. u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
  2765. uint64_t fb_modifier, uint32_t pixel_format)
  2766. {
  2767. if (fb_modifier == DRM_FORMAT_MOD_NONE) {
  2768. return 64;
  2769. } else {
  2770. int cpp = drm_format_plane_cpp(pixel_format, 0);
  2771. return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  2772. }
  2773. }
  2774. u32 intel_fb_gtt_offset(struct drm_framebuffer *fb,
  2775. unsigned int rotation)
  2776. {
  2777. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2778. struct i915_ggtt_view view;
  2779. struct i915_vma *vma;
  2780. intel_fill_fb_ggtt_view(&view, fb, rotation);
  2781. vma = i915_gem_object_to_ggtt(obj, &view);
  2782. if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
  2783. view.type))
  2784. return -1;
  2785. return i915_ggtt_offset(vma);
  2786. }
  2787. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2788. {
  2789. struct drm_device *dev = intel_crtc->base.dev;
  2790. struct drm_i915_private *dev_priv = to_i915(dev);
  2791. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2792. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2793. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2794. }
  2795. /*
  2796. * This function detaches (aka. unbinds) unused scalers in hardware
  2797. */
  2798. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2799. {
  2800. struct intel_crtc_scaler_state *scaler_state;
  2801. int i;
  2802. scaler_state = &intel_crtc->config->scaler_state;
  2803. /* loop through and disable scalers that aren't in use */
  2804. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2805. if (!scaler_state->scalers[i].in_use)
  2806. skl_detach_scaler(intel_crtc, i);
  2807. }
  2808. }
  2809. u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
  2810. unsigned int rotation)
  2811. {
  2812. const struct drm_i915_private *dev_priv = to_i915(fb->dev);
  2813. u32 stride = intel_fb_pitch(fb, plane, rotation);
  2814. /*
  2815. * The stride is either expressed as a multiple of 64 bytes chunks for
  2816. * linear buffers or in number of tiles for tiled buffers.
  2817. */
  2818. if (drm_rotation_90_or_270(rotation)) {
  2819. int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2820. stride /= intel_tile_height(dev_priv, fb->modifier, cpp);
  2821. } else {
  2822. stride /= intel_fb_stride_alignment(dev_priv, fb->modifier,
  2823. fb->pixel_format);
  2824. }
  2825. return stride;
  2826. }
  2827. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2828. {
  2829. switch (pixel_format) {
  2830. case DRM_FORMAT_C8:
  2831. return PLANE_CTL_FORMAT_INDEXED;
  2832. case DRM_FORMAT_RGB565:
  2833. return PLANE_CTL_FORMAT_RGB_565;
  2834. case DRM_FORMAT_XBGR8888:
  2835. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2836. case DRM_FORMAT_XRGB8888:
  2837. return PLANE_CTL_FORMAT_XRGB_8888;
  2838. /*
  2839. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2840. * to be already pre-multiplied. We need to add a knob (or a different
  2841. * DRM_FORMAT) for user-space to configure that.
  2842. */
  2843. case DRM_FORMAT_ABGR8888:
  2844. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2845. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2846. case DRM_FORMAT_ARGB8888:
  2847. return PLANE_CTL_FORMAT_XRGB_8888 |
  2848. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2849. case DRM_FORMAT_XRGB2101010:
  2850. return PLANE_CTL_FORMAT_XRGB_2101010;
  2851. case DRM_FORMAT_XBGR2101010:
  2852. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2853. case DRM_FORMAT_YUYV:
  2854. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2855. case DRM_FORMAT_YVYU:
  2856. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2857. case DRM_FORMAT_UYVY:
  2858. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2859. case DRM_FORMAT_VYUY:
  2860. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2861. default:
  2862. MISSING_CASE(pixel_format);
  2863. }
  2864. return 0;
  2865. }
  2866. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2867. {
  2868. switch (fb_modifier) {
  2869. case DRM_FORMAT_MOD_NONE:
  2870. break;
  2871. case I915_FORMAT_MOD_X_TILED:
  2872. return PLANE_CTL_TILED_X;
  2873. case I915_FORMAT_MOD_Y_TILED:
  2874. return PLANE_CTL_TILED_Y;
  2875. case I915_FORMAT_MOD_Yf_TILED:
  2876. return PLANE_CTL_TILED_YF;
  2877. default:
  2878. MISSING_CASE(fb_modifier);
  2879. }
  2880. return 0;
  2881. }
  2882. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2883. {
  2884. switch (rotation) {
  2885. case DRM_ROTATE_0:
  2886. break;
  2887. /*
  2888. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2889. * while i915 HW rotation is clockwise, thats why this swapping.
  2890. */
  2891. case DRM_ROTATE_90:
  2892. return PLANE_CTL_ROTATE_270;
  2893. case DRM_ROTATE_180:
  2894. return PLANE_CTL_ROTATE_180;
  2895. case DRM_ROTATE_270:
  2896. return PLANE_CTL_ROTATE_90;
  2897. default:
  2898. MISSING_CASE(rotation);
  2899. }
  2900. return 0;
  2901. }
  2902. static void skylake_update_primary_plane(struct drm_plane *plane,
  2903. const struct intel_crtc_state *crtc_state,
  2904. const struct intel_plane_state *plane_state)
  2905. {
  2906. struct drm_device *dev = plane->dev;
  2907. struct drm_i915_private *dev_priv = to_i915(dev);
  2908. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2909. struct drm_framebuffer *fb = plane_state->base.fb;
  2910. enum plane_id plane_id = to_intel_plane(plane)->id;
  2911. enum pipe pipe = to_intel_plane(plane)->pipe;
  2912. u32 plane_ctl;
  2913. unsigned int rotation = plane_state->base.rotation;
  2914. u32 stride = skl_plane_stride(fb, 0, rotation);
  2915. u32 surf_addr = plane_state->main.offset;
  2916. int scaler_id = plane_state->scaler_id;
  2917. int src_x = plane_state->main.x;
  2918. int src_y = plane_state->main.y;
  2919. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  2920. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  2921. int dst_x = plane_state->base.dst.x1;
  2922. int dst_y = plane_state->base.dst.y1;
  2923. int dst_w = drm_rect_width(&plane_state->base.dst);
  2924. int dst_h = drm_rect_height(&plane_state->base.dst);
  2925. plane_ctl = PLANE_CTL_ENABLE |
  2926. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2927. PLANE_CTL_PIPE_CSC_ENABLE;
  2928. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2929. plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
  2930. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2931. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2932. /* Sizes are 0 based */
  2933. src_w--;
  2934. src_h--;
  2935. dst_w--;
  2936. dst_h--;
  2937. intel_crtc->dspaddr_offset = surf_addr;
  2938. intel_crtc->adjusted_x = src_x;
  2939. intel_crtc->adjusted_y = src_y;
  2940. I915_WRITE(PLANE_CTL(pipe, plane_id), plane_ctl);
  2941. I915_WRITE(PLANE_OFFSET(pipe, plane_id), (src_y << 16) | src_x);
  2942. I915_WRITE(PLANE_STRIDE(pipe, plane_id), stride);
  2943. I915_WRITE(PLANE_SIZE(pipe, plane_id), (src_h << 16) | src_w);
  2944. if (scaler_id >= 0) {
  2945. uint32_t ps_ctrl = 0;
  2946. WARN_ON(!dst_w || !dst_h);
  2947. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(plane_id) |
  2948. crtc_state->scaler_state.scalers[scaler_id].mode;
  2949. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2950. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2951. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2952. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2953. I915_WRITE(PLANE_POS(pipe, plane_id), 0);
  2954. } else {
  2955. I915_WRITE(PLANE_POS(pipe, plane_id), (dst_y << 16) | dst_x);
  2956. }
  2957. I915_WRITE(PLANE_SURF(pipe, plane_id),
  2958. intel_fb_gtt_offset(fb, rotation) + surf_addr);
  2959. POSTING_READ(PLANE_SURF(pipe, plane_id));
  2960. }
  2961. static void skylake_disable_primary_plane(struct drm_plane *primary,
  2962. struct drm_crtc *crtc)
  2963. {
  2964. struct drm_device *dev = crtc->dev;
  2965. struct drm_i915_private *dev_priv = to_i915(dev);
  2966. enum plane_id plane_id = to_intel_plane(primary)->id;
  2967. enum pipe pipe = to_intel_plane(primary)->pipe;
  2968. I915_WRITE(PLANE_CTL(pipe, plane_id), 0);
  2969. I915_WRITE(PLANE_SURF(pipe, plane_id), 0);
  2970. POSTING_READ(PLANE_SURF(pipe, plane_id));
  2971. }
  2972. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2973. static int
  2974. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2975. int x, int y, enum mode_set_atomic state)
  2976. {
  2977. /* Support for kgdboc is disabled, this needs a major rework. */
  2978. DRM_ERROR("legacy panic handler not supported any more.\n");
  2979. return -ENODEV;
  2980. }
  2981. static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
  2982. {
  2983. struct intel_crtc *crtc;
  2984. for_each_intel_crtc(&dev_priv->drm, crtc)
  2985. intel_finish_page_flip_cs(dev_priv, crtc->pipe);
  2986. }
  2987. static void intel_update_primary_planes(struct drm_device *dev)
  2988. {
  2989. struct drm_crtc *crtc;
  2990. for_each_crtc(dev, crtc) {
  2991. struct intel_plane *plane = to_intel_plane(crtc->primary);
  2992. struct intel_plane_state *plane_state =
  2993. to_intel_plane_state(plane->base.state);
  2994. if (plane_state->base.visible)
  2995. plane->update_plane(&plane->base,
  2996. to_intel_crtc_state(crtc->state),
  2997. plane_state);
  2998. }
  2999. }
  3000. static int
  3001. __intel_display_resume(struct drm_device *dev,
  3002. struct drm_atomic_state *state)
  3003. {
  3004. struct drm_crtc_state *crtc_state;
  3005. struct drm_crtc *crtc;
  3006. int i, ret;
  3007. intel_modeset_setup_hw_state(dev);
  3008. i915_redisable_vga(to_i915(dev));
  3009. if (!state)
  3010. return 0;
  3011. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  3012. /*
  3013. * Force recalculation even if we restore
  3014. * current state. With fast modeset this may not result
  3015. * in a modeset when the state is compatible.
  3016. */
  3017. crtc_state->mode_changed = true;
  3018. }
  3019. /* ignore any reset values/BIOS leftovers in the WM registers */
  3020. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  3021. ret = drm_atomic_commit(state);
  3022. WARN_ON(ret == -EDEADLK);
  3023. return ret;
  3024. }
  3025. static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
  3026. {
  3027. return intel_has_gpu_reset(dev_priv) &&
  3028. INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
  3029. }
  3030. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  3031. {
  3032. struct drm_device *dev = &dev_priv->drm;
  3033. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3034. struct drm_atomic_state *state;
  3035. int ret;
  3036. /*
  3037. * Need mode_config.mutex so that we don't
  3038. * trample ongoing ->detect() and whatnot.
  3039. */
  3040. mutex_lock(&dev->mode_config.mutex);
  3041. drm_modeset_acquire_init(ctx, 0);
  3042. while (1) {
  3043. ret = drm_modeset_lock_all_ctx(dev, ctx);
  3044. if (ret != -EDEADLK)
  3045. break;
  3046. drm_modeset_backoff(ctx);
  3047. }
  3048. /* reset doesn't touch the display, but flips might get nuked anyway, */
  3049. if (!i915.force_reset_modeset_test &&
  3050. !gpu_reset_clobbers_display(dev_priv))
  3051. return;
  3052. /*
  3053. * Disabling the crtcs gracefully seems nicer. Also the
  3054. * g33 docs say we should at least disable all the planes.
  3055. */
  3056. state = drm_atomic_helper_duplicate_state(dev, ctx);
  3057. if (IS_ERR(state)) {
  3058. ret = PTR_ERR(state);
  3059. state = NULL;
  3060. DRM_ERROR("Duplicating state failed with %i\n", ret);
  3061. goto err;
  3062. }
  3063. ret = drm_atomic_helper_disable_all(dev, ctx);
  3064. if (ret) {
  3065. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  3066. goto err;
  3067. }
  3068. dev_priv->modeset_restore_state = state;
  3069. state->acquire_ctx = ctx;
  3070. return;
  3071. err:
  3072. drm_atomic_state_put(state);
  3073. }
  3074. void intel_finish_reset(struct drm_i915_private *dev_priv)
  3075. {
  3076. struct drm_device *dev = &dev_priv->drm;
  3077. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3078. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  3079. int ret;
  3080. /*
  3081. * Flips in the rings will be nuked by the reset,
  3082. * so complete all pending flips so that user space
  3083. * will get its events and not get stuck.
  3084. */
  3085. intel_complete_page_flips(dev_priv);
  3086. dev_priv->modeset_restore_state = NULL;
  3087. /* reset doesn't touch the display */
  3088. if (!gpu_reset_clobbers_display(dev_priv)) {
  3089. if (!state) {
  3090. /*
  3091. * Flips in the rings have been nuked by the reset,
  3092. * so update the base address of all primary
  3093. * planes to the the last fb to make sure we're
  3094. * showing the correct fb after a reset.
  3095. *
  3096. * FIXME: Atomic will make this obsolete since we won't schedule
  3097. * CS-based flips (which might get lost in gpu resets) any more.
  3098. */
  3099. intel_update_primary_planes(dev);
  3100. } else {
  3101. ret = __intel_display_resume(dev, state);
  3102. if (ret)
  3103. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3104. }
  3105. } else {
  3106. /*
  3107. * The display has been reset as well,
  3108. * so need a full re-initialization.
  3109. */
  3110. intel_runtime_pm_disable_interrupts(dev_priv);
  3111. intel_runtime_pm_enable_interrupts(dev_priv);
  3112. intel_pps_unlock_regs_wa(dev_priv);
  3113. intel_modeset_init_hw(dev);
  3114. spin_lock_irq(&dev_priv->irq_lock);
  3115. if (dev_priv->display.hpd_irq_setup)
  3116. dev_priv->display.hpd_irq_setup(dev_priv);
  3117. spin_unlock_irq(&dev_priv->irq_lock);
  3118. ret = __intel_display_resume(dev, state);
  3119. if (ret)
  3120. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3121. intel_hpd_init(dev_priv);
  3122. }
  3123. if (state)
  3124. drm_atomic_state_put(state);
  3125. drm_modeset_drop_locks(ctx);
  3126. drm_modeset_acquire_fini(ctx);
  3127. mutex_unlock(&dev->mode_config.mutex);
  3128. }
  3129. static bool abort_flip_on_reset(struct intel_crtc *crtc)
  3130. {
  3131. struct i915_gpu_error *error = &to_i915(crtc->base.dev)->gpu_error;
  3132. if (i915_reset_in_progress(error))
  3133. return true;
  3134. if (crtc->reset_count != i915_reset_count(error))
  3135. return true;
  3136. return false;
  3137. }
  3138. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  3139. {
  3140. struct drm_device *dev = crtc->dev;
  3141. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3142. bool pending;
  3143. if (abort_flip_on_reset(intel_crtc))
  3144. return false;
  3145. spin_lock_irq(&dev->event_lock);
  3146. pending = to_intel_crtc(crtc)->flip_work != NULL;
  3147. spin_unlock_irq(&dev->event_lock);
  3148. return pending;
  3149. }
  3150. static void intel_update_pipe_config(struct intel_crtc *crtc,
  3151. struct intel_crtc_state *old_crtc_state)
  3152. {
  3153. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3154. struct intel_crtc_state *pipe_config =
  3155. to_intel_crtc_state(crtc->base.state);
  3156. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  3157. crtc->base.mode = crtc->base.state->mode;
  3158. DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
  3159. old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
  3160. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  3161. /*
  3162. * Update pipe size and adjust fitter if needed: the reason for this is
  3163. * that in compute_mode_changes we check the native mode (not the pfit
  3164. * mode) to see if we can flip rather than do a full mode set. In the
  3165. * fastboot case, we'll flip, but if we don't update the pipesrc and
  3166. * pfit state, we'll end up with a big fb scanned out into the wrong
  3167. * sized surface.
  3168. */
  3169. I915_WRITE(PIPESRC(crtc->pipe),
  3170. ((pipe_config->pipe_src_w - 1) << 16) |
  3171. (pipe_config->pipe_src_h - 1));
  3172. /* on skylake this is done by detaching scalers */
  3173. if (INTEL_GEN(dev_priv) >= 9) {
  3174. skl_detach_scalers(crtc);
  3175. if (pipe_config->pch_pfit.enabled)
  3176. skylake_pfit_enable(crtc);
  3177. } else if (HAS_PCH_SPLIT(dev_priv)) {
  3178. if (pipe_config->pch_pfit.enabled)
  3179. ironlake_pfit_enable(crtc);
  3180. else if (old_crtc_state->pch_pfit.enabled)
  3181. ironlake_pfit_disable(crtc, true);
  3182. }
  3183. }
  3184. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  3185. {
  3186. struct drm_device *dev = crtc->dev;
  3187. struct drm_i915_private *dev_priv = to_i915(dev);
  3188. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3189. int pipe = intel_crtc->pipe;
  3190. i915_reg_t reg;
  3191. u32 temp;
  3192. /* enable normal train */
  3193. reg = FDI_TX_CTL(pipe);
  3194. temp = I915_READ(reg);
  3195. if (IS_IVYBRIDGE(dev_priv)) {
  3196. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3197. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  3198. } else {
  3199. temp &= ~FDI_LINK_TRAIN_NONE;
  3200. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  3201. }
  3202. I915_WRITE(reg, temp);
  3203. reg = FDI_RX_CTL(pipe);
  3204. temp = I915_READ(reg);
  3205. if (HAS_PCH_CPT(dev_priv)) {
  3206. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3207. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  3208. } else {
  3209. temp &= ~FDI_LINK_TRAIN_NONE;
  3210. temp |= FDI_LINK_TRAIN_NONE;
  3211. }
  3212. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  3213. /* wait one idle pattern time */
  3214. POSTING_READ(reg);
  3215. udelay(1000);
  3216. /* IVB wants error correction enabled */
  3217. if (IS_IVYBRIDGE(dev_priv))
  3218. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  3219. FDI_FE_ERRC_ENABLE);
  3220. }
  3221. /* The FDI link training functions for ILK/Ibexpeak. */
  3222. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  3223. {
  3224. struct drm_device *dev = crtc->dev;
  3225. struct drm_i915_private *dev_priv = to_i915(dev);
  3226. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3227. int pipe = intel_crtc->pipe;
  3228. i915_reg_t reg;
  3229. u32 temp, tries;
  3230. /* FDI needs bits from pipe first */
  3231. assert_pipe_enabled(dev_priv, pipe);
  3232. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3233. for train result */
  3234. reg = FDI_RX_IMR(pipe);
  3235. temp = I915_READ(reg);
  3236. temp &= ~FDI_RX_SYMBOL_LOCK;
  3237. temp &= ~FDI_RX_BIT_LOCK;
  3238. I915_WRITE(reg, temp);
  3239. I915_READ(reg);
  3240. udelay(150);
  3241. /* enable CPU FDI TX and PCH FDI RX */
  3242. reg = FDI_TX_CTL(pipe);
  3243. temp = I915_READ(reg);
  3244. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3245. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3246. temp &= ~FDI_LINK_TRAIN_NONE;
  3247. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3248. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3249. reg = FDI_RX_CTL(pipe);
  3250. temp = I915_READ(reg);
  3251. temp &= ~FDI_LINK_TRAIN_NONE;
  3252. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3253. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3254. POSTING_READ(reg);
  3255. udelay(150);
  3256. /* Ironlake workaround, enable clock pointer after FDI enable*/
  3257. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3258. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  3259. FDI_RX_PHASE_SYNC_POINTER_EN);
  3260. reg = FDI_RX_IIR(pipe);
  3261. for (tries = 0; tries < 5; tries++) {
  3262. temp = I915_READ(reg);
  3263. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3264. if ((temp & FDI_RX_BIT_LOCK)) {
  3265. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3266. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3267. break;
  3268. }
  3269. }
  3270. if (tries == 5)
  3271. DRM_ERROR("FDI train 1 fail!\n");
  3272. /* Train 2 */
  3273. reg = FDI_TX_CTL(pipe);
  3274. temp = I915_READ(reg);
  3275. temp &= ~FDI_LINK_TRAIN_NONE;
  3276. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3277. I915_WRITE(reg, temp);
  3278. reg = FDI_RX_CTL(pipe);
  3279. temp = I915_READ(reg);
  3280. temp &= ~FDI_LINK_TRAIN_NONE;
  3281. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3282. I915_WRITE(reg, temp);
  3283. POSTING_READ(reg);
  3284. udelay(150);
  3285. reg = FDI_RX_IIR(pipe);
  3286. for (tries = 0; tries < 5; tries++) {
  3287. temp = I915_READ(reg);
  3288. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3289. if (temp & FDI_RX_SYMBOL_LOCK) {
  3290. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3291. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3292. break;
  3293. }
  3294. }
  3295. if (tries == 5)
  3296. DRM_ERROR("FDI train 2 fail!\n");
  3297. DRM_DEBUG_KMS("FDI train done\n");
  3298. }
  3299. static const int snb_b_fdi_train_param[] = {
  3300. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3301. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3302. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3303. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3304. };
  3305. /* The FDI link training functions for SNB/Cougarpoint. */
  3306. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  3307. {
  3308. struct drm_device *dev = crtc->dev;
  3309. struct drm_i915_private *dev_priv = to_i915(dev);
  3310. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3311. int pipe = intel_crtc->pipe;
  3312. i915_reg_t reg;
  3313. u32 temp, i, retry;
  3314. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3315. for train result */
  3316. reg = FDI_RX_IMR(pipe);
  3317. temp = I915_READ(reg);
  3318. temp &= ~FDI_RX_SYMBOL_LOCK;
  3319. temp &= ~FDI_RX_BIT_LOCK;
  3320. I915_WRITE(reg, temp);
  3321. POSTING_READ(reg);
  3322. udelay(150);
  3323. /* enable CPU FDI TX and PCH FDI RX */
  3324. reg = FDI_TX_CTL(pipe);
  3325. temp = I915_READ(reg);
  3326. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3327. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3328. temp &= ~FDI_LINK_TRAIN_NONE;
  3329. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3330. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3331. /* SNB-B */
  3332. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3333. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3334. I915_WRITE(FDI_RX_MISC(pipe),
  3335. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3336. reg = FDI_RX_CTL(pipe);
  3337. temp = I915_READ(reg);
  3338. if (HAS_PCH_CPT(dev_priv)) {
  3339. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3340. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3341. } else {
  3342. temp &= ~FDI_LINK_TRAIN_NONE;
  3343. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3344. }
  3345. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3346. POSTING_READ(reg);
  3347. udelay(150);
  3348. for (i = 0; i < 4; i++) {
  3349. reg = FDI_TX_CTL(pipe);
  3350. temp = I915_READ(reg);
  3351. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3352. temp |= snb_b_fdi_train_param[i];
  3353. I915_WRITE(reg, temp);
  3354. POSTING_READ(reg);
  3355. udelay(500);
  3356. for (retry = 0; retry < 5; retry++) {
  3357. reg = FDI_RX_IIR(pipe);
  3358. temp = I915_READ(reg);
  3359. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3360. if (temp & FDI_RX_BIT_LOCK) {
  3361. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3362. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3363. break;
  3364. }
  3365. udelay(50);
  3366. }
  3367. if (retry < 5)
  3368. break;
  3369. }
  3370. if (i == 4)
  3371. DRM_ERROR("FDI train 1 fail!\n");
  3372. /* Train 2 */
  3373. reg = FDI_TX_CTL(pipe);
  3374. temp = I915_READ(reg);
  3375. temp &= ~FDI_LINK_TRAIN_NONE;
  3376. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3377. if (IS_GEN6(dev_priv)) {
  3378. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3379. /* SNB-B */
  3380. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3381. }
  3382. I915_WRITE(reg, temp);
  3383. reg = FDI_RX_CTL(pipe);
  3384. temp = I915_READ(reg);
  3385. if (HAS_PCH_CPT(dev_priv)) {
  3386. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3387. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3388. } else {
  3389. temp &= ~FDI_LINK_TRAIN_NONE;
  3390. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3391. }
  3392. I915_WRITE(reg, temp);
  3393. POSTING_READ(reg);
  3394. udelay(150);
  3395. for (i = 0; i < 4; i++) {
  3396. reg = FDI_TX_CTL(pipe);
  3397. temp = I915_READ(reg);
  3398. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3399. temp |= snb_b_fdi_train_param[i];
  3400. I915_WRITE(reg, temp);
  3401. POSTING_READ(reg);
  3402. udelay(500);
  3403. for (retry = 0; retry < 5; retry++) {
  3404. reg = FDI_RX_IIR(pipe);
  3405. temp = I915_READ(reg);
  3406. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3407. if (temp & FDI_RX_SYMBOL_LOCK) {
  3408. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3409. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3410. break;
  3411. }
  3412. udelay(50);
  3413. }
  3414. if (retry < 5)
  3415. break;
  3416. }
  3417. if (i == 4)
  3418. DRM_ERROR("FDI train 2 fail!\n");
  3419. DRM_DEBUG_KMS("FDI train done.\n");
  3420. }
  3421. /* Manual link training for Ivy Bridge A0 parts */
  3422. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3423. {
  3424. struct drm_device *dev = crtc->dev;
  3425. struct drm_i915_private *dev_priv = to_i915(dev);
  3426. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3427. int pipe = intel_crtc->pipe;
  3428. i915_reg_t reg;
  3429. u32 temp, i, j;
  3430. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3431. for train result */
  3432. reg = FDI_RX_IMR(pipe);
  3433. temp = I915_READ(reg);
  3434. temp &= ~FDI_RX_SYMBOL_LOCK;
  3435. temp &= ~FDI_RX_BIT_LOCK;
  3436. I915_WRITE(reg, temp);
  3437. POSTING_READ(reg);
  3438. udelay(150);
  3439. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3440. I915_READ(FDI_RX_IIR(pipe)));
  3441. /* Try each vswing and preemphasis setting twice before moving on */
  3442. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3443. /* disable first in case we need to retry */
  3444. reg = FDI_TX_CTL(pipe);
  3445. temp = I915_READ(reg);
  3446. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3447. temp &= ~FDI_TX_ENABLE;
  3448. I915_WRITE(reg, temp);
  3449. reg = FDI_RX_CTL(pipe);
  3450. temp = I915_READ(reg);
  3451. temp &= ~FDI_LINK_TRAIN_AUTO;
  3452. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3453. temp &= ~FDI_RX_ENABLE;
  3454. I915_WRITE(reg, temp);
  3455. /* enable CPU FDI TX and PCH FDI RX */
  3456. reg = FDI_TX_CTL(pipe);
  3457. temp = I915_READ(reg);
  3458. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3459. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3460. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3461. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3462. temp |= snb_b_fdi_train_param[j/2];
  3463. temp |= FDI_COMPOSITE_SYNC;
  3464. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3465. I915_WRITE(FDI_RX_MISC(pipe),
  3466. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3467. reg = FDI_RX_CTL(pipe);
  3468. temp = I915_READ(reg);
  3469. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3470. temp |= FDI_COMPOSITE_SYNC;
  3471. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3472. POSTING_READ(reg);
  3473. udelay(1); /* should be 0.5us */
  3474. for (i = 0; i < 4; i++) {
  3475. reg = FDI_RX_IIR(pipe);
  3476. temp = I915_READ(reg);
  3477. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3478. if (temp & FDI_RX_BIT_LOCK ||
  3479. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3480. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3481. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3482. i);
  3483. break;
  3484. }
  3485. udelay(1); /* should be 0.5us */
  3486. }
  3487. if (i == 4) {
  3488. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3489. continue;
  3490. }
  3491. /* Train 2 */
  3492. reg = FDI_TX_CTL(pipe);
  3493. temp = I915_READ(reg);
  3494. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3495. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3496. I915_WRITE(reg, temp);
  3497. reg = FDI_RX_CTL(pipe);
  3498. temp = I915_READ(reg);
  3499. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3500. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3501. I915_WRITE(reg, temp);
  3502. POSTING_READ(reg);
  3503. udelay(2); /* should be 1.5us */
  3504. for (i = 0; i < 4; i++) {
  3505. reg = FDI_RX_IIR(pipe);
  3506. temp = I915_READ(reg);
  3507. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3508. if (temp & FDI_RX_SYMBOL_LOCK ||
  3509. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3510. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3511. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3512. i);
  3513. goto train_done;
  3514. }
  3515. udelay(2); /* should be 1.5us */
  3516. }
  3517. if (i == 4)
  3518. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3519. }
  3520. train_done:
  3521. DRM_DEBUG_KMS("FDI train done.\n");
  3522. }
  3523. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3524. {
  3525. struct drm_device *dev = intel_crtc->base.dev;
  3526. struct drm_i915_private *dev_priv = to_i915(dev);
  3527. int pipe = intel_crtc->pipe;
  3528. i915_reg_t reg;
  3529. u32 temp;
  3530. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3531. reg = FDI_RX_CTL(pipe);
  3532. temp = I915_READ(reg);
  3533. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3534. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3535. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3536. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3537. POSTING_READ(reg);
  3538. udelay(200);
  3539. /* Switch from Rawclk to PCDclk */
  3540. temp = I915_READ(reg);
  3541. I915_WRITE(reg, temp | FDI_PCDCLK);
  3542. POSTING_READ(reg);
  3543. udelay(200);
  3544. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3545. reg = FDI_TX_CTL(pipe);
  3546. temp = I915_READ(reg);
  3547. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3548. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3549. POSTING_READ(reg);
  3550. udelay(100);
  3551. }
  3552. }
  3553. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3554. {
  3555. struct drm_device *dev = intel_crtc->base.dev;
  3556. struct drm_i915_private *dev_priv = to_i915(dev);
  3557. int pipe = intel_crtc->pipe;
  3558. i915_reg_t reg;
  3559. u32 temp;
  3560. /* Switch from PCDclk to Rawclk */
  3561. reg = FDI_RX_CTL(pipe);
  3562. temp = I915_READ(reg);
  3563. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3564. /* Disable CPU FDI TX PLL */
  3565. reg = FDI_TX_CTL(pipe);
  3566. temp = I915_READ(reg);
  3567. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3568. POSTING_READ(reg);
  3569. udelay(100);
  3570. reg = FDI_RX_CTL(pipe);
  3571. temp = I915_READ(reg);
  3572. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3573. /* Wait for the clocks to turn off. */
  3574. POSTING_READ(reg);
  3575. udelay(100);
  3576. }
  3577. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3578. {
  3579. struct drm_device *dev = crtc->dev;
  3580. struct drm_i915_private *dev_priv = to_i915(dev);
  3581. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3582. int pipe = intel_crtc->pipe;
  3583. i915_reg_t reg;
  3584. u32 temp;
  3585. /* disable CPU FDI tx and PCH FDI rx */
  3586. reg = FDI_TX_CTL(pipe);
  3587. temp = I915_READ(reg);
  3588. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3589. POSTING_READ(reg);
  3590. reg = FDI_RX_CTL(pipe);
  3591. temp = I915_READ(reg);
  3592. temp &= ~(0x7 << 16);
  3593. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3594. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3595. POSTING_READ(reg);
  3596. udelay(100);
  3597. /* Ironlake workaround, disable clock pointer after downing FDI */
  3598. if (HAS_PCH_IBX(dev_priv))
  3599. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3600. /* still set train pattern 1 */
  3601. reg = FDI_TX_CTL(pipe);
  3602. temp = I915_READ(reg);
  3603. temp &= ~FDI_LINK_TRAIN_NONE;
  3604. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3605. I915_WRITE(reg, temp);
  3606. reg = FDI_RX_CTL(pipe);
  3607. temp = I915_READ(reg);
  3608. if (HAS_PCH_CPT(dev_priv)) {
  3609. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3610. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3611. } else {
  3612. temp &= ~FDI_LINK_TRAIN_NONE;
  3613. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3614. }
  3615. /* BPC in FDI rx is consistent with that in PIPECONF */
  3616. temp &= ~(0x07 << 16);
  3617. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3618. I915_WRITE(reg, temp);
  3619. POSTING_READ(reg);
  3620. udelay(100);
  3621. }
  3622. bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
  3623. {
  3624. struct intel_crtc *crtc;
  3625. /* Note that we don't need to be called with mode_config.lock here
  3626. * as our list of CRTC objects is static for the lifetime of the
  3627. * device and so cannot disappear as we iterate. Similarly, we can
  3628. * happily treat the predicates as racy, atomic checks as userspace
  3629. * cannot claim and pin a new fb without at least acquring the
  3630. * struct_mutex and so serialising with us.
  3631. */
  3632. for_each_intel_crtc(&dev_priv->drm, crtc) {
  3633. if (atomic_read(&crtc->unpin_work_count) == 0)
  3634. continue;
  3635. if (crtc->flip_work)
  3636. intel_wait_for_vblank(dev_priv, crtc->pipe);
  3637. return true;
  3638. }
  3639. return false;
  3640. }
  3641. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3642. {
  3643. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3644. struct intel_flip_work *work = intel_crtc->flip_work;
  3645. intel_crtc->flip_work = NULL;
  3646. if (work->event)
  3647. drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
  3648. drm_crtc_vblank_put(&intel_crtc->base);
  3649. wake_up_all(&dev_priv->pending_flip_queue);
  3650. queue_work(dev_priv->wq, &work->unpin_work);
  3651. trace_i915_flip_complete(intel_crtc->plane,
  3652. work->pending_flip_obj);
  3653. }
  3654. static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3655. {
  3656. struct drm_device *dev = crtc->dev;
  3657. struct drm_i915_private *dev_priv = to_i915(dev);
  3658. long ret;
  3659. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3660. ret = wait_event_interruptible_timeout(
  3661. dev_priv->pending_flip_queue,
  3662. !intel_crtc_has_pending_flip(crtc),
  3663. 60*HZ);
  3664. if (ret < 0)
  3665. return ret;
  3666. if (ret == 0) {
  3667. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3668. struct intel_flip_work *work;
  3669. spin_lock_irq(&dev->event_lock);
  3670. work = intel_crtc->flip_work;
  3671. if (work && !is_mmio_work(work)) {
  3672. WARN_ONCE(1, "Removing stuck page flip\n");
  3673. page_flip_completed(intel_crtc);
  3674. }
  3675. spin_unlock_irq(&dev->event_lock);
  3676. }
  3677. return 0;
  3678. }
  3679. void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3680. {
  3681. u32 temp;
  3682. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3683. mutex_lock(&dev_priv->sb_lock);
  3684. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3685. temp |= SBI_SSCCTL_DISABLE;
  3686. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3687. mutex_unlock(&dev_priv->sb_lock);
  3688. }
  3689. /* Program iCLKIP clock to the desired frequency */
  3690. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3691. {
  3692. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  3693. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3694. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3695. u32 temp;
  3696. lpt_disable_iclkip(dev_priv);
  3697. /* The iCLK virtual clock root frequency is in MHz,
  3698. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3699. * divisors, it is necessary to divide one by another, so we
  3700. * convert the virtual clock precision to KHz here for higher
  3701. * precision.
  3702. */
  3703. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3704. u32 iclk_virtual_root_freq = 172800 * 1000;
  3705. u32 iclk_pi_range = 64;
  3706. u32 desired_divisor;
  3707. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3708. clock << auxdiv);
  3709. divsel = (desired_divisor / iclk_pi_range) - 2;
  3710. phaseinc = desired_divisor % iclk_pi_range;
  3711. /*
  3712. * Near 20MHz is a corner case which is
  3713. * out of range for the 7-bit divisor
  3714. */
  3715. if (divsel <= 0x7f)
  3716. break;
  3717. }
  3718. /* This should not happen with any sane values */
  3719. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3720. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3721. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3722. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3723. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3724. clock,
  3725. auxdiv,
  3726. divsel,
  3727. phasedir,
  3728. phaseinc);
  3729. mutex_lock(&dev_priv->sb_lock);
  3730. /* Program SSCDIVINTPHASE6 */
  3731. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3732. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3733. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3734. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3735. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3736. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3737. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3738. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3739. /* Program SSCAUXDIV */
  3740. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3741. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3742. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3743. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3744. /* Enable modulator and associated divider */
  3745. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3746. temp &= ~SBI_SSCCTL_DISABLE;
  3747. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3748. mutex_unlock(&dev_priv->sb_lock);
  3749. /* Wait for initialization time */
  3750. udelay(24);
  3751. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3752. }
  3753. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3754. {
  3755. u32 divsel, phaseinc, auxdiv;
  3756. u32 iclk_virtual_root_freq = 172800 * 1000;
  3757. u32 iclk_pi_range = 64;
  3758. u32 desired_divisor;
  3759. u32 temp;
  3760. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3761. return 0;
  3762. mutex_lock(&dev_priv->sb_lock);
  3763. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3764. if (temp & SBI_SSCCTL_DISABLE) {
  3765. mutex_unlock(&dev_priv->sb_lock);
  3766. return 0;
  3767. }
  3768. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3769. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3770. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3771. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3772. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3773. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3774. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3775. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3776. mutex_unlock(&dev_priv->sb_lock);
  3777. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3778. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3779. desired_divisor << auxdiv);
  3780. }
  3781. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3782. enum pipe pch_transcoder)
  3783. {
  3784. struct drm_device *dev = crtc->base.dev;
  3785. struct drm_i915_private *dev_priv = to_i915(dev);
  3786. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3787. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3788. I915_READ(HTOTAL(cpu_transcoder)));
  3789. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3790. I915_READ(HBLANK(cpu_transcoder)));
  3791. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3792. I915_READ(HSYNC(cpu_transcoder)));
  3793. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3794. I915_READ(VTOTAL(cpu_transcoder)));
  3795. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3796. I915_READ(VBLANK(cpu_transcoder)));
  3797. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3798. I915_READ(VSYNC(cpu_transcoder)));
  3799. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3800. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3801. }
  3802. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3803. {
  3804. struct drm_i915_private *dev_priv = to_i915(dev);
  3805. uint32_t temp;
  3806. temp = I915_READ(SOUTH_CHICKEN1);
  3807. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3808. return;
  3809. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3810. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3811. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3812. if (enable)
  3813. temp |= FDI_BC_BIFURCATION_SELECT;
  3814. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3815. I915_WRITE(SOUTH_CHICKEN1, temp);
  3816. POSTING_READ(SOUTH_CHICKEN1);
  3817. }
  3818. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3819. {
  3820. struct drm_device *dev = intel_crtc->base.dev;
  3821. switch (intel_crtc->pipe) {
  3822. case PIPE_A:
  3823. break;
  3824. case PIPE_B:
  3825. if (intel_crtc->config->fdi_lanes > 2)
  3826. cpt_set_fdi_bc_bifurcation(dev, false);
  3827. else
  3828. cpt_set_fdi_bc_bifurcation(dev, true);
  3829. break;
  3830. case PIPE_C:
  3831. cpt_set_fdi_bc_bifurcation(dev, true);
  3832. break;
  3833. default:
  3834. BUG();
  3835. }
  3836. }
  3837. /* Return which DP Port should be selected for Transcoder DP control */
  3838. static enum port
  3839. intel_trans_dp_port_sel(struct drm_crtc *crtc)
  3840. {
  3841. struct drm_device *dev = crtc->dev;
  3842. struct intel_encoder *encoder;
  3843. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3844. if (encoder->type == INTEL_OUTPUT_DP ||
  3845. encoder->type == INTEL_OUTPUT_EDP)
  3846. return enc_to_dig_port(&encoder->base)->port;
  3847. }
  3848. return -1;
  3849. }
  3850. /*
  3851. * Enable PCH resources required for PCH ports:
  3852. * - PCH PLLs
  3853. * - FDI training & RX/TX
  3854. * - update transcoder timings
  3855. * - DP transcoding bits
  3856. * - transcoder
  3857. */
  3858. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3859. {
  3860. struct drm_device *dev = crtc->dev;
  3861. struct drm_i915_private *dev_priv = to_i915(dev);
  3862. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3863. int pipe = intel_crtc->pipe;
  3864. u32 temp;
  3865. assert_pch_transcoder_disabled(dev_priv, pipe);
  3866. if (IS_IVYBRIDGE(dev_priv))
  3867. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3868. /* Write the TU size bits before fdi link training, so that error
  3869. * detection works. */
  3870. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3871. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3872. /* For PCH output, training FDI link */
  3873. dev_priv->display.fdi_link_train(crtc);
  3874. /* We need to program the right clock selection before writing the pixel
  3875. * mutliplier into the DPLL. */
  3876. if (HAS_PCH_CPT(dev_priv)) {
  3877. u32 sel;
  3878. temp = I915_READ(PCH_DPLL_SEL);
  3879. temp |= TRANS_DPLL_ENABLE(pipe);
  3880. sel = TRANS_DPLLB_SEL(pipe);
  3881. if (intel_crtc->config->shared_dpll ==
  3882. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3883. temp |= sel;
  3884. else
  3885. temp &= ~sel;
  3886. I915_WRITE(PCH_DPLL_SEL, temp);
  3887. }
  3888. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3889. * transcoder, and we actually should do this to not upset any PCH
  3890. * transcoder that already use the clock when we share it.
  3891. *
  3892. * Note that enable_shared_dpll tries to do the right thing, but
  3893. * get_shared_dpll unconditionally resets the pll - we need that to have
  3894. * the right LVDS enable sequence. */
  3895. intel_enable_shared_dpll(intel_crtc);
  3896. /* set transcoder timing, panel must allow it */
  3897. assert_panel_unlocked(dev_priv, pipe);
  3898. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3899. intel_fdi_normal_train(crtc);
  3900. /* For PCH DP, enable TRANS_DP_CTL */
  3901. if (HAS_PCH_CPT(dev_priv) &&
  3902. intel_crtc_has_dp_encoder(intel_crtc->config)) {
  3903. const struct drm_display_mode *adjusted_mode =
  3904. &intel_crtc->config->base.adjusted_mode;
  3905. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3906. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3907. temp = I915_READ(reg);
  3908. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3909. TRANS_DP_SYNC_MASK |
  3910. TRANS_DP_BPC_MASK);
  3911. temp |= TRANS_DP_OUTPUT_ENABLE;
  3912. temp |= bpc << 9; /* same format but at 11:9 */
  3913. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3914. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3915. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3916. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3917. switch (intel_trans_dp_port_sel(crtc)) {
  3918. case PORT_B:
  3919. temp |= TRANS_DP_PORT_SEL_B;
  3920. break;
  3921. case PORT_C:
  3922. temp |= TRANS_DP_PORT_SEL_C;
  3923. break;
  3924. case PORT_D:
  3925. temp |= TRANS_DP_PORT_SEL_D;
  3926. break;
  3927. default:
  3928. BUG();
  3929. }
  3930. I915_WRITE(reg, temp);
  3931. }
  3932. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3933. }
  3934. static void lpt_pch_enable(struct drm_crtc *crtc)
  3935. {
  3936. struct drm_device *dev = crtc->dev;
  3937. struct drm_i915_private *dev_priv = to_i915(dev);
  3938. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3939. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3940. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3941. lpt_program_iclkip(crtc);
  3942. /* Set transcoder timing. */
  3943. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3944. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3945. }
  3946. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3947. {
  3948. struct drm_i915_private *dev_priv = to_i915(dev);
  3949. i915_reg_t dslreg = PIPEDSL(pipe);
  3950. u32 temp;
  3951. temp = I915_READ(dslreg);
  3952. udelay(500);
  3953. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3954. if (wait_for(I915_READ(dslreg) != temp, 5))
  3955. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3956. }
  3957. }
  3958. static int
  3959. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3960. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3961. int src_w, int src_h, int dst_w, int dst_h)
  3962. {
  3963. struct intel_crtc_scaler_state *scaler_state =
  3964. &crtc_state->scaler_state;
  3965. struct intel_crtc *intel_crtc =
  3966. to_intel_crtc(crtc_state->base.crtc);
  3967. int need_scaling;
  3968. need_scaling = drm_rotation_90_or_270(rotation) ?
  3969. (src_h != dst_w || src_w != dst_h):
  3970. (src_w != dst_w || src_h != dst_h);
  3971. /*
  3972. * if plane is being disabled or scaler is no more required or force detach
  3973. * - free scaler binded to this plane/crtc
  3974. * - in order to do this, update crtc->scaler_usage
  3975. *
  3976. * Here scaler state in crtc_state is set free so that
  3977. * scaler can be assigned to other user. Actual register
  3978. * update to free the scaler is done in plane/panel-fit programming.
  3979. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3980. */
  3981. if (force_detach || !need_scaling) {
  3982. if (*scaler_id >= 0) {
  3983. scaler_state->scaler_users &= ~(1 << scaler_user);
  3984. scaler_state->scalers[*scaler_id].in_use = 0;
  3985. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3986. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3987. intel_crtc->pipe, scaler_user, *scaler_id,
  3988. scaler_state->scaler_users);
  3989. *scaler_id = -1;
  3990. }
  3991. return 0;
  3992. }
  3993. /* range checks */
  3994. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3995. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3996. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3997. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3998. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3999. "size is out of scaler range\n",
  4000. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  4001. return -EINVAL;
  4002. }
  4003. /* mark this plane as a scaler user in crtc_state */
  4004. scaler_state->scaler_users |= (1 << scaler_user);
  4005. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  4006. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  4007. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  4008. scaler_state->scaler_users);
  4009. return 0;
  4010. }
  4011. /**
  4012. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  4013. *
  4014. * @state: crtc's scaler state
  4015. *
  4016. * Return
  4017. * 0 - scaler_usage updated successfully
  4018. * error - requested scaling cannot be supported or other error condition
  4019. */
  4020. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  4021. {
  4022. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  4023. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  4024. &state->scaler_state.scaler_id, DRM_ROTATE_0,
  4025. state->pipe_src_w, state->pipe_src_h,
  4026. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  4027. }
  4028. /**
  4029. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  4030. *
  4031. * @state: crtc's scaler state
  4032. * @plane_state: atomic plane state to update
  4033. *
  4034. * Return
  4035. * 0 - scaler_usage updated successfully
  4036. * error - requested scaling cannot be supported or other error condition
  4037. */
  4038. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  4039. struct intel_plane_state *plane_state)
  4040. {
  4041. struct intel_plane *intel_plane =
  4042. to_intel_plane(plane_state->base.plane);
  4043. struct drm_framebuffer *fb = plane_state->base.fb;
  4044. int ret;
  4045. bool force_detach = !fb || !plane_state->base.visible;
  4046. ret = skl_update_scaler(crtc_state, force_detach,
  4047. drm_plane_index(&intel_plane->base),
  4048. &plane_state->scaler_id,
  4049. plane_state->base.rotation,
  4050. drm_rect_width(&plane_state->base.src) >> 16,
  4051. drm_rect_height(&plane_state->base.src) >> 16,
  4052. drm_rect_width(&plane_state->base.dst),
  4053. drm_rect_height(&plane_state->base.dst));
  4054. if (ret || plane_state->scaler_id < 0)
  4055. return ret;
  4056. /* check colorkey */
  4057. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  4058. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  4059. intel_plane->base.base.id,
  4060. intel_plane->base.name);
  4061. return -EINVAL;
  4062. }
  4063. /* Check src format */
  4064. switch (fb->pixel_format) {
  4065. case DRM_FORMAT_RGB565:
  4066. case DRM_FORMAT_XBGR8888:
  4067. case DRM_FORMAT_XRGB8888:
  4068. case DRM_FORMAT_ABGR8888:
  4069. case DRM_FORMAT_ARGB8888:
  4070. case DRM_FORMAT_XRGB2101010:
  4071. case DRM_FORMAT_XBGR2101010:
  4072. case DRM_FORMAT_YUYV:
  4073. case DRM_FORMAT_YVYU:
  4074. case DRM_FORMAT_UYVY:
  4075. case DRM_FORMAT_VYUY:
  4076. break;
  4077. default:
  4078. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  4079. intel_plane->base.base.id, intel_plane->base.name,
  4080. fb->base.id, fb->pixel_format);
  4081. return -EINVAL;
  4082. }
  4083. return 0;
  4084. }
  4085. static void skylake_scaler_disable(struct intel_crtc *crtc)
  4086. {
  4087. int i;
  4088. for (i = 0; i < crtc->num_scalers; i++)
  4089. skl_detach_scaler(crtc, i);
  4090. }
  4091. static void skylake_pfit_enable(struct intel_crtc *crtc)
  4092. {
  4093. struct drm_device *dev = crtc->base.dev;
  4094. struct drm_i915_private *dev_priv = to_i915(dev);
  4095. int pipe = crtc->pipe;
  4096. struct intel_crtc_scaler_state *scaler_state =
  4097. &crtc->config->scaler_state;
  4098. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  4099. if (crtc->config->pch_pfit.enabled) {
  4100. int id;
  4101. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  4102. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  4103. return;
  4104. }
  4105. id = scaler_state->scaler_id;
  4106. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  4107. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  4108. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  4109. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  4110. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  4111. }
  4112. }
  4113. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  4114. {
  4115. struct drm_device *dev = crtc->base.dev;
  4116. struct drm_i915_private *dev_priv = to_i915(dev);
  4117. int pipe = crtc->pipe;
  4118. if (crtc->config->pch_pfit.enabled) {
  4119. /* Force use of hard-coded filter coefficients
  4120. * as some pre-programmed values are broken,
  4121. * e.g. x201.
  4122. */
  4123. if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
  4124. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  4125. PF_PIPE_SEL_IVB(pipe));
  4126. else
  4127. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  4128. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  4129. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  4130. }
  4131. }
  4132. void hsw_enable_ips(struct intel_crtc *crtc)
  4133. {
  4134. struct drm_device *dev = crtc->base.dev;
  4135. struct drm_i915_private *dev_priv = to_i915(dev);
  4136. if (!crtc->config->ips_enabled)
  4137. return;
  4138. /*
  4139. * We can only enable IPS after we enable a plane and wait for a vblank
  4140. * This function is called from post_plane_update, which is run after
  4141. * a vblank wait.
  4142. */
  4143. assert_plane_enabled(dev_priv, crtc->plane);
  4144. if (IS_BROADWELL(dev_priv)) {
  4145. mutex_lock(&dev_priv->rps.hw_lock);
  4146. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  4147. mutex_unlock(&dev_priv->rps.hw_lock);
  4148. /* Quoting Art Runyan: "its not safe to expect any particular
  4149. * value in IPS_CTL bit 31 after enabling IPS through the
  4150. * mailbox." Moreover, the mailbox may return a bogus state,
  4151. * so we need to just enable it and continue on.
  4152. */
  4153. } else {
  4154. I915_WRITE(IPS_CTL, IPS_ENABLE);
  4155. /* The bit only becomes 1 in the next vblank, so this wait here
  4156. * is essentially intel_wait_for_vblank. If we don't have this
  4157. * and don't wait for vblanks until the end of crtc_enable, then
  4158. * the HW state readout code will complain that the expected
  4159. * IPS_CTL value is not the one we read. */
  4160. if (intel_wait_for_register(dev_priv,
  4161. IPS_CTL, IPS_ENABLE, IPS_ENABLE,
  4162. 50))
  4163. DRM_ERROR("Timed out waiting for IPS enable\n");
  4164. }
  4165. }
  4166. void hsw_disable_ips(struct intel_crtc *crtc)
  4167. {
  4168. struct drm_device *dev = crtc->base.dev;
  4169. struct drm_i915_private *dev_priv = to_i915(dev);
  4170. if (!crtc->config->ips_enabled)
  4171. return;
  4172. assert_plane_enabled(dev_priv, crtc->plane);
  4173. if (IS_BROADWELL(dev_priv)) {
  4174. mutex_lock(&dev_priv->rps.hw_lock);
  4175. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  4176. mutex_unlock(&dev_priv->rps.hw_lock);
  4177. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  4178. if (intel_wait_for_register(dev_priv,
  4179. IPS_CTL, IPS_ENABLE, 0,
  4180. 42))
  4181. DRM_ERROR("Timed out waiting for IPS disable\n");
  4182. } else {
  4183. I915_WRITE(IPS_CTL, 0);
  4184. POSTING_READ(IPS_CTL);
  4185. }
  4186. /* We need to wait for a vblank before we can disable the plane. */
  4187. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4188. }
  4189. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  4190. {
  4191. if (intel_crtc->overlay) {
  4192. struct drm_device *dev = intel_crtc->base.dev;
  4193. struct drm_i915_private *dev_priv = to_i915(dev);
  4194. mutex_lock(&dev->struct_mutex);
  4195. dev_priv->mm.interruptible = false;
  4196. (void) intel_overlay_switch_off(intel_crtc->overlay);
  4197. dev_priv->mm.interruptible = true;
  4198. mutex_unlock(&dev->struct_mutex);
  4199. }
  4200. /* Let userspace switch the overlay on again. In most cases userspace
  4201. * has to recompute where to put it anyway.
  4202. */
  4203. }
  4204. /**
  4205. * intel_post_enable_primary - Perform operations after enabling primary plane
  4206. * @crtc: the CRTC whose primary plane was just enabled
  4207. *
  4208. * Performs potentially sleeping operations that must be done after the primary
  4209. * plane is enabled, such as updating FBC and IPS. Note that this may be
  4210. * called due to an explicit primary plane update, or due to an implicit
  4211. * re-enable that is caused when a sprite plane is updated to no longer
  4212. * completely hide the primary plane.
  4213. */
  4214. static void
  4215. intel_post_enable_primary(struct drm_crtc *crtc)
  4216. {
  4217. struct drm_device *dev = crtc->dev;
  4218. struct drm_i915_private *dev_priv = to_i915(dev);
  4219. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4220. int pipe = intel_crtc->pipe;
  4221. /*
  4222. * FIXME IPS should be fine as long as one plane is
  4223. * enabled, but in practice it seems to have problems
  4224. * when going from primary only to sprite only and vice
  4225. * versa.
  4226. */
  4227. hsw_enable_ips(intel_crtc);
  4228. /*
  4229. * Gen2 reports pipe underruns whenever all planes are disabled.
  4230. * So don't enable underrun reporting before at least some planes
  4231. * are enabled.
  4232. * FIXME: Need to fix the logic to work when we turn off all planes
  4233. * but leave the pipe running.
  4234. */
  4235. if (IS_GEN2(dev_priv))
  4236. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4237. /* Underruns don't always raise interrupts, so check manually. */
  4238. intel_check_cpu_fifo_underruns(dev_priv);
  4239. intel_check_pch_fifo_underruns(dev_priv);
  4240. }
  4241. /* FIXME move all this to pre_plane_update() with proper state tracking */
  4242. static void
  4243. intel_pre_disable_primary(struct drm_crtc *crtc)
  4244. {
  4245. struct drm_device *dev = crtc->dev;
  4246. struct drm_i915_private *dev_priv = to_i915(dev);
  4247. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4248. int pipe = intel_crtc->pipe;
  4249. /*
  4250. * Gen2 reports pipe underruns whenever all planes are disabled.
  4251. * So diasble underrun reporting before all the planes get disabled.
  4252. * FIXME: Need to fix the logic to work when we turn off all planes
  4253. * but leave the pipe running.
  4254. */
  4255. if (IS_GEN2(dev_priv))
  4256. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4257. /*
  4258. * FIXME IPS should be fine as long as one plane is
  4259. * enabled, but in practice it seems to have problems
  4260. * when going from primary only to sprite only and vice
  4261. * versa.
  4262. */
  4263. hsw_disable_ips(intel_crtc);
  4264. }
  4265. /* FIXME get rid of this and use pre_plane_update */
  4266. static void
  4267. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  4268. {
  4269. struct drm_device *dev = crtc->dev;
  4270. struct drm_i915_private *dev_priv = to_i915(dev);
  4271. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4272. int pipe = intel_crtc->pipe;
  4273. intel_pre_disable_primary(crtc);
  4274. /*
  4275. * Vblank time updates from the shadow to live plane control register
  4276. * are blocked if the memory self-refresh mode is active at that
  4277. * moment. So to make sure the plane gets truly disabled, disable
  4278. * first the self-refresh mode. The self-refresh enable bit in turn
  4279. * will be checked/applied by the HW only at the next frame start
  4280. * event which is after the vblank start event, so we need to have a
  4281. * wait-for-vblank between disabling the plane and the pipe.
  4282. */
  4283. if (HAS_GMCH_DISPLAY(dev_priv) &&
  4284. intel_set_memory_cxsr(dev_priv, false))
  4285. intel_wait_for_vblank(dev_priv, pipe);
  4286. }
  4287. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  4288. {
  4289. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4290. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4291. struct intel_crtc_state *pipe_config =
  4292. to_intel_crtc_state(crtc->base.state);
  4293. struct drm_plane *primary = crtc->base.primary;
  4294. struct drm_plane_state *old_pri_state =
  4295. drm_atomic_get_existing_plane_state(old_state, primary);
  4296. intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
  4297. crtc->wm.cxsr_allowed = true;
  4298. if (pipe_config->update_wm_post && pipe_config->base.active)
  4299. intel_update_watermarks(crtc);
  4300. if (old_pri_state) {
  4301. struct intel_plane_state *primary_state =
  4302. to_intel_plane_state(primary->state);
  4303. struct intel_plane_state *old_primary_state =
  4304. to_intel_plane_state(old_pri_state);
  4305. intel_fbc_post_update(crtc);
  4306. if (primary_state->base.visible &&
  4307. (needs_modeset(&pipe_config->base) ||
  4308. !old_primary_state->base.visible))
  4309. intel_post_enable_primary(&crtc->base);
  4310. }
  4311. }
  4312. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
  4313. {
  4314. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4315. struct drm_device *dev = crtc->base.dev;
  4316. struct drm_i915_private *dev_priv = to_i915(dev);
  4317. struct intel_crtc_state *pipe_config =
  4318. to_intel_crtc_state(crtc->base.state);
  4319. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4320. struct drm_plane *primary = crtc->base.primary;
  4321. struct drm_plane_state *old_pri_state =
  4322. drm_atomic_get_existing_plane_state(old_state, primary);
  4323. bool modeset = needs_modeset(&pipe_config->base);
  4324. struct intel_atomic_state *old_intel_state =
  4325. to_intel_atomic_state(old_state);
  4326. if (old_pri_state) {
  4327. struct intel_plane_state *primary_state =
  4328. to_intel_plane_state(primary->state);
  4329. struct intel_plane_state *old_primary_state =
  4330. to_intel_plane_state(old_pri_state);
  4331. intel_fbc_pre_update(crtc, pipe_config, primary_state);
  4332. if (old_primary_state->base.visible &&
  4333. (modeset || !primary_state->base.visible))
  4334. intel_pre_disable_primary(&crtc->base);
  4335. }
  4336. if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev_priv)) {
  4337. crtc->wm.cxsr_allowed = false;
  4338. /*
  4339. * Vblank time updates from the shadow to live plane control register
  4340. * are blocked if the memory self-refresh mode is active at that
  4341. * moment. So to make sure the plane gets truly disabled, disable
  4342. * first the self-refresh mode. The self-refresh enable bit in turn
  4343. * will be checked/applied by the HW only at the next frame start
  4344. * event which is after the vblank start event, so we need to have a
  4345. * wait-for-vblank between disabling the plane and the pipe.
  4346. */
  4347. if (old_crtc_state->base.active &&
  4348. intel_set_memory_cxsr(dev_priv, false))
  4349. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4350. }
  4351. /*
  4352. * IVB workaround: must disable low power watermarks for at least
  4353. * one frame before enabling scaling. LP watermarks can be re-enabled
  4354. * when scaling is disabled.
  4355. *
  4356. * WaCxSRDisabledForSpriteScaling:ivb
  4357. */
  4358. if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev))
  4359. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4360. /*
  4361. * If we're doing a modeset, we're done. No need to do any pre-vblank
  4362. * watermark programming here.
  4363. */
  4364. if (needs_modeset(&pipe_config->base))
  4365. return;
  4366. /*
  4367. * For platforms that support atomic watermarks, program the
  4368. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  4369. * will be the intermediate values that are safe for both pre- and
  4370. * post- vblank; when vblank happens, the 'active' values will be set
  4371. * to the final 'target' values and we'll do this again to get the
  4372. * optimal watermarks. For gen9+ platforms, the values we program here
  4373. * will be the final target values which will get automatically latched
  4374. * at vblank time; no further programming will be necessary.
  4375. *
  4376. * If a platform hasn't been transitioned to atomic watermarks yet,
  4377. * we'll continue to update watermarks the old way, if flags tell
  4378. * us to.
  4379. */
  4380. if (dev_priv->display.initial_watermarks != NULL)
  4381. dev_priv->display.initial_watermarks(old_intel_state,
  4382. pipe_config);
  4383. else if (pipe_config->update_wm_pre)
  4384. intel_update_watermarks(crtc);
  4385. }
  4386. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4387. {
  4388. struct drm_device *dev = crtc->dev;
  4389. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4390. struct drm_plane *p;
  4391. int pipe = intel_crtc->pipe;
  4392. intel_crtc_dpms_overlay_disable(intel_crtc);
  4393. drm_for_each_plane_mask(p, dev, plane_mask)
  4394. to_intel_plane(p)->disable_plane(p, crtc);
  4395. /*
  4396. * FIXME: Once we grow proper nuclear flip support out of this we need
  4397. * to compute the mask of flip planes precisely. For the time being
  4398. * consider this a flip to a NULL plane.
  4399. */
  4400. intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4401. }
  4402. static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
  4403. struct intel_crtc_state *crtc_state,
  4404. struct drm_atomic_state *old_state)
  4405. {
  4406. struct drm_connector_state *old_conn_state;
  4407. struct drm_connector *conn;
  4408. int i;
  4409. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4410. struct drm_connector_state *conn_state = conn->state;
  4411. struct intel_encoder *encoder =
  4412. to_intel_encoder(conn_state->best_encoder);
  4413. if (conn_state->crtc != crtc)
  4414. continue;
  4415. if (encoder->pre_pll_enable)
  4416. encoder->pre_pll_enable(encoder, crtc_state, conn_state);
  4417. }
  4418. }
  4419. static void intel_encoders_pre_enable(struct drm_crtc *crtc,
  4420. struct intel_crtc_state *crtc_state,
  4421. struct drm_atomic_state *old_state)
  4422. {
  4423. struct drm_connector_state *old_conn_state;
  4424. struct drm_connector *conn;
  4425. int i;
  4426. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4427. struct drm_connector_state *conn_state = conn->state;
  4428. struct intel_encoder *encoder =
  4429. to_intel_encoder(conn_state->best_encoder);
  4430. if (conn_state->crtc != crtc)
  4431. continue;
  4432. if (encoder->pre_enable)
  4433. encoder->pre_enable(encoder, crtc_state, conn_state);
  4434. }
  4435. }
  4436. static void intel_encoders_enable(struct drm_crtc *crtc,
  4437. struct intel_crtc_state *crtc_state,
  4438. struct drm_atomic_state *old_state)
  4439. {
  4440. struct drm_connector_state *old_conn_state;
  4441. struct drm_connector *conn;
  4442. int i;
  4443. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4444. struct drm_connector_state *conn_state = conn->state;
  4445. struct intel_encoder *encoder =
  4446. to_intel_encoder(conn_state->best_encoder);
  4447. if (conn_state->crtc != crtc)
  4448. continue;
  4449. encoder->enable(encoder, crtc_state, conn_state);
  4450. intel_opregion_notify_encoder(encoder, true);
  4451. }
  4452. }
  4453. static void intel_encoders_disable(struct drm_crtc *crtc,
  4454. struct intel_crtc_state *old_crtc_state,
  4455. struct drm_atomic_state *old_state)
  4456. {
  4457. struct drm_connector_state *old_conn_state;
  4458. struct drm_connector *conn;
  4459. int i;
  4460. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4461. struct intel_encoder *encoder =
  4462. to_intel_encoder(old_conn_state->best_encoder);
  4463. if (old_conn_state->crtc != crtc)
  4464. continue;
  4465. intel_opregion_notify_encoder(encoder, false);
  4466. encoder->disable(encoder, old_crtc_state, old_conn_state);
  4467. }
  4468. }
  4469. static void intel_encoders_post_disable(struct drm_crtc *crtc,
  4470. struct intel_crtc_state *old_crtc_state,
  4471. struct drm_atomic_state *old_state)
  4472. {
  4473. struct drm_connector_state *old_conn_state;
  4474. struct drm_connector *conn;
  4475. int i;
  4476. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4477. struct intel_encoder *encoder =
  4478. to_intel_encoder(old_conn_state->best_encoder);
  4479. if (old_conn_state->crtc != crtc)
  4480. continue;
  4481. if (encoder->post_disable)
  4482. encoder->post_disable(encoder, old_crtc_state, old_conn_state);
  4483. }
  4484. }
  4485. static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
  4486. struct intel_crtc_state *old_crtc_state,
  4487. struct drm_atomic_state *old_state)
  4488. {
  4489. struct drm_connector_state *old_conn_state;
  4490. struct drm_connector *conn;
  4491. int i;
  4492. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4493. struct intel_encoder *encoder =
  4494. to_intel_encoder(old_conn_state->best_encoder);
  4495. if (old_conn_state->crtc != crtc)
  4496. continue;
  4497. if (encoder->post_pll_disable)
  4498. encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
  4499. }
  4500. }
  4501. static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
  4502. struct drm_atomic_state *old_state)
  4503. {
  4504. struct drm_crtc *crtc = pipe_config->base.crtc;
  4505. struct drm_device *dev = crtc->dev;
  4506. struct drm_i915_private *dev_priv = to_i915(dev);
  4507. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4508. int pipe = intel_crtc->pipe;
  4509. struct intel_atomic_state *old_intel_state =
  4510. to_intel_atomic_state(old_state);
  4511. if (WARN_ON(intel_crtc->active))
  4512. return;
  4513. /*
  4514. * Sometimes spurious CPU pipe underruns happen during FDI
  4515. * training, at least with VGA+HDMI cloning. Suppress them.
  4516. *
  4517. * On ILK we get an occasional spurious CPU pipe underruns
  4518. * between eDP port A enable and vdd enable. Also PCH port
  4519. * enable seems to result in the occasional CPU pipe underrun.
  4520. *
  4521. * Spurious PCH underruns also occur during PCH enabling.
  4522. */
  4523. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4524. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4525. if (intel_crtc->config->has_pch_encoder)
  4526. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4527. if (intel_crtc->config->has_pch_encoder)
  4528. intel_prepare_shared_dpll(intel_crtc);
  4529. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4530. intel_dp_set_m_n(intel_crtc, M1_N1);
  4531. intel_set_pipe_timings(intel_crtc);
  4532. intel_set_pipe_src_size(intel_crtc);
  4533. if (intel_crtc->config->has_pch_encoder) {
  4534. intel_cpu_transcoder_set_m_n(intel_crtc,
  4535. &intel_crtc->config->fdi_m_n, NULL);
  4536. }
  4537. ironlake_set_pipeconf(crtc);
  4538. intel_crtc->active = true;
  4539. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4540. if (intel_crtc->config->has_pch_encoder) {
  4541. /* Note: FDI PLL enabling _must_ be done before we enable the
  4542. * cpu pipes, hence this is separate from all the other fdi/pch
  4543. * enabling. */
  4544. ironlake_fdi_pll_enable(intel_crtc);
  4545. } else {
  4546. assert_fdi_tx_disabled(dev_priv, pipe);
  4547. assert_fdi_rx_disabled(dev_priv, pipe);
  4548. }
  4549. ironlake_pfit_enable(intel_crtc);
  4550. /*
  4551. * On ILK+ LUT must be loaded before the pipe is running but with
  4552. * clocks enabled
  4553. */
  4554. intel_color_load_luts(&pipe_config->base);
  4555. if (dev_priv->display.initial_watermarks != NULL)
  4556. dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
  4557. intel_enable_pipe(intel_crtc);
  4558. if (intel_crtc->config->has_pch_encoder)
  4559. ironlake_pch_enable(crtc);
  4560. assert_vblank_disabled(crtc);
  4561. drm_crtc_vblank_on(crtc);
  4562. intel_encoders_enable(crtc, pipe_config, old_state);
  4563. if (HAS_PCH_CPT(dev_priv))
  4564. cpt_verify_modeset(dev, intel_crtc->pipe);
  4565. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4566. if (intel_crtc->config->has_pch_encoder)
  4567. intel_wait_for_vblank(dev_priv, pipe);
  4568. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4569. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4570. }
  4571. /* IPS only exists on ULT machines and is tied to pipe A. */
  4572. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4573. {
  4574. return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
  4575. }
  4576. static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
  4577. struct drm_atomic_state *old_state)
  4578. {
  4579. struct drm_crtc *crtc = pipe_config->base.crtc;
  4580. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4581. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4582. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4583. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4584. struct intel_atomic_state *old_intel_state =
  4585. to_intel_atomic_state(old_state);
  4586. if (WARN_ON(intel_crtc->active))
  4587. return;
  4588. if (intel_crtc->config->has_pch_encoder)
  4589. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4590. false);
  4591. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4592. if (intel_crtc->config->shared_dpll)
  4593. intel_enable_shared_dpll(intel_crtc);
  4594. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4595. intel_dp_set_m_n(intel_crtc, M1_N1);
  4596. if (!transcoder_is_dsi(cpu_transcoder))
  4597. intel_set_pipe_timings(intel_crtc);
  4598. intel_set_pipe_src_size(intel_crtc);
  4599. if (cpu_transcoder != TRANSCODER_EDP &&
  4600. !transcoder_is_dsi(cpu_transcoder)) {
  4601. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4602. intel_crtc->config->pixel_multiplier - 1);
  4603. }
  4604. if (intel_crtc->config->has_pch_encoder) {
  4605. intel_cpu_transcoder_set_m_n(intel_crtc,
  4606. &intel_crtc->config->fdi_m_n, NULL);
  4607. }
  4608. if (!transcoder_is_dsi(cpu_transcoder))
  4609. haswell_set_pipeconf(crtc);
  4610. haswell_set_pipemisc(crtc);
  4611. intel_color_set_csc(&pipe_config->base);
  4612. intel_crtc->active = true;
  4613. if (intel_crtc->config->has_pch_encoder)
  4614. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4615. else
  4616. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4617. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4618. if (intel_crtc->config->has_pch_encoder)
  4619. dev_priv->display.fdi_link_train(crtc);
  4620. if (!transcoder_is_dsi(cpu_transcoder))
  4621. intel_ddi_enable_pipe_clock(intel_crtc);
  4622. if (INTEL_GEN(dev_priv) >= 9)
  4623. skylake_pfit_enable(intel_crtc);
  4624. else
  4625. ironlake_pfit_enable(intel_crtc);
  4626. /*
  4627. * On ILK+ LUT must be loaded before the pipe is running but with
  4628. * clocks enabled
  4629. */
  4630. intel_color_load_luts(&pipe_config->base);
  4631. intel_ddi_set_pipe_settings(crtc);
  4632. if (!transcoder_is_dsi(cpu_transcoder))
  4633. intel_ddi_enable_transcoder_func(crtc);
  4634. if (dev_priv->display.initial_watermarks != NULL)
  4635. dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
  4636. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4637. if (!transcoder_is_dsi(cpu_transcoder))
  4638. intel_enable_pipe(intel_crtc);
  4639. if (intel_crtc->config->has_pch_encoder)
  4640. lpt_pch_enable(crtc);
  4641. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4642. intel_ddi_set_vc_payload_alloc(crtc, true);
  4643. assert_vblank_disabled(crtc);
  4644. drm_crtc_vblank_on(crtc);
  4645. intel_encoders_enable(crtc, pipe_config, old_state);
  4646. if (intel_crtc->config->has_pch_encoder) {
  4647. intel_wait_for_vblank(dev_priv, pipe);
  4648. intel_wait_for_vblank(dev_priv, pipe);
  4649. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4650. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4651. true);
  4652. }
  4653. /* If we change the relative order between pipe/planes enabling, we need
  4654. * to change the workaround. */
  4655. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4656. if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
  4657. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4658. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4659. }
  4660. }
  4661. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4662. {
  4663. struct drm_device *dev = crtc->base.dev;
  4664. struct drm_i915_private *dev_priv = to_i915(dev);
  4665. int pipe = crtc->pipe;
  4666. /* To avoid upsetting the power well on haswell only disable the pfit if
  4667. * it's in use. The hw state code will make sure we get this right. */
  4668. if (force || crtc->config->pch_pfit.enabled) {
  4669. I915_WRITE(PF_CTL(pipe), 0);
  4670. I915_WRITE(PF_WIN_POS(pipe), 0);
  4671. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4672. }
  4673. }
  4674. static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4675. struct drm_atomic_state *old_state)
  4676. {
  4677. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4678. struct drm_device *dev = crtc->dev;
  4679. struct drm_i915_private *dev_priv = to_i915(dev);
  4680. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4681. int pipe = intel_crtc->pipe;
  4682. /*
  4683. * Sometimes spurious CPU pipe underruns happen when the
  4684. * pipe is already disabled, but FDI RX/TX is still enabled.
  4685. * Happens at least with VGA+HDMI cloning. Suppress them.
  4686. */
  4687. if (intel_crtc->config->has_pch_encoder) {
  4688. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4689. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4690. }
  4691. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4692. drm_crtc_vblank_off(crtc);
  4693. assert_vblank_disabled(crtc);
  4694. intel_disable_pipe(intel_crtc);
  4695. ironlake_pfit_disable(intel_crtc, false);
  4696. if (intel_crtc->config->has_pch_encoder)
  4697. ironlake_fdi_disable(crtc);
  4698. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4699. if (intel_crtc->config->has_pch_encoder) {
  4700. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4701. if (HAS_PCH_CPT(dev_priv)) {
  4702. i915_reg_t reg;
  4703. u32 temp;
  4704. /* disable TRANS_DP_CTL */
  4705. reg = TRANS_DP_CTL(pipe);
  4706. temp = I915_READ(reg);
  4707. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4708. TRANS_DP_PORT_SEL_MASK);
  4709. temp |= TRANS_DP_PORT_SEL_NONE;
  4710. I915_WRITE(reg, temp);
  4711. /* disable DPLL_SEL */
  4712. temp = I915_READ(PCH_DPLL_SEL);
  4713. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4714. I915_WRITE(PCH_DPLL_SEL, temp);
  4715. }
  4716. ironlake_fdi_pll_disable(intel_crtc);
  4717. }
  4718. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4719. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4720. }
  4721. static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4722. struct drm_atomic_state *old_state)
  4723. {
  4724. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4725. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4726. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4727. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4728. if (intel_crtc->config->has_pch_encoder)
  4729. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4730. false);
  4731. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4732. drm_crtc_vblank_off(crtc);
  4733. assert_vblank_disabled(crtc);
  4734. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4735. if (!transcoder_is_dsi(cpu_transcoder))
  4736. intel_disable_pipe(intel_crtc);
  4737. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4738. intel_ddi_set_vc_payload_alloc(crtc, false);
  4739. if (!transcoder_is_dsi(cpu_transcoder))
  4740. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4741. if (INTEL_GEN(dev_priv) >= 9)
  4742. skylake_scaler_disable(intel_crtc);
  4743. else
  4744. ironlake_pfit_disable(intel_crtc, false);
  4745. if (!transcoder_is_dsi(cpu_transcoder))
  4746. intel_ddi_disable_pipe_clock(intel_crtc);
  4747. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4748. if (old_crtc_state->has_pch_encoder)
  4749. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4750. true);
  4751. }
  4752. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4753. {
  4754. struct drm_device *dev = crtc->base.dev;
  4755. struct drm_i915_private *dev_priv = to_i915(dev);
  4756. struct intel_crtc_state *pipe_config = crtc->config;
  4757. if (!pipe_config->gmch_pfit.control)
  4758. return;
  4759. /*
  4760. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4761. * according to register description and PRM.
  4762. */
  4763. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4764. assert_pipe_disabled(dev_priv, crtc->pipe);
  4765. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4766. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4767. /* Border color in case we don't scale up to the full screen. Black by
  4768. * default, change to something else for debugging. */
  4769. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4770. }
  4771. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4772. {
  4773. switch (port) {
  4774. case PORT_A:
  4775. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4776. case PORT_B:
  4777. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4778. case PORT_C:
  4779. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4780. case PORT_D:
  4781. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4782. case PORT_E:
  4783. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4784. default:
  4785. MISSING_CASE(port);
  4786. return POWER_DOMAIN_PORT_OTHER;
  4787. }
  4788. }
  4789. static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
  4790. {
  4791. switch (port) {
  4792. case PORT_A:
  4793. return POWER_DOMAIN_AUX_A;
  4794. case PORT_B:
  4795. return POWER_DOMAIN_AUX_B;
  4796. case PORT_C:
  4797. return POWER_DOMAIN_AUX_C;
  4798. case PORT_D:
  4799. return POWER_DOMAIN_AUX_D;
  4800. case PORT_E:
  4801. /* FIXME: Check VBT for actual wiring of PORT E */
  4802. return POWER_DOMAIN_AUX_D;
  4803. default:
  4804. MISSING_CASE(port);
  4805. return POWER_DOMAIN_AUX_A;
  4806. }
  4807. }
  4808. enum intel_display_power_domain
  4809. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4810. {
  4811. struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
  4812. struct intel_digital_port *intel_dig_port;
  4813. switch (intel_encoder->type) {
  4814. case INTEL_OUTPUT_UNKNOWN:
  4815. /* Only DDI platforms should ever use this output type */
  4816. WARN_ON_ONCE(!HAS_DDI(dev_priv));
  4817. case INTEL_OUTPUT_DP:
  4818. case INTEL_OUTPUT_HDMI:
  4819. case INTEL_OUTPUT_EDP:
  4820. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4821. return port_to_power_domain(intel_dig_port->port);
  4822. case INTEL_OUTPUT_DP_MST:
  4823. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4824. return port_to_power_domain(intel_dig_port->port);
  4825. case INTEL_OUTPUT_ANALOG:
  4826. return POWER_DOMAIN_PORT_CRT;
  4827. case INTEL_OUTPUT_DSI:
  4828. return POWER_DOMAIN_PORT_DSI;
  4829. default:
  4830. return POWER_DOMAIN_PORT_OTHER;
  4831. }
  4832. }
  4833. enum intel_display_power_domain
  4834. intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
  4835. {
  4836. struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
  4837. struct intel_digital_port *intel_dig_port;
  4838. switch (intel_encoder->type) {
  4839. case INTEL_OUTPUT_UNKNOWN:
  4840. case INTEL_OUTPUT_HDMI:
  4841. /*
  4842. * Only DDI platforms should ever use these output types.
  4843. * We can get here after the HDMI detect code has already set
  4844. * the type of the shared encoder. Since we can't be sure
  4845. * what's the status of the given connectors, play safe and
  4846. * run the DP detection too.
  4847. */
  4848. WARN_ON_ONCE(!HAS_DDI(dev_priv));
  4849. case INTEL_OUTPUT_DP:
  4850. case INTEL_OUTPUT_EDP:
  4851. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4852. return port_to_aux_power_domain(intel_dig_port->port);
  4853. case INTEL_OUTPUT_DP_MST:
  4854. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4855. return port_to_aux_power_domain(intel_dig_port->port);
  4856. default:
  4857. MISSING_CASE(intel_encoder->type);
  4858. return POWER_DOMAIN_AUX_A;
  4859. }
  4860. }
  4861. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
  4862. struct intel_crtc_state *crtc_state)
  4863. {
  4864. struct drm_device *dev = crtc->dev;
  4865. struct drm_encoder *encoder;
  4866. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4867. enum pipe pipe = intel_crtc->pipe;
  4868. unsigned long mask;
  4869. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4870. if (!crtc_state->base.active)
  4871. return 0;
  4872. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4873. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4874. if (crtc_state->pch_pfit.enabled ||
  4875. crtc_state->pch_pfit.force_thru)
  4876. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4877. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4878. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4879. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4880. }
  4881. if (crtc_state->shared_dpll)
  4882. mask |= BIT(POWER_DOMAIN_PLLS);
  4883. return mask;
  4884. }
  4885. static unsigned long
  4886. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4887. struct intel_crtc_state *crtc_state)
  4888. {
  4889. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4890. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4891. enum intel_display_power_domain domain;
  4892. unsigned long domains, new_domains, old_domains;
  4893. old_domains = intel_crtc->enabled_power_domains;
  4894. intel_crtc->enabled_power_domains = new_domains =
  4895. get_crtc_power_domains(crtc, crtc_state);
  4896. domains = new_domains & ~old_domains;
  4897. for_each_power_domain(domain, domains)
  4898. intel_display_power_get(dev_priv, domain);
  4899. return old_domains & ~new_domains;
  4900. }
  4901. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4902. unsigned long domains)
  4903. {
  4904. enum intel_display_power_domain domain;
  4905. for_each_power_domain(domain, domains)
  4906. intel_display_power_put(dev_priv, domain);
  4907. }
  4908. static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
  4909. {
  4910. int max_cdclk_freq = dev_priv->max_cdclk_freq;
  4911. if (IS_GEMINILAKE(dev_priv))
  4912. return 2 * max_cdclk_freq;
  4913. else if (INTEL_INFO(dev_priv)->gen >= 9 ||
  4914. IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  4915. return max_cdclk_freq;
  4916. else if (IS_CHERRYVIEW(dev_priv))
  4917. return max_cdclk_freq*95/100;
  4918. else if (INTEL_INFO(dev_priv)->gen < 4)
  4919. return 2*max_cdclk_freq*90/100;
  4920. else
  4921. return max_cdclk_freq*90/100;
  4922. }
  4923. static int skl_calc_cdclk(int max_pixclk, int vco);
  4924. static void intel_update_max_cdclk(struct drm_i915_private *dev_priv)
  4925. {
  4926. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
  4927. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4928. int max_cdclk, vco;
  4929. vco = dev_priv->skl_preferred_vco_freq;
  4930. WARN_ON(vco != 8100000 && vco != 8640000);
  4931. /*
  4932. * Use the lower (vco 8640) cdclk values as a
  4933. * first guess. skl_calc_cdclk() will correct it
  4934. * if the preferred vco is 8100 instead.
  4935. */
  4936. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4937. max_cdclk = 617143;
  4938. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4939. max_cdclk = 540000;
  4940. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4941. max_cdclk = 432000;
  4942. else
  4943. max_cdclk = 308571;
  4944. dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
  4945. } else if (IS_GEMINILAKE(dev_priv)) {
  4946. dev_priv->max_cdclk_freq = 316800;
  4947. } else if (IS_BROXTON(dev_priv)) {
  4948. dev_priv->max_cdclk_freq = 624000;
  4949. } else if (IS_BROADWELL(dev_priv)) {
  4950. /*
  4951. * FIXME with extra cooling we can allow
  4952. * 540 MHz for ULX and 675 Mhz for ULT.
  4953. * How can we know if extra cooling is
  4954. * available? PCI ID, VTB, something else?
  4955. */
  4956. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4957. dev_priv->max_cdclk_freq = 450000;
  4958. else if (IS_BDW_ULX(dev_priv))
  4959. dev_priv->max_cdclk_freq = 450000;
  4960. else if (IS_BDW_ULT(dev_priv))
  4961. dev_priv->max_cdclk_freq = 540000;
  4962. else
  4963. dev_priv->max_cdclk_freq = 675000;
  4964. } else if (IS_CHERRYVIEW(dev_priv)) {
  4965. dev_priv->max_cdclk_freq = 320000;
  4966. } else if (IS_VALLEYVIEW(dev_priv)) {
  4967. dev_priv->max_cdclk_freq = 400000;
  4968. } else {
  4969. /* otherwise assume cdclk is fixed */
  4970. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4971. }
  4972. dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
  4973. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4974. dev_priv->max_cdclk_freq);
  4975. DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
  4976. dev_priv->max_dotclk_freq);
  4977. }
  4978. static void intel_update_cdclk(struct drm_i915_private *dev_priv)
  4979. {
  4980. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev_priv);
  4981. if (INTEL_GEN(dev_priv) >= 9)
  4982. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
  4983. dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
  4984. dev_priv->cdclk_pll.ref);
  4985. else
  4986. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4987. dev_priv->cdclk_freq);
  4988. /*
  4989. * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
  4990. * Programmng [sic] note: bit[9:2] should be programmed to the number
  4991. * of cdclk that generates 4MHz reference clock freq which is used to
  4992. * generate GMBus clock. This will vary with the cdclk freq.
  4993. */
  4994. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  4995. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4996. }
  4997. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4998. static int skl_cdclk_decimal(int cdclk)
  4999. {
  5000. return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
  5001. }
  5002. static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
  5003. {
  5004. int ratio;
  5005. if (cdclk == dev_priv->cdclk_pll.ref)
  5006. return 0;
  5007. switch (cdclk) {
  5008. default:
  5009. MISSING_CASE(cdclk);
  5010. case 144000:
  5011. case 288000:
  5012. case 384000:
  5013. case 576000:
  5014. ratio = 60;
  5015. break;
  5016. case 624000:
  5017. ratio = 65;
  5018. break;
  5019. }
  5020. return dev_priv->cdclk_pll.ref * ratio;
  5021. }
  5022. static int glk_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
  5023. {
  5024. int ratio;
  5025. if (cdclk == dev_priv->cdclk_pll.ref)
  5026. return 0;
  5027. switch (cdclk) {
  5028. default:
  5029. MISSING_CASE(cdclk);
  5030. case 79200:
  5031. case 158400:
  5032. case 316800:
  5033. ratio = 33;
  5034. break;
  5035. }
  5036. return dev_priv->cdclk_pll.ref * ratio;
  5037. }
  5038. static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
  5039. {
  5040. I915_WRITE(BXT_DE_PLL_ENABLE, 0);
  5041. /* Timeout 200us */
  5042. if (intel_wait_for_register(dev_priv,
  5043. BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
  5044. 1))
  5045. DRM_ERROR("timeout waiting for DE PLL unlock\n");
  5046. dev_priv->cdclk_pll.vco = 0;
  5047. }
  5048. static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
  5049. {
  5050. int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
  5051. u32 val;
  5052. val = I915_READ(BXT_DE_PLL_CTL);
  5053. val &= ~BXT_DE_PLL_RATIO_MASK;
  5054. val |= BXT_DE_PLL_RATIO(ratio);
  5055. I915_WRITE(BXT_DE_PLL_CTL, val);
  5056. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  5057. /* Timeout 200us */
  5058. if (intel_wait_for_register(dev_priv,
  5059. BXT_DE_PLL_ENABLE,
  5060. BXT_DE_PLL_LOCK,
  5061. BXT_DE_PLL_LOCK,
  5062. 1))
  5063. DRM_ERROR("timeout waiting for DE PLL lock\n");
  5064. dev_priv->cdclk_pll.vco = vco;
  5065. }
  5066. static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
  5067. {
  5068. u32 val, divider;
  5069. int vco, ret;
  5070. if (IS_GEMINILAKE(dev_priv))
  5071. vco = glk_de_pll_vco(dev_priv, cdclk);
  5072. else
  5073. vco = bxt_de_pll_vco(dev_priv, cdclk);
  5074. DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
  5075. /* cdclk = vco / 2 / div{1,1.5,2,4} */
  5076. switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
  5077. case 8:
  5078. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  5079. break;
  5080. case 4:
  5081. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  5082. break;
  5083. case 3:
  5084. WARN(IS_GEMINILAKE(dev_priv), "Unsupported divider\n");
  5085. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  5086. break;
  5087. case 2:
  5088. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  5089. break;
  5090. default:
  5091. WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
  5092. WARN_ON(vco != 0);
  5093. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  5094. break;
  5095. }
  5096. /* Inform power controller of upcoming frequency change */
  5097. mutex_lock(&dev_priv->rps.hw_lock);
  5098. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  5099. 0x80000000);
  5100. mutex_unlock(&dev_priv->rps.hw_lock);
  5101. if (ret) {
  5102. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  5103. ret, cdclk);
  5104. return;
  5105. }
  5106. if (dev_priv->cdclk_pll.vco != 0 &&
  5107. dev_priv->cdclk_pll.vco != vco)
  5108. bxt_de_pll_disable(dev_priv);
  5109. if (dev_priv->cdclk_pll.vco != vco)
  5110. bxt_de_pll_enable(dev_priv, vco);
  5111. val = divider | skl_cdclk_decimal(cdclk);
  5112. /*
  5113. * FIXME if only the cd2x divider needs changing, it could be done
  5114. * without shutting off the pipe (if only one pipe is active).
  5115. */
  5116. val |= BXT_CDCLK_CD2X_PIPE_NONE;
  5117. /*
  5118. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  5119. * enable otherwise.
  5120. */
  5121. if (cdclk >= 500000)
  5122. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  5123. I915_WRITE(CDCLK_CTL, val);
  5124. mutex_lock(&dev_priv->rps.hw_lock);
  5125. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  5126. DIV_ROUND_UP(cdclk, 25000));
  5127. mutex_unlock(&dev_priv->rps.hw_lock);
  5128. if (ret) {
  5129. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  5130. ret, cdclk);
  5131. return;
  5132. }
  5133. intel_update_cdclk(dev_priv);
  5134. }
  5135. static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
  5136. {
  5137. u32 cdctl, expected;
  5138. intel_update_cdclk(dev_priv);
  5139. if (dev_priv->cdclk_pll.vco == 0 ||
  5140. dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
  5141. goto sanitize;
  5142. /* DPLL okay; verify the cdclock
  5143. *
  5144. * Some BIOS versions leave an incorrect decimal frequency value and
  5145. * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
  5146. * so sanitize this register.
  5147. */
  5148. cdctl = I915_READ(CDCLK_CTL);
  5149. /*
  5150. * Let's ignore the pipe field, since BIOS could have configured the
  5151. * dividers both synching to an active pipe, or asynchronously
  5152. * (PIPE_NONE).
  5153. */
  5154. cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
  5155. expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
  5156. skl_cdclk_decimal(dev_priv->cdclk_freq);
  5157. /*
  5158. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  5159. * enable otherwise.
  5160. */
  5161. if (dev_priv->cdclk_freq >= 500000)
  5162. expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  5163. if (cdctl == expected)
  5164. /* All well; nothing to sanitize */
  5165. return;
  5166. sanitize:
  5167. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  5168. /* force cdclk programming */
  5169. dev_priv->cdclk_freq = 0;
  5170. /* force full PLL disable + enable */
  5171. dev_priv->cdclk_pll.vco = -1;
  5172. }
  5173. void bxt_init_cdclk(struct drm_i915_private *dev_priv)
  5174. {
  5175. int cdclk;
  5176. bxt_sanitize_cdclk(dev_priv);
  5177. if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
  5178. return;
  5179. /*
  5180. * FIXME:
  5181. * - The initial CDCLK needs to be read from VBT.
  5182. * Need to make this change after VBT has changes for BXT.
  5183. */
  5184. if (IS_GEMINILAKE(dev_priv))
  5185. cdclk = glk_calc_cdclk(0);
  5186. else
  5187. cdclk = bxt_calc_cdclk(0);
  5188. bxt_set_cdclk(dev_priv, cdclk);
  5189. }
  5190. void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
  5191. {
  5192. bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
  5193. }
  5194. static int skl_calc_cdclk(int max_pixclk, int vco)
  5195. {
  5196. if (vco == 8640000) {
  5197. if (max_pixclk > 540000)
  5198. return 617143;
  5199. else if (max_pixclk > 432000)
  5200. return 540000;
  5201. else if (max_pixclk > 308571)
  5202. return 432000;
  5203. else
  5204. return 308571;
  5205. } else {
  5206. if (max_pixclk > 540000)
  5207. return 675000;
  5208. else if (max_pixclk > 450000)
  5209. return 540000;
  5210. else if (max_pixclk > 337500)
  5211. return 450000;
  5212. else
  5213. return 337500;
  5214. }
  5215. }
  5216. static void
  5217. skl_dpll0_update(struct drm_i915_private *dev_priv)
  5218. {
  5219. u32 val;
  5220. dev_priv->cdclk_pll.ref = 24000;
  5221. dev_priv->cdclk_pll.vco = 0;
  5222. val = I915_READ(LCPLL1_CTL);
  5223. if ((val & LCPLL_PLL_ENABLE) == 0)
  5224. return;
  5225. if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
  5226. return;
  5227. val = I915_READ(DPLL_CTRL1);
  5228. if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
  5229. DPLL_CTRL1_SSC(SKL_DPLL0) |
  5230. DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
  5231. DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
  5232. return;
  5233. switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
  5234. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
  5235. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
  5236. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
  5237. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
  5238. dev_priv->cdclk_pll.vco = 8100000;
  5239. break;
  5240. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
  5241. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
  5242. dev_priv->cdclk_pll.vco = 8640000;
  5243. break;
  5244. default:
  5245. MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  5246. break;
  5247. }
  5248. }
  5249. void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
  5250. {
  5251. bool changed = dev_priv->skl_preferred_vco_freq != vco;
  5252. dev_priv->skl_preferred_vco_freq = vco;
  5253. if (changed)
  5254. intel_update_max_cdclk(dev_priv);
  5255. }
  5256. static void
  5257. skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
  5258. {
  5259. int min_cdclk = skl_calc_cdclk(0, vco);
  5260. u32 val;
  5261. WARN_ON(vco != 8100000 && vco != 8640000);
  5262. /* select the minimum CDCLK before enabling DPLL 0 */
  5263. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
  5264. I915_WRITE(CDCLK_CTL, val);
  5265. POSTING_READ(CDCLK_CTL);
  5266. /*
  5267. * We always enable DPLL0 with the lowest link rate possible, but still
  5268. * taking into account the VCO required to operate the eDP panel at the
  5269. * desired frequency. The usual DP link rates operate with a VCO of
  5270. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  5271. * The modeset code is responsible for the selection of the exact link
  5272. * rate later on, with the constraint of choosing a frequency that
  5273. * works with vco.
  5274. */
  5275. val = I915_READ(DPLL_CTRL1);
  5276. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  5277. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  5278. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  5279. if (vco == 8640000)
  5280. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  5281. SKL_DPLL0);
  5282. else
  5283. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  5284. SKL_DPLL0);
  5285. I915_WRITE(DPLL_CTRL1, val);
  5286. POSTING_READ(DPLL_CTRL1);
  5287. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  5288. if (intel_wait_for_register(dev_priv,
  5289. LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  5290. 5))
  5291. DRM_ERROR("DPLL0 not locked\n");
  5292. dev_priv->cdclk_pll.vco = vco;
  5293. /* We'll want to keep using the current vco from now on. */
  5294. skl_set_preferred_cdclk_vco(dev_priv, vco);
  5295. }
  5296. static void
  5297. skl_dpll0_disable(struct drm_i915_private *dev_priv)
  5298. {
  5299. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  5300. if (intel_wait_for_register(dev_priv,
  5301. LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
  5302. 1))
  5303. DRM_ERROR("Couldn't disable DPLL0\n");
  5304. dev_priv->cdclk_pll.vco = 0;
  5305. }
  5306. static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
  5307. {
  5308. u32 freq_select, pcu_ack;
  5309. int ret;
  5310. WARN_ON((cdclk == 24000) != (vco == 0));
  5311. DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
  5312. mutex_lock(&dev_priv->rps.hw_lock);
  5313. ret = skl_pcode_request(dev_priv, SKL_PCODE_CDCLK_CONTROL,
  5314. SKL_CDCLK_PREPARE_FOR_CHANGE,
  5315. SKL_CDCLK_READY_FOR_CHANGE,
  5316. SKL_CDCLK_READY_FOR_CHANGE, 3);
  5317. mutex_unlock(&dev_priv->rps.hw_lock);
  5318. if (ret) {
  5319. DRM_ERROR("Failed to inform PCU about cdclk change (%d)\n",
  5320. ret);
  5321. return;
  5322. }
  5323. /* set CDCLK_CTL */
  5324. switch (cdclk) {
  5325. case 450000:
  5326. case 432000:
  5327. freq_select = CDCLK_FREQ_450_432;
  5328. pcu_ack = 1;
  5329. break;
  5330. case 540000:
  5331. freq_select = CDCLK_FREQ_540;
  5332. pcu_ack = 2;
  5333. break;
  5334. case 308571:
  5335. case 337500:
  5336. default:
  5337. freq_select = CDCLK_FREQ_337_308;
  5338. pcu_ack = 0;
  5339. break;
  5340. case 617143:
  5341. case 675000:
  5342. freq_select = CDCLK_FREQ_675_617;
  5343. pcu_ack = 3;
  5344. break;
  5345. }
  5346. if (dev_priv->cdclk_pll.vco != 0 &&
  5347. dev_priv->cdclk_pll.vco != vco)
  5348. skl_dpll0_disable(dev_priv);
  5349. if (dev_priv->cdclk_pll.vco != vco)
  5350. skl_dpll0_enable(dev_priv, vco);
  5351. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
  5352. POSTING_READ(CDCLK_CTL);
  5353. /* inform PCU of the change */
  5354. mutex_lock(&dev_priv->rps.hw_lock);
  5355. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  5356. mutex_unlock(&dev_priv->rps.hw_lock);
  5357. intel_update_cdclk(dev_priv);
  5358. }
  5359. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
  5360. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  5361. {
  5362. skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
  5363. }
  5364. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  5365. {
  5366. int cdclk, vco;
  5367. skl_sanitize_cdclk(dev_priv);
  5368. if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
  5369. /*
  5370. * Use the current vco as our initial
  5371. * guess as to what the preferred vco is.
  5372. */
  5373. if (dev_priv->skl_preferred_vco_freq == 0)
  5374. skl_set_preferred_cdclk_vco(dev_priv,
  5375. dev_priv->cdclk_pll.vco);
  5376. return;
  5377. }
  5378. vco = dev_priv->skl_preferred_vco_freq;
  5379. if (vco == 0)
  5380. vco = 8100000;
  5381. cdclk = skl_calc_cdclk(0, vco);
  5382. skl_set_cdclk(dev_priv, cdclk, vco);
  5383. }
  5384. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
  5385. {
  5386. uint32_t cdctl, expected;
  5387. /*
  5388. * check if the pre-os intialized the display
  5389. * There is SWF18 scratchpad register defined which is set by the
  5390. * pre-os which can be used by the OS drivers to check the status
  5391. */
  5392. if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
  5393. goto sanitize;
  5394. intel_update_cdclk(dev_priv);
  5395. /* Is PLL enabled and locked ? */
  5396. if (dev_priv->cdclk_pll.vco == 0 ||
  5397. dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
  5398. goto sanitize;
  5399. /* DPLL okay; verify the cdclock
  5400. *
  5401. * Noticed in some instances that the freq selection is correct but
  5402. * decimal part is programmed wrong from BIOS where pre-os does not
  5403. * enable display. Verify the same as well.
  5404. */
  5405. cdctl = I915_READ(CDCLK_CTL);
  5406. expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
  5407. skl_cdclk_decimal(dev_priv->cdclk_freq);
  5408. if (cdctl == expected)
  5409. /* All well; nothing to sanitize */
  5410. return;
  5411. sanitize:
  5412. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  5413. /* force cdclk programming */
  5414. dev_priv->cdclk_freq = 0;
  5415. /* force full PLL disable + enable */
  5416. dev_priv->cdclk_pll.vco = -1;
  5417. }
  5418. /* Adjust CDclk dividers to allow high res or save power if possible */
  5419. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  5420. {
  5421. struct drm_i915_private *dev_priv = to_i915(dev);
  5422. u32 val, cmd;
  5423. WARN_ON(dev_priv->display.get_display_clock_speed(dev_priv)
  5424. != dev_priv->cdclk_freq);
  5425. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  5426. cmd = 2;
  5427. else if (cdclk == 266667)
  5428. cmd = 1;
  5429. else
  5430. cmd = 0;
  5431. mutex_lock(&dev_priv->rps.hw_lock);
  5432. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  5433. val &= ~DSPFREQGUAR_MASK;
  5434. val |= (cmd << DSPFREQGUAR_SHIFT);
  5435. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  5436. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  5437. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  5438. 50)) {
  5439. DRM_ERROR("timed out waiting for CDclk change\n");
  5440. }
  5441. mutex_unlock(&dev_priv->rps.hw_lock);
  5442. mutex_lock(&dev_priv->sb_lock);
  5443. if (cdclk == 400000) {
  5444. u32 divider;
  5445. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  5446. /* adjust cdclk divider */
  5447. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  5448. val &= ~CCK_FREQUENCY_VALUES;
  5449. val |= divider;
  5450. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  5451. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  5452. CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
  5453. 50))
  5454. DRM_ERROR("timed out waiting for CDclk change\n");
  5455. }
  5456. /* adjust self-refresh exit latency value */
  5457. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  5458. val &= ~0x7f;
  5459. /*
  5460. * For high bandwidth configs, we set a higher latency in the bunit
  5461. * so that the core display fetch happens in time to avoid underruns.
  5462. */
  5463. if (cdclk == 400000)
  5464. val |= 4500 / 250; /* 4.5 usec */
  5465. else
  5466. val |= 3000 / 250; /* 3.0 usec */
  5467. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  5468. mutex_unlock(&dev_priv->sb_lock);
  5469. intel_update_cdclk(dev_priv);
  5470. }
  5471. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  5472. {
  5473. struct drm_i915_private *dev_priv = to_i915(dev);
  5474. u32 val, cmd;
  5475. WARN_ON(dev_priv->display.get_display_clock_speed(dev_priv)
  5476. != dev_priv->cdclk_freq);
  5477. switch (cdclk) {
  5478. case 333333:
  5479. case 320000:
  5480. case 266667:
  5481. case 200000:
  5482. break;
  5483. default:
  5484. MISSING_CASE(cdclk);
  5485. return;
  5486. }
  5487. /*
  5488. * Specs are full of misinformation, but testing on actual
  5489. * hardware has shown that we just need to write the desired
  5490. * CCK divider into the Punit register.
  5491. */
  5492. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  5493. mutex_lock(&dev_priv->rps.hw_lock);
  5494. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  5495. val &= ~DSPFREQGUAR_MASK_CHV;
  5496. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  5497. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  5498. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  5499. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  5500. 50)) {
  5501. DRM_ERROR("timed out waiting for CDclk change\n");
  5502. }
  5503. mutex_unlock(&dev_priv->rps.hw_lock);
  5504. intel_update_cdclk(dev_priv);
  5505. }
  5506. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  5507. int max_pixclk)
  5508. {
  5509. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  5510. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  5511. /*
  5512. * Really only a few cases to deal with, as only 4 CDclks are supported:
  5513. * 200MHz
  5514. * 267MHz
  5515. * 320/333MHz (depends on HPLL freq)
  5516. * 400MHz (VLV only)
  5517. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  5518. * of the lower bin and adjust if needed.
  5519. *
  5520. * We seem to get an unstable or solid color picture at 200MHz.
  5521. * Not sure what's wrong. For now use 200MHz only when all pipes
  5522. * are off.
  5523. */
  5524. if (!IS_CHERRYVIEW(dev_priv) &&
  5525. max_pixclk > freq_320*limit/100)
  5526. return 400000;
  5527. else if (max_pixclk > 266667*limit/100)
  5528. return freq_320;
  5529. else if (max_pixclk > 0)
  5530. return 266667;
  5531. else
  5532. return 200000;
  5533. }
  5534. static int glk_calc_cdclk(int max_pixclk)
  5535. {
  5536. if (max_pixclk > 2 * 158400)
  5537. return 316800;
  5538. else if (max_pixclk > 2 * 79200)
  5539. return 158400;
  5540. else
  5541. return 79200;
  5542. }
  5543. static int bxt_calc_cdclk(int max_pixclk)
  5544. {
  5545. if (max_pixclk > 576000)
  5546. return 624000;
  5547. else if (max_pixclk > 384000)
  5548. return 576000;
  5549. else if (max_pixclk > 288000)
  5550. return 384000;
  5551. else if (max_pixclk > 144000)
  5552. return 288000;
  5553. else
  5554. return 144000;
  5555. }
  5556. /* Compute the max pixel clock for new configuration. */
  5557. static int intel_mode_max_pixclk(struct drm_device *dev,
  5558. struct drm_atomic_state *state)
  5559. {
  5560. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  5561. struct drm_i915_private *dev_priv = to_i915(dev);
  5562. struct drm_crtc *crtc;
  5563. struct drm_crtc_state *crtc_state;
  5564. unsigned max_pixclk = 0, i;
  5565. enum pipe pipe;
  5566. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  5567. sizeof(intel_state->min_pixclk));
  5568. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  5569. int pixclk = 0;
  5570. if (crtc_state->enable)
  5571. pixclk = crtc_state->adjusted_mode.crtc_clock;
  5572. intel_state->min_pixclk[i] = pixclk;
  5573. }
  5574. for_each_pipe(dev_priv, pipe)
  5575. max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
  5576. return max_pixclk;
  5577. }
  5578. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  5579. {
  5580. struct drm_device *dev = state->dev;
  5581. struct drm_i915_private *dev_priv = to_i915(dev);
  5582. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5583. struct intel_atomic_state *intel_state =
  5584. to_intel_atomic_state(state);
  5585. intel_state->cdclk = intel_state->dev_cdclk =
  5586. valleyview_calc_cdclk(dev_priv, max_pixclk);
  5587. if (!intel_state->active_crtcs)
  5588. intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
  5589. return 0;
  5590. }
  5591. static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
  5592. {
  5593. struct drm_i915_private *dev_priv = to_i915(state->dev);
  5594. int max_pixclk = ilk_max_pixel_rate(state);
  5595. struct intel_atomic_state *intel_state =
  5596. to_intel_atomic_state(state);
  5597. int cdclk;
  5598. if (IS_GEMINILAKE(dev_priv))
  5599. cdclk = glk_calc_cdclk(max_pixclk);
  5600. else
  5601. cdclk = bxt_calc_cdclk(max_pixclk);
  5602. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  5603. if (!intel_state->active_crtcs) {
  5604. if (IS_GEMINILAKE(dev_priv))
  5605. cdclk = glk_calc_cdclk(0);
  5606. else
  5607. cdclk = bxt_calc_cdclk(0);
  5608. intel_state->dev_cdclk = cdclk;
  5609. }
  5610. return 0;
  5611. }
  5612. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5613. {
  5614. unsigned int credits, default_credits;
  5615. if (IS_CHERRYVIEW(dev_priv))
  5616. default_credits = PFI_CREDIT(12);
  5617. else
  5618. default_credits = PFI_CREDIT(8);
  5619. if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
  5620. /* CHV suggested value is 31 or 63 */
  5621. if (IS_CHERRYVIEW(dev_priv))
  5622. credits = PFI_CREDIT_63;
  5623. else
  5624. credits = PFI_CREDIT(15);
  5625. } else {
  5626. credits = default_credits;
  5627. }
  5628. /*
  5629. * WA - write default credits before re-programming
  5630. * FIXME: should we also set the resend bit here?
  5631. */
  5632. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5633. default_credits);
  5634. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5635. credits | PFI_CREDIT_RESEND);
  5636. /*
  5637. * FIXME is this guaranteed to clear
  5638. * immediately or should we poll for it?
  5639. */
  5640. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5641. }
  5642. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5643. {
  5644. struct drm_device *dev = old_state->dev;
  5645. struct drm_i915_private *dev_priv = to_i915(dev);
  5646. struct intel_atomic_state *old_intel_state =
  5647. to_intel_atomic_state(old_state);
  5648. unsigned req_cdclk = old_intel_state->dev_cdclk;
  5649. /*
  5650. * FIXME: We can end up here with all power domains off, yet
  5651. * with a CDCLK frequency other than the minimum. To account
  5652. * for this take the PIPE-A power domain, which covers the HW
  5653. * blocks needed for the following programming. This can be
  5654. * removed once it's guaranteed that we get here either with
  5655. * the minimum CDCLK set, or the required power domains
  5656. * enabled.
  5657. */
  5658. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5659. if (IS_CHERRYVIEW(dev_priv))
  5660. cherryview_set_cdclk(dev, req_cdclk);
  5661. else
  5662. valleyview_set_cdclk(dev, req_cdclk);
  5663. vlv_program_pfi_credits(dev_priv);
  5664. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5665. }
  5666. static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
  5667. struct drm_atomic_state *old_state)
  5668. {
  5669. struct drm_crtc *crtc = pipe_config->base.crtc;
  5670. struct drm_device *dev = crtc->dev;
  5671. struct drm_i915_private *dev_priv = to_i915(dev);
  5672. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5673. int pipe = intel_crtc->pipe;
  5674. if (WARN_ON(intel_crtc->active))
  5675. return;
  5676. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5677. intel_dp_set_m_n(intel_crtc, M1_N1);
  5678. intel_set_pipe_timings(intel_crtc);
  5679. intel_set_pipe_src_size(intel_crtc);
  5680. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  5681. struct drm_i915_private *dev_priv = to_i915(dev);
  5682. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5683. I915_WRITE(CHV_CANVAS(pipe), 0);
  5684. }
  5685. i9xx_set_pipeconf(intel_crtc);
  5686. intel_crtc->active = true;
  5687. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5688. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  5689. if (IS_CHERRYVIEW(dev_priv)) {
  5690. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5691. chv_enable_pll(intel_crtc, intel_crtc->config);
  5692. } else {
  5693. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5694. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5695. }
  5696. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  5697. i9xx_pfit_enable(intel_crtc);
  5698. intel_color_load_luts(&pipe_config->base);
  5699. intel_update_watermarks(intel_crtc);
  5700. intel_enable_pipe(intel_crtc);
  5701. assert_vblank_disabled(crtc);
  5702. drm_crtc_vblank_on(crtc);
  5703. intel_encoders_enable(crtc, pipe_config, old_state);
  5704. }
  5705. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5706. {
  5707. struct drm_device *dev = crtc->base.dev;
  5708. struct drm_i915_private *dev_priv = to_i915(dev);
  5709. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5710. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5711. }
  5712. static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
  5713. struct drm_atomic_state *old_state)
  5714. {
  5715. struct drm_crtc *crtc = pipe_config->base.crtc;
  5716. struct drm_device *dev = crtc->dev;
  5717. struct drm_i915_private *dev_priv = to_i915(dev);
  5718. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5719. enum pipe pipe = intel_crtc->pipe;
  5720. if (WARN_ON(intel_crtc->active))
  5721. return;
  5722. i9xx_set_pll_dividers(intel_crtc);
  5723. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5724. intel_dp_set_m_n(intel_crtc, M1_N1);
  5725. intel_set_pipe_timings(intel_crtc);
  5726. intel_set_pipe_src_size(intel_crtc);
  5727. i9xx_set_pipeconf(intel_crtc);
  5728. intel_crtc->active = true;
  5729. if (!IS_GEN2(dev_priv))
  5730. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5731. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  5732. i9xx_enable_pll(intel_crtc);
  5733. i9xx_pfit_enable(intel_crtc);
  5734. intel_color_load_luts(&pipe_config->base);
  5735. intel_update_watermarks(intel_crtc);
  5736. intel_enable_pipe(intel_crtc);
  5737. assert_vblank_disabled(crtc);
  5738. drm_crtc_vblank_on(crtc);
  5739. intel_encoders_enable(crtc, pipe_config, old_state);
  5740. }
  5741. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5742. {
  5743. struct drm_device *dev = crtc->base.dev;
  5744. struct drm_i915_private *dev_priv = to_i915(dev);
  5745. if (!crtc->config->gmch_pfit.control)
  5746. return;
  5747. assert_pipe_disabled(dev_priv, crtc->pipe);
  5748. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5749. I915_READ(PFIT_CONTROL));
  5750. I915_WRITE(PFIT_CONTROL, 0);
  5751. }
  5752. static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
  5753. struct drm_atomic_state *old_state)
  5754. {
  5755. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  5756. struct drm_device *dev = crtc->dev;
  5757. struct drm_i915_private *dev_priv = to_i915(dev);
  5758. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5759. int pipe = intel_crtc->pipe;
  5760. /*
  5761. * On gen2 planes are double buffered but the pipe isn't, so we must
  5762. * wait for planes to fully turn off before disabling the pipe.
  5763. */
  5764. if (IS_GEN2(dev_priv))
  5765. intel_wait_for_vblank(dev_priv, pipe);
  5766. intel_encoders_disable(crtc, old_crtc_state, old_state);
  5767. drm_crtc_vblank_off(crtc);
  5768. assert_vblank_disabled(crtc);
  5769. intel_disable_pipe(intel_crtc);
  5770. i9xx_pfit_disable(intel_crtc);
  5771. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  5772. if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
  5773. if (IS_CHERRYVIEW(dev_priv))
  5774. chv_disable_pll(dev_priv, pipe);
  5775. else if (IS_VALLEYVIEW(dev_priv))
  5776. vlv_disable_pll(dev_priv, pipe);
  5777. else
  5778. i9xx_disable_pll(intel_crtc);
  5779. }
  5780. intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
  5781. if (!IS_GEN2(dev_priv))
  5782. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5783. }
  5784. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5785. {
  5786. struct intel_encoder *encoder;
  5787. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5788. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5789. enum intel_display_power_domain domain;
  5790. unsigned long domains;
  5791. struct drm_atomic_state *state;
  5792. struct intel_crtc_state *crtc_state;
  5793. int ret;
  5794. if (!intel_crtc->active)
  5795. return;
  5796. if (to_intel_plane_state(crtc->primary->state)->base.visible) {
  5797. WARN_ON(intel_crtc->flip_work);
  5798. intel_pre_disable_primary_noatomic(crtc);
  5799. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  5800. to_intel_plane_state(crtc->primary->state)->base.visible = false;
  5801. }
  5802. state = drm_atomic_state_alloc(crtc->dev);
  5803. state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
  5804. /* Everything's already locked, -EDEADLK can't happen. */
  5805. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  5806. ret = drm_atomic_add_affected_connectors(state, crtc);
  5807. WARN_ON(IS_ERR(crtc_state) || ret);
  5808. dev_priv->display.crtc_disable(crtc_state, state);
  5809. drm_atomic_state_put(state);
  5810. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  5811. crtc->base.id, crtc->name);
  5812. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  5813. crtc->state->active = false;
  5814. intel_crtc->active = false;
  5815. crtc->enabled = false;
  5816. crtc->state->connector_mask = 0;
  5817. crtc->state->encoder_mask = 0;
  5818. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  5819. encoder->base.crtc = NULL;
  5820. intel_fbc_disable(intel_crtc);
  5821. intel_update_watermarks(intel_crtc);
  5822. intel_disable_shared_dpll(intel_crtc);
  5823. domains = intel_crtc->enabled_power_domains;
  5824. for_each_power_domain(domain, domains)
  5825. intel_display_power_put(dev_priv, domain);
  5826. intel_crtc->enabled_power_domains = 0;
  5827. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5828. dev_priv->min_pixclk[intel_crtc->pipe] = 0;
  5829. }
  5830. /*
  5831. * turn all crtc's off, but do not adjust state
  5832. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5833. */
  5834. int intel_display_suspend(struct drm_device *dev)
  5835. {
  5836. struct drm_i915_private *dev_priv = to_i915(dev);
  5837. struct drm_atomic_state *state;
  5838. int ret;
  5839. state = drm_atomic_helper_suspend(dev);
  5840. ret = PTR_ERR_OR_ZERO(state);
  5841. if (ret)
  5842. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5843. else
  5844. dev_priv->modeset_restore_state = state;
  5845. return ret;
  5846. }
  5847. void intel_encoder_destroy(struct drm_encoder *encoder)
  5848. {
  5849. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5850. drm_encoder_cleanup(encoder);
  5851. kfree(intel_encoder);
  5852. }
  5853. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5854. * internal consistency). */
  5855. static void intel_connector_verify_state(struct intel_connector *connector)
  5856. {
  5857. struct drm_crtc *crtc = connector->base.state->crtc;
  5858. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5859. connector->base.base.id,
  5860. connector->base.name);
  5861. if (connector->get_hw_state(connector)) {
  5862. struct intel_encoder *encoder = connector->encoder;
  5863. struct drm_connector_state *conn_state = connector->base.state;
  5864. I915_STATE_WARN(!crtc,
  5865. "connector enabled without attached crtc\n");
  5866. if (!crtc)
  5867. return;
  5868. I915_STATE_WARN(!crtc->state->active,
  5869. "connector is active, but attached crtc isn't\n");
  5870. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5871. return;
  5872. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5873. "atomic encoder doesn't match attached encoder\n");
  5874. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5875. "attached encoder crtc differs from connector crtc\n");
  5876. } else {
  5877. I915_STATE_WARN(crtc && crtc->state->active,
  5878. "attached crtc is active, but connector isn't\n");
  5879. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5880. "best encoder set without crtc!\n");
  5881. }
  5882. }
  5883. int intel_connector_init(struct intel_connector *connector)
  5884. {
  5885. drm_atomic_helper_connector_reset(&connector->base);
  5886. if (!connector->base.state)
  5887. return -ENOMEM;
  5888. return 0;
  5889. }
  5890. struct intel_connector *intel_connector_alloc(void)
  5891. {
  5892. struct intel_connector *connector;
  5893. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5894. if (!connector)
  5895. return NULL;
  5896. if (intel_connector_init(connector) < 0) {
  5897. kfree(connector);
  5898. return NULL;
  5899. }
  5900. return connector;
  5901. }
  5902. /* Simple connector->get_hw_state implementation for encoders that support only
  5903. * one connector and no cloning and hence the encoder state determines the state
  5904. * of the connector. */
  5905. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5906. {
  5907. enum pipe pipe = 0;
  5908. struct intel_encoder *encoder = connector->encoder;
  5909. return encoder->get_hw_state(encoder, &pipe);
  5910. }
  5911. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5912. {
  5913. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5914. return crtc_state->fdi_lanes;
  5915. return 0;
  5916. }
  5917. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5918. struct intel_crtc_state *pipe_config)
  5919. {
  5920. struct drm_i915_private *dev_priv = to_i915(dev);
  5921. struct drm_atomic_state *state = pipe_config->base.state;
  5922. struct intel_crtc *other_crtc;
  5923. struct intel_crtc_state *other_crtc_state;
  5924. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5925. pipe_name(pipe), pipe_config->fdi_lanes);
  5926. if (pipe_config->fdi_lanes > 4) {
  5927. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5928. pipe_name(pipe), pipe_config->fdi_lanes);
  5929. return -EINVAL;
  5930. }
  5931. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  5932. if (pipe_config->fdi_lanes > 2) {
  5933. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5934. pipe_config->fdi_lanes);
  5935. return -EINVAL;
  5936. } else {
  5937. return 0;
  5938. }
  5939. }
  5940. if (INTEL_INFO(dev_priv)->num_pipes == 2)
  5941. return 0;
  5942. /* Ivybridge 3 pipe is really complicated */
  5943. switch (pipe) {
  5944. case PIPE_A:
  5945. return 0;
  5946. case PIPE_B:
  5947. if (pipe_config->fdi_lanes <= 2)
  5948. return 0;
  5949. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
  5950. other_crtc_state =
  5951. intel_atomic_get_crtc_state(state, other_crtc);
  5952. if (IS_ERR(other_crtc_state))
  5953. return PTR_ERR(other_crtc_state);
  5954. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5955. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5956. pipe_name(pipe), pipe_config->fdi_lanes);
  5957. return -EINVAL;
  5958. }
  5959. return 0;
  5960. case PIPE_C:
  5961. if (pipe_config->fdi_lanes > 2) {
  5962. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5963. pipe_name(pipe), pipe_config->fdi_lanes);
  5964. return -EINVAL;
  5965. }
  5966. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
  5967. other_crtc_state =
  5968. intel_atomic_get_crtc_state(state, other_crtc);
  5969. if (IS_ERR(other_crtc_state))
  5970. return PTR_ERR(other_crtc_state);
  5971. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5972. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5973. return -EINVAL;
  5974. }
  5975. return 0;
  5976. default:
  5977. BUG();
  5978. }
  5979. }
  5980. #define RETRY 1
  5981. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5982. struct intel_crtc_state *pipe_config)
  5983. {
  5984. struct drm_device *dev = intel_crtc->base.dev;
  5985. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5986. int lane, link_bw, fdi_dotclock, ret;
  5987. bool needs_recompute = false;
  5988. retry:
  5989. /* FDI is a binary signal running at ~2.7GHz, encoding
  5990. * each output octet as 10 bits. The actual frequency
  5991. * is stored as a divider into a 100MHz clock, and the
  5992. * mode pixel clock is stored in units of 1KHz.
  5993. * Hence the bw of each lane in terms of the mode signal
  5994. * is:
  5995. */
  5996. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5997. fdi_dotclock = adjusted_mode->crtc_clock;
  5998. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5999. pipe_config->pipe_bpp);
  6000. pipe_config->fdi_lanes = lane;
  6001. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  6002. link_bw, &pipe_config->fdi_m_n);
  6003. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  6004. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  6005. pipe_config->pipe_bpp -= 2*3;
  6006. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  6007. pipe_config->pipe_bpp);
  6008. needs_recompute = true;
  6009. pipe_config->bw_constrained = true;
  6010. goto retry;
  6011. }
  6012. if (needs_recompute)
  6013. return RETRY;
  6014. return ret;
  6015. }
  6016. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  6017. struct intel_crtc_state *pipe_config)
  6018. {
  6019. if (pipe_config->pipe_bpp > 24)
  6020. return false;
  6021. /* HSW can handle pixel rate up to cdclk? */
  6022. if (IS_HASWELL(dev_priv))
  6023. return true;
  6024. /*
  6025. * We compare against max which means we must take
  6026. * the increased cdclk requirement into account when
  6027. * calculating the new cdclk.
  6028. *
  6029. * Should measure whether using a lower cdclk w/o IPS
  6030. */
  6031. return ilk_pipe_pixel_rate(pipe_config) <=
  6032. dev_priv->max_cdclk_freq * 95 / 100;
  6033. }
  6034. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  6035. struct intel_crtc_state *pipe_config)
  6036. {
  6037. struct drm_device *dev = crtc->base.dev;
  6038. struct drm_i915_private *dev_priv = to_i915(dev);
  6039. pipe_config->ips_enabled = i915.enable_ips &&
  6040. hsw_crtc_supports_ips(crtc) &&
  6041. pipe_config_supports_ips(dev_priv, pipe_config);
  6042. }
  6043. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  6044. {
  6045. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6046. /* GDG double wide on either pipe, otherwise pipe A only */
  6047. return INTEL_INFO(dev_priv)->gen < 4 &&
  6048. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  6049. }
  6050. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  6051. struct intel_crtc_state *pipe_config)
  6052. {
  6053. struct drm_device *dev = crtc->base.dev;
  6054. struct drm_i915_private *dev_priv = to_i915(dev);
  6055. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  6056. int clock_limit = dev_priv->max_dotclk_freq;
  6057. if (INTEL_GEN(dev_priv) < 4) {
  6058. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  6059. /*
  6060. * Enable double wide mode when the dot clock
  6061. * is > 90% of the (display) core speed.
  6062. */
  6063. if (intel_crtc_supports_double_wide(crtc) &&
  6064. adjusted_mode->crtc_clock > clock_limit) {
  6065. clock_limit = dev_priv->max_dotclk_freq;
  6066. pipe_config->double_wide = true;
  6067. }
  6068. }
  6069. if (adjusted_mode->crtc_clock > clock_limit) {
  6070. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  6071. adjusted_mode->crtc_clock, clock_limit,
  6072. yesno(pipe_config->double_wide));
  6073. return -EINVAL;
  6074. }
  6075. /*
  6076. * Pipe horizontal size must be even in:
  6077. * - DVO ganged mode
  6078. * - LVDS dual channel mode
  6079. * - Double wide pipe
  6080. */
  6081. if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  6082. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  6083. pipe_config->pipe_src_w &= ~1;
  6084. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  6085. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  6086. */
  6087. if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
  6088. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  6089. return -EINVAL;
  6090. if (HAS_IPS(dev_priv))
  6091. hsw_compute_ips_config(crtc, pipe_config);
  6092. if (pipe_config->has_pch_encoder)
  6093. return ironlake_fdi_compute_config(crtc, pipe_config);
  6094. return 0;
  6095. }
  6096. static int skylake_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6097. {
  6098. u32 cdctl;
  6099. skl_dpll0_update(dev_priv);
  6100. if (dev_priv->cdclk_pll.vco == 0)
  6101. return dev_priv->cdclk_pll.ref;
  6102. cdctl = I915_READ(CDCLK_CTL);
  6103. if (dev_priv->cdclk_pll.vco == 8640000) {
  6104. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  6105. case CDCLK_FREQ_450_432:
  6106. return 432000;
  6107. case CDCLK_FREQ_337_308:
  6108. return 308571;
  6109. case CDCLK_FREQ_540:
  6110. return 540000;
  6111. case CDCLK_FREQ_675_617:
  6112. return 617143;
  6113. default:
  6114. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  6115. }
  6116. } else {
  6117. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  6118. case CDCLK_FREQ_450_432:
  6119. return 450000;
  6120. case CDCLK_FREQ_337_308:
  6121. return 337500;
  6122. case CDCLK_FREQ_540:
  6123. return 540000;
  6124. case CDCLK_FREQ_675_617:
  6125. return 675000;
  6126. default:
  6127. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  6128. }
  6129. }
  6130. return dev_priv->cdclk_pll.ref;
  6131. }
  6132. static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
  6133. {
  6134. u32 val;
  6135. dev_priv->cdclk_pll.ref = 19200;
  6136. dev_priv->cdclk_pll.vco = 0;
  6137. val = I915_READ(BXT_DE_PLL_ENABLE);
  6138. if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
  6139. return;
  6140. if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
  6141. return;
  6142. val = I915_READ(BXT_DE_PLL_CTL);
  6143. dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
  6144. dev_priv->cdclk_pll.ref;
  6145. }
  6146. static int broxton_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6147. {
  6148. u32 divider;
  6149. int div, vco;
  6150. bxt_de_pll_update(dev_priv);
  6151. vco = dev_priv->cdclk_pll.vco;
  6152. if (vco == 0)
  6153. return dev_priv->cdclk_pll.ref;
  6154. divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
  6155. switch (divider) {
  6156. case BXT_CDCLK_CD2X_DIV_SEL_1:
  6157. div = 2;
  6158. break;
  6159. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  6160. WARN(IS_GEMINILAKE(dev_priv), "Unsupported divider\n");
  6161. div = 3;
  6162. break;
  6163. case BXT_CDCLK_CD2X_DIV_SEL_2:
  6164. div = 4;
  6165. break;
  6166. case BXT_CDCLK_CD2X_DIV_SEL_4:
  6167. div = 8;
  6168. break;
  6169. default:
  6170. MISSING_CASE(divider);
  6171. return dev_priv->cdclk_pll.ref;
  6172. }
  6173. return DIV_ROUND_CLOSEST(vco, div);
  6174. }
  6175. static int broadwell_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6176. {
  6177. uint32_t lcpll = I915_READ(LCPLL_CTL);
  6178. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  6179. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  6180. return 800000;
  6181. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  6182. return 450000;
  6183. else if (freq == LCPLL_CLK_FREQ_450)
  6184. return 450000;
  6185. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  6186. return 540000;
  6187. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  6188. return 337500;
  6189. else
  6190. return 675000;
  6191. }
  6192. static int haswell_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6193. {
  6194. uint32_t lcpll = I915_READ(LCPLL_CTL);
  6195. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  6196. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  6197. return 800000;
  6198. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  6199. return 450000;
  6200. else if (freq == LCPLL_CLK_FREQ_450)
  6201. return 450000;
  6202. else if (IS_HSW_ULT(dev_priv))
  6203. return 337500;
  6204. else
  6205. return 540000;
  6206. }
  6207. static int valleyview_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6208. {
  6209. return vlv_get_cck_clock_hpll(dev_priv, "cdclk",
  6210. CCK_DISPLAY_CLOCK_CONTROL);
  6211. }
  6212. static int ilk_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6213. {
  6214. return 450000;
  6215. }
  6216. static int i945_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6217. {
  6218. return 400000;
  6219. }
  6220. static int i915_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6221. {
  6222. return 333333;
  6223. }
  6224. static int i9xx_misc_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6225. {
  6226. return 200000;
  6227. }
  6228. static int pnv_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6229. {
  6230. struct pci_dev *pdev = dev_priv->drm.pdev;
  6231. u16 gcfgc = 0;
  6232. pci_read_config_word(pdev, GCFGC, &gcfgc);
  6233. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  6234. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  6235. return 266667;
  6236. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  6237. return 333333;
  6238. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  6239. return 444444;
  6240. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  6241. return 200000;
  6242. default:
  6243. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  6244. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  6245. return 133333;
  6246. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  6247. return 166667;
  6248. }
  6249. }
  6250. static int i915gm_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6251. {
  6252. struct pci_dev *pdev = dev_priv->drm.pdev;
  6253. u16 gcfgc = 0;
  6254. pci_read_config_word(pdev, GCFGC, &gcfgc);
  6255. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  6256. return 133333;
  6257. else {
  6258. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  6259. case GC_DISPLAY_CLOCK_333_MHZ:
  6260. return 333333;
  6261. default:
  6262. case GC_DISPLAY_CLOCK_190_200_MHZ:
  6263. return 190000;
  6264. }
  6265. }
  6266. }
  6267. static int i865_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6268. {
  6269. return 266667;
  6270. }
  6271. static int i85x_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6272. {
  6273. struct pci_dev *pdev = dev_priv->drm.pdev;
  6274. u16 hpllcc = 0;
  6275. /*
  6276. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  6277. * encoding is different :(
  6278. * FIXME is this the right way to detect 852GM/852GMV?
  6279. */
  6280. if (pdev->revision == 0x1)
  6281. return 133333;
  6282. pci_bus_read_config_word(pdev->bus,
  6283. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  6284. /* Assume that the hardware is in the high speed state. This
  6285. * should be the default.
  6286. */
  6287. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  6288. case GC_CLOCK_133_200:
  6289. case GC_CLOCK_133_200_2:
  6290. case GC_CLOCK_100_200:
  6291. return 200000;
  6292. case GC_CLOCK_166_250:
  6293. return 250000;
  6294. case GC_CLOCK_100_133:
  6295. return 133333;
  6296. case GC_CLOCK_133_266:
  6297. case GC_CLOCK_133_266_2:
  6298. case GC_CLOCK_166_266:
  6299. return 266667;
  6300. }
  6301. /* Shouldn't happen */
  6302. return 0;
  6303. }
  6304. static int i830_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6305. {
  6306. return 133333;
  6307. }
  6308. static unsigned int intel_hpll_vco(struct drm_i915_private *dev_priv)
  6309. {
  6310. static const unsigned int blb_vco[8] = {
  6311. [0] = 3200000,
  6312. [1] = 4000000,
  6313. [2] = 5333333,
  6314. [3] = 4800000,
  6315. [4] = 6400000,
  6316. };
  6317. static const unsigned int pnv_vco[8] = {
  6318. [0] = 3200000,
  6319. [1] = 4000000,
  6320. [2] = 5333333,
  6321. [3] = 4800000,
  6322. [4] = 2666667,
  6323. };
  6324. static const unsigned int cl_vco[8] = {
  6325. [0] = 3200000,
  6326. [1] = 4000000,
  6327. [2] = 5333333,
  6328. [3] = 6400000,
  6329. [4] = 3333333,
  6330. [5] = 3566667,
  6331. [6] = 4266667,
  6332. };
  6333. static const unsigned int elk_vco[8] = {
  6334. [0] = 3200000,
  6335. [1] = 4000000,
  6336. [2] = 5333333,
  6337. [3] = 4800000,
  6338. };
  6339. static const unsigned int ctg_vco[8] = {
  6340. [0] = 3200000,
  6341. [1] = 4000000,
  6342. [2] = 5333333,
  6343. [3] = 6400000,
  6344. [4] = 2666667,
  6345. [5] = 4266667,
  6346. };
  6347. const unsigned int *vco_table;
  6348. unsigned int vco;
  6349. uint8_t tmp = 0;
  6350. /* FIXME other chipsets? */
  6351. if (IS_GM45(dev_priv))
  6352. vco_table = ctg_vco;
  6353. else if (IS_G4X(dev_priv))
  6354. vco_table = elk_vco;
  6355. else if (IS_I965GM(dev_priv))
  6356. vco_table = cl_vco;
  6357. else if (IS_PINEVIEW(dev_priv))
  6358. vco_table = pnv_vco;
  6359. else if (IS_G33(dev_priv))
  6360. vco_table = blb_vco;
  6361. else
  6362. return 0;
  6363. tmp = I915_READ(IS_MOBILE(dev_priv) ? HPLLVCO_MOBILE : HPLLVCO);
  6364. vco = vco_table[tmp & 0x7];
  6365. if (vco == 0)
  6366. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  6367. else
  6368. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  6369. return vco;
  6370. }
  6371. static int gm45_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6372. {
  6373. struct pci_dev *pdev = dev_priv->drm.pdev;
  6374. unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
  6375. uint16_t tmp = 0;
  6376. pci_read_config_word(pdev, GCFGC, &tmp);
  6377. cdclk_sel = (tmp >> 12) & 0x1;
  6378. switch (vco) {
  6379. case 2666667:
  6380. case 4000000:
  6381. case 5333333:
  6382. return cdclk_sel ? 333333 : 222222;
  6383. case 3200000:
  6384. return cdclk_sel ? 320000 : 228571;
  6385. default:
  6386. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  6387. return 222222;
  6388. }
  6389. }
  6390. static int i965gm_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6391. {
  6392. struct pci_dev *pdev = dev_priv->drm.pdev;
  6393. static const uint8_t div_3200[] = { 16, 10, 8 };
  6394. static const uint8_t div_4000[] = { 20, 12, 10 };
  6395. static const uint8_t div_5333[] = { 24, 16, 14 };
  6396. const uint8_t *div_table;
  6397. unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
  6398. uint16_t tmp = 0;
  6399. pci_read_config_word(pdev, GCFGC, &tmp);
  6400. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  6401. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  6402. goto fail;
  6403. switch (vco) {
  6404. case 3200000:
  6405. div_table = div_3200;
  6406. break;
  6407. case 4000000:
  6408. div_table = div_4000;
  6409. break;
  6410. case 5333333:
  6411. div_table = div_5333;
  6412. break;
  6413. default:
  6414. goto fail;
  6415. }
  6416. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  6417. fail:
  6418. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  6419. return 200000;
  6420. }
  6421. static int g33_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6422. {
  6423. struct pci_dev *pdev = dev_priv->drm.pdev;
  6424. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  6425. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  6426. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  6427. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  6428. const uint8_t *div_table;
  6429. unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
  6430. uint16_t tmp = 0;
  6431. pci_read_config_word(pdev, GCFGC, &tmp);
  6432. cdclk_sel = (tmp >> 4) & 0x7;
  6433. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  6434. goto fail;
  6435. switch (vco) {
  6436. case 3200000:
  6437. div_table = div_3200;
  6438. break;
  6439. case 4000000:
  6440. div_table = div_4000;
  6441. break;
  6442. case 4800000:
  6443. div_table = div_4800;
  6444. break;
  6445. case 5333333:
  6446. div_table = div_5333;
  6447. break;
  6448. default:
  6449. goto fail;
  6450. }
  6451. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  6452. fail:
  6453. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  6454. return 190476;
  6455. }
  6456. static void
  6457. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  6458. {
  6459. while (*num > DATA_LINK_M_N_MASK ||
  6460. *den > DATA_LINK_M_N_MASK) {
  6461. *num >>= 1;
  6462. *den >>= 1;
  6463. }
  6464. }
  6465. static void compute_m_n(unsigned int m, unsigned int n,
  6466. uint32_t *ret_m, uint32_t *ret_n)
  6467. {
  6468. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  6469. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  6470. intel_reduce_m_n_ratio(ret_m, ret_n);
  6471. }
  6472. void
  6473. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  6474. int pixel_clock, int link_clock,
  6475. struct intel_link_m_n *m_n)
  6476. {
  6477. m_n->tu = 64;
  6478. compute_m_n(bits_per_pixel * pixel_clock,
  6479. link_clock * nlanes * 8,
  6480. &m_n->gmch_m, &m_n->gmch_n);
  6481. compute_m_n(pixel_clock, link_clock,
  6482. &m_n->link_m, &m_n->link_n);
  6483. }
  6484. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  6485. {
  6486. if (i915.panel_use_ssc >= 0)
  6487. return i915.panel_use_ssc != 0;
  6488. return dev_priv->vbt.lvds_use_ssc
  6489. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  6490. }
  6491. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  6492. {
  6493. return (1 << dpll->n) << 16 | dpll->m2;
  6494. }
  6495. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  6496. {
  6497. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  6498. }
  6499. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  6500. struct intel_crtc_state *crtc_state,
  6501. struct dpll *reduced_clock)
  6502. {
  6503. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6504. u32 fp, fp2 = 0;
  6505. if (IS_PINEVIEW(dev_priv)) {
  6506. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  6507. if (reduced_clock)
  6508. fp2 = pnv_dpll_compute_fp(reduced_clock);
  6509. } else {
  6510. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6511. if (reduced_clock)
  6512. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6513. }
  6514. crtc_state->dpll_hw_state.fp0 = fp;
  6515. crtc->lowfreq_avail = false;
  6516. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6517. reduced_clock) {
  6518. crtc_state->dpll_hw_state.fp1 = fp2;
  6519. crtc->lowfreq_avail = true;
  6520. } else {
  6521. crtc_state->dpll_hw_state.fp1 = fp;
  6522. }
  6523. }
  6524. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  6525. pipe)
  6526. {
  6527. u32 reg_val;
  6528. /*
  6529. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  6530. * and set it to a reasonable value instead.
  6531. */
  6532. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6533. reg_val &= 0xffffff00;
  6534. reg_val |= 0x00000030;
  6535. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6536. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6537. reg_val &= 0x8cffffff;
  6538. reg_val = 0x8c000000;
  6539. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6540. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6541. reg_val &= 0xffffff00;
  6542. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6543. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6544. reg_val &= 0x00ffffff;
  6545. reg_val |= 0xb0000000;
  6546. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6547. }
  6548. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  6549. struct intel_link_m_n *m_n)
  6550. {
  6551. struct drm_device *dev = crtc->base.dev;
  6552. struct drm_i915_private *dev_priv = to_i915(dev);
  6553. int pipe = crtc->pipe;
  6554. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6555. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  6556. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  6557. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  6558. }
  6559. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6560. struct intel_link_m_n *m_n,
  6561. struct intel_link_m_n *m2_n2)
  6562. {
  6563. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6564. int pipe = crtc->pipe;
  6565. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6566. if (INTEL_GEN(dev_priv) >= 5) {
  6567. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6568. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6569. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6570. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6571. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6572. * for gen < 8) and if DRRS is supported (to make sure the
  6573. * registers are not unnecessarily accessed).
  6574. */
  6575. if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
  6576. INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
  6577. I915_WRITE(PIPE_DATA_M2(transcoder),
  6578. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6579. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6580. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6581. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6582. }
  6583. } else {
  6584. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6585. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6586. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6587. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6588. }
  6589. }
  6590. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6591. {
  6592. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6593. if (m_n == M1_N1) {
  6594. dp_m_n = &crtc->config->dp_m_n;
  6595. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6596. } else if (m_n == M2_N2) {
  6597. /*
  6598. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6599. * needs to be programmed into M1_N1.
  6600. */
  6601. dp_m_n = &crtc->config->dp_m2_n2;
  6602. } else {
  6603. DRM_ERROR("Unsupported divider value\n");
  6604. return;
  6605. }
  6606. if (crtc->config->has_pch_encoder)
  6607. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6608. else
  6609. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6610. }
  6611. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6612. struct intel_crtc_state *pipe_config)
  6613. {
  6614. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  6615. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6616. if (crtc->pipe != PIPE_A)
  6617. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6618. /* DPLL not used with DSI, but still need the rest set up */
  6619. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  6620. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  6621. DPLL_EXT_BUFFER_ENABLE_VLV;
  6622. pipe_config->dpll_hw_state.dpll_md =
  6623. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6624. }
  6625. static void chv_compute_dpll(struct intel_crtc *crtc,
  6626. struct intel_crtc_state *pipe_config)
  6627. {
  6628. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  6629. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6630. if (crtc->pipe != PIPE_A)
  6631. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6632. /* DPLL not used with DSI, but still need the rest set up */
  6633. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  6634. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  6635. pipe_config->dpll_hw_state.dpll_md =
  6636. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6637. }
  6638. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6639. const struct intel_crtc_state *pipe_config)
  6640. {
  6641. struct drm_device *dev = crtc->base.dev;
  6642. struct drm_i915_private *dev_priv = to_i915(dev);
  6643. enum pipe pipe = crtc->pipe;
  6644. u32 mdiv;
  6645. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6646. u32 coreclk, reg_val;
  6647. /* Enable Refclk */
  6648. I915_WRITE(DPLL(pipe),
  6649. pipe_config->dpll_hw_state.dpll &
  6650. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  6651. /* No need to actually set up the DPLL with DSI */
  6652. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6653. return;
  6654. mutex_lock(&dev_priv->sb_lock);
  6655. bestn = pipe_config->dpll.n;
  6656. bestm1 = pipe_config->dpll.m1;
  6657. bestm2 = pipe_config->dpll.m2;
  6658. bestp1 = pipe_config->dpll.p1;
  6659. bestp2 = pipe_config->dpll.p2;
  6660. /* See eDP HDMI DPIO driver vbios notes doc */
  6661. /* PLL B needs special handling */
  6662. if (pipe == PIPE_B)
  6663. vlv_pllb_recal_opamp(dev_priv, pipe);
  6664. /* Set up Tx target for periodic Rcomp update */
  6665. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6666. /* Disable target IRef on PLL */
  6667. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6668. reg_val &= 0x00ffffff;
  6669. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6670. /* Disable fast lock */
  6671. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6672. /* Set idtafcrecal before PLL is enabled */
  6673. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6674. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6675. mdiv |= ((bestn << DPIO_N_SHIFT));
  6676. mdiv |= (1 << DPIO_K_SHIFT);
  6677. /*
  6678. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6679. * but we don't support that).
  6680. * Note: don't use the DAC post divider as it seems unstable.
  6681. */
  6682. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6683. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6684. mdiv |= DPIO_ENABLE_CALIBRATION;
  6685. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6686. /* Set HBR and RBR LPF coefficients */
  6687. if (pipe_config->port_clock == 162000 ||
  6688. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
  6689. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
  6690. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6691. 0x009f0003);
  6692. else
  6693. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6694. 0x00d0000f);
  6695. if (intel_crtc_has_dp_encoder(pipe_config)) {
  6696. /* Use SSC source */
  6697. if (pipe == PIPE_A)
  6698. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6699. 0x0df40000);
  6700. else
  6701. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6702. 0x0df70000);
  6703. } else { /* HDMI or VGA */
  6704. /* Use bend source */
  6705. if (pipe == PIPE_A)
  6706. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6707. 0x0df70000);
  6708. else
  6709. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6710. 0x0df40000);
  6711. }
  6712. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6713. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6714. if (intel_crtc_has_dp_encoder(crtc->config))
  6715. coreclk |= 0x01000000;
  6716. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6717. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6718. mutex_unlock(&dev_priv->sb_lock);
  6719. }
  6720. static void chv_prepare_pll(struct intel_crtc *crtc,
  6721. const struct intel_crtc_state *pipe_config)
  6722. {
  6723. struct drm_device *dev = crtc->base.dev;
  6724. struct drm_i915_private *dev_priv = to_i915(dev);
  6725. enum pipe pipe = crtc->pipe;
  6726. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6727. u32 loopfilter, tribuf_calcntr;
  6728. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6729. u32 dpio_val;
  6730. int vco;
  6731. /* Enable Refclk and SSC */
  6732. I915_WRITE(DPLL(pipe),
  6733. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6734. /* No need to actually set up the DPLL with DSI */
  6735. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6736. return;
  6737. bestn = pipe_config->dpll.n;
  6738. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6739. bestm1 = pipe_config->dpll.m1;
  6740. bestm2 = pipe_config->dpll.m2 >> 22;
  6741. bestp1 = pipe_config->dpll.p1;
  6742. bestp2 = pipe_config->dpll.p2;
  6743. vco = pipe_config->dpll.vco;
  6744. dpio_val = 0;
  6745. loopfilter = 0;
  6746. mutex_lock(&dev_priv->sb_lock);
  6747. /* p1 and p2 divider */
  6748. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6749. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6750. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6751. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6752. 1 << DPIO_CHV_K_DIV_SHIFT);
  6753. /* Feedback post-divider - m2 */
  6754. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6755. /* Feedback refclk divider - n and m1 */
  6756. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6757. DPIO_CHV_M1_DIV_BY_2 |
  6758. 1 << DPIO_CHV_N_DIV_SHIFT);
  6759. /* M2 fraction division */
  6760. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6761. /* M2 fraction division enable */
  6762. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6763. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6764. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6765. if (bestm2_frac)
  6766. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6767. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6768. /* Program digital lock detect threshold */
  6769. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6770. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6771. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6772. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6773. if (!bestm2_frac)
  6774. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6775. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6776. /* Loop filter */
  6777. if (vco == 5400000) {
  6778. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6779. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6780. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6781. tribuf_calcntr = 0x9;
  6782. } else if (vco <= 6200000) {
  6783. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6784. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6785. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6786. tribuf_calcntr = 0x9;
  6787. } else if (vco <= 6480000) {
  6788. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6789. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6790. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6791. tribuf_calcntr = 0x8;
  6792. } else {
  6793. /* Not supported. Apply the same limits as in the max case */
  6794. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6795. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6796. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6797. tribuf_calcntr = 0;
  6798. }
  6799. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6800. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6801. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6802. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6803. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6804. /* AFC Recal */
  6805. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6806. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6807. DPIO_AFC_RECAL);
  6808. mutex_unlock(&dev_priv->sb_lock);
  6809. }
  6810. /**
  6811. * vlv_force_pll_on - forcibly enable just the PLL
  6812. * @dev_priv: i915 private structure
  6813. * @pipe: pipe PLL to enable
  6814. * @dpll: PLL configuration
  6815. *
  6816. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6817. * in cases where we need the PLL enabled even when @pipe is not going to
  6818. * be enabled.
  6819. */
  6820. int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
  6821. const struct dpll *dpll)
  6822. {
  6823. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  6824. struct intel_crtc_state *pipe_config;
  6825. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  6826. if (!pipe_config)
  6827. return -ENOMEM;
  6828. pipe_config->base.crtc = &crtc->base;
  6829. pipe_config->pixel_multiplier = 1;
  6830. pipe_config->dpll = *dpll;
  6831. if (IS_CHERRYVIEW(dev_priv)) {
  6832. chv_compute_dpll(crtc, pipe_config);
  6833. chv_prepare_pll(crtc, pipe_config);
  6834. chv_enable_pll(crtc, pipe_config);
  6835. } else {
  6836. vlv_compute_dpll(crtc, pipe_config);
  6837. vlv_prepare_pll(crtc, pipe_config);
  6838. vlv_enable_pll(crtc, pipe_config);
  6839. }
  6840. kfree(pipe_config);
  6841. return 0;
  6842. }
  6843. /**
  6844. * vlv_force_pll_off - forcibly disable just the PLL
  6845. * @dev_priv: i915 private structure
  6846. * @pipe: pipe PLL to disable
  6847. *
  6848. * Disable the PLL for @pipe. To be used in cases where we need
  6849. * the PLL enabled even when @pipe is not going to be enabled.
  6850. */
  6851. void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
  6852. {
  6853. if (IS_CHERRYVIEW(dev_priv))
  6854. chv_disable_pll(dev_priv, pipe);
  6855. else
  6856. vlv_disable_pll(dev_priv, pipe);
  6857. }
  6858. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6859. struct intel_crtc_state *crtc_state,
  6860. struct dpll *reduced_clock)
  6861. {
  6862. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6863. u32 dpll;
  6864. struct dpll *clock = &crtc_state->dpll;
  6865. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6866. dpll = DPLL_VGA_MODE_DIS;
  6867. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  6868. dpll |= DPLLB_MODE_LVDS;
  6869. else
  6870. dpll |= DPLLB_MODE_DAC_SERIAL;
  6871. if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  6872. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  6873. dpll |= (crtc_state->pixel_multiplier - 1)
  6874. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6875. }
  6876. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6877. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  6878. dpll |= DPLL_SDVO_HIGH_SPEED;
  6879. if (intel_crtc_has_dp_encoder(crtc_state))
  6880. dpll |= DPLL_SDVO_HIGH_SPEED;
  6881. /* compute bitmask from p1 value */
  6882. if (IS_PINEVIEW(dev_priv))
  6883. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6884. else {
  6885. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6886. if (IS_G4X(dev_priv) && reduced_clock)
  6887. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6888. }
  6889. switch (clock->p2) {
  6890. case 5:
  6891. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6892. break;
  6893. case 7:
  6894. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6895. break;
  6896. case 10:
  6897. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6898. break;
  6899. case 14:
  6900. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6901. break;
  6902. }
  6903. if (INTEL_GEN(dev_priv) >= 4)
  6904. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6905. if (crtc_state->sdvo_tv_clock)
  6906. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6907. else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6908. intel_panel_use_ssc(dev_priv))
  6909. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6910. else
  6911. dpll |= PLL_REF_INPUT_DREFCLK;
  6912. dpll |= DPLL_VCO_ENABLE;
  6913. crtc_state->dpll_hw_state.dpll = dpll;
  6914. if (INTEL_GEN(dev_priv) >= 4) {
  6915. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6916. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6917. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6918. }
  6919. }
  6920. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6921. struct intel_crtc_state *crtc_state,
  6922. struct dpll *reduced_clock)
  6923. {
  6924. struct drm_device *dev = crtc->base.dev;
  6925. struct drm_i915_private *dev_priv = to_i915(dev);
  6926. u32 dpll;
  6927. struct dpll *clock = &crtc_state->dpll;
  6928. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6929. dpll = DPLL_VGA_MODE_DIS;
  6930. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6931. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6932. } else {
  6933. if (clock->p1 == 2)
  6934. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6935. else
  6936. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6937. if (clock->p2 == 4)
  6938. dpll |= PLL_P2_DIVIDE_BY_4;
  6939. }
  6940. if (!IS_I830(dev_priv) &&
  6941. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
  6942. dpll |= DPLL_DVO_2X_MODE;
  6943. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6944. intel_panel_use_ssc(dev_priv))
  6945. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6946. else
  6947. dpll |= PLL_REF_INPUT_DREFCLK;
  6948. dpll |= DPLL_VCO_ENABLE;
  6949. crtc_state->dpll_hw_state.dpll = dpll;
  6950. }
  6951. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6952. {
  6953. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  6954. enum pipe pipe = intel_crtc->pipe;
  6955. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6956. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  6957. uint32_t crtc_vtotal, crtc_vblank_end;
  6958. int vsyncshift = 0;
  6959. /* We need to be careful not to changed the adjusted mode, for otherwise
  6960. * the hw state checker will get angry at the mismatch. */
  6961. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6962. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6963. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6964. /* the chip adds 2 halflines automatically */
  6965. crtc_vtotal -= 1;
  6966. crtc_vblank_end -= 1;
  6967. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6968. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6969. else
  6970. vsyncshift = adjusted_mode->crtc_hsync_start -
  6971. adjusted_mode->crtc_htotal / 2;
  6972. if (vsyncshift < 0)
  6973. vsyncshift += adjusted_mode->crtc_htotal;
  6974. }
  6975. if (INTEL_GEN(dev_priv) > 3)
  6976. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6977. I915_WRITE(HTOTAL(cpu_transcoder),
  6978. (adjusted_mode->crtc_hdisplay - 1) |
  6979. ((adjusted_mode->crtc_htotal - 1) << 16));
  6980. I915_WRITE(HBLANK(cpu_transcoder),
  6981. (adjusted_mode->crtc_hblank_start - 1) |
  6982. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6983. I915_WRITE(HSYNC(cpu_transcoder),
  6984. (adjusted_mode->crtc_hsync_start - 1) |
  6985. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6986. I915_WRITE(VTOTAL(cpu_transcoder),
  6987. (adjusted_mode->crtc_vdisplay - 1) |
  6988. ((crtc_vtotal - 1) << 16));
  6989. I915_WRITE(VBLANK(cpu_transcoder),
  6990. (adjusted_mode->crtc_vblank_start - 1) |
  6991. ((crtc_vblank_end - 1) << 16));
  6992. I915_WRITE(VSYNC(cpu_transcoder),
  6993. (adjusted_mode->crtc_vsync_start - 1) |
  6994. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6995. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6996. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6997. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6998. * bits. */
  6999. if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
  7000. (pipe == PIPE_B || pipe == PIPE_C))
  7001. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  7002. }
  7003. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  7004. {
  7005. struct drm_device *dev = intel_crtc->base.dev;
  7006. struct drm_i915_private *dev_priv = to_i915(dev);
  7007. enum pipe pipe = intel_crtc->pipe;
  7008. /* pipesrc controls the size that is scaled from, which should
  7009. * always be the user's requested size.
  7010. */
  7011. I915_WRITE(PIPESRC(pipe),
  7012. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  7013. (intel_crtc->config->pipe_src_h - 1));
  7014. }
  7015. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  7016. struct intel_crtc_state *pipe_config)
  7017. {
  7018. struct drm_device *dev = crtc->base.dev;
  7019. struct drm_i915_private *dev_priv = to_i915(dev);
  7020. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  7021. uint32_t tmp;
  7022. tmp = I915_READ(HTOTAL(cpu_transcoder));
  7023. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  7024. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  7025. tmp = I915_READ(HBLANK(cpu_transcoder));
  7026. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  7027. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  7028. tmp = I915_READ(HSYNC(cpu_transcoder));
  7029. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  7030. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  7031. tmp = I915_READ(VTOTAL(cpu_transcoder));
  7032. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  7033. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  7034. tmp = I915_READ(VBLANK(cpu_transcoder));
  7035. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  7036. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  7037. tmp = I915_READ(VSYNC(cpu_transcoder));
  7038. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  7039. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  7040. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  7041. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  7042. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  7043. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  7044. }
  7045. }
  7046. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  7047. struct intel_crtc_state *pipe_config)
  7048. {
  7049. struct drm_device *dev = crtc->base.dev;
  7050. struct drm_i915_private *dev_priv = to_i915(dev);
  7051. u32 tmp;
  7052. tmp = I915_READ(PIPESRC(crtc->pipe));
  7053. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  7054. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  7055. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  7056. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  7057. }
  7058. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  7059. struct intel_crtc_state *pipe_config)
  7060. {
  7061. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  7062. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  7063. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  7064. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  7065. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  7066. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  7067. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  7068. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  7069. mode->flags = pipe_config->base.adjusted_mode.flags;
  7070. mode->type = DRM_MODE_TYPE_DRIVER;
  7071. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  7072. mode->flags |= pipe_config->base.adjusted_mode.flags;
  7073. mode->hsync = drm_mode_hsync(mode);
  7074. mode->vrefresh = drm_mode_vrefresh(mode);
  7075. drm_mode_set_name(mode);
  7076. }
  7077. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  7078. {
  7079. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  7080. uint32_t pipeconf;
  7081. pipeconf = 0;
  7082. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  7083. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  7084. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  7085. if (intel_crtc->config->double_wide)
  7086. pipeconf |= PIPECONF_DOUBLE_WIDE;
  7087. /* only g4x and later have fancy bpc/dither controls */
  7088. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  7089. IS_CHERRYVIEW(dev_priv)) {
  7090. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  7091. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  7092. pipeconf |= PIPECONF_DITHER_EN |
  7093. PIPECONF_DITHER_TYPE_SP;
  7094. switch (intel_crtc->config->pipe_bpp) {
  7095. case 18:
  7096. pipeconf |= PIPECONF_6BPC;
  7097. break;
  7098. case 24:
  7099. pipeconf |= PIPECONF_8BPC;
  7100. break;
  7101. case 30:
  7102. pipeconf |= PIPECONF_10BPC;
  7103. break;
  7104. default:
  7105. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7106. BUG();
  7107. }
  7108. }
  7109. if (HAS_PIPE_CXSR(dev_priv)) {
  7110. if (intel_crtc->lowfreq_avail) {
  7111. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  7112. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  7113. } else {
  7114. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  7115. }
  7116. }
  7117. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  7118. if (INTEL_GEN(dev_priv) < 4 ||
  7119. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  7120. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  7121. else
  7122. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  7123. } else
  7124. pipeconf |= PIPECONF_PROGRESSIVE;
  7125. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  7126. intel_crtc->config->limited_color_range)
  7127. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  7128. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  7129. POSTING_READ(PIPECONF(intel_crtc->pipe));
  7130. }
  7131. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  7132. struct intel_crtc_state *crtc_state)
  7133. {
  7134. struct drm_device *dev = crtc->base.dev;
  7135. struct drm_i915_private *dev_priv = to_i915(dev);
  7136. const struct intel_limit *limit;
  7137. int refclk = 48000;
  7138. memset(&crtc_state->dpll_hw_state, 0,
  7139. sizeof(crtc_state->dpll_hw_state));
  7140. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7141. if (intel_panel_use_ssc(dev_priv)) {
  7142. refclk = dev_priv->vbt.lvds_ssc_freq;
  7143. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7144. }
  7145. limit = &intel_limits_i8xx_lvds;
  7146. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
  7147. limit = &intel_limits_i8xx_dvo;
  7148. } else {
  7149. limit = &intel_limits_i8xx_dac;
  7150. }
  7151. if (!crtc_state->clock_set &&
  7152. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7153. refclk, NULL, &crtc_state->dpll)) {
  7154. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7155. return -EINVAL;
  7156. }
  7157. i8xx_compute_dpll(crtc, crtc_state, NULL);
  7158. return 0;
  7159. }
  7160. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  7161. struct intel_crtc_state *crtc_state)
  7162. {
  7163. struct drm_device *dev = crtc->base.dev;
  7164. struct drm_i915_private *dev_priv = to_i915(dev);
  7165. const struct intel_limit *limit;
  7166. int refclk = 96000;
  7167. memset(&crtc_state->dpll_hw_state, 0,
  7168. sizeof(crtc_state->dpll_hw_state));
  7169. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7170. if (intel_panel_use_ssc(dev_priv)) {
  7171. refclk = dev_priv->vbt.lvds_ssc_freq;
  7172. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7173. }
  7174. if (intel_is_dual_link_lvds(dev))
  7175. limit = &intel_limits_g4x_dual_channel_lvds;
  7176. else
  7177. limit = &intel_limits_g4x_single_channel_lvds;
  7178. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  7179. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  7180. limit = &intel_limits_g4x_hdmi;
  7181. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  7182. limit = &intel_limits_g4x_sdvo;
  7183. } else {
  7184. /* The option is for other outputs */
  7185. limit = &intel_limits_i9xx_sdvo;
  7186. }
  7187. if (!crtc_state->clock_set &&
  7188. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7189. refclk, NULL, &crtc_state->dpll)) {
  7190. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7191. return -EINVAL;
  7192. }
  7193. i9xx_compute_dpll(crtc, crtc_state, NULL);
  7194. return 0;
  7195. }
  7196. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  7197. struct intel_crtc_state *crtc_state)
  7198. {
  7199. struct drm_device *dev = crtc->base.dev;
  7200. struct drm_i915_private *dev_priv = to_i915(dev);
  7201. const struct intel_limit *limit;
  7202. int refclk = 96000;
  7203. memset(&crtc_state->dpll_hw_state, 0,
  7204. sizeof(crtc_state->dpll_hw_state));
  7205. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7206. if (intel_panel_use_ssc(dev_priv)) {
  7207. refclk = dev_priv->vbt.lvds_ssc_freq;
  7208. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7209. }
  7210. limit = &intel_limits_pineview_lvds;
  7211. } else {
  7212. limit = &intel_limits_pineview_sdvo;
  7213. }
  7214. if (!crtc_state->clock_set &&
  7215. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7216. refclk, NULL, &crtc_state->dpll)) {
  7217. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7218. return -EINVAL;
  7219. }
  7220. i9xx_compute_dpll(crtc, crtc_state, NULL);
  7221. return 0;
  7222. }
  7223. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  7224. struct intel_crtc_state *crtc_state)
  7225. {
  7226. struct drm_device *dev = crtc->base.dev;
  7227. struct drm_i915_private *dev_priv = to_i915(dev);
  7228. const struct intel_limit *limit;
  7229. int refclk = 96000;
  7230. memset(&crtc_state->dpll_hw_state, 0,
  7231. sizeof(crtc_state->dpll_hw_state));
  7232. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7233. if (intel_panel_use_ssc(dev_priv)) {
  7234. refclk = dev_priv->vbt.lvds_ssc_freq;
  7235. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7236. }
  7237. limit = &intel_limits_i9xx_lvds;
  7238. } else {
  7239. limit = &intel_limits_i9xx_sdvo;
  7240. }
  7241. if (!crtc_state->clock_set &&
  7242. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7243. refclk, NULL, &crtc_state->dpll)) {
  7244. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7245. return -EINVAL;
  7246. }
  7247. i9xx_compute_dpll(crtc, crtc_state, NULL);
  7248. return 0;
  7249. }
  7250. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  7251. struct intel_crtc_state *crtc_state)
  7252. {
  7253. int refclk = 100000;
  7254. const struct intel_limit *limit = &intel_limits_chv;
  7255. memset(&crtc_state->dpll_hw_state, 0,
  7256. sizeof(crtc_state->dpll_hw_state));
  7257. if (!crtc_state->clock_set &&
  7258. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7259. refclk, NULL, &crtc_state->dpll)) {
  7260. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7261. return -EINVAL;
  7262. }
  7263. chv_compute_dpll(crtc, crtc_state);
  7264. return 0;
  7265. }
  7266. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  7267. struct intel_crtc_state *crtc_state)
  7268. {
  7269. int refclk = 100000;
  7270. const struct intel_limit *limit = &intel_limits_vlv;
  7271. memset(&crtc_state->dpll_hw_state, 0,
  7272. sizeof(crtc_state->dpll_hw_state));
  7273. if (!crtc_state->clock_set &&
  7274. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7275. refclk, NULL, &crtc_state->dpll)) {
  7276. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7277. return -EINVAL;
  7278. }
  7279. vlv_compute_dpll(crtc, crtc_state);
  7280. return 0;
  7281. }
  7282. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  7283. struct intel_crtc_state *pipe_config)
  7284. {
  7285. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7286. uint32_t tmp;
  7287. if (INTEL_GEN(dev_priv) <= 3 &&
  7288. (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
  7289. return;
  7290. tmp = I915_READ(PFIT_CONTROL);
  7291. if (!(tmp & PFIT_ENABLE))
  7292. return;
  7293. /* Check whether the pfit is attached to our pipe. */
  7294. if (INTEL_GEN(dev_priv) < 4) {
  7295. if (crtc->pipe != PIPE_B)
  7296. return;
  7297. } else {
  7298. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  7299. return;
  7300. }
  7301. pipe_config->gmch_pfit.control = tmp;
  7302. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  7303. }
  7304. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  7305. struct intel_crtc_state *pipe_config)
  7306. {
  7307. struct drm_device *dev = crtc->base.dev;
  7308. struct drm_i915_private *dev_priv = to_i915(dev);
  7309. int pipe = pipe_config->cpu_transcoder;
  7310. struct dpll clock;
  7311. u32 mdiv;
  7312. int refclk = 100000;
  7313. /* In case of DSI, DPLL will not be used */
  7314. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  7315. return;
  7316. mutex_lock(&dev_priv->sb_lock);
  7317. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  7318. mutex_unlock(&dev_priv->sb_lock);
  7319. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  7320. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  7321. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  7322. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  7323. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  7324. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  7325. }
  7326. static void
  7327. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  7328. struct intel_initial_plane_config *plane_config)
  7329. {
  7330. struct drm_device *dev = crtc->base.dev;
  7331. struct drm_i915_private *dev_priv = to_i915(dev);
  7332. u32 val, base, offset;
  7333. int pipe = crtc->pipe, plane = crtc->plane;
  7334. int fourcc, pixel_format;
  7335. unsigned int aligned_height;
  7336. struct drm_framebuffer *fb;
  7337. struct intel_framebuffer *intel_fb;
  7338. val = I915_READ(DSPCNTR(plane));
  7339. if (!(val & DISPLAY_PLANE_ENABLE))
  7340. return;
  7341. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7342. if (!intel_fb) {
  7343. DRM_DEBUG_KMS("failed to alloc fb\n");
  7344. return;
  7345. }
  7346. fb = &intel_fb->base;
  7347. if (INTEL_GEN(dev_priv) >= 4) {
  7348. if (val & DISPPLANE_TILED) {
  7349. plane_config->tiling = I915_TILING_X;
  7350. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7351. }
  7352. }
  7353. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7354. fourcc = i9xx_format_to_fourcc(pixel_format);
  7355. fb->pixel_format = fourcc;
  7356. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7357. if (INTEL_GEN(dev_priv) >= 4) {
  7358. if (plane_config->tiling)
  7359. offset = I915_READ(DSPTILEOFF(plane));
  7360. else
  7361. offset = I915_READ(DSPLINOFF(plane));
  7362. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  7363. } else {
  7364. base = I915_READ(DSPADDR(plane));
  7365. }
  7366. plane_config->base = base;
  7367. val = I915_READ(PIPESRC(pipe));
  7368. fb->width = ((val >> 16) & 0xfff) + 1;
  7369. fb->height = ((val >> 0) & 0xfff) + 1;
  7370. val = I915_READ(DSPSTRIDE(pipe));
  7371. fb->pitches[0] = val & 0xffffffc0;
  7372. aligned_height = intel_fb_align_height(dev, fb->height,
  7373. fb->pixel_format,
  7374. fb->modifier);
  7375. plane_config->size = fb->pitches[0] * aligned_height;
  7376. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7377. pipe_name(pipe), plane, fb->width, fb->height,
  7378. fb->bits_per_pixel, base, fb->pitches[0],
  7379. plane_config->size);
  7380. plane_config->fb = intel_fb;
  7381. }
  7382. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  7383. struct intel_crtc_state *pipe_config)
  7384. {
  7385. struct drm_device *dev = crtc->base.dev;
  7386. struct drm_i915_private *dev_priv = to_i915(dev);
  7387. int pipe = pipe_config->cpu_transcoder;
  7388. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  7389. struct dpll clock;
  7390. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  7391. int refclk = 100000;
  7392. /* In case of DSI, DPLL will not be used */
  7393. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  7394. return;
  7395. mutex_lock(&dev_priv->sb_lock);
  7396. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  7397. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  7398. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  7399. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  7400. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  7401. mutex_unlock(&dev_priv->sb_lock);
  7402. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  7403. clock.m2 = (pll_dw0 & 0xff) << 22;
  7404. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  7405. clock.m2 |= pll_dw2 & 0x3fffff;
  7406. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  7407. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  7408. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  7409. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  7410. }
  7411. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  7412. struct intel_crtc_state *pipe_config)
  7413. {
  7414. struct drm_i915_private *dev_priv = to_i915(crtc->base.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_GEN(dev_priv) < 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_GEN(dev_priv) >= 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) || IS_PINEVIEW(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_i915_private *dev_priv)
  7509. {
  7510. struct intel_encoder *encoder;
  7511. int i;
  7512. u32 val, final;
  7513. bool has_lvds = false;
  7514. bool has_cpu_edp = false;
  7515. bool has_panel = false;
  7516. bool has_ck505 = false;
  7517. bool can_ssc = false;
  7518. bool using_ssc_source = false;
  7519. /* We need to take the global config into account */
  7520. for_each_intel_encoder(&dev_priv->drm, encoder) {
  7521. switch (encoder->type) {
  7522. case INTEL_OUTPUT_LVDS:
  7523. has_panel = true;
  7524. has_lvds = true;
  7525. break;
  7526. case INTEL_OUTPUT_EDP:
  7527. has_panel = true;
  7528. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  7529. has_cpu_edp = true;
  7530. break;
  7531. default:
  7532. break;
  7533. }
  7534. }
  7535. if (HAS_PCH_IBX(dev_priv)) {
  7536. has_ck505 = dev_priv->vbt.display_clock_mode;
  7537. can_ssc = has_ck505;
  7538. } else {
  7539. has_ck505 = false;
  7540. can_ssc = true;
  7541. }
  7542. /* Check if any DPLLs are using the SSC source */
  7543. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  7544. u32 temp = I915_READ(PCH_DPLL(i));
  7545. if (!(temp & DPLL_VCO_ENABLE))
  7546. continue;
  7547. if ((temp & PLL_REF_INPUT_MASK) ==
  7548. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  7549. using_ssc_source = true;
  7550. break;
  7551. }
  7552. }
  7553. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  7554. has_panel, has_lvds, has_ck505, using_ssc_source);
  7555. /* Ironlake: try to setup display ref clock before DPLL
  7556. * enabling. This is only under driver's control after
  7557. * PCH B stepping, previous chipset stepping should be
  7558. * ignoring this setting.
  7559. */
  7560. val = I915_READ(PCH_DREF_CONTROL);
  7561. /* As we must carefully and slowly disable/enable each source in turn,
  7562. * compute the final state we want first and check if we need to
  7563. * make any changes at all.
  7564. */
  7565. final = val;
  7566. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  7567. if (has_ck505)
  7568. final |= DREF_NONSPREAD_CK505_ENABLE;
  7569. else
  7570. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  7571. final &= ~DREF_SSC_SOURCE_MASK;
  7572. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7573. final &= ~DREF_SSC1_ENABLE;
  7574. if (has_panel) {
  7575. final |= DREF_SSC_SOURCE_ENABLE;
  7576. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7577. final |= DREF_SSC1_ENABLE;
  7578. if (has_cpu_edp) {
  7579. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7580. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7581. else
  7582. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7583. } else
  7584. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7585. } else if (using_ssc_source) {
  7586. final |= DREF_SSC_SOURCE_ENABLE;
  7587. final |= DREF_SSC1_ENABLE;
  7588. }
  7589. if (final == val)
  7590. return;
  7591. /* Always enable nonspread source */
  7592. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  7593. if (has_ck505)
  7594. val |= DREF_NONSPREAD_CK505_ENABLE;
  7595. else
  7596. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  7597. if (has_panel) {
  7598. val &= ~DREF_SSC_SOURCE_MASK;
  7599. val |= DREF_SSC_SOURCE_ENABLE;
  7600. /* SSC must be turned on before enabling the CPU output */
  7601. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7602. DRM_DEBUG_KMS("Using SSC on panel\n");
  7603. val |= DREF_SSC1_ENABLE;
  7604. } else
  7605. val &= ~DREF_SSC1_ENABLE;
  7606. /* Get SSC going before enabling the outputs */
  7607. I915_WRITE(PCH_DREF_CONTROL, val);
  7608. POSTING_READ(PCH_DREF_CONTROL);
  7609. udelay(200);
  7610. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7611. /* Enable CPU source on CPU attached eDP */
  7612. if (has_cpu_edp) {
  7613. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7614. DRM_DEBUG_KMS("Using SSC on eDP\n");
  7615. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7616. } else
  7617. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7618. } else
  7619. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7620. I915_WRITE(PCH_DREF_CONTROL, val);
  7621. POSTING_READ(PCH_DREF_CONTROL);
  7622. udelay(200);
  7623. } else {
  7624. DRM_DEBUG_KMS("Disabling CPU source output\n");
  7625. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7626. /* Turn off CPU output */
  7627. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7628. I915_WRITE(PCH_DREF_CONTROL, val);
  7629. POSTING_READ(PCH_DREF_CONTROL);
  7630. udelay(200);
  7631. if (!using_ssc_source) {
  7632. DRM_DEBUG_KMS("Disabling SSC source\n");
  7633. /* Turn off the SSC source */
  7634. val &= ~DREF_SSC_SOURCE_MASK;
  7635. val |= DREF_SSC_SOURCE_DISABLE;
  7636. /* Turn off SSC1 */
  7637. val &= ~DREF_SSC1_ENABLE;
  7638. I915_WRITE(PCH_DREF_CONTROL, val);
  7639. POSTING_READ(PCH_DREF_CONTROL);
  7640. udelay(200);
  7641. }
  7642. }
  7643. BUG_ON(val != final);
  7644. }
  7645. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  7646. {
  7647. uint32_t tmp;
  7648. tmp = I915_READ(SOUTH_CHICKEN2);
  7649. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  7650. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7651. if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
  7652. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  7653. DRM_ERROR("FDI mPHY reset assert timeout\n");
  7654. tmp = I915_READ(SOUTH_CHICKEN2);
  7655. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  7656. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7657. if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
  7658. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  7659. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  7660. }
  7661. /* WaMPhyProgramming:hsw */
  7662. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  7663. {
  7664. uint32_t tmp;
  7665. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  7666. tmp &= ~(0xFF << 24);
  7667. tmp |= (0x12 << 24);
  7668. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  7669. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  7670. tmp |= (1 << 11);
  7671. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  7672. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  7673. tmp |= (1 << 11);
  7674. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  7675. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  7676. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7677. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7678. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7679. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7680. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7681. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7682. tmp &= ~(7 << 13);
  7683. tmp |= (5 << 13);
  7684. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7685. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7686. tmp &= ~(7 << 13);
  7687. tmp |= (5 << 13);
  7688. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7689. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7690. tmp &= ~0xFF;
  7691. tmp |= 0x1C;
  7692. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7693. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7694. tmp &= ~0xFF;
  7695. tmp |= 0x1C;
  7696. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7697. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7698. tmp &= ~(0xFF << 16);
  7699. tmp |= (0x1C << 16);
  7700. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7701. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7702. tmp &= ~(0xFF << 16);
  7703. tmp |= (0x1C << 16);
  7704. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7705. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7706. tmp |= (1 << 27);
  7707. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7708. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7709. tmp |= (1 << 27);
  7710. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7711. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7712. tmp &= ~(0xF << 28);
  7713. tmp |= (4 << 28);
  7714. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7715. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7716. tmp &= ~(0xF << 28);
  7717. tmp |= (4 << 28);
  7718. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7719. }
  7720. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7721. * Programming" based on the parameters passed:
  7722. * - Sequence to enable CLKOUT_DP
  7723. * - Sequence to enable CLKOUT_DP without spread
  7724. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7725. */
  7726. static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
  7727. bool with_spread, bool with_fdi)
  7728. {
  7729. uint32_t reg, tmp;
  7730. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7731. with_spread = true;
  7732. if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
  7733. with_fdi, "LP PCH doesn't have FDI\n"))
  7734. with_fdi = false;
  7735. mutex_lock(&dev_priv->sb_lock);
  7736. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7737. tmp &= ~SBI_SSCCTL_DISABLE;
  7738. tmp |= SBI_SSCCTL_PATHALT;
  7739. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7740. udelay(24);
  7741. if (with_spread) {
  7742. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7743. tmp &= ~SBI_SSCCTL_PATHALT;
  7744. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7745. if (with_fdi) {
  7746. lpt_reset_fdi_mphy(dev_priv);
  7747. lpt_program_fdi_mphy(dev_priv);
  7748. }
  7749. }
  7750. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  7751. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7752. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7753. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7754. mutex_unlock(&dev_priv->sb_lock);
  7755. }
  7756. /* Sequence to disable CLKOUT_DP */
  7757. static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
  7758. {
  7759. uint32_t reg, tmp;
  7760. mutex_lock(&dev_priv->sb_lock);
  7761. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  7762. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7763. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7764. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7765. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7766. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7767. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7768. tmp |= SBI_SSCCTL_PATHALT;
  7769. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7770. udelay(32);
  7771. }
  7772. tmp |= SBI_SSCCTL_DISABLE;
  7773. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7774. }
  7775. mutex_unlock(&dev_priv->sb_lock);
  7776. }
  7777. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  7778. static const uint16_t sscdivintphase[] = {
  7779. [BEND_IDX( 50)] = 0x3B23,
  7780. [BEND_IDX( 45)] = 0x3B23,
  7781. [BEND_IDX( 40)] = 0x3C23,
  7782. [BEND_IDX( 35)] = 0x3C23,
  7783. [BEND_IDX( 30)] = 0x3D23,
  7784. [BEND_IDX( 25)] = 0x3D23,
  7785. [BEND_IDX( 20)] = 0x3E23,
  7786. [BEND_IDX( 15)] = 0x3E23,
  7787. [BEND_IDX( 10)] = 0x3F23,
  7788. [BEND_IDX( 5)] = 0x3F23,
  7789. [BEND_IDX( 0)] = 0x0025,
  7790. [BEND_IDX( -5)] = 0x0025,
  7791. [BEND_IDX(-10)] = 0x0125,
  7792. [BEND_IDX(-15)] = 0x0125,
  7793. [BEND_IDX(-20)] = 0x0225,
  7794. [BEND_IDX(-25)] = 0x0225,
  7795. [BEND_IDX(-30)] = 0x0325,
  7796. [BEND_IDX(-35)] = 0x0325,
  7797. [BEND_IDX(-40)] = 0x0425,
  7798. [BEND_IDX(-45)] = 0x0425,
  7799. [BEND_IDX(-50)] = 0x0525,
  7800. };
  7801. /*
  7802. * Bend CLKOUT_DP
  7803. * steps -50 to 50 inclusive, in steps of 5
  7804. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  7805. * change in clock period = -(steps / 10) * 5.787 ps
  7806. */
  7807. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  7808. {
  7809. uint32_t tmp;
  7810. int idx = BEND_IDX(steps);
  7811. if (WARN_ON(steps % 5 != 0))
  7812. return;
  7813. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  7814. return;
  7815. mutex_lock(&dev_priv->sb_lock);
  7816. if (steps % 10 != 0)
  7817. tmp = 0xAAAAAAAB;
  7818. else
  7819. tmp = 0x00000000;
  7820. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  7821. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  7822. tmp &= 0xffff0000;
  7823. tmp |= sscdivintphase[idx];
  7824. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  7825. mutex_unlock(&dev_priv->sb_lock);
  7826. }
  7827. #undef BEND_IDX
  7828. static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
  7829. {
  7830. struct intel_encoder *encoder;
  7831. bool has_vga = false;
  7832. for_each_intel_encoder(&dev_priv->drm, encoder) {
  7833. switch (encoder->type) {
  7834. case INTEL_OUTPUT_ANALOG:
  7835. has_vga = true;
  7836. break;
  7837. default:
  7838. break;
  7839. }
  7840. }
  7841. if (has_vga) {
  7842. lpt_bend_clkout_dp(dev_priv, 0);
  7843. lpt_enable_clkout_dp(dev_priv, true, true);
  7844. } else {
  7845. lpt_disable_clkout_dp(dev_priv);
  7846. }
  7847. }
  7848. /*
  7849. * Initialize reference clocks when the driver loads
  7850. */
  7851. void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
  7852. {
  7853. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
  7854. ironlake_init_pch_refclk(dev_priv);
  7855. else if (HAS_PCH_LPT(dev_priv))
  7856. lpt_init_pch_refclk(dev_priv);
  7857. }
  7858. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7859. {
  7860. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7861. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7862. int pipe = intel_crtc->pipe;
  7863. uint32_t val;
  7864. val = 0;
  7865. switch (intel_crtc->config->pipe_bpp) {
  7866. case 18:
  7867. val |= PIPECONF_6BPC;
  7868. break;
  7869. case 24:
  7870. val |= PIPECONF_8BPC;
  7871. break;
  7872. case 30:
  7873. val |= PIPECONF_10BPC;
  7874. break;
  7875. case 36:
  7876. val |= PIPECONF_12BPC;
  7877. break;
  7878. default:
  7879. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7880. BUG();
  7881. }
  7882. if (intel_crtc->config->dither)
  7883. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7884. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7885. val |= PIPECONF_INTERLACED_ILK;
  7886. else
  7887. val |= PIPECONF_PROGRESSIVE;
  7888. if (intel_crtc->config->limited_color_range)
  7889. val |= PIPECONF_COLOR_RANGE_SELECT;
  7890. I915_WRITE(PIPECONF(pipe), val);
  7891. POSTING_READ(PIPECONF(pipe));
  7892. }
  7893. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7894. {
  7895. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7896. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7897. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7898. u32 val = 0;
  7899. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  7900. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7901. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7902. val |= PIPECONF_INTERLACED_ILK;
  7903. else
  7904. val |= PIPECONF_PROGRESSIVE;
  7905. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7906. POSTING_READ(PIPECONF(cpu_transcoder));
  7907. }
  7908. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  7909. {
  7910. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7911. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7912. if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
  7913. u32 val = 0;
  7914. switch (intel_crtc->config->pipe_bpp) {
  7915. case 18:
  7916. val |= PIPEMISC_DITHER_6_BPC;
  7917. break;
  7918. case 24:
  7919. val |= PIPEMISC_DITHER_8_BPC;
  7920. break;
  7921. case 30:
  7922. val |= PIPEMISC_DITHER_10_BPC;
  7923. break;
  7924. case 36:
  7925. val |= PIPEMISC_DITHER_12_BPC;
  7926. break;
  7927. default:
  7928. /* Case prevented by pipe_config_set_bpp. */
  7929. BUG();
  7930. }
  7931. if (intel_crtc->config->dither)
  7932. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7933. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  7934. }
  7935. }
  7936. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7937. {
  7938. /*
  7939. * Account for spread spectrum to avoid
  7940. * oversubscribing the link. Max center spread
  7941. * is 2.5%; use 5% for safety's sake.
  7942. */
  7943. u32 bps = target_clock * bpp * 21 / 20;
  7944. return DIV_ROUND_UP(bps, link_bw * 8);
  7945. }
  7946. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7947. {
  7948. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7949. }
  7950. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7951. struct intel_crtc_state *crtc_state,
  7952. struct dpll *reduced_clock)
  7953. {
  7954. struct drm_crtc *crtc = &intel_crtc->base;
  7955. struct drm_device *dev = crtc->dev;
  7956. struct drm_i915_private *dev_priv = to_i915(dev);
  7957. u32 dpll, fp, fp2;
  7958. int factor;
  7959. /* Enable autotuning of the PLL clock (if permissible) */
  7960. factor = 21;
  7961. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7962. if ((intel_panel_use_ssc(dev_priv) &&
  7963. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7964. (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
  7965. factor = 25;
  7966. } else if (crtc_state->sdvo_tv_clock)
  7967. factor = 20;
  7968. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7969. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7970. fp |= FP_CB_TUNE;
  7971. if (reduced_clock) {
  7972. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  7973. if (reduced_clock->m < factor * reduced_clock->n)
  7974. fp2 |= FP_CB_TUNE;
  7975. } else {
  7976. fp2 = fp;
  7977. }
  7978. dpll = 0;
  7979. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  7980. dpll |= DPLLB_MODE_LVDS;
  7981. else
  7982. dpll |= DPLLB_MODE_DAC_SERIAL;
  7983. dpll |= (crtc_state->pixel_multiplier - 1)
  7984. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7985. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  7986. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  7987. dpll |= DPLL_SDVO_HIGH_SPEED;
  7988. if (intel_crtc_has_dp_encoder(crtc_state))
  7989. dpll |= DPLL_SDVO_HIGH_SPEED;
  7990. /*
  7991. * The high speed IO clock is only really required for
  7992. * SDVO/HDMI/DP, but we also enable it for CRT to make it
  7993. * possible to share the DPLL between CRT and HDMI. Enabling
  7994. * the clock needlessly does no real harm, except use up a
  7995. * bit of power potentially.
  7996. *
  7997. * We'll limit this to IVB with 3 pipes, since it has only two
  7998. * DPLLs and so DPLL sharing is the only way to get three pipes
  7999. * driving PCH ports at the same time. On SNB we could do this,
  8000. * and potentially avoid enabling the second DPLL, but it's not
  8001. * clear if it''s a win or loss power wise. No point in doing
  8002. * this on ILK at all since it has a fixed DPLL<->pipe mapping.
  8003. */
  8004. if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
  8005. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
  8006. dpll |= DPLL_SDVO_HIGH_SPEED;
  8007. /* compute bitmask from p1 value */
  8008. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  8009. /* also FPA1 */
  8010. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  8011. switch (crtc_state->dpll.p2) {
  8012. case 5:
  8013. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  8014. break;
  8015. case 7:
  8016. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  8017. break;
  8018. case 10:
  8019. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  8020. break;
  8021. case 14:
  8022. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  8023. break;
  8024. }
  8025. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  8026. intel_panel_use_ssc(dev_priv))
  8027. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  8028. else
  8029. dpll |= PLL_REF_INPUT_DREFCLK;
  8030. dpll |= DPLL_VCO_ENABLE;
  8031. crtc_state->dpll_hw_state.dpll = dpll;
  8032. crtc_state->dpll_hw_state.fp0 = fp;
  8033. crtc_state->dpll_hw_state.fp1 = fp2;
  8034. }
  8035. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  8036. struct intel_crtc_state *crtc_state)
  8037. {
  8038. struct drm_device *dev = crtc->base.dev;
  8039. struct drm_i915_private *dev_priv = to_i915(dev);
  8040. struct dpll reduced_clock;
  8041. bool has_reduced_clock = false;
  8042. struct intel_shared_dpll *pll;
  8043. const struct intel_limit *limit;
  8044. int refclk = 120000;
  8045. memset(&crtc_state->dpll_hw_state, 0,
  8046. sizeof(crtc_state->dpll_hw_state));
  8047. crtc->lowfreq_avail = false;
  8048. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  8049. if (!crtc_state->has_pch_encoder)
  8050. return 0;
  8051. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  8052. if (intel_panel_use_ssc(dev_priv)) {
  8053. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  8054. dev_priv->vbt.lvds_ssc_freq);
  8055. refclk = dev_priv->vbt.lvds_ssc_freq;
  8056. }
  8057. if (intel_is_dual_link_lvds(dev)) {
  8058. if (refclk == 100000)
  8059. limit = &intel_limits_ironlake_dual_lvds_100m;
  8060. else
  8061. limit = &intel_limits_ironlake_dual_lvds;
  8062. } else {
  8063. if (refclk == 100000)
  8064. limit = &intel_limits_ironlake_single_lvds_100m;
  8065. else
  8066. limit = &intel_limits_ironlake_single_lvds;
  8067. }
  8068. } else {
  8069. limit = &intel_limits_ironlake_dac;
  8070. }
  8071. if (!crtc_state->clock_set &&
  8072. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  8073. refclk, NULL, &crtc_state->dpll)) {
  8074. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  8075. return -EINVAL;
  8076. }
  8077. ironlake_compute_dpll(crtc, crtc_state,
  8078. has_reduced_clock ? &reduced_clock : NULL);
  8079. pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
  8080. if (pll == NULL) {
  8081. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  8082. pipe_name(crtc->pipe));
  8083. return -EINVAL;
  8084. }
  8085. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  8086. has_reduced_clock)
  8087. crtc->lowfreq_avail = true;
  8088. return 0;
  8089. }
  8090. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  8091. struct intel_link_m_n *m_n)
  8092. {
  8093. struct drm_device *dev = crtc->base.dev;
  8094. struct drm_i915_private *dev_priv = to_i915(dev);
  8095. enum pipe pipe = crtc->pipe;
  8096. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  8097. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  8098. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  8099. & ~TU_SIZE_MASK;
  8100. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  8101. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  8102. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8103. }
  8104. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  8105. enum transcoder transcoder,
  8106. struct intel_link_m_n *m_n,
  8107. struct intel_link_m_n *m2_n2)
  8108. {
  8109. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8110. enum pipe pipe = crtc->pipe;
  8111. if (INTEL_GEN(dev_priv) >= 5) {
  8112. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  8113. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  8114. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  8115. & ~TU_SIZE_MASK;
  8116. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  8117. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  8118. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8119. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  8120. * gen < 8) and if DRRS is supported (to make sure the
  8121. * registers are not unnecessarily read).
  8122. */
  8123. if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
  8124. crtc->config->has_drrs) {
  8125. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  8126. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  8127. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  8128. & ~TU_SIZE_MASK;
  8129. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  8130. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  8131. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8132. }
  8133. } else {
  8134. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  8135. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  8136. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  8137. & ~TU_SIZE_MASK;
  8138. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  8139. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  8140. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8141. }
  8142. }
  8143. void intel_dp_get_m_n(struct intel_crtc *crtc,
  8144. struct intel_crtc_state *pipe_config)
  8145. {
  8146. if (pipe_config->has_pch_encoder)
  8147. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  8148. else
  8149. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  8150. &pipe_config->dp_m_n,
  8151. &pipe_config->dp_m2_n2);
  8152. }
  8153. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  8154. struct intel_crtc_state *pipe_config)
  8155. {
  8156. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  8157. &pipe_config->fdi_m_n, NULL);
  8158. }
  8159. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  8160. struct intel_crtc_state *pipe_config)
  8161. {
  8162. struct drm_device *dev = crtc->base.dev;
  8163. struct drm_i915_private *dev_priv = to_i915(dev);
  8164. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  8165. uint32_t ps_ctrl = 0;
  8166. int id = -1;
  8167. int i;
  8168. /* find scaler attached to this pipe */
  8169. for (i = 0; i < crtc->num_scalers; i++) {
  8170. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  8171. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  8172. id = i;
  8173. pipe_config->pch_pfit.enabled = true;
  8174. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  8175. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  8176. break;
  8177. }
  8178. }
  8179. scaler_state->scaler_id = id;
  8180. if (id >= 0) {
  8181. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  8182. } else {
  8183. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8184. }
  8185. }
  8186. static void
  8187. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  8188. struct intel_initial_plane_config *plane_config)
  8189. {
  8190. struct drm_device *dev = crtc->base.dev;
  8191. struct drm_i915_private *dev_priv = to_i915(dev);
  8192. u32 val, base, offset, stride_mult, tiling;
  8193. int pipe = crtc->pipe;
  8194. int fourcc, pixel_format;
  8195. unsigned int aligned_height;
  8196. struct drm_framebuffer *fb;
  8197. struct intel_framebuffer *intel_fb;
  8198. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8199. if (!intel_fb) {
  8200. DRM_DEBUG_KMS("failed to alloc fb\n");
  8201. return;
  8202. }
  8203. fb = &intel_fb->base;
  8204. val = I915_READ(PLANE_CTL(pipe, 0));
  8205. if (!(val & PLANE_CTL_ENABLE))
  8206. goto error;
  8207. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  8208. fourcc = skl_format_to_fourcc(pixel_format,
  8209. val & PLANE_CTL_ORDER_RGBX,
  8210. val & PLANE_CTL_ALPHA_MASK);
  8211. fb->pixel_format = fourcc;
  8212. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  8213. tiling = val & PLANE_CTL_TILED_MASK;
  8214. switch (tiling) {
  8215. case PLANE_CTL_TILED_LINEAR:
  8216. fb->modifier = DRM_FORMAT_MOD_NONE;
  8217. break;
  8218. case PLANE_CTL_TILED_X:
  8219. plane_config->tiling = I915_TILING_X;
  8220. fb->modifier = I915_FORMAT_MOD_X_TILED;
  8221. break;
  8222. case PLANE_CTL_TILED_Y:
  8223. fb->modifier = I915_FORMAT_MOD_Y_TILED;
  8224. break;
  8225. case PLANE_CTL_TILED_YF:
  8226. fb->modifier = I915_FORMAT_MOD_Yf_TILED;
  8227. break;
  8228. default:
  8229. MISSING_CASE(tiling);
  8230. goto error;
  8231. }
  8232. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  8233. plane_config->base = base;
  8234. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  8235. val = I915_READ(PLANE_SIZE(pipe, 0));
  8236. fb->height = ((val >> 16) & 0xfff) + 1;
  8237. fb->width = ((val >> 0) & 0x1fff) + 1;
  8238. val = I915_READ(PLANE_STRIDE(pipe, 0));
  8239. stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier,
  8240. fb->pixel_format);
  8241. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  8242. aligned_height = intel_fb_align_height(dev, fb->height,
  8243. fb->pixel_format,
  8244. fb->modifier);
  8245. plane_config->size = fb->pitches[0] * aligned_height;
  8246. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  8247. pipe_name(pipe), fb->width, fb->height,
  8248. fb->bits_per_pixel, base, fb->pitches[0],
  8249. plane_config->size);
  8250. plane_config->fb = intel_fb;
  8251. return;
  8252. error:
  8253. kfree(intel_fb);
  8254. }
  8255. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  8256. struct intel_crtc_state *pipe_config)
  8257. {
  8258. struct drm_device *dev = crtc->base.dev;
  8259. struct drm_i915_private *dev_priv = to_i915(dev);
  8260. uint32_t tmp;
  8261. tmp = I915_READ(PF_CTL(crtc->pipe));
  8262. if (tmp & PF_ENABLE) {
  8263. pipe_config->pch_pfit.enabled = true;
  8264. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  8265. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  8266. /* We currently do not free assignements of panel fitters on
  8267. * ivb/hsw (since we don't use the higher upscaling modes which
  8268. * differentiates them) so just WARN about this case for now. */
  8269. if (IS_GEN7(dev_priv)) {
  8270. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  8271. PF_PIPE_SEL_IVB(crtc->pipe));
  8272. }
  8273. }
  8274. }
  8275. static void
  8276. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  8277. struct intel_initial_plane_config *plane_config)
  8278. {
  8279. struct drm_device *dev = crtc->base.dev;
  8280. struct drm_i915_private *dev_priv = to_i915(dev);
  8281. u32 val, base, offset;
  8282. int pipe = crtc->pipe;
  8283. int fourcc, pixel_format;
  8284. unsigned int aligned_height;
  8285. struct drm_framebuffer *fb;
  8286. struct intel_framebuffer *intel_fb;
  8287. val = I915_READ(DSPCNTR(pipe));
  8288. if (!(val & DISPLAY_PLANE_ENABLE))
  8289. return;
  8290. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8291. if (!intel_fb) {
  8292. DRM_DEBUG_KMS("failed to alloc fb\n");
  8293. return;
  8294. }
  8295. fb = &intel_fb->base;
  8296. if (INTEL_GEN(dev_priv) >= 4) {
  8297. if (val & DISPPLANE_TILED) {
  8298. plane_config->tiling = I915_TILING_X;
  8299. fb->modifier = I915_FORMAT_MOD_X_TILED;
  8300. }
  8301. }
  8302. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  8303. fourcc = i9xx_format_to_fourcc(pixel_format);
  8304. fb->pixel_format = fourcc;
  8305. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  8306. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  8307. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  8308. offset = I915_READ(DSPOFFSET(pipe));
  8309. } else {
  8310. if (plane_config->tiling)
  8311. offset = I915_READ(DSPTILEOFF(pipe));
  8312. else
  8313. offset = I915_READ(DSPLINOFF(pipe));
  8314. }
  8315. plane_config->base = base;
  8316. val = I915_READ(PIPESRC(pipe));
  8317. fb->width = ((val >> 16) & 0xfff) + 1;
  8318. fb->height = ((val >> 0) & 0xfff) + 1;
  8319. val = I915_READ(DSPSTRIDE(pipe));
  8320. fb->pitches[0] = val & 0xffffffc0;
  8321. aligned_height = intel_fb_align_height(dev, fb->height,
  8322. fb->pixel_format,
  8323. fb->modifier);
  8324. plane_config->size = fb->pitches[0] * aligned_height;
  8325. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  8326. pipe_name(pipe), fb->width, fb->height,
  8327. fb->bits_per_pixel, base, fb->pitches[0],
  8328. plane_config->size);
  8329. plane_config->fb = intel_fb;
  8330. }
  8331. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  8332. struct intel_crtc_state *pipe_config)
  8333. {
  8334. struct drm_device *dev = crtc->base.dev;
  8335. struct drm_i915_private *dev_priv = to_i915(dev);
  8336. enum intel_display_power_domain power_domain;
  8337. uint32_t tmp;
  8338. bool ret;
  8339. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  8340. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8341. return false;
  8342. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8343. pipe_config->shared_dpll = NULL;
  8344. ret = false;
  8345. tmp = I915_READ(PIPECONF(crtc->pipe));
  8346. if (!(tmp & PIPECONF_ENABLE))
  8347. goto out;
  8348. switch (tmp & PIPECONF_BPC_MASK) {
  8349. case PIPECONF_6BPC:
  8350. pipe_config->pipe_bpp = 18;
  8351. break;
  8352. case PIPECONF_8BPC:
  8353. pipe_config->pipe_bpp = 24;
  8354. break;
  8355. case PIPECONF_10BPC:
  8356. pipe_config->pipe_bpp = 30;
  8357. break;
  8358. case PIPECONF_12BPC:
  8359. pipe_config->pipe_bpp = 36;
  8360. break;
  8361. default:
  8362. break;
  8363. }
  8364. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  8365. pipe_config->limited_color_range = true;
  8366. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  8367. struct intel_shared_dpll *pll;
  8368. enum intel_dpll_id pll_id;
  8369. pipe_config->has_pch_encoder = true;
  8370. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  8371. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8372. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8373. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8374. if (HAS_PCH_IBX(dev_priv)) {
  8375. /*
  8376. * The pipe->pch transcoder and pch transcoder->pll
  8377. * mapping is fixed.
  8378. */
  8379. pll_id = (enum intel_dpll_id) crtc->pipe;
  8380. } else {
  8381. tmp = I915_READ(PCH_DPLL_SEL);
  8382. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  8383. pll_id = DPLL_ID_PCH_PLL_B;
  8384. else
  8385. pll_id= DPLL_ID_PCH_PLL_A;
  8386. }
  8387. pipe_config->shared_dpll =
  8388. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  8389. pll = pipe_config->shared_dpll;
  8390. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  8391. &pipe_config->dpll_hw_state));
  8392. tmp = pipe_config->dpll_hw_state.dpll;
  8393. pipe_config->pixel_multiplier =
  8394. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  8395. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  8396. ironlake_pch_clock_get(crtc, pipe_config);
  8397. } else {
  8398. pipe_config->pixel_multiplier = 1;
  8399. }
  8400. intel_get_pipe_timings(crtc, pipe_config);
  8401. intel_get_pipe_src_size(crtc, pipe_config);
  8402. ironlake_get_pfit_config(crtc, pipe_config);
  8403. ret = true;
  8404. out:
  8405. intel_display_power_put(dev_priv, power_domain);
  8406. return ret;
  8407. }
  8408. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  8409. {
  8410. struct drm_device *dev = &dev_priv->drm;
  8411. struct intel_crtc *crtc;
  8412. for_each_intel_crtc(dev, crtc)
  8413. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  8414. pipe_name(crtc->pipe));
  8415. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  8416. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  8417. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  8418. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  8419. I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
  8420. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  8421. "CPU PWM1 enabled\n");
  8422. if (IS_HASWELL(dev_priv))
  8423. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  8424. "CPU PWM2 enabled\n");
  8425. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  8426. "PCH PWM1 enabled\n");
  8427. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  8428. "Utility pin enabled\n");
  8429. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  8430. /*
  8431. * In theory we can still leave IRQs enabled, as long as only the HPD
  8432. * interrupts remain enabled. We used to check for that, but since it's
  8433. * gen-specific and since we only disable LCPLL after we fully disable
  8434. * the interrupts, the check below should be enough.
  8435. */
  8436. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  8437. }
  8438. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  8439. {
  8440. if (IS_HASWELL(dev_priv))
  8441. return I915_READ(D_COMP_HSW);
  8442. else
  8443. return I915_READ(D_COMP_BDW);
  8444. }
  8445. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  8446. {
  8447. if (IS_HASWELL(dev_priv)) {
  8448. mutex_lock(&dev_priv->rps.hw_lock);
  8449. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  8450. val))
  8451. DRM_DEBUG_KMS("Failed to write to D_COMP\n");
  8452. mutex_unlock(&dev_priv->rps.hw_lock);
  8453. } else {
  8454. I915_WRITE(D_COMP_BDW, val);
  8455. POSTING_READ(D_COMP_BDW);
  8456. }
  8457. }
  8458. /*
  8459. * This function implements pieces of two sequences from BSpec:
  8460. * - Sequence for display software to disable LCPLL
  8461. * - Sequence for display software to allow package C8+
  8462. * The steps implemented here are just the steps that actually touch the LCPLL
  8463. * register. Callers should take care of disabling all the display engine
  8464. * functions, doing the mode unset, fixing interrupts, etc.
  8465. */
  8466. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  8467. bool switch_to_fclk, bool allow_power_down)
  8468. {
  8469. uint32_t val;
  8470. assert_can_disable_lcpll(dev_priv);
  8471. val = I915_READ(LCPLL_CTL);
  8472. if (switch_to_fclk) {
  8473. val |= LCPLL_CD_SOURCE_FCLK;
  8474. I915_WRITE(LCPLL_CTL, val);
  8475. if (wait_for_us(I915_READ(LCPLL_CTL) &
  8476. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8477. DRM_ERROR("Switching to FCLK failed\n");
  8478. val = I915_READ(LCPLL_CTL);
  8479. }
  8480. val |= LCPLL_PLL_DISABLE;
  8481. I915_WRITE(LCPLL_CTL, val);
  8482. POSTING_READ(LCPLL_CTL);
  8483. if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
  8484. DRM_ERROR("LCPLL still locked\n");
  8485. val = hsw_read_dcomp(dev_priv);
  8486. val |= D_COMP_COMP_DISABLE;
  8487. hsw_write_dcomp(dev_priv, val);
  8488. ndelay(100);
  8489. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  8490. 1))
  8491. DRM_ERROR("D_COMP RCOMP still in progress\n");
  8492. if (allow_power_down) {
  8493. val = I915_READ(LCPLL_CTL);
  8494. val |= LCPLL_POWER_DOWN_ALLOW;
  8495. I915_WRITE(LCPLL_CTL, val);
  8496. POSTING_READ(LCPLL_CTL);
  8497. }
  8498. }
  8499. /*
  8500. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  8501. * source.
  8502. */
  8503. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  8504. {
  8505. uint32_t val;
  8506. val = I915_READ(LCPLL_CTL);
  8507. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  8508. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  8509. return;
  8510. /*
  8511. * Make sure we're not on PC8 state before disabling PC8, otherwise
  8512. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  8513. */
  8514. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  8515. if (val & LCPLL_POWER_DOWN_ALLOW) {
  8516. val &= ~LCPLL_POWER_DOWN_ALLOW;
  8517. I915_WRITE(LCPLL_CTL, val);
  8518. POSTING_READ(LCPLL_CTL);
  8519. }
  8520. val = hsw_read_dcomp(dev_priv);
  8521. val |= D_COMP_COMP_FORCE;
  8522. val &= ~D_COMP_COMP_DISABLE;
  8523. hsw_write_dcomp(dev_priv, val);
  8524. val = I915_READ(LCPLL_CTL);
  8525. val &= ~LCPLL_PLL_DISABLE;
  8526. I915_WRITE(LCPLL_CTL, val);
  8527. if (intel_wait_for_register(dev_priv,
  8528. LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  8529. 5))
  8530. DRM_ERROR("LCPLL not locked yet\n");
  8531. if (val & LCPLL_CD_SOURCE_FCLK) {
  8532. val = I915_READ(LCPLL_CTL);
  8533. val &= ~LCPLL_CD_SOURCE_FCLK;
  8534. I915_WRITE(LCPLL_CTL, val);
  8535. if (wait_for_us((I915_READ(LCPLL_CTL) &
  8536. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8537. DRM_ERROR("Switching back to LCPLL failed\n");
  8538. }
  8539. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  8540. intel_update_cdclk(dev_priv);
  8541. }
  8542. /*
  8543. * Package states C8 and deeper are really deep PC states that can only be
  8544. * reached when all the devices on the system allow it, so even if the graphics
  8545. * device allows PC8+, it doesn't mean the system will actually get to these
  8546. * states. Our driver only allows PC8+ when going into runtime PM.
  8547. *
  8548. * The requirements for PC8+ are that all the outputs are disabled, the power
  8549. * well is disabled and most interrupts are disabled, and these are also
  8550. * requirements for runtime PM. When these conditions are met, we manually do
  8551. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  8552. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  8553. * hang the machine.
  8554. *
  8555. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  8556. * the state of some registers, so when we come back from PC8+ we need to
  8557. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  8558. * need to take care of the registers kept by RC6. Notice that this happens even
  8559. * if we don't put the device in PCI D3 state (which is what currently happens
  8560. * because of the runtime PM support).
  8561. *
  8562. * For more, read "Display Sequences for Package C8" on the hardware
  8563. * documentation.
  8564. */
  8565. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  8566. {
  8567. uint32_t val;
  8568. DRM_DEBUG_KMS("Enabling package C8+\n");
  8569. if (HAS_PCH_LPT_LP(dev_priv)) {
  8570. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8571. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  8572. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8573. }
  8574. lpt_disable_clkout_dp(dev_priv);
  8575. hsw_disable_lcpll(dev_priv, true, true);
  8576. }
  8577. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  8578. {
  8579. uint32_t val;
  8580. DRM_DEBUG_KMS("Disabling package C8+\n");
  8581. hsw_restore_lcpll(dev_priv);
  8582. lpt_init_pch_refclk(dev_priv);
  8583. if (HAS_PCH_LPT_LP(dev_priv)) {
  8584. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8585. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  8586. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8587. }
  8588. }
  8589. static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8590. {
  8591. struct drm_device *dev = old_state->dev;
  8592. struct intel_atomic_state *old_intel_state =
  8593. to_intel_atomic_state(old_state);
  8594. unsigned int req_cdclk = old_intel_state->dev_cdclk;
  8595. bxt_set_cdclk(to_i915(dev), req_cdclk);
  8596. }
  8597. static int bdw_adjust_min_pipe_pixel_rate(struct intel_crtc_state *crtc_state,
  8598. int pixel_rate)
  8599. {
  8600. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  8601. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  8602. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  8603. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  8604. /* BSpec says "Do not use DisplayPort with CDCLK less than
  8605. * 432 MHz, audio enabled, port width x4, and link rate
  8606. * HBR2 (5.4 GHz), or else there may be audio corruption or
  8607. * screen corruption."
  8608. */
  8609. if (intel_crtc_has_dp_encoder(crtc_state) &&
  8610. crtc_state->has_audio &&
  8611. crtc_state->port_clock >= 540000 &&
  8612. crtc_state->lane_count == 4)
  8613. pixel_rate = max(432000, pixel_rate);
  8614. return pixel_rate;
  8615. }
  8616. /* compute the max rate for new configuration */
  8617. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  8618. {
  8619. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8620. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8621. struct drm_crtc *crtc;
  8622. struct drm_crtc_state *cstate;
  8623. struct intel_crtc_state *crtc_state;
  8624. unsigned max_pixel_rate = 0, i;
  8625. enum pipe pipe;
  8626. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  8627. sizeof(intel_state->min_pixclk));
  8628. for_each_crtc_in_state(state, crtc, cstate, i) {
  8629. int pixel_rate;
  8630. crtc_state = to_intel_crtc_state(cstate);
  8631. if (!crtc_state->base.enable) {
  8632. intel_state->min_pixclk[i] = 0;
  8633. continue;
  8634. }
  8635. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  8636. if (IS_BROADWELL(dev_priv) || IS_GEN9(dev_priv))
  8637. pixel_rate = bdw_adjust_min_pipe_pixel_rate(crtc_state,
  8638. pixel_rate);
  8639. intel_state->min_pixclk[i] = pixel_rate;
  8640. }
  8641. for_each_pipe(dev_priv, pipe)
  8642. max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
  8643. return max_pixel_rate;
  8644. }
  8645. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  8646. {
  8647. struct drm_i915_private *dev_priv = to_i915(dev);
  8648. uint32_t val, data;
  8649. int ret;
  8650. if (WARN((I915_READ(LCPLL_CTL) &
  8651. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  8652. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  8653. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  8654. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  8655. "trying to change cdclk frequency with cdclk not enabled\n"))
  8656. return;
  8657. mutex_lock(&dev_priv->rps.hw_lock);
  8658. ret = sandybridge_pcode_write(dev_priv,
  8659. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  8660. mutex_unlock(&dev_priv->rps.hw_lock);
  8661. if (ret) {
  8662. DRM_ERROR("failed to inform pcode about cdclk change\n");
  8663. return;
  8664. }
  8665. val = I915_READ(LCPLL_CTL);
  8666. val |= LCPLL_CD_SOURCE_FCLK;
  8667. I915_WRITE(LCPLL_CTL, val);
  8668. if (wait_for_us(I915_READ(LCPLL_CTL) &
  8669. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8670. DRM_ERROR("Switching to FCLK failed\n");
  8671. val = I915_READ(LCPLL_CTL);
  8672. val &= ~LCPLL_CLK_FREQ_MASK;
  8673. switch (cdclk) {
  8674. case 450000:
  8675. val |= LCPLL_CLK_FREQ_450;
  8676. data = 0;
  8677. break;
  8678. case 540000:
  8679. val |= LCPLL_CLK_FREQ_54O_BDW;
  8680. data = 1;
  8681. break;
  8682. case 337500:
  8683. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8684. data = 2;
  8685. break;
  8686. case 675000:
  8687. val |= LCPLL_CLK_FREQ_675_BDW;
  8688. data = 3;
  8689. break;
  8690. default:
  8691. WARN(1, "invalid cdclk frequency\n");
  8692. return;
  8693. }
  8694. I915_WRITE(LCPLL_CTL, val);
  8695. val = I915_READ(LCPLL_CTL);
  8696. val &= ~LCPLL_CD_SOURCE_FCLK;
  8697. I915_WRITE(LCPLL_CTL, val);
  8698. if (wait_for_us((I915_READ(LCPLL_CTL) &
  8699. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8700. DRM_ERROR("Switching back to LCPLL failed\n");
  8701. mutex_lock(&dev_priv->rps.hw_lock);
  8702. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8703. mutex_unlock(&dev_priv->rps.hw_lock);
  8704. I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
  8705. intel_update_cdclk(dev_priv);
  8706. WARN(cdclk != dev_priv->cdclk_freq,
  8707. "cdclk requested %d kHz but got %d kHz\n",
  8708. cdclk, dev_priv->cdclk_freq);
  8709. }
  8710. static int broadwell_calc_cdclk(int max_pixclk)
  8711. {
  8712. if (max_pixclk > 540000)
  8713. return 675000;
  8714. else if (max_pixclk > 450000)
  8715. return 540000;
  8716. else if (max_pixclk > 337500)
  8717. return 450000;
  8718. else
  8719. return 337500;
  8720. }
  8721. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8722. {
  8723. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8724. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8725. int max_pixclk = ilk_max_pixel_rate(state);
  8726. int cdclk;
  8727. /*
  8728. * FIXME should also account for plane ratio
  8729. * once 64bpp pixel formats are supported.
  8730. */
  8731. cdclk = broadwell_calc_cdclk(max_pixclk);
  8732. if (cdclk > dev_priv->max_cdclk_freq) {
  8733. DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8734. cdclk, dev_priv->max_cdclk_freq);
  8735. return -EINVAL;
  8736. }
  8737. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8738. if (!intel_state->active_crtcs)
  8739. intel_state->dev_cdclk = broadwell_calc_cdclk(0);
  8740. return 0;
  8741. }
  8742. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8743. {
  8744. struct drm_device *dev = old_state->dev;
  8745. struct intel_atomic_state *old_intel_state =
  8746. to_intel_atomic_state(old_state);
  8747. unsigned req_cdclk = old_intel_state->dev_cdclk;
  8748. broadwell_set_cdclk(dev, req_cdclk);
  8749. }
  8750. static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
  8751. {
  8752. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8753. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8754. const int max_pixclk = ilk_max_pixel_rate(state);
  8755. int vco = intel_state->cdclk_pll_vco;
  8756. int cdclk;
  8757. /*
  8758. * FIXME should also account for plane ratio
  8759. * once 64bpp pixel formats are supported.
  8760. */
  8761. cdclk = skl_calc_cdclk(max_pixclk, vco);
  8762. /*
  8763. * FIXME move the cdclk caclulation to
  8764. * compute_config() so we can fail gracegully.
  8765. */
  8766. if (cdclk > dev_priv->max_cdclk_freq) {
  8767. DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8768. cdclk, dev_priv->max_cdclk_freq);
  8769. cdclk = dev_priv->max_cdclk_freq;
  8770. }
  8771. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8772. if (!intel_state->active_crtcs)
  8773. intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
  8774. return 0;
  8775. }
  8776. static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8777. {
  8778. struct drm_i915_private *dev_priv = to_i915(old_state->dev);
  8779. struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
  8780. unsigned int req_cdclk = intel_state->dev_cdclk;
  8781. unsigned int req_vco = intel_state->cdclk_pll_vco;
  8782. skl_set_cdclk(dev_priv, req_cdclk, req_vco);
  8783. }
  8784. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8785. struct intel_crtc_state *crtc_state)
  8786. {
  8787. if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
  8788. if (!intel_ddi_pll_select(crtc, crtc_state))
  8789. return -EINVAL;
  8790. }
  8791. crtc->lowfreq_avail = false;
  8792. return 0;
  8793. }
  8794. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8795. enum port port,
  8796. struct intel_crtc_state *pipe_config)
  8797. {
  8798. enum intel_dpll_id id;
  8799. switch (port) {
  8800. case PORT_A:
  8801. id = DPLL_ID_SKL_DPLL0;
  8802. break;
  8803. case PORT_B:
  8804. id = DPLL_ID_SKL_DPLL1;
  8805. break;
  8806. case PORT_C:
  8807. id = DPLL_ID_SKL_DPLL2;
  8808. break;
  8809. default:
  8810. DRM_ERROR("Incorrect port type\n");
  8811. return;
  8812. }
  8813. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8814. }
  8815. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8816. enum port port,
  8817. struct intel_crtc_state *pipe_config)
  8818. {
  8819. enum intel_dpll_id id;
  8820. u32 temp;
  8821. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8822. id = temp >> (port * 3 + 1);
  8823. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
  8824. return;
  8825. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8826. }
  8827. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8828. enum port port,
  8829. struct intel_crtc_state *pipe_config)
  8830. {
  8831. enum intel_dpll_id id;
  8832. uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8833. switch (ddi_pll_sel) {
  8834. case PORT_CLK_SEL_WRPLL1:
  8835. id = DPLL_ID_WRPLL1;
  8836. break;
  8837. case PORT_CLK_SEL_WRPLL2:
  8838. id = DPLL_ID_WRPLL2;
  8839. break;
  8840. case PORT_CLK_SEL_SPLL:
  8841. id = DPLL_ID_SPLL;
  8842. break;
  8843. case PORT_CLK_SEL_LCPLL_810:
  8844. id = DPLL_ID_LCPLL_810;
  8845. break;
  8846. case PORT_CLK_SEL_LCPLL_1350:
  8847. id = DPLL_ID_LCPLL_1350;
  8848. break;
  8849. case PORT_CLK_SEL_LCPLL_2700:
  8850. id = DPLL_ID_LCPLL_2700;
  8851. break;
  8852. default:
  8853. MISSING_CASE(ddi_pll_sel);
  8854. /* fall through */
  8855. case PORT_CLK_SEL_NONE:
  8856. return;
  8857. }
  8858. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8859. }
  8860. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  8861. struct intel_crtc_state *pipe_config,
  8862. unsigned long *power_domain_mask)
  8863. {
  8864. struct drm_device *dev = crtc->base.dev;
  8865. struct drm_i915_private *dev_priv = to_i915(dev);
  8866. enum intel_display_power_domain power_domain;
  8867. u32 tmp;
  8868. /*
  8869. * The pipe->transcoder mapping is fixed with the exception of the eDP
  8870. * transcoder handled below.
  8871. */
  8872. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8873. /*
  8874. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  8875. * consistency and less surprising code; it's in always on power).
  8876. */
  8877. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8878. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8879. enum pipe trans_edp_pipe;
  8880. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8881. default:
  8882. WARN(1, "unknown pipe linked to edp transcoder\n");
  8883. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8884. case TRANS_DDI_EDP_INPUT_A_ON:
  8885. trans_edp_pipe = PIPE_A;
  8886. break;
  8887. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8888. trans_edp_pipe = PIPE_B;
  8889. break;
  8890. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8891. trans_edp_pipe = PIPE_C;
  8892. break;
  8893. }
  8894. if (trans_edp_pipe == crtc->pipe)
  8895. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8896. }
  8897. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  8898. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8899. return false;
  8900. *power_domain_mask |= BIT(power_domain);
  8901. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8902. return tmp & PIPECONF_ENABLE;
  8903. }
  8904. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  8905. struct intel_crtc_state *pipe_config,
  8906. unsigned long *power_domain_mask)
  8907. {
  8908. struct drm_device *dev = crtc->base.dev;
  8909. struct drm_i915_private *dev_priv = to_i915(dev);
  8910. enum intel_display_power_domain power_domain;
  8911. enum port port;
  8912. enum transcoder cpu_transcoder;
  8913. u32 tmp;
  8914. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  8915. if (port == PORT_A)
  8916. cpu_transcoder = TRANSCODER_DSI_A;
  8917. else
  8918. cpu_transcoder = TRANSCODER_DSI_C;
  8919. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  8920. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8921. continue;
  8922. *power_domain_mask |= BIT(power_domain);
  8923. /*
  8924. * The PLL needs to be enabled with a valid divider
  8925. * configuration, otherwise accessing DSI registers will hang
  8926. * the machine. See BSpec North Display Engine
  8927. * registers/MIPI[BXT]. We can break out here early, since we
  8928. * need the same DSI PLL to be enabled for both DSI ports.
  8929. */
  8930. if (!intel_dsi_pll_is_enabled(dev_priv))
  8931. break;
  8932. /* XXX: this works for video mode only */
  8933. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  8934. if (!(tmp & DPI_ENABLE))
  8935. continue;
  8936. tmp = I915_READ(MIPI_CTRL(port));
  8937. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  8938. continue;
  8939. pipe_config->cpu_transcoder = cpu_transcoder;
  8940. break;
  8941. }
  8942. return transcoder_is_dsi(pipe_config->cpu_transcoder);
  8943. }
  8944. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8945. struct intel_crtc_state *pipe_config)
  8946. {
  8947. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8948. struct intel_shared_dpll *pll;
  8949. enum port port;
  8950. uint32_t tmp;
  8951. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8952. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8953. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  8954. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8955. else if (IS_GEN9_LP(dev_priv))
  8956. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8957. else
  8958. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8959. pll = pipe_config->shared_dpll;
  8960. if (pll) {
  8961. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  8962. &pipe_config->dpll_hw_state));
  8963. }
  8964. /*
  8965. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8966. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8967. * the PCH transcoder is on.
  8968. */
  8969. if (INTEL_GEN(dev_priv) < 9 &&
  8970. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8971. pipe_config->has_pch_encoder = true;
  8972. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8973. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8974. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8975. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8976. }
  8977. }
  8978. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8979. struct intel_crtc_state *pipe_config)
  8980. {
  8981. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8982. enum intel_display_power_domain power_domain;
  8983. unsigned long power_domain_mask;
  8984. bool active;
  8985. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  8986. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8987. return false;
  8988. power_domain_mask = BIT(power_domain);
  8989. pipe_config->shared_dpll = NULL;
  8990. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  8991. if (IS_GEN9_LP(dev_priv) &&
  8992. bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
  8993. WARN_ON(active);
  8994. active = true;
  8995. }
  8996. if (!active)
  8997. goto out;
  8998. if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  8999. haswell_get_ddi_port_state(crtc, pipe_config);
  9000. intel_get_pipe_timings(crtc, pipe_config);
  9001. }
  9002. intel_get_pipe_src_size(crtc, pipe_config);
  9003. pipe_config->gamma_mode =
  9004. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  9005. if (INTEL_GEN(dev_priv) >= 9) {
  9006. skl_init_scalers(dev_priv, crtc, pipe_config);
  9007. pipe_config->scaler_state.scaler_id = -1;
  9008. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  9009. }
  9010. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  9011. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  9012. power_domain_mask |= BIT(power_domain);
  9013. if (INTEL_GEN(dev_priv) >= 9)
  9014. skylake_get_pfit_config(crtc, pipe_config);
  9015. else
  9016. ironlake_get_pfit_config(crtc, pipe_config);
  9017. }
  9018. if (IS_HASWELL(dev_priv))
  9019. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  9020. (I915_READ(IPS_CTL) & IPS_ENABLE);
  9021. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  9022. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  9023. pipe_config->pixel_multiplier =
  9024. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  9025. } else {
  9026. pipe_config->pixel_multiplier = 1;
  9027. }
  9028. out:
  9029. for_each_power_domain(power_domain, power_domain_mask)
  9030. intel_display_power_put(dev_priv, power_domain);
  9031. return active;
  9032. }
  9033. static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
  9034. const struct intel_plane_state *plane_state)
  9035. {
  9036. struct drm_device *dev = crtc->dev;
  9037. struct drm_i915_private *dev_priv = to_i915(dev);
  9038. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9039. uint32_t cntl = 0, size = 0;
  9040. if (plane_state && plane_state->base.visible) {
  9041. unsigned int width = plane_state->base.crtc_w;
  9042. unsigned int height = plane_state->base.crtc_h;
  9043. unsigned int stride = roundup_pow_of_two(width) * 4;
  9044. switch (stride) {
  9045. default:
  9046. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  9047. width, stride);
  9048. stride = 256;
  9049. /* fallthrough */
  9050. case 256:
  9051. case 512:
  9052. case 1024:
  9053. case 2048:
  9054. break;
  9055. }
  9056. cntl |= CURSOR_ENABLE |
  9057. CURSOR_GAMMA_ENABLE |
  9058. CURSOR_FORMAT_ARGB |
  9059. CURSOR_STRIDE(stride);
  9060. size = (height << 12) | width;
  9061. }
  9062. if (intel_crtc->cursor_cntl != 0 &&
  9063. (intel_crtc->cursor_base != base ||
  9064. intel_crtc->cursor_size != size ||
  9065. intel_crtc->cursor_cntl != cntl)) {
  9066. /* On these chipsets we can only modify the base/size/stride
  9067. * whilst the cursor is disabled.
  9068. */
  9069. I915_WRITE(CURCNTR(PIPE_A), 0);
  9070. POSTING_READ(CURCNTR(PIPE_A));
  9071. intel_crtc->cursor_cntl = 0;
  9072. }
  9073. if (intel_crtc->cursor_base != base) {
  9074. I915_WRITE(CURBASE(PIPE_A), base);
  9075. intel_crtc->cursor_base = base;
  9076. }
  9077. if (intel_crtc->cursor_size != size) {
  9078. I915_WRITE(CURSIZE, size);
  9079. intel_crtc->cursor_size = size;
  9080. }
  9081. if (intel_crtc->cursor_cntl != cntl) {
  9082. I915_WRITE(CURCNTR(PIPE_A), cntl);
  9083. POSTING_READ(CURCNTR(PIPE_A));
  9084. intel_crtc->cursor_cntl = cntl;
  9085. }
  9086. }
  9087. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
  9088. const struct intel_plane_state *plane_state)
  9089. {
  9090. struct drm_device *dev = crtc->dev;
  9091. struct drm_i915_private *dev_priv = to_i915(dev);
  9092. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9093. int pipe = intel_crtc->pipe;
  9094. uint32_t cntl = 0;
  9095. if (plane_state && plane_state->base.visible) {
  9096. cntl = MCURSOR_GAMMA_ENABLE;
  9097. switch (plane_state->base.crtc_w) {
  9098. case 64:
  9099. cntl |= CURSOR_MODE_64_ARGB_AX;
  9100. break;
  9101. case 128:
  9102. cntl |= CURSOR_MODE_128_ARGB_AX;
  9103. break;
  9104. case 256:
  9105. cntl |= CURSOR_MODE_256_ARGB_AX;
  9106. break;
  9107. default:
  9108. MISSING_CASE(plane_state->base.crtc_w);
  9109. return;
  9110. }
  9111. cntl |= pipe << 28; /* Connect to correct pipe */
  9112. if (HAS_DDI(dev_priv))
  9113. cntl |= CURSOR_PIPE_CSC_ENABLE;
  9114. if (plane_state->base.rotation & DRM_ROTATE_180)
  9115. cntl |= CURSOR_ROTATE_180;
  9116. }
  9117. if (intel_crtc->cursor_cntl != cntl) {
  9118. I915_WRITE(CURCNTR(pipe), cntl);
  9119. POSTING_READ(CURCNTR(pipe));
  9120. intel_crtc->cursor_cntl = cntl;
  9121. }
  9122. /* and commit changes on next vblank */
  9123. I915_WRITE(CURBASE(pipe), base);
  9124. POSTING_READ(CURBASE(pipe));
  9125. intel_crtc->cursor_base = base;
  9126. }
  9127. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  9128. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  9129. const struct intel_plane_state *plane_state)
  9130. {
  9131. struct drm_device *dev = crtc->dev;
  9132. struct drm_i915_private *dev_priv = to_i915(dev);
  9133. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9134. int pipe = intel_crtc->pipe;
  9135. u32 base = intel_crtc->cursor_addr;
  9136. u32 pos = 0;
  9137. if (plane_state) {
  9138. int x = plane_state->base.crtc_x;
  9139. int y = plane_state->base.crtc_y;
  9140. if (x < 0) {
  9141. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  9142. x = -x;
  9143. }
  9144. pos |= x << CURSOR_X_SHIFT;
  9145. if (y < 0) {
  9146. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  9147. y = -y;
  9148. }
  9149. pos |= y << CURSOR_Y_SHIFT;
  9150. /* ILK+ do this automagically */
  9151. if (HAS_GMCH_DISPLAY(dev_priv) &&
  9152. plane_state->base.rotation & DRM_ROTATE_180) {
  9153. base += (plane_state->base.crtc_h *
  9154. plane_state->base.crtc_w - 1) * 4;
  9155. }
  9156. }
  9157. I915_WRITE(CURPOS(pipe), pos);
  9158. if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  9159. i845_update_cursor(crtc, base, plane_state);
  9160. else
  9161. i9xx_update_cursor(crtc, base, plane_state);
  9162. }
  9163. static bool cursor_size_ok(struct drm_i915_private *dev_priv,
  9164. uint32_t width, uint32_t height)
  9165. {
  9166. if (width == 0 || height == 0)
  9167. return false;
  9168. /*
  9169. * 845g/865g are special in that they are only limited by
  9170. * the width of their cursors, the height is arbitrary up to
  9171. * the precision of the register. Everything else requires
  9172. * square cursors, limited to a few power-of-two sizes.
  9173. */
  9174. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  9175. if ((width & 63) != 0)
  9176. return false;
  9177. if (width > (IS_I845G(dev_priv) ? 64 : 512))
  9178. return false;
  9179. if (height > 1023)
  9180. return false;
  9181. } else {
  9182. switch (width | height) {
  9183. case 256:
  9184. case 128:
  9185. if (IS_GEN2(dev_priv))
  9186. return false;
  9187. case 64:
  9188. break;
  9189. default:
  9190. return false;
  9191. }
  9192. }
  9193. return true;
  9194. }
  9195. /* VESA 640x480x72Hz mode to set on the pipe */
  9196. static struct drm_display_mode load_detect_mode = {
  9197. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  9198. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  9199. };
  9200. struct drm_framebuffer *
  9201. __intel_framebuffer_create(struct drm_device *dev,
  9202. struct drm_mode_fb_cmd2 *mode_cmd,
  9203. struct drm_i915_gem_object *obj)
  9204. {
  9205. struct intel_framebuffer *intel_fb;
  9206. int ret;
  9207. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  9208. if (!intel_fb)
  9209. return ERR_PTR(-ENOMEM);
  9210. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  9211. if (ret)
  9212. goto err;
  9213. return &intel_fb->base;
  9214. err:
  9215. kfree(intel_fb);
  9216. return ERR_PTR(ret);
  9217. }
  9218. static struct drm_framebuffer *
  9219. intel_framebuffer_create(struct drm_device *dev,
  9220. struct drm_mode_fb_cmd2 *mode_cmd,
  9221. struct drm_i915_gem_object *obj)
  9222. {
  9223. struct drm_framebuffer *fb;
  9224. int ret;
  9225. ret = i915_mutex_lock_interruptible(dev);
  9226. if (ret)
  9227. return ERR_PTR(ret);
  9228. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  9229. mutex_unlock(&dev->struct_mutex);
  9230. return fb;
  9231. }
  9232. static u32
  9233. intel_framebuffer_pitch_for_width(int width, int bpp)
  9234. {
  9235. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  9236. return ALIGN(pitch, 64);
  9237. }
  9238. static u32
  9239. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  9240. {
  9241. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  9242. return PAGE_ALIGN(pitch * mode->vdisplay);
  9243. }
  9244. static struct drm_framebuffer *
  9245. intel_framebuffer_create_for_mode(struct drm_device *dev,
  9246. struct drm_display_mode *mode,
  9247. int depth, int bpp)
  9248. {
  9249. struct drm_framebuffer *fb;
  9250. struct drm_i915_gem_object *obj;
  9251. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  9252. obj = i915_gem_object_create(to_i915(dev),
  9253. intel_framebuffer_size_for_mode(mode, bpp));
  9254. if (IS_ERR(obj))
  9255. return ERR_CAST(obj);
  9256. mode_cmd.width = mode->hdisplay;
  9257. mode_cmd.height = mode->vdisplay;
  9258. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  9259. bpp);
  9260. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  9261. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  9262. if (IS_ERR(fb))
  9263. i915_gem_object_put(obj);
  9264. return fb;
  9265. }
  9266. static struct drm_framebuffer *
  9267. mode_fits_in_fbdev(struct drm_device *dev,
  9268. struct drm_display_mode *mode)
  9269. {
  9270. #ifdef CONFIG_DRM_FBDEV_EMULATION
  9271. struct drm_i915_private *dev_priv = to_i915(dev);
  9272. struct drm_i915_gem_object *obj;
  9273. struct drm_framebuffer *fb;
  9274. if (!dev_priv->fbdev)
  9275. return NULL;
  9276. if (!dev_priv->fbdev->fb)
  9277. return NULL;
  9278. obj = dev_priv->fbdev->fb->obj;
  9279. BUG_ON(!obj);
  9280. fb = &dev_priv->fbdev->fb->base;
  9281. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  9282. fb->bits_per_pixel))
  9283. return NULL;
  9284. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  9285. return NULL;
  9286. drm_framebuffer_reference(fb);
  9287. return fb;
  9288. #else
  9289. return NULL;
  9290. #endif
  9291. }
  9292. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  9293. struct drm_crtc *crtc,
  9294. struct drm_display_mode *mode,
  9295. struct drm_framebuffer *fb,
  9296. int x, int y)
  9297. {
  9298. struct drm_plane_state *plane_state;
  9299. int hdisplay, vdisplay;
  9300. int ret;
  9301. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  9302. if (IS_ERR(plane_state))
  9303. return PTR_ERR(plane_state);
  9304. if (mode)
  9305. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  9306. else
  9307. hdisplay = vdisplay = 0;
  9308. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  9309. if (ret)
  9310. return ret;
  9311. drm_atomic_set_fb_for_plane(plane_state, fb);
  9312. plane_state->crtc_x = 0;
  9313. plane_state->crtc_y = 0;
  9314. plane_state->crtc_w = hdisplay;
  9315. plane_state->crtc_h = vdisplay;
  9316. plane_state->src_x = x << 16;
  9317. plane_state->src_y = y << 16;
  9318. plane_state->src_w = hdisplay << 16;
  9319. plane_state->src_h = vdisplay << 16;
  9320. return 0;
  9321. }
  9322. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  9323. struct drm_display_mode *mode,
  9324. struct intel_load_detect_pipe *old,
  9325. struct drm_modeset_acquire_ctx *ctx)
  9326. {
  9327. struct intel_crtc *intel_crtc;
  9328. struct intel_encoder *intel_encoder =
  9329. intel_attached_encoder(connector);
  9330. struct drm_crtc *possible_crtc;
  9331. struct drm_encoder *encoder = &intel_encoder->base;
  9332. struct drm_crtc *crtc = NULL;
  9333. struct drm_device *dev = encoder->dev;
  9334. struct drm_i915_private *dev_priv = to_i915(dev);
  9335. struct drm_framebuffer *fb;
  9336. struct drm_mode_config *config = &dev->mode_config;
  9337. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  9338. struct drm_connector_state *connector_state;
  9339. struct intel_crtc_state *crtc_state;
  9340. int ret, i = -1;
  9341. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  9342. connector->base.id, connector->name,
  9343. encoder->base.id, encoder->name);
  9344. old->restore_state = NULL;
  9345. retry:
  9346. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  9347. if (ret)
  9348. goto fail;
  9349. /*
  9350. * Algorithm gets a little messy:
  9351. *
  9352. * - if the connector already has an assigned crtc, use it (but make
  9353. * sure it's on first)
  9354. *
  9355. * - try to find the first unused crtc that can drive this connector,
  9356. * and use that if we find one
  9357. */
  9358. /* See if we already have a CRTC for this connector */
  9359. if (connector->state->crtc) {
  9360. crtc = connector->state->crtc;
  9361. ret = drm_modeset_lock(&crtc->mutex, ctx);
  9362. if (ret)
  9363. goto fail;
  9364. /* Make sure the crtc and connector are running */
  9365. goto found;
  9366. }
  9367. /* Find an unused one (if possible) */
  9368. for_each_crtc(dev, possible_crtc) {
  9369. i++;
  9370. if (!(encoder->possible_crtcs & (1 << i)))
  9371. continue;
  9372. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  9373. if (ret)
  9374. goto fail;
  9375. if (possible_crtc->state->enable) {
  9376. drm_modeset_unlock(&possible_crtc->mutex);
  9377. continue;
  9378. }
  9379. crtc = possible_crtc;
  9380. break;
  9381. }
  9382. /*
  9383. * If we didn't find an unused CRTC, don't use any.
  9384. */
  9385. if (!crtc) {
  9386. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  9387. goto fail;
  9388. }
  9389. found:
  9390. intel_crtc = to_intel_crtc(crtc);
  9391. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  9392. if (ret)
  9393. goto fail;
  9394. state = drm_atomic_state_alloc(dev);
  9395. restore_state = drm_atomic_state_alloc(dev);
  9396. if (!state || !restore_state) {
  9397. ret = -ENOMEM;
  9398. goto fail;
  9399. }
  9400. state->acquire_ctx = ctx;
  9401. restore_state->acquire_ctx = ctx;
  9402. connector_state = drm_atomic_get_connector_state(state, connector);
  9403. if (IS_ERR(connector_state)) {
  9404. ret = PTR_ERR(connector_state);
  9405. goto fail;
  9406. }
  9407. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  9408. if (ret)
  9409. goto fail;
  9410. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  9411. if (IS_ERR(crtc_state)) {
  9412. ret = PTR_ERR(crtc_state);
  9413. goto fail;
  9414. }
  9415. crtc_state->base.active = crtc_state->base.enable = true;
  9416. if (!mode)
  9417. mode = &load_detect_mode;
  9418. /* We need a framebuffer large enough to accommodate all accesses
  9419. * that the plane may generate whilst we perform load detection.
  9420. * We can not rely on the fbcon either being present (we get called
  9421. * during its initialisation to detect all boot displays, or it may
  9422. * not even exist) or that it is large enough to satisfy the
  9423. * requested mode.
  9424. */
  9425. fb = mode_fits_in_fbdev(dev, mode);
  9426. if (fb == NULL) {
  9427. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  9428. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  9429. } else
  9430. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  9431. if (IS_ERR(fb)) {
  9432. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  9433. goto fail;
  9434. }
  9435. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  9436. if (ret)
  9437. goto fail;
  9438. drm_framebuffer_unreference(fb);
  9439. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  9440. if (ret)
  9441. goto fail;
  9442. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  9443. if (!ret)
  9444. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  9445. if (!ret)
  9446. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
  9447. if (ret) {
  9448. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  9449. goto fail;
  9450. }
  9451. ret = drm_atomic_commit(state);
  9452. if (ret) {
  9453. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  9454. goto fail;
  9455. }
  9456. old->restore_state = restore_state;
  9457. /* let the connector get through one full cycle before testing */
  9458. intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
  9459. return true;
  9460. fail:
  9461. if (state) {
  9462. drm_atomic_state_put(state);
  9463. state = NULL;
  9464. }
  9465. if (restore_state) {
  9466. drm_atomic_state_put(restore_state);
  9467. restore_state = NULL;
  9468. }
  9469. if (ret == -EDEADLK) {
  9470. drm_modeset_backoff(ctx);
  9471. goto retry;
  9472. }
  9473. return false;
  9474. }
  9475. void intel_release_load_detect_pipe(struct drm_connector *connector,
  9476. struct intel_load_detect_pipe *old,
  9477. struct drm_modeset_acquire_ctx *ctx)
  9478. {
  9479. struct intel_encoder *intel_encoder =
  9480. intel_attached_encoder(connector);
  9481. struct drm_encoder *encoder = &intel_encoder->base;
  9482. struct drm_atomic_state *state = old->restore_state;
  9483. int ret;
  9484. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  9485. connector->base.id, connector->name,
  9486. encoder->base.id, encoder->name);
  9487. if (!state)
  9488. return;
  9489. ret = drm_atomic_commit(state);
  9490. if (ret)
  9491. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  9492. drm_atomic_state_put(state);
  9493. }
  9494. static int i9xx_pll_refclk(struct drm_device *dev,
  9495. const struct intel_crtc_state *pipe_config)
  9496. {
  9497. struct drm_i915_private *dev_priv = to_i915(dev);
  9498. u32 dpll = pipe_config->dpll_hw_state.dpll;
  9499. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  9500. return dev_priv->vbt.lvds_ssc_freq;
  9501. else if (HAS_PCH_SPLIT(dev_priv))
  9502. return 120000;
  9503. else if (!IS_GEN2(dev_priv))
  9504. return 96000;
  9505. else
  9506. return 48000;
  9507. }
  9508. /* Returns the clock of the currently programmed mode of the given pipe. */
  9509. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  9510. struct intel_crtc_state *pipe_config)
  9511. {
  9512. struct drm_device *dev = crtc->base.dev;
  9513. struct drm_i915_private *dev_priv = to_i915(dev);
  9514. int pipe = pipe_config->cpu_transcoder;
  9515. u32 dpll = pipe_config->dpll_hw_state.dpll;
  9516. u32 fp;
  9517. struct dpll clock;
  9518. int port_clock;
  9519. int refclk = i9xx_pll_refclk(dev, pipe_config);
  9520. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  9521. fp = pipe_config->dpll_hw_state.fp0;
  9522. else
  9523. fp = pipe_config->dpll_hw_state.fp1;
  9524. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  9525. if (IS_PINEVIEW(dev_priv)) {
  9526. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  9527. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  9528. } else {
  9529. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  9530. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  9531. }
  9532. if (!IS_GEN2(dev_priv)) {
  9533. if (IS_PINEVIEW(dev_priv))
  9534. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  9535. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  9536. else
  9537. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  9538. DPLL_FPA01_P1_POST_DIV_SHIFT);
  9539. switch (dpll & DPLL_MODE_MASK) {
  9540. case DPLLB_MODE_DAC_SERIAL:
  9541. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  9542. 5 : 10;
  9543. break;
  9544. case DPLLB_MODE_LVDS:
  9545. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  9546. 7 : 14;
  9547. break;
  9548. default:
  9549. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  9550. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  9551. return;
  9552. }
  9553. if (IS_PINEVIEW(dev_priv))
  9554. port_clock = pnv_calc_dpll_params(refclk, &clock);
  9555. else
  9556. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  9557. } else {
  9558. u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
  9559. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  9560. if (is_lvds) {
  9561. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  9562. DPLL_FPA01_P1_POST_DIV_SHIFT);
  9563. if (lvds & LVDS_CLKB_POWER_UP)
  9564. clock.p2 = 7;
  9565. else
  9566. clock.p2 = 14;
  9567. } else {
  9568. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  9569. clock.p1 = 2;
  9570. else {
  9571. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  9572. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  9573. }
  9574. if (dpll & PLL_P2_DIVIDE_BY_4)
  9575. clock.p2 = 4;
  9576. else
  9577. clock.p2 = 2;
  9578. }
  9579. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  9580. }
  9581. /*
  9582. * This value includes pixel_multiplier. We will use
  9583. * port_clock to compute adjusted_mode.crtc_clock in the
  9584. * encoder's get_config() function.
  9585. */
  9586. pipe_config->port_clock = port_clock;
  9587. }
  9588. int intel_dotclock_calculate(int link_freq,
  9589. const struct intel_link_m_n *m_n)
  9590. {
  9591. /*
  9592. * The calculation for the data clock is:
  9593. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  9594. * But we want to avoid losing precison if possible, so:
  9595. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  9596. *
  9597. * and the link clock is simpler:
  9598. * link_clock = (m * link_clock) / n
  9599. */
  9600. if (!m_n->link_n)
  9601. return 0;
  9602. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  9603. }
  9604. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  9605. struct intel_crtc_state *pipe_config)
  9606. {
  9607. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9608. /* read out port_clock from the DPLL */
  9609. i9xx_crtc_clock_get(crtc, pipe_config);
  9610. /*
  9611. * In case there is an active pipe without active ports,
  9612. * we may need some idea for the dotclock anyway.
  9613. * Calculate one based on the FDI configuration.
  9614. */
  9615. pipe_config->base.adjusted_mode.crtc_clock =
  9616. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9617. &pipe_config->fdi_m_n);
  9618. }
  9619. /** Returns the currently programmed mode of the given pipe. */
  9620. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  9621. struct drm_crtc *crtc)
  9622. {
  9623. struct drm_i915_private *dev_priv = to_i915(dev);
  9624. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9625. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  9626. struct drm_display_mode *mode;
  9627. struct intel_crtc_state *pipe_config;
  9628. int htot = I915_READ(HTOTAL(cpu_transcoder));
  9629. int hsync = I915_READ(HSYNC(cpu_transcoder));
  9630. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  9631. int vsync = I915_READ(VSYNC(cpu_transcoder));
  9632. enum pipe pipe = intel_crtc->pipe;
  9633. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  9634. if (!mode)
  9635. return NULL;
  9636. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  9637. if (!pipe_config) {
  9638. kfree(mode);
  9639. return NULL;
  9640. }
  9641. /*
  9642. * Construct a pipe_config sufficient for getting the clock info
  9643. * back out of crtc_clock_get.
  9644. *
  9645. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  9646. * to use a real value here instead.
  9647. */
  9648. pipe_config->cpu_transcoder = (enum transcoder) pipe;
  9649. pipe_config->pixel_multiplier = 1;
  9650. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  9651. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  9652. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  9653. i9xx_crtc_clock_get(intel_crtc, pipe_config);
  9654. mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
  9655. mode->hdisplay = (htot & 0xffff) + 1;
  9656. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  9657. mode->hsync_start = (hsync & 0xffff) + 1;
  9658. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  9659. mode->vdisplay = (vtot & 0xffff) + 1;
  9660. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  9661. mode->vsync_start = (vsync & 0xffff) + 1;
  9662. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  9663. drm_mode_set_name(mode);
  9664. kfree(pipe_config);
  9665. return mode;
  9666. }
  9667. static void intel_crtc_destroy(struct drm_crtc *crtc)
  9668. {
  9669. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9670. struct drm_device *dev = crtc->dev;
  9671. struct intel_flip_work *work;
  9672. spin_lock_irq(&dev->event_lock);
  9673. work = intel_crtc->flip_work;
  9674. intel_crtc->flip_work = NULL;
  9675. spin_unlock_irq(&dev->event_lock);
  9676. if (work) {
  9677. cancel_work_sync(&work->mmio_work);
  9678. cancel_work_sync(&work->unpin_work);
  9679. kfree(work);
  9680. }
  9681. drm_crtc_cleanup(crtc);
  9682. kfree(intel_crtc);
  9683. }
  9684. static void intel_unpin_work_fn(struct work_struct *__work)
  9685. {
  9686. struct intel_flip_work *work =
  9687. container_of(__work, struct intel_flip_work, unpin_work);
  9688. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  9689. struct drm_device *dev = crtc->base.dev;
  9690. struct drm_plane *primary = crtc->base.primary;
  9691. if (is_mmio_work(work))
  9692. flush_work(&work->mmio_work);
  9693. mutex_lock(&dev->struct_mutex);
  9694. intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
  9695. i915_gem_object_put(work->pending_flip_obj);
  9696. mutex_unlock(&dev->struct_mutex);
  9697. i915_gem_request_put(work->flip_queued_req);
  9698. intel_frontbuffer_flip_complete(to_i915(dev),
  9699. to_intel_plane(primary)->frontbuffer_bit);
  9700. intel_fbc_post_update(crtc);
  9701. drm_framebuffer_unreference(work->old_fb);
  9702. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  9703. atomic_dec(&crtc->unpin_work_count);
  9704. kfree(work);
  9705. }
  9706. /* Is 'a' after or equal to 'b'? */
  9707. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9708. {
  9709. return !((a - b) & 0x80000000);
  9710. }
  9711. static bool __pageflip_finished_cs(struct intel_crtc *crtc,
  9712. struct intel_flip_work *work)
  9713. {
  9714. struct drm_device *dev = crtc->base.dev;
  9715. struct drm_i915_private *dev_priv = to_i915(dev);
  9716. if (abort_flip_on_reset(crtc))
  9717. return true;
  9718. /*
  9719. * The relevant registers doen't exist on pre-ctg.
  9720. * As the flip done interrupt doesn't trigger for mmio
  9721. * flips on gmch platforms, a flip count check isn't
  9722. * really needed there. But since ctg has the registers,
  9723. * include it in the check anyway.
  9724. */
  9725. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  9726. return true;
  9727. /*
  9728. * BDW signals flip done immediately if the plane
  9729. * is disabled, even if the plane enable is already
  9730. * armed to occur at the next vblank :(
  9731. */
  9732. /*
  9733. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9734. * used the same base address. In that case the mmio flip might
  9735. * have completed, but the CS hasn't even executed the flip yet.
  9736. *
  9737. * A flip count check isn't enough as the CS might have updated
  9738. * the base address just after start of vblank, but before we
  9739. * managed to process the interrupt. This means we'd complete the
  9740. * CS flip too soon.
  9741. *
  9742. * Combining both checks should get us a good enough result. It may
  9743. * still happen that the CS flip has been executed, but has not
  9744. * yet actually completed. But in case the base address is the same
  9745. * anyway, we don't really care.
  9746. */
  9747. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9748. crtc->flip_work->gtt_offset &&
  9749. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
  9750. crtc->flip_work->flip_count);
  9751. }
  9752. static bool
  9753. __pageflip_finished_mmio(struct intel_crtc *crtc,
  9754. struct intel_flip_work *work)
  9755. {
  9756. /*
  9757. * MMIO work completes when vblank is different from
  9758. * flip_queued_vblank.
  9759. *
  9760. * Reset counter value doesn't matter, this is handled by
  9761. * i915_wait_request finishing early, so no need to handle
  9762. * reset here.
  9763. */
  9764. return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
  9765. }
  9766. static bool pageflip_finished(struct intel_crtc *crtc,
  9767. struct intel_flip_work *work)
  9768. {
  9769. if (!atomic_read(&work->pending))
  9770. return false;
  9771. smp_rmb();
  9772. if (is_mmio_work(work))
  9773. return __pageflip_finished_mmio(crtc, work);
  9774. else
  9775. return __pageflip_finished_cs(crtc, work);
  9776. }
  9777. void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
  9778. {
  9779. struct drm_device *dev = &dev_priv->drm;
  9780. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  9781. struct intel_flip_work *work;
  9782. unsigned long flags;
  9783. /* Ignore early vblank irqs */
  9784. if (!crtc)
  9785. return;
  9786. /*
  9787. * This is called both by irq handlers and the reset code (to complete
  9788. * lost pageflips) so needs the full irqsave spinlocks.
  9789. */
  9790. spin_lock_irqsave(&dev->event_lock, flags);
  9791. work = crtc->flip_work;
  9792. if (work != NULL &&
  9793. !is_mmio_work(work) &&
  9794. pageflip_finished(crtc, work))
  9795. page_flip_completed(crtc);
  9796. spin_unlock_irqrestore(&dev->event_lock, flags);
  9797. }
  9798. void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
  9799. {
  9800. struct drm_device *dev = &dev_priv->drm;
  9801. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  9802. struct intel_flip_work *work;
  9803. unsigned long flags;
  9804. /* Ignore early vblank irqs */
  9805. if (!crtc)
  9806. return;
  9807. /*
  9808. * This is called both by irq handlers and the reset code (to complete
  9809. * lost pageflips) so needs the full irqsave spinlocks.
  9810. */
  9811. spin_lock_irqsave(&dev->event_lock, flags);
  9812. work = crtc->flip_work;
  9813. if (work != NULL &&
  9814. is_mmio_work(work) &&
  9815. pageflip_finished(crtc, work))
  9816. page_flip_completed(crtc);
  9817. spin_unlock_irqrestore(&dev->event_lock, flags);
  9818. }
  9819. static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
  9820. struct intel_flip_work *work)
  9821. {
  9822. work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
  9823. /* Ensure that the work item is consistent when activating it ... */
  9824. smp_mb__before_atomic();
  9825. atomic_set(&work->pending, 1);
  9826. }
  9827. static int intel_gen2_queue_flip(struct drm_device *dev,
  9828. struct drm_crtc *crtc,
  9829. struct drm_framebuffer *fb,
  9830. struct drm_i915_gem_object *obj,
  9831. struct drm_i915_gem_request *req,
  9832. uint32_t flags)
  9833. {
  9834. struct intel_ring *ring = req->ring;
  9835. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9836. u32 flip_mask;
  9837. int ret;
  9838. ret = intel_ring_begin(req, 6);
  9839. if (ret)
  9840. return ret;
  9841. /* Can't queue multiple flips, so wait for the previous
  9842. * one to finish before executing the next.
  9843. */
  9844. if (intel_crtc->plane)
  9845. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9846. else
  9847. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9848. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9849. intel_ring_emit(ring, MI_NOOP);
  9850. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9851. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9852. intel_ring_emit(ring, fb->pitches[0]);
  9853. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  9854. intel_ring_emit(ring, 0); /* aux display base address, unused */
  9855. return 0;
  9856. }
  9857. static int intel_gen3_queue_flip(struct drm_device *dev,
  9858. struct drm_crtc *crtc,
  9859. struct drm_framebuffer *fb,
  9860. struct drm_i915_gem_object *obj,
  9861. struct drm_i915_gem_request *req,
  9862. uint32_t flags)
  9863. {
  9864. struct intel_ring *ring = req->ring;
  9865. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9866. u32 flip_mask;
  9867. int ret;
  9868. ret = intel_ring_begin(req, 6);
  9869. if (ret)
  9870. return ret;
  9871. if (intel_crtc->plane)
  9872. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9873. else
  9874. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9875. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9876. intel_ring_emit(ring, MI_NOOP);
  9877. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  9878. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9879. intel_ring_emit(ring, fb->pitches[0]);
  9880. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  9881. intel_ring_emit(ring, MI_NOOP);
  9882. return 0;
  9883. }
  9884. static int intel_gen4_queue_flip(struct drm_device *dev,
  9885. struct drm_crtc *crtc,
  9886. struct drm_framebuffer *fb,
  9887. struct drm_i915_gem_object *obj,
  9888. struct drm_i915_gem_request *req,
  9889. uint32_t flags)
  9890. {
  9891. struct intel_ring *ring = req->ring;
  9892. struct drm_i915_private *dev_priv = to_i915(dev);
  9893. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9894. uint32_t pf, pipesrc;
  9895. int ret;
  9896. ret = intel_ring_begin(req, 4);
  9897. if (ret)
  9898. return ret;
  9899. /* i965+ uses the linear or tiled offsets from the
  9900. * Display Registers (which do not change across a page-flip)
  9901. * so we need only reprogram the base address.
  9902. */
  9903. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9904. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9905. intel_ring_emit(ring, fb->pitches[0]);
  9906. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset |
  9907. intel_fb_modifier_to_tiling(fb->modifier));
  9908. /* XXX Enabling the panel-fitter across page-flip is so far
  9909. * untested on non-native modes, so ignore it for now.
  9910. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9911. */
  9912. pf = 0;
  9913. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9914. intel_ring_emit(ring, pf | pipesrc);
  9915. return 0;
  9916. }
  9917. static int intel_gen6_queue_flip(struct drm_device *dev,
  9918. struct drm_crtc *crtc,
  9919. struct drm_framebuffer *fb,
  9920. struct drm_i915_gem_object *obj,
  9921. struct drm_i915_gem_request *req,
  9922. uint32_t flags)
  9923. {
  9924. struct intel_ring *ring = req->ring;
  9925. struct drm_i915_private *dev_priv = to_i915(dev);
  9926. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9927. uint32_t pf, pipesrc;
  9928. int ret;
  9929. ret = intel_ring_begin(req, 4);
  9930. if (ret)
  9931. return ret;
  9932. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9933. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9934. intel_ring_emit(ring, fb->pitches[0] |
  9935. intel_fb_modifier_to_tiling(fb->modifier));
  9936. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  9937. /* Contrary to the suggestions in the documentation,
  9938. * "Enable Panel Fitter" does not seem to be required when page
  9939. * flipping with a non-native mode, and worse causes a normal
  9940. * modeset to fail.
  9941. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9942. */
  9943. pf = 0;
  9944. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9945. intel_ring_emit(ring, pf | pipesrc);
  9946. return 0;
  9947. }
  9948. static int intel_gen7_queue_flip(struct drm_device *dev,
  9949. struct drm_crtc *crtc,
  9950. struct drm_framebuffer *fb,
  9951. struct drm_i915_gem_object *obj,
  9952. struct drm_i915_gem_request *req,
  9953. uint32_t flags)
  9954. {
  9955. struct drm_i915_private *dev_priv = to_i915(dev);
  9956. struct intel_ring *ring = req->ring;
  9957. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9958. uint32_t plane_bit = 0;
  9959. int len, ret;
  9960. switch (intel_crtc->plane) {
  9961. case PLANE_A:
  9962. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9963. break;
  9964. case PLANE_B:
  9965. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9966. break;
  9967. case PLANE_C:
  9968. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9969. break;
  9970. default:
  9971. WARN_ONCE(1, "unknown plane in flip command\n");
  9972. return -ENODEV;
  9973. }
  9974. len = 4;
  9975. if (req->engine->id == RCS) {
  9976. len += 6;
  9977. /*
  9978. * On Gen 8, SRM is now taking an extra dword to accommodate
  9979. * 48bits addresses, and we need a NOOP for the batch size to
  9980. * stay even.
  9981. */
  9982. if (IS_GEN8(dev_priv))
  9983. len += 2;
  9984. }
  9985. /*
  9986. * BSpec MI_DISPLAY_FLIP for IVB:
  9987. * "The full packet must be contained within the same cache line."
  9988. *
  9989. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9990. * cacheline, if we ever start emitting more commands before
  9991. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9992. * then do the cacheline alignment, and finally emit the
  9993. * MI_DISPLAY_FLIP.
  9994. */
  9995. ret = intel_ring_cacheline_align(req);
  9996. if (ret)
  9997. return ret;
  9998. ret = intel_ring_begin(req, len);
  9999. if (ret)
  10000. return ret;
  10001. /* Unmask the flip-done completion message. Note that the bspec says that
  10002. * we should do this for both the BCS and RCS, and that we must not unmask
  10003. * more than one flip event at any time (or ensure that one flip message
  10004. * can be sent by waiting for flip-done prior to queueing new flips).
  10005. * Experimentation says that BCS works despite DERRMR masking all
  10006. * flip-done completion events and that unmasking all planes at once
  10007. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  10008. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  10009. */
  10010. if (req->engine->id == RCS) {
  10011. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  10012. intel_ring_emit_reg(ring, DERRMR);
  10013. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  10014. DERRMR_PIPEB_PRI_FLIP_DONE |
  10015. DERRMR_PIPEC_PRI_FLIP_DONE));
  10016. if (IS_GEN8(dev_priv))
  10017. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
  10018. MI_SRM_LRM_GLOBAL_GTT);
  10019. else
  10020. intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
  10021. MI_SRM_LRM_GLOBAL_GTT);
  10022. intel_ring_emit_reg(ring, DERRMR);
  10023. intel_ring_emit(ring,
  10024. i915_ggtt_offset(req->engine->scratch) + 256);
  10025. if (IS_GEN8(dev_priv)) {
  10026. intel_ring_emit(ring, 0);
  10027. intel_ring_emit(ring, MI_NOOP);
  10028. }
  10029. }
  10030. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  10031. intel_ring_emit(ring, fb->pitches[0] |
  10032. intel_fb_modifier_to_tiling(fb->modifier));
  10033. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  10034. intel_ring_emit(ring, (MI_NOOP));
  10035. return 0;
  10036. }
  10037. static bool use_mmio_flip(struct intel_engine_cs *engine,
  10038. struct drm_i915_gem_object *obj)
  10039. {
  10040. /*
  10041. * This is not being used for older platforms, because
  10042. * non-availability of flip done interrupt forces us to use
  10043. * CS flips. Older platforms derive flip done using some clever
  10044. * tricks involving the flip_pending status bits and vblank irqs.
  10045. * So using MMIO flips there would disrupt this mechanism.
  10046. */
  10047. if (engine == NULL)
  10048. return true;
  10049. if (INTEL_GEN(engine->i915) < 5)
  10050. return false;
  10051. if (i915.use_mmio_flip < 0)
  10052. return false;
  10053. else if (i915.use_mmio_flip > 0)
  10054. return true;
  10055. else if (i915.enable_execlists)
  10056. return true;
  10057. return engine != i915_gem_object_last_write_engine(obj);
  10058. }
  10059. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
  10060. unsigned int rotation,
  10061. struct intel_flip_work *work)
  10062. {
  10063. struct drm_device *dev = intel_crtc->base.dev;
  10064. struct drm_i915_private *dev_priv = to_i915(dev);
  10065. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  10066. const enum pipe pipe = intel_crtc->pipe;
  10067. u32 ctl, stride = skl_plane_stride(fb, 0, rotation);
  10068. ctl = I915_READ(PLANE_CTL(pipe, 0));
  10069. ctl &= ~PLANE_CTL_TILED_MASK;
  10070. switch (fb->modifier) {
  10071. case DRM_FORMAT_MOD_NONE:
  10072. break;
  10073. case I915_FORMAT_MOD_X_TILED:
  10074. ctl |= PLANE_CTL_TILED_X;
  10075. break;
  10076. case I915_FORMAT_MOD_Y_TILED:
  10077. ctl |= PLANE_CTL_TILED_Y;
  10078. break;
  10079. case I915_FORMAT_MOD_Yf_TILED:
  10080. ctl |= PLANE_CTL_TILED_YF;
  10081. break;
  10082. default:
  10083. MISSING_CASE(fb->modifier);
  10084. }
  10085. /*
  10086. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  10087. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  10088. */
  10089. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  10090. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  10091. I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
  10092. POSTING_READ(PLANE_SURF(pipe, 0));
  10093. }
  10094. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
  10095. struct intel_flip_work *work)
  10096. {
  10097. struct drm_device *dev = intel_crtc->base.dev;
  10098. struct drm_i915_private *dev_priv = to_i915(dev);
  10099. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  10100. i915_reg_t reg = DSPCNTR(intel_crtc->plane);
  10101. u32 dspcntr;
  10102. dspcntr = I915_READ(reg);
  10103. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  10104. dspcntr |= DISPPLANE_TILED;
  10105. else
  10106. dspcntr &= ~DISPPLANE_TILED;
  10107. I915_WRITE(reg, dspcntr);
  10108. I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
  10109. POSTING_READ(DSPSURF(intel_crtc->plane));
  10110. }
  10111. static void intel_mmio_flip_work_func(struct work_struct *w)
  10112. {
  10113. struct intel_flip_work *work =
  10114. container_of(w, struct intel_flip_work, mmio_work);
  10115. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  10116. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  10117. struct intel_framebuffer *intel_fb =
  10118. to_intel_framebuffer(crtc->base.primary->fb);
  10119. struct drm_i915_gem_object *obj = intel_fb->obj;
  10120. WARN_ON(i915_gem_object_wait(obj, 0, MAX_SCHEDULE_TIMEOUT, NULL) < 0);
  10121. intel_pipe_update_start(crtc);
  10122. if (INTEL_GEN(dev_priv) >= 9)
  10123. skl_do_mmio_flip(crtc, work->rotation, work);
  10124. else
  10125. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  10126. ilk_do_mmio_flip(crtc, work);
  10127. intel_pipe_update_end(crtc, work);
  10128. }
  10129. static int intel_default_queue_flip(struct drm_device *dev,
  10130. struct drm_crtc *crtc,
  10131. struct drm_framebuffer *fb,
  10132. struct drm_i915_gem_object *obj,
  10133. struct drm_i915_gem_request *req,
  10134. uint32_t flags)
  10135. {
  10136. return -ENODEV;
  10137. }
  10138. static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
  10139. struct intel_crtc *intel_crtc,
  10140. struct intel_flip_work *work)
  10141. {
  10142. u32 addr, vblank;
  10143. if (!atomic_read(&work->pending))
  10144. return false;
  10145. smp_rmb();
  10146. vblank = intel_crtc_get_vblank_counter(intel_crtc);
  10147. if (work->flip_ready_vblank == 0) {
  10148. if (work->flip_queued_req &&
  10149. !i915_gem_request_completed(work->flip_queued_req))
  10150. return false;
  10151. work->flip_ready_vblank = vblank;
  10152. }
  10153. if (vblank - work->flip_ready_vblank < 3)
  10154. return false;
  10155. /* Potential stall - if we see that the flip has happened,
  10156. * assume a missed interrupt. */
  10157. if (INTEL_GEN(dev_priv) >= 4)
  10158. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  10159. else
  10160. addr = I915_READ(DSPADDR(intel_crtc->plane));
  10161. /* There is a potential issue here with a false positive after a flip
  10162. * to the same address. We could address this by checking for a
  10163. * non-incrementing frame counter.
  10164. */
  10165. return addr == work->gtt_offset;
  10166. }
  10167. void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
  10168. {
  10169. struct drm_device *dev = &dev_priv->drm;
  10170. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  10171. struct intel_flip_work *work;
  10172. WARN_ON(!in_interrupt());
  10173. if (crtc == NULL)
  10174. return;
  10175. spin_lock(&dev->event_lock);
  10176. work = crtc->flip_work;
  10177. if (work != NULL && !is_mmio_work(work) &&
  10178. __pageflip_stall_check_cs(dev_priv, crtc, work)) {
  10179. WARN_ONCE(1,
  10180. "Kicking stuck page flip: queued at %d, now %d\n",
  10181. work->flip_queued_vblank, intel_crtc_get_vblank_counter(crtc));
  10182. page_flip_completed(crtc);
  10183. work = NULL;
  10184. }
  10185. if (work != NULL && !is_mmio_work(work) &&
  10186. intel_crtc_get_vblank_counter(crtc) - work->flip_queued_vblank > 1)
  10187. intel_queue_rps_boost_for_request(work->flip_queued_req);
  10188. spin_unlock(&dev->event_lock);
  10189. }
  10190. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  10191. struct drm_framebuffer *fb,
  10192. struct drm_pending_vblank_event *event,
  10193. uint32_t page_flip_flags)
  10194. {
  10195. struct drm_device *dev = crtc->dev;
  10196. struct drm_i915_private *dev_priv = to_i915(dev);
  10197. struct drm_framebuffer *old_fb = crtc->primary->fb;
  10198. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10199. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10200. struct drm_plane *primary = crtc->primary;
  10201. enum pipe pipe = intel_crtc->pipe;
  10202. struct intel_flip_work *work;
  10203. struct intel_engine_cs *engine;
  10204. bool mmio_flip;
  10205. struct drm_i915_gem_request *request;
  10206. struct i915_vma *vma;
  10207. int ret;
  10208. /*
  10209. * drm_mode_page_flip_ioctl() should already catch this, but double
  10210. * check to be safe. In the future we may enable pageflipping from
  10211. * a disabled primary plane.
  10212. */
  10213. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  10214. return -EBUSY;
  10215. /* Can't change pixel format via MI display flips. */
  10216. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  10217. return -EINVAL;
  10218. /*
  10219. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  10220. * Note that pitch changes could also affect these register.
  10221. */
  10222. if (INTEL_GEN(dev_priv) > 3 &&
  10223. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  10224. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  10225. return -EINVAL;
  10226. if (i915_terminally_wedged(&dev_priv->gpu_error))
  10227. goto out_hang;
  10228. work = kzalloc(sizeof(*work), GFP_KERNEL);
  10229. if (work == NULL)
  10230. return -ENOMEM;
  10231. work->event = event;
  10232. work->crtc = crtc;
  10233. work->old_fb = old_fb;
  10234. INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
  10235. ret = drm_crtc_vblank_get(crtc);
  10236. if (ret)
  10237. goto free_work;
  10238. /* We borrow the event spin lock for protecting flip_work */
  10239. spin_lock_irq(&dev->event_lock);
  10240. if (intel_crtc->flip_work) {
  10241. /* Before declaring the flip queue wedged, check if
  10242. * the hardware completed the operation behind our backs.
  10243. */
  10244. if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
  10245. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  10246. page_flip_completed(intel_crtc);
  10247. } else {
  10248. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  10249. spin_unlock_irq(&dev->event_lock);
  10250. drm_crtc_vblank_put(crtc);
  10251. kfree(work);
  10252. return -EBUSY;
  10253. }
  10254. }
  10255. intel_crtc->flip_work = work;
  10256. spin_unlock_irq(&dev->event_lock);
  10257. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  10258. flush_workqueue(dev_priv->wq);
  10259. /* Reference the objects for the scheduled work. */
  10260. drm_framebuffer_reference(work->old_fb);
  10261. crtc->primary->fb = fb;
  10262. update_state_fb(crtc->primary);
  10263. work->pending_flip_obj = i915_gem_object_get(obj);
  10264. ret = i915_mutex_lock_interruptible(dev);
  10265. if (ret)
  10266. goto cleanup;
  10267. intel_crtc->reset_count = i915_reset_count(&dev_priv->gpu_error);
  10268. if (i915_reset_in_progress_or_wedged(&dev_priv->gpu_error)) {
  10269. ret = -EIO;
  10270. goto unlock;
  10271. }
  10272. atomic_inc(&intel_crtc->unpin_work_count);
  10273. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  10274. work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
  10275. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  10276. engine = dev_priv->engine[BCS];
  10277. if (fb->modifier != old_fb->modifier)
  10278. /* vlv: DISPLAY_FLIP fails to change tiling */
  10279. engine = NULL;
  10280. } else if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
  10281. engine = dev_priv->engine[BCS];
  10282. } else if (INTEL_GEN(dev_priv) >= 7) {
  10283. engine = i915_gem_object_last_write_engine(obj);
  10284. if (engine == NULL || engine->id != RCS)
  10285. engine = dev_priv->engine[BCS];
  10286. } else {
  10287. engine = dev_priv->engine[RCS];
  10288. }
  10289. mmio_flip = use_mmio_flip(engine, obj);
  10290. vma = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  10291. if (IS_ERR(vma)) {
  10292. ret = PTR_ERR(vma);
  10293. goto cleanup_pending;
  10294. }
  10295. work->gtt_offset = intel_fb_gtt_offset(fb, primary->state->rotation);
  10296. work->gtt_offset += intel_crtc->dspaddr_offset;
  10297. work->rotation = crtc->primary->state->rotation;
  10298. /*
  10299. * There's the potential that the next frame will not be compatible with
  10300. * FBC, so we want to call pre_update() before the actual page flip.
  10301. * The problem is that pre_update() caches some information about the fb
  10302. * object, so we want to do this only after the object is pinned. Let's
  10303. * be on the safe side and do this immediately before scheduling the
  10304. * flip.
  10305. */
  10306. intel_fbc_pre_update(intel_crtc, intel_crtc->config,
  10307. to_intel_plane_state(primary->state));
  10308. if (mmio_flip) {
  10309. INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
  10310. queue_work(system_unbound_wq, &work->mmio_work);
  10311. } else {
  10312. request = i915_gem_request_alloc(engine, engine->last_context);
  10313. if (IS_ERR(request)) {
  10314. ret = PTR_ERR(request);
  10315. goto cleanup_unpin;
  10316. }
  10317. ret = i915_gem_request_await_object(request, obj, false);
  10318. if (ret)
  10319. goto cleanup_request;
  10320. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  10321. page_flip_flags);
  10322. if (ret)
  10323. goto cleanup_request;
  10324. intel_mark_page_flip_active(intel_crtc, work);
  10325. work->flip_queued_req = i915_gem_request_get(request);
  10326. i915_add_request_no_flush(request);
  10327. }
  10328. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  10329. i915_gem_track_fb(intel_fb_obj(old_fb), obj,
  10330. to_intel_plane(primary)->frontbuffer_bit);
  10331. mutex_unlock(&dev->struct_mutex);
  10332. intel_frontbuffer_flip_prepare(to_i915(dev),
  10333. to_intel_plane(primary)->frontbuffer_bit);
  10334. trace_i915_flip_request(intel_crtc->plane, obj);
  10335. return 0;
  10336. cleanup_request:
  10337. i915_add_request_no_flush(request);
  10338. cleanup_unpin:
  10339. intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
  10340. cleanup_pending:
  10341. atomic_dec(&intel_crtc->unpin_work_count);
  10342. unlock:
  10343. mutex_unlock(&dev->struct_mutex);
  10344. cleanup:
  10345. crtc->primary->fb = old_fb;
  10346. update_state_fb(crtc->primary);
  10347. i915_gem_object_put(obj);
  10348. drm_framebuffer_unreference(work->old_fb);
  10349. spin_lock_irq(&dev->event_lock);
  10350. intel_crtc->flip_work = NULL;
  10351. spin_unlock_irq(&dev->event_lock);
  10352. drm_crtc_vblank_put(crtc);
  10353. free_work:
  10354. kfree(work);
  10355. if (ret == -EIO) {
  10356. struct drm_atomic_state *state;
  10357. struct drm_plane_state *plane_state;
  10358. out_hang:
  10359. state = drm_atomic_state_alloc(dev);
  10360. if (!state)
  10361. return -ENOMEM;
  10362. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  10363. retry:
  10364. plane_state = drm_atomic_get_plane_state(state, primary);
  10365. ret = PTR_ERR_OR_ZERO(plane_state);
  10366. if (!ret) {
  10367. drm_atomic_set_fb_for_plane(plane_state, fb);
  10368. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  10369. if (!ret)
  10370. ret = drm_atomic_commit(state);
  10371. }
  10372. if (ret == -EDEADLK) {
  10373. drm_modeset_backoff(state->acquire_ctx);
  10374. drm_atomic_state_clear(state);
  10375. goto retry;
  10376. }
  10377. drm_atomic_state_put(state);
  10378. if (ret == 0 && event) {
  10379. spin_lock_irq(&dev->event_lock);
  10380. drm_crtc_send_vblank_event(crtc, event);
  10381. spin_unlock_irq(&dev->event_lock);
  10382. }
  10383. }
  10384. return ret;
  10385. }
  10386. /**
  10387. * intel_wm_need_update - Check whether watermarks need updating
  10388. * @plane: drm plane
  10389. * @state: new plane state
  10390. *
  10391. * Check current plane state versus the new one to determine whether
  10392. * watermarks need to be recalculated.
  10393. *
  10394. * Returns true or false.
  10395. */
  10396. static bool intel_wm_need_update(struct drm_plane *plane,
  10397. struct drm_plane_state *state)
  10398. {
  10399. struct intel_plane_state *new = to_intel_plane_state(state);
  10400. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  10401. /* Update watermarks on tiling or size changes. */
  10402. if (new->base.visible != cur->base.visible)
  10403. return true;
  10404. if (!cur->base.fb || !new->base.fb)
  10405. return false;
  10406. if (cur->base.fb->modifier != new->base.fb->modifier ||
  10407. cur->base.rotation != new->base.rotation ||
  10408. drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
  10409. drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
  10410. drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
  10411. drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
  10412. return true;
  10413. return false;
  10414. }
  10415. static bool needs_scaling(struct intel_plane_state *state)
  10416. {
  10417. int src_w = drm_rect_width(&state->base.src) >> 16;
  10418. int src_h = drm_rect_height(&state->base.src) >> 16;
  10419. int dst_w = drm_rect_width(&state->base.dst);
  10420. int dst_h = drm_rect_height(&state->base.dst);
  10421. return (src_w != dst_w || src_h != dst_h);
  10422. }
  10423. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  10424. struct drm_plane_state *plane_state)
  10425. {
  10426. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  10427. struct drm_crtc *crtc = crtc_state->crtc;
  10428. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10429. struct drm_plane *plane = plane_state->plane;
  10430. struct drm_device *dev = crtc->dev;
  10431. struct drm_i915_private *dev_priv = to_i915(dev);
  10432. struct intel_plane_state *old_plane_state =
  10433. to_intel_plane_state(plane->state);
  10434. bool mode_changed = needs_modeset(crtc_state);
  10435. bool was_crtc_enabled = crtc->state->active;
  10436. bool is_crtc_enabled = crtc_state->active;
  10437. bool turn_off, turn_on, visible, was_visible;
  10438. struct drm_framebuffer *fb = plane_state->fb;
  10439. int ret;
  10440. if (INTEL_GEN(dev_priv) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
  10441. ret = skl_update_scaler_plane(
  10442. to_intel_crtc_state(crtc_state),
  10443. to_intel_plane_state(plane_state));
  10444. if (ret)
  10445. return ret;
  10446. }
  10447. was_visible = old_plane_state->base.visible;
  10448. visible = to_intel_plane_state(plane_state)->base.visible;
  10449. if (!was_crtc_enabled && WARN_ON(was_visible))
  10450. was_visible = false;
  10451. /*
  10452. * Visibility is calculated as if the crtc was on, but
  10453. * after scaler setup everything depends on it being off
  10454. * when the crtc isn't active.
  10455. *
  10456. * FIXME this is wrong for watermarks. Watermarks should also
  10457. * be computed as if the pipe would be active. Perhaps move
  10458. * per-plane wm computation to the .check_plane() hook, and
  10459. * only combine the results from all planes in the current place?
  10460. */
  10461. if (!is_crtc_enabled)
  10462. to_intel_plane_state(plane_state)->base.visible = visible = false;
  10463. if (!was_visible && !visible)
  10464. return 0;
  10465. if (fb != old_plane_state->base.fb)
  10466. pipe_config->fb_changed = true;
  10467. turn_off = was_visible && (!visible || mode_changed);
  10468. turn_on = visible && (!was_visible || mode_changed);
  10469. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  10470. intel_crtc->base.base.id,
  10471. intel_crtc->base.name,
  10472. plane->base.id, plane->name,
  10473. fb ? fb->base.id : -1);
  10474. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  10475. plane->base.id, plane->name,
  10476. was_visible, visible,
  10477. turn_off, turn_on, mode_changed);
  10478. if (turn_on) {
  10479. pipe_config->update_wm_pre = true;
  10480. /* must disable cxsr around plane enable/disable */
  10481. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  10482. pipe_config->disable_cxsr = true;
  10483. } else if (turn_off) {
  10484. pipe_config->update_wm_post = true;
  10485. /* must disable cxsr around plane enable/disable */
  10486. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  10487. pipe_config->disable_cxsr = true;
  10488. } else if (intel_wm_need_update(plane, plane_state)) {
  10489. /* FIXME bollocks */
  10490. pipe_config->update_wm_pre = true;
  10491. pipe_config->update_wm_post = true;
  10492. }
  10493. /* Pre-gen9 platforms need two-step watermark updates */
  10494. if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
  10495. INTEL_GEN(dev_priv) < 9 && dev_priv->display.optimize_watermarks)
  10496. to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
  10497. if (visible || was_visible)
  10498. pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
  10499. /*
  10500. * WaCxSRDisabledForSpriteScaling:ivb
  10501. *
  10502. * cstate->update_wm was already set above, so this flag will
  10503. * take effect when we commit and program watermarks.
  10504. */
  10505. if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev_priv) &&
  10506. needs_scaling(to_intel_plane_state(plane_state)) &&
  10507. !needs_scaling(old_plane_state))
  10508. pipe_config->disable_lp_wm = true;
  10509. return 0;
  10510. }
  10511. static bool encoders_cloneable(const struct intel_encoder *a,
  10512. const struct intel_encoder *b)
  10513. {
  10514. /* masks could be asymmetric, so check both ways */
  10515. return a == b || (a->cloneable & (1 << b->type) &&
  10516. b->cloneable & (1 << a->type));
  10517. }
  10518. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  10519. struct intel_crtc *crtc,
  10520. struct intel_encoder *encoder)
  10521. {
  10522. struct intel_encoder *source_encoder;
  10523. struct drm_connector *connector;
  10524. struct drm_connector_state *connector_state;
  10525. int i;
  10526. for_each_connector_in_state(state, connector, connector_state, i) {
  10527. if (connector_state->crtc != &crtc->base)
  10528. continue;
  10529. source_encoder =
  10530. to_intel_encoder(connector_state->best_encoder);
  10531. if (!encoders_cloneable(encoder, source_encoder))
  10532. return false;
  10533. }
  10534. return true;
  10535. }
  10536. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  10537. struct drm_crtc_state *crtc_state)
  10538. {
  10539. struct drm_device *dev = crtc->dev;
  10540. struct drm_i915_private *dev_priv = to_i915(dev);
  10541. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10542. struct intel_crtc_state *pipe_config =
  10543. to_intel_crtc_state(crtc_state);
  10544. struct drm_atomic_state *state = crtc_state->state;
  10545. int ret;
  10546. bool mode_changed = needs_modeset(crtc_state);
  10547. if (mode_changed && !crtc_state->active)
  10548. pipe_config->update_wm_post = true;
  10549. if (mode_changed && crtc_state->enable &&
  10550. dev_priv->display.crtc_compute_clock &&
  10551. !WARN_ON(pipe_config->shared_dpll)) {
  10552. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  10553. pipe_config);
  10554. if (ret)
  10555. return ret;
  10556. }
  10557. if (crtc_state->color_mgmt_changed) {
  10558. ret = intel_color_check(crtc, crtc_state);
  10559. if (ret)
  10560. return ret;
  10561. /*
  10562. * Changing color management on Intel hardware is
  10563. * handled as part of planes update.
  10564. */
  10565. crtc_state->planes_changed = true;
  10566. }
  10567. ret = 0;
  10568. if (dev_priv->display.compute_pipe_wm) {
  10569. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  10570. if (ret) {
  10571. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  10572. return ret;
  10573. }
  10574. }
  10575. if (dev_priv->display.compute_intermediate_wm &&
  10576. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  10577. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  10578. return 0;
  10579. /*
  10580. * Calculate 'intermediate' watermarks that satisfy both the
  10581. * old state and the new state. We can program these
  10582. * immediately.
  10583. */
  10584. ret = dev_priv->display.compute_intermediate_wm(dev,
  10585. intel_crtc,
  10586. pipe_config);
  10587. if (ret) {
  10588. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  10589. return ret;
  10590. }
  10591. } else if (dev_priv->display.compute_intermediate_wm) {
  10592. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  10593. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  10594. }
  10595. if (INTEL_GEN(dev_priv) >= 9) {
  10596. if (mode_changed)
  10597. ret = skl_update_scaler_crtc(pipe_config);
  10598. if (!ret)
  10599. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  10600. pipe_config);
  10601. }
  10602. return ret;
  10603. }
  10604. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  10605. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  10606. .atomic_begin = intel_begin_crtc_commit,
  10607. .atomic_flush = intel_finish_crtc_commit,
  10608. .atomic_check = intel_crtc_atomic_check,
  10609. };
  10610. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  10611. {
  10612. struct intel_connector *connector;
  10613. for_each_intel_connector(dev, connector) {
  10614. if (connector->base.state->crtc)
  10615. drm_connector_unreference(&connector->base);
  10616. if (connector->base.encoder) {
  10617. connector->base.state->best_encoder =
  10618. connector->base.encoder;
  10619. connector->base.state->crtc =
  10620. connector->base.encoder->crtc;
  10621. drm_connector_reference(&connector->base);
  10622. } else {
  10623. connector->base.state->best_encoder = NULL;
  10624. connector->base.state->crtc = NULL;
  10625. }
  10626. }
  10627. }
  10628. static void
  10629. connected_sink_compute_bpp(struct intel_connector *connector,
  10630. struct intel_crtc_state *pipe_config)
  10631. {
  10632. const struct drm_display_info *info = &connector->base.display_info;
  10633. int bpp = pipe_config->pipe_bpp;
  10634. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  10635. connector->base.base.id,
  10636. connector->base.name);
  10637. /* Don't use an invalid EDID bpc value */
  10638. if (info->bpc != 0 && info->bpc * 3 < bpp) {
  10639. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  10640. bpp, info->bpc * 3);
  10641. pipe_config->pipe_bpp = info->bpc * 3;
  10642. }
  10643. /* Clamp bpp to 8 on screens without EDID 1.4 */
  10644. if (info->bpc == 0 && bpp > 24) {
  10645. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  10646. bpp);
  10647. pipe_config->pipe_bpp = 24;
  10648. }
  10649. }
  10650. static int
  10651. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  10652. struct intel_crtc_state *pipe_config)
  10653. {
  10654. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  10655. struct drm_atomic_state *state;
  10656. struct drm_connector *connector;
  10657. struct drm_connector_state *connector_state;
  10658. int bpp, i;
  10659. if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  10660. IS_CHERRYVIEW(dev_priv)))
  10661. bpp = 10*3;
  10662. else if (INTEL_GEN(dev_priv) >= 5)
  10663. bpp = 12*3;
  10664. else
  10665. bpp = 8*3;
  10666. pipe_config->pipe_bpp = bpp;
  10667. state = pipe_config->base.state;
  10668. /* Clamp display bpp to EDID value */
  10669. for_each_connector_in_state(state, connector, connector_state, i) {
  10670. if (connector_state->crtc != &crtc->base)
  10671. continue;
  10672. connected_sink_compute_bpp(to_intel_connector(connector),
  10673. pipe_config);
  10674. }
  10675. return bpp;
  10676. }
  10677. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  10678. {
  10679. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  10680. "type: 0x%x flags: 0x%x\n",
  10681. mode->crtc_clock,
  10682. mode->crtc_hdisplay, mode->crtc_hsync_start,
  10683. mode->crtc_hsync_end, mode->crtc_htotal,
  10684. mode->crtc_vdisplay, mode->crtc_vsync_start,
  10685. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  10686. }
  10687. static inline void
  10688. intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
  10689. unsigned int lane_count, struct intel_link_m_n *m_n)
  10690. {
  10691. DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10692. id, lane_count,
  10693. m_n->gmch_m, m_n->gmch_n,
  10694. m_n->link_m, m_n->link_n, m_n->tu);
  10695. }
  10696. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  10697. struct intel_crtc_state *pipe_config,
  10698. const char *context)
  10699. {
  10700. struct drm_device *dev = crtc->base.dev;
  10701. struct drm_i915_private *dev_priv = to_i915(dev);
  10702. struct drm_plane *plane;
  10703. struct intel_plane *intel_plane;
  10704. struct intel_plane_state *state;
  10705. struct drm_framebuffer *fb;
  10706. DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
  10707. crtc->base.base.id, crtc->base.name, context);
  10708. DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
  10709. transcoder_name(pipe_config->cpu_transcoder),
  10710. pipe_config->pipe_bpp, pipe_config->dither);
  10711. if (pipe_config->has_pch_encoder)
  10712. intel_dump_m_n_config(pipe_config, "fdi",
  10713. pipe_config->fdi_lanes,
  10714. &pipe_config->fdi_m_n);
  10715. if (intel_crtc_has_dp_encoder(pipe_config)) {
  10716. intel_dump_m_n_config(pipe_config, "dp m_n",
  10717. pipe_config->lane_count, &pipe_config->dp_m_n);
  10718. if (pipe_config->has_drrs)
  10719. intel_dump_m_n_config(pipe_config, "dp m2_n2",
  10720. pipe_config->lane_count,
  10721. &pipe_config->dp_m2_n2);
  10722. }
  10723. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  10724. pipe_config->has_audio, pipe_config->has_infoframe);
  10725. DRM_DEBUG_KMS("requested mode:\n");
  10726. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  10727. DRM_DEBUG_KMS("adjusted mode:\n");
  10728. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  10729. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  10730. DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d\n",
  10731. pipe_config->port_clock,
  10732. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  10733. if (INTEL_GEN(dev_priv) >= 9)
  10734. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  10735. crtc->num_scalers,
  10736. pipe_config->scaler_state.scaler_users,
  10737. pipe_config->scaler_state.scaler_id);
  10738. if (HAS_GMCH_DISPLAY(dev_priv))
  10739. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10740. pipe_config->gmch_pfit.control,
  10741. pipe_config->gmch_pfit.pgm_ratios,
  10742. pipe_config->gmch_pfit.lvds_border_bits);
  10743. else
  10744. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10745. pipe_config->pch_pfit.pos,
  10746. pipe_config->pch_pfit.size,
  10747. enableddisabled(pipe_config->pch_pfit.enabled));
  10748. DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
  10749. pipe_config->ips_enabled, pipe_config->double_wide);
  10750. if (IS_GEN9_LP(dev_priv)) {
  10751. DRM_DEBUG_KMS("dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
  10752. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10753. "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
  10754. pipe_config->dpll_hw_state.ebb0,
  10755. pipe_config->dpll_hw_state.ebb4,
  10756. pipe_config->dpll_hw_state.pll0,
  10757. pipe_config->dpll_hw_state.pll1,
  10758. pipe_config->dpll_hw_state.pll2,
  10759. pipe_config->dpll_hw_state.pll3,
  10760. pipe_config->dpll_hw_state.pll6,
  10761. pipe_config->dpll_hw_state.pll8,
  10762. pipe_config->dpll_hw_state.pll9,
  10763. pipe_config->dpll_hw_state.pll10,
  10764. pipe_config->dpll_hw_state.pcsdw12);
  10765. } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
  10766. DRM_DEBUG_KMS("dpll_hw_state: "
  10767. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10768. pipe_config->dpll_hw_state.ctrl1,
  10769. pipe_config->dpll_hw_state.cfgcr1,
  10770. pipe_config->dpll_hw_state.cfgcr2);
  10771. } else if (HAS_DDI(dev_priv)) {
  10772. DRM_DEBUG_KMS("dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
  10773. pipe_config->dpll_hw_state.wrpll,
  10774. pipe_config->dpll_hw_state.spll);
  10775. } else {
  10776. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10777. "fp0: 0x%x, fp1: 0x%x\n",
  10778. pipe_config->dpll_hw_state.dpll,
  10779. pipe_config->dpll_hw_state.dpll_md,
  10780. pipe_config->dpll_hw_state.fp0,
  10781. pipe_config->dpll_hw_state.fp1);
  10782. }
  10783. DRM_DEBUG_KMS("planes on this crtc\n");
  10784. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10785. struct drm_format_name_buf format_name;
  10786. intel_plane = to_intel_plane(plane);
  10787. if (intel_plane->pipe != crtc->pipe)
  10788. continue;
  10789. state = to_intel_plane_state(plane->state);
  10790. fb = state->base.fb;
  10791. if (!fb) {
  10792. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  10793. plane->base.id, plane->name, state->scaler_id);
  10794. continue;
  10795. }
  10796. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
  10797. plane->base.id, plane->name,
  10798. fb->base.id, fb->width, fb->height,
  10799. drm_get_format_name(fb->pixel_format, &format_name));
  10800. if (INTEL_GEN(dev_priv) >= 9)
  10801. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  10802. state->scaler_id,
  10803. state->base.src.x1 >> 16,
  10804. state->base.src.y1 >> 16,
  10805. drm_rect_width(&state->base.src) >> 16,
  10806. drm_rect_height(&state->base.src) >> 16,
  10807. state->base.dst.x1, state->base.dst.y1,
  10808. drm_rect_width(&state->base.dst),
  10809. drm_rect_height(&state->base.dst));
  10810. }
  10811. }
  10812. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10813. {
  10814. struct drm_device *dev = state->dev;
  10815. struct drm_connector *connector;
  10816. unsigned int used_ports = 0;
  10817. unsigned int used_mst_ports = 0;
  10818. /*
  10819. * Walk the connector list instead of the encoder
  10820. * list to detect the problem on ddi platforms
  10821. * where there's just one encoder per digital port.
  10822. */
  10823. drm_for_each_connector(connector, dev) {
  10824. struct drm_connector_state *connector_state;
  10825. struct intel_encoder *encoder;
  10826. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  10827. if (!connector_state)
  10828. connector_state = connector->state;
  10829. if (!connector_state->best_encoder)
  10830. continue;
  10831. encoder = to_intel_encoder(connector_state->best_encoder);
  10832. WARN_ON(!connector_state->crtc);
  10833. switch (encoder->type) {
  10834. unsigned int port_mask;
  10835. case INTEL_OUTPUT_UNKNOWN:
  10836. if (WARN_ON(!HAS_DDI(to_i915(dev))))
  10837. break;
  10838. case INTEL_OUTPUT_DP:
  10839. case INTEL_OUTPUT_HDMI:
  10840. case INTEL_OUTPUT_EDP:
  10841. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10842. /* the same port mustn't appear more than once */
  10843. if (used_ports & port_mask)
  10844. return false;
  10845. used_ports |= port_mask;
  10846. break;
  10847. case INTEL_OUTPUT_DP_MST:
  10848. used_mst_ports |=
  10849. 1 << enc_to_mst(&encoder->base)->primary->port;
  10850. break;
  10851. default:
  10852. break;
  10853. }
  10854. }
  10855. /* can't mix MST and SST/HDMI on the same port */
  10856. if (used_ports & used_mst_ports)
  10857. return false;
  10858. return true;
  10859. }
  10860. static void
  10861. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10862. {
  10863. struct drm_crtc_state tmp_state;
  10864. struct intel_crtc_scaler_state scaler_state;
  10865. struct intel_dpll_hw_state dpll_hw_state;
  10866. struct intel_shared_dpll *shared_dpll;
  10867. bool force_thru;
  10868. /* FIXME: before the switch to atomic started, a new pipe_config was
  10869. * kzalloc'd. Code that depends on any field being zero should be
  10870. * fixed, so that the crtc_state can be safely duplicated. For now,
  10871. * only fields that are know to not cause problems are preserved. */
  10872. tmp_state = crtc_state->base;
  10873. scaler_state = crtc_state->scaler_state;
  10874. shared_dpll = crtc_state->shared_dpll;
  10875. dpll_hw_state = crtc_state->dpll_hw_state;
  10876. force_thru = crtc_state->pch_pfit.force_thru;
  10877. memset(crtc_state, 0, sizeof *crtc_state);
  10878. crtc_state->base = tmp_state;
  10879. crtc_state->scaler_state = scaler_state;
  10880. crtc_state->shared_dpll = shared_dpll;
  10881. crtc_state->dpll_hw_state = dpll_hw_state;
  10882. crtc_state->pch_pfit.force_thru = force_thru;
  10883. }
  10884. static int
  10885. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10886. struct intel_crtc_state *pipe_config)
  10887. {
  10888. struct drm_atomic_state *state = pipe_config->base.state;
  10889. struct intel_encoder *encoder;
  10890. struct drm_connector *connector;
  10891. struct drm_connector_state *connector_state;
  10892. int base_bpp, ret = -EINVAL;
  10893. int i;
  10894. bool retry = true;
  10895. clear_intel_crtc_state(pipe_config);
  10896. pipe_config->cpu_transcoder =
  10897. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10898. /*
  10899. * Sanitize sync polarity flags based on requested ones. If neither
  10900. * positive or negative polarity is requested, treat this as meaning
  10901. * negative polarity.
  10902. */
  10903. if (!(pipe_config->base.adjusted_mode.flags &
  10904. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10905. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10906. if (!(pipe_config->base.adjusted_mode.flags &
  10907. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10908. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10909. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10910. pipe_config);
  10911. if (base_bpp < 0)
  10912. goto fail;
  10913. /*
  10914. * Determine the real pipe dimensions. Note that stereo modes can
  10915. * increase the actual pipe size due to the frame doubling and
  10916. * insertion of additional space for blanks between the frame. This
  10917. * is stored in the crtc timings. We use the requested mode to do this
  10918. * computation to clearly distinguish it from the adjusted mode, which
  10919. * can be changed by the connectors in the below retry loop.
  10920. */
  10921. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10922. &pipe_config->pipe_src_w,
  10923. &pipe_config->pipe_src_h);
  10924. for_each_connector_in_state(state, connector, connector_state, i) {
  10925. if (connector_state->crtc != crtc)
  10926. continue;
  10927. encoder = to_intel_encoder(connector_state->best_encoder);
  10928. if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
  10929. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  10930. goto fail;
  10931. }
  10932. /*
  10933. * Determine output_types before calling the .compute_config()
  10934. * hooks so that the hooks can use this information safely.
  10935. */
  10936. pipe_config->output_types |= 1 << encoder->type;
  10937. }
  10938. encoder_retry:
  10939. /* Ensure the port clock defaults are reset when retrying. */
  10940. pipe_config->port_clock = 0;
  10941. pipe_config->pixel_multiplier = 1;
  10942. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10943. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10944. CRTC_STEREO_DOUBLE);
  10945. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10946. * adjust it according to limitations or connector properties, and also
  10947. * a chance to reject the mode entirely.
  10948. */
  10949. for_each_connector_in_state(state, connector, connector_state, i) {
  10950. if (connector_state->crtc != crtc)
  10951. continue;
  10952. encoder = to_intel_encoder(connector_state->best_encoder);
  10953. if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
  10954. DRM_DEBUG_KMS("Encoder config failure\n");
  10955. goto fail;
  10956. }
  10957. }
  10958. /* Set default port clock if not overwritten by the encoder. Needs to be
  10959. * done afterwards in case the encoder adjusts the mode. */
  10960. if (!pipe_config->port_clock)
  10961. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10962. * pipe_config->pixel_multiplier;
  10963. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10964. if (ret < 0) {
  10965. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10966. goto fail;
  10967. }
  10968. if (ret == RETRY) {
  10969. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10970. ret = -EINVAL;
  10971. goto fail;
  10972. }
  10973. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10974. retry = false;
  10975. goto encoder_retry;
  10976. }
  10977. /* Dithering seems to not pass-through bits correctly when it should, so
  10978. * only enable it on 6bpc panels. */
  10979. pipe_config->dither = pipe_config->pipe_bpp == 6*3;
  10980. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  10981. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10982. fail:
  10983. return ret;
  10984. }
  10985. static void
  10986. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  10987. {
  10988. struct drm_crtc *crtc;
  10989. struct drm_crtc_state *crtc_state;
  10990. int i;
  10991. /* Double check state. */
  10992. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10993. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10994. /* Update hwmode for vblank functions */
  10995. if (crtc->state->active)
  10996. crtc->hwmode = crtc->state->adjusted_mode;
  10997. else
  10998. crtc->hwmode.crtc_clock = 0;
  10999. /*
  11000. * Update legacy state to satisfy fbc code. This can
  11001. * be removed when fbc uses the atomic state.
  11002. */
  11003. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  11004. struct drm_plane_state *plane_state = crtc->primary->state;
  11005. crtc->primary->fb = plane_state->fb;
  11006. crtc->x = plane_state->src_x >> 16;
  11007. crtc->y = plane_state->src_y >> 16;
  11008. }
  11009. }
  11010. }
  11011. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  11012. {
  11013. int diff;
  11014. if (clock1 == clock2)
  11015. return true;
  11016. if (!clock1 || !clock2)
  11017. return false;
  11018. diff = abs(clock1 - clock2);
  11019. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  11020. return true;
  11021. return false;
  11022. }
  11023. static bool
  11024. intel_compare_m_n(unsigned int m, unsigned int n,
  11025. unsigned int m2, unsigned int n2,
  11026. bool exact)
  11027. {
  11028. if (m == m2 && n == n2)
  11029. return true;
  11030. if (exact || !m || !n || !m2 || !n2)
  11031. return false;
  11032. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  11033. if (n > n2) {
  11034. while (n > n2) {
  11035. m2 <<= 1;
  11036. n2 <<= 1;
  11037. }
  11038. } else if (n < n2) {
  11039. while (n < n2) {
  11040. m <<= 1;
  11041. n <<= 1;
  11042. }
  11043. }
  11044. if (n != n2)
  11045. return false;
  11046. return intel_fuzzy_clock_check(m, m2);
  11047. }
  11048. static bool
  11049. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  11050. struct intel_link_m_n *m2_n2,
  11051. bool adjust)
  11052. {
  11053. if (m_n->tu == m2_n2->tu &&
  11054. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  11055. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  11056. intel_compare_m_n(m_n->link_m, m_n->link_n,
  11057. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  11058. if (adjust)
  11059. *m2_n2 = *m_n;
  11060. return true;
  11061. }
  11062. return false;
  11063. }
  11064. static void __printf(3, 4)
  11065. pipe_config_err(bool adjust, const char *name, const char *format, ...)
  11066. {
  11067. char *level;
  11068. unsigned int category;
  11069. struct va_format vaf;
  11070. va_list args;
  11071. if (adjust) {
  11072. level = KERN_DEBUG;
  11073. category = DRM_UT_KMS;
  11074. } else {
  11075. level = KERN_ERR;
  11076. category = DRM_UT_NONE;
  11077. }
  11078. va_start(args, format);
  11079. vaf.fmt = format;
  11080. vaf.va = &args;
  11081. drm_printk(level, category, "mismatch in %s %pV", name, &vaf);
  11082. va_end(args);
  11083. }
  11084. static bool
  11085. intel_pipe_config_compare(struct drm_i915_private *dev_priv,
  11086. struct intel_crtc_state *current_config,
  11087. struct intel_crtc_state *pipe_config,
  11088. bool adjust)
  11089. {
  11090. bool ret = true;
  11091. #define PIPE_CONF_CHECK_X(name) \
  11092. if (current_config->name != pipe_config->name) { \
  11093. pipe_config_err(adjust, __stringify(name), \
  11094. "(expected 0x%08x, found 0x%08x)\n", \
  11095. current_config->name, \
  11096. pipe_config->name); \
  11097. ret = false; \
  11098. }
  11099. #define PIPE_CONF_CHECK_I(name) \
  11100. if (current_config->name != pipe_config->name) { \
  11101. pipe_config_err(adjust, __stringify(name), \
  11102. "(expected %i, found %i)\n", \
  11103. current_config->name, \
  11104. pipe_config->name); \
  11105. ret = false; \
  11106. }
  11107. #define PIPE_CONF_CHECK_P(name) \
  11108. if (current_config->name != pipe_config->name) { \
  11109. pipe_config_err(adjust, __stringify(name), \
  11110. "(expected %p, found %p)\n", \
  11111. current_config->name, \
  11112. pipe_config->name); \
  11113. ret = false; \
  11114. }
  11115. #define PIPE_CONF_CHECK_M_N(name) \
  11116. if (!intel_compare_link_m_n(&current_config->name, \
  11117. &pipe_config->name,\
  11118. adjust)) { \
  11119. pipe_config_err(adjust, __stringify(name), \
  11120. "(expected tu %i gmch %i/%i link %i/%i, " \
  11121. "found tu %i, gmch %i/%i link %i/%i)\n", \
  11122. current_config->name.tu, \
  11123. current_config->name.gmch_m, \
  11124. current_config->name.gmch_n, \
  11125. current_config->name.link_m, \
  11126. current_config->name.link_n, \
  11127. pipe_config->name.tu, \
  11128. pipe_config->name.gmch_m, \
  11129. pipe_config->name.gmch_n, \
  11130. pipe_config->name.link_m, \
  11131. pipe_config->name.link_n); \
  11132. ret = false; \
  11133. }
  11134. /* This is required for BDW+ where there is only one set of registers for
  11135. * switching between high and low RR.
  11136. * This macro can be used whenever a comparison has to be made between one
  11137. * hw state and multiple sw state variables.
  11138. */
  11139. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  11140. if (!intel_compare_link_m_n(&current_config->name, \
  11141. &pipe_config->name, adjust) && \
  11142. !intel_compare_link_m_n(&current_config->alt_name, \
  11143. &pipe_config->name, adjust)) { \
  11144. pipe_config_err(adjust, __stringify(name), \
  11145. "(expected tu %i gmch %i/%i link %i/%i, " \
  11146. "or tu %i gmch %i/%i link %i/%i, " \
  11147. "found tu %i, gmch %i/%i link %i/%i)\n", \
  11148. current_config->name.tu, \
  11149. current_config->name.gmch_m, \
  11150. current_config->name.gmch_n, \
  11151. current_config->name.link_m, \
  11152. current_config->name.link_n, \
  11153. current_config->alt_name.tu, \
  11154. current_config->alt_name.gmch_m, \
  11155. current_config->alt_name.gmch_n, \
  11156. current_config->alt_name.link_m, \
  11157. current_config->alt_name.link_n, \
  11158. pipe_config->name.tu, \
  11159. pipe_config->name.gmch_m, \
  11160. pipe_config->name.gmch_n, \
  11161. pipe_config->name.link_m, \
  11162. pipe_config->name.link_n); \
  11163. ret = false; \
  11164. }
  11165. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  11166. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  11167. pipe_config_err(adjust, __stringify(name), \
  11168. "(%x) (expected %i, found %i)\n", \
  11169. (mask), \
  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. pipe_config_err(adjust, __stringify(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_GEN(dev_priv) < 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_GEN(dev_priv) < 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. return ret;
  11270. }
  11271. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  11272. const struct intel_crtc_state *pipe_config)
  11273. {
  11274. if (pipe_config->has_pch_encoder) {
  11275. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  11276. &pipe_config->fdi_m_n);
  11277. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  11278. /*
  11279. * FDI already provided one idea for the dotclock.
  11280. * Yell if the encoder disagrees.
  11281. */
  11282. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  11283. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  11284. fdi_dotclock, dotclock);
  11285. }
  11286. }
  11287. static void verify_wm_state(struct drm_crtc *crtc,
  11288. struct drm_crtc_state *new_state)
  11289. {
  11290. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  11291. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  11292. struct skl_pipe_wm hw_wm, *sw_wm;
  11293. struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
  11294. struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
  11295. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11296. const enum pipe pipe = intel_crtc->pipe;
  11297. int plane, level, max_level = ilk_wm_max_level(dev_priv);
  11298. if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
  11299. return;
  11300. skl_pipe_wm_get_hw_state(crtc, &hw_wm);
  11301. sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
  11302. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  11303. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  11304. /* planes */
  11305. for_each_universal_plane(dev_priv, pipe, plane) {
  11306. hw_plane_wm = &hw_wm.planes[plane];
  11307. sw_plane_wm = &sw_wm->planes[plane];
  11308. /* Watermarks */
  11309. for (level = 0; level <= max_level; level++) {
  11310. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  11311. &sw_plane_wm->wm[level]))
  11312. continue;
  11313. 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",
  11314. pipe_name(pipe), plane + 1, level,
  11315. sw_plane_wm->wm[level].plane_en,
  11316. sw_plane_wm->wm[level].plane_res_b,
  11317. sw_plane_wm->wm[level].plane_res_l,
  11318. hw_plane_wm->wm[level].plane_en,
  11319. hw_plane_wm->wm[level].plane_res_b,
  11320. hw_plane_wm->wm[level].plane_res_l);
  11321. }
  11322. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  11323. &sw_plane_wm->trans_wm)) {
  11324. 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",
  11325. pipe_name(pipe), plane + 1,
  11326. sw_plane_wm->trans_wm.plane_en,
  11327. sw_plane_wm->trans_wm.plane_res_b,
  11328. sw_plane_wm->trans_wm.plane_res_l,
  11329. hw_plane_wm->trans_wm.plane_en,
  11330. hw_plane_wm->trans_wm.plane_res_b,
  11331. hw_plane_wm->trans_wm.plane_res_l);
  11332. }
  11333. /* DDB */
  11334. hw_ddb_entry = &hw_ddb.plane[pipe][plane];
  11335. sw_ddb_entry = &sw_ddb->plane[pipe][plane];
  11336. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  11337. DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
  11338. pipe_name(pipe), plane + 1,
  11339. sw_ddb_entry->start, sw_ddb_entry->end,
  11340. hw_ddb_entry->start, hw_ddb_entry->end);
  11341. }
  11342. }
  11343. /*
  11344. * cursor
  11345. * If the cursor plane isn't active, we may not have updated it's ddb
  11346. * allocation. In that case since the ddb allocation will be updated
  11347. * once the plane becomes visible, we can skip this check
  11348. */
  11349. if (intel_crtc->cursor_addr) {
  11350. hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
  11351. sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
  11352. /* Watermarks */
  11353. for (level = 0; level <= max_level; level++) {
  11354. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  11355. &sw_plane_wm->wm[level]))
  11356. continue;
  11357. 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",
  11358. pipe_name(pipe), level,
  11359. sw_plane_wm->wm[level].plane_en,
  11360. sw_plane_wm->wm[level].plane_res_b,
  11361. sw_plane_wm->wm[level].plane_res_l,
  11362. hw_plane_wm->wm[level].plane_en,
  11363. hw_plane_wm->wm[level].plane_res_b,
  11364. hw_plane_wm->wm[level].plane_res_l);
  11365. }
  11366. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  11367. &sw_plane_wm->trans_wm)) {
  11368. DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  11369. pipe_name(pipe),
  11370. sw_plane_wm->trans_wm.plane_en,
  11371. sw_plane_wm->trans_wm.plane_res_b,
  11372. sw_plane_wm->trans_wm.plane_res_l,
  11373. hw_plane_wm->trans_wm.plane_en,
  11374. hw_plane_wm->trans_wm.plane_res_b,
  11375. hw_plane_wm->trans_wm.plane_res_l);
  11376. }
  11377. /* DDB */
  11378. hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  11379. sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  11380. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  11381. DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
  11382. pipe_name(pipe),
  11383. sw_ddb_entry->start, sw_ddb_entry->end,
  11384. hw_ddb_entry->start, hw_ddb_entry->end);
  11385. }
  11386. }
  11387. }
  11388. static void
  11389. verify_connector_state(struct drm_device *dev,
  11390. struct drm_atomic_state *state,
  11391. struct drm_crtc *crtc)
  11392. {
  11393. struct drm_connector *connector;
  11394. struct drm_connector_state *old_conn_state;
  11395. int i;
  11396. for_each_connector_in_state(state, connector, old_conn_state, i) {
  11397. struct drm_encoder *encoder = connector->encoder;
  11398. struct drm_connector_state *state = connector->state;
  11399. if (state->crtc != crtc)
  11400. continue;
  11401. intel_connector_verify_state(to_intel_connector(connector));
  11402. I915_STATE_WARN(state->best_encoder != encoder,
  11403. "connector's atomic encoder doesn't match legacy encoder\n");
  11404. }
  11405. }
  11406. static void
  11407. verify_encoder_state(struct drm_device *dev)
  11408. {
  11409. struct intel_encoder *encoder;
  11410. struct intel_connector *connector;
  11411. for_each_intel_encoder(dev, encoder) {
  11412. bool enabled = false;
  11413. enum pipe pipe;
  11414. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  11415. encoder->base.base.id,
  11416. encoder->base.name);
  11417. for_each_intel_connector(dev, connector) {
  11418. if (connector->base.state->best_encoder != &encoder->base)
  11419. continue;
  11420. enabled = true;
  11421. I915_STATE_WARN(connector->base.state->crtc !=
  11422. encoder->base.crtc,
  11423. "connector's crtc doesn't match encoder crtc\n");
  11424. }
  11425. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  11426. "encoder's enabled state mismatch "
  11427. "(expected %i, found %i)\n",
  11428. !!encoder->base.crtc, enabled);
  11429. if (!encoder->base.crtc) {
  11430. bool active;
  11431. active = encoder->get_hw_state(encoder, &pipe);
  11432. I915_STATE_WARN(active,
  11433. "encoder detached but still enabled on pipe %c.\n",
  11434. pipe_name(pipe));
  11435. }
  11436. }
  11437. }
  11438. static void
  11439. verify_crtc_state(struct drm_crtc *crtc,
  11440. struct drm_crtc_state *old_crtc_state,
  11441. struct drm_crtc_state *new_crtc_state)
  11442. {
  11443. struct drm_device *dev = crtc->dev;
  11444. struct drm_i915_private *dev_priv = to_i915(dev);
  11445. struct intel_encoder *encoder;
  11446. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11447. struct intel_crtc_state *pipe_config, *sw_config;
  11448. struct drm_atomic_state *old_state;
  11449. bool active;
  11450. old_state = old_crtc_state->state;
  11451. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  11452. pipe_config = to_intel_crtc_state(old_crtc_state);
  11453. memset(pipe_config, 0, sizeof(*pipe_config));
  11454. pipe_config->base.crtc = crtc;
  11455. pipe_config->base.state = old_state;
  11456. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  11457. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  11458. /* hw state is inconsistent with the pipe quirk */
  11459. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  11460. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  11461. active = new_crtc_state->active;
  11462. I915_STATE_WARN(new_crtc_state->active != active,
  11463. "crtc active state doesn't match with hw state "
  11464. "(expected %i, found %i)\n", new_crtc_state->active, active);
  11465. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  11466. "transitional active state does not match atomic hw state "
  11467. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  11468. for_each_encoder_on_crtc(dev, crtc, encoder) {
  11469. enum pipe pipe;
  11470. active = encoder->get_hw_state(encoder, &pipe);
  11471. I915_STATE_WARN(active != new_crtc_state->active,
  11472. "[ENCODER:%i] active %i with crtc active %i\n",
  11473. encoder->base.base.id, active, new_crtc_state->active);
  11474. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  11475. "Encoder connected to wrong pipe %c\n",
  11476. pipe_name(pipe));
  11477. if (active) {
  11478. pipe_config->output_types |= 1 << encoder->type;
  11479. encoder->get_config(encoder, pipe_config);
  11480. }
  11481. }
  11482. if (!new_crtc_state->active)
  11483. return;
  11484. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  11485. sw_config = to_intel_crtc_state(crtc->state);
  11486. if (!intel_pipe_config_compare(dev_priv, sw_config,
  11487. pipe_config, false)) {
  11488. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  11489. intel_dump_pipe_config(intel_crtc, pipe_config,
  11490. "[hw state]");
  11491. intel_dump_pipe_config(intel_crtc, sw_config,
  11492. "[sw state]");
  11493. }
  11494. }
  11495. static void
  11496. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  11497. struct intel_shared_dpll *pll,
  11498. struct drm_crtc *crtc,
  11499. struct drm_crtc_state *new_state)
  11500. {
  11501. struct intel_dpll_hw_state dpll_hw_state;
  11502. unsigned crtc_mask;
  11503. bool active;
  11504. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  11505. DRM_DEBUG_KMS("%s\n", pll->name);
  11506. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  11507. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  11508. I915_STATE_WARN(!pll->on && pll->active_mask,
  11509. "pll in active use but not on in sw tracking\n");
  11510. I915_STATE_WARN(pll->on && !pll->active_mask,
  11511. "pll is on but not used by any active crtc\n");
  11512. I915_STATE_WARN(pll->on != active,
  11513. "pll on state mismatch (expected %i, found %i)\n",
  11514. pll->on, active);
  11515. }
  11516. if (!crtc) {
  11517. I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
  11518. "more active pll users than references: %x vs %x\n",
  11519. pll->active_mask, pll->config.crtc_mask);
  11520. return;
  11521. }
  11522. crtc_mask = 1 << drm_crtc_index(crtc);
  11523. if (new_state->active)
  11524. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  11525. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  11526. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  11527. else
  11528. I915_STATE_WARN(pll->active_mask & crtc_mask,
  11529. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  11530. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  11531. I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
  11532. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  11533. crtc_mask, pll->config.crtc_mask);
  11534. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
  11535. &dpll_hw_state,
  11536. sizeof(dpll_hw_state)),
  11537. "pll hw state mismatch\n");
  11538. }
  11539. static void
  11540. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  11541. struct drm_crtc_state *old_crtc_state,
  11542. struct drm_crtc_state *new_crtc_state)
  11543. {
  11544. struct drm_i915_private *dev_priv = to_i915(dev);
  11545. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  11546. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  11547. if (new_state->shared_dpll)
  11548. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  11549. if (old_state->shared_dpll &&
  11550. old_state->shared_dpll != new_state->shared_dpll) {
  11551. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  11552. struct intel_shared_dpll *pll = old_state->shared_dpll;
  11553. I915_STATE_WARN(pll->active_mask & crtc_mask,
  11554. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  11555. pipe_name(drm_crtc_index(crtc)));
  11556. I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
  11557. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  11558. pipe_name(drm_crtc_index(crtc)));
  11559. }
  11560. }
  11561. static void
  11562. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  11563. struct drm_atomic_state *state,
  11564. struct drm_crtc_state *old_state,
  11565. struct drm_crtc_state *new_state)
  11566. {
  11567. if (!needs_modeset(new_state) &&
  11568. !to_intel_crtc_state(new_state)->update_pipe)
  11569. return;
  11570. verify_wm_state(crtc, new_state);
  11571. verify_connector_state(crtc->dev, state, crtc);
  11572. verify_crtc_state(crtc, old_state, new_state);
  11573. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  11574. }
  11575. static void
  11576. verify_disabled_dpll_state(struct drm_device *dev)
  11577. {
  11578. struct drm_i915_private *dev_priv = to_i915(dev);
  11579. int i;
  11580. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  11581. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  11582. }
  11583. static void
  11584. intel_modeset_verify_disabled(struct drm_device *dev,
  11585. struct drm_atomic_state *state)
  11586. {
  11587. verify_encoder_state(dev);
  11588. verify_connector_state(dev, state, NULL);
  11589. verify_disabled_dpll_state(dev);
  11590. }
  11591. static void update_scanline_offset(struct intel_crtc *crtc)
  11592. {
  11593. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  11594. /*
  11595. * The scanline counter increments at the leading edge of hsync.
  11596. *
  11597. * On most platforms it starts counting from vtotal-1 on the
  11598. * first active line. That means the scanline counter value is
  11599. * always one less than what we would expect. Ie. just after
  11600. * start of vblank, which also occurs at start of hsync (on the
  11601. * last active line), the scanline counter will read vblank_start-1.
  11602. *
  11603. * On gen2 the scanline counter starts counting from 1 instead
  11604. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  11605. * to keep the value positive), instead of adding one.
  11606. *
  11607. * On HSW+ the behaviour of the scanline counter depends on the output
  11608. * type. For DP ports it behaves like most other platforms, but on HDMI
  11609. * there's an extra 1 line difference. So we need to add two instead of
  11610. * one to the value.
  11611. */
  11612. if (IS_GEN2(dev_priv)) {
  11613. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  11614. int vtotal;
  11615. vtotal = adjusted_mode->crtc_vtotal;
  11616. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  11617. vtotal /= 2;
  11618. crtc->scanline_offset = vtotal - 1;
  11619. } else if (HAS_DDI(dev_priv) &&
  11620. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
  11621. crtc->scanline_offset = 2;
  11622. } else
  11623. crtc->scanline_offset = 1;
  11624. }
  11625. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  11626. {
  11627. struct drm_device *dev = state->dev;
  11628. struct drm_i915_private *dev_priv = to_i915(dev);
  11629. struct intel_shared_dpll_config *shared_dpll = NULL;
  11630. struct drm_crtc *crtc;
  11631. struct drm_crtc_state *crtc_state;
  11632. int i;
  11633. if (!dev_priv->display.crtc_compute_clock)
  11634. return;
  11635. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11636. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11637. struct intel_shared_dpll *old_dpll =
  11638. to_intel_crtc_state(crtc->state)->shared_dpll;
  11639. if (!needs_modeset(crtc_state))
  11640. continue;
  11641. to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
  11642. if (!old_dpll)
  11643. continue;
  11644. if (!shared_dpll)
  11645. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  11646. intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
  11647. }
  11648. }
  11649. /*
  11650. * This implements the workaround described in the "notes" section of the mode
  11651. * set sequence documentation. When going from no pipes or single pipe to
  11652. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  11653. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  11654. */
  11655. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  11656. {
  11657. struct drm_crtc_state *crtc_state;
  11658. struct intel_crtc *intel_crtc;
  11659. struct drm_crtc *crtc;
  11660. struct intel_crtc_state *first_crtc_state = NULL;
  11661. struct intel_crtc_state *other_crtc_state = NULL;
  11662. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  11663. int i;
  11664. /* look at all crtc's that are going to be enabled in during modeset */
  11665. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11666. intel_crtc = to_intel_crtc(crtc);
  11667. if (!crtc_state->active || !needs_modeset(crtc_state))
  11668. continue;
  11669. if (first_crtc_state) {
  11670. other_crtc_state = to_intel_crtc_state(crtc_state);
  11671. break;
  11672. } else {
  11673. first_crtc_state = to_intel_crtc_state(crtc_state);
  11674. first_pipe = intel_crtc->pipe;
  11675. }
  11676. }
  11677. /* No workaround needed? */
  11678. if (!first_crtc_state)
  11679. return 0;
  11680. /* w/a possibly needed, check how many crtc's are already enabled. */
  11681. for_each_intel_crtc(state->dev, intel_crtc) {
  11682. struct intel_crtc_state *pipe_config;
  11683. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  11684. if (IS_ERR(pipe_config))
  11685. return PTR_ERR(pipe_config);
  11686. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  11687. if (!pipe_config->base.active ||
  11688. needs_modeset(&pipe_config->base))
  11689. continue;
  11690. /* 2 or more enabled crtcs means no need for w/a */
  11691. if (enabled_pipe != INVALID_PIPE)
  11692. return 0;
  11693. enabled_pipe = intel_crtc->pipe;
  11694. }
  11695. if (enabled_pipe != INVALID_PIPE)
  11696. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  11697. else if (other_crtc_state)
  11698. other_crtc_state->hsw_workaround_pipe = first_pipe;
  11699. return 0;
  11700. }
  11701. static int intel_lock_all_pipes(struct drm_atomic_state *state)
  11702. {
  11703. struct drm_crtc *crtc;
  11704. /* Add all pipes to the state */
  11705. for_each_crtc(state->dev, crtc) {
  11706. struct drm_crtc_state *crtc_state;
  11707. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11708. if (IS_ERR(crtc_state))
  11709. return PTR_ERR(crtc_state);
  11710. }
  11711. return 0;
  11712. }
  11713. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  11714. {
  11715. struct drm_crtc *crtc;
  11716. /*
  11717. * Add all pipes to the state, and force
  11718. * a modeset on all the active ones.
  11719. */
  11720. for_each_crtc(state->dev, crtc) {
  11721. struct drm_crtc_state *crtc_state;
  11722. int ret;
  11723. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11724. if (IS_ERR(crtc_state))
  11725. return PTR_ERR(crtc_state);
  11726. if (!crtc_state->active || needs_modeset(crtc_state))
  11727. continue;
  11728. crtc_state->mode_changed = true;
  11729. ret = drm_atomic_add_affected_connectors(state, crtc);
  11730. if (ret)
  11731. return ret;
  11732. ret = drm_atomic_add_affected_planes(state, crtc);
  11733. if (ret)
  11734. return ret;
  11735. }
  11736. return 0;
  11737. }
  11738. static int intel_modeset_checks(struct drm_atomic_state *state)
  11739. {
  11740. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11741. struct drm_i915_private *dev_priv = to_i915(state->dev);
  11742. struct drm_crtc *crtc;
  11743. struct drm_crtc_state *crtc_state;
  11744. int ret = 0, i;
  11745. if (!check_digital_port_conflicts(state)) {
  11746. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  11747. return -EINVAL;
  11748. }
  11749. intel_state->modeset = true;
  11750. intel_state->active_crtcs = dev_priv->active_crtcs;
  11751. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11752. if (crtc_state->active)
  11753. intel_state->active_crtcs |= 1 << i;
  11754. else
  11755. intel_state->active_crtcs &= ~(1 << i);
  11756. if (crtc_state->active != crtc->state->active)
  11757. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  11758. }
  11759. /*
  11760. * See if the config requires any additional preparation, e.g.
  11761. * to adjust global state with pipes off. We need to do this
  11762. * here so we can get the modeset_pipe updated config for the new
  11763. * mode set on this crtc. For other crtcs we need to use the
  11764. * adjusted_mode bits in the crtc directly.
  11765. */
  11766. if (dev_priv->display.modeset_calc_cdclk) {
  11767. if (!intel_state->cdclk_pll_vco)
  11768. intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
  11769. if (!intel_state->cdclk_pll_vco)
  11770. intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
  11771. ret = dev_priv->display.modeset_calc_cdclk(state);
  11772. if (ret < 0)
  11773. return ret;
  11774. /*
  11775. * Writes to dev_priv->atomic_cdclk_freq must protected by
  11776. * holding all the crtc locks, even if we don't end up
  11777. * touching the hardware
  11778. */
  11779. if (intel_state->cdclk != dev_priv->atomic_cdclk_freq) {
  11780. ret = intel_lock_all_pipes(state);
  11781. if (ret < 0)
  11782. return ret;
  11783. }
  11784. /* All pipes must be switched off while we change the cdclk. */
  11785. if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
  11786. intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco) {
  11787. ret = intel_modeset_all_pipes(state);
  11788. if (ret < 0)
  11789. return ret;
  11790. }
  11791. DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
  11792. intel_state->cdclk, intel_state->dev_cdclk);
  11793. } else {
  11794. to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
  11795. }
  11796. intel_modeset_clear_plls(state);
  11797. if (IS_HASWELL(dev_priv))
  11798. return haswell_mode_set_planes_workaround(state);
  11799. return 0;
  11800. }
  11801. /*
  11802. * Handle calculation of various watermark data at the end of the atomic check
  11803. * phase. The code here should be run after the per-crtc and per-plane 'check'
  11804. * handlers to ensure that all derived state has been updated.
  11805. */
  11806. static int calc_watermark_data(struct drm_atomic_state *state)
  11807. {
  11808. struct drm_device *dev = state->dev;
  11809. struct drm_i915_private *dev_priv = to_i915(dev);
  11810. /* Is there platform-specific watermark information to calculate? */
  11811. if (dev_priv->display.compute_global_watermarks)
  11812. return dev_priv->display.compute_global_watermarks(state);
  11813. return 0;
  11814. }
  11815. /**
  11816. * intel_atomic_check - validate state object
  11817. * @dev: drm device
  11818. * @state: state to validate
  11819. */
  11820. static int intel_atomic_check(struct drm_device *dev,
  11821. struct drm_atomic_state *state)
  11822. {
  11823. struct drm_i915_private *dev_priv = to_i915(dev);
  11824. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11825. struct drm_crtc *crtc;
  11826. struct drm_crtc_state *crtc_state;
  11827. int ret, i;
  11828. bool any_ms = false;
  11829. ret = drm_atomic_helper_check_modeset(dev, state);
  11830. if (ret)
  11831. return ret;
  11832. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11833. struct intel_crtc_state *pipe_config =
  11834. to_intel_crtc_state(crtc_state);
  11835. /* Catch I915_MODE_FLAG_INHERITED */
  11836. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  11837. crtc_state->mode_changed = true;
  11838. if (!needs_modeset(crtc_state))
  11839. continue;
  11840. if (!crtc_state->enable) {
  11841. any_ms = true;
  11842. continue;
  11843. }
  11844. /* FIXME: For only active_changed we shouldn't need to do any
  11845. * state recomputation at all. */
  11846. ret = drm_atomic_add_affected_connectors(state, crtc);
  11847. if (ret)
  11848. return ret;
  11849. ret = intel_modeset_pipe_config(crtc, pipe_config);
  11850. if (ret) {
  11851. intel_dump_pipe_config(to_intel_crtc(crtc),
  11852. pipe_config, "[failed]");
  11853. return ret;
  11854. }
  11855. if (i915.fastboot &&
  11856. intel_pipe_config_compare(dev_priv,
  11857. to_intel_crtc_state(crtc->state),
  11858. pipe_config, true)) {
  11859. crtc_state->mode_changed = false;
  11860. to_intel_crtc_state(crtc_state)->update_pipe = true;
  11861. }
  11862. if (needs_modeset(crtc_state))
  11863. any_ms = true;
  11864. ret = drm_atomic_add_affected_planes(state, crtc);
  11865. if (ret)
  11866. return ret;
  11867. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  11868. needs_modeset(crtc_state) ?
  11869. "[modeset]" : "[fastset]");
  11870. }
  11871. if (any_ms) {
  11872. ret = intel_modeset_checks(state);
  11873. if (ret)
  11874. return ret;
  11875. } else {
  11876. intel_state->cdclk = dev_priv->atomic_cdclk_freq;
  11877. }
  11878. ret = drm_atomic_helper_check_planes(dev, state);
  11879. if (ret)
  11880. return ret;
  11881. intel_fbc_choose_crtc(dev_priv, state);
  11882. return calc_watermark_data(state);
  11883. }
  11884. static int intel_atomic_prepare_commit(struct drm_device *dev,
  11885. struct drm_atomic_state *state)
  11886. {
  11887. struct drm_i915_private *dev_priv = to_i915(dev);
  11888. struct drm_crtc_state *crtc_state;
  11889. struct drm_crtc *crtc;
  11890. int i, ret;
  11891. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11892. if (state->legacy_cursor_update)
  11893. continue;
  11894. ret = intel_crtc_wait_for_pending_flips(crtc);
  11895. if (ret)
  11896. return ret;
  11897. if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
  11898. flush_workqueue(dev_priv->wq);
  11899. }
  11900. ret = mutex_lock_interruptible(&dev->struct_mutex);
  11901. if (ret)
  11902. return ret;
  11903. ret = drm_atomic_helper_prepare_planes(dev, state);
  11904. mutex_unlock(&dev->struct_mutex);
  11905. return ret;
  11906. }
  11907. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  11908. {
  11909. struct drm_device *dev = crtc->base.dev;
  11910. if (!dev->max_vblank_count)
  11911. return drm_accurate_vblank_count(&crtc->base);
  11912. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  11913. }
  11914. static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
  11915. struct drm_i915_private *dev_priv,
  11916. unsigned crtc_mask)
  11917. {
  11918. unsigned last_vblank_count[I915_MAX_PIPES];
  11919. enum pipe pipe;
  11920. int ret;
  11921. if (!crtc_mask)
  11922. return;
  11923. for_each_pipe(dev_priv, pipe) {
  11924. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  11925. pipe);
  11926. if (!((1 << pipe) & crtc_mask))
  11927. continue;
  11928. ret = drm_crtc_vblank_get(&crtc->base);
  11929. if (WARN_ON(ret != 0)) {
  11930. crtc_mask &= ~(1 << pipe);
  11931. continue;
  11932. }
  11933. last_vblank_count[pipe] = drm_crtc_vblank_count(&crtc->base);
  11934. }
  11935. for_each_pipe(dev_priv, pipe) {
  11936. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  11937. pipe);
  11938. long lret;
  11939. if (!((1 << pipe) & crtc_mask))
  11940. continue;
  11941. lret = wait_event_timeout(dev->vblank[pipe].queue,
  11942. last_vblank_count[pipe] !=
  11943. drm_crtc_vblank_count(&crtc->base),
  11944. msecs_to_jiffies(50));
  11945. WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
  11946. drm_crtc_vblank_put(&crtc->base);
  11947. }
  11948. }
  11949. static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
  11950. {
  11951. /* fb updated, need to unpin old fb */
  11952. if (crtc_state->fb_changed)
  11953. return true;
  11954. /* wm changes, need vblank before final wm's */
  11955. if (crtc_state->update_wm_post)
  11956. return true;
  11957. /*
  11958. * cxsr is re-enabled after vblank.
  11959. * This is already handled by crtc_state->update_wm_post,
  11960. * but added for clarity.
  11961. */
  11962. if (crtc_state->disable_cxsr)
  11963. return true;
  11964. return false;
  11965. }
  11966. static void intel_update_crtc(struct drm_crtc *crtc,
  11967. struct drm_atomic_state *state,
  11968. struct drm_crtc_state *old_crtc_state,
  11969. unsigned int *crtc_vblank_mask)
  11970. {
  11971. struct drm_device *dev = crtc->dev;
  11972. struct drm_i915_private *dev_priv = to_i915(dev);
  11973. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11974. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc->state);
  11975. bool modeset = needs_modeset(crtc->state);
  11976. if (modeset) {
  11977. update_scanline_offset(intel_crtc);
  11978. dev_priv->display.crtc_enable(pipe_config, state);
  11979. } else {
  11980. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  11981. }
  11982. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  11983. intel_fbc_enable(
  11984. intel_crtc, pipe_config,
  11985. to_intel_plane_state(crtc->primary->state));
  11986. }
  11987. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  11988. if (needs_vblank_wait(pipe_config))
  11989. *crtc_vblank_mask |= drm_crtc_mask(crtc);
  11990. }
  11991. static void intel_update_crtcs(struct drm_atomic_state *state,
  11992. unsigned int *crtc_vblank_mask)
  11993. {
  11994. struct drm_crtc *crtc;
  11995. struct drm_crtc_state *old_crtc_state;
  11996. int i;
  11997. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11998. if (!crtc->state->active)
  11999. continue;
  12000. intel_update_crtc(crtc, state, old_crtc_state,
  12001. crtc_vblank_mask);
  12002. }
  12003. }
  12004. static void skl_update_crtcs(struct drm_atomic_state *state,
  12005. unsigned int *crtc_vblank_mask)
  12006. {
  12007. struct drm_i915_private *dev_priv = to_i915(state->dev);
  12008. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  12009. struct drm_crtc *crtc;
  12010. struct intel_crtc *intel_crtc;
  12011. struct drm_crtc_state *old_crtc_state;
  12012. struct intel_crtc_state *cstate;
  12013. unsigned int updated = 0;
  12014. bool progress;
  12015. enum pipe pipe;
  12016. int i;
  12017. const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
  12018. for_each_crtc_in_state(state, crtc, old_crtc_state, i)
  12019. /* ignore allocations for crtc's that have been turned off. */
  12020. if (crtc->state->active)
  12021. entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
  12022. /*
  12023. * Whenever the number of active pipes changes, we need to make sure we
  12024. * update the pipes in the right order so that their ddb allocations
  12025. * never overlap with eachother inbetween CRTC updates. Otherwise we'll
  12026. * cause pipe underruns and other bad stuff.
  12027. */
  12028. do {
  12029. progress = false;
  12030. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12031. bool vbl_wait = false;
  12032. unsigned int cmask = drm_crtc_mask(crtc);
  12033. intel_crtc = to_intel_crtc(crtc);
  12034. cstate = to_intel_crtc_state(crtc->state);
  12035. pipe = intel_crtc->pipe;
  12036. if (updated & cmask || !cstate->base.active)
  12037. continue;
  12038. if (skl_ddb_allocation_overlaps(entries, &cstate->wm.skl.ddb, i))
  12039. continue;
  12040. updated |= cmask;
  12041. entries[i] = &cstate->wm.skl.ddb;
  12042. /*
  12043. * If this is an already active pipe, it's DDB changed,
  12044. * and this isn't the last pipe that needs updating
  12045. * then we need to wait for a vblank to pass for the
  12046. * new ddb allocation to take effect.
  12047. */
  12048. if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
  12049. &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
  12050. !crtc->state->active_changed &&
  12051. intel_state->wm_results.dirty_pipes != updated)
  12052. vbl_wait = true;
  12053. intel_update_crtc(crtc, state, old_crtc_state,
  12054. crtc_vblank_mask);
  12055. if (vbl_wait)
  12056. intel_wait_for_vblank(dev_priv, pipe);
  12057. progress = true;
  12058. }
  12059. } while (progress);
  12060. }
  12061. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  12062. {
  12063. struct drm_device *dev = state->dev;
  12064. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  12065. struct drm_i915_private *dev_priv = to_i915(dev);
  12066. struct drm_crtc_state *old_crtc_state;
  12067. struct drm_crtc *crtc;
  12068. struct intel_crtc_state *intel_cstate;
  12069. bool hw_check = intel_state->modeset;
  12070. unsigned long put_domains[I915_MAX_PIPES] = {};
  12071. unsigned crtc_vblank_mask = 0;
  12072. int i;
  12073. drm_atomic_helper_wait_for_dependencies(state);
  12074. if (intel_state->modeset)
  12075. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  12076. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12077. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12078. if (needs_modeset(crtc->state) ||
  12079. to_intel_crtc_state(crtc->state)->update_pipe) {
  12080. hw_check = true;
  12081. put_domains[to_intel_crtc(crtc)->pipe] =
  12082. modeset_get_crtc_power_domains(crtc,
  12083. to_intel_crtc_state(crtc->state));
  12084. }
  12085. if (!needs_modeset(crtc->state))
  12086. continue;
  12087. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  12088. if (old_crtc_state->active) {
  12089. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  12090. dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
  12091. intel_crtc->active = false;
  12092. intel_fbc_disable(intel_crtc);
  12093. intel_disable_shared_dpll(intel_crtc);
  12094. /*
  12095. * Underruns don't always raise
  12096. * interrupts, so check manually.
  12097. */
  12098. intel_check_cpu_fifo_underruns(dev_priv);
  12099. intel_check_pch_fifo_underruns(dev_priv);
  12100. if (!crtc->state->active) {
  12101. /*
  12102. * Make sure we don't call initial_watermarks
  12103. * for ILK-style watermark updates.
  12104. */
  12105. if (dev_priv->display.atomic_update_watermarks)
  12106. dev_priv->display.initial_watermarks(intel_state,
  12107. to_intel_crtc_state(crtc->state));
  12108. else
  12109. intel_update_watermarks(intel_crtc);
  12110. }
  12111. }
  12112. }
  12113. /* Only after disabling all output pipelines that will be changed can we
  12114. * update the the output configuration. */
  12115. intel_modeset_update_crtc_state(state);
  12116. if (intel_state->modeset) {
  12117. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  12118. if (dev_priv->display.modeset_commit_cdclk &&
  12119. (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
  12120. intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
  12121. dev_priv->display.modeset_commit_cdclk(state);
  12122. /*
  12123. * SKL workaround: bspec recommends we disable the SAGV when we
  12124. * have more then one pipe enabled
  12125. */
  12126. if (!intel_can_enable_sagv(state))
  12127. intel_disable_sagv(dev_priv);
  12128. intel_modeset_verify_disabled(dev, state);
  12129. }
  12130. /* Complete the events for pipes that have now been disabled */
  12131. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12132. bool modeset = needs_modeset(crtc->state);
  12133. /* Complete events for now disable pipes here. */
  12134. if (modeset && !crtc->state->active && crtc->state->event) {
  12135. spin_lock_irq(&dev->event_lock);
  12136. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  12137. spin_unlock_irq(&dev->event_lock);
  12138. crtc->state->event = NULL;
  12139. }
  12140. }
  12141. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  12142. dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
  12143. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  12144. * already, but still need the state for the delayed optimization. To
  12145. * fix this:
  12146. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  12147. * - schedule that vblank worker _before_ calling hw_done
  12148. * - at the start of commit_tail, cancel it _synchrously
  12149. * - switch over to the vblank wait helper in the core after that since
  12150. * we don't need out special handling any more.
  12151. */
  12152. if (!state->legacy_cursor_update)
  12153. intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
  12154. /*
  12155. * Now that the vblank has passed, we can go ahead and program the
  12156. * optimal watermarks on platforms that need two-step watermark
  12157. * programming.
  12158. *
  12159. * TODO: Move this (and other cleanup) to an async worker eventually.
  12160. */
  12161. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12162. intel_cstate = to_intel_crtc_state(crtc->state);
  12163. if (dev_priv->display.optimize_watermarks)
  12164. dev_priv->display.optimize_watermarks(intel_state,
  12165. intel_cstate);
  12166. }
  12167. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12168. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  12169. if (put_domains[i])
  12170. modeset_put_power_domains(dev_priv, put_domains[i]);
  12171. intel_modeset_verify_crtc(crtc, state, old_crtc_state, crtc->state);
  12172. }
  12173. if (intel_state->modeset && intel_can_enable_sagv(state))
  12174. intel_enable_sagv(dev_priv);
  12175. drm_atomic_helper_commit_hw_done(state);
  12176. if (intel_state->modeset)
  12177. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  12178. mutex_lock(&dev->struct_mutex);
  12179. drm_atomic_helper_cleanup_planes(dev, state);
  12180. mutex_unlock(&dev->struct_mutex);
  12181. drm_atomic_helper_commit_cleanup_done(state);
  12182. drm_atomic_state_put(state);
  12183. /* As one of the primary mmio accessors, KMS has a high likelihood
  12184. * of triggering bugs in unclaimed access. After we finish
  12185. * modesetting, see if an error has been flagged, and if so
  12186. * enable debugging for the next modeset - and hope we catch
  12187. * the culprit.
  12188. *
  12189. * XXX note that we assume display power is on at this point.
  12190. * This might hold true now but we need to add pm helper to check
  12191. * unclaimed only when the hardware is on, as atomic commits
  12192. * can happen also when the device is completely off.
  12193. */
  12194. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  12195. }
  12196. static void intel_atomic_commit_work(struct work_struct *work)
  12197. {
  12198. struct drm_atomic_state *state =
  12199. container_of(work, struct drm_atomic_state, commit_work);
  12200. intel_atomic_commit_tail(state);
  12201. }
  12202. static int __i915_sw_fence_call
  12203. intel_atomic_commit_ready(struct i915_sw_fence *fence,
  12204. enum i915_sw_fence_notify notify)
  12205. {
  12206. struct intel_atomic_state *state =
  12207. container_of(fence, struct intel_atomic_state, commit_ready);
  12208. switch (notify) {
  12209. case FENCE_COMPLETE:
  12210. if (state->base.commit_work.func)
  12211. queue_work(system_unbound_wq, &state->base.commit_work);
  12212. break;
  12213. case FENCE_FREE:
  12214. drm_atomic_state_put(&state->base);
  12215. break;
  12216. }
  12217. return NOTIFY_DONE;
  12218. }
  12219. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  12220. {
  12221. struct drm_plane_state *old_plane_state;
  12222. struct drm_plane *plane;
  12223. int i;
  12224. for_each_plane_in_state(state, plane, old_plane_state, i)
  12225. i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
  12226. intel_fb_obj(plane->state->fb),
  12227. to_intel_plane(plane)->frontbuffer_bit);
  12228. }
  12229. /**
  12230. * intel_atomic_commit - commit validated state object
  12231. * @dev: DRM device
  12232. * @state: the top-level driver state object
  12233. * @nonblock: nonblocking commit
  12234. *
  12235. * This function commits a top-level state object that has been validated
  12236. * with drm_atomic_helper_check().
  12237. *
  12238. * RETURNS
  12239. * Zero for success or -errno.
  12240. */
  12241. static int intel_atomic_commit(struct drm_device *dev,
  12242. struct drm_atomic_state *state,
  12243. bool nonblock)
  12244. {
  12245. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  12246. struct drm_i915_private *dev_priv = to_i915(dev);
  12247. int ret = 0;
  12248. ret = drm_atomic_helper_setup_commit(state, nonblock);
  12249. if (ret)
  12250. return ret;
  12251. drm_atomic_state_get(state);
  12252. i915_sw_fence_init(&intel_state->commit_ready,
  12253. intel_atomic_commit_ready);
  12254. ret = intel_atomic_prepare_commit(dev, state);
  12255. if (ret) {
  12256. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  12257. i915_sw_fence_commit(&intel_state->commit_ready);
  12258. return ret;
  12259. }
  12260. drm_atomic_helper_swap_state(state, true);
  12261. dev_priv->wm.distrust_bios_wm = false;
  12262. intel_shared_dpll_commit(state);
  12263. intel_atomic_track_fbs(state);
  12264. if (intel_state->modeset) {
  12265. memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
  12266. sizeof(intel_state->min_pixclk));
  12267. dev_priv->active_crtcs = intel_state->active_crtcs;
  12268. dev_priv->atomic_cdclk_freq = intel_state->cdclk;
  12269. }
  12270. drm_atomic_state_get(state);
  12271. INIT_WORK(&state->commit_work,
  12272. nonblock ? intel_atomic_commit_work : NULL);
  12273. i915_sw_fence_commit(&intel_state->commit_ready);
  12274. if (!nonblock) {
  12275. i915_sw_fence_wait(&intel_state->commit_ready);
  12276. intel_atomic_commit_tail(state);
  12277. }
  12278. return 0;
  12279. }
  12280. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  12281. {
  12282. struct drm_device *dev = crtc->dev;
  12283. struct drm_atomic_state *state;
  12284. struct drm_crtc_state *crtc_state;
  12285. int ret;
  12286. state = drm_atomic_state_alloc(dev);
  12287. if (!state) {
  12288. DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
  12289. crtc->base.id, crtc->name);
  12290. return;
  12291. }
  12292. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  12293. retry:
  12294. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  12295. ret = PTR_ERR_OR_ZERO(crtc_state);
  12296. if (!ret) {
  12297. if (!crtc_state->active)
  12298. goto out;
  12299. crtc_state->mode_changed = true;
  12300. ret = drm_atomic_commit(state);
  12301. }
  12302. if (ret == -EDEADLK) {
  12303. drm_atomic_state_clear(state);
  12304. drm_modeset_backoff(state->acquire_ctx);
  12305. goto retry;
  12306. }
  12307. out:
  12308. drm_atomic_state_put(state);
  12309. }
  12310. /*
  12311. * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
  12312. * drm_atomic_helper_legacy_gamma_set() directly.
  12313. */
  12314. static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
  12315. u16 *red, u16 *green, u16 *blue,
  12316. uint32_t size)
  12317. {
  12318. struct drm_device *dev = crtc->dev;
  12319. struct drm_mode_config *config = &dev->mode_config;
  12320. struct drm_crtc_state *state;
  12321. int ret;
  12322. ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
  12323. if (ret)
  12324. return ret;
  12325. /*
  12326. * Make sure we update the legacy properties so this works when
  12327. * atomic is not enabled.
  12328. */
  12329. state = crtc->state;
  12330. drm_object_property_set_value(&crtc->base,
  12331. config->degamma_lut_property,
  12332. (state->degamma_lut) ?
  12333. state->degamma_lut->base.id : 0);
  12334. drm_object_property_set_value(&crtc->base,
  12335. config->ctm_property,
  12336. (state->ctm) ?
  12337. state->ctm->base.id : 0);
  12338. drm_object_property_set_value(&crtc->base,
  12339. config->gamma_lut_property,
  12340. (state->gamma_lut) ?
  12341. state->gamma_lut->base.id : 0);
  12342. return 0;
  12343. }
  12344. static const struct drm_crtc_funcs intel_crtc_funcs = {
  12345. .gamma_set = intel_atomic_legacy_gamma_set,
  12346. .set_config = drm_atomic_helper_set_config,
  12347. .set_property = drm_atomic_helper_crtc_set_property,
  12348. .destroy = intel_crtc_destroy,
  12349. .page_flip = intel_crtc_page_flip,
  12350. .atomic_duplicate_state = intel_crtc_duplicate_state,
  12351. .atomic_destroy_state = intel_crtc_destroy_state,
  12352. };
  12353. /**
  12354. * intel_prepare_plane_fb - Prepare fb for usage on plane
  12355. * @plane: drm plane to prepare for
  12356. * @fb: framebuffer to prepare for presentation
  12357. *
  12358. * Prepares a framebuffer for usage on a display plane. Generally this
  12359. * involves pinning the underlying object and updating the frontbuffer tracking
  12360. * bits. Some older platforms need special physical address handling for
  12361. * cursor planes.
  12362. *
  12363. * Must be called with struct_mutex held.
  12364. *
  12365. * Returns 0 on success, negative error code on failure.
  12366. */
  12367. int
  12368. intel_prepare_plane_fb(struct drm_plane *plane,
  12369. struct drm_plane_state *new_state)
  12370. {
  12371. struct intel_atomic_state *intel_state =
  12372. to_intel_atomic_state(new_state->state);
  12373. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  12374. struct drm_framebuffer *fb = new_state->fb;
  12375. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  12376. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  12377. int ret;
  12378. if (!obj && !old_obj)
  12379. return 0;
  12380. if (old_obj) {
  12381. struct drm_crtc_state *crtc_state =
  12382. drm_atomic_get_existing_crtc_state(new_state->state,
  12383. plane->state->crtc);
  12384. /* Big Hammer, we also need to ensure that any pending
  12385. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  12386. * current scanout is retired before unpinning the old
  12387. * framebuffer. Note that we rely on userspace rendering
  12388. * into the buffer attached to the pipe they are waiting
  12389. * on. If not, userspace generates a GPU hang with IPEHR
  12390. * point to the MI_WAIT_FOR_EVENT.
  12391. *
  12392. * This should only fail upon a hung GPU, in which case we
  12393. * can safely continue.
  12394. */
  12395. if (needs_modeset(crtc_state)) {
  12396. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  12397. old_obj->resv, NULL,
  12398. false, 0,
  12399. GFP_KERNEL);
  12400. if (ret < 0)
  12401. return ret;
  12402. }
  12403. }
  12404. if (new_state->fence) { /* explicit fencing */
  12405. ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
  12406. new_state->fence,
  12407. I915_FENCE_TIMEOUT,
  12408. GFP_KERNEL);
  12409. if (ret < 0)
  12410. return ret;
  12411. }
  12412. if (!obj)
  12413. return 0;
  12414. if (!new_state->fence) { /* implicit fencing */
  12415. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  12416. obj->resv, NULL,
  12417. false, I915_FENCE_TIMEOUT,
  12418. GFP_KERNEL);
  12419. if (ret < 0)
  12420. return ret;
  12421. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  12422. }
  12423. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  12424. INTEL_INFO(dev_priv)->cursor_needs_physical) {
  12425. int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
  12426. ret = i915_gem_object_attach_phys(obj, align);
  12427. if (ret) {
  12428. DRM_DEBUG_KMS("failed to attach phys object\n");
  12429. return ret;
  12430. }
  12431. } else {
  12432. struct i915_vma *vma;
  12433. vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  12434. if (IS_ERR(vma)) {
  12435. DRM_DEBUG_KMS("failed to pin object\n");
  12436. return PTR_ERR(vma);
  12437. }
  12438. }
  12439. return 0;
  12440. }
  12441. /**
  12442. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  12443. * @plane: drm plane to clean up for
  12444. * @fb: old framebuffer that was on plane
  12445. *
  12446. * Cleans up a framebuffer that has just been removed from a plane.
  12447. *
  12448. * Must be called with struct_mutex held.
  12449. */
  12450. void
  12451. intel_cleanup_plane_fb(struct drm_plane *plane,
  12452. struct drm_plane_state *old_state)
  12453. {
  12454. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  12455. struct intel_plane_state *old_intel_state;
  12456. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
  12457. struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
  12458. old_intel_state = to_intel_plane_state(old_state);
  12459. if (!obj && !old_obj)
  12460. return;
  12461. if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
  12462. !INTEL_INFO(dev_priv)->cursor_needs_physical))
  12463. intel_unpin_fb_obj(old_state->fb, old_state->rotation);
  12464. }
  12465. int
  12466. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  12467. {
  12468. int max_scale;
  12469. int crtc_clock, cdclk;
  12470. if (!intel_crtc || !crtc_state->base.enable)
  12471. return DRM_PLANE_HELPER_NO_SCALING;
  12472. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  12473. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  12474. if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
  12475. return DRM_PLANE_HELPER_NO_SCALING;
  12476. /*
  12477. * skl max scale is lower of:
  12478. * close to 3 but not 3, -1 is for that purpose
  12479. * or
  12480. * cdclk/crtc_clock
  12481. */
  12482. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  12483. return max_scale;
  12484. }
  12485. static int
  12486. intel_check_primary_plane(struct drm_plane *plane,
  12487. struct intel_crtc_state *crtc_state,
  12488. struct intel_plane_state *state)
  12489. {
  12490. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  12491. struct drm_crtc *crtc = state->base.crtc;
  12492. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  12493. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  12494. bool can_position = false;
  12495. int ret;
  12496. if (INTEL_GEN(dev_priv) >= 9) {
  12497. /* use scaler when colorkey is not required */
  12498. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  12499. min_scale = 1;
  12500. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  12501. }
  12502. can_position = true;
  12503. }
  12504. ret = drm_plane_helper_check_state(&state->base,
  12505. &state->clip,
  12506. min_scale, max_scale,
  12507. can_position, true);
  12508. if (ret)
  12509. return ret;
  12510. if (!state->base.fb)
  12511. return 0;
  12512. if (INTEL_GEN(dev_priv) >= 9) {
  12513. ret = skl_check_plane_surface(state);
  12514. if (ret)
  12515. return ret;
  12516. }
  12517. return 0;
  12518. }
  12519. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  12520. struct drm_crtc_state *old_crtc_state)
  12521. {
  12522. struct drm_device *dev = crtc->dev;
  12523. struct drm_i915_private *dev_priv = to_i915(dev);
  12524. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12525. struct intel_crtc_state *intel_cstate =
  12526. to_intel_crtc_state(crtc->state);
  12527. struct intel_crtc_state *old_intel_cstate =
  12528. to_intel_crtc_state(old_crtc_state);
  12529. struct intel_atomic_state *old_intel_state =
  12530. to_intel_atomic_state(old_crtc_state->state);
  12531. bool modeset = needs_modeset(crtc->state);
  12532. /* Perform vblank evasion around commit operation */
  12533. intel_pipe_update_start(intel_crtc);
  12534. if (modeset)
  12535. goto out;
  12536. if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
  12537. intel_color_set_csc(crtc->state);
  12538. intel_color_load_luts(crtc->state);
  12539. }
  12540. if (intel_cstate->update_pipe)
  12541. intel_update_pipe_config(intel_crtc, old_intel_cstate);
  12542. else if (INTEL_GEN(dev_priv) >= 9)
  12543. skl_detach_scalers(intel_crtc);
  12544. out:
  12545. if (dev_priv->display.atomic_update_watermarks)
  12546. dev_priv->display.atomic_update_watermarks(old_intel_state,
  12547. intel_cstate);
  12548. }
  12549. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  12550. struct drm_crtc_state *old_crtc_state)
  12551. {
  12552. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12553. intel_pipe_update_end(intel_crtc, NULL);
  12554. }
  12555. /**
  12556. * intel_plane_destroy - destroy a plane
  12557. * @plane: plane to destroy
  12558. *
  12559. * Common destruction function for all types of planes (primary, cursor,
  12560. * sprite).
  12561. */
  12562. void intel_plane_destroy(struct drm_plane *plane)
  12563. {
  12564. drm_plane_cleanup(plane);
  12565. kfree(to_intel_plane(plane));
  12566. }
  12567. const struct drm_plane_funcs intel_plane_funcs = {
  12568. .update_plane = drm_atomic_helper_update_plane,
  12569. .disable_plane = drm_atomic_helper_disable_plane,
  12570. .destroy = intel_plane_destroy,
  12571. .set_property = drm_atomic_helper_plane_set_property,
  12572. .atomic_get_property = intel_plane_atomic_get_property,
  12573. .atomic_set_property = intel_plane_atomic_set_property,
  12574. .atomic_duplicate_state = intel_plane_duplicate_state,
  12575. .atomic_destroy_state = intel_plane_destroy_state,
  12576. };
  12577. static struct intel_plane *
  12578. intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  12579. {
  12580. struct intel_plane *primary = NULL;
  12581. struct intel_plane_state *state = NULL;
  12582. const uint32_t *intel_primary_formats;
  12583. unsigned int supported_rotations;
  12584. unsigned int num_formats;
  12585. int ret;
  12586. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  12587. if (!primary) {
  12588. ret = -ENOMEM;
  12589. goto fail;
  12590. }
  12591. state = intel_create_plane_state(&primary->base);
  12592. if (!state) {
  12593. ret = -ENOMEM;
  12594. goto fail;
  12595. }
  12596. primary->base.state = &state->base;
  12597. primary->can_scale = false;
  12598. primary->max_downscale = 1;
  12599. if (INTEL_GEN(dev_priv) >= 9) {
  12600. primary->can_scale = true;
  12601. state->scaler_id = -1;
  12602. }
  12603. primary->pipe = pipe;
  12604. /*
  12605. * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
  12606. * port is hooked to pipe B. Hence we want plane A feeding pipe B.
  12607. */
  12608. if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
  12609. primary->plane = (enum plane) !pipe;
  12610. else
  12611. primary->plane = (enum plane) pipe;
  12612. primary->id = PLANE_PRIMARY;
  12613. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  12614. primary->check_plane = intel_check_primary_plane;
  12615. if (INTEL_GEN(dev_priv) >= 9) {
  12616. intel_primary_formats = skl_primary_formats;
  12617. num_formats = ARRAY_SIZE(skl_primary_formats);
  12618. primary->update_plane = skylake_update_primary_plane;
  12619. primary->disable_plane = skylake_disable_primary_plane;
  12620. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12621. intel_primary_formats = i965_primary_formats;
  12622. num_formats = ARRAY_SIZE(i965_primary_formats);
  12623. primary->update_plane = ironlake_update_primary_plane;
  12624. primary->disable_plane = i9xx_disable_primary_plane;
  12625. } else if (INTEL_GEN(dev_priv) >= 4) {
  12626. intel_primary_formats = i965_primary_formats;
  12627. num_formats = ARRAY_SIZE(i965_primary_formats);
  12628. primary->update_plane = i9xx_update_primary_plane;
  12629. primary->disable_plane = i9xx_disable_primary_plane;
  12630. } else {
  12631. intel_primary_formats = i8xx_primary_formats;
  12632. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  12633. primary->update_plane = i9xx_update_primary_plane;
  12634. primary->disable_plane = i9xx_disable_primary_plane;
  12635. }
  12636. if (INTEL_GEN(dev_priv) >= 9)
  12637. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  12638. 0, &intel_plane_funcs,
  12639. intel_primary_formats, num_formats,
  12640. DRM_PLANE_TYPE_PRIMARY,
  12641. "plane 1%c", pipe_name(pipe));
  12642. else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  12643. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  12644. 0, &intel_plane_funcs,
  12645. intel_primary_formats, num_formats,
  12646. DRM_PLANE_TYPE_PRIMARY,
  12647. "primary %c", pipe_name(pipe));
  12648. else
  12649. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  12650. 0, &intel_plane_funcs,
  12651. intel_primary_formats, num_formats,
  12652. DRM_PLANE_TYPE_PRIMARY,
  12653. "plane %c", plane_name(primary->plane));
  12654. if (ret)
  12655. goto fail;
  12656. if (INTEL_GEN(dev_priv) >= 9) {
  12657. supported_rotations =
  12658. DRM_ROTATE_0 | DRM_ROTATE_90 |
  12659. DRM_ROTATE_180 | DRM_ROTATE_270;
  12660. } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  12661. supported_rotations =
  12662. DRM_ROTATE_0 | DRM_ROTATE_180 |
  12663. DRM_REFLECT_X;
  12664. } else if (INTEL_GEN(dev_priv) >= 4) {
  12665. supported_rotations =
  12666. DRM_ROTATE_0 | DRM_ROTATE_180;
  12667. } else {
  12668. supported_rotations = DRM_ROTATE_0;
  12669. }
  12670. if (INTEL_GEN(dev_priv) >= 4)
  12671. drm_plane_create_rotation_property(&primary->base,
  12672. DRM_ROTATE_0,
  12673. supported_rotations);
  12674. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  12675. return primary;
  12676. fail:
  12677. kfree(state);
  12678. kfree(primary);
  12679. return ERR_PTR(ret);
  12680. }
  12681. static int
  12682. intel_check_cursor_plane(struct drm_plane *plane,
  12683. struct intel_crtc_state *crtc_state,
  12684. struct intel_plane_state *state)
  12685. {
  12686. struct drm_framebuffer *fb = state->base.fb;
  12687. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  12688. enum pipe pipe = to_intel_plane(plane)->pipe;
  12689. unsigned stride;
  12690. int ret;
  12691. ret = drm_plane_helper_check_state(&state->base,
  12692. &state->clip,
  12693. DRM_PLANE_HELPER_NO_SCALING,
  12694. DRM_PLANE_HELPER_NO_SCALING,
  12695. true, true);
  12696. if (ret)
  12697. return ret;
  12698. /* if we want to turn off the cursor ignore width and height */
  12699. if (!obj)
  12700. return 0;
  12701. /* Check for which cursor types we support */
  12702. if (!cursor_size_ok(to_i915(plane->dev), state->base.crtc_w,
  12703. state->base.crtc_h)) {
  12704. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  12705. state->base.crtc_w, state->base.crtc_h);
  12706. return -EINVAL;
  12707. }
  12708. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  12709. if (obj->base.size < stride * state->base.crtc_h) {
  12710. DRM_DEBUG_KMS("buffer is too small\n");
  12711. return -ENOMEM;
  12712. }
  12713. if (fb->modifier != DRM_FORMAT_MOD_NONE) {
  12714. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  12715. return -EINVAL;
  12716. }
  12717. /*
  12718. * There's something wrong with the cursor on CHV pipe C.
  12719. * If it straddles the left edge of the screen then
  12720. * moving it away from the edge or disabling it often
  12721. * results in a pipe underrun, and often that can lead to
  12722. * dead pipe (constant underrun reported, and it scans
  12723. * out just a solid color). To recover from that, the
  12724. * display power well must be turned off and on again.
  12725. * Refuse the put the cursor into that compromised position.
  12726. */
  12727. if (IS_CHERRYVIEW(to_i915(plane->dev)) && pipe == PIPE_C &&
  12728. state->base.visible && state->base.crtc_x < 0) {
  12729. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  12730. return -EINVAL;
  12731. }
  12732. return 0;
  12733. }
  12734. static void
  12735. intel_disable_cursor_plane(struct drm_plane *plane,
  12736. struct drm_crtc *crtc)
  12737. {
  12738. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12739. intel_crtc->cursor_addr = 0;
  12740. intel_crtc_update_cursor(crtc, NULL);
  12741. }
  12742. static void
  12743. intel_update_cursor_plane(struct drm_plane *plane,
  12744. const struct intel_crtc_state *crtc_state,
  12745. const struct intel_plane_state *state)
  12746. {
  12747. struct drm_crtc *crtc = crtc_state->base.crtc;
  12748. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12749. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  12750. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  12751. uint32_t addr;
  12752. if (!obj)
  12753. addr = 0;
  12754. else if (!INTEL_INFO(dev_priv)->cursor_needs_physical)
  12755. addr = i915_gem_object_ggtt_offset(obj, NULL);
  12756. else
  12757. addr = obj->phys_handle->busaddr;
  12758. intel_crtc->cursor_addr = addr;
  12759. intel_crtc_update_cursor(crtc, state);
  12760. }
  12761. static struct intel_plane *
  12762. intel_cursor_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  12763. {
  12764. struct intel_plane *cursor = NULL;
  12765. struct intel_plane_state *state = NULL;
  12766. int ret;
  12767. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  12768. if (!cursor) {
  12769. ret = -ENOMEM;
  12770. goto fail;
  12771. }
  12772. state = intel_create_plane_state(&cursor->base);
  12773. if (!state) {
  12774. ret = -ENOMEM;
  12775. goto fail;
  12776. }
  12777. cursor->base.state = &state->base;
  12778. cursor->can_scale = false;
  12779. cursor->max_downscale = 1;
  12780. cursor->pipe = pipe;
  12781. cursor->plane = pipe;
  12782. cursor->id = PLANE_CURSOR;
  12783. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  12784. cursor->check_plane = intel_check_cursor_plane;
  12785. cursor->update_plane = intel_update_cursor_plane;
  12786. cursor->disable_plane = intel_disable_cursor_plane;
  12787. ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
  12788. 0, &intel_plane_funcs,
  12789. intel_cursor_formats,
  12790. ARRAY_SIZE(intel_cursor_formats),
  12791. DRM_PLANE_TYPE_CURSOR,
  12792. "cursor %c", pipe_name(pipe));
  12793. if (ret)
  12794. goto fail;
  12795. if (INTEL_GEN(dev_priv) >= 4)
  12796. drm_plane_create_rotation_property(&cursor->base,
  12797. DRM_ROTATE_0,
  12798. DRM_ROTATE_0 |
  12799. DRM_ROTATE_180);
  12800. if (INTEL_GEN(dev_priv) >= 9)
  12801. state->scaler_id = -1;
  12802. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  12803. return cursor;
  12804. fail:
  12805. kfree(state);
  12806. kfree(cursor);
  12807. return ERR_PTR(ret);
  12808. }
  12809. static void skl_init_scalers(struct drm_i915_private *dev_priv,
  12810. struct intel_crtc *crtc,
  12811. struct intel_crtc_state *crtc_state)
  12812. {
  12813. struct intel_crtc_scaler_state *scaler_state =
  12814. &crtc_state->scaler_state;
  12815. int i;
  12816. for (i = 0; i < crtc->num_scalers; i++) {
  12817. struct intel_scaler *scaler = &scaler_state->scalers[i];
  12818. scaler->in_use = 0;
  12819. scaler->mode = PS_SCALER_MODE_DYN;
  12820. }
  12821. scaler_state->scaler_id = -1;
  12822. }
  12823. static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
  12824. {
  12825. struct intel_crtc *intel_crtc;
  12826. struct intel_crtc_state *crtc_state = NULL;
  12827. struct intel_plane *primary = NULL;
  12828. struct intel_plane *cursor = NULL;
  12829. int sprite, ret;
  12830. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  12831. if (!intel_crtc)
  12832. return -ENOMEM;
  12833. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  12834. if (!crtc_state) {
  12835. ret = -ENOMEM;
  12836. goto fail;
  12837. }
  12838. intel_crtc->config = crtc_state;
  12839. intel_crtc->base.state = &crtc_state->base;
  12840. crtc_state->base.crtc = &intel_crtc->base;
  12841. /* initialize shared scalers */
  12842. if (INTEL_GEN(dev_priv) >= 9) {
  12843. if (pipe == PIPE_C)
  12844. intel_crtc->num_scalers = 1;
  12845. else
  12846. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  12847. skl_init_scalers(dev_priv, intel_crtc, crtc_state);
  12848. }
  12849. primary = intel_primary_plane_create(dev_priv, pipe);
  12850. if (IS_ERR(primary)) {
  12851. ret = PTR_ERR(primary);
  12852. goto fail;
  12853. }
  12854. intel_crtc->plane_ids_mask |= BIT(primary->id);
  12855. for_each_sprite(dev_priv, pipe, sprite) {
  12856. struct intel_plane *plane;
  12857. plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
  12858. if (IS_ERR(plane)) {
  12859. ret = PTR_ERR(plane);
  12860. goto fail;
  12861. }
  12862. intel_crtc->plane_ids_mask |= BIT(plane->id);
  12863. }
  12864. cursor = intel_cursor_plane_create(dev_priv, pipe);
  12865. if (IS_ERR(cursor)) {
  12866. ret = PTR_ERR(cursor);
  12867. goto fail;
  12868. }
  12869. intel_crtc->plane_ids_mask |= BIT(cursor->id);
  12870. ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
  12871. &primary->base, &cursor->base,
  12872. &intel_crtc_funcs,
  12873. "pipe %c", pipe_name(pipe));
  12874. if (ret)
  12875. goto fail;
  12876. intel_crtc->pipe = pipe;
  12877. intel_crtc->plane = primary->plane;
  12878. intel_crtc->cursor_base = ~0;
  12879. intel_crtc->cursor_cntl = ~0;
  12880. intel_crtc->cursor_size = ~0;
  12881. intel_crtc->wm.cxsr_allowed = true;
  12882. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  12883. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  12884. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
  12885. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
  12886. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  12887. intel_color_init(&intel_crtc->base);
  12888. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  12889. return 0;
  12890. fail:
  12891. /*
  12892. * drm_mode_config_cleanup() will free up any
  12893. * crtcs/planes already initialized.
  12894. */
  12895. kfree(crtc_state);
  12896. kfree(intel_crtc);
  12897. return ret;
  12898. }
  12899. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  12900. {
  12901. struct drm_encoder *encoder = connector->base.encoder;
  12902. struct drm_device *dev = connector->base.dev;
  12903. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  12904. if (!encoder || WARN_ON(!encoder->crtc))
  12905. return INVALID_PIPE;
  12906. return to_intel_crtc(encoder->crtc)->pipe;
  12907. }
  12908. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  12909. struct drm_file *file)
  12910. {
  12911. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  12912. struct drm_crtc *drmmode_crtc;
  12913. struct intel_crtc *crtc;
  12914. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  12915. if (!drmmode_crtc)
  12916. return -ENOENT;
  12917. crtc = to_intel_crtc(drmmode_crtc);
  12918. pipe_from_crtc_id->pipe = crtc->pipe;
  12919. return 0;
  12920. }
  12921. static int intel_encoder_clones(struct intel_encoder *encoder)
  12922. {
  12923. struct drm_device *dev = encoder->base.dev;
  12924. struct intel_encoder *source_encoder;
  12925. int index_mask = 0;
  12926. int entry = 0;
  12927. for_each_intel_encoder(dev, source_encoder) {
  12928. if (encoders_cloneable(encoder, source_encoder))
  12929. index_mask |= (1 << entry);
  12930. entry++;
  12931. }
  12932. return index_mask;
  12933. }
  12934. static bool has_edp_a(struct drm_i915_private *dev_priv)
  12935. {
  12936. if (!IS_MOBILE(dev_priv))
  12937. return false;
  12938. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  12939. return false;
  12940. if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  12941. return false;
  12942. return true;
  12943. }
  12944. static bool intel_crt_present(struct drm_i915_private *dev_priv)
  12945. {
  12946. if (INTEL_GEN(dev_priv) >= 9)
  12947. return false;
  12948. if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
  12949. return false;
  12950. if (IS_CHERRYVIEW(dev_priv))
  12951. return false;
  12952. if (HAS_PCH_LPT_H(dev_priv) &&
  12953. I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  12954. return false;
  12955. /* DDI E can't be used if DDI A requires 4 lanes */
  12956. if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  12957. return false;
  12958. if (!dev_priv->vbt.int_crt_support)
  12959. return false;
  12960. return true;
  12961. }
  12962. void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
  12963. {
  12964. int pps_num;
  12965. int pps_idx;
  12966. if (HAS_DDI(dev_priv))
  12967. return;
  12968. /*
  12969. * This w/a is needed at least on CPT/PPT, but to be sure apply it
  12970. * everywhere where registers can be write protected.
  12971. */
  12972. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12973. pps_num = 2;
  12974. else
  12975. pps_num = 1;
  12976. for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
  12977. u32 val = I915_READ(PP_CONTROL(pps_idx));
  12978. val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
  12979. I915_WRITE(PP_CONTROL(pps_idx), val);
  12980. }
  12981. }
  12982. static void intel_pps_init(struct drm_i915_private *dev_priv)
  12983. {
  12984. if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
  12985. dev_priv->pps_mmio_base = PCH_PPS_BASE;
  12986. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12987. dev_priv->pps_mmio_base = VLV_PPS_BASE;
  12988. else
  12989. dev_priv->pps_mmio_base = PPS_BASE;
  12990. intel_pps_unlock_regs_wa(dev_priv);
  12991. }
  12992. static void intel_setup_outputs(struct drm_i915_private *dev_priv)
  12993. {
  12994. struct intel_encoder *encoder;
  12995. bool dpd_is_edp = false;
  12996. intel_pps_init(dev_priv);
  12997. /*
  12998. * intel_edp_init_connector() depends on this completing first, to
  12999. * prevent the registeration of both eDP and LVDS and the incorrect
  13000. * sharing of the PPS.
  13001. */
  13002. intel_lvds_init(dev_priv);
  13003. if (intel_crt_present(dev_priv))
  13004. intel_crt_init(dev_priv);
  13005. if (IS_GEN9_LP(dev_priv)) {
  13006. /*
  13007. * FIXME: Broxton doesn't support port detection via the
  13008. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  13009. * detect the ports.
  13010. */
  13011. intel_ddi_init(dev_priv, PORT_A);
  13012. intel_ddi_init(dev_priv, PORT_B);
  13013. intel_ddi_init(dev_priv, PORT_C);
  13014. intel_dsi_init(dev_priv);
  13015. } else if (HAS_DDI(dev_priv)) {
  13016. int found;
  13017. /*
  13018. * Haswell uses DDI functions to detect digital outputs.
  13019. * On SKL pre-D0 the strap isn't connected, so we assume
  13020. * it's there.
  13021. */
  13022. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  13023. /* WaIgnoreDDIAStrap: skl */
  13024. if (found || IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  13025. intel_ddi_init(dev_priv, PORT_A);
  13026. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  13027. * register */
  13028. found = I915_READ(SFUSE_STRAP);
  13029. if (found & SFUSE_STRAP_DDIB_DETECTED)
  13030. intel_ddi_init(dev_priv, PORT_B);
  13031. if (found & SFUSE_STRAP_DDIC_DETECTED)
  13032. intel_ddi_init(dev_priv, PORT_C);
  13033. if (found & SFUSE_STRAP_DDID_DETECTED)
  13034. intel_ddi_init(dev_priv, PORT_D);
  13035. /*
  13036. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  13037. */
  13038. if ((IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) &&
  13039. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  13040. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  13041. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  13042. intel_ddi_init(dev_priv, PORT_E);
  13043. } else if (HAS_PCH_SPLIT(dev_priv)) {
  13044. int found;
  13045. dpd_is_edp = intel_dp_is_edp(dev_priv, PORT_D);
  13046. if (has_edp_a(dev_priv))
  13047. intel_dp_init(dev_priv, DP_A, PORT_A);
  13048. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  13049. /* PCH SDVOB multiplex with HDMIB */
  13050. found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
  13051. if (!found)
  13052. intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
  13053. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  13054. intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
  13055. }
  13056. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  13057. intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
  13058. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  13059. intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
  13060. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  13061. intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
  13062. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  13063. intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
  13064. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  13065. bool has_edp, has_port;
  13066. /*
  13067. * The DP_DETECTED bit is the latched state of the DDC
  13068. * SDA pin at boot. However since eDP doesn't require DDC
  13069. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  13070. * eDP ports may have been muxed to an alternate function.
  13071. * Thus we can't rely on the DP_DETECTED bit alone to detect
  13072. * eDP ports. Consult the VBT as well as DP_DETECTED to
  13073. * detect eDP ports.
  13074. *
  13075. * Sadly the straps seem to be missing sometimes even for HDMI
  13076. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  13077. * and VBT for the presence of the port. Additionally we can't
  13078. * trust the port type the VBT declares as we've seen at least
  13079. * HDMI ports that the VBT claim are DP or eDP.
  13080. */
  13081. has_edp = intel_dp_is_edp(dev_priv, PORT_B);
  13082. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  13083. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  13084. has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
  13085. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  13086. intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
  13087. has_edp = intel_dp_is_edp(dev_priv, PORT_C);
  13088. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  13089. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  13090. has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
  13091. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  13092. intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
  13093. if (IS_CHERRYVIEW(dev_priv)) {
  13094. /*
  13095. * eDP not supported on port D,
  13096. * so no need to worry about it
  13097. */
  13098. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  13099. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  13100. intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
  13101. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  13102. intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
  13103. }
  13104. intel_dsi_init(dev_priv);
  13105. } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
  13106. bool found = false;
  13107. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  13108. DRM_DEBUG_KMS("probing SDVOB\n");
  13109. found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
  13110. if (!found && IS_G4X(dev_priv)) {
  13111. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  13112. intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
  13113. }
  13114. if (!found && IS_G4X(dev_priv))
  13115. intel_dp_init(dev_priv, DP_B, PORT_B);
  13116. }
  13117. /* Before G4X SDVOC doesn't have its own detect register */
  13118. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  13119. DRM_DEBUG_KMS("probing SDVOC\n");
  13120. found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
  13121. }
  13122. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  13123. if (IS_G4X(dev_priv)) {
  13124. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  13125. intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
  13126. }
  13127. if (IS_G4X(dev_priv))
  13128. intel_dp_init(dev_priv, DP_C, PORT_C);
  13129. }
  13130. if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
  13131. intel_dp_init(dev_priv, DP_D, PORT_D);
  13132. } else if (IS_GEN2(dev_priv))
  13133. intel_dvo_init(dev_priv);
  13134. if (SUPPORTS_TV(dev_priv))
  13135. intel_tv_init(dev_priv);
  13136. intel_psr_init(dev_priv);
  13137. for_each_intel_encoder(&dev_priv->drm, encoder) {
  13138. encoder->base.possible_crtcs = encoder->crtc_mask;
  13139. encoder->base.possible_clones =
  13140. intel_encoder_clones(encoder);
  13141. }
  13142. intel_init_pch_refclk(dev_priv);
  13143. drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
  13144. }
  13145. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  13146. {
  13147. struct drm_device *dev = fb->dev;
  13148. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  13149. drm_framebuffer_cleanup(fb);
  13150. mutex_lock(&dev->struct_mutex);
  13151. WARN_ON(!intel_fb->obj->framebuffer_references--);
  13152. i915_gem_object_put(intel_fb->obj);
  13153. mutex_unlock(&dev->struct_mutex);
  13154. kfree(intel_fb);
  13155. }
  13156. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  13157. struct drm_file *file,
  13158. unsigned int *handle)
  13159. {
  13160. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  13161. struct drm_i915_gem_object *obj = intel_fb->obj;
  13162. if (obj->userptr.mm) {
  13163. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  13164. return -EINVAL;
  13165. }
  13166. return drm_gem_handle_create(file, &obj->base, handle);
  13167. }
  13168. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  13169. struct drm_file *file,
  13170. unsigned flags, unsigned color,
  13171. struct drm_clip_rect *clips,
  13172. unsigned num_clips)
  13173. {
  13174. struct drm_device *dev = fb->dev;
  13175. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  13176. struct drm_i915_gem_object *obj = intel_fb->obj;
  13177. mutex_lock(&dev->struct_mutex);
  13178. if (obj->pin_display && obj->cache_dirty)
  13179. i915_gem_clflush_object(obj, true);
  13180. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  13181. mutex_unlock(&dev->struct_mutex);
  13182. return 0;
  13183. }
  13184. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  13185. .destroy = intel_user_framebuffer_destroy,
  13186. .create_handle = intel_user_framebuffer_create_handle,
  13187. .dirty = intel_user_framebuffer_dirty,
  13188. };
  13189. static
  13190. u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
  13191. uint64_t fb_modifier, uint32_t pixel_format)
  13192. {
  13193. u32 gen = INTEL_INFO(dev_priv)->gen;
  13194. if (gen >= 9) {
  13195. int cpp = drm_format_plane_cpp(pixel_format, 0);
  13196. /* "The stride in bytes must not exceed the of the size of 8K
  13197. * pixels and 32K bytes."
  13198. */
  13199. return min(8192 * cpp, 32768);
  13200. } else if (gen >= 5 && !IS_VALLEYVIEW(dev_priv) &&
  13201. !IS_CHERRYVIEW(dev_priv)) {
  13202. return 32*1024;
  13203. } else if (gen >= 4) {
  13204. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  13205. return 16*1024;
  13206. else
  13207. return 32*1024;
  13208. } else if (gen >= 3) {
  13209. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  13210. return 8*1024;
  13211. else
  13212. return 16*1024;
  13213. } else {
  13214. /* XXX DSPC is limited to 4k tiled */
  13215. return 8*1024;
  13216. }
  13217. }
  13218. static int intel_framebuffer_init(struct drm_device *dev,
  13219. struct intel_framebuffer *intel_fb,
  13220. struct drm_mode_fb_cmd2 *mode_cmd,
  13221. struct drm_i915_gem_object *obj)
  13222. {
  13223. struct drm_i915_private *dev_priv = to_i915(dev);
  13224. unsigned int tiling = i915_gem_object_get_tiling(obj);
  13225. int ret;
  13226. u32 pitch_limit, stride_alignment;
  13227. struct drm_format_name_buf format_name;
  13228. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  13229. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  13230. /*
  13231. * If there's a fence, enforce that
  13232. * the fb modifier and tiling mode match.
  13233. */
  13234. if (tiling != I915_TILING_NONE &&
  13235. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  13236. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  13237. return -EINVAL;
  13238. }
  13239. } else {
  13240. if (tiling == I915_TILING_X) {
  13241. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  13242. } else if (tiling == I915_TILING_Y) {
  13243. DRM_DEBUG("No Y tiling for legacy addfb\n");
  13244. return -EINVAL;
  13245. }
  13246. }
  13247. /* Passed in modifier sanity checking. */
  13248. switch (mode_cmd->modifier[0]) {
  13249. case I915_FORMAT_MOD_Y_TILED:
  13250. case I915_FORMAT_MOD_Yf_TILED:
  13251. if (INTEL_GEN(dev_priv) < 9) {
  13252. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  13253. mode_cmd->modifier[0]);
  13254. return -EINVAL;
  13255. }
  13256. case DRM_FORMAT_MOD_NONE:
  13257. case I915_FORMAT_MOD_X_TILED:
  13258. break;
  13259. default:
  13260. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  13261. mode_cmd->modifier[0]);
  13262. return -EINVAL;
  13263. }
  13264. /*
  13265. * gen2/3 display engine uses the fence if present,
  13266. * so the tiling mode must match the fb modifier exactly.
  13267. */
  13268. if (INTEL_INFO(dev_priv)->gen < 4 &&
  13269. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  13270. DRM_DEBUG("tiling_mode must match fb modifier exactly on gen2/3\n");
  13271. return -EINVAL;
  13272. }
  13273. stride_alignment = intel_fb_stride_alignment(dev_priv,
  13274. mode_cmd->modifier[0],
  13275. mode_cmd->pixel_format);
  13276. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  13277. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  13278. mode_cmd->pitches[0], stride_alignment);
  13279. return -EINVAL;
  13280. }
  13281. pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
  13282. mode_cmd->pixel_format);
  13283. if (mode_cmd->pitches[0] > pitch_limit) {
  13284. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  13285. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  13286. "tiled" : "linear",
  13287. mode_cmd->pitches[0], pitch_limit);
  13288. return -EINVAL;
  13289. }
  13290. /*
  13291. * If there's a fence, enforce that
  13292. * the fb pitch and fence stride match.
  13293. */
  13294. if (tiling != I915_TILING_NONE &&
  13295. mode_cmd->pitches[0] != i915_gem_object_get_stride(obj)) {
  13296. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  13297. mode_cmd->pitches[0],
  13298. i915_gem_object_get_stride(obj));
  13299. return -EINVAL;
  13300. }
  13301. /* Reject formats not supported by any plane early. */
  13302. switch (mode_cmd->pixel_format) {
  13303. case DRM_FORMAT_C8:
  13304. case DRM_FORMAT_RGB565:
  13305. case DRM_FORMAT_XRGB8888:
  13306. case DRM_FORMAT_ARGB8888:
  13307. break;
  13308. case DRM_FORMAT_XRGB1555:
  13309. if (INTEL_GEN(dev_priv) > 3) {
  13310. DRM_DEBUG("unsupported pixel format: %s\n",
  13311. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  13312. return -EINVAL;
  13313. }
  13314. break;
  13315. case DRM_FORMAT_ABGR8888:
  13316. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  13317. INTEL_GEN(dev_priv) < 9) {
  13318. DRM_DEBUG("unsupported pixel format: %s\n",
  13319. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  13320. return -EINVAL;
  13321. }
  13322. break;
  13323. case DRM_FORMAT_XBGR8888:
  13324. case DRM_FORMAT_XRGB2101010:
  13325. case DRM_FORMAT_XBGR2101010:
  13326. if (INTEL_GEN(dev_priv) < 4) {
  13327. DRM_DEBUG("unsupported pixel format: %s\n",
  13328. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  13329. return -EINVAL;
  13330. }
  13331. break;
  13332. case DRM_FORMAT_ABGR2101010:
  13333. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  13334. DRM_DEBUG("unsupported pixel format: %s\n",
  13335. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  13336. return -EINVAL;
  13337. }
  13338. break;
  13339. case DRM_FORMAT_YUYV:
  13340. case DRM_FORMAT_UYVY:
  13341. case DRM_FORMAT_YVYU:
  13342. case DRM_FORMAT_VYUY:
  13343. if (INTEL_GEN(dev_priv) < 5) {
  13344. DRM_DEBUG("unsupported pixel format: %s\n",
  13345. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  13346. return -EINVAL;
  13347. }
  13348. break;
  13349. default:
  13350. DRM_DEBUG("unsupported pixel format: %s\n",
  13351. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  13352. return -EINVAL;
  13353. }
  13354. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  13355. if (mode_cmd->offsets[0] != 0)
  13356. return -EINVAL;
  13357. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  13358. intel_fb->obj = obj;
  13359. ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
  13360. if (ret)
  13361. return ret;
  13362. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  13363. if (ret) {
  13364. DRM_ERROR("framebuffer init failed %d\n", ret);
  13365. return ret;
  13366. }
  13367. intel_fb->obj->framebuffer_references++;
  13368. return 0;
  13369. }
  13370. static struct drm_framebuffer *
  13371. intel_user_framebuffer_create(struct drm_device *dev,
  13372. struct drm_file *filp,
  13373. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  13374. {
  13375. struct drm_framebuffer *fb;
  13376. struct drm_i915_gem_object *obj;
  13377. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  13378. obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
  13379. if (!obj)
  13380. return ERR_PTR(-ENOENT);
  13381. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  13382. if (IS_ERR(fb))
  13383. i915_gem_object_put(obj);
  13384. return fb;
  13385. }
  13386. static void intel_atomic_state_free(struct drm_atomic_state *state)
  13387. {
  13388. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  13389. drm_atomic_state_default_release(state);
  13390. i915_sw_fence_fini(&intel_state->commit_ready);
  13391. kfree(state);
  13392. }
  13393. static const struct drm_mode_config_funcs intel_mode_funcs = {
  13394. .fb_create = intel_user_framebuffer_create,
  13395. .output_poll_changed = intel_fbdev_output_poll_changed,
  13396. .atomic_check = intel_atomic_check,
  13397. .atomic_commit = intel_atomic_commit,
  13398. .atomic_state_alloc = intel_atomic_state_alloc,
  13399. .atomic_state_clear = intel_atomic_state_clear,
  13400. .atomic_state_free = intel_atomic_state_free,
  13401. };
  13402. /**
  13403. * intel_init_display_hooks - initialize the display modesetting hooks
  13404. * @dev_priv: device private
  13405. */
  13406. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  13407. {
  13408. if (INTEL_INFO(dev_priv)->gen >= 9) {
  13409. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  13410. dev_priv->display.get_initial_plane_config =
  13411. skylake_get_initial_plane_config;
  13412. dev_priv->display.crtc_compute_clock =
  13413. haswell_crtc_compute_clock;
  13414. dev_priv->display.crtc_enable = haswell_crtc_enable;
  13415. dev_priv->display.crtc_disable = haswell_crtc_disable;
  13416. } else if (HAS_DDI(dev_priv)) {
  13417. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  13418. dev_priv->display.get_initial_plane_config =
  13419. ironlake_get_initial_plane_config;
  13420. dev_priv->display.crtc_compute_clock =
  13421. haswell_crtc_compute_clock;
  13422. dev_priv->display.crtc_enable = haswell_crtc_enable;
  13423. dev_priv->display.crtc_disable = haswell_crtc_disable;
  13424. } else if (HAS_PCH_SPLIT(dev_priv)) {
  13425. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  13426. dev_priv->display.get_initial_plane_config =
  13427. ironlake_get_initial_plane_config;
  13428. dev_priv->display.crtc_compute_clock =
  13429. ironlake_crtc_compute_clock;
  13430. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  13431. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  13432. } else if (IS_CHERRYVIEW(dev_priv)) {
  13433. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13434. dev_priv->display.get_initial_plane_config =
  13435. i9xx_get_initial_plane_config;
  13436. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  13437. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  13438. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13439. } else if (IS_VALLEYVIEW(dev_priv)) {
  13440. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13441. dev_priv->display.get_initial_plane_config =
  13442. i9xx_get_initial_plane_config;
  13443. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  13444. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  13445. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13446. } else if (IS_G4X(dev_priv)) {
  13447. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13448. dev_priv->display.get_initial_plane_config =
  13449. i9xx_get_initial_plane_config;
  13450. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  13451. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13452. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13453. } else if (IS_PINEVIEW(dev_priv)) {
  13454. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13455. dev_priv->display.get_initial_plane_config =
  13456. i9xx_get_initial_plane_config;
  13457. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  13458. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13459. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13460. } else if (!IS_GEN2(dev_priv)) {
  13461. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13462. dev_priv->display.get_initial_plane_config =
  13463. i9xx_get_initial_plane_config;
  13464. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  13465. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13466. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13467. } else {
  13468. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13469. dev_priv->display.get_initial_plane_config =
  13470. i9xx_get_initial_plane_config;
  13471. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  13472. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13473. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13474. }
  13475. /* Returns the core display clock speed */
  13476. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  13477. dev_priv->display.get_display_clock_speed =
  13478. skylake_get_display_clock_speed;
  13479. else if (IS_GEN9_LP(dev_priv))
  13480. dev_priv->display.get_display_clock_speed =
  13481. broxton_get_display_clock_speed;
  13482. else if (IS_BROADWELL(dev_priv))
  13483. dev_priv->display.get_display_clock_speed =
  13484. broadwell_get_display_clock_speed;
  13485. else if (IS_HASWELL(dev_priv))
  13486. dev_priv->display.get_display_clock_speed =
  13487. haswell_get_display_clock_speed;
  13488. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  13489. dev_priv->display.get_display_clock_speed =
  13490. valleyview_get_display_clock_speed;
  13491. else if (IS_GEN5(dev_priv))
  13492. dev_priv->display.get_display_clock_speed =
  13493. ilk_get_display_clock_speed;
  13494. else if (IS_I945G(dev_priv) || IS_I965G(dev_priv) ||
  13495. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  13496. dev_priv->display.get_display_clock_speed =
  13497. i945_get_display_clock_speed;
  13498. else if (IS_GM45(dev_priv))
  13499. dev_priv->display.get_display_clock_speed =
  13500. gm45_get_display_clock_speed;
  13501. else if (IS_I965GM(dev_priv))
  13502. dev_priv->display.get_display_clock_speed =
  13503. i965gm_get_display_clock_speed;
  13504. else if (IS_PINEVIEW(dev_priv))
  13505. dev_priv->display.get_display_clock_speed =
  13506. pnv_get_display_clock_speed;
  13507. else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
  13508. dev_priv->display.get_display_clock_speed =
  13509. g33_get_display_clock_speed;
  13510. else if (IS_I915G(dev_priv))
  13511. dev_priv->display.get_display_clock_speed =
  13512. i915_get_display_clock_speed;
  13513. else if (IS_I945GM(dev_priv) || IS_I845G(dev_priv))
  13514. dev_priv->display.get_display_clock_speed =
  13515. i9xx_misc_get_display_clock_speed;
  13516. else if (IS_I915GM(dev_priv))
  13517. dev_priv->display.get_display_clock_speed =
  13518. i915gm_get_display_clock_speed;
  13519. else if (IS_I865G(dev_priv))
  13520. dev_priv->display.get_display_clock_speed =
  13521. i865_get_display_clock_speed;
  13522. else if (IS_I85X(dev_priv))
  13523. dev_priv->display.get_display_clock_speed =
  13524. i85x_get_display_clock_speed;
  13525. else { /* 830 */
  13526. WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
  13527. dev_priv->display.get_display_clock_speed =
  13528. i830_get_display_clock_speed;
  13529. }
  13530. if (IS_GEN5(dev_priv)) {
  13531. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  13532. } else if (IS_GEN6(dev_priv)) {
  13533. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  13534. } else if (IS_IVYBRIDGE(dev_priv)) {
  13535. /* FIXME: detect B0+ stepping and use auto training */
  13536. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  13537. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  13538. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  13539. }
  13540. if (IS_BROADWELL(dev_priv)) {
  13541. dev_priv->display.modeset_commit_cdclk =
  13542. broadwell_modeset_commit_cdclk;
  13543. dev_priv->display.modeset_calc_cdclk =
  13544. broadwell_modeset_calc_cdclk;
  13545. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  13546. dev_priv->display.modeset_commit_cdclk =
  13547. valleyview_modeset_commit_cdclk;
  13548. dev_priv->display.modeset_calc_cdclk =
  13549. valleyview_modeset_calc_cdclk;
  13550. } else if (IS_GEN9_LP(dev_priv)) {
  13551. dev_priv->display.modeset_commit_cdclk =
  13552. bxt_modeset_commit_cdclk;
  13553. dev_priv->display.modeset_calc_cdclk =
  13554. bxt_modeset_calc_cdclk;
  13555. } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
  13556. dev_priv->display.modeset_commit_cdclk =
  13557. skl_modeset_commit_cdclk;
  13558. dev_priv->display.modeset_calc_cdclk =
  13559. skl_modeset_calc_cdclk;
  13560. }
  13561. if (dev_priv->info.gen >= 9)
  13562. dev_priv->display.update_crtcs = skl_update_crtcs;
  13563. else
  13564. dev_priv->display.update_crtcs = intel_update_crtcs;
  13565. switch (INTEL_INFO(dev_priv)->gen) {
  13566. case 2:
  13567. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  13568. break;
  13569. case 3:
  13570. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  13571. break;
  13572. case 4:
  13573. case 5:
  13574. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  13575. break;
  13576. case 6:
  13577. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  13578. break;
  13579. case 7:
  13580. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  13581. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  13582. break;
  13583. case 9:
  13584. /* Drop through - unsupported since execlist only. */
  13585. default:
  13586. /* Default just returns -ENODEV to indicate unsupported */
  13587. dev_priv->display.queue_flip = intel_default_queue_flip;
  13588. }
  13589. }
  13590. /*
  13591. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  13592. * resume, or other times. This quirk makes sure that's the case for
  13593. * affected systems.
  13594. */
  13595. static void quirk_pipea_force(struct drm_device *dev)
  13596. {
  13597. struct drm_i915_private *dev_priv = to_i915(dev);
  13598. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  13599. DRM_INFO("applying pipe a force quirk\n");
  13600. }
  13601. static void quirk_pipeb_force(struct drm_device *dev)
  13602. {
  13603. struct drm_i915_private *dev_priv = to_i915(dev);
  13604. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  13605. DRM_INFO("applying pipe b force quirk\n");
  13606. }
  13607. /*
  13608. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  13609. */
  13610. static void quirk_ssc_force_disable(struct drm_device *dev)
  13611. {
  13612. struct drm_i915_private *dev_priv = to_i915(dev);
  13613. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  13614. DRM_INFO("applying lvds SSC disable quirk\n");
  13615. }
  13616. /*
  13617. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  13618. * brightness value
  13619. */
  13620. static void quirk_invert_brightness(struct drm_device *dev)
  13621. {
  13622. struct drm_i915_private *dev_priv = to_i915(dev);
  13623. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  13624. DRM_INFO("applying inverted panel brightness quirk\n");
  13625. }
  13626. /* Some VBT's incorrectly indicate no backlight is present */
  13627. static void quirk_backlight_present(struct drm_device *dev)
  13628. {
  13629. struct drm_i915_private *dev_priv = to_i915(dev);
  13630. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  13631. DRM_INFO("applying backlight present quirk\n");
  13632. }
  13633. struct intel_quirk {
  13634. int device;
  13635. int subsystem_vendor;
  13636. int subsystem_device;
  13637. void (*hook)(struct drm_device *dev);
  13638. };
  13639. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  13640. struct intel_dmi_quirk {
  13641. void (*hook)(struct drm_device *dev);
  13642. const struct dmi_system_id (*dmi_id_list)[];
  13643. };
  13644. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  13645. {
  13646. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  13647. return 1;
  13648. }
  13649. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  13650. {
  13651. .dmi_id_list = &(const struct dmi_system_id[]) {
  13652. {
  13653. .callback = intel_dmi_reverse_brightness,
  13654. .ident = "NCR Corporation",
  13655. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  13656. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  13657. },
  13658. },
  13659. { } /* terminating entry */
  13660. },
  13661. .hook = quirk_invert_brightness,
  13662. },
  13663. };
  13664. static struct intel_quirk intel_quirks[] = {
  13665. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  13666. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  13667. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  13668. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  13669. /* 830 needs to leave pipe A & dpll A up */
  13670. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  13671. /* 830 needs to leave pipe B & dpll B up */
  13672. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  13673. /* Lenovo U160 cannot use SSC on LVDS */
  13674. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  13675. /* Sony Vaio Y cannot use SSC on LVDS */
  13676. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  13677. /* Acer Aspire 5734Z must invert backlight brightness */
  13678. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  13679. /* Acer/eMachines G725 */
  13680. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  13681. /* Acer/eMachines e725 */
  13682. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  13683. /* Acer/Packard Bell NCL20 */
  13684. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  13685. /* Acer Aspire 4736Z */
  13686. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  13687. /* Acer Aspire 5336 */
  13688. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  13689. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  13690. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  13691. /* Acer C720 Chromebook (Core i3 4005U) */
  13692. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  13693. /* Apple Macbook 2,1 (Core 2 T7400) */
  13694. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  13695. /* Apple Macbook 4,1 */
  13696. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  13697. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  13698. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  13699. /* HP Chromebook 14 (Celeron 2955U) */
  13700. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  13701. /* Dell Chromebook 11 */
  13702. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  13703. /* Dell Chromebook 11 (2015 version) */
  13704. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  13705. };
  13706. static void intel_init_quirks(struct drm_device *dev)
  13707. {
  13708. struct pci_dev *d = dev->pdev;
  13709. int i;
  13710. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  13711. struct intel_quirk *q = &intel_quirks[i];
  13712. if (d->device == q->device &&
  13713. (d->subsystem_vendor == q->subsystem_vendor ||
  13714. q->subsystem_vendor == PCI_ANY_ID) &&
  13715. (d->subsystem_device == q->subsystem_device ||
  13716. q->subsystem_device == PCI_ANY_ID))
  13717. q->hook(dev);
  13718. }
  13719. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  13720. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  13721. intel_dmi_quirks[i].hook(dev);
  13722. }
  13723. }
  13724. /* Disable the VGA plane that we never use */
  13725. static void i915_disable_vga(struct drm_i915_private *dev_priv)
  13726. {
  13727. struct pci_dev *pdev = dev_priv->drm.pdev;
  13728. u8 sr1;
  13729. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  13730. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  13731. vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
  13732. outb(SR01, VGA_SR_INDEX);
  13733. sr1 = inb(VGA_SR_DATA);
  13734. outb(sr1 | 1<<5, VGA_SR_DATA);
  13735. vga_put(pdev, VGA_RSRC_LEGACY_IO);
  13736. udelay(300);
  13737. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  13738. POSTING_READ(vga_reg);
  13739. }
  13740. void intel_modeset_init_hw(struct drm_device *dev)
  13741. {
  13742. struct drm_i915_private *dev_priv = to_i915(dev);
  13743. intel_update_cdclk(dev_priv);
  13744. dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
  13745. intel_init_clock_gating(dev_priv);
  13746. }
  13747. /*
  13748. * Calculate what we think the watermarks should be for the state we've read
  13749. * out of the hardware and then immediately program those watermarks so that
  13750. * we ensure the hardware settings match our internal state.
  13751. *
  13752. * We can calculate what we think WM's should be by creating a duplicate of the
  13753. * current state (which was constructed during hardware readout) and running it
  13754. * through the atomic check code to calculate new watermark values in the
  13755. * state object.
  13756. */
  13757. static void sanitize_watermarks(struct drm_device *dev)
  13758. {
  13759. struct drm_i915_private *dev_priv = to_i915(dev);
  13760. struct drm_atomic_state *state;
  13761. struct intel_atomic_state *intel_state;
  13762. struct drm_crtc *crtc;
  13763. struct drm_crtc_state *cstate;
  13764. struct drm_modeset_acquire_ctx ctx;
  13765. int ret;
  13766. int i;
  13767. /* Only supported on platforms that use atomic watermark design */
  13768. if (!dev_priv->display.optimize_watermarks)
  13769. return;
  13770. /*
  13771. * We need to hold connection_mutex before calling duplicate_state so
  13772. * that the connector loop is protected.
  13773. */
  13774. drm_modeset_acquire_init(&ctx, 0);
  13775. retry:
  13776. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  13777. if (ret == -EDEADLK) {
  13778. drm_modeset_backoff(&ctx);
  13779. goto retry;
  13780. } else if (WARN_ON(ret)) {
  13781. goto fail;
  13782. }
  13783. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  13784. if (WARN_ON(IS_ERR(state)))
  13785. goto fail;
  13786. intel_state = to_intel_atomic_state(state);
  13787. /*
  13788. * Hardware readout is the only time we don't want to calculate
  13789. * intermediate watermarks (since we don't trust the current
  13790. * watermarks).
  13791. */
  13792. intel_state->skip_intermediate_wm = true;
  13793. ret = intel_atomic_check(dev, state);
  13794. if (ret) {
  13795. /*
  13796. * If we fail here, it means that the hardware appears to be
  13797. * programmed in a way that shouldn't be possible, given our
  13798. * understanding of watermark requirements. This might mean a
  13799. * mistake in the hardware readout code or a mistake in the
  13800. * watermark calculations for a given platform. Raise a WARN
  13801. * so that this is noticeable.
  13802. *
  13803. * If this actually happens, we'll have to just leave the
  13804. * BIOS-programmed watermarks untouched and hope for the best.
  13805. */
  13806. WARN(true, "Could not determine valid watermarks for inherited state\n");
  13807. goto put_state;
  13808. }
  13809. /* Write calculated watermark values back */
  13810. for_each_crtc_in_state(state, crtc, cstate, i) {
  13811. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  13812. cs->wm.need_postvbl_update = true;
  13813. dev_priv->display.optimize_watermarks(intel_state, cs);
  13814. }
  13815. put_state:
  13816. drm_atomic_state_put(state);
  13817. fail:
  13818. drm_modeset_drop_locks(&ctx);
  13819. drm_modeset_acquire_fini(&ctx);
  13820. }
  13821. int intel_modeset_init(struct drm_device *dev)
  13822. {
  13823. struct drm_i915_private *dev_priv = to_i915(dev);
  13824. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  13825. enum pipe pipe;
  13826. struct intel_crtc *crtc;
  13827. drm_mode_config_init(dev);
  13828. dev->mode_config.min_width = 0;
  13829. dev->mode_config.min_height = 0;
  13830. dev->mode_config.preferred_depth = 24;
  13831. dev->mode_config.prefer_shadow = 1;
  13832. dev->mode_config.allow_fb_modifiers = true;
  13833. dev->mode_config.funcs = &intel_mode_funcs;
  13834. intel_init_quirks(dev);
  13835. intel_init_pm(dev_priv);
  13836. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  13837. return 0;
  13838. /*
  13839. * There may be no VBT; and if the BIOS enabled SSC we can
  13840. * just keep using it to avoid unnecessary flicker. Whereas if the
  13841. * BIOS isn't using it, don't assume it will work even if the VBT
  13842. * indicates as much.
  13843. */
  13844. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
  13845. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  13846. DREF_SSC1_ENABLE);
  13847. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  13848. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  13849. bios_lvds_use_ssc ? "en" : "dis",
  13850. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  13851. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  13852. }
  13853. }
  13854. if (IS_GEN2(dev_priv)) {
  13855. dev->mode_config.max_width = 2048;
  13856. dev->mode_config.max_height = 2048;
  13857. } else if (IS_GEN3(dev_priv)) {
  13858. dev->mode_config.max_width = 4096;
  13859. dev->mode_config.max_height = 4096;
  13860. } else {
  13861. dev->mode_config.max_width = 8192;
  13862. dev->mode_config.max_height = 8192;
  13863. }
  13864. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  13865. dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
  13866. dev->mode_config.cursor_height = 1023;
  13867. } else if (IS_GEN2(dev_priv)) {
  13868. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  13869. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  13870. } else {
  13871. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  13872. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  13873. }
  13874. dev->mode_config.fb_base = ggtt->mappable_base;
  13875. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  13876. INTEL_INFO(dev_priv)->num_pipes,
  13877. INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
  13878. for_each_pipe(dev_priv, pipe) {
  13879. int ret;
  13880. ret = intel_crtc_init(dev_priv, pipe);
  13881. if (ret) {
  13882. drm_mode_config_cleanup(dev);
  13883. return ret;
  13884. }
  13885. }
  13886. intel_update_czclk(dev_priv);
  13887. intel_update_cdclk(dev_priv);
  13888. dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
  13889. intel_shared_dpll_init(dev);
  13890. if (dev_priv->max_cdclk_freq == 0)
  13891. intel_update_max_cdclk(dev_priv);
  13892. /* Just disable it once at startup */
  13893. i915_disable_vga(dev_priv);
  13894. intel_setup_outputs(dev_priv);
  13895. drm_modeset_lock_all(dev);
  13896. intel_modeset_setup_hw_state(dev);
  13897. drm_modeset_unlock_all(dev);
  13898. for_each_intel_crtc(dev, crtc) {
  13899. struct intel_initial_plane_config plane_config = {};
  13900. if (!crtc->active)
  13901. continue;
  13902. /*
  13903. * Note that reserving the BIOS fb up front prevents us
  13904. * from stuffing other stolen allocations like the ring
  13905. * on top. This prevents some ugliness at boot time, and
  13906. * can even allow for smooth boot transitions if the BIOS
  13907. * fb is large enough for the active pipe configuration.
  13908. */
  13909. dev_priv->display.get_initial_plane_config(crtc,
  13910. &plane_config);
  13911. /*
  13912. * If the fb is shared between multiple heads, we'll
  13913. * just get the first one.
  13914. */
  13915. intel_find_initial_plane_obj(crtc, &plane_config);
  13916. }
  13917. /*
  13918. * Make sure hardware watermarks really match the state we read out.
  13919. * Note that we need to do this after reconstructing the BIOS fb's
  13920. * since the watermark calculation done here will use pstate->fb.
  13921. */
  13922. sanitize_watermarks(dev);
  13923. return 0;
  13924. }
  13925. static void intel_enable_pipe_a(struct drm_device *dev)
  13926. {
  13927. struct intel_connector *connector;
  13928. struct drm_connector *crt = NULL;
  13929. struct intel_load_detect_pipe load_detect_temp;
  13930. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  13931. /* We can't just switch on the pipe A, we need to set things up with a
  13932. * proper mode and output configuration. As a gross hack, enable pipe A
  13933. * by enabling the load detect pipe once. */
  13934. for_each_intel_connector(dev, connector) {
  13935. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  13936. crt = &connector->base;
  13937. break;
  13938. }
  13939. }
  13940. if (!crt)
  13941. return;
  13942. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  13943. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  13944. }
  13945. static bool
  13946. intel_check_plane_mapping(struct intel_crtc *crtc)
  13947. {
  13948. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  13949. u32 val;
  13950. if (INTEL_INFO(dev_priv)->num_pipes == 1)
  13951. return true;
  13952. val = I915_READ(DSPCNTR(!crtc->plane));
  13953. if ((val & DISPLAY_PLANE_ENABLE) &&
  13954. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  13955. return false;
  13956. return true;
  13957. }
  13958. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  13959. {
  13960. struct drm_device *dev = crtc->base.dev;
  13961. struct intel_encoder *encoder;
  13962. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  13963. return true;
  13964. return false;
  13965. }
  13966. static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
  13967. {
  13968. struct drm_device *dev = encoder->base.dev;
  13969. struct intel_connector *connector;
  13970. for_each_connector_on_encoder(dev, &encoder->base, connector)
  13971. return connector;
  13972. return NULL;
  13973. }
  13974. static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
  13975. enum transcoder pch_transcoder)
  13976. {
  13977. return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
  13978. (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
  13979. }
  13980. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  13981. {
  13982. struct drm_device *dev = crtc->base.dev;
  13983. struct drm_i915_private *dev_priv = to_i915(dev);
  13984. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  13985. /* Clear any frame start delays used for debugging left by the BIOS */
  13986. if (!transcoder_is_dsi(cpu_transcoder)) {
  13987. i915_reg_t reg = PIPECONF(cpu_transcoder);
  13988. I915_WRITE(reg,
  13989. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  13990. }
  13991. /* restore vblank interrupts to correct state */
  13992. drm_crtc_vblank_reset(&crtc->base);
  13993. if (crtc->active) {
  13994. struct intel_plane *plane;
  13995. drm_crtc_vblank_on(&crtc->base);
  13996. /* Disable everything but the primary plane */
  13997. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  13998. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  13999. continue;
  14000. plane->disable_plane(&plane->base, &crtc->base);
  14001. }
  14002. }
  14003. /* We need to sanitize the plane -> pipe mapping first because this will
  14004. * disable the crtc (and hence change the state) if it is wrong. Note
  14005. * that gen4+ has a fixed plane -> pipe mapping. */
  14006. if (INTEL_GEN(dev_priv) < 4 && !intel_check_plane_mapping(crtc)) {
  14007. bool plane;
  14008. DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
  14009. crtc->base.base.id, crtc->base.name);
  14010. /* Pipe has the wrong plane attached and the plane is active.
  14011. * Temporarily change the plane mapping and disable everything
  14012. * ... */
  14013. plane = crtc->plane;
  14014. to_intel_plane_state(crtc->base.primary->state)->base.visible = true;
  14015. crtc->plane = !plane;
  14016. intel_crtc_disable_noatomic(&crtc->base);
  14017. crtc->plane = plane;
  14018. }
  14019. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  14020. crtc->pipe == PIPE_A && !crtc->active) {
  14021. /* BIOS forgot to enable pipe A, this mostly happens after
  14022. * resume. Force-enable the pipe to fix this, the update_dpms
  14023. * call below we restore the pipe to the right state, but leave
  14024. * the required bits on. */
  14025. intel_enable_pipe_a(dev);
  14026. }
  14027. /* Adjust the state of the output pipe according to whether we
  14028. * have active connectors/encoders. */
  14029. if (crtc->active && !intel_crtc_has_encoders(crtc))
  14030. intel_crtc_disable_noatomic(&crtc->base);
  14031. if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
  14032. /*
  14033. * We start out with underrun reporting disabled to avoid races.
  14034. * For correct bookkeeping mark this on active crtcs.
  14035. *
  14036. * Also on gmch platforms we dont have any hardware bits to
  14037. * disable the underrun reporting. Which means we need to start
  14038. * out with underrun reporting disabled also on inactive pipes,
  14039. * since otherwise we'll complain about the garbage we read when
  14040. * e.g. coming up after runtime pm.
  14041. *
  14042. * No protection against concurrent access is required - at
  14043. * worst a fifo underrun happens which also sets this to false.
  14044. */
  14045. crtc->cpu_fifo_underrun_disabled = true;
  14046. /*
  14047. * We track the PCH trancoder underrun reporting state
  14048. * within the crtc. With crtc for pipe A housing the underrun
  14049. * reporting state for PCH transcoder A, crtc for pipe B housing
  14050. * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
  14051. * and marking underrun reporting as disabled for the non-existing
  14052. * PCH transcoders B and C would prevent enabling the south
  14053. * error interrupt (see cpt_can_enable_serr_int()).
  14054. */
  14055. if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
  14056. crtc->pch_fifo_underrun_disabled = true;
  14057. }
  14058. }
  14059. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  14060. {
  14061. struct intel_connector *connector;
  14062. /* We need to check both for a crtc link (meaning that the
  14063. * encoder is active and trying to read from a pipe) and the
  14064. * pipe itself being active. */
  14065. bool has_active_crtc = encoder->base.crtc &&
  14066. to_intel_crtc(encoder->base.crtc)->active;
  14067. connector = intel_encoder_find_connector(encoder);
  14068. if (connector && !has_active_crtc) {
  14069. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  14070. encoder->base.base.id,
  14071. encoder->base.name);
  14072. /* Connector is active, but has no active pipe. This is
  14073. * fallout from our resume register restoring. Disable
  14074. * the encoder manually again. */
  14075. if (encoder->base.crtc) {
  14076. struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
  14077. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  14078. encoder->base.base.id,
  14079. encoder->base.name);
  14080. encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  14081. if (encoder->post_disable)
  14082. encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  14083. }
  14084. encoder->base.crtc = NULL;
  14085. /* Inconsistent output/port/pipe state happens presumably due to
  14086. * a bug in one of the get_hw_state functions. Or someplace else
  14087. * in our code, like the register restore mess on resume. Clamp
  14088. * things to off as a safer default. */
  14089. connector->base.dpms = DRM_MODE_DPMS_OFF;
  14090. connector->base.encoder = NULL;
  14091. }
  14092. /* Enabled encoders without active connectors will be fixed in
  14093. * the crtc fixup. */
  14094. }
  14095. void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
  14096. {
  14097. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  14098. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  14099. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  14100. i915_disable_vga(dev_priv);
  14101. }
  14102. }
  14103. void i915_redisable_vga(struct drm_i915_private *dev_priv)
  14104. {
  14105. /* This function can be called both from intel_modeset_setup_hw_state or
  14106. * at a very early point in our resume sequence, where the power well
  14107. * structures are not yet restored. Since this function is at a very
  14108. * paranoid "someone might have enabled VGA while we were not looking"
  14109. * level, just check if the power well is enabled instead of trying to
  14110. * follow the "don't touch the power well if we don't need it" policy
  14111. * the rest of the driver uses. */
  14112. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  14113. return;
  14114. i915_redisable_vga_power_on(dev_priv);
  14115. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  14116. }
  14117. static bool primary_get_hw_state(struct intel_plane *plane)
  14118. {
  14119. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  14120. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  14121. }
  14122. /* FIXME read out full plane state for all planes */
  14123. static void readout_plane_state(struct intel_crtc *crtc)
  14124. {
  14125. struct drm_plane *primary = crtc->base.primary;
  14126. struct intel_plane_state *plane_state =
  14127. to_intel_plane_state(primary->state);
  14128. plane_state->base.visible = crtc->active &&
  14129. primary_get_hw_state(to_intel_plane(primary));
  14130. if (plane_state->base.visible)
  14131. crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
  14132. }
  14133. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  14134. {
  14135. struct drm_i915_private *dev_priv = to_i915(dev);
  14136. enum pipe pipe;
  14137. struct intel_crtc *crtc;
  14138. struct intel_encoder *encoder;
  14139. struct intel_connector *connector;
  14140. int i;
  14141. dev_priv->active_crtcs = 0;
  14142. for_each_intel_crtc(dev, crtc) {
  14143. struct intel_crtc_state *crtc_state = crtc->config;
  14144. int pixclk = 0;
  14145. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  14146. memset(crtc_state, 0, sizeof(*crtc_state));
  14147. crtc_state->base.crtc = &crtc->base;
  14148. crtc_state->base.active = crtc_state->base.enable =
  14149. dev_priv->display.get_pipe_config(crtc, crtc_state);
  14150. crtc->base.enabled = crtc_state->base.enable;
  14151. crtc->active = crtc_state->base.active;
  14152. if (crtc_state->base.active) {
  14153. dev_priv->active_crtcs |= 1 << crtc->pipe;
  14154. if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
  14155. pixclk = ilk_pipe_pixel_rate(crtc_state);
  14156. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  14157. pixclk = crtc_state->base.adjusted_mode.crtc_clock;
  14158. else
  14159. WARN_ON(dev_priv->display.modeset_calc_cdclk);
  14160. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  14161. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  14162. pixclk = DIV_ROUND_UP(pixclk * 100, 95);
  14163. }
  14164. dev_priv->min_pixclk[crtc->pipe] = pixclk;
  14165. readout_plane_state(crtc);
  14166. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  14167. crtc->base.base.id, crtc->base.name,
  14168. enableddisabled(crtc->active));
  14169. }
  14170. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  14171. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  14172. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  14173. &pll->config.hw_state);
  14174. pll->config.crtc_mask = 0;
  14175. for_each_intel_crtc(dev, crtc) {
  14176. if (crtc->active && crtc->config->shared_dpll == pll)
  14177. pll->config.crtc_mask |= 1 << crtc->pipe;
  14178. }
  14179. pll->active_mask = pll->config.crtc_mask;
  14180. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  14181. pll->name, pll->config.crtc_mask, pll->on);
  14182. }
  14183. for_each_intel_encoder(dev, encoder) {
  14184. pipe = 0;
  14185. if (encoder->get_hw_state(encoder, &pipe)) {
  14186. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  14187. encoder->base.crtc = &crtc->base;
  14188. crtc->config->output_types |= 1 << encoder->type;
  14189. encoder->get_config(encoder, crtc->config);
  14190. } else {
  14191. encoder->base.crtc = NULL;
  14192. }
  14193. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  14194. encoder->base.base.id, encoder->base.name,
  14195. enableddisabled(encoder->base.crtc),
  14196. pipe_name(pipe));
  14197. }
  14198. for_each_intel_connector(dev, connector) {
  14199. if (connector->get_hw_state(connector)) {
  14200. connector->base.dpms = DRM_MODE_DPMS_ON;
  14201. encoder = connector->encoder;
  14202. connector->base.encoder = &encoder->base;
  14203. if (encoder->base.crtc &&
  14204. encoder->base.crtc->state->active) {
  14205. /*
  14206. * This has to be done during hardware readout
  14207. * because anything calling .crtc_disable may
  14208. * rely on the connector_mask being accurate.
  14209. */
  14210. encoder->base.crtc->state->connector_mask |=
  14211. 1 << drm_connector_index(&connector->base);
  14212. encoder->base.crtc->state->encoder_mask |=
  14213. 1 << drm_encoder_index(&encoder->base);
  14214. }
  14215. } else {
  14216. connector->base.dpms = DRM_MODE_DPMS_OFF;
  14217. connector->base.encoder = NULL;
  14218. }
  14219. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  14220. connector->base.base.id, connector->base.name,
  14221. enableddisabled(connector->base.encoder));
  14222. }
  14223. for_each_intel_crtc(dev, crtc) {
  14224. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  14225. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  14226. if (crtc->base.state->active) {
  14227. intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
  14228. intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
  14229. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  14230. /*
  14231. * The initial mode needs to be set in order to keep
  14232. * the atomic core happy. It wants a valid mode if the
  14233. * crtc's enabled, so we do the above call.
  14234. *
  14235. * At this point some state updated by the connectors
  14236. * in their ->detect() callback has not run yet, so
  14237. * no recalculation can be done yet.
  14238. *
  14239. * Even if we could do a recalculation and modeset
  14240. * right now it would cause a double modeset if
  14241. * fbdev or userspace chooses a different initial mode.
  14242. *
  14243. * If that happens, someone indicated they wanted a
  14244. * mode change, which means it's safe to do a full
  14245. * recalculation.
  14246. */
  14247. crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
  14248. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  14249. update_scanline_offset(crtc);
  14250. }
  14251. intel_pipe_config_sanity_check(dev_priv, crtc->config);
  14252. }
  14253. }
  14254. /* Scan out the current hw modeset state,
  14255. * and sanitizes it to the current state
  14256. */
  14257. static void
  14258. intel_modeset_setup_hw_state(struct drm_device *dev)
  14259. {
  14260. struct drm_i915_private *dev_priv = to_i915(dev);
  14261. enum pipe pipe;
  14262. struct intel_crtc *crtc;
  14263. struct intel_encoder *encoder;
  14264. int i;
  14265. intel_modeset_readout_hw_state(dev);
  14266. /* HW state is read out, now we need to sanitize this mess. */
  14267. for_each_intel_encoder(dev, encoder) {
  14268. intel_sanitize_encoder(encoder);
  14269. }
  14270. for_each_pipe(dev_priv, pipe) {
  14271. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  14272. intel_sanitize_crtc(crtc);
  14273. intel_dump_pipe_config(crtc, crtc->config,
  14274. "[setup_hw_state]");
  14275. }
  14276. intel_modeset_update_connector_atomic_state(dev);
  14277. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  14278. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  14279. if (!pll->on || pll->active_mask)
  14280. continue;
  14281. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  14282. pll->funcs.disable(dev_priv, pll);
  14283. pll->on = false;
  14284. }
  14285. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  14286. vlv_wm_get_hw_state(dev);
  14287. else if (IS_GEN9(dev_priv))
  14288. skl_wm_get_hw_state(dev);
  14289. else if (HAS_PCH_SPLIT(dev_priv))
  14290. ilk_wm_get_hw_state(dev);
  14291. for_each_intel_crtc(dev, crtc) {
  14292. unsigned long put_domains;
  14293. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  14294. if (WARN_ON(put_domains))
  14295. modeset_put_power_domains(dev_priv, put_domains);
  14296. }
  14297. intel_display_set_init_power(dev_priv, false);
  14298. intel_fbc_init_pipe_state(dev_priv);
  14299. }
  14300. void intel_display_resume(struct drm_device *dev)
  14301. {
  14302. struct drm_i915_private *dev_priv = to_i915(dev);
  14303. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  14304. struct drm_modeset_acquire_ctx ctx;
  14305. int ret;
  14306. dev_priv->modeset_restore_state = NULL;
  14307. if (state)
  14308. state->acquire_ctx = &ctx;
  14309. /*
  14310. * This is a cludge because with real atomic modeset mode_config.mutex
  14311. * won't be taken. Unfortunately some probed state like
  14312. * audio_codec_enable is still protected by mode_config.mutex, so lock
  14313. * it here for now.
  14314. */
  14315. mutex_lock(&dev->mode_config.mutex);
  14316. drm_modeset_acquire_init(&ctx, 0);
  14317. while (1) {
  14318. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  14319. if (ret != -EDEADLK)
  14320. break;
  14321. drm_modeset_backoff(&ctx);
  14322. }
  14323. if (!ret)
  14324. ret = __intel_display_resume(dev, state);
  14325. drm_modeset_drop_locks(&ctx);
  14326. drm_modeset_acquire_fini(&ctx);
  14327. mutex_unlock(&dev->mode_config.mutex);
  14328. if (ret)
  14329. DRM_ERROR("Restoring old state failed with %i\n", ret);
  14330. drm_atomic_state_put(state);
  14331. }
  14332. void intel_modeset_gem_init(struct drm_device *dev)
  14333. {
  14334. struct drm_i915_private *dev_priv = to_i915(dev);
  14335. struct drm_crtc *c;
  14336. struct drm_i915_gem_object *obj;
  14337. intel_init_gt_powersave(dev_priv);
  14338. intel_modeset_init_hw(dev);
  14339. intel_setup_overlay(dev_priv);
  14340. /*
  14341. * Make sure any fbs we allocated at startup are properly
  14342. * pinned & fenced. When we do the allocation it's too early
  14343. * for this.
  14344. */
  14345. for_each_crtc(dev, c) {
  14346. struct i915_vma *vma;
  14347. obj = intel_fb_obj(c->primary->fb);
  14348. if (obj == NULL)
  14349. continue;
  14350. mutex_lock(&dev->struct_mutex);
  14351. vma = intel_pin_and_fence_fb_obj(c->primary->fb,
  14352. c->primary->state->rotation);
  14353. mutex_unlock(&dev->struct_mutex);
  14354. if (IS_ERR(vma)) {
  14355. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  14356. to_intel_crtc(c)->pipe);
  14357. drm_framebuffer_unreference(c->primary->fb);
  14358. c->primary->fb = NULL;
  14359. c->primary->crtc = c->primary->state->crtc = NULL;
  14360. update_state_fb(c->primary);
  14361. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  14362. }
  14363. }
  14364. }
  14365. int intel_connector_register(struct drm_connector *connector)
  14366. {
  14367. struct intel_connector *intel_connector = to_intel_connector(connector);
  14368. int ret;
  14369. ret = intel_backlight_device_register(intel_connector);
  14370. if (ret)
  14371. goto err;
  14372. return 0;
  14373. err:
  14374. return ret;
  14375. }
  14376. void intel_connector_unregister(struct drm_connector *connector)
  14377. {
  14378. struct intel_connector *intel_connector = to_intel_connector(connector);
  14379. intel_backlight_device_unregister(intel_connector);
  14380. intel_panel_destroy_backlight(connector);
  14381. }
  14382. void intel_modeset_cleanup(struct drm_device *dev)
  14383. {
  14384. struct drm_i915_private *dev_priv = to_i915(dev);
  14385. intel_disable_gt_powersave(dev_priv);
  14386. /*
  14387. * Interrupts and polling as the first thing to avoid creating havoc.
  14388. * Too much stuff here (turning of connectors, ...) would
  14389. * experience fancy races otherwise.
  14390. */
  14391. intel_irq_uninstall(dev_priv);
  14392. /*
  14393. * Due to the hpd irq storm handling the hotplug work can re-arm the
  14394. * poll handlers. Hence disable polling after hpd handling is shut down.
  14395. */
  14396. drm_kms_helper_poll_fini(dev);
  14397. intel_unregister_dsm_handler();
  14398. intel_fbc_global_disable(dev_priv);
  14399. /* flush any delayed tasks or pending work */
  14400. flush_scheduled_work();
  14401. drm_mode_config_cleanup(dev);
  14402. intel_cleanup_overlay(dev_priv);
  14403. intel_cleanup_gt_powersave(dev_priv);
  14404. intel_teardown_gmbus(dev_priv);
  14405. }
  14406. void intel_connector_attach_encoder(struct intel_connector *connector,
  14407. struct intel_encoder *encoder)
  14408. {
  14409. connector->encoder = encoder;
  14410. drm_mode_connector_attach_encoder(&connector->base,
  14411. &encoder->base);
  14412. }
  14413. /*
  14414. * set vga decode state - true == enable VGA decode
  14415. */
  14416. int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
  14417. {
  14418. unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  14419. u16 gmch_ctrl;
  14420. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  14421. DRM_ERROR("failed to read control word\n");
  14422. return -EIO;
  14423. }
  14424. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  14425. return 0;
  14426. if (state)
  14427. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  14428. else
  14429. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  14430. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  14431. DRM_ERROR("failed to write control word\n");
  14432. return -EIO;
  14433. }
  14434. return 0;
  14435. }
  14436. #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
  14437. struct intel_display_error_state {
  14438. u32 power_well_driver;
  14439. int num_transcoders;
  14440. struct intel_cursor_error_state {
  14441. u32 control;
  14442. u32 position;
  14443. u32 base;
  14444. u32 size;
  14445. } cursor[I915_MAX_PIPES];
  14446. struct intel_pipe_error_state {
  14447. bool power_domain_on;
  14448. u32 source;
  14449. u32 stat;
  14450. } pipe[I915_MAX_PIPES];
  14451. struct intel_plane_error_state {
  14452. u32 control;
  14453. u32 stride;
  14454. u32 size;
  14455. u32 pos;
  14456. u32 addr;
  14457. u32 surface;
  14458. u32 tile_offset;
  14459. } plane[I915_MAX_PIPES];
  14460. struct intel_transcoder_error_state {
  14461. bool power_domain_on;
  14462. enum transcoder cpu_transcoder;
  14463. u32 conf;
  14464. u32 htotal;
  14465. u32 hblank;
  14466. u32 hsync;
  14467. u32 vtotal;
  14468. u32 vblank;
  14469. u32 vsync;
  14470. } transcoder[4];
  14471. };
  14472. struct intel_display_error_state *
  14473. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  14474. {
  14475. struct intel_display_error_state *error;
  14476. int transcoders[] = {
  14477. TRANSCODER_A,
  14478. TRANSCODER_B,
  14479. TRANSCODER_C,
  14480. TRANSCODER_EDP,
  14481. };
  14482. int i;
  14483. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  14484. return NULL;
  14485. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  14486. if (error == NULL)
  14487. return NULL;
  14488. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  14489. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  14490. for_each_pipe(dev_priv, i) {
  14491. error->pipe[i].power_domain_on =
  14492. __intel_display_power_is_enabled(dev_priv,
  14493. POWER_DOMAIN_PIPE(i));
  14494. if (!error->pipe[i].power_domain_on)
  14495. continue;
  14496. error->cursor[i].control = I915_READ(CURCNTR(i));
  14497. error->cursor[i].position = I915_READ(CURPOS(i));
  14498. error->cursor[i].base = I915_READ(CURBASE(i));
  14499. error->plane[i].control = I915_READ(DSPCNTR(i));
  14500. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  14501. if (INTEL_GEN(dev_priv) <= 3) {
  14502. error->plane[i].size = I915_READ(DSPSIZE(i));
  14503. error->plane[i].pos = I915_READ(DSPPOS(i));
  14504. }
  14505. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  14506. error->plane[i].addr = I915_READ(DSPADDR(i));
  14507. if (INTEL_GEN(dev_priv) >= 4) {
  14508. error->plane[i].surface = I915_READ(DSPSURF(i));
  14509. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  14510. }
  14511. error->pipe[i].source = I915_READ(PIPESRC(i));
  14512. if (HAS_GMCH_DISPLAY(dev_priv))
  14513. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  14514. }
  14515. /* Note: this does not include DSI transcoders. */
  14516. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  14517. if (HAS_DDI(dev_priv))
  14518. error->num_transcoders++; /* Account for eDP. */
  14519. for (i = 0; i < error->num_transcoders; i++) {
  14520. enum transcoder cpu_transcoder = transcoders[i];
  14521. error->transcoder[i].power_domain_on =
  14522. __intel_display_power_is_enabled(dev_priv,
  14523. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  14524. if (!error->transcoder[i].power_domain_on)
  14525. continue;
  14526. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  14527. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  14528. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  14529. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  14530. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  14531. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  14532. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  14533. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  14534. }
  14535. return error;
  14536. }
  14537. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  14538. void
  14539. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  14540. struct drm_i915_private *dev_priv,
  14541. struct intel_display_error_state *error)
  14542. {
  14543. int i;
  14544. if (!error)
  14545. return;
  14546. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
  14547. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  14548. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  14549. error->power_well_driver);
  14550. for_each_pipe(dev_priv, i) {
  14551. err_printf(m, "Pipe [%d]:\n", i);
  14552. err_printf(m, " Power: %s\n",
  14553. onoff(error->pipe[i].power_domain_on));
  14554. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  14555. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  14556. err_printf(m, "Plane [%d]:\n", i);
  14557. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  14558. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  14559. if (INTEL_GEN(dev_priv) <= 3) {
  14560. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  14561. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  14562. }
  14563. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  14564. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  14565. if (INTEL_GEN(dev_priv) >= 4) {
  14566. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  14567. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  14568. }
  14569. err_printf(m, "Cursor [%d]:\n", i);
  14570. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  14571. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  14572. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  14573. }
  14574. for (i = 0; i < error->num_transcoders; i++) {
  14575. err_printf(m, "CPU transcoder: %s\n",
  14576. transcoder_name(error->transcoder[i].cpu_transcoder));
  14577. err_printf(m, " Power: %s\n",
  14578. onoff(error->transcoder[i].power_domain_on));
  14579. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  14580. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  14581. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  14582. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  14583. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  14584. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  14585. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  14586. }
  14587. }
  14588. #endif