intel_display.c 487 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include "intel_frontbuffer.h"
  37. #include <drm/i915_drm.h>
  38. #include "i915_drv.h"
  39. #include "i915_gem_dmabuf.h"
  40. #include "intel_dsi.h"
  41. #include "i915_trace.h"
  42. #include <drm/drm_atomic.h>
  43. #include <drm/drm_atomic_helper.h>
  44. #include <drm/drm_dp_helper.h>
  45. #include <drm/drm_crtc_helper.h>
  46. #include <drm/drm_plane_helper.h>
  47. #include <drm/drm_rect.h>
  48. #include <linux/dma_remapping.h>
  49. #include <linux/reservation.h>
  50. static bool is_mmio_work(struct intel_flip_work *work)
  51. {
  52. return work->mmio_work.func;
  53. }
  54. /* Primary plane formats for gen <= 3 */
  55. static const uint32_t i8xx_primary_formats[] = {
  56. DRM_FORMAT_C8,
  57. DRM_FORMAT_RGB565,
  58. DRM_FORMAT_XRGB1555,
  59. DRM_FORMAT_XRGB8888,
  60. };
  61. /* Primary plane formats for gen >= 4 */
  62. static const uint32_t i965_primary_formats[] = {
  63. DRM_FORMAT_C8,
  64. DRM_FORMAT_RGB565,
  65. DRM_FORMAT_XRGB8888,
  66. DRM_FORMAT_XBGR8888,
  67. DRM_FORMAT_XRGB2101010,
  68. DRM_FORMAT_XBGR2101010,
  69. };
  70. static const uint32_t skl_primary_formats[] = {
  71. DRM_FORMAT_C8,
  72. DRM_FORMAT_RGB565,
  73. DRM_FORMAT_XRGB8888,
  74. DRM_FORMAT_XBGR8888,
  75. DRM_FORMAT_ARGB8888,
  76. DRM_FORMAT_ABGR8888,
  77. DRM_FORMAT_XRGB2101010,
  78. DRM_FORMAT_XBGR2101010,
  79. DRM_FORMAT_YUYV,
  80. DRM_FORMAT_YVYU,
  81. DRM_FORMAT_UYVY,
  82. DRM_FORMAT_VYUY,
  83. };
  84. /* Cursor formats */
  85. static const uint32_t intel_cursor_formats[] = {
  86. DRM_FORMAT_ARGB8888,
  87. };
  88. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  89. struct intel_crtc_state *pipe_config);
  90. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  91. struct intel_crtc_state *pipe_config);
  92. static int intel_framebuffer_init(struct drm_device *dev,
  93. struct intel_framebuffer *ifb,
  94. struct drm_mode_fb_cmd2 *mode_cmd,
  95. struct drm_i915_gem_object *obj);
  96. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  97. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  98. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
  99. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  100. struct intel_link_m_n *m_n,
  101. struct intel_link_m_n *m2_n2);
  102. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  103. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  104. static void haswell_set_pipemisc(struct drm_crtc *crtc);
  105. static void vlv_prepare_pll(struct intel_crtc *crtc,
  106. const struct intel_crtc_state *pipe_config);
  107. static void chv_prepare_pll(struct intel_crtc *crtc,
  108. const struct intel_crtc_state *pipe_config);
  109. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  110. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  111. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  112. struct intel_crtc_state *crtc_state);
  113. static void skylake_pfit_enable(struct intel_crtc *crtc);
  114. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
  115. static void ironlake_pfit_enable(struct intel_crtc *crtc);
  116. static void intel_modeset_setup_hw_state(struct drm_device *dev);
  117. static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
  118. static int ilk_max_pixel_rate(struct drm_atomic_state *state);
  119. static int bxt_calc_cdclk(int max_pixclk);
  120. struct intel_limit {
  121. struct {
  122. int min, max;
  123. } dot, vco, n, m, m1, m2, p, p1;
  124. struct {
  125. int dot_limit;
  126. int p2_slow, p2_fast;
  127. } p2;
  128. };
  129. /* returns HPLL frequency in kHz */
  130. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  131. {
  132. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  133. /* Obtain SKU information */
  134. mutex_lock(&dev_priv->sb_lock);
  135. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  136. CCK_FUSE_HPLL_FREQ_MASK;
  137. mutex_unlock(&dev_priv->sb_lock);
  138. return vco_freq[hpll_freq] * 1000;
  139. }
  140. int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
  141. const char *name, u32 reg, int ref_freq)
  142. {
  143. u32 val;
  144. int divider;
  145. mutex_lock(&dev_priv->sb_lock);
  146. val = vlv_cck_read(dev_priv, reg);
  147. mutex_unlock(&dev_priv->sb_lock);
  148. divider = val & CCK_FREQUENCY_VALUES;
  149. WARN((val & CCK_FREQUENCY_STATUS) !=
  150. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  151. "%s change in progress\n", name);
  152. return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
  153. }
  154. static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  155. const char *name, u32 reg)
  156. {
  157. if (dev_priv->hpll_freq == 0)
  158. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  159. return vlv_get_cck_clock(dev_priv, name, reg,
  160. dev_priv->hpll_freq);
  161. }
  162. static int
  163. intel_pch_rawclk(struct drm_i915_private *dev_priv)
  164. {
  165. return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
  166. }
  167. static int
  168. intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
  169. {
  170. /* RAWCLK_FREQ_VLV register updated from power well code */
  171. return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
  172. CCK_DISPLAY_REF_CLOCK_CONTROL);
  173. }
  174. static int
  175. intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
  176. {
  177. uint32_t clkcfg;
  178. /* hrawclock is 1/4 the FSB frequency */
  179. clkcfg = I915_READ(CLKCFG);
  180. switch (clkcfg & CLKCFG_FSB_MASK) {
  181. case CLKCFG_FSB_400:
  182. return 100000;
  183. case CLKCFG_FSB_533:
  184. return 133333;
  185. case CLKCFG_FSB_667:
  186. return 166667;
  187. case CLKCFG_FSB_800:
  188. return 200000;
  189. case CLKCFG_FSB_1067:
  190. return 266667;
  191. case CLKCFG_FSB_1333:
  192. return 333333;
  193. /* these two are just a guess; one of them might be right */
  194. case CLKCFG_FSB_1600:
  195. case CLKCFG_FSB_1600_ALT:
  196. return 400000;
  197. default:
  198. return 133333;
  199. }
  200. }
  201. void intel_update_rawclk(struct drm_i915_private *dev_priv)
  202. {
  203. if (HAS_PCH_SPLIT(dev_priv))
  204. dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
  205. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  206. dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
  207. else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
  208. dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
  209. else
  210. return; /* no rawclk on other platforms, or no need to know it */
  211. DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
  212. }
  213. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  214. {
  215. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  216. return;
  217. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  218. CCK_CZ_CLOCK_CONTROL);
  219. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  220. }
  221. static inline u32 /* units of 100MHz */
  222. intel_fdi_link_freq(struct drm_i915_private *dev_priv,
  223. const struct intel_crtc_state *pipe_config)
  224. {
  225. if (HAS_DDI(dev_priv))
  226. return pipe_config->port_clock; /* SPLL */
  227. else if (IS_GEN5(dev_priv))
  228. return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
  229. else
  230. return 270000;
  231. }
  232. static const struct intel_limit intel_limits_i8xx_dac = {
  233. .dot = { .min = 25000, .max = 350000 },
  234. .vco = { .min = 908000, .max = 1512000 },
  235. .n = { .min = 2, .max = 16 },
  236. .m = { .min = 96, .max = 140 },
  237. .m1 = { .min = 18, .max = 26 },
  238. .m2 = { .min = 6, .max = 16 },
  239. .p = { .min = 4, .max = 128 },
  240. .p1 = { .min = 2, .max = 33 },
  241. .p2 = { .dot_limit = 165000,
  242. .p2_slow = 4, .p2_fast = 2 },
  243. };
  244. static const struct intel_limit intel_limits_i8xx_dvo = {
  245. .dot = { .min = 25000, .max = 350000 },
  246. .vco = { .min = 908000, .max = 1512000 },
  247. .n = { .min = 2, .max = 16 },
  248. .m = { .min = 96, .max = 140 },
  249. .m1 = { .min = 18, .max = 26 },
  250. .m2 = { .min = 6, .max = 16 },
  251. .p = { .min = 4, .max = 128 },
  252. .p1 = { .min = 2, .max = 33 },
  253. .p2 = { .dot_limit = 165000,
  254. .p2_slow = 4, .p2_fast = 4 },
  255. };
  256. static const struct intel_limit intel_limits_i8xx_lvds = {
  257. .dot = { .min = 25000, .max = 350000 },
  258. .vco = { .min = 908000, .max = 1512000 },
  259. .n = { .min = 2, .max = 16 },
  260. .m = { .min = 96, .max = 140 },
  261. .m1 = { .min = 18, .max = 26 },
  262. .m2 = { .min = 6, .max = 16 },
  263. .p = { .min = 4, .max = 128 },
  264. .p1 = { .min = 1, .max = 6 },
  265. .p2 = { .dot_limit = 165000,
  266. .p2_slow = 14, .p2_fast = 7 },
  267. };
  268. static const struct intel_limit intel_limits_i9xx_sdvo = {
  269. .dot = { .min = 20000, .max = 400000 },
  270. .vco = { .min = 1400000, .max = 2800000 },
  271. .n = { .min = 1, .max = 6 },
  272. .m = { .min = 70, .max = 120 },
  273. .m1 = { .min = 8, .max = 18 },
  274. .m2 = { .min = 3, .max = 7 },
  275. .p = { .min = 5, .max = 80 },
  276. .p1 = { .min = 1, .max = 8 },
  277. .p2 = { .dot_limit = 200000,
  278. .p2_slow = 10, .p2_fast = 5 },
  279. };
  280. static const struct intel_limit intel_limits_i9xx_lvds = {
  281. .dot = { .min = 20000, .max = 400000 },
  282. .vco = { .min = 1400000, .max = 2800000 },
  283. .n = { .min = 1, .max = 6 },
  284. .m = { .min = 70, .max = 120 },
  285. .m1 = { .min = 8, .max = 18 },
  286. .m2 = { .min = 3, .max = 7 },
  287. .p = { .min = 7, .max = 98 },
  288. .p1 = { .min = 1, .max = 8 },
  289. .p2 = { .dot_limit = 112000,
  290. .p2_slow = 14, .p2_fast = 7 },
  291. };
  292. static const struct intel_limit intel_limits_g4x_sdvo = {
  293. .dot = { .min = 25000, .max = 270000 },
  294. .vco = { .min = 1750000, .max = 3500000},
  295. .n = { .min = 1, .max = 4 },
  296. .m = { .min = 104, .max = 138 },
  297. .m1 = { .min = 17, .max = 23 },
  298. .m2 = { .min = 5, .max = 11 },
  299. .p = { .min = 10, .max = 30 },
  300. .p1 = { .min = 1, .max = 3},
  301. .p2 = { .dot_limit = 270000,
  302. .p2_slow = 10,
  303. .p2_fast = 10
  304. },
  305. };
  306. static const struct intel_limit intel_limits_g4x_hdmi = {
  307. .dot = { .min = 22000, .max = 400000 },
  308. .vco = { .min = 1750000, .max = 3500000},
  309. .n = { .min = 1, .max = 4 },
  310. .m = { .min = 104, .max = 138 },
  311. .m1 = { .min = 16, .max = 23 },
  312. .m2 = { .min = 5, .max = 11 },
  313. .p = { .min = 5, .max = 80 },
  314. .p1 = { .min = 1, .max = 8},
  315. .p2 = { .dot_limit = 165000,
  316. .p2_slow = 10, .p2_fast = 5 },
  317. };
  318. static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
  319. .dot = { .min = 20000, .max = 115000 },
  320. .vco = { .min = 1750000, .max = 3500000 },
  321. .n = { .min = 1, .max = 3 },
  322. .m = { .min = 104, .max = 138 },
  323. .m1 = { .min = 17, .max = 23 },
  324. .m2 = { .min = 5, .max = 11 },
  325. .p = { .min = 28, .max = 112 },
  326. .p1 = { .min = 2, .max = 8 },
  327. .p2 = { .dot_limit = 0,
  328. .p2_slow = 14, .p2_fast = 14
  329. },
  330. };
  331. static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
  332. .dot = { .min = 80000, .max = 224000 },
  333. .vco = { .min = 1750000, .max = 3500000 },
  334. .n = { .min = 1, .max = 3 },
  335. .m = { .min = 104, .max = 138 },
  336. .m1 = { .min = 17, .max = 23 },
  337. .m2 = { .min = 5, .max = 11 },
  338. .p = { .min = 14, .max = 42 },
  339. .p1 = { .min = 2, .max = 6 },
  340. .p2 = { .dot_limit = 0,
  341. .p2_slow = 7, .p2_fast = 7
  342. },
  343. };
  344. static const struct intel_limit intel_limits_pineview_sdvo = {
  345. .dot = { .min = 20000, .max = 400000},
  346. .vco = { .min = 1700000, .max = 3500000 },
  347. /* Pineview's Ncounter is a ring counter */
  348. .n = { .min = 3, .max = 6 },
  349. .m = { .min = 2, .max = 256 },
  350. /* Pineview only has one combined m divider, which we treat as m2. */
  351. .m1 = { .min = 0, .max = 0 },
  352. .m2 = { .min = 0, .max = 254 },
  353. .p = { .min = 5, .max = 80 },
  354. .p1 = { .min = 1, .max = 8 },
  355. .p2 = { .dot_limit = 200000,
  356. .p2_slow = 10, .p2_fast = 5 },
  357. };
  358. static const struct intel_limit intel_limits_pineview_lvds = {
  359. .dot = { .min = 20000, .max = 400000 },
  360. .vco = { .min = 1700000, .max = 3500000 },
  361. .n = { .min = 3, .max = 6 },
  362. .m = { .min = 2, .max = 256 },
  363. .m1 = { .min = 0, .max = 0 },
  364. .m2 = { .min = 0, .max = 254 },
  365. .p = { .min = 7, .max = 112 },
  366. .p1 = { .min = 1, .max = 8 },
  367. .p2 = { .dot_limit = 112000,
  368. .p2_slow = 14, .p2_fast = 14 },
  369. };
  370. /* Ironlake / Sandybridge
  371. *
  372. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  373. * the range value for them is (actual_value - 2).
  374. */
  375. static const struct intel_limit intel_limits_ironlake_dac = {
  376. .dot = { .min = 25000, .max = 350000 },
  377. .vco = { .min = 1760000, .max = 3510000 },
  378. .n = { .min = 1, .max = 5 },
  379. .m = { .min = 79, .max = 127 },
  380. .m1 = { .min = 12, .max = 22 },
  381. .m2 = { .min = 5, .max = 9 },
  382. .p = { .min = 5, .max = 80 },
  383. .p1 = { .min = 1, .max = 8 },
  384. .p2 = { .dot_limit = 225000,
  385. .p2_slow = 10, .p2_fast = 5 },
  386. };
  387. static const struct intel_limit intel_limits_ironlake_single_lvds = {
  388. .dot = { .min = 25000, .max = 350000 },
  389. .vco = { .min = 1760000, .max = 3510000 },
  390. .n = { .min = 1, .max = 3 },
  391. .m = { .min = 79, .max = 118 },
  392. .m1 = { .min = 12, .max = 22 },
  393. .m2 = { .min = 5, .max = 9 },
  394. .p = { .min = 28, .max = 112 },
  395. .p1 = { .min = 2, .max = 8 },
  396. .p2 = { .dot_limit = 225000,
  397. .p2_slow = 14, .p2_fast = 14 },
  398. };
  399. static const struct intel_limit intel_limits_ironlake_dual_lvds = {
  400. .dot = { .min = 25000, .max = 350000 },
  401. .vco = { .min = 1760000, .max = 3510000 },
  402. .n = { .min = 1, .max = 3 },
  403. .m = { .min = 79, .max = 127 },
  404. .m1 = { .min = 12, .max = 22 },
  405. .m2 = { .min = 5, .max = 9 },
  406. .p = { .min = 14, .max = 56 },
  407. .p1 = { .min = 2, .max = 8 },
  408. .p2 = { .dot_limit = 225000,
  409. .p2_slow = 7, .p2_fast = 7 },
  410. };
  411. /* LVDS 100mhz refclk limits. */
  412. static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
  413. .dot = { .min = 25000, .max = 350000 },
  414. .vco = { .min = 1760000, .max = 3510000 },
  415. .n = { .min = 1, .max = 2 },
  416. .m = { .min = 79, .max = 126 },
  417. .m1 = { .min = 12, .max = 22 },
  418. .m2 = { .min = 5, .max = 9 },
  419. .p = { .min = 28, .max = 112 },
  420. .p1 = { .min = 2, .max = 8 },
  421. .p2 = { .dot_limit = 225000,
  422. .p2_slow = 14, .p2_fast = 14 },
  423. };
  424. static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
  425. .dot = { .min = 25000, .max = 350000 },
  426. .vco = { .min = 1760000, .max = 3510000 },
  427. .n = { .min = 1, .max = 3 },
  428. .m = { .min = 79, .max = 126 },
  429. .m1 = { .min = 12, .max = 22 },
  430. .m2 = { .min = 5, .max = 9 },
  431. .p = { .min = 14, .max = 42 },
  432. .p1 = { .min = 2, .max = 6 },
  433. .p2 = { .dot_limit = 225000,
  434. .p2_slow = 7, .p2_fast = 7 },
  435. };
  436. static const struct intel_limit intel_limits_vlv = {
  437. /*
  438. * These are the data rate limits (measured in fast clocks)
  439. * since those are the strictest limits we have. The fast
  440. * clock and actual rate limits are more relaxed, so checking
  441. * them would make no difference.
  442. */
  443. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  444. .vco = { .min = 4000000, .max = 6000000 },
  445. .n = { .min = 1, .max = 7 },
  446. .m1 = { .min = 2, .max = 3 },
  447. .m2 = { .min = 11, .max = 156 },
  448. .p1 = { .min = 2, .max = 3 },
  449. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  450. };
  451. static const struct intel_limit intel_limits_chv = {
  452. /*
  453. * These are the data rate limits (measured in fast clocks)
  454. * since those are the strictest limits we have. The fast
  455. * clock and actual rate limits are more relaxed, so checking
  456. * them would make no difference.
  457. */
  458. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  459. .vco = { .min = 4800000, .max = 6480000 },
  460. .n = { .min = 1, .max = 1 },
  461. .m1 = { .min = 2, .max = 2 },
  462. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  463. .p1 = { .min = 2, .max = 4 },
  464. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  465. };
  466. static const struct intel_limit intel_limits_bxt = {
  467. /* FIXME: find real dot limits */
  468. .dot = { .min = 0, .max = INT_MAX },
  469. .vco = { .min = 4800000, .max = 6700000 },
  470. .n = { .min = 1, .max = 1 },
  471. .m1 = { .min = 2, .max = 2 },
  472. /* FIXME: find real m2 limits */
  473. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  474. .p1 = { .min = 2, .max = 4 },
  475. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  476. };
  477. static bool
  478. needs_modeset(struct drm_crtc_state *state)
  479. {
  480. return drm_atomic_crtc_needs_modeset(state);
  481. }
  482. /*
  483. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  484. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  485. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  486. * The helpers' return value is the rate of the clock that is fed to the
  487. * display engine's pipe which can be the above fast dot clock rate or a
  488. * divided-down version of it.
  489. */
  490. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  491. static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
  492. {
  493. clock->m = clock->m2 + 2;
  494. clock->p = clock->p1 * clock->p2;
  495. if (WARN_ON(clock->n == 0 || clock->p == 0))
  496. return 0;
  497. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  498. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  499. return clock->dot;
  500. }
  501. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  502. {
  503. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  504. }
  505. static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
  506. {
  507. clock->m = i9xx_dpll_compute_m(clock);
  508. clock->p = clock->p1 * clock->p2;
  509. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  510. return 0;
  511. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  512. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  513. return clock->dot;
  514. }
  515. static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
  516. {
  517. clock->m = clock->m1 * clock->m2;
  518. clock->p = clock->p1 * clock->p2;
  519. if (WARN_ON(clock->n == 0 || clock->p == 0))
  520. return 0;
  521. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  522. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  523. return clock->dot / 5;
  524. }
  525. int chv_calc_dpll_params(int refclk, struct dpll *clock)
  526. {
  527. clock->m = clock->m1 * clock->m2;
  528. clock->p = clock->p1 * clock->p2;
  529. if (WARN_ON(clock->n == 0 || clock->p == 0))
  530. return 0;
  531. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  532. clock->n << 22);
  533. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  534. return clock->dot / 5;
  535. }
  536. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  537. /**
  538. * Returns whether the given set of divisors are valid for a given refclk with
  539. * the given connectors.
  540. */
  541. static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
  542. const struct intel_limit *limit,
  543. const struct dpll *clock)
  544. {
  545. if (clock->n < limit->n.min || limit->n.max < clock->n)
  546. INTELPllInvalid("n out of range\n");
  547. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  548. INTELPllInvalid("p1 out of range\n");
  549. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  550. INTELPllInvalid("m2 out of range\n");
  551. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  552. INTELPllInvalid("m1 out of range\n");
  553. if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
  554. !IS_CHERRYVIEW(dev_priv) && !IS_BROXTON(dev_priv))
  555. if (clock->m1 <= clock->m2)
  556. INTELPllInvalid("m1 <= m2\n");
  557. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  558. !IS_BROXTON(dev_priv)) {
  559. if (clock->p < limit->p.min || limit->p.max < clock->p)
  560. INTELPllInvalid("p out of range\n");
  561. if (clock->m < limit->m.min || limit->m.max < clock->m)
  562. INTELPllInvalid("m out of range\n");
  563. }
  564. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  565. INTELPllInvalid("vco out of range\n");
  566. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  567. * connector, etc., rather than just a single range.
  568. */
  569. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  570. INTELPllInvalid("dot out of range\n");
  571. return true;
  572. }
  573. static int
  574. i9xx_select_p2_div(const struct intel_limit *limit,
  575. const struct intel_crtc_state *crtc_state,
  576. int target)
  577. {
  578. struct drm_device *dev = crtc_state->base.crtc->dev;
  579. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  580. /*
  581. * For LVDS just rely on its current settings for dual-channel.
  582. * We haven't figured out how to reliably set up different
  583. * single/dual channel state, if we even can.
  584. */
  585. if (intel_is_dual_link_lvds(dev))
  586. return limit->p2.p2_fast;
  587. else
  588. return limit->p2.p2_slow;
  589. } else {
  590. if (target < limit->p2.dot_limit)
  591. return limit->p2.p2_slow;
  592. else
  593. return limit->p2.p2_fast;
  594. }
  595. }
  596. /*
  597. * Returns a set of divisors for the desired target clock with the given
  598. * refclk, or FALSE. The returned values represent the clock equation:
  599. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  600. *
  601. * Target and reference clocks are specified in kHz.
  602. *
  603. * If match_clock is provided, then best_clock P divider must match the P
  604. * divider from @match_clock used for LVDS downclocking.
  605. */
  606. static bool
  607. i9xx_find_best_dpll(const struct intel_limit *limit,
  608. struct intel_crtc_state *crtc_state,
  609. int target, int refclk, struct dpll *match_clock,
  610. struct dpll *best_clock)
  611. {
  612. struct drm_device *dev = crtc_state->base.crtc->dev;
  613. struct dpll clock;
  614. int err = target;
  615. memset(best_clock, 0, sizeof(*best_clock));
  616. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  617. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  618. clock.m1++) {
  619. for (clock.m2 = limit->m2.min;
  620. clock.m2 <= limit->m2.max; clock.m2++) {
  621. if (clock.m2 >= clock.m1)
  622. break;
  623. for (clock.n = limit->n.min;
  624. clock.n <= limit->n.max; clock.n++) {
  625. for (clock.p1 = limit->p1.min;
  626. clock.p1 <= limit->p1.max; clock.p1++) {
  627. int this_err;
  628. i9xx_calc_dpll_params(refclk, &clock);
  629. if (!intel_PLL_is_valid(to_i915(dev),
  630. limit,
  631. &clock))
  632. continue;
  633. if (match_clock &&
  634. clock.p != match_clock->p)
  635. continue;
  636. this_err = abs(clock.dot - target);
  637. if (this_err < err) {
  638. *best_clock = clock;
  639. err = this_err;
  640. }
  641. }
  642. }
  643. }
  644. }
  645. return (err != target);
  646. }
  647. /*
  648. * Returns a set of divisors for the desired target clock with the given
  649. * refclk, or FALSE. The returned values represent the clock equation:
  650. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  651. *
  652. * Target and reference clocks are specified in kHz.
  653. *
  654. * If match_clock is provided, then best_clock P divider must match the P
  655. * divider from @match_clock used for LVDS downclocking.
  656. */
  657. static bool
  658. pnv_find_best_dpll(const struct intel_limit *limit,
  659. struct intel_crtc_state *crtc_state,
  660. int target, int refclk, struct dpll *match_clock,
  661. struct dpll *best_clock)
  662. {
  663. struct drm_device *dev = crtc_state->base.crtc->dev;
  664. struct dpll clock;
  665. int err = target;
  666. memset(best_clock, 0, sizeof(*best_clock));
  667. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  668. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  669. clock.m1++) {
  670. for (clock.m2 = limit->m2.min;
  671. clock.m2 <= limit->m2.max; clock.m2++) {
  672. for (clock.n = limit->n.min;
  673. clock.n <= limit->n.max; clock.n++) {
  674. for (clock.p1 = limit->p1.min;
  675. clock.p1 <= limit->p1.max; clock.p1++) {
  676. int this_err;
  677. pnv_calc_dpll_params(refclk, &clock);
  678. if (!intel_PLL_is_valid(to_i915(dev),
  679. limit,
  680. &clock))
  681. continue;
  682. if (match_clock &&
  683. clock.p != match_clock->p)
  684. continue;
  685. this_err = abs(clock.dot - target);
  686. if (this_err < err) {
  687. *best_clock = clock;
  688. err = this_err;
  689. }
  690. }
  691. }
  692. }
  693. }
  694. return (err != target);
  695. }
  696. /*
  697. * Returns a set of divisors for the desired target clock with the given
  698. * refclk, or FALSE. The returned values represent the clock equation:
  699. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  700. *
  701. * Target and reference clocks are specified in kHz.
  702. *
  703. * If match_clock is provided, then best_clock P divider must match the P
  704. * divider from @match_clock used for LVDS downclocking.
  705. */
  706. static bool
  707. g4x_find_best_dpll(const struct intel_limit *limit,
  708. struct intel_crtc_state *crtc_state,
  709. int target, int refclk, struct dpll *match_clock,
  710. struct dpll *best_clock)
  711. {
  712. struct drm_device *dev = crtc_state->base.crtc->dev;
  713. struct dpll clock;
  714. int max_n;
  715. bool found = false;
  716. /* approximately equals target * 0.00585 */
  717. int err_most = (target >> 8) + (target >> 9);
  718. memset(best_clock, 0, sizeof(*best_clock));
  719. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  720. max_n = limit->n.max;
  721. /* based on hardware requirement, prefer smaller n to precision */
  722. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  723. /* based on hardware requirement, prefere larger m1,m2 */
  724. for (clock.m1 = limit->m1.max;
  725. clock.m1 >= limit->m1.min; clock.m1--) {
  726. for (clock.m2 = limit->m2.max;
  727. clock.m2 >= limit->m2.min; clock.m2--) {
  728. for (clock.p1 = limit->p1.max;
  729. clock.p1 >= limit->p1.min; clock.p1--) {
  730. int this_err;
  731. i9xx_calc_dpll_params(refclk, &clock);
  732. if (!intel_PLL_is_valid(to_i915(dev),
  733. limit,
  734. &clock))
  735. continue;
  736. this_err = abs(clock.dot - target);
  737. if (this_err < err_most) {
  738. *best_clock = clock;
  739. err_most = this_err;
  740. max_n = clock.n;
  741. found = true;
  742. }
  743. }
  744. }
  745. }
  746. }
  747. return found;
  748. }
  749. /*
  750. * Check if the calculated PLL configuration is more optimal compared to the
  751. * best configuration and error found so far. Return the calculated error.
  752. */
  753. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  754. const struct dpll *calculated_clock,
  755. const struct dpll *best_clock,
  756. unsigned int best_error_ppm,
  757. unsigned int *error_ppm)
  758. {
  759. /*
  760. * For CHV ignore the error and consider only the P value.
  761. * Prefer a bigger P value based on HW requirements.
  762. */
  763. if (IS_CHERRYVIEW(to_i915(dev))) {
  764. *error_ppm = 0;
  765. return calculated_clock->p > best_clock->p;
  766. }
  767. if (WARN_ON_ONCE(!target_freq))
  768. return false;
  769. *error_ppm = div_u64(1000000ULL *
  770. abs(target_freq - calculated_clock->dot),
  771. target_freq);
  772. /*
  773. * Prefer a better P value over a better (smaller) error if the error
  774. * is small. Ensure this preference for future configurations too by
  775. * setting the error to 0.
  776. */
  777. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  778. *error_ppm = 0;
  779. return true;
  780. }
  781. return *error_ppm + 10 < best_error_ppm;
  782. }
  783. /*
  784. * Returns a set of divisors for the desired target clock with the given
  785. * refclk, or FALSE. The returned values represent the clock equation:
  786. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  787. */
  788. static bool
  789. vlv_find_best_dpll(const struct intel_limit *limit,
  790. struct intel_crtc_state *crtc_state,
  791. int target, int refclk, struct dpll *match_clock,
  792. struct dpll *best_clock)
  793. {
  794. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  795. struct drm_device *dev = crtc->base.dev;
  796. struct dpll clock;
  797. unsigned int bestppm = 1000000;
  798. /* min update 19.2 MHz */
  799. int max_n = min(limit->n.max, refclk / 19200);
  800. bool found = false;
  801. target *= 5; /* fast clock */
  802. memset(best_clock, 0, sizeof(*best_clock));
  803. /* based on hardware requirement, prefer smaller n to precision */
  804. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  805. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  806. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  807. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  808. clock.p = clock.p1 * clock.p2;
  809. /* based on hardware requirement, prefer bigger m1,m2 values */
  810. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  811. unsigned int ppm;
  812. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  813. refclk * clock.m1);
  814. vlv_calc_dpll_params(refclk, &clock);
  815. if (!intel_PLL_is_valid(to_i915(dev),
  816. limit,
  817. &clock))
  818. continue;
  819. if (!vlv_PLL_is_optimal(dev, target,
  820. &clock,
  821. best_clock,
  822. bestppm, &ppm))
  823. continue;
  824. *best_clock = clock;
  825. bestppm = ppm;
  826. found = true;
  827. }
  828. }
  829. }
  830. }
  831. return found;
  832. }
  833. /*
  834. * Returns a set of divisors for the desired target clock with the given
  835. * refclk, or FALSE. The returned values represent the clock equation:
  836. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  837. */
  838. static bool
  839. chv_find_best_dpll(const struct intel_limit *limit,
  840. struct intel_crtc_state *crtc_state,
  841. int target, int refclk, struct dpll *match_clock,
  842. struct dpll *best_clock)
  843. {
  844. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  845. struct drm_device *dev = crtc->base.dev;
  846. unsigned int best_error_ppm;
  847. struct dpll clock;
  848. uint64_t m2;
  849. int found = false;
  850. memset(best_clock, 0, sizeof(*best_clock));
  851. best_error_ppm = 1000000;
  852. /*
  853. * Based on hardware doc, the n always set to 1, and m1 always
  854. * set to 2. If requires to support 200Mhz refclk, we need to
  855. * revisit this because n may not 1 anymore.
  856. */
  857. clock.n = 1, clock.m1 = 2;
  858. target *= 5; /* fast clock */
  859. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  860. for (clock.p2 = limit->p2.p2_fast;
  861. clock.p2 >= limit->p2.p2_slow;
  862. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  863. unsigned int error_ppm;
  864. clock.p = clock.p1 * clock.p2;
  865. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  866. clock.n) << 22, refclk * clock.m1);
  867. if (m2 > INT_MAX/clock.m1)
  868. continue;
  869. clock.m2 = m2;
  870. chv_calc_dpll_params(refclk, &clock);
  871. if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
  872. continue;
  873. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  874. best_error_ppm, &error_ppm))
  875. continue;
  876. *best_clock = clock;
  877. best_error_ppm = error_ppm;
  878. found = true;
  879. }
  880. }
  881. return found;
  882. }
  883. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  884. struct dpll *best_clock)
  885. {
  886. int refclk = 100000;
  887. const struct intel_limit *limit = &intel_limits_bxt;
  888. return chv_find_best_dpll(limit, crtc_state,
  889. target_clock, refclk, NULL, best_clock);
  890. }
  891. bool intel_crtc_active(struct drm_crtc *crtc)
  892. {
  893. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  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 intel_crtc->active && crtc->primary->state->fb &&
  908. intel_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 drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  914. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  915. return intel_crtc->config->cpu_transcoder;
  916. }
  917. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  918. {
  919. struct drm_i915_private *dev_priv = to_i915(dev);
  920. i915_reg_t reg = PIPEDSL(pipe);
  921. u32 line1, line2;
  922. u32 line_mask;
  923. if (IS_GEN2(dev_priv))
  924. line_mask = DSL_LINEMASK_GEN2;
  925. else
  926. line_mask = DSL_LINEMASK_GEN3;
  927. line1 = I915_READ(reg) & line_mask;
  928. msleep(5);
  929. line2 = I915_READ(reg) & line_mask;
  930. return line1 == line2;
  931. }
  932. /*
  933. * intel_wait_for_pipe_off - wait for pipe to turn off
  934. * @crtc: crtc whose pipe to wait for
  935. *
  936. * After disabling a pipe, we can't wait for vblank in the usual way,
  937. * spinning on the vblank interrupt status bit, since we won't actually
  938. * see an interrupt when the pipe is disabled.
  939. *
  940. * On Gen4 and above:
  941. * wait for the pipe register state bit to turn off
  942. *
  943. * Otherwise:
  944. * wait for the display line value to settle (it usually
  945. * ends up stopping at the start of the next frame).
  946. *
  947. */
  948. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  949. {
  950. struct drm_device *dev = crtc->base.dev;
  951. struct drm_i915_private *dev_priv = to_i915(dev);
  952. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  953. enum pipe pipe = crtc->pipe;
  954. if (INTEL_INFO(dev)->gen >= 4) {
  955. i915_reg_t reg = PIPECONF(cpu_transcoder);
  956. /* Wait for the Pipe State to go off */
  957. if (intel_wait_for_register(dev_priv,
  958. reg, I965_PIPECONF_ACTIVE, 0,
  959. 100))
  960. WARN(1, "pipe_off wait timed out\n");
  961. } else {
  962. /* Wait for the display line to settle */
  963. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  964. WARN(1, "pipe_off wait timed out\n");
  965. }
  966. }
  967. /* Only for pre-ILK configs */
  968. void assert_pll(struct drm_i915_private *dev_priv,
  969. enum pipe pipe, bool state)
  970. {
  971. u32 val;
  972. bool cur_state;
  973. val = I915_READ(DPLL(pipe));
  974. cur_state = !!(val & DPLL_VCO_ENABLE);
  975. I915_STATE_WARN(cur_state != state,
  976. "PLL state assertion failure (expected %s, current %s)\n",
  977. onoff(state), onoff(cur_state));
  978. }
  979. /* XXX: the dsi pll is shared between MIPI DSI ports */
  980. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  981. {
  982. u32 val;
  983. bool cur_state;
  984. mutex_lock(&dev_priv->sb_lock);
  985. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  986. mutex_unlock(&dev_priv->sb_lock);
  987. cur_state = val & DSI_PLL_VCO_EN;
  988. I915_STATE_WARN(cur_state != state,
  989. "DSI PLL state assertion failure (expected %s, current %s)\n",
  990. onoff(state), onoff(cur_state));
  991. }
  992. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  993. enum pipe pipe, bool state)
  994. {
  995. bool cur_state;
  996. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  997. pipe);
  998. if (HAS_DDI(dev_priv)) {
  999. /* DDI does not have a specific FDI_TX register */
  1000. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  1001. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1002. } else {
  1003. u32 val = I915_READ(FDI_TX_CTL(pipe));
  1004. cur_state = !!(val & FDI_TX_ENABLE);
  1005. }
  1006. I915_STATE_WARN(cur_state != state,
  1007. "FDI TX state assertion failure (expected %s, current %s)\n",
  1008. onoff(state), onoff(cur_state));
  1009. }
  1010. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1011. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1012. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1013. enum pipe pipe, bool state)
  1014. {
  1015. u32 val;
  1016. bool cur_state;
  1017. val = I915_READ(FDI_RX_CTL(pipe));
  1018. cur_state = !!(val & FDI_RX_ENABLE);
  1019. I915_STATE_WARN(cur_state != state,
  1020. "FDI RX state assertion failure (expected %s, current %s)\n",
  1021. onoff(state), onoff(cur_state));
  1022. }
  1023. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1024. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1025. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1026. enum pipe pipe)
  1027. {
  1028. u32 val;
  1029. /* ILK FDI PLL is always enabled */
  1030. if (IS_GEN5(dev_priv))
  1031. return;
  1032. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1033. if (HAS_DDI(dev_priv))
  1034. return;
  1035. val = I915_READ(FDI_TX_CTL(pipe));
  1036. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1037. }
  1038. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1039. enum pipe pipe, bool state)
  1040. {
  1041. u32 val;
  1042. bool cur_state;
  1043. val = I915_READ(FDI_RX_CTL(pipe));
  1044. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1045. I915_STATE_WARN(cur_state != state,
  1046. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1047. onoff(state), onoff(cur_state));
  1048. }
  1049. void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
  1050. {
  1051. i915_reg_t pp_reg;
  1052. u32 val;
  1053. enum pipe panel_pipe = PIPE_A;
  1054. bool locked = true;
  1055. if (WARN_ON(HAS_DDI(dev_priv)))
  1056. return;
  1057. if (HAS_PCH_SPLIT(dev_priv)) {
  1058. u32 port_sel;
  1059. pp_reg = PP_CONTROL(0);
  1060. port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
  1061. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1062. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1063. panel_pipe = PIPE_B;
  1064. /* XXX: else fix for eDP */
  1065. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1066. /* presumably write lock depends on pipe, not port select */
  1067. pp_reg = PP_CONTROL(pipe);
  1068. panel_pipe = pipe;
  1069. } else {
  1070. pp_reg = PP_CONTROL(0);
  1071. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1072. panel_pipe = PIPE_B;
  1073. }
  1074. val = I915_READ(pp_reg);
  1075. if (!(val & PANEL_POWER_ON) ||
  1076. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1077. locked = false;
  1078. I915_STATE_WARN(panel_pipe == pipe && locked,
  1079. "panel assertion failure, pipe %c regs locked\n",
  1080. pipe_name(pipe));
  1081. }
  1082. static void assert_cursor(struct drm_i915_private *dev_priv,
  1083. enum pipe pipe, bool state)
  1084. {
  1085. bool cur_state;
  1086. if (IS_845G(dev_priv) || IS_I865G(dev_priv))
  1087. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1088. else
  1089. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1090. I915_STATE_WARN(cur_state != state,
  1091. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1092. pipe_name(pipe), onoff(state), onoff(cur_state));
  1093. }
  1094. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1095. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1096. void assert_pipe(struct drm_i915_private *dev_priv,
  1097. enum pipe pipe, bool state)
  1098. {
  1099. bool cur_state;
  1100. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1101. pipe);
  1102. enum intel_display_power_domain power_domain;
  1103. /* if we need the pipe quirk it must be always on */
  1104. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1105. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1106. state = true;
  1107. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1108. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1109. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1110. cur_state = !!(val & PIPECONF_ENABLE);
  1111. intel_display_power_put(dev_priv, power_domain);
  1112. } else {
  1113. cur_state = false;
  1114. }
  1115. I915_STATE_WARN(cur_state != state,
  1116. "pipe %c assertion failure (expected %s, current %s)\n",
  1117. pipe_name(pipe), onoff(state), onoff(cur_state));
  1118. }
  1119. static void assert_plane(struct drm_i915_private *dev_priv,
  1120. enum plane plane, bool state)
  1121. {
  1122. u32 val;
  1123. bool cur_state;
  1124. val = I915_READ(DSPCNTR(plane));
  1125. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1126. I915_STATE_WARN(cur_state != state,
  1127. "plane %c assertion failure (expected %s, current %s)\n",
  1128. plane_name(plane), onoff(state), onoff(cur_state));
  1129. }
  1130. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1131. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1132. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1133. enum pipe pipe)
  1134. {
  1135. struct drm_device *dev = &dev_priv->drm;
  1136. int i;
  1137. /* Primary planes are fixed to pipes on gen4+ */
  1138. if (INTEL_INFO(dev)->gen >= 4) {
  1139. u32 val = I915_READ(DSPCNTR(pipe));
  1140. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1141. "plane %c assertion failure, should be disabled but not\n",
  1142. plane_name(pipe));
  1143. return;
  1144. }
  1145. /* Need to check both planes against the pipe */
  1146. for_each_pipe(dev_priv, i) {
  1147. u32 val = I915_READ(DSPCNTR(i));
  1148. enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1149. DISPPLANE_SEL_PIPE_SHIFT;
  1150. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1151. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1152. plane_name(i), pipe_name(pipe));
  1153. }
  1154. }
  1155. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1156. enum pipe pipe)
  1157. {
  1158. struct drm_device *dev = &dev_priv->drm;
  1159. int sprite;
  1160. if (INTEL_INFO(dev)->gen >= 9) {
  1161. for_each_sprite(dev_priv, pipe, sprite) {
  1162. u32 val = I915_READ(PLANE_CTL(pipe, sprite));
  1163. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1164. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1165. sprite, pipe_name(pipe));
  1166. }
  1167. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1168. for_each_sprite(dev_priv, pipe, sprite) {
  1169. u32 val = I915_READ(SPCNTR(pipe, sprite));
  1170. I915_STATE_WARN(val & SP_ENABLE,
  1171. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1172. sprite_name(pipe, sprite), pipe_name(pipe));
  1173. }
  1174. } else if (INTEL_INFO(dev)->gen >= 7) {
  1175. u32 val = I915_READ(SPRCTL(pipe));
  1176. I915_STATE_WARN(val & SPRITE_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. } else if (INTEL_INFO(dev)->gen >= 5) {
  1180. u32 val = I915_READ(DVSCNTR(pipe));
  1181. I915_STATE_WARN(val & DVS_ENABLE,
  1182. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1183. plane_name(pipe), pipe_name(pipe));
  1184. }
  1185. }
  1186. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1187. {
  1188. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1189. drm_crtc_vblank_put(crtc);
  1190. }
  1191. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1192. enum pipe pipe)
  1193. {
  1194. u32 val;
  1195. bool enabled;
  1196. val = I915_READ(PCH_TRANSCONF(pipe));
  1197. enabled = !!(val & TRANS_ENABLE);
  1198. I915_STATE_WARN(enabled,
  1199. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1200. pipe_name(pipe));
  1201. }
  1202. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1203. enum pipe pipe, u32 port_sel, u32 val)
  1204. {
  1205. if ((val & DP_PORT_EN) == 0)
  1206. return false;
  1207. if (HAS_PCH_CPT(dev_priv)) {
  1208. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1209. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1210. return false;
  1211. } else if (IS_CHERRYVIEW(dev_priv)) {
  1212. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1213. return false;
  1214. } else {
  1215. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1216. return false;
  1217. }
  1218. return true;
  1219. }
  1220. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1221. enum pipe pipe, u32 val)
  1222. {
  1223. if ((val & SDVO_ENABLE) == 0)
  1224. return false;
  1225. if (HAS_PCH_CPT(dev_priv)) {
  1226. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1227. return false;
  1228. } else if (IS_CHERRYVIEW(dev_priv)) {
  1229. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1230. return false;
  1231. } else {
  1232. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1233. return false;
  1234. }
  1235. return true;
  1236. }
  1237. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1238. enum pipe pipe, u32 val)
  1239. {
  1240. if ((val & LVDS_PORT_EN) == 0)
  1241. return false;
  1242. if (HAS_PCH_CPT(dev_priv)) {
  1243. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1244. return false;
  1245. } else {
  1246. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1247. return false;
  1248. }
  1249. return true;
  1250. }
  1251. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1252. enum pipe pipe, u32 val)
  1253. {
  1254. if ((val & ADPA_DAC_ENABLE) == 0)
  1255. return false;
  1256. if (HAS_PCH_CPT(dev_priv)) {
  1257. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1258. return false;
  1259. } else {
  1260. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1261. return false;
  1262. }
  1263. return true;
  1264. }
  1265. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1266. enum pipe pipe, i915_reg_t reg,
  1267. u32 port_sel)
  1268. {
  1269. u32 val = I915_READ(reg);
  1270. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1271. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1272. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1273. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
  1274. && (val & DP_PIPEB_SELECT),
  1275. "IBX PCH dp port still using transcoder B\n");
  1276. }
  1277. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1278. enum pipe pipe, i915_reg_t reg)
  1279. {
  1280. u32 val = I915_READ(reg);
  1281. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1282. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1283. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1284. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
  1285. && (val & SDVO_PIPE_B_SELECT),
  1286. "IBX PCH hdmi port still using transcoder B\n");
  1287. }
  1288. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1289. enum pipe pipe)
  1290. {
  1291. u32 val;
  1292. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1293. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1294. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1295. val = I915_READ(PCH_ADPA);
  1296. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1297. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1298. pipe_name(pipe));
  1299. val = I915_READ(PCH_LVDS);
  1300. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1301. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1302. pipe_name(pipe));
  1303. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1304. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1305. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1306. }
  1307. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1308. const struct intel_crtc_state *pipe_config)
  1309. {
  1310. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1311. enum pipe pipe = crtc->pipe;
  1312. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1313. POSTING_READ(DPLL(pipe));
  1314. udelay(150);
  1315. if (intel_wait_for_register(dev_priv,
  1316. DPLL(pipe),
  1317. DPLL_LOCK_VLV,
  1318. DPLL_LOCK_VLV,
  1319. 1))
  1320. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1321. }
  1322. static void vlv_enable_pll(struct intel_crtc *crtc,
  1323. const struct intel_crtc_state *pipe_config)
  1324. {
  1325. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1326. enum pipe pipe = crtc->pipe;
  1327. assert_pipe_disabled(dev_priv, pipe);
  1328. /* PLL is protected by panel, make sure we can write it */
  1329. assert_panel_unlocked(dev_priv, pipe);
  1330. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1331. _vlv_enable_pll(crtc, pipe_config);
  1332. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1333. POSTING_READ(DPLL_MD(pipe));
  1334. }
  1335. static void _chv_enable_pll(struct intel_crtc *crtc,
  1336. const struct intel_crtc_state *pipe_config)
  1337. {
  1338. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1339. enum pipe pipe = crtc->pipe;
  1340. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1341. u32 tmp;
  1342. mutex_lock(&dev_priv->sb_lock);
  1343. /* Enable back the 10bit clock to display controller */
  1344. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1345. tmp |= DPIO_DCLKP_EN;
  1346. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1347. mutex_unlock(&dev_priv->sb_lock);
  1348. /*
  1349. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1350. */
  1351. udelay(1);
  1352. /* Enable PLL */
  1353. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1354. /* Check PLL is locked */
  1355. if (intel_wait_for_register(dev_priv,
  1356. DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
  1357. 1))
  1358. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1359. }
  1360. static void chv_enable_pll(struct intel_crtc *crtc,
  1361. const struct intel_crtc_state *pipe_config)
  1362. {
  1363. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1364. enum pipe pipe = crtc->pipe;
  1365. assert_pipe_disabled(dev_priv, pipe);
  1366. /* PLL is protected by panel, make sure we can write it */
  1367. assert_panel_unlocked(dev_priv, pipe);
  1368. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1369. _chv_enable_pll(crtc, pipe_config);
  1370. if (pipe != PIPE_A) {
  1371. /*
  1372. * WaPixelRepeatModeFixForC0:chv
  1373. *
  1374. * DPLLCMD is AWOL. Use chicken bits to propagate
  1375. * the value from DPLLBMD to either pipe B or C.
  1376. */
  1377. I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
  1378. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1379. I915_WRITE(CBR4_VLV, 0);
  1380. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1381. /*
  1382. * DPLLB VGA mode also seems to cause problems.
  1383. * We should always have it disabled.
  1384. */
  1385. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1386. } else {
  1387. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1388. POSTING_READ(DPLL_MD(pipe));
  1389. }
  1390. }
  1391. static int intel_num_dvo_pipes(struct drm_device *dev)
  1392. {
  1393. struct intel_crtc *crtc;
  1394. int count = 0;
  1395. for_each_intel_crtc(dev, crtc) {
  1396. count += crtc->base.state->active &&
  1397. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
  1398. }
  1399. return count;
  1400. }
  1401. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1402. {
  1403. struct drm_device *dev = crtc->base.dev;
  1404. struct drm_i915_private *dev_priv = to_i915(dev);
  1405. i915_reg_t reg = DPLL(crtc->pipe);
  1406. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1407. assert_pipe_disabled(dev_priv, crtc->pipe);
  1408. /* PLL is protected by panel, make sure we can write it */
  1409. if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
  1410. assert_panel_unlocked(dev_priv, crtc->pipe);
  1411. /* Enable DVO 2x clock on both PLLs if necessary */
  1412. if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev) > 0) {
  1413. /*
  1414. * It appears to be important that we don't enable this
  1415. * for the current pipe before otherwise configuring the
  1416. * PLL. No idea how this should be handled if multiple
  1417. * DVO outputs are enabled simultaneosly.
  1418. */
  1419. dpll |= DPLL_DVO_2X_MODE;
  1420. I915_WRITE(DPLL(!crtc->pipe),
  1421. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1422. }
  1423. /*
  1424. * Apparently we need to have VGA mode enabled prior to changing
  1425. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1426. * dividers, even though the register value does change.
  1427. */
  1428. I915_WRITE(reg, 0);
  1429. I915_WRITE(reg, dpll);
  1430. /* Wait for the clocks to stabilize. */
  1431. POSTING_READ(reg);
  1432. udelay(150);
  1433. if (INTEL_INFO(dev)->gen >= 4) {
  1434. I915_WRITE(DPLL_MD(crtc->pipe),
  1435. crtc->config->dpll_hw_state.dpll_md);
  1436. } else {
  1437. /* The pixel multiplier can only be updated once the
  1438. * DPLL is enabled and the clocks are stable.
  1439. *
  1440. * So write it again.
  1441. */
  1442. I915_WRITE(reg, dpll);
  1443. }
  1444. /* We do this three times for luck */
  1445. I915_WRITE(reg, dpll);
  1446. POSTING_READ(reg);
  1447. udelay(150); /* wait for warmup */
  1448. I915_WRITE(reg, dpll);
  1449. POSTING_READ(reg);
  1450. udelay(150); /* wait for warmup */
  1451. I915_WRITE(reg, dpll);
  1452. POSTING_READ(reg);
  1453. udelay(150); /* wait for warmup */
  1454. }
  1455. /**
  1456. * i9xx_disable_pll - disable a PLL
  1457. * @dev_priv: i915 private structure
  1458. * @pipe: pipe PLL to disable
  1459. *
  1460. * Disable the PLL for @pipe, making sure the pipe is off first.
  1461. *
  1462. * Note! This is for pre-ILK only.
  1463. */
  1464. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1465. {
  1466. struct drm_device *dev = crtc->base.dev;
  1467. struct drm_i915_private *dev_priv = to_i915(dev);
  1468. enum pipe pipe = crtc->pipe;
  1469. /* Disable DVO 2x clock on both PLLs if necessary */
  1470. if (IS_I830(dev_priv) &&
  1471. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
  1472. !intel_num_dvo_pipes(dev)) {
  1473. I915_WRITE(DPLL(PIPE_B),
  1474. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1475. I915_WRITE(DPLL(PIPE_A),
  1476. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1477. }
  1478. /* Don't disable pipe or pipe PLLs if needed */
  1479. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1480. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1481. return;
  1482. /* Make sure the pipe isn't still relying on us */
  1483. assert_pipe_disabled(dev_priv, pipe);
  1484. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1485. POSTING_READ(DPLL(pipe));
  1486. }
  1487. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1488. {
  1489. u32 val;
  1490. /* Make sure the pipe isn't still relying on us */
  1491. assert_pipe_disabled(dev_priv, pipe);
  1492. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1493. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1494. if (pipe != PIPE_A)
  1495. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1496. I915_WRITE(DPLL(pipe), val);
  1497. POSTING_READ(DPLL(pipe));
  1498. }
  1499. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1500. {
  1501. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1502. u32 val;
  1503. /* Make sure the pipe isn't still relying on us */
  1504. assert_pipe_disabled(dev_priv, pipe);
  1505. val = DPLL_SSC_REF_CLK_CHV |
  1506. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1507. if (pipe != PIPE_A)
  1508. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1509. I915_WRITE(DPLL(pipe), val);
  1510. POSTING_READ(DPLL(pipe));
  1511. mutex_lock(&dev_priv->sb_lock);
  1512. /* Disable 10bit clock to display controller */
  1513. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1514. val &= ~DPIO_DCLKP_EN;
  1515. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1516. mutex_unlock(&dev_priv->sb_lock);
  1517. }
  1518. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1519. struct intel_digital_port *dport,
  1520. unsigned int expected_mask)
  1521. {
  1522. u32 port_mask;
  1523. i915_reg_t dpll_reg;
  1524. switch (dport->port) {
  1525. case PORT_B:
  1526. port_mask = DPLL_PORTB_READY_MASK;
  1527. dpll_reg = DPLL(0);
  1528. break;
  1529. case PORT_C:
  1530. port_mask = DPLL_PORTC_READY_MASK;
  1531. dpll_reg = DPLL(0);
  1532. expected_mask <<= 4;
  1533. break;
  1534. case PORT_D:
  1535. port_mask = DPLL_PORTD_READY_MASK;
  1536. dpll_reg = DPIO_PHY_STATUS;
  1537. break;
  1538. default:
  1539. BUG();
  1540. }
  1541. if (intel_wait_for_register(dev_priv,
  1542. dpll_reg, port_mask, expected_mask,
  1543. 1000))
  1544. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1545. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1546. }
  1547. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1548. enum pipe pipe)
  1549. {
  1550. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  1551. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  1552. i915_reg_t reg;
  1553. uint32_t val, pipeconf_val;
  1554. /* Make sure PCH DPLL is enabled */
  1555. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1556. /* FDI must be feeding us bits for PCH ports */
  1557. assert_fdi_tx_enabled(dev_priv, pipe);
  1558. assert_fdi_rx_enabled(dev_priv, pipe);
  1559. if (HAS_PCH_CPT(dev_priv)) {
  1560. /* Workaround: Set the timing override bit before enabling the
  1561. * pch transcoder. */
  1562. reg = TRANS_CHICKEN2(pipe);
  1563. val = I915_READ(reg);
  1564. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1565. I915_WRITE(reg, val);
  1566. }
  1567. reg = PCH_TRANSCONF(pipe);
  1568. val = I915_READ(reg);
  1569. pipeconf_val = I915_READ(PIPECONF(pipe));
  1570. if (HAS_PCH_IBX(dev_priv)) {
  1571. /*
  1572. * Make the BPC in transcoder be consistent with
  1573. * that in pipeconf reg. For HDMI we must use 8bpc
  1574. * here for both 8bpc and 12bpc.
  1575. */
  1576. val &= ~PIPECONF_BPC_MASK;
  1577. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
  1578. val |= PIPECONF_8BPC;
  1579. else
  1580. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1581. }
  1582. val &= ~TRANS_INTERLACE_MASK;
  1583. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1584. if (HAS_PCH_IBX(dev_priv) &&
  1585. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  1586. val |= TRANS_LEGACY_INTERLACED_ILK;
  1587. else
  1588. val |= TRANS_INTERLACED;
  1589. else
  1590. val |= TRANS_PROGRESSIVE;
  1591. I915_WRITE(reg, val | TRANS_ENABLE);
  1592. if (intel_wait_for_register(dev_priv,
  1593. reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
  1594. 100))
  1595. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1596. }
  1597. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1598. enum transcoder cpu_transcoder)
  1599. {
  1600. u32 val, pipeconf_val;
  1601. /* FDI must be feeding us bits for PCH ports */
  1602. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1603. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1604. /* Workaround: set timing override bit. */
  1605. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1606. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1607. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1608. val = TRANS_ENABLE;
  1609. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1610. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1611. PIPECONF_INTERLACED_ILK)
  1612. val |= TRANS_INTERLACED;
  1613. else
  1614. val |= TRANS_PROGRESSIVE;
  1615. I915_WRITE(LPT_TRANSCONF, val);
  1616. if (intel_wait_for_register(dev_priv,
  1617. LPT_TRANSCONF,
  1618. TRANS_STATE_ENABLE,
  1619. TRANS_STATE_ENABLE,
  1620. 100))
  1621. DRM_ERROR("Failed to enable PCH transcoder\n");
  1622. }
  1623. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1624. enum pipe pipe)
  1625. {
  1626. i915_reg_t reg;
  1627. uint32_t val;
  1628. /* FDI relies on the transcoder */
  1629. assert_fdi_tx_disabled(dev_priv, pipe);
  1630. assert_fdi_rx_disabled(dev_priv, pipe);
  1631. /* Ports must be off as well */
  1632. assert_pch_ports_disabled(dev_priv, pipe);
  1633. reg = PCH_TRANSCONF(pipe);
  1634. val = I915_READ(reg);
  1635. val &= ~TRANS_ENABLE;
  1636. I915_WRITE(reg, val);
  1637. /* wait for PCH transcoder off, transcoder state */
  1638. if (intel_wait_for_register(dev_priv,
  1639. reg, TRANS_STATE_ENABLE, 0,
  1640. 50))
  1641. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1642. if (HAS_PCH_CPT(dev_priv)) {
  1643. /* Workaround: Clear the timing override chicken bit again. */
  1644. reg = TRANS_CHICKEN2(pipe);
  1645. val = I915_READ(reg);
  1646. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1647. I915_WRITE(reg, val);
  1648. }
  1649. }
  1650. void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1651. {
  1652. u32 val;
  1653. val = I915_READ(LPT_TRANSCONF);
  1654. val &= ~TRANS_ENABLE;
  1655. I915_WRITE(LPT_TRANSCONF, val);
  1656. /* wait for PCH transcoder off, transcoder state */
  1657. if (intel_wait_for_register(dev_priv,
  1658. LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
  1659. 50))
  1660. DRM_ERROR("Failed to disable PCH transcoder\n");
  1661. /* Workaround: clear timing override bit. */
  1662. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1663. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1664. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1665. }
  1666. enum transcoder intel_crtc_pch_transcoder(struct intel_crtc *crtc)
  1667. {
  1668. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1669. WARN_ON(!crtc->config->has_pch_encoder);
  1670. if (HAS_PCH_LPT(dev_priv))
  1671. return TRANSCODER_A;
  1672. else
  1673. return (enum transcoder) crtc->pipe;
  1674. }
  1675. /**
  1676. * intel_enable_pipe - enable a pipe, asserting requirements
  1677. * @crtc: crtc responsible for the pipe
  1678. *
  1679. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1680. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1681. */
  1682. static void intel_enable_pipe(struct intel_crtc *crtc)
  1683. {
  1684. struct drm_device *dev = crtc->base.dev;
  1685. struct drm_i915_private *dev_priv = to_i915(dev);
  1686. enum pipe pipe = crtc->pipe;
  1687. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1688. i915_reg_t reg;
  1689. u32 val;
  1690. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1691. assert_planes_disabled(dev_priv, pipe);
  1692. assert_cursor_disabled(dev_priv, pipe);
  1693. assert_sprites_disabled(dev_priv, pipe);
  1694. /*
  1695. * A pipe without a PLL won't actually be able to drive bits from
  1696. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1697. * need the check.
  1698. */
  1699. if (HAS_GMCH_DISPLAY(dev_priv)) {
  1700. if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
  1701. assert_dsi_pll_enabled(dev_priv);
  1702. else
  1703. assert_pll_enabled(dev_priv, pipe);
  1704. } else {
  1705. if (crtc->config->has_pch_encoder) {
  1706. /* if driving the PCH, we need FDI enabled */
  1707. assert_fdi_rx_pll_enabled(dev_priv,
  1708. (enum pipe) intel_crtc_pch_transcoder(crtc));
  1709. assert_fdi_tx_pll_enabled(dev_priv,
  1710. (enum pipe) cpu_transcoder);
  1711. }
  1712. /* FIXME: assert CPU port conditions for SNB+ */
  1713. }
  1714. reg = PIPECONF(cpu_transcoder);
  1715. val = I915_READ(reg);
  1716. if (val & PIPECONF_ENABLE) {
  1717. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1718. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1719. return;
  1720. }
  1721. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1722. POSTING_READ(reg);
  1723. /*
  1724. * Until the pipe starts DSL will read as 0, which would cause
  1725. * an apparent vblank timestamp jump, which messes up also the
  1726. * frame count when it's derived from the timestamps. So let's
  1727. * wait for the pipe to start properly before we call
  1728. * drm_crtc_vblank_on()
  1729. */
  1730. if (dev->max_vblank_count == 0 &&
  1731. wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
  1732. DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
  1733. }
  1734. /**
  1735. * intel_disable_pipe - disable a pipe, asserting requirements
  1736. * @crtc: crtc whose pipes is to be disabled
  1737. *
  1738. * Disable the pipe of @crtc, making sure that various hardware
  1739. * specific requirements are met, if applicable, e.g. plane
  1740. * disabled, panel fitter off, etc.
  1741. *
  1742. * Will wait until the pipe has shut down before returning.
  1743. */
  1744. static void intel_disable_pipe(struct intel_crtc *crtc)
  1745. {
  1746. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1747. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1748. enum pipe pipe = crtc->pipe;
  1749. i915_reg_t reg;
  1750. u32 val;
  1751. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1752. /*
  1753. * Make sure planes won't keep trying to pump pixels to us,
  1754. * or we might hang the display.
  1755. */
  1756. assert_planes_disabled(dev_priv, pipe);
  1757. assert_cursor_disabled(dev_priv, pipe);
  1758. assert_sprites_disabled(dev_priv, pipe);
  1759. reg = PIPECONF(cpu_transcoder);
  1760. val = I915_READ(reg);
  1761. if ((val & PIPECONF_ENABLE) == 0)
  1762. return;
  1763. /*
  1764. * Double wide has implications for planes
  1765. * so best keep it disabled when not needed.
  1766. */
  1767. if (crtc->config->double_wide)
  1768. val &= ~PIPECONF_DOUBLE_WIDE;
  1769. /* Don't disable pipe or pipe PLLs if needed */
  1770. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1771. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1772. val &= ~PIPECONF_ENABLE;
  1773. I915_WRITE(reg, val);
  1774. if ((val & PIPECONF_ENABLE) == 0)
  1775. intel_wait_for_pipe_off(crtc);
  1776. }
  1777. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1778. {
  1779. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1780. }
  1781. static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
  1782. uint64_t fb_modifier, unsigned int cpp)
  1783. {
  1784. switch (fb_modifier) {
  1785. case DRM_FORMAT_MOD_NONE:
  1786. return cpp;
  1787. case I915_FORMAT_MOD_X_TILED:
  1788. if (IS_GEN2(dev_priv))
  1789. return 128;
  1790. else
  1791. return 512;
  1792. case I915_FORMAT_MOD_Y_TILED:
  1793. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1794. return 128;
  1795. else
  1796. return 512;
  1797. case I915_FORMAT_MOD_Yf_TILED:
  1798. switch (cpp) {
  1799. case 1:
  1800. return 64;
  1801. case 2:
  1802. case 4:
  1803. return 128;
  1804. case 8:
  1805. case 16:
  1806. return 256;
  1807. default:
  1808. MISSING_CASE(cpp);
  1809. return cpp;
  1810. }
  1811. break;
  1812. default:
  1813. MISSING_CASE(fb_modifier);
  1814. return cpp;
  1815. }
  1816. }
  1817. unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
  1818. uint64_t fb_modifier, unsigned int cpp)
  1819. {
  1820. if (fb_modifier == DRM_FORMAT_MOD_NONE)
  1821. return 1;
  1822. else
  1823. return intel_tile_size(dev_priv) /
  1824. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1825. }
  1826. /* Return the tile dimensions in pixel units */
  1827. static void intel_tile_dims(const struct drm_i915_private *dev_priv,
  1828. unsigned int *tile_width,
  1829. unsigned int *tile_height,
  1830. uint64_t fb_modifier,
  1831. unsigned int cpp)
  1832. {
  1833. unsigned int tile_width_bytes =
  1834. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1835. *tile_width = tile_width_bytes / cpp;
  1836. *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
  1837. }
  1838. unsigned int
  1839. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1840. uint32_t pixel_format, uint64_t fb_modifier)
  1841. {
  1842. unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
  1843. unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
  1844. return ALIGN(height, tile_height);
  1845. }
  1846. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1847. {
  1848. unsigned int size = 0;
  1849. int i;
  1850. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1851. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1852. return size;
  1853. }
  1854. static void
  1855. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1856. const struct drm_framebuffer *fb,
  1857. unsigned int rotation)
  1858. {
  1859. if (intel_rotation_90_or_270(rotation)) {
  1860. *view = i915_ggtt_view_rotated;
  1861. view->params.rotated = to_intel_framebuffer(fb)->rot_info;
  1862. } else {
  1863. *view = i915_ggtt_view_normal;
  1864. }
  1865. }
  1866. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1867. {
  1868. if (INTEL_INFO(dev_priv)->gen >= 9)
  1869. return 256 * 1024;
  1870. else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
  1871. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1872. return 128 * 1024;
  1873. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1874. return 4 * 1024;
  1875. else
  1876. return 0;
  1877. }
  1878. static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
  1879. uint64_t fb_modifier)
  1880. {
  1881. switch (fb_modifier) {
  1882. case DRM_FORMAT_MOD_NONE:
  1883. return intel_linear_alignment(dev_priv);
  1884. case I915_FORMAT_MOD_X_TILED:
  1885. if (INTEL_INFO(dev_priv)->gen >= 9)
  1886. return 256 * 1024;
  1887. return 0;
  1888. case I915_FORMAT_MOD_Y_TILED:
  1889. case I915_FORMAT_MOD_Yf_TILED:
  1890. return 1 * 1024 * 1024;
  1891. default:
  1892. MISSING_CASE(fb_modifier);
  1893. return 0;
  1894. }
  1895. }
  1896. struct i915_vma *
  1897. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1898. {
  1899. struct drm_device *dev = fb->dev;
  1900. struct drm_i915_private *dev_priv = to_i915(dev);
  1901. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1902. struct i915_ggtt_view view;
  1903. struct i915_vma *vma;
  1904. u32 alignment;
  1905. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1906. alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
  1907. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1908. /* Note that the w/a also requires 64 PTE of padding following the
  1909. * bo. We currently fill all unused PTE with the shadow page and so
  1910. * we should always have valid PTE following the scanout preventing
  1911. * the VT-d warning.
  1912. */
  1913. if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
  1914. alignment = 256 * 1024;
  1915. /*
  1916. * Global gtt pte registers are special registers which actually forward
  1917. * writes to a chunk of system memory. Which means that there is no risk
  1918. * that the register values disappear as soon as we call
  1919. * intel_runtime_pm_put(), so it is correct to wrap only the
  1920. * pin/unpin/fence and not more.
  1921. */
  1922. intel_runtime_pm_get(dev_priv);
  1923. vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
  1924. if (IS_ERR(vma))
  1925. goto err;
  1926. if (i915_vma_is_map_and_fenceable(vma)) {
  1927. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1928. * fence, whereas 965+ only requires a fence if using
  1929. * framebuffer compression. For simplicity, we always, when
  1930. * possible, install a fence as the cost is not that onerous.
  1931. *
  1932. * If we fail to fence the tiled scanout, then either the
  1933. * modeset will reject the change (which is highly unlikely as
  1934. * the affected systems, all but one, do not have unmappable
  1935. * space) or we will not be able to enable full powersaving
  1936. * techniques (also likely not to apply due to various limits
  1937. * FBC and the like impose on the size of the buffer, which
  1938. * presumably we violated anyway with this unmappable buffer).
  1939. * Anyway, it is presumably better to stumble onwards with
  1940. * something and try to run the system in a "less than optimal"
  1941. * mode that matches the user configuration.
  1942. */
  1943. if (i915_vma_get_fence(vma) == 0)
  1944. i915_vma_pin_fence(vma);
  1945. }
  1946. err:
  1947. intel_runtime_pm_put(dev_priv);
  1948. return vma;
  1949. }
  1950. void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1951. {
  1952. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1953. struct i915_ggtt_view view;
  1954. struct i915_vma *vma;
  1955. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  1956. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1957. vma = i915_gem_object_to_ggtt(obj, &view);
  1958. i915_vma_unpin_fence(vma);
  1959. i915_gem_object_unpin_from_display_plane(vma);
  1960. }
  1961. static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
  1962. unsigned int rotation)
  1963. {
  1964. if (intel_rotation_90_or_270(rotation))
  1965. return to_intel_framebuffer(fb)->rotated[plane].pitch;
  1966. else
  1967. return fb->pitches[plane];
  1968. }
  1969. /*
  1970. * Convert the x/y offsets into a linear offset.
  1971. * Only valid with 0/180 degree rotation, which is fine since linear
  1972. * offset is only used with linear buffers on pre-hsw and tiled buffers
  1973. * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
  1974. */
  1975. u32 intel_fb_xy_to_linear(int x, int y,
  1976. const struct intel_plane_state *state,
  1977. int plane)
  1978. {
  1979. const struct drm_framebuffer *fb = state->base.fb;
  1980. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  1981. unsigned int pitch = fb->pitches[plane];
  1982. return y * pitch + x * cpp;
  1983. }
  1984. /*
  1985. * Add the x/y offsets derived from fb->offsets[] to the user
  1986. * specified plane src x/y offsets. The resulting x/y offsets
  1987. * specify the start of scanout from the beginning of the gtt mapping.
  1988. */
  1989. void intel_add_fb_offsets(int *x, int *y,
  1990. const struct intel_plane_state *state,
  1991. int plane)
  1992. {
  1993. const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
  1994. unsigned int rotation = state->base.rotation;
  1995. if (intel_rotation_90_or_270(rotation)) {
  1996. *x += intel_fb->rotated[plane].x;
  1997. *y += intel_fb->rotated[plane].y;
  1998. } else {
  1999. *x += intel_fb->normal[plane].x;
  2000. *y += intel_fb->normal[plane].y;
  2001. }
  2002. }
  2003. /*
  2004. * Input tile dimensions and pitch must already be
  2005. * rotated to match x and y, and in pixel units.
  2006. */
  2007. static u32 _intel_adjust_tile_offset(int *x, int *y,
  2008. unsigned int tile_width,
  2009. unsigned int tile_height,
  2010. unsigned int tile_size,
  2011. unsigned int pitch_tiles,
  2012. u32 old_offset,
  2013. u32 new_offset)
  2014. {
  2015. unsigned int pitch_pixels = pitch_tiles * tile_width;
  2016. unsigned int tiles;
  2017. WARN_ON(old_offset & (tile_size - 1));
  2018. WARN_ON(new_offset & (tile_size - 1));
  2019. WARN_ON(new_offset > old_offset);
  2020. tiles = (old_offset - new_offset) / tile_size;
  2021. *y += tiles / pitch_tiles * tile_height;
  2022. *x += tiles % pitch_tiles * tile_width;
  2023. /* minimize x in case it got needlessly big */
  2024. *y += *x / pitch_pixels * tile_height;
  2025. *x %= pitch_pixels;
  2026. return new_offset;
  2027. }
  2028. /*
  2029. * Adjust the tile offset by moving the difference into
  2030. * the x/y offsets.
  2031. */
  2032. static u32 intel_adjust_tile_offset(int *x, int *y,
  2033. const struct intel_plane_state *state, int plane,
  2034. u32 old_offset, u32 new_offset)
  2035. {
  2036. const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
  2037. const struct drm_framebuffer *fb = state->base.fb;
  2038. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2039. unsigned int rotation = state->base.rotation;
  2040. unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
  2041. WARN_ON(new_offset > old_offset);
  2042. if (fb->modifier[plane] != DRM_FORMAT_MOD_NONE) {
  2043. unsigned int tile_size, tile_width, tile_height;
  2044. unsigned int pitch_tiles;
  2045. tile_size = intel_tile_size(dev_priv);
  2046. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2047. fb->modifier[plane], cpp);
  2048. if (intel_rotation_90_or_270(rotation)) {
  2049. pitch_tiles = pitch / tile_height;
  2050. swap(tile_width, tile_height);
  2051. } else {
  2052. pitch_tiles = pitch / (tile_width * cpp);
  2053. }
  2054. _intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2055. tile_size, pitch_tiles,
  2056. old_offset, new_offset);
  2057. } else {
  2058. old_offset += *y * pitch + *x * cpp;
  2059. *y = (old_offset - new_offset) / pitch;
  2060. *x = ((old_offset - new_offset) - *y * pitch) / cpp;
  2061. }
  2062. return new_offset;
  2063. }
  2064. /*
  2065. * Computes the linear offset to the base tile and adjusts
  2066. * x, y. bytes per pixel is assumed to be a power-of-two.
  2067. *
  2068. * In the 90/270 rotated case, x and y are assumed
  2069. * to be already rotated to match the rotated GTT view, and
  2070. * pitch is the tile_height aligned framebuffer height.
  2071. *
  2072. * This function is used when computing the derived information
  2073. * under intel_framebuffer, so using any of that information
  2074. * here is not allowed. Anything under drm_framebuffer can be
  2075. * used. This is why the user has to pass in the pitch since it
  2076. * is specified in the rotated orientation.
  2077. */
  2078. static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
  2079. int *x, int *y,
  2080. const struct drm_framebuffer *fb, int plane,
  2081. unsigned int pitch,
  2082. unsigned int rotation,
  2083. u32 alignment)
  2084. {
  2085. uint64_t fb_modifier = fb->modifier[plane];
  2086. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2087. u32 offset, offset_aligned;
  2088. if (alignment)
  2089. alignment--;
  2090. if (fb_modifier != DRM_FORMAT_MOD_NONE) {
  2091. unsigned int tile_size, tile_width, tile_height;
  2092. unsigned int tile_rows, tiles, pitch_tiles;
  2093. tile_size = intel_tile_size(dev_priv);
  2094. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2095. fb_modifier, cpp);
  2096. if (intel_rotation_90_or_270(rotation)) {
  2097. pitch_tiles = pitch / tile_height;
  2098. swap(tile_width, tile_height);
  2099. } else {
  2100. pitch_tiles = pitch / (tile_width * cpp);
  2101. }
  2102. tile_rows = *y / tile_height;
  2103. *y %= tile_height;
  2104. tiles = *x / tile_width;
  2105. *x %= tile_width;
  2106. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  2107. offset_aligned = offset & ~alignment;
  2108. _intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2109. tile_size, pitch_tiles,
  2110. offset, offset_aligned);
  2111. } else {
  2112. offset = *y * pitch + *x * cpp;
  2113. offset_aligned = offset & ~alignment;
  2114. *y = (offset & alignment) / pitch;
  2115. *x = ((offset & alignment) - *y * pitch) / cpp;
  2116. }
  2117. return offset_aligned;
  2118. }
  2119. u32 intel_compute_tile_offset(int *x, int *y,
  2120. const struct intel_plane_state *state,
  2121. int plane)
  2122. {
  2123. const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
  2124. const struct drm_framebuffer *fb = state->base.fb;
  2125. unsigned int rotation = state->base.rotation;
  2126. int pitch = intel_fb_pitch(fb, plane, rotation);
  2127. u32 alignment;
  2128. /* AUX_DIST needs only 4K alignment */
  2129. if (fb->pixel_format == DRM_FORMAT_NV12 && plane == 1)
  2130. alignment = 4096;
  2131. else
  2132. alignment = intel_surf_alignment(dev_priv, fb->modifier[plane]);
  2133. return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
  2134. rotation, alignment);
  2135. }
  2136. /* Convert the fb->offset[] linear offset into x/y offsets */
  2137. static void intel_fb_offset_to_xy(int *x, int *y,
  2138. const struct drm_framebuffer *fb, int plane)
  2139. {
  2140. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2141. unsigned int pitch = fb->pitches[plane];
  2142. u32 linear_offset = fb->offsets[plane];
  2143. *y = linear_offset / pitch;
  2144. *x = linear_offset % pitch / cpp;
  2145. }
  2146. static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
  2147. {
  2148. switch (fb_modifier) {
  2149. case I915_FORMAT_MOD_X_TILED:
  2150. return I915_TILING_X;
  2151. case I915_FORMAT_MOD_Y_TILED:
  2152. return I915_TILING_Y;
  2153. default:
  2154. return I915_TILING_NONE;
  2155. }
  2156. }
  2157. static int
  2158. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  2159. struct drm_framebuffer *fb)
  2160. {
  2161. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  2162. struct intel_rotation_info *rot_info = &intel_fb->rot_info;
  2163. u32 gtt_offset_rotated = 0;
  2164. unsigned int max_size = 0;
  2165. uint32_t format = fb->pixel_format;
  2166. int i, num_planes = drm_format_num_planes(format);
  2167. unsigned int tile_size = intel_tile_size(dev_priv);
  2168. for (i = 0; i < num_planes; i++) {
  2169. unsigned int width, height;
  2170. unsigned int cpp, size;
  2171. u32 offset;
  2172. int x, y;
  2173. cpp = drm_format_plane_cpp(format, i);
  2174. width = drm_format_plane_width(fb->width, format, i);
  2175. height = drm_format_plane_height(fb->height, format, i);
  2176. intel_fb_offset_to_xy(&x, &y, fb, i);
  2177. /*
  2178. * The fence (if used) is aligned to the start of the object
  2179. * so having the framebuffer wrap around across the edge of the
  2180. * fenced region doesn't really work. We have no API to configure
  2181. * the fence start offset within the object (nor could we probably
  2182. * on gen2/3). So it's just easier if we just require that the
  2183. * fb layout agrees with the fence layout. We already check that the
  2184. * fb stride matches the fence stride elsewhere.
  2185. */
  2186. if (i915_gem_object_is_tiled(intel_fb->obj) &&
  2187. (x + width) * cpp > fb->pitches[i]) {
  2188. DRM_DEBUG("bad fb plane %d offset: 0x%x\n",
  2189. i, fb->offsets[i]);
  2190. return -EINVAL;
  2191. }
  2192. /*
  2193. * First pixel of the framebuffer from
  2194. * the start of the normal gtt mapping.
  2195. */
  2196. intel_fb->normal[i].x = x;
  2197. intel_fb->normal[i].y = y;
  2198. offset = _intel_compute_tile_offset(dev_priv, &x, &y,
  2199. fb, 0, fb->pitches[i],
  2200. DRM_ROTATE_0, tile_size);
  2201. offset /= tile_size;
  2202. if (fb->modifier[i] != DRM_FORMAT_MOD_NONE) {
  2203. unsigned int tile_width, tile_height;
  2204. unsigned int pitch_tiles;
  2205. struct drm_rect r;
  2206. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2207. fb->modifier[i], cpp);
  2208. rot_info->plane[i].offset = offset;
  2209. rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
  2210. rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
  2211. rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
  2212. intel_fb->rotated[i].pitch =
  2213. rot_info->plane[i].height * tile_height;
  2214. /* how many tiles does this plane need */
  2215. size = rot_info->plane[i].stride * rot_info->plane[i].height;
  2216. /*
  2217. * If the plane isn't horizontally tile aligned,
  2218. * we need one more tile.
  2219. */
  2220. if (x != 0)
  2221. size++;
  2222. /* rotate the x/y offsets to match the GTT view */
  2223. r.x1 = x;
  2224. r.y1 = y;
  2225. r.x2 = x + width;
  2226. r.y2 = y + height;
  2227. drm_rect_rotate(&r,
  2228. rot_info->plane[i].width * tile_width,
  2229. rot_info->plane[i].height * tile_height,
  2230. DRM_ROTATE_270);
  2231. x = r.x1;
  2232. y = r.y1;
  2233. /* rotate the tile dimensions to match the GTT view */
  2234. pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
  2235. swap(tile_width, tile_height);
  2236. /*
  2237. * We only keep the x/y offsets, so push all of the
  2238. * gtt offset into the x/y offsets.
  2239. */
  2240. _intel_adjust_tile_offset(&x, &y, tile_size,
  2241. tile_width, tile_height, pitch_tiles,
  2242. gtt_offset_rotated * tile_size, 0);
  2243. gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
  2244. /*
  2245. * First pixel of the framebuffer from
  2246. * the start of the rotated gtt mapping.
  2247. */
  2248. intel_fb->rotated[i].x = x;
  2249. intel_fb->rotated[i].y = y;
  2250. } else {
  2251. size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
  2252. x * cpp, tile_size);
  2253. }
  2254. /* how many tiles in total needed in the bo */
  2255. max_size = max(max_size, offset + size);
  2256. }
  2257. if (max_size * tile_size > to_intel_framebuffer(fb)->obj->base.size) {
  2258. DRM_DEBUG("fb too big for bo (need %u bytes, have %zu bytes)\n",
  2259. max_size * tile_size, to_intel_framebuffer(fb)->obj->base.size);
  2260. return -EINVAL;
  2261. }
  2262. return 0;
  2263. }
  2264. static int i9xx_format_to_fourcc(int format)
  2265. {
  2266. switch (format) {
  2267. case DISPPLANE_8BPP:
  2268. return DRM_FORMAT_C8;
  2269. case DISPPLANE_BGRX555:
  2270. return DRM_FORMAT_XRGB1555;
  2271. case DISPPLANE_BGRX565:
  2272. return DRM_FORMAT_RGB565;
  2273. default:
  2274. case DISPPLANE_BGRX888:
  2275. return DRM_FORMAT_XRGB8888;
  2276. case DISPPLANE_RGBX888:
  2277. return DRM_FORMAT_XBGR8888;
  2278. case DISPPLANE_BGRX101010:
  2279. return DRM_FORMAT_XRGB2101010;
  2280. case DISPPLANE_RGBX101010:
  2281. return DRM_FORMAT_XBGR2101010;
  2282. }
  2283. }
  2284. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2285. {
  2286. switch (format) {
  2287. case PLANE_CTL_FORMAT_RGB_565:
  2288. return DRM_FORMAT_RGB565;
  2289. default:
  2290. case PLANE_CTL_FORMAT_XRGB_8888:
  2291. if (rgb_order) {
  2292. if (alpha)
  2293. return DRM_FORMAT_ABGR8888;
  2294. else
  2295. return DRM_FORMAT_XBGR8888;
  2296. } else {
  2297. if (alpha)
  2298. return DRM_FORMAT_ARGB8888;
  2299. else
  2300. return DRM_FORMAT_XRGB8888;
  2301. }
  2302. case PLANE_CTL_FORMAT_XRGB_2101010:
  2303. if (rgb_order)
  2304. return DRM_FORMAT_XBGR2101010;
  2305. else
  2306. return DRM_FORMAT_XRGB2101010;
  2307. }
  2308. }
  2309. static bool
  2310. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2311. struct intel_initial_plane_config *plane_config)
  2312. {
  2313. struct drm_device *dev = crtc->base.dev;
  2314. struct drm_i915_private *dev_priv = to_i915(dev);
  2315. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2316. struct drm_i915_gem_object *obj = NULL;
  2317. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2318. struct drm_framebuffer *fb = &plane_config->fb->base;
  2319. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2320. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2321. PAGE_SIZE);
  2322. size_aligned -= base_aligned;
  2323. if (plane_config->size == 0)
  2324. return false;
  2325. /* If the FB is too big, just don't use it since fbdev is not very
  2326. * important and we should probably use that space with FBC or other
  2327. * features. */
  2328. if (size_aligned * 2 > ggtt->stolen_usable_size)
  2329. return false;
  2330. mutex_lock(&dev->struct_mutex);
  2331. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2332. base_aligned,
  2333. base_aligned,
  2334. size_aligned);
  2335. if (!obj) {
  2336. mutex_unlock(&dev->struct_mutex);
  2337. return false;
  2338. }
  2339. if (plane_config->tiling == I915_TILING_X)
  2340. obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
  2341. mode_cmd.pixel_format = fb->pixel_format;
  2342. mode_cmd.width = fb->width;
  2343. mode_cmd.height = fb->height;
  2344. mode_cmd.pitches[0] = fb->pitches[0];
  2345. mode_cmd.modifier[0] = fb->modifier[0];
  2346. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2347. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2348. &mode_cmd, obj)) {
  2349. DRM_DEBUG_KMS("intel fb init failed\n");
  2350. goto out_unref_obj;
  2351. }
  2352. mutex_unlock(&dev->struct_mutex);
  2353. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2354. return true;
  2355. out_unref_obj:
  2356. i915_gem_object_put(obj);
  2357. mutex_unlock(&dev->struct_mutex);
  2358. return false;
  2359. }
  2360. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2361. static void
  2362. update_state_fb(struct drm_plane *plane)
  2363. {
  2364. if (plane->fb == plane->state->fb)
  2365. return;
  2366. if (plane->state->fb)
  2367. drm_framebuffer_unreference(plane->state->fb);
  2368. plane->state->fb = plane->fb;
  2369. if (plane->state->fb)
  2370. drm_framebuffer_reference(plane->state->fb);
  2371. }
  2372. static void
  2373. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2374. struct intel_initial_plane_config *plane_config)
  2375. {
  2376. struct drm_device *dev = intel_crtc->base.dev;
  2377. struct drm_i915_private *dev_priv = to_i915(dev);
  2378. struct drm_crtc *c;
  2379. struct intel_crtc *i;
  2380. struct drm_i915_gem_object *obj;
  2381. struct drm_plane *primary = intel_crtc->base.primary;
  2382. struct drm_plane_state *plane_state = primary->state;
  2383. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2384. struct intel_plane *intel_plane = to_intel_plane(primary);
  2385. struct intel_plane_state *intel_state =
  2386. to_intel_plane_state(plane_state);
  2387. struct drm_framebuffer *fb;
  2388. if (!plane_config->fb)
  2389. return;
  2390. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2391. fb = &plane_config->fb->base;
  2392. goto valid_fb;
  2393. }
  2394. kfree(plane_config->fb);
  2395. /*
  2396. * Failed to alloc the obj, check to see if we should share
  2397. * an fb with another CRTC instead
  2398. */
  2399. for_each_crtc(dev, c) {
  2400. i = to_intel_crtc(c);
  2401. if (c == &intel_crtc->base)
  2402. continue;
  2403. if (!i->active)
  2404. continue;
  2405. fb = c->primary->fb;
  2406. if (!fb)
  2407. continue;
  2408. obj = intel_fb_obj(fb);
  2409. if (i915_gem_object_ggtt_offset(obj, NULL) == plane_config->base) {
  2410. drm_framebuffer_reference(fb);
  2411. goto valid_fb;
  2412. }
  2413. }
  2414. /*
  2415. * We've failed to reconstruct the BIOS FB. Current display state
  2416. * indicates that the primary plane is visible, but has a NULL FB,
  2417. * which will lead to problems later if we don't fix it up. The
  2418. * simplest solution is to just disable the primary plane now and
  2419. * pretend the BIOS never had it enabled.
  2420. */
  2421. to_intel_plane_state(plane_state)->base.visible = false;
  2422. crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
  2423. intel_pre_disable_primary_noatomic(&intel_crtc->base);
  2424. intel_plane->disable_plane(primary, &intel_crtc->base);
  2425. return;
  2426. valid_fb:
  2427. plane_state->src_x = 0;
  2428. plane_state->src_y = 0;
  2429. plane_state->src_w = fb->width << 16;
  2430. plane_state->src_h = fb->height << 16;
  2431. plane_state->crtc_x = 0;
  2432. plane_state->crtc_y = 0;
  2433. plane_state->crtc_w = fb->width;
  2434. plane_state->crtc_h = fb->height;
  2435. intel_state->base.src.x1 = plane_state->src_x;
  2436. intel_state->base.src.y1 = plane_state->src_y;
  2437. intel_state->base.src.x2 = plane_state->src_x + plane_state->src_w;
  2438. intel_state->base.src.y2 = plane_state->src_y + plane_state->src_h;
  2439. intel_state->base.dst.x1 = plane_state->crtc_x;
  2440. intel_state->base.dst.y1 = plane_state->crtc_y;
  2441. intel_state->base.dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
  2442. intel_state->base.dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
  2443. obj = intel_fb_obj(fb);
  2444. if (i915_gem_object_is_tiled(obj))
  2445. dev_priv->preserve_bios_swizzle = true;
  2446. drm_framebuffer_reference(fb);
  2447. primary->fb = primary->state->fb = fb;
  2448. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2449. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2450. atomic_or(to_intel_plane(primary)->frontbuffer_bit,
  2451. &obj->frontbuffer_bits);
  2452. }
  2453. static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
  2454. unsigned int rotation)
  2455. {
  2456. int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2457. switch (fb->modifier[plane]) {
  2458. case DRM_FORMAT_MOD_NONE:
  2459. case I915_FORMAT_MOD_X_TILED:
  2460. switch (cpp) {
  2461. case 8:
  2462. return 4096;
  2463. case 4:
  2464. case 2:
  2465. case 1:
  2466. return 8192;
  2467. default:
  2468. MISSING_CASE(cpp);
  2469. break;
  2470. }
  2471. break;
  2472. case I915_FORMAT_MOD_Y_TILED:
  2473. case I915_FORMAT_MOD_Yf_TILED:
  2474. switch (cpp) {
  2475. case 8:
  2476. return 2048;
  2477. case 4:
  2478. return 4096;
  2479. case 2:
  2480. case 1:
  2481. return 8192;
  2482. default:
  2483. MISSING_CASE(cpp);
  2484. break;
  2485. }
  2486. break;
  2487. default:
  2488. MISSING_CASE(fb->modifier[plane]);
  2489. }
  2490. return 2048;
  2491. }
  2492. static int skl_check_main_surface(struct intel_plane_state *plane_state)
  2493. {
  2494. const struct drm_i915_private *dev_priv = to_i915(plane_state->base.plane->dev);
  2495. const struct drm_framebuffer *fb = plane_state->base.fb;
  2496. unsigned int rotation = plane_state->base.rotation;
  2497. int x = plane_state->base.src.x1 >> 16;
  2498. int y = plane_state->base.src.y1 >> 16;
  2499. int w = drm_rect_width(&plane_state->base.src) >> 16;
  2500. int h = drm_rect_height(&plane_state->base.src) >> 16;
  2501. int max_width = skl_max_plane_width(fb, 0, rotation);
  2502. int max_height = 4096;
  2503. u32 alignment, offset, aux_offset = plane_state->aux.offset;
  2504. if (w > max_width || h > max_height) {
  2505. DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
  2506. w, h, max_width, max_height);
  2507. return -EINVAL;
  2508. }
  2509. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2510. offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
  2511. alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
  2512. /*
  2513. * AUX surface offset is specified as the distance from the
  2514. * main surface offset, and it must be non-negative. Make
  2515. * sure that is what we will get.
  2516. */
  2517. if (offset > aux_offset)
  2518. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2519. offset, aux_offset & ~(alignment - 1));
  2520. /*
  2521. * When using an X-tiled surface, the plane blows up
  2522. * if the x offset + width exceed the stride.
  2523. *
  2524. * TODO: linear and Y-tiled seem fine, Yf untested,
  2525. */
  2526. if (fb->modifier[0] == I915_FORMAT_MOD_X_TILED) {
  2527. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2528. while ((x + w) * cpp > fb->pitches[0]) {
  2529. if (offset == 0) {
  2530. DRM_DEBUG_KMS("Unable to find suitable display surface offset\n");
  2531. return -EINVAL;
  2532. }
  2533. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2534. offset, offset - alignment);
  2535. }
  2536. }
  2537. plane_state->main.offset = offset;
  2538. plane_state->main.x = x;
  2539. plane_state->main.y = y;
  2540. return 0;
  2541. }
  2542. static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
  2543. {
  2544. const struct drm_framebuffer *fb = plane_state->base.fb;
  2545. unsigned int rotation = plane_state->base.rotation;
  2546. int max_width = skl_max_plane_width(fb, 1, rotation);
  2547. int max_height = 4096;
  2548. int x = plane_state->base.src.x1 >> 17;
  2549. int y = plane_state->base.src.y1 >> 17;
  2550. int w = drm_rect_width(&plane_state->base.src) >> 17;
  2551. int h = drm_rect_height(&plane_state->base.src) >> 17;
  2552. u32 offset;
  2553. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2554. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2555. /* FIXME not quite sure how/if these apply to the chroma plane */
  2556. if (w > max_width || h > max_height) {
  2557. DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
  2558. w, h, max_width, max_height);
  2559. return -EINVAL;
  2560. }
  2561. plane_state->aux.offset = offset;
  2562. plane_state->aux.x = x;
  2563. plane_state->aux.y = y;
  2564. return 0;
  2565. }
  2566. int skl_check_plane_surface(struct intel_plane_state *plane_state)
  2567. {
  2568. const struct drm_framebuffer *fb = plane_state->base.fb;
  2569. unsigned int rotation = plane_state->base.rotation;
  2570. int ret;
  2571. /* Rotate src coordinates to match rotated GTT view */
  2572. if (intel_rotation_90_or_270(rotation))
  2573. drm_rect_rotate(&plane_state->base.src,
  2574. fb->width, fb->height, DRM_ROTATE_270);
  2575. /*
  2576. * Handle the AUX surface first since
  2577. * the main surface setup depends on it.
  2578. */
  2579. if (fb->pixel_format == DRM_FORMAT_NV12) {
  2580. ret = skl_check_nv12_aux_surface(plane_state);
  2581. if (ret)
  2582. return ret;
  2583. } else {
  2584. plane_state->aux.offset = ~0xfff;
  2585. plane_state->aux.x = 0;
  2586. plane_state->aux.y = 0;
  2587. }
  2588. ret = skl_check_main_surface(plane_state);
  2589. if (ret)
  2590. return ret;
  2591. return 0;
  2592. }
  2593. static void i9xx_update_primary_plane(struct drm_plane *primary,
  2594. const struct intel_crtc_state *crtc_state,
  2595. const struct intel_plane_state *plane_state)
  2596. {
  2597. struct drm_device *dev = primary->dev;
  2598. struct drm_i915_private *dev_priv = to_i915(dev);
  2599. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2600. struct drm_framebuffer *fb = plane_state->base.fb;
  2601. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2602. int plane = intel_crtc->plane;
  2603. u32 linear_offset;
  2604. u32 dspcntr;
  2605. i915_reg_t reg = DSPCNTR(plane);
  2606. unsigned int rotation = plane_state->base.rotation;
  2607. int x = plane_state->base.src.x1 >> 16;
  2608. int y = plane_state->base.src.y1 >> 16;
  2609. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2610. dspcntr |= DISPLAY_PLANE_ENABLE;
  2611. if (INTEL_INFO(dev)->gen < 4) {
  2612. if (intel_crtc->pipe == PIPE_B)
  2613. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2614. /* pipesrc and dspsize control the size that is scaled from,
  2615. * which should always be the user's requested size.
  2616. */
  2617. I915_WRITE(DSPSIZE(plane),
  2618. ((crtc_state->pipe_src_h - 1) << 16) |
  2619. (crtc_state->pipe_src_w - 1));
  2620. I915_WRITE(DSPPOS(plane), 0);
  2621. } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
  2622. I915_WRITE(PRIMSIZE(plane),
  2623. ((crtc_state->pipe_src_h - 1) << 16) |
  2624. (crtc_state->pipe_src_w - 1));
  2625. I915_WRITE(PRIMPOS(plane), 0);
  2626. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2627. }
  2628. switch (fb->pixel_format) {
  2629. case DRM_FORMAT_C8:
  2630. dspcntr |= DISPPLANE_8BPP;
  2631. break;
  2632. case DRM_FORMAT_XRGB1555:
  2633. dspcntr |= DISPPLANE_BGRX555;
  2634. break;
  2635. case DRM_FORMAT_RGB565:
  2636. dspcntr |= DISPPLANE_BGRX565;
  2637. break;
  2638. case DRM_FORMAT_XRGB8888:
  2639. dspcntr |= DISPPLANE_BGRX888;
  2640. break;
  2641. case DRM_FORMAT_XBGR8888:
  2642. dspcntr |= DISPPLANE_RGBX888;
  2643. break;
  2644. case DRM_FORMAT_XRGB2101010:
  2645. dspcntr |= DISPPLANE_BGRX101010;
  2646. break;
  2647. case DRM_FORMAT_XBGR2101010:
  2648. dspcntr |= DISPPLANE_RGBX101010;
  2649. break;
  2650. default:
  2651. BUG();
  2652. }
  2653. if (INTEL_GEN(dev_priv) >= 4 &&
  2654. fb->modifier[0] == I915_FORMAT_MOD_X_TILED)
  2655. dspcntr |= DISPPLANE_TILED;
  2656. if (IS_G4X(dev_priv))
  2657. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2658. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2659. if (INTEL_INFO(dev)->gen >= 4)
  2660. intel_crtc->dspaddr_offset =
  2661. intel_compute_tile_offset(&x, &y, plane_state, 0);
  2662. if (rotation == DRM_ROTATE_180) {
  2663. dspcntr |= DISPPLANE_ROTATE_180;
  2664. x += (crtc_state->pipe_src_w - 1);
  2665. y += (crtc_state->pipe_src_h - 1);
  2666. }
  2667. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2668. if (INTEL_INFO(dev)->gen < 4)
  2669. intel_crtc->dspaddr_offset = linear_offset;
  2670. intel_crtc->adjusted_x = x;
  2671. intel_crtc->adjusted_y = y;
  2672. I915_WRITE(reg, dspcntr);
  2673. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2674. if (INTEL_INFO(dev)->gen >= 4) {
  2675. I915_WRITE(DSPSURF(plane),
  2676. intel_fb_gtt_offset(fb, rotation) +
  2677. intel_crtc->dspaddr_offset);
  2678. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2679. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2680. } else
  2681. I915_WRITE(DSPADDR(plane), i915_gem_object_ggtt_offset(obj, NULL) + linear_offset);
  2682. POSTING_READ(reg);
  2683. }
  2684. static void i9xx_disable_primary_plane(struct drm_plane *primary,
  2685. struct drm_crtc *crtc)
  2686. {
  2687. struct drm_device *dev = crtc->dev;
  2688. struct drm_i915_private *dev_priv = to_i915(dev);
  2689. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2690. int plane = intel_crtc->plane;
  2691. I915_WRITE(DSPCNTR(plane), 0);
  2692. if (INTEL_INFO(dev_priv)->gen >= 4)
  2693. I915_WRITE(DSPSURF(plane), 0);
  2694. else
  2695. I915_WRITE(DSPADDR(plane), 0);
  2696. POSTING_READ(DSPCNTR(plane));
  2697. }
  2698. static void ironlake_update_primary_plane(struct drm_plane *primary,
  2699. const struct intel_crtc_state *crtc_state,
  2700. const struct intel_plane_state *plane_state)
  2701. {
  2702. struct drm_device *dev = primary->dev;
  2703. struct drm_i915_private *dev_priv = to_i915(dev);
  2704. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2705. struct drm_framebuffer *fb = plane_state->base.fb;
  2706. int plane = intel_crtc->plane;
  2707. u32 linear_offset;
  2708. u32 dspcntr;
  2709. i915_reg_t reg = DSPCNTR(plane);
  2710. unsigned int rotation = plane_state->base.rotation;
  2711. int x = plane_state->base.src.x1 >> 16;
  2712. int y = plane_state->base.src.y1 >> 16;
  2713. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2714. dspcntr |= DISPLAY_PLANE_ENABLE;
  2715. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  2716. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2717. switch (fb->pixel_format) {
  2718. case DRM_FORMAT_C8:
  2719. dspcntr |= DISPPLANE_8BPP;
  2720. break;
  2721. case DRM_FORMAT_RGB565:
  2722. dspcntr |= DISPPLANE_BGRX565;
  2723. break;
  2724. case DRM_FORMAT_XRGB8888:
  2725. dspcntr |= DISPPLANE_BGRX888;
  2726. break;
  2727. case DRM_FORMAT_XBGR8888:
  2728. dspcntr |= DISPPLANE_RGBX888;
  2729. break;
  2730. case DRM_FORMAT_XRGB2101010:
  2731. dspcntr |= DISPPLANE_BGRX101010;
  2732. break;
  2733. case DRM_FORMAT_XBGR2101010:
  2734. dspcntr |= DISPPLANE_RGBX101010;
  2735. break;
  2736. default:
  2737. BUG();
  2738. }
  2739. if (fb->modifier[0] == I915_FORMAT_MOD_X_TILED)
  2740. dspcntr |= DISPPLANE_TILED;
  2741. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv))
  2742. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2743. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2744. intel_crtc->dspaddr_offset =
  2745. intel_compute_tile_offset(&x, &y, plane_state, 0);
  2746. if (rotation == DRM_ROTATE_180) {
  2747. dspcntr |= DISPPLANE_ROTATE_180;
  2748. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
  2749. x += (crtc_state->pipe_src_w - 1);
  2750. y += (crtc_state->pipe_src_h - 1);
  2751. }
  2752. }
  2753. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2754. intel_crtc->adjusted_x = x;
  2755. intel_crtc->adjusted_y = y;
  2756. I915_WRITE(reg, dspcntr);
  2757. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2758. I915_WRITE(DSPSURF(plane),
  2759. intel_fb_gtt_offset(fb, rotation) +
  2760. intel_crtc->dspaddr_offset);
  2761. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  2762. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2763. } else {
  2764. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2765. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2766. }
  2767. POSTING_READ(reg);
  2768. }
  2769. u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
  2770. uint64_t fb_modifier, uint32_t pixel_format)
  2771. {
  2772. if (fb_modifier == DRM_FORMAT_MOD_NONE) {
  2773. return 64;
  2774. } else {
  2775. int cpp = drm_format_plane_cpp(pixel_format, 0);
  2776. return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  2777. }
  2778. }
  2779. u32 intel_fb_gtt_offset(struct drm_framebuffer *fb,
  2780. unsigned int rotation)
  2781. {
  2782. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2783. struct i915_ggtt_view view;
  2784. struct i915_vma *vma;
  2785. intel_fill_fb_ggtt_view(&view, fb, rotation);
  2786. vma = i915_gem_object_to_ggtt(obj, &view);
  2787. if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
  2788. view.type))
  2789. return -1;
  2790. return i915_ggtt_offset(vma);
  2791. }
  2792. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2793. {
  2794. struct drm_device *dev = intel_crtc->base.dev;
  2795. struct drm_i915_private *dev_priv = to_i915(dev);
  2796. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2797. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2798. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2799. }
  2800. /*
  2801. * This function detaches (aka. unbinds) unused scalers in hardware
  2802. */
  2803. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2804. {
  2805. struct intel_crtc_scaler_state *scaler_state;
  2806. int i;
  2807. scaler_state = &intel_crtc->config->scaler_state;
  2808. /* loop through and disable scalers that aren't in use */
  2809. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2810. if (!scaler_state->scalers[i].in_use)
  2811. skl_detach_scaler(intel_crtc, i);
  2812. }
  2813. }
  2814. u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
  2815. unsigned int rotation)
  2816. {
  2817. const struct drm_i915_private *dev_priv = to_i915(fb->dev);
  2818. u32 stride = intel_fb_pitch(fb, plane, rotation);
  2819. /*
  2820. * The stride is either expressed as a multiple of 64 bytes chunks for
  2821. * linear buffers or in number of tiles for tiled buffers.
  2822. */
  2823. if (intel_rotation_90_or_270(rotation)) {
  2824. int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2825. stride /= intel_tile_height(dev_priv, fb->modifier[0], cpp);
  2826. } else {
  2827. stride /= intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  2828. fb->pixel_format);
  2829. }
  2830. return stride;
  2831. }
  2832. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2833. {
  2834. switch (pixel_format) {
  2835. case DRM_FORMAT_C8:
  2836. return PLANE_CTL_FORMAT_INDEXED;
  2837. case DRM_FORMAT_RGB565:
  2838. return PLANE_CTL_FORMAT_RGB_565;
  2839. case DRM_FORMAT_XBGR8888:
  2840. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2841. case DRM_FORMAT_XRGB8888:
  2842. return PLANE_CTL_FORMAT_XRGB_8888;
  2843. /*
  2844. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2845. * to be already pre-multiplied. We need to add a knob (or a different
  2846. * DRM_FORMAT) for user-space to configure that.
  2847. */
  2848. case DRM_FORMAT_ABGR8888:
  2849. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2850. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2851. case DRM_FORMAT_ARGB8888:
  2852. return PLANE_CTL_FORMAT_XRGB_8888 |
  2853. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2854. case DRM_FORMAT_XRGB2101010:
  2855. return PLANE_CTL_FORMAT_XRGB_2101010;
  2856. case DRM_FORMAT_XBGR2101010:
  2857. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2858. case DRM_FORMAT_YUYV:
  2859. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2860. case DRM_FORMAT_YVYU:
  2861. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2862. case DRM_FORMAT_UYVY:
  2863. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2864. case DRM_FORMAT_VYUY:
  2865. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2866. default:
  2867. MISSING_CASE(pixel_format);
  2868. }
  2869. return 0;
  2870. }
  2871. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2872. {
  2873. switch (fb_modifier) {
  2874. case DRM_FORMAT_MOD_NONE:
  2875. break;
  2876. case I915_FORMAT_MOD_X_TILED:
  2877. return PLANE_CTL_TILED_X;
  2878. case I915_FORMAT_MOD_Y_TILED:
  2879. return PLANE_CTL_TILED_Y;
  2880. case I915_FORMAT_MOD_Yf_TILED:
  2881. return PLANE_CTL_TILED_YF;
  2882. default:
  2883. MISSING_CASE(fb_modifier);
  2884. }
  2885. return 0;
  2886. }
  2887. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2888. {
  2889. switch (rotation) {
  2890. case DRM_ROTATE_0:
  2891. break;
  2892. /*
  2893. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2894. * while i915 HW rotation is clockwise, thats why this swapping.
  2895. */
  2896. case DRM_ROTATE_90:
  2897. return PLANE_CTL_ROTATE_270;
  2898. case DRM_ROTATE_180:
  2899. return PLANE_CTL_ROTATE_180;
  2900. case DRM_ROTATE_270:
  2901. return PLANE_CTL_ROTATE_90;
  2902. default:
  2903. MISSING_CASE(rotation);
  2904. }
  2905. return 0;
  2906. }
  2907. static void skylake_update_primary_plane(struct drm_plane *plane,
  2908. const struct intel_crtc_state *crtc_state,
  2909. const struct intel_plane_state *plane_state)
  2910. {
  2911. struct drm_device *dev = plane->dev;
  2912. struct drm_i915_private *dev_priv = to_i915(dev);
  2913. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2914. struct drm_framebuffer *fb = plane_state->base.fb;
  2915. const struct skl_wm_values *wm = &dev_priv->wm.skl_results;
  2916. const struct skl_plane_wm *p_wm =
  2917. &crtc_state->wm.skl.optimal.planes[0];
  2918. int pipe = intel_crtc->pipe;
  2919. u32 plane_ctl;
  2920. unsigned int rotation = plane_state->base.rotation;
  2921. u32 stride = skl_plane_stride(fb, 0, rotation);
  2922. u32 surf_addr = plane_state->main.offset;
  2923. int scaler_id = plane_state->scaler_id;
  2924. int src_x = plane_state->main.x;
  2925. int src_y = plane_state->main.y;
  2926. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  2927. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  2928. int dst_x = plane_state->base.dst.x1;
  2929. int dst_y = plane_state->base.dst.y1;
  2930. int dst_w = drm_rect_width(&plane_state->base.dst);
  2931. int dst_h = drm_rect_height(&plane_state->base.dst);
  2932. plane_ctl = PLANE_CTL_ENABLE |
  2933. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2934. PLANE_CTL_PIPE_CSC_ENABLE;
  2935. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2936. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2937. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2938. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2939. /* Sizes are 0 based */
  2940. src_w--;
  2941. src_h--;
  2942. dst_w--;
  2943. dst_h--;
  2944. intel_crtc->dspaddr_offset = surf_addr;
  2945. intel_crtc->adjusted_x = src_x;
  2946. intel_crtc->adjusted_y = src_y;
  2947. if (wm->dirty_pipes & drm_crtc_mask(&intel_crtc->base))
  2948. skl_write_plane_wm(intel_crtc, p_wm, &wm->ddb, 0);
  2949. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2950. I915_WRITE(PLANE_OFFSET(pipe, 0), (src_y << 16) | src_x);
  2951. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2952. I915_WRITE(PLANE_SIZE(pipe, 0), (src_h << 16) | src_w);
  2953. if (scaler_id >= 0) {
  2954. uint32_t ps_ctrl = 0;
  2955. WARN_ON(!dst_w || !dst_h);
  2956. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2957. crtc_state->scaler_state.scalers[scaler_id].mode;
  2958. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2959. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2960. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2961. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2962. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2963. } else {
  2964. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2965. }
  2966. I915_WRITE(PLANE_SURF(pipe, 0),
  2967. intel_fb_gtt_offset(fb, rotation) + surf_addr);
  2968. POSTING_READ(PLANE_SURF(pipe, 0));
  2969. }
  2970. static void skylake_disable_primary_plane(struct drm_plane *primary,
  2971. struct drm_crtc *crtc)
  2972. {
  2973. struct drm_device *dev = crtc->dev;
  2974. struct drm_i915_private *dev_priv = to_i915(dev);
  2975. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2976. struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
  2977. const struct skl_plane_wm *p_wm = &cstate->wm.skl.optimal.planes[0];
  2978. int pipe = intel_crtc->pipe;
  2979. /*
  2980. * We only populate skl_results on watermark updates, and if the
  2981. * plane's visiblity isn't actually changing neither is its watermarks.
  2982. */
  2983. if (!crtc->primary->state->visible)
  2984. skl_write_plane_wm(intel_crtc, p_wm,
  2985. &dev_priv->wm.skl_results.ddb, 0);
  2986. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2987. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2988. POSTING_READ(PLANE_SURF(pipe, 0));
  2989. }
  2990. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2991. static int
  2992. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2993. int x, int y, enum mode_set_atomic state)
  2994. {
  2995. /* Support for kgdboc is disabled, this needs a major rework. */
  2996. DRM_ERROR("legacy panic handler not supported any more.\n");
  2997. return -ENODEV;
  2998. }
  2999. static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
  3000. {
  3001. struct intel_crtc *crtc;
  3002. for_each_intel_crtc(&dev_priv->drm, crtc)
  3003. intel_finish_page_flip_cs(dev_priv, crtc->pipe);
  3004. }
  3005. static void intel_update_primary_planes(struct drm_device *dev)
  3006. {
  3007. struct drm_crtc *crtc;
  3008. for_each_crtc(dev, crtc) {
  3009. struct intel_plane *plane = to_intel_plane(crtc->primary);
  3010. struct intel_plane_state *plane_state =
  3011. to_intel_plane_state(plane->base.state);
  3012. if (plane_state->base.visible)
  3013. plane->update_plane(&plane->base,
  3014. to_intel_crtc_state(crtc->state),
  3015. plane_state);
  3016. }
  3017. }
  3018. static int
  3019. __intel_display_resume(struct drm_device *dev,
  3020. struct drm_atomic_state *state)
  3021. {
  3022. struct drm_crtc_state *crtc_state;
  3023. struct drm_crtc *crtc;
  3024. int i, ret;
  3025. intel_modeset_setup_hw_state(dev);
  3026. i915_redisable_vga(dev);
  3027. if (!state)
  3028. return 0;
  3029. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  3030. /*
  3031. * Force recalculation even if we restore
  3032. * current state. With fast modeset this may not result
  3033. * in a modeset when the state is compatible.
  3034. */
  3035. crtc_state->mode_changed = true;
  3036. }
  3037. /* ignore any reset values/BIOS leftovers in the WM registers */
  3038. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  3039. ret = drm_atomic_commit(state);
  3040. WARN_ON(ret == -EDEADLK);
  3041. return ret;
  3042. }
  3043. static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
  3044. {
  3045. return intel_has_gpu_reset(dev_priv) &&
  3046. INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
  3047. }
  3048. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  3049. {
  3050. struct drm_device *dev = &dev_priv->drm;
  3051. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3052. struct drm_atomic_state *state;
  3053. int ret;
  3054. /*
  3055. * Need mode_config.mutex so that we don't
  3056. * trample ongoing ->detect() and whatnot.
  3057. */
  3058. mutex_lock(&dev->mode_config.mutex);
  3059. drm_modeset_acquire_init(ctx, 0);
  3060. while (1) {
  3061. ret = drm_modeset_lock_all_ctx(dev, ctx);
  3062. if (ret != -EDEADLK)
  3063. break;
  3064. drm_modeset_backoff(ctx);
  3065. }
  3066. /* reset doesn't touch the display, but flips might get nuked anyway, */
  3067. if (!i915.force_reset_modeset_test &&
  3068. !gpu_reset_clobbers_display(dev_priv))
  3069. return;
  3070. /*
  3071. * Disabling the crtcs gracefully seems nicer. Also the
  3072. * g33 docs say we should at least disable all the planes.
  3073. */
  3074. state = drm_atomic_helper_duplicate_state(dev, ctx);
  3075. if (IS_ERR(state)) {
  3076. ret = PTR_ERR(state);
  3077. state = NULL;
  3078. DRM_ERROR("Duplicating state failed with %i\n", ret);
  3079. goto err;
  3080. }
  3081. ret = drm_atomic_helper_disable_all(dev, ctx);
  3082. if (ret) {
  3083. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  3084. goto err;
  3085. }
  3086. dev_priv->modeset_restore_state = state;
  3087. state->acquire_ctx = ctx;
  3088. return;
  3089. err:
  3090. drm_atomic_state_free(state);
  3091. }
  3092. void intel_finish_reset(struct drm_i915_private *dev_priv)
  3093. {
  3094. struct drm_device *dev = &dev_priv->drm;
  3095. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3096. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  3097. int ret;
  3098. /*
  3099. * Flips in the rings will be nuked by the reset,
  3100. * so complete all pending flips so that user space
  3101. * will get its events and not get stuck.
  3102. */
  3103. intel_complete_page_flips(dev_priv);
  3104. dev_priv->modeset_restore_state = NULL;
  3105. /* reset doesn't touch the display */
  3106. if (!gpu_reset_clobbers_display(dev_priv)) {
  3107. if (!state) {
  3108. /*
  3109. * Flips in the rings have been nuked by the reset,
  3110. * so update the base address of all primary
  3111. * planes to the the last fb to make sure we're
  3112. * showing the correct fb after a reset.
  3113. *
  3114. * FIXME: Atomic will make this obsolete since we won't schedule
  3115. * CS-based flips (which might get lost in gpu resets) any more.
  3116. */
  3117. intel_update_primary_planes(dev);
  3118. } else {
  3119. ret = __intel_display_resume(dev, state);
  3120. if (ret)
  3121. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3122. }
  3123. } else {
  3124. /*
  3125. * The display has been reset as well,
  3126. * so need a full re-initialization.
  3127. */
  3128. intel_runtime_pm_disable_interrupts(dev_priv);
  3129. intel_runtime_pm_enable_interrupts(dev_priv);
  3130. intel_pps_unlock_regs_wa(dev_priv);
  3131. intel_modeset_init_hw(dev);
  3132. spin_lock_irq(&dev_priv->irq_lock);
  3133. if (dev_priv->display.hpd_irq_setup)
  3134. dev_priv->display.hpd_irq_setup(dev_priv);
  3135. spin_unlock_irq(&dev_priv->irq_lock);
  3136. ret = __intel_display_resume(dev, state);
  3137. if (ret)
  3138. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3139. intel_hpd_init(dev_priv);
  3140. }
  3141. drm_modeset_drop_locks(ctx);
  3142. drm_modeset_acquire_fini(ctx);
  3143. mutex_unlock(&dev->mode_config.mutex);
  3144. }
  3145. static bool abort_flip_on_reset(struct intel_crtc *crtc)
  3146. {
  3147. struct i915_gpu_error *error = &to_i915(crtc->base.dev)->gpu_error;
  3148. if (i915_reset_in_progress(error))
  3149. return true;
  3150. if (crtc->reset_count != i915_reset_count(error))
  3151. return true;
  3152. return false;
  3153. }
  3154. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  3155. {
  3156. struct drm_device *dev = crtc->dev;
  3157. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3158. bool pending;
  3159. if (abort_flip_on_reset(intel_crtc))
  3160. return false;
  3161. spin_lock_irq(&dev->event_lock);
  3162. pending = to_intel_crtc(crtc)->flip_work != NULL;
  3163. spin_unlock_irq(&dev->event_lock);
  3164. return pending;
  3165. }
  3166. static void intel_update_pipe_config(struct intel_crtc *crtc,
  3167. struct intel_crtc_state *old_crtc_state)
  3168. {
  3169. struct drm_device *dev = crtc->base.dev;
  3170. struct drm_i915_private *dev_priv = to_i915(dev);
  3171. struct intel_crtc_state *pipe_config =
  3172. to_intel_crtc_state(crtc->base.state);
  3173. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  3174. crtc->base.mode = crtc->base.state->mode;
  3175. DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
  3176. old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
  3177. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  3178. /*
  3179. * Update pipe size and adjust fitter if needed: the reason for this is
  3180. * that in compute_mode_changes we check the native mode (not the pfit
  3181. * mode) to see if we can flip rather than do a full mode set. In the
  3182. * fastboot case, we'll flip, but if we don't update the pipesrc and
  3183. * pfit state, we'll end up with a big fb scanned out into the wrong
  3184. * sized surface.
  3185. */
  3186. I915_WRITE(PIPESRC(crtc->pipe),
  3187. ((pipe_config->pipe_src_w - 1) << 16) |
  3188. (pipe_config->pipe_src_h - 1));
  3189. /* on skylake this is done by detaching scalers */
  3190. if (INTEL_INFO(dev)->gen >= 9) {
  3191. skl_detach_scalers(crtc);
  3192. if (pipe_config->pch_pfit.enabled)
  3193. skylake_pfit_enable(crtc);
  3194. } else if (HAS_PCH_SPLIT(dev_priv)) {
  3195. if (pipe_config->pch_pfit.enabled)
  3196. ironlake_pfit_enable(crtc);
  3197. else if (old_crtc_state->pch_pfit.enabled)
  3198. ironlake_pfit_disable(crtc, true);
  3199. }
  3200. }
  3201. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  3202. {
  3203. struct drm_device *dev = crtc->dev;
  3204. struct drm_i915_private *dev_priv = to_i915(dev);
  3205. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3206. int pipe = intel_crtc->pipe;
  3207. i915_reg_t reg;
  3208. u32 temp;
  3209. /* enable normal train */
  3210. reg = FDI_TX_CTL(pipe);
  3211. temp = I915_READ(reg);
  3212. if (IS_IVYBRIDGE(dev_priv)) {
  3213. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3214. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  3215. } else {
  3216. temp &= ~FDI_LINK_TRAIN_NONE;
  3217. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  3218. }
  3219. I915_WRITE(reg, temp);
  3220. reg = FDI_RX_CTL(pipe);
  3221. temp = I915_READ(reg);
  3222. if (HAS_PCH_CPT(dev_priv)) {
  3223. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3224. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  3225. } else {
  3226. temp &= ~FDI_LINK_TRAIN_NONE;
  3227. temp |= FDI_LINK_TRAIN_NONE;
  3228. }
  3229. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  3230. /* wait one idle pattern time */
  3231. POSTING_READ(reg);
  3232. udelay(1000);
  3233. /* IVB wants error correction enabled */
  3234. if (IS_IVYBRIDGE(dev_priv))
  3235. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  3236. FDI_FE_ERRC_ENABLE);
  3237. }
  3238. /* The FDI link training functions for ILK/Ibexpeak. */
  3239. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  3240. {
  3241. struct drm_device *dev = crtc->dev;
  3242. struct drm_i915_private *dev_priv = to_i915(dev);
  3243. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3244. int pipe = intel_crtc->pipe;
  3245. i915_reg_t reg;
  3246. u32 temp, tries;
  3247. /* FDI needs bits from pipe first */
  3248. assert_pipe_enabled(dev_priv, pipe);
  3249. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3250. for train result */
  3251. reg = FDI_RX_IMR(pipe);
  3252. temp = I915_READ(reg);
  3253. temp &= ~FDI_RX_SYMBOL_LOCK;
  3254. temp &= ~FDI_RX_BIT_LOCK;
  3255. I915_WRITE(reg, temp);
  3256. I915_READ(reg);
  3257. udelay(150);
  3258. /* enable CPU FDI TX and PCH FDI RX */
  3259. reg = FDI_TX_CTL(pipe);
  3260. temp = I915_READ(reg);
  3261. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3262. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3263. temp &= ~FDI_LINK_TRAIN_NONE;
  3264. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3265. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3266. reg = FDI_RX_CTL(pipe);
  3267. temp = I915_READ(reg);
  3268. temp &= ~FDI_LINK_TRAIN_NONE;
  3269. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3270. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3271. POSTING_READ(reg);
  3272. udelay(150);
  3273. /* Ironlake workaround, enable clock pointer after FDI enable*/
  3274. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3275. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  3276. FDI_RX_PHASE_SYNC_POINTER_EN);
  3277. reg = FDI_RX_IIR(pipe);
  3278. for (tries = 0; tries < 5; tries++) {
  3279. temp = I915_READ(reg);
  3280. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3281. if ((temp & FDI_RX_BIT_LOCK)) {
  3282. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3283. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3284. break;
  3285. }
  3286. }
  3287. if (tries == 5)
  3288. DRM_ERROR("FDI train 1 fail!\n");
  3289. /* Train 2 */
  3290. reg = FDI_TX_CTL(pipe);
  3291. temp = I915_READ(reg);
  3292. temp &= ~FDI_LINK_TRAIN_NONE;
  3293. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3294. I915_WRITE(reg, temp);
  3295. reg = FDI_RX_CTL(pipe);
  3296. temp = I915_READ(reg);
  3297. temp &= ~FDI_LINK_TRAIN_NONE;
  3298. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3299. I915_WRITE(reg, temp);
  3300. POSTING_READ(reg);
  3301. udelay(150);
  3302. reg = FDI_RX_IIR(pipe);
  3303. for (tries = 0; tries < 5; tries++) {
  3304. temp = I915_READ(reg);
  3305. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3306. if (temp & FDI_RX_SYMBOL_LOCK) {
  3307. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3308. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3309. break;
  3310. }
  3311. }
  3312. if (tries == 5)
  3313. DRM_ERROR("FDI train 2 fail!\n");
  3314. DRM_DEBUG_KMS("FDI train done\n");
  3315. }
  3316. static const int snb_b_fdi_train_param[] = {
  3317. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3318. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3319. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3320. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3321. };
  3322. /* The FDI link training functions for SNB/Cougarpoint. */
  3323. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  3324. {
  3325. struct drm_device *dev = crtc->dev;
  3326. struct drm_i915_private *dev_priv = to_i915(dev);
  3327. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3328. int pipe = intel_crtc->pipe;
  3329. i915_reg_t reg;
  3330. u32 temp, i, retry;
  3331. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3332. for train result */
  3333. reg = FDI_RX_IMR(pipe);
  3334. temp = I915_READ(reg);
  3335. temp &= ~FDI_RX_SYMBOL_LOCK;
  3336. temp &= ~FDI_RX_BIT_LOCK;
  3337. I915_WRITE(reg, temp);
  3338. POSTING_READ(reg);
  3339. udelay(150);
  3340. /* enable CPU FDI TX and PCH FDI RX */
  3341. reg = FDI_TX_CTL(pipe);
  3342. temp = I915_READ(reg);
  3343. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3344. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3345. temp &= ~FDI_LINK_TRAIN_NONE;
  3346. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3347. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3348. /* SNB-B */
  3349. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3350. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3351. I915_WRITE(FDI_RX_MISC(pipe),
  3352. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3353. reg = FDI_RX_CTL(pipe);
  3354. temp = I915_READ(reg);
  3355. if (HAS_PCH_CPT(dev_priv)) {
  3356. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3357. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3358. } else {
  3359. temp &= ~FDI_LINK_TRAIN_NONE;
  3360. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3361. }
  3362. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3363. POSTING_READ(reg);
  3364. udelay(150);
  3365. for (i = 0; i < 4; i++) {
  3366. reg = FDI_TX_CTL(pipe);
  3367. temp = I915_READ(reg);
  3368. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3369. temp |= snb_b_fdi_train_param[i];
  3370. I915_WRITE(reg, temp);
  3371. POSTING_READ(reg);
  3372. udelay(500);
  3373. for (retry = 0; retry < 5; retry++) {
  3374. reg = FDI_RX_IIR(pipe);
  3375. temp = I915_READ(reg);
  3376. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3377. if (temp & FDI_RX_BIT_LOCK) {
  3378. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3379. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3380. break;
  3381. }
  3382. udelay(50);
  3383. }
  3384. if (retry < 5)
  3385. break;
  3386. }
  3387. if (i == 4)
  3388. DRM_ERROR("FDI train 1 fail!\n");
  3389. /* Train 2 */
  3390. reg = FDI_TX_CTL(pipe);
  3391. temp = I915_READ(reg);
  3392. temp &= ~FDI_LINK_TRAIN_NONE;
  3393. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3394. if (IS_GEN6(dev_priv)) {
  3395. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3396. /* SNB-B */
  3397. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3398. }
  3399. I915_WRITE(reg, temp);
  3400. reg = FDI_RX_CTL(pipe);
  3401. temp = I915_READ(reg);
  3402. if (HAS_PCH_CPT(dev_priv)) {
  3403. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3404. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3405. } else {
  3406. temp &= ~FDI_LINK_TRAIN_NONE;
  3407. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3408. }
  3409. I915_WRITE(reg, temp);
  3410. POSTING_READ(reg);
  3411. udelay(150);
  3412. for (i = 0; i < 4; i++) {
  3413. reg = FDI_TX_CTL(pipe);
  3414. temp = I915_READ(reg);
  3415. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3416. temp |= snb_b_fdi_train_param[i];
  3417. I915_WRITE(reg, temp);
  3418. POSTING_READ(reg);
  3419. udelay(500);
  3420. for (retry = 0; retry < 5; retry++) {
  3421. reg = FDI_RX_IIR(pipe);
  3422. temp = I915_READ(reg);
  3423. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3424. if (temp & FDI_RX_SYMBOL_LOCK) {
  3425. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3426. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3427. break;
  3428. }
  3429. udelay(50);
  3430. }
  3431. if (retry < 5)
  3432. break;
  3433. }
  3434. if (i == 4)
  3435. DRM_ERROR("FDI train 2 fail!\n");
  3436. DRM_DEBUG_KMS("FDI train done.\n");
  3437. }
  3438. /* Manual link training for Ivy Bridge A0 parts */
  3439. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3440. {
  3441. struct drm_device *dev = crtc->dev;
  3442. struct drm_i915_private *dev_priv = to_i915(dev);
  3443. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3444. int pipe = intel_crtc->pipe;
  3445. i915_reg_t reg;
  3446. u32 temp, i, j;
  3447. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3448. for train result */
  3449. reg = FDI_RX_IMR(pipe);
  3450. temp = I915_READ(reg);
  3451. temp &= ~FDI_RX_SYMBOL_LOCK;
  3452. temp &= ~FDI_RX_BIT_LOCK;
  3453. I915_WRITE(reg, temp);
  3454. POSTING_READ(reg);
  3455. udelay(150);
  3456. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3457. I915_READ(FDI_RX_IIR(pipe)));
  3458. /* Try each vswing and preemphasis setting twice before moving on */
  3459. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3460. /* disable first in case we need to retry */
  3461. reg = FDI_TX_CTL(pipe);
  3462. temp = I915_READ(reg);
  3463. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3464. temp &= ~FDI_TX_ENABLE;
  3465. I915_WRITE(reg, temp);
  3466. reg = FDI_RX_CTL(pipe);
  3467. temp = I915_READ(reg);
  3468. temp &= ~FDI_LINK_TRAIN_AUTO;
  3469. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3470. temp &= ~FDI_RX_ENABLE;
  3471. I915_WRITE(reg, temp);
  3472. /* enable CPU FDI TX and PCH FDI RX */
  3473. reg = FDI_TX_CTL(pipe);
  3474. temp = I915_READ(reg);
  3475. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3476. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3477. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3478. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3479. temp |= snb_b_fdi_train_param[j/2];
  3480. temp |= FDI_COMPOSITE_SYNC;
  3481. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3482. I915_WRITE(FDI_RX_MISC(pipe),
  3483. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3484. reg = FDI_RX_CTL(pipe);
  3485. temp = I915_READ(reg);
  3486. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3487. temp |= FDI_COMPOSITE_SYNC;
  3488. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3489. POSTING_READ(reg);
  3490. udelay(1); /* should be 0.5us */
  3491. for (i = 0; i < 4; i++) {
  3492. reg = FDI_RX_IIR(pipe);
  3493. temp = I915_READ(reg);
  3494. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3495. if (temp & FDI_RX_BIT_LOCK ||
  3496. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3497. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3498. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3499. i);
  3500. break;
  3501. }
  3502. udelay(1); /* should be 0.5us */
  3503. }
  3504. if (i == 4) {
  3505. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3506. continue;
  3507. }
  3508. /* Train 2 */
  3509. reg = FDI_TX_CTL(pipe);
  3510. temp = I915_READ(reg);
  3511. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3512. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3513. I915_WRITE(reg, temp);
  3514. reg = FDI_RX_CTL(pipe);
  3515. temp = I915_READ(reg);
  3516. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3517. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3518. I915_WRITE(reg, temp);
  3519. POSTING_READ(reg);
  3520. udelay(2); /* should be 1.5us */
  3521. for (i = 0; i < 4; i++) {
  3522. reg = FDI_RX_IIR(pipe);
  3523. temp = I915_READ(reg);
  3524. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3525. if (temp & FDI_RX_SYMBOL_LOCK ||
  3526. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3527. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3528. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3529. i);
  3530. goto train_done;
  3531. }
  3532. udelay(2); /* should be 1.5us */
  3533. }
  3534. if (i == 4)
  3535. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3536. }
  3537. train_done:
  3538. DRM_DEBUG_KMS("FDI train done.\n");
  3539. }
  3540. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3541. {
  3542. struct drm_device *dev = intel_crtc->base.dev;
  3543. struct drm_i915_private *dev_priv = to_i915(dev);
  3544. int pipe = intel_crtc->pipe;
  3545. i915_reg_t reg;
  3546. u32 temp;
  3547. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3548. reg = FDI_RX_CTL(pipe);
  3549. temp = I915_READ(reg);
  3550. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3551. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3552. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3553. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3554. POSTING_READ(reg);
  3555. udelay(200);
  3556. /* Switch from Rawclk to PCDclk */
  3557. temp = I915_READ(reg);
  3558. I915_WRITE(reg, temp | FDI_PCDCLK);
  3559. POSTING_READ(reg);
  3560. udelay(200);
  3561. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3562. reg = FDI_TX_CTL(pipe);
  3563. temp = I915_READ(reg);
  3564. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3565. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3566. POSTING_READ(reg);
  3567. udelay(100);
  3568. }
  3569. }
  3570. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3571. {
  3572. struct drm_device *dev = intel_crtc->base.dev;
  3573. struct drm_i915_private *dev_priv = to_i915(dev);
  3574. int pipe = intel_crtc->pipe;
  3575. i915_reg_t reg;
  3576. u32 temp;
  3577. /* Switch from PCDclk to Rawclk */
  3578. reg = FDI_RX_CTL(pipe);
  3579. temp = I915_READ(reg);
  3580. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3581. /* Disable CPU FDI TX PLL */
  3582. reg = FDI_TX_CTL(pipe);
  3583. temp = I915_READ(reg);
  3584. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3585. POSTING_READ(reg);
  3586. udelay(100);
  3587. reg = FDI_RX_CTL(pipe);
  3588. temp = I915_READ(reg);
  3589. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3590. /* Wait for the clocks to turn off. */
  3591. POSTING_READ(reg);
  3592. udelay(100);
  3593. }
  3594. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3595. {
  3596. struct drm_device *dev = crtc->dev;
  3597. struct drm_i915_private *dev_priv = to_i915(dev);
  3598. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3599. int pipe = intel_crtc->pipe;
  3600. i915_reg_t reg;
  3601. u32 temp;
  3602. /* disable CPU FDI tx and PCH FDI rx */
  3603. reg = FDI_TX_CTL(pipe);
  3604. temp = I915_READ(reg);
  3605. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3606. POSTING_READ(reg);
  3607. reg = FDI_RX_CTL(pipe);
  3608. temp = I915_READ(reg);
  3609. temp &= ~(0x7 << 16);
  3610. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3611. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3612. POSTING_READ(reg);
  3613. udelay(100);
  3614. /* Ironlake workaround, disable clock pointer after downing FDI */
  3615. if (HAS_PCH_IBX(dev_priv))
  3616. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3617. /* still set train pattern 1 */
  3618. reg = FDI_TX_CTL(pipe);
  3619. temp = I915_READ(reg);
  3620. temp &= ~FDI_LINK_TRAIN_NONE;
  3621. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3622. I915_WRITE(reg, temp);
  3623. reg = FDI_RX_CTL(pipe);
  3624. temp = I915_READ(reg);
  3625. if (HAS_PCH_CPT(dev_priv)) {
  3626. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3627. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3628. } else {
  3629. temp &= ~FDI_LINK_TRAIN_NONE;
  3630. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3631. }
  3632. /* BPC in FDI rx is consistent with that in PIPECONF */
  3633. temp &= ~(0x07 << 16);
  3634. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3635. I915_WRITE(reg, temp);
  3636. POSTING_READ(reg);
  3637. udelay(100);
  3638. }
  3639. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3640. {
  3641. struct intel_crtc *crtc;
  3642. /* Note that we don't need to be called with mode_config.lock here
  3643. * as our list of CRTC objects is static for the lifetime of the
  3644. * device and so cannot disappear as we iterate. Similarly, we can
  3645. * happily treat the predicates as racy, atomic checks as userspace
  3646. * cannot claim and pin a new fb without at least acquring the
  3647. * struct_mutex and so serialising with us.
  3648. */
  3649. for_each_intel_crtc(dev, crtc) {
  3650. if (atomic_read(&crtc->unpin_work_count) == 0)
  3651. continue;
  3652. if (crtc->flip_work)
  3653. intel_wait_for_vblank(dev, crtc->pipe);
  3654. return true;
  3655. }
  3656. return false;
  3657. }
  3658. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3659. {
  3660. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3661. struct intel_flip_work *work = intel_crtc->flip_work;
  3662. intel_crtc->flip_work = NULL;
  3663. if (work->event)
  3664. drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
  3665. drm_crtc_vblank_put(&intel_crtc->base);
  3666. wake_up_all(&dev_priv->pending_flip_queue);
  3667. queue_work(dev_priv->wq, &work->unpin_work);
  3668. trace_i915_flip_complete(intel_crtc->plane,
  3669. work->pending_flip_obj);
  3670. }
  3671. static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3672. {
  3673. struct drm_device *dev = crtc->dev;
  3674. struct drm_i915_private *dev_priv = to_i915(dev);
  3675. long ret;
  3676. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3677. ret = wait_event_interruptible_timeout(
  3678. dev_priv->pending_flip_queue,
  3679. !intel_crtc_has_pending_flip(crtc),
  3680. 60*HZ);
  3681. if (ret < 0)
  3682. return ret;
  3683. if (ret == 0) {
  3684. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3685. struct intel_flip_work *work;
  3686. spin_lock_irq(&dev->event_lock);
  3687. work = intel_crtc->flip_work;
  3688. if (work && !is_mmio_work(work)) {
  3689. WARN_ONCE(1, "Removing stuck page flip\n");
  3690. page_flip_completed(intel_crtc);
  3691. }
  3692. spin_unlock_irq(&dev->event_lock);
  3693. }
  3694. return 0;
  3695. }
  3696. void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3697. {
  3698. u32 temp;
  3699. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3700. mutex_lock(&dev_priv->sb_lock);
  3701. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3702. temp |= SBI_SSCCTL_DISABLE;
  3703. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3704. mutex_unlock(&dev_priv->sb_lock);
  3705. }
  3706. /* Program iCLKIP clock to the desired frequency */
  3707. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3708. {
  3709. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  3710. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3711. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3712. u32 temp;
  3713. lpt_disable_iclkip(dev_priv);
  3714. /* The iCLK virtual clock root frequency is in MHz,
  3715. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3716. * divisors, it is necessary to divide one by another, so we
  3717. * convert the virtual clock precision to KHz here for higher
  3718. * precision.
  3719. */
  3720. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3721. u32 iclk_virtual_root_freq = 172800 * 1000;
  3722. u32 iclk_pi_range = 64;
  3723. u32 desired_divisor;
  3724. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3725. clock << auxdiv);
  3726. divsel = (desired_divisor / iclk_pi_range) - 2;
  3727. phaseinc = desired_divisor % iclk_pi_range;
  3728. /*
  3729. * Near 20MHz is a corner case which is
  3730. * out of range for the 7-bit divisor
  3731. */
  3732. if (divsel <= 0x7f)
  3733. break;
  3734. }
  3735. /* This should not happen with any sane values */
  3736. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3737. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3738. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3739. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3740. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3741. clock,
  3742. auxdiv,
  3743. divsel,
  3744. phasedir,
  3745. phaseinc);
  3746. mutex_lock(&dev_priv->sb_lock);
  3747. /* Program SSCDIVINTPHASE6 */
  3748. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3749. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3750. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3751. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3752. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3753. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3754. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3755. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3756. /* Program SSCAUXDIV */
  3757. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3758. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3759. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3760. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3761. /* Enable modulator and associated divider */
  3762. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3763. temp &= ~SBI_SSCCTL_DISABLE;
  3764. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3765. mutex_unlock(&dev_priv->sb_lock);
  3766. /* Wait for initialization time */
  3767. udelay(24);
  3768. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3769. }
  3770. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3771. {
  3772. u32 divsel, phaseinc, auxdiv;
  3773. u32 iclk_virtual_root_freq = 172800 * 1000;
  3774. u32 iclk_pi_range = 64;
  3775. u32 desired_divisor;
  3776. u32 temp;
  3777. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3778. return 0;
  3779. mutex_lock(&dev_priv->sb_lock);
  3780. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3781. if (temp & SBI_SSCCTL_DISABLE) {
  3782. mutex_unlock(&dev_priv->sb_lock);
  3783. return 0;
  3784. }
  3785. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3786. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3787. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3788. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3789. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3790. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3791. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3792. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3793. mutex_unlock(&dev_priv->sb_lock);
  3794. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3795. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3796. desired_divisor << auxdiv);
  3797. }
  3798. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3799. enum pipe pch_transcoder)
  3800. {
  3801. struct drm_device *dev = crtc->base.dev;
  3802. struct drm_i915_private *dev_priv = to_i915(dev);
  3803. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3804. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3805. I915_READ(HTOTAL(cpu_transcoder)));
  3806. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3807. I915_READ(HBLANK(cpu_transcoder)));
  3808. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3809. I915_READ(HSYNC(cpu_transcoder)));
  3810. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3811. I915_READ(VTOTAL(cpu_transcoder)));
  3812. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3813. I915_READ(VBLANK(cpu_transcoder)));
  3814. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3815. I915_READ(VSYNC(cpu_transcoder)));
  3816. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3817. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3818. }
  3819. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3820. {
  3821. struct drm_i915_private *dev_priv = to_i915(dev);
  3822. uint32_t temp;
  3823. temp = I915_READ(SOUTH_CHICKEN1);
  3824. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3825. return;
  3826. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3827. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3828. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3829. if (enable)
  3830. temp |= FDI_BC_BIFURCATION_SELECT;
  3831. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3832. I915_WRITE(SOUTH_CHICKEN1, temp);
  3833. POSTING_READ(SOUTH_CHICKEN1);
  3834. }
  3835. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3836. {
  3837. struct drm_device *dev = intel_crtc->base.dev;
  3838. switch (intel_crtc->pipe) {
  3839. case PIPE_A:
  3840. break;
  3841. case PIPE_B:
  3842. if (intel_crtc->config->fdi_lanes > 2)
  3843. cpt_set_fdi_bc_bifurcation(dev, false);
  3844. else
  3845. cpt_set_fdi_bc_bifurcation(dev, true);
  3846. break;
  3847. case PIPE_C:
  3848. cpt_set_fdi_bc_bifurcation(dev, true);
  3849. break;
  3850. default:
  3851. BUG();
  3852. }
  3853. }
  3854. /* Return which DP Port should be selected for Transcoder DP control */
  3855. static enum port
  3856. intel_trans_dp_port_sel(struct drm_crtc *crtc)
  3857. {
  3858. struct drm_device *dev = crtc->dev;
  3859. struct intel_encoder *encoder;
  3860. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3861. if (encoder->type == INTEL_OUTPUT_DP ||
  3862. encoder->type == INTEL_OUTPUT_EDP)
  3863. return enc_to_dig_port(&encoder->base)->port;
  3864. }
  3865. return -1;
  3866. }
  3867. /*
  3868. * Enable PCH resources required for PCH ports:
  3869. * - PCH PLLs
  3870. * - FDI training & RX/TX
  3871. * - update transcoder timings
  3872. * - DP transcoding bits
  3873. * - transcoder
  3874. */
  3875. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3876. {
  3877. struct drm_device *dev = crtc->dev;
  3878. struct drm_i915_private *dev_priv = to_i915(dev);
  3879. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3880. int pipe = intel_crtc->pipe;
  3881. u32 temp;
  3882. assert_pch_transcoder_disabled(dev_priv, pipe);
  3883. if (IS_IVYBRIDGE(dev_priv))
  3884. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3885. /* Write the TU size bits before fdi link training, so that error
  3886. * detection works. */
  3887. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3888. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3889. /* For PCH output, training FDI link */
  3890. dev_priv->display.fdi_link_train(crtc);
  3891. /* We need to program the right clock selection before writing the pixel
  3892. * mutliplier into the DPLL. */
  3893. if (HAS_PCH_CPT(dev_priv)) {
  3894. u32 sel;
  3895. temp = I915_READ(PCH_DPLL_SEL);
  3896. temp |= TRANS_DPLL_ENABLE(pipe);
  3897. sel = TRANS_DPLLB_SEL(pipe);
  3898. if (intel_crtc->config->shared_dpll ==
  3899. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3900. temp |= sel;
  3901. else
  3902. temp &= ~sel;
  3903. I915_WRITE(PCH_DPLL_SEL, temp);
  3904. }
  3905. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3906. * transcoder, and we actually should do this to not upset any PCH
  3907. * transcoder that already use the clock when we share it.
  3908. *
  3909. * Note that enable_shared_dpll tries to do the right thing, but
  3910. * get_shared_dpll unconditionally resets the pll - we need that to have
  3911. * the right LVDS enable sequence. */
  3912. intel_enable_shared_dpll(intel_crtc);
  3913. /* set transcoder timing, panel must allow it */
  3914. assert_panel_unlocked(dev_priv, pipe);
  3915. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3916. intel_fdi_normal_train(crtc);
  3917. /* For PCH DP, enable TRANS_DP_CTL */
  3918. if (HAS_PCH_CPT(dev_priv) &&
  3919. intel_crtc_has_dp_encoder(intel_crtc->config)) {
  3920. const struct drm_display_mode *adjusted_mode =
  3921. &intel_crtc->config->base.adjusted_mode;
  3922. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3923. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3924. temp = I915_READ(reg);
  3925. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3926. TRANS_DP_SYNC_MASK |
  3927. TRANS_DP_BPC_MASK);
  3928. temp |= TRANS_DP_OUTPUT_ENABLE;
  3929. temp |= bpc << 9; /* same format but at 11:9 */
  3930. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3931. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3932. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3933. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3934. switch (intel_trans_dp_port_sel(crtc)) {
  3935. case PORT_B:
  3936. temp |= TRANS_DP_PORT_SEL_B;
  3937. break;
  3938. case PORT_C:
  3939. temp |= TRANS_DP_PORT_SEL_C;
  3940. break;
  3941. case PORT_D:
  3942. temp |= TRANS_DP_PORT_SEL_D;
  3943. break;
  3944. default:
  3945. BUG();
  3946. }
  3947. I915_WRITE(reg, temp);
  3948. }
  3949. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3950. }
  3951. static void lpt_pch_enable(struct drm_crtc *crtc)
  3952. {
  3953. struct drm_device *dev = crtc->dev;
  3954. struct drm_i915_private *dev_priv = to_i915(dev);
  3955. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3956. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3957. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3958. lpt_program_iclkip(crtc);
  3959. /* Set transcoder timing. */
  3960. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3961. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3962. }
  3963. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3964. {
  3965. struct drm_i915_private *dev_priv = to_i915(dev);
  3966. i915_reg_t dslreg = PIPEDSL(pipe);
  3967. u32 temp;
  3968. temp = I915_READ(dslreg);
  3969. udelay(500);
  3970. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3971. if (wait_for(I915_READ(dslreg) != temp, 5))
  3972. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3973. }
  3974. }
  3975. static int
  3976. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3977. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3978. int src_w, int src_h, int dst_w, int dst_h)
  3979. {
  3980. struct intel_crtc_scaler_state *scaler_state =
  3981. &crtc_state->scaler_state;
  3982. struct intel_crtc *intel_crtc =
  3983. to_intel_crtc(crtc_state->base.crtc);
  3984. int need_scaling;
  3985. need_scaling = intel_rotation_90_or_270(rotation) ?
  3986. (src_h != dst_w || src_w != dst_h):
  3987. (src_w != dst_w || src_h != dst_h);
  3988. /*
  3989. * if plane is being disabled or scaler is no more required or force detach
  3990. * - free scaler binded to this plane/crtc
  3991. * - in order to do this, update crtc->scaler_usage
  3992. *
  3993. * Here scaler state in crtc_state is set free so that
  3994. * scaler can be assigned to other user. Actual register
  3995. * update to free the scaler is done in plane/panel-fit programming.
  3996. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3997. */
  3998. if (force_detach || !need_scaling) {
  3999. if (*scaler_id >= 0) {
  4000. scaler_state->scaler_users &= ~(1 << scaler_user);
  4001. scaler_state->scalers[*scaler_id].in_use = 0;
  4002. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  4003. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  4004. intel_crtc->pipe, scaler_user, *scaler_id,
  4005. scaler_state->scaler_users);
  4006. *scaler_id = -1;
  4007. }
  4008. return 0;
  4009. }
  4010. /* range checks */
  4011. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  4012. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  4013. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  4014. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  4015. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  4016. "size is out of scaler range\n",
  4017. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  4018. return -EINVAL;
  4019. }
  4020. /* mark this plane as a scaler user in crtc_state */
  4021. scaler_state->scaler_users |= (1 << scaler_user);
  4022. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  4023. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  4024. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  4025. scaler_state->scaler_users);
  4026. return 0;
  4027. }
  4028. /**
  4029. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  4030. *
  4031. * @state: crtc's scaler state
  4032. *
  4033. * Return
  4034. * 0 - scaler_usage updated successfully
  4035. * error - requested scaling cannot be supported or other error condition
  4036. */
  4037. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  4038. {
  4039. struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
  4040. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  4041. DRM_DEBUG_KMS("Updating scaler for [CRTC:%d:%s] scaler_user index %u.%u\n",
  4042. intel_crtc->base.base.id, intel_crtc->base.name,
  4043. intel_crtc->pipe, SKL_CRTC_INDEX);
  4044. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  4045. &state->scaler_state.scaler_id, DRM_ROTATE_0,
  4046. state->pipe_src_w, state->pipe_src_h,
  4047. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  4048. }
  4049. /**
  4050. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  4051. *
  4052. * @state: crtc's scaler state
  4053. * @plane_state: atomic plane state to update
  4054. *
  4055. * Return
  4056. * 0 - scaler_usage updated successfully
  4057. * error - requested scaling cannot be supported or other error condition
  4058. */
  4059. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  4060. struct intel_plane_state *plane_state)
  4061. {
  4062. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  4063. struct intel_plane *intel_plane =
  4064. to_intel_plane(plane_state->base.plane);
  4065. struct drm_framebuffer *fb = plane_state->base.fb;
  4066. int ret;
  4067. bool force_detach = !fb || !plane_state->base.visible;
  4068. DRM_DEBUG_KMS("Updating scaler for [PLANE:%d:%s] scaler_user index %u.%u\n",
  4069. intel_plane->base.base.id, intel_plane->base.name,
  4070. intel_crtc->pipe, drm_plane_index(&intel_plane->base));
  4071. ret = skl_update_scaler(crtc_state, force_detach,
  4072. drm_plane_index(&intel_plane->base),
  4073. &plane_state->scaler_id,
  4074. plane_state->base.rotation,
  4075. drm_rect_width(&plane_state->base.src) >> 16,
  4076. drm_rect_height(&plane_state->base.src) >> 16,
  4077. drm_rect_width(&plane_state->base.dst),
  4078. drm_rect_height(&plane_state->base.dst));
  4079. if (ret || plane_state->scaler_id < 0)
  4080. return ret;
  4081. /* check colorkey */
  4082. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  4083. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  4084. intel_plane->base.base.id,
  4085. intel_plane->base.name);
  4086. return -EINVAL;
  4087. }
  4088. /* Check src format */
  4089. switch (fb->pixel_format) {
  4090. case DRM_FORMAT_RGB565:
  4091. case DRM_FORMAT_XBGR8888:
  4092. case DRM_FORMAT_XRGB8888:
  4093. case DRM_FORMAT_ABGR8888:
  4094. case DRM_FORMAT_ARGB8888:
  4095. case DRM_FORMAT_XRGB2101010:
  4096. case DRM_FORMAT_XBGR2101010:
  4097. case DRM_FORMAT_YUYV:
  4098. case DRM_FORMAT_YVYU:
  4099. case DRM_FORMAT_UYVY:
  4100. case DRM_FORMAT_VYUY:
  4101. break;
  4102. default:
  4103. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  4104. intel_plane->base.base.id, intel_plane->base.name,
  4105. fb->base.id, fb->pixel_format);
  4106. return -EINVAL;
  4107. }
  4108. return 0;
  4109. }
  4110. static void skylake_scaler_disable(struct intel_crtc *crtc)
  4111. {
  4112. int i;
  4113. for (i = 0; i < crtc->num_scalers; i++)
  4114. skl_detach_scaler(crtc, i);
  4115. }
  4116. static void skylake_pfit_enable(struct intel_crtc *crtc)
  4117. {
  4118. struct drm_device *dev = crtc->base.dev;
  4119. struct drm_i915_private *dev_priv = to_i915(dev);
  4120. int pipe = crtc->pipe;
  4121. struct intel_crtc_scaler_state *scaler_state =
  4122. &crtc->config->scaler_state;
  4123. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  4124. if (crtc->config->pch_pfit.enabled) {
  4125. int id;
  4126. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  4127. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  4128. return;
  4129. }
  4130. id = scaler_state->scaler_id;
  4131. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  4132. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  4133. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  4134. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  4135. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  4136. }
  4137. }
  4138. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  4139. {
  4140. struct drm_device *dev = crtc->base.dev;
  4141. struct drm_i915_private *dev_priv = to_i915(dev);
  4142. int pipe = crtc->pipe;
  4143. if (crtc->config->pch_pfit.enabled) {
  4144. /* Force use of hard-coded filter coefficients
  4145. * as some pre-programmed values are broken,
  4146. * e.g. x201.
  4147. */
  4148. if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
  4149. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  4150. PF_PIPE_SEL_IVB(pipe));
  4151. else
  4152. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  4153. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  4154. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  4155. }
  4156. }
  4157. void hsw_enable_ips(struct intel_crtc *crtc)
  4158. {
  4159. struct drm_device *dev = crtc->base.dev;
  4160. struct drm_i915_private *dev_priv = to_i915(dev);
  4161. if (!crtc->config->ips_enabled)
  4162. return;
  4163. /*
  4164. * We can only enable IPS after we enable a plane and wait for a vblank
  4165. * This function is called from post_plane_update, which is run after
  4166. * a vblank wait.
  4167. */
  4168. assert_plane_enabled(dev_priv, crtc->plane);
  4169. if (IS_BROADWELL(dev_priv)) {
  4170. mutex_lock(&dev_priv->rps.hw_lock);
  4171. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  4172. mutex_unlock(&dev_priv->rps.hw_lock);
  4173. /* Quoting Art Runyan: "its not safe to expect any particular
  4174. * value in IPS_CTL bit 31 after enabling IPS through the
  4175. * mailbox." Moreover, the mailbox may return a bogus state,
  4176. * so we need to just enable it and continue on.
  4177. */
  4178. } else {
  4179. I915_WRITE(IPS_CTL, IPS_ENABLE);
  4180. /* The bit only becomes 1 in the next vblank, so this wait here
  4181. * is essentially intel_wait_for_vblank. If we don't have this
  4182. * and don't wait for vblanks until the end of crtc_enable, then
  4183. * the HW state readout code will complain that the expected
  4184. * IPS_CTL value is not the one we read. */
  4185. if (intel_wait_for_register(dev_priv,
  4186. IPS_CTL, IPS_ENABLE, IPS_ENABLE,
  4187. 50))
  4188. DRM_ERROR("Timed out waiting for IPS enable\n");
  4189. }
  4190. }
  4191. void hsw_disable_ips(struct intel_crtc *crtc)
  4192. {
  4193. struct drm_device *dev = crtc->base.dev;
  4194. struct drm_i915_private *dev_priv = to_i915(dev);
  4195. if (!crtc->config->ips_enabled)
  4196. return;
  4197. assert_plane_enabled(dev_priv, crtc->plane);
  4198. if (IS_BROADWELL(dev_priv)) {
  4199. mutex_lock(&dev_priv->rps.hw_lock);
  4200. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  4201. mutex_unlock(&dev_priv->rps.hw_lock);
  4202. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  4203. if (intel_wait_for_register(dev_priv,
  4204. IPS_CTL, IPS_ENABLE, 0,
  4205. 42))
  4206. DRM_ERROR("Timed out waiting for IPS disable\n");
  4207. } else {
  4208. I915_WRITE(IPS_CTL, 0);
  4209. POSTING_READ(IPS_CTL);
  4210. }
  4211. /* We need to wait for a vblank before we can disable the plane. */
  4212. intel_wait_for_vblank(dev, crtc->pipe);
  4213. }
  4214. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  4215. {
  4216. if (intel_crtc->overlay) {
  4217. struct drm_device *dev = intel_crtc->base.dev;
  4218. struct drm_i915_private *dev_priv = to_i915(dev);
  4219. mutex_lock(&dev->struct_mutex);
  4220. dev_priv->mm.interruptible = false;
  4221. (void) intel_overlay_switch_off(intel_crtc->overlay);
  4222. dev_priv->mm.interruptible = true;
  4223. mutex_unlock(&dev->struct_mutex);
  4224. }
  4225. /* Let userspace switch the overlay on again. In most cases userspace
  4226. * has to recompute where to put it anyway.
  4227. */
  4228. }
  4229. /**
  4230. * intel_post_enable_primary - Perform operations after enabling primary plane
  4231. * @crtc: the CRTC whose primary plane was just enabled
  4232. *
  4233. * Performs potentially sleeping operations that must be done after the primary
  4234. * plane is enabled, such as updating FBC and IPS. Note that this may be
  4235. * called due to an explicit primary plane update, or due to an implicit
  4236. * re-enable that is caused when a sprite plane is updated to no longer
  4237. * completely hide the primary plane.
  4238. */
  4239. static void
  4240. intel_post_enable_primary(struct drm_crtc *crtc)
  4241. {
  4242. struct drm_device *dev = crtc->dev;
  4243. struct drm_i915_private *dev_priv = to_i915(dev);
  4244. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4245. int pipe = intel_crtc->pipe;
  4246. /*
  4247. * FIXME IPS should be fine as long as one plane is
  4248. * enabled, but in practice it seems to have problems
  4249. * when going from primary only to sprite only and vice
  4250. * versa.
  4251. */
  4252. hsw_enable_ips(intel_crtc);
  4253. /*
  4254. * Gen2 reports pipe underruns whenever all planes are disabled.
  4255. * So don't enable underrun reporting before at least some planes
  4256. * are enabled.
  4257. * FIXME: Need to fix the logic to work when we turn off all planes
  4258. * but leave the pipe running.
  4259. */
  4260. if (IS_GEN2(dev_priv))
  4261. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4262. /* Underruns don't always raise interrupts, so check manually. */
  4263. intel_check_cpu_fifo_underruns(dev_priv);
  4264. intel_check_pch_fifo_underruns(dev_priv);
  4265. }
  4266. /* FIXME move all this to pre_plane_update() with proper state tracking */
  4267. static void
  4268. intel_pre_disable_primary(struct drm_crtc *crtc)
  4269. {
  4270. struct drm_device *dev = crtc->dev;
  4271. struct drm_i915_private *dev_priv = to_i915(dev);
  4272. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4273. int pipe = intel_crtc->pipe;
  4274. /*
  4275. * Gen2 reports pipe underruns whenever all planes are disabled.
  4276. * So diasble underrun reporting before all the planes get disabled.
  4277. * FIXME: Need to fix the logic to work when we turn off all planes
  4278. * but leave the pipe running.
  4279. */
  4280. if (IS_GEN2(dev_priv))
  4281. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4282. /*
  4283. * FIXME IPS should be fine as long as one plane is
  4284. * enabled, but in practice it seems to have problems
  4285. * when going from primary only to sprite only and vice
  4286. * versa.
  4287. */
  4288. hsw_disable_ips(intel_crtc);
  4289. }
  4290. /* FIXME get rid of this and use pre_plane_update */
  4291. static void
  4292. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  4293. {
  4294. struct drm_device *dev = crtc->dev;
  4295. struct drm_i915_private *dev_priv = to_i915(dev);
  4296. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4297. int pipe = intel_crtc->pipe;
  4298. intel_pre_disable_primary(crtc);
  4299. /*
  4300. * Vblank time updates from the shadow to live plane control register
  4301. * are blocked if the memory self-refresh mode is active at that
  4302. * moment. So to make sure the plane gets truly disabled, disable
  4303. * first the self-refresh mode. The self-refresh enable bit in turn
  4304. * will be checked/applied by the HW only at the next frame start
  4305. * event which is after the vblank start event, so we need to have a
  4306. * wait-for-vblank between disabling the plane and the pipe.
  4307. */
  4308. if (HAS_GMCH_DISPLAY(dev_priv)) {
  4309. intel_set_memory_cxsr(dev_priv, false);
  4310. dev_priv->wm.vlv.cxsr = false;
  4311. intel_wait_for_vblank(dev, pipe);
  4312. }
  4313. }
  4314. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  4315. {
  4316. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4317. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4318. struct intel_crtc_state *pipe_config =
  4319. to_intel_crtc_state(crtc->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. intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
  4324. crtc->wm.cxsr_allowed = true;
  4325. if (pipe_config->update_wm_post && pipe_config->base.active)
  4326. intel_update_watermarks(&crtc->base);
  4327. if (old_pri_state) {
  4328. struct intel_plane_state *primary_state =
  4329. to_intel_plane_state(primary->state);
  4330. struct intel_plane_state *old_primary_state =
  4331. to_intel_plane_state(old_pri_state);
  4332. intel_fbc_post_update(crtc);
  4333. if (primary_state->base.visible &&
  4334. (needs_modeset(&pipe_config->base) ||
  4335. !old_primary_state->base.visible))
  4336. intel_post_enable_primary(&crtc->base);
  4337. }
  4338. }
  4339. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
  4340. {
  4341. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4342. struct drm_device *dev = crtc->base.dev;
  4343. struct drm_i915_private *dev_priv = to_i915(dev);
  4344. struct intel_crtc_state *pipe_config =
  4345. to_intel_crtc_state(crtc->base.state);
  4346. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4347. struct drm_plane *primary = crtc->base.primary;
  4348. struct drm_plane_state *old_pri_state =
  4349. drm_atomic_get_existing_plane_state(old_state, primary);
  4350. bool modeset = needs_modeset(&pipe_config->base);
  4351. if (old_pri_state) {
  4352. struct intel_plane_state *primary_state =
  4353. to_intel_plane_state(primary->state);
  4354. struct intel_plane_state *old_primary_state =
  4355. to_intel_plane_state(old_pri_state);
  4356. intel_fbc_pre_update(crtc, pipe_config, primary_state);
  4357. if (old_primary_state->base.visible &&
  4358. (modeset || !primary_state->base.visible))
  4359. intel_pre_disable_primary(&crtc->base);
  4360. }
  4361. if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev_priv)) {
  4362. crtc->wm.cxsr_allowed = false;
  4363. /*
  4364. * Vblank time updates from the shadow to live plane control register
  4365. * are blocked if the memory self-refresh mode is active at that
  4366. * moment. So to make sure the plane gets truly disabled, disable
  4367. * first the self-refresh mode. The self-refresh enable bit in turn
  4368. * will be checked/applied by the HW only at the next frame start
  4369. * event which is after the vblank start event, so we need to have a
  4370. * wait-for-vblank between disabling the plane and the pipe.
  4371. */
  4372. if (old_crtc_state->base.active) {
  4373. intel_set_memory_cxsr(dev_priv, false);
  4374. dev_priv->wm.vlv.cxsr = false;
  4375. intel_wait_for_vblank(dev, crtc->pipe);
  4376. }
  4377. }
  4378. /*
  4379. * IVB workaround: must disable low power watermarks for at least
  4380. * one frame before enabling scaling. LP watermarks can be re-enabled
  4381. * when scaling is disabled.
  4382. *
  4383. * WaCxSRDisabledForSpriteScaling:ivb
  4384. */
  4385. if (pipe_config->disable_lp_wm) {
  4386. ilk_disable_lp_wm(dev);
  4387. intel_wait_for_vblank(dev, crtc->pipe);
  4388. }
  4389. /*
  4390. * If we're doing a modeset, we're done. No need to do any pre-vblank
  4391. * watermark programming here.
  4392. */
  4393. if (needs_modeset(&pipe_config->base))
  4394. return;
  4395. /*
  4396. * For platforms that support atomic watermarks, program the
  4397. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  4398. * will be the intermediate values that are safe for both pre- and
  4399. * post- vblank; when vblank happens, the 'active' values will be set
  4400. * to the final 'target' values and we'll do this again to get the
  4401. * optimal watermarks. For gen9+ platforms, the values we program here
  4402. * will be the final target values which will get automatically latched
  4403. * at vblank time; no further programming will be necessary.
  4404. *
  4405. * If a platform hasn't been transitioned to atomic watermarks yet,
  4406. * we'll continue to update watermarks the old way, if flags tell
  4407. * us to.
  4408. */
  4409. if (dev_priv->display.initial_watermarks != NULL)
  4410. dev_priv->display.initial_watermarks(pipe_config);
  4411. else if (pipe_config->update_wm_pre)
  4412. intel_update_watermarks(&crtc->base);
  4413. }
  4414. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4415. {
  4416. struct drm_device *dev = crtc->dev;
  4417. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4418. struct drm_plane *p;
  4419. int pipe = intel_crtc->pipe;
  4420. intel_crtc_dpms_overlay_disable(intel_crtc);
  4421. drm_for_each_plane_mask(p, dev, plane_mask)
  4422. to_intel_plane(p)->disable_plane(p, crtc);
  4423. /*
  4424. * FIXME: Once we grow proper nuclear flip support out of this we need
  4425. * to compute the mask of flip planes precisely. For the time being
  4426. * consider this a flip to a NULL plane.
  4427. */
  4428. intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4429. }
  4430. static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
  4431. struct intel_crtc_state *crtc_state,
  4432. struct drm_atomic_state *old_state)
  4433. {
  4434. struct drm_connector_state *old_conn_state;
  4435. struct drm_connector *conn;
  4436. int i;
  4437. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4438. struct drm_connector_state *conn_state = conn->state;
  4439. struct intel_encoder *encoder =
  4440. to_intel_encoder(conn_state->best_encoder);
  4441. if (conn_state->crtc != crtc)
  4442. continue;
  4443. if (encoder->pre_pll_enable)
  4444. encoder->pre_pll_enable(encoder, crtc_state, conn_state);
  4445. }
  4446. }
  4447. static void intel_encoders_pre_enable(struct drm_crtc *crtc,
  4448. struct intel_crtc_state *crtc_state,
  4449. struct drm_atomic_state *old_state)
  4450. {
  4451. struct drm_connector_state *old_conn_state;
  4452. struct drm_connector *conn;
  4453. int i;
  4454. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4455. struct drm_connector_state *conn_state = conn->state;
  4456. struct intel_encoder *encoder =
  4457. to_intel_encoder(conn_state->best_encoder);
  4458. if (conn_state->crtc != crtc)
  4459. continue;
  4460. if (encoder->pre_enable)
  4461. encoder->pre_enable(encoder, crtc_state, conn_state);
  4462. }
  4463. }
  4464. static void intel_encoders_enable(struct drm_crtc *crtc,
  4465. struct intel_crtc_state *crtc_state,
  4466. struct drm_atomic_state *old_state)
  4467. {
  4468. struct drm_connector_state *old_conn_state;
  4469. struct drm_connector *conn;
  4470. int i;
  4471. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4472. struct drm_connector_state *conn_state = conn->state;
  4473. struct intel_encoder *encoder =
  4474. to_intel_encoder(conn_state->best_encoder);
  4475. if (conn_state->crtc != crtc)
  4476. continue;
  4477. encoder->enable(encoder, crtc_state, conn_state);
  4478. intel_opregion_notify_encoder(encoder, true);
  4479. }
  4480. }
  4481. static void intel_encoders_disable(struct drm_crtc *crtc,
  4482. struct intel_crtc_state *old_crtc_state,
  4483. struct drm_atomic_state *old_state)
  4484. {
  4485. struct drm_connector_state *old_conn_state;
  4486. struct drm_connector *conn;
  4487. int i;
  4488. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4489. struct intel_encoder *encoder =
  4490. to_intel_encoder(old_conn_state->best_encoder);
  4491. if (old_conn_state->crtc != crtc)
  4492. continue;
  4493. intel_opregion_notify_encoder(encoder, false);
  4494. encoder->disable(encoder, old_crtc_state, old_conn_state);
  4495. }
  4496. }
  4497. static void intel_encoders_post_disable(struct drm_crtc *crtc,
  4498. struct intel_crtc_state *old_crtc_state,
  4499. struct drm_atomic_state *old_state)
  4500. {
  4501. struct drm_connector_state *old_conn_state;
  4502. struct drm_connector *conn;
  4503. int i;
  4504. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4505. struct intel_encoder *encoder =
  4506. to_intel_encoder(old_conn_state->best_encoder);
  4507. if (old_conn_state->crtc != crtc)
  4508. continue;
  4509. if (encoder->post_disable)
  4510. encoder->post_disable(encoder, old_crtc_state, old_conn_state);
  4511. }
  4512. }
  4513. static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
  4514. struct intel_crtc_state *old_crtc_state,
  4515. struct drm_atomic_state *old_state)
  4516. {
  4517. struct drm_connector_state *old_conn_state;
  4518. struct drm_connector *conn;
  4519. int i;
  4520. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4521. struct intel_encoder *encoder =
  4522. to_intel_encoder(old_conn_state->best_encoder);
  4523. if (old_conn_state->crtc != crtc)
  4524. continue;
  4525. if (encoder->post_pll_disable)
  4526. encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
  4527. }
  4528. }
  4529. static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
  4530. struct drm_atomic_state *old_state)
  4531. {
  4532. struct drm_crtc *crtc = pipe_config->base.crtc;
  4533. struct drm_device *dev = crtc->dev;
  4534. struct drm_i915_private *dev_priv = to_i915(dev);
  4535. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4536. int pipe = intel_crtc->pipe;
  4537. if (WARN_ON(intel_crtc->active))
  4538. return;
  4539. /*
  4540. * Sometimes spurious CPU pipe underruns happen during FDI
  4541. * training, at least with VGA+HDMI cloning. Suppress them.
  4542. *
  4543. * On ILK we get an occasional spurious CPU pipe underruns
  4544. * between eDP port A enable and vdd enable. Also PCH port
  4545. * enable seems to result in the occasional CPU pipe underrun.
  4546. *
  4547. * Spurious PCH underruns also occur during PCH enabling.
  4548. */
  4549. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4550. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4551. if (intel_crtc->config->has_pch_encoder)
  4552. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4553. if (intel_crtc->config->has_pch_encoder)
  4554. intel_prepare_shared_dpll(intel_crtc);
  4555. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4556. intel_dp_set_m_n(intel_crtc, M1_N1);
  4557. intel_set_pipe_timings(intel_crtc);
  4558. intel_set_pipe_src_size(intel_crtc);
  4559. if (intel_crtc->config->has_pch_encoder) {
  4560. intel_cpu_transcoder_set_m_n(intel_crtc,
  4561. &intel_crtc->config->fdi_m_n, NULL);
  4562. }
  4563. ironlake_set_pipeconf(crtc);
  4564. intel_crtc->active = true;
  4565. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4566. if (intel_crtc->config->has_pch_encoder) {
  4567. /* Note: FDI PLL enabling _must_ be done before we enable the
  4568. * cpu pipes, hence this is separate from all the other fdi/pch
  4569. * enabling. */
  4570. ironlake_fdi_pll_enable(intel_crtc);
  4571. } else {
  4572. assert_fdi_tx_disabled(dev_priv, pipe);
  4573. assert_fdi_rx_disabled(dev_priv, pipe);
  4574. }
  4575. ironlake_pfit_enable(intel_crtc);
  4576. /*
  4577. * On ILK+ LUT must be loaded before the pipe is running but with
  4578. * clocks enabled
  4579. */
  4580. intel_color_load_luts(&pipe_config->base);
  4581. if (dev_priv->display.initial_watermarks != NULL)
  4582. dev_priv->display.initial_watermarks(intel_crtc->config);
  4583. intel_enable_pipe(intel_crtc);
  4584. if (intel_crtc->config->has_pch_encoder)
  4585. ironlake_pch_enable(crtc);
  4586. assert_vblank_disabled(crtc);
  4587. drm_crtc_vblank_on(crtc);
  4588. intel_encoders_enable(crtc, pipe_config, old_state);
  4589. if (HAS_PCH_CPT(dev_priv))
  4590. cpt_verify_modeset(dev, intel_crtc->pipe);
  4591. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4592. if (intel_crtc->config->has_pch_encoder)
  4593. intel_wait_for_vblank(dev, pipe);
  4594. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4595. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4596. }
  4597. /* IPS only exists on ULT machines and is tied to pipe A. */
  4598. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4599. {
  4600. return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
  4601. }
  4602. static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
  4603. struct drm_atomic_state *old_state)
  4604. {
  4605. struct drm_crtc *crtc = pipe_config->base.crtc;
  4606. struct drm_device *dev = crtc->dev;
  4607. struct drm_i915_private *dev_priv = to_i915(dev);
  4608. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4609. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4610. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4611. if (WARN_ON(intel_crtc->active))
  4612. return;
  4613. if (intel_crtc->config->has_pch_encoder)
  4614. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4615. false);
  4616. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4617. if (intel_crtc->config->shared_dpll)
  4618. intel_enable_shared_dpll(intel_crtc);
  4619. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4620. intel_dp_set_m_n(intel_crtc, M1_N1);
  4621. if (!transcoder_is_dsi(cpu_transcoder))
  4622. intel_set_pipe_timings(intel_crtc);
  4623. intel_set_pipe_src_size(intel_crtc);
  4624. if (cpu_transcoder != TRANSCODER_EDP &&
  4625. !transcoder_is_dsi(cpu_transcoder)) {
  4626. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4627. intel_crtc->config->pixel_multiplier - 1);
  4628. }
  4629. if (intel_crtc->config->has_pch_encoder) {
  4630. intel_cpu_transcoder_set_m_n(intel_crtc,
  4631. &intel_crtc->config->fdi_m_n, NULL);
  4632. }
  4633. if (!transcoder_is_dsi(cpu_transcoder))
  4634. haswell_set_pipeconf(crtc);
  4635. haswell_set_pipemisc(crtc);
  4636. intel_color_set_csc(&pipe_config->base);
  4637. intel_crtc->active = true;
  4638. if (intel_crtc->config->has_pch_encoder)
  4639. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4640. else
  4641. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4642. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4643. if (intel_crtc->config->has_pch_encoder)
  4644. dev_priv->display.fdi_link_train(crtc);
  4645. if (!transcoder_is_dsi(cpu_transcoder))
  4646. intel_ddi_enable_pipe_clock(intel_crtc);
  4647. if (INTEL_INFO(dev)->gen >= 9)
  4648. skylake_pfit_enable(intel_crtc);
  4649. else
  4650. ironlake_pfit_enable(intel_crtc);
  4651. /*
  4652. * On ILK+ LUT must be loaded before the pipe is running but with
  4653. * clocks enabled
  4654. */
  4655. intel_color_load_luts(&pipe_config->base);
  4656. intel_ddi_set_pipe_settings(crtc);
  4657. if (!transcoder_is_dsi(cpu_transcoder))
  4658. intel_ddi_enable_transcoder_func(crtc);
  4659. if (dev_priv->display.initial_watermarks != NULL)
  4660. dev_priv->display.initial_watermarks(pipe_config);
  4661. else
  4662. intel_update_watermarks(crtc);
  4663. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4664. if (!transcoder_is_dsi(cpu_transcoder))
  4665. intel_enable_pipe(intel_crtc);
  4666. if (intel_crtc->config->has_pch_encoder)
  4667. lpt_pch_enable(crtc);
  4668. if (intel_crtc->config->dp_encoder_is_mst)
  4669. intel_ddi_set_vc_payload_alloc(crtc, true);
  4670. assert_vblank_disabled(crtc);
  4671. drm_crtc_vblank_on(crtc);
  4672. intel_encoders_enable(crtc, pipe_config, old_state);
  4673. if (intel_crtc->config->has_pch_encoder) {
  4674. intel_wait_for_vblank(dev, pipe);
  4675. intel_wait_for_vblank(dev, pipe);
  4676. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4677. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4678. true);
  4679. }
  4680. /* If we change the relative order between pipe/planes enabling, we need
  4681. * to change the workaround. */
  4682. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4683. if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
  4684. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4685. intel_wait_for_vblank(dev, hsw_workaround_pipe);
  4686. }
  4687. }
  4688. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4689. {
  4690. struct drm_device *dev = crtc->base.dev;
  4691. struct drm_i915_private *dev_priv = to_i915(dev);
  4692. int pipe = crtc->pipe;
  4693. /* To avoid upsetting the power well on haswell only disable the pfit if
  4694. * it's in use. The hw state code will make sure we get this right. */
  4695. if (force || crtc->config->pch_pfit.enabled) {
  4696. I915_WRITE(PF_CTL(pipe), 0);
  4697. I915_WRITE(PF_WIN_POS(pipe), 0);
  4698. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4699. }
  4700. }
  4701. static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4702. struct drm_atomic_state *old_state)
  4703. {
  4704. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4705. struct drm_device *dev = crtc->dev;
  4706. struct drm_i915_private *dev_priv = to_i915(dev);
  4707. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4708. int pipe = intel_crtc->pipe;
  4709. /*
  4710. * Sometimes spurious CPU pipe underruns happen when the
  4711. * pipe is already disabled, but FDI RX/TX is still enabled.
  4712. * Happens at least with VGA+HDMI cloning. Suppress them.
  4713. */
  4714. if (intel_crtc->config->has_pch_encoder) {
  4715. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4716. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4717. }
  4718. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4719. drm_crtc_vblank_off(crtc);
  4720. assert_vblank_disabled(crtc);
  4721. intel_disable_pipe(intel_crtc);
  4722. ironlake_pfit_disable(intel_crtc, false);
  4723. if (intel_crtc->config->has_pch_encoder)
  4724. ironlake_fdi_disable(crtc);
  4725. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4726. if (intel_crtc->config->has_pch_encoder) {
  4727. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4728. if (HAS_PCH_CPT(dev_priv)) {
  4729. i915_reg_t reg;
  4730. u32 temp;
  4731. /* disable TRANS_DP_CTL */
  4732. reg = TRANS_DP_CTL(pipe);
  4733. temp = I915_READ(reg);
  4734. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4735. TRANS_DP_PORT_SEL_MASK);
  4736. temp |= TRANS_DP_PORT_SEL_NONE;
  4737. I915_WRITE(reg, temp);
  4738. /* disable DPLL_SEL */
  4739. temp = I915_READ(PCH_DPLL_SEL);
  4740. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4741. I915_WRITE(PCH_DPLL_SEL, temp);
  4742. }
  4743. ironlake_fdi_pll_disable(intel_crtc);
  4744. }
  4745. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4746. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4747. }
  4748. static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4749. struct drm_atomic_state *old_state)
  4750. {
  4751. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4752. struct drm_device *dev = crtc->dev;
  4753. struct drm_i915_private *dev_priv = to_i915(dev);
  4754. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4755. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4756. if (intel_crtc->config->has_pch_encoder)
  4757. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4758. false);
  4759. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4760. drm_crtc_vblank_off(crtc);
  4761. assert_vblank_disabled(crtc);
  4762. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4763. if (!transcoder_is_dsi(cpu_transcoder))
  4764. intel_disable_pipe(intel_crtc);
  4765. if (intel_crtc->config->dp_encoder_is_mst)
  4766. intel_ddi_set_vc_payload_alloc(crtc, false);
  4767. if (!transcoder_is_dsi(cpu_transcoder))
  4768. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4769. if (INTEL_INFO(dev)->gen >= 9)
  4770. skylake_scaler_disable(intel_crtc);
  4771. else
  4772. ironlake_pfit_disable(intel_crtc, false);
  4773. if (!transcoder_is_dsi(cpu_transcoder))
  4774. intel_ddi_disable_pipe_clock(intel_crtc);
  4775. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4776. if (old_crtc_state->has_pch_encoder)
  4777. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4778. true);
  4779. }
  4780. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4781. {
  4782. struct drm_device *dev = crtc->base.dev;
  4783. struct drm_i915_private *dev_priv = to_i915(dev);
  4784. struct intel_crtc_state *pipe_config = crtc->config;
  4785. if (!pipe_config->gmch_pfit.control)
  4786. return;
  4787. /*
  4788. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4789. * according to register description and PRM.
  4790. */
  4791. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4792. assert_pipe_disabled(dev_priv, crtc->pipe);
  4793. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4794. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4795. /* Border color in case we don't scale up to the full screen. Black by
  4796. * default, change to something else for debugging. */
  4797. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4798. }
  4799. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4800. {
  4801. switch (port) {
  4802. case PORT_A:
  4803. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4804. case PORT_B:
  4805. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4806. case PORT_C:
  4807. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4808. case PORT_D:
  4809. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4810. case PORT_E:
  4811. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4812. default:
  4813. MISSING_CASE(port);
  4814. return POWER_DOMAIN_PORT_OTHER;
  4815. }
  4816. }
  4817. static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
  4818. {
  4819. switch (port) {
  4820. case PORT_A:
  4821. return POWER_DOMAIN_AUX_A;
  4822. case PORT_B:
  4823. return POWER_DOMAIN_AUX_B;
  4824. case PORT_C:
  4825. return POWER_DOMAIN_AUX_C;
  4826. case PORT_D:
  4827. return POWER_DOMAIN_AUX_D;
  4828. case PORT_E:
  4829. /* FIXME: Check VBT for actual wiring of PORT E */
  4830. return POWER_DOMAIN_AUX_D;
  4831. default:
  4832. MISSING_CASE(port);
  4833. return POWER_DOMAIN_AUX_A;
  4834. }
  4835. }
  4836. enum intel_display_power_domain
  4837. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4838. {
  4839. struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
  4840. struct intel_digital_port *intel_dig_port;
  4841. switch (intel_encoder->type) {
  4842. case INTEL_OUTPUT_UNKNOWN:
  4843. /* Only DDI platforms should ever use this output type */
  4844. WARN_ON_ONCE(!HAS_DDI(dev_priv));
  4845. case INTEL_OUTPUT_DP:
  4846. case INTEL_OUTPUT_HDMI:
  4847. case INTEL_OUTPUT_EDP:
  4848. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4849. return port_to_power_domain(intel_dig_port->port);
  4850. case INTEL_OUTPUT_DP_MST:
  4851. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4852. return port_to_power_domain(intel_dig_port->port);
  4853. case INTEL_OUTPUT_ANALOG:
  4854. return POWER_DOMAIN_PORT_CRT;
  4855. case INTEL_OUTPUT_DSI:
  4856. return POWER_DOMAIN_PORT_DSI;
  4857. default:
  4858. return POWER_DOMAIN_PORT_OTHER;
  4859. }
  4860. }
  4861. enum intel_display_power_domain
  4862. intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
  4863. {
  4864. struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
  4865. struct intel_digital_port *intel_dig_port;
  4866. switch (intel_encoder->type) {
  4867. case INTEL_OUTPUT_UNKNOWN:
  4868. case INTEL_OUTPUT_HDMI:
  4869. /*
  4870. * Only DDI platforms should ever use these output types.
  4871. * We can get here after the HDMI detect code has already set
  4872. * the type of the shared encoder. Since we can't be sure
  4873. * what's the status of the given connectors, play safe and
  4874. * run the DP detection too.
  4875. */
  4876. WARN_ON_ONCE(!HAS_DDI(dev_priv));
  4877. case INTEL_OUTPUT_DP:
  4878. case INTEL_OUTPUT_EDP:
  4879. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4880. return port_to_aux_power_domain(intel_dig_port->port);
  4881. case INTEL_OUTPUT_DP_MST:
  4882. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4883. return port_to_aux_power_domain(intel_dig_port->port);
  4884. default:
  4885. MISSING_CASE(intel_encoder->type);
  4886. return POWER_DOMAIN_AUX_A;
  4887. }
  4888. }
  4889. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
  4890. struct intel_crtc_state *crtc_state)
  4891. {
  4892. struct drm_device *dev = crtc->dev;
  4893. struct drm_encoder *encoder;
  4894. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4895. enum pipe pipe = intel_crtc->pipe;
  4896. unsigned long mask;
  4897. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4898. if (!crtc_state->base.active)
  4899. return 0;
  4900. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4901. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4902. if (crtc_state->pch_pfit.enabled ||
  4903. crtc_state->pch_pfit.force_thru)
  4904. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4905. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4906. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4907. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4908. }
  4909. if (crtc_state->shared_dpll)
  4910. mask |= BIT(POWER_DOMAIN_PLLS);
  4911. return mask;
  4912. }
  4913. static unsigned long
  4914. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4915. struct intel_crtc_state *crtc_state)
  4916. {
  4917. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4918. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4919. enum intel_display_power_domain domain;
  4920. unsigned long domains, new_domains, old_domains;
  4921. old_domains = intel_crtc->enabled_power_domains;
  4922. intel_crtc->enabled_power_domains = new_domains =
  4923. get_crtc_power_domains(crtc, crtc_state);
  4924. domains = new_domains & ~old_domains;
  4925. for_each_power_domain(domain, domains)
  4926. intel_display_power_get(dev_priv, domain);
  4927. return old_domains & ~new_domains;
  4928. }
  4929. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4930. unsigned long domains)
  4931. {
  4932. enum intel_display_power_domain domain;
  4933. for_each_power_domain(domain, domains)
  4934. intel_display_power_put(dev_priv, domain);
  4935. }
  4936. static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
  4937. {
  4938. int max_cdclk_freq = dev_priv->max_cdclk_freq;
  4939. if (INTEL_INFO(dev_priv)->gen >= 9 ||
  4940. IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  4941. return max_cdclk_freq;
  4942. else if (IS_CHERRYVIEW(dev_priv))
  4943. return max_cdclk_freq*95/100;
  4944. else if (INTEL_INFO(dev_priv)->gen < 4)
  4945. return 2*max_cdclk_freq*90/100;
  4946. else
  4947. return max_cdclk_freq*90/100;
  4948. }
  4949. static int skl_calc_cdclk(int max_pixclk, int vco);
  4950. static void intel_update_max_cdclk(struct drm_device *dev)
  4951. {
  4952. struct drm_i915_private *dev_priv = to_i915(dev);
  4953. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
  4954. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4955. int max_cdclk, vco;
  4956. vco = dev_priv->skl_preferred_vco_freq;
  4957. WARN_ON(vco != 8100000 && vco != 8640000);
  4958. /*
  4959. * Use the lower (vco 8640) cdclk values as a
  4960. * first guess. skl_calc_cdclk() will correct it
  4961. * if the preferred vco is 8100 instead.
  4962. */
  4963. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4964. max_cdclk = 617143;
  4965. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4966. max_cdclk = 540000;
  4967. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4968. max_cdclk = 432000;
  4969. else
  4970. max_cdclk = 308571;
  4971. dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
  4972. } else if (IS_BROXTON(dev_priv)) {
  4973. dev_priv->max_cdclk_freq = 624000;
  4974. } else if (IS_BROADWELL(dev_priv)) {
  4975. /*
  4976. * FIXME with extra cooling we can allow
  4977. * 540 MHz for ULX and 675 Mhz for ULT.
  4978. * How can we know if extra cooling is
  4979. * available? PCI ID, VTB, something else?
  4980. */
  4981. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4982. dev_priv->max_cdclk_freq = 450000;
  4983. else if (IS_BDW_ULX(dev_priv))
  4984. dev_priv->max_cdclk_freq = 450000;
  4985. else if (IS_BDW_ULT(dev_priv))
  4986. dev_priv->max_cdclk_freq = 540000;
  4987. else
  4988. dev_priv->max_cdclk_freq = 675000;
  4989. } else if (IS_CHERRYVIEW(dev_priv)) {
  4990. dev_priv->max_cdclk_freq = 320000;
  4991. } else if (IS_VALLEYVIEW(dev_priv)) {
  4992. dev_priv->max_cdclk_freq = 400000;
  4993. } else {
  4994. /* otherwise assume cdclk is fixed */
  4995. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4996. }
  4997. dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
  4998. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4999. dev_priv->max_cdclk_freq);
  5000. DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
  5001. dev_priv->max_dotclk_freq);
  5002. }
  5003. static void intel_update_cdclk(struct drm_device *dev)
  5004. {
  5005. struct drm_i915_private *dev_priv = to_i915(dev);
  5006. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  5007. if (INTEL_GEN(dev_priv) >= 9)
  5008. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
  5009. dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
  5010. dev_priv->cdclk_pll.ref);
  5011. else
  5012. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  5013. dev_priv->cdclk_freq);
  5014. /*
  5015. * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
  5016. * Programmng [sic] note: bit[9:2] should be programmed to the number
  5017. * of cdclk that generates 4MHz reference clock freq which is used to
  5018. * generate GMBus clock. This will vary with the cdclk freq.
  5019. */
  5020. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  5021. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  5022. }
  5023. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  5024. static int skl_cdclk_decimal(int cdclk)
  5025. {
  5026. return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
  5027. }
  5028. static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
  5029. {
  5030. int ratio;
  5031. if (cdclk == dev_priv->cdclk_pll.ref)
  5032. return 0;
  5033. switch (cdclk) {
  5034. default:
  5035. MISSING_CASE(cdclk);
  5036. case 144000:
  5037. case 288000:
  5038. case 384000:
  5039. case 576000:
  5040. ratio = 60;
  5041. break;
  5042. case 624000:
  5043. ratio = 65;
  5044. break;
  5045. }
  5046. return dev_priv->cdclk_pll.ref * ratio;
  5047. }
  5048. static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
  5049. {
  5050. I915_WRITE(BXT_DE_PLL_ENABLE, 0);
  5051. /* Timeout 200us */
  5052. if (intel_wait_for_register(dev_priv,
  5053. BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
  5054. 1))
  5055. DRM_ERROR("timeout waiting for DE PLL unlock\n");
  5056. dev_priv->cdclk_pll.vco = 0;
  5057. }
  5058. static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
  5059. {
  5060. int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
  5061. u32 val;
  5062. val = I915_READ(BXT_DE_PLL_CTL);
  5063. val &= ~BXT_DE_PLL_RATIO_MASK;
  5064. val |= BXT_DE_PLL_RATIO(ratio);
  5065. I915_WRITE(BXT_DE_PLL_CTL, val);
  5066. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  5067. /* Timeout 200us */
  5068. if (intel_wait_for_register(dev_priv,
  5069. BXT_DE_PLL_ENABLE,
  5070. BXT_DE_PLL_LOCK,
  5071. BXT_DE_PLL_LOCK,
  5072. 1))
  5073. DRM_ERROR("timeout waiting for DE PLL lock\n");
  5074. dev_priv->cdclk_pll.vco = vco;
  5075. }
  5076. static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
  5077. {
  5078. u32 val, divider;
  5079. int vco, ret;
  5080. vco = bxt_de_pll_vco(dev_priv, cdclk);
  5081. DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
  5082. /* cdclk = vco / 2 / div{1,1.5,2,4} */
  5083. switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
  5084. case 8:
  5085. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  5086. break;
  5087. case 4:
  5088. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  5089. break;
  5090. case 3:
  5091. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  5092. break;
  5093. case 2:
  5094. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  5095. break;
  5096. default:
  5097. WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
  5098. WARN_ON(vco != 0);
  5099. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  5100. break;
  5101. }
  5102. /* Inform power controller of upcoming frequency change */
  5103. mutex_lock(&dev_priv->rps.hw_lock);
  5104. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  5105. 0x80000000);
  5106. mutex_unlock(&dev_priv->rps.hw_lock);
  5107. if (ret) {
  5108. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  5109. ret, cdclk);
  5110. return;
  5111. }
  5112. if (dev_priv->cdclk_pll.vco != 0 &&
  5113. dev_priv->cdclk_pll.vco != vco)
  5114. bxt_de_pll_disable(dev_priv);
  5115. if (dev_priv->cdclk_pll.vco != vco)
  5116. bxt_de_pll_enable(dev_priv, vco);
  5117. val = divider | skl_cdclk_decimal(cdclk);
  5118. /*
  5119. * FIXME if only the cd2x divider needs changing, it could be done
  5120. * without shutting off the pipe (if only one pipe is active).
  5121. */
  5122. val |= BXT_CDCLK_CD2X_PIPE_NONE;
  5123. /*
  5124. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  5125. * enable otherwise.
  5126. */
  5127. if (cdclk >= 500000)
  5128. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  5129. I915_WRITE(CDCLK_CTL, val);
  5130. mutex_lock(&dev_priv->rps.hw_lock);
  5131. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  5132. DIV_ROUND_UP(cdclk, 25000));
  5133. mutex_unlock(&dev_priv->rps.hw_lock);
  5134. if (ret) {
  5135. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  5136. ret, cdclk);
  5137. return;
  5138. }
  5139. intel_update_cdclk(&dev_priv->drm);
  5140. }
  5141. static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
  5142. {
  5143. u32 cdctl, expected;
  5144. intel_update_cdclk(&dev_priv->drm);
  5145. if (dev_priv->cdclk_pll.vco == 0 ||
  5146. dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
  5147. goto sanitize;
  5148. /* DPLL okay; verify the cdclock
  5149. *
  5150. * Some BIOS versions leave an incorrect decimal frequency value and
  5151. * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
  5152. * so sanitize this register.
  5153. */
  5154. cdctl = I915_READ(CDCLK_CTL);
  5155. /*
  5156. * Let's ignore the pipe field, since BIOS could have configured the
  5157. * dividers both synching to an active pipe, or asynchronously
  5158. * (PIPE_NONE).
  5159. */
  5160. cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
  5161. expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
  5162. skl_cdclk_decimal(dev_priv->cdclk_freq);
  5163. /*
  5164. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  5165. * enable otherwise.
  5166. */
  5167. if (dev_priv->cdclk_freq >= 500000)
  5168. expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  5169. if (cdctl == expected)
  5170. /* All well; nothing to sanitize */
  5171. return;
  5172. sanitize:
  5173. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  5174. /* force cdclk programming */
  5175. dev_priv->cdclk_freq = 0;
  5176. /* force full PLL disable + enable */
  5177. dev_priv->cdclk_pll.vco = -1;
  5178. }
  5179. void bxt_init_cdclk(struct drm_i915_private *dev_priv)
  5180. {
  5181. bxt_sanitize_cdclk(dev_priv);
  5182. if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
  5183. return;
  5184. /*
  5185. * FIXME:
  5186. * - The initial CDCLK needs to be read from VBT.
  5187. * Need to make this change after VBT has changes for BXT.
  5188. */
  5189. bxt_set_cdclk(dev_priv, bxt_calc_cdclk(0));
  5190. }
  5191. void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
  5192. {
  5193. bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
  5194. }
  5195. static int skl_calc_cdclk(int max_pixclk, int vco)
  5196. {
  5197. if (vco == 8640000) {
  5198. if (max_pixclk > 540000)
  5199. return 617143;
  5200. else if (max_pixclk > 432000)
  5201. return 540000;
  5202. else if (max_pixclk > 308571)
  5203. return 432000;
  5204. else
  5205. return 308571;
  5206. } else {
  5207. if (max_pixclk > 540000)
  5208. return 675000;
  5209. else if (max_pixclk > 450000)
  5210. return 540000;
  5211. else if (max_pixclk > 337500)
  5212. return 450000;
  5213. else
  5214. return 337500;
  5215. }
  5216. }
  5217. static void
  5218. skl_dpll0_update(struct drm_i915_private *dev_priv)
  5219. {
  5220. u32 val;
  5221. dev_priv->cdclk_pll.ref = 24000;
  5222. dev_priv->cdclk_pll.vco = 0;
  5223. val = I915_READ(LCPLL1_CTL);
  5224. if ((val & LCPLL_PLL_ENABLE) == 0)
  5225. return;
  5226. if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
  5227. return;
  5228. val = I915_READ(DPLL_CTRL1);
  5229. if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
  5230. DPLL_CTRL1_SSC(SKL_DPLL0) |
  5231. DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
  5232. DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
  5233. return;
  5234. switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
  5235. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
  5236. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
  5237. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
  5238. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
  5239. dev_priv->cdclk_pll.vco = 8100000;
  5240. break;
  5241. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
  5242. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
  5243. dev_priv->cdclk_pll.vco = 8640000;
  5244. break;
  5245. default:
  5246. MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  5247. break;
  5248. }
  5249. }
  5250. void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
  5251. {
  5252. bool changed = dev_priv->skl_preferred_vco_freq != vco;
  5253. dev_priv->skl_preferred_vco_freq = vco;
  5254. if (changed)
  5255. intel_update_max_cdclk(&dev_priv->drm);
  5256. }
  5257. static void
  5258. skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
  5259. {
  5260. int min_cdclk = skl_calc_cdclk(0, vco);
  5261. u32 val;
  5262. WARN_ON(vco != 8100000 && vco != 8640000);
  5263. /* select the minimum CDCLK before enabling DPLL 0 */
  5264. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
  5265. I915_WRITE(CDCLK_CTL, val);
  5266. POSTING_READ(CDCLK_CTL);
  5267. /*
  5268. * We always enable DPLL0 with the lowest link rate possible, but still
  5269. * taking into account the VCO required to operate the eDP panel at the
  5270. * desired frequency. The usual DP link rates operate with a VCO of
  5271. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  5272. * The modeset code is responsible for the selection of the exact link
  5273. * rate later on, with the constraint of choosing a frequency that
  5274. * works with vco.
  5275. */
  5276. val = I915_READ(DPLL_CTRL1);
  5277. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  5278. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  5279. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  5280. if (vco == 8640000)
  5281. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  5282. SKL_DPLL0);
  5283. else
  5284. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  5285. SKL_DPLL0);
  5286. I915_WRITE(DPLL_CTRL1, val);
  5287. POSTING_READ(DPLL_CTRL1);
  5288. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  5289. if (intel_wait_for_register(dev_priv,
  5290. LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  5291. 5))
  5292. DRM_ERROR("DPLL0 not locked\n");
  5293. dev_priv->cdclk_pll.vco = vco;
  5294. /* We'll want to keep using the current vco from now on. */
  5295. skl_set_preferred_cdclk_vco(dev_priv, vco);
  5296. }
  5297. static void
  5298. skl_dpll0_disable(struct drm_i915_private *dev_priv)
  5299. {
  5300. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  5301. if (intel_wait_for_register(dev_priv,
  5302. LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
  5303. 1))
  5304. DRM_ERROR("Couldn't disable DPLL0\n");
  5305. dev_priv->cdclk_pll.vco = 0;
  5306. }
  5307. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  5308. {
  5309. int ret;
  5310. u32 val;
  5311. /* inform PCU we want to change CDCLK */
  5312. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  5313. mutex_lock(&dev_priv->rps.hw_lock);
  5314. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  5315. mutex_unlock(&dev_priv->rps.hw_lock);
  5316. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  5317. }
  5318. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  5319. {
  5320. return _wait_for(skl_cdclk_pcu_ready(dev_priv), 3000, 10) == 0;
  5321. }
  5322. static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
  5323. {
  5324. struct drm_device *dev = &dev_priv->drm;
  5325. u32 freq_select, pcu_ack;
  5326. WARN_ON((cdclk == 24000) != (vco == 0));
  5327. DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
  5328. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  5329. DRM_ERROR("failed to inform PCU about cdclk change\n");
  5330. return;
  5331. }
  5332. /* set CDCLK_CTL */
  5333. switch (cdclk) {
  5334. case 450000:
  5335. case 432000:
  5336. freq_select = CDCLK_FREQ_450_432;
  5337. pcu_ack = 1;
  5338. break;
  5339. case 540000:
  5340. freq_select = CDCLK_FREQ_540;
  5341. pcu_ack = 2;
  5342. break;
  5343. case 308571:
  5344. case 337500:
  5345. default:
  5346. freq_select = CDCLK_FREQ_337_308;
  5347. pcu_ack = 0;
  5348. break;
  5349. case 617143:
  5350. case 675000:
  5351. freq_select = CDCLK_FREQ_675_617;
  5352. pcu_ack = 3;
  5353. break;
  5354. }
  5355. if (dev_priv->cdclk_pll.vco != 0 &&
  5356. dev_priv->cdclk_pll.vco != vco)
  5357. skl_dpll0_disable(dev_priv);
  5358. if (dev_priv->cdclk_pll.vco != vco)
  5359. skl_dpll0_enable(dev_priv, vco);
  5360. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
  5361. POSTING_READ(CDCLK_CTL);
  5362. /* inform PCU of the change */
  5363. mutex_lock(&dev_priv->rps.hw_lock);
  5364. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  5365. mutex_unlock(&dev_priv->rps.hw_lock);
  5366. intel_update_cdclk(dev);
  5367. }
  5368. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
  5369. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  5370. {
  5371. skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
  5372. }
  5373. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  5374. {
  5375. int cdclk, vco;
  5376. skl_sanitize_cdclk(dev_priv);
  5377. if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
  5378. /*
  5379. * Use the current vco as our initial
  5380. * guess as to what the preferred vco is.
  5381. */
  5382. if (dev_priv->skl_preferred_vco_freq == 0)
  5383. skl_set_preferred_cdclk_vco(dev_priv,
  5384. dev_priv->cdclk_pll.vco);
  5385. return;
  5386. }
  5387. vco = dev_priv->skl_preferred_vco_freq;
  5388. if (vco == 0)
  5389. vco = 8100000;
  5390. cdclk = skl_calc_cdclk(0, vco);
  5391. skl_set_cdclk(dev_priv, cdclk, vco);
  5392. }
  5393. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
  5394. {
  5395. uint32_t cdctl, expected;
  5396. /*
  5397. * check if the pre-os intialized the display
  5398. * There is SWF18 scratchpad register defined which is set by the
  5399. * pre-os which can be used by the OS drivers to check the status
  5400. */
  5401. if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
  5402. goto sanitize;
  5403. intel_update_cdclk(&dev_priv->drm);
  5404. /* Is PLL enabled and locked ? */
  5405. if (dev_priv->cdclk_pll.vco == 0 ||
  5406. dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
  5407. goto sanitize;
  5408. /* DPLL okay; verify the cdclock
  5409. *
  5410. * Noticed in some instances that the freq selection is correct but
  5411. * decimal part is programmed wrong from BIOS where pre-os does not
  5412. * enable display. Verify the same as well.
  5413. */
  5414. cdctl = I915_READ(CDCLK_CTL);
  5415. expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
  5416. skl_cdclk_decimal(dev_priv->cdclk_freq);
  5417. if (cdctl == expected)
  5418. /* All well; nothing to sanitize */
  5419. return;
  5420. sanitize:
  5421. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  5422. /* force cdclk programming */
  5423. dev_priv->cdclk_freq = 0;
  5424. /* force full PLL disable + enable */
  5425. dev_priv->cdclk_pll.vco = -1;
  5426. }
  5427. /* Adjust CDclk dividers to allow high res or save power if possible */
  5428. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  5429. {
  5430. struct drm_i915_private *dev_priv = to_i915(dev);
  5431. u32 val, cmd;
  5432. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  5433. != dev_priv->cdclk_freq);
  5434. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  5435. cmd = 2;
  5436. else if (cdclk == 266667)
  5437. cmd = 1;
  5438. else
  5439. cmd = 0;
  5440. mutex_lock(&dev_priv->rps.hw_lock);
  5441. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  5442. val &= ~DSPFREQGUAR_MASK;
  5443. val |= (cmd << DSPFREQGUAR_SHIFT);
  5444. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  5445. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  5446. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  5447. 50)) {
  5448. DRM_ERROR("timed out waiting for CDclk change\n");
  5449. }
  5450. mutex_unlock(&dev_priv->rps.hw_lock);
  5451. mutex_lock(&dev_priv->sb_lock);
  5452. if (cdclk == 400000) {
  5453. u32 divider;
  5454. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  5455. /* adjust cdclk divider */
  5456. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  5457. val &= ~CCK_FREQUENCY_VALUES;
  5458. val |= divider;
  5459. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  5460. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  5461. CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
  5462. 50))
  5463. DRM_ERROR("timed out waiting for CDclk change\n");
  5464. }
  5465. /* adjust self-refresh exit latency value */
  5466. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  5467. val &= ~0x7f;
  5468. /*
  5469. * For high bandwidth configs, we set a higher latency in the bunit
  5470. * so that the core display fetch happens in time to avoid underruns.
  5471. */
  5472. if (cdclk == 400000)
  5473. val |= 4500 / 250; /* 4.5 usec */
  5474. else
  5475. val |= 3000 / 250; /* 3.0 usec */
  5476. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  5477. mutex_unlock(&dev_priv->sb_lock);
  5478. intel_update_cdclk(dev);
  5479. }
  5480. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  5481. {
  5482. struct drm_i915_private *dev_priv = to_i915(dev);
  5483. u32 val, cmd;
  5484. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  5485. != dev_priv->cdclk_freq);
  5486. switch (cdclk) {
  5487. case 333333:
  5488. case 320000:
  5489. case 266667:
  5490. case 200000:
  5491. break;
  5492. default:
  5493. MISSING_CASE(cdclk);
  5494. return;
  5495. }
  5496. /*
  5497. * Specs are full of misinformation, but testing on actual
  5498. * hardware has shown that we just need to write the desired
  5499. * CCK divider into the Punit register.
  5500. */
  5501. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  5502. mutex_lock(&dev_priv->rps.hw_lock);
  5503. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  5504. val &= ~DSPFREQGUAR_MASK_CHV;
  5505. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  5506. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  5507. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  5508. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  5509. 50)) {
  5510. DRM_ERROR("timed out waiting for CDclk change\n");
  5511. }
  5512. mutex_unlock(&dev_priv->rps.hw_lock);
  5513. intel_update_cdclk(dev);
  5514. }
  5515. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  5516. int max_pixclk)
  5517. {
  5518. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  5519. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  5520. /*
  5521. * Really only a few cases to deal with, as only 4 CDclks are supported:
  5522. * 200MHz
  5523. * 267MHz
  5524. * 320/333MHz (depends on HPLL freq)
  5525. * 400MHz (VLV only)
  5526. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  5527. * of the lower bin and adjust if needed.
  5528. *
  5529. * We seem to get an unstable or solid color picture at 200MHz.
  5530. * Not sure what's wrong. For now use 200MHz only when all pipes
  5531. * are off.
  5532. */
  5533. if (!IS_CHERRYVIEW(dev_priv) &&
  5534. max_pixclk > freq_320*limit/100)
  5535. return 400000;
  5536. else if (max_pixclk > 266667*limit/100)
  5537. return freq_320;
  5538. else if (max_pixclk > 0)
  5539. return 266667;
  5540. else
  5541. return 200000;
  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. int max_pixclk = ilk_max_pixel_rate(state);
  5594. struct intel_atomic_state *intel_state =
  5595. to_intel_atomic_state(state);
  5596. intel_state->cdclk = intel_state->dev_cdclk =
  5597. bxt_calc_cdclk(max_pixclk);
  5598. if (!intel_state->active_crtcs)
  5599. intel_state->dev_cdclk = bxt_calc_cdclk(0);
  5600. return 0;
  5601. }
  5602. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5603. {
  5604. unsigned int credits, default_credits;
  5605. if (IS_CHERRYVIEW(dev_priv))
  5606. default_credits = PFI_CREDIT(12);
  5607. else
  5608. default_credits = PFI_CREDIT(8);
  5609. if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
  5610. /* CHV suggested value is 31 or 63 */
  5611. if (IS_CHERRYVIEW(dev_priv))
  5612. credits = PFI_CREDIT_63;
  5613. else
  5614. credits = PFI_CREDIT(15);
  5615. } else {
  5616. credits = default_credits;
  5617. }
  5618. /*
  5619. * WA - write default credits before re-programming
  5620. * FIXME: should we also set the resend bit here?
  5621. */
  5622. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5623. default_credits);
  5624. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5625. credits | PFI_CREDIT_RESEND);
  5626. /*
  5627. * FIXME is this guaranteed to clear
  5628. * immediately or should we poll for it?
  5629. */
  5630. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5631. }
  5632. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5633. {
  5634. struct drm_device *dev = old_state->dev;
  5635. struct drm_i915_private *dev_priv = to_i915(dev);
  5636. struct intel_atomic_state *old_intel_state =
  5637. to_intel_atomic_state(old_state);
  5638. unsigned req_cdclk = old_intel_state->dev_cdclk;
  5639. /*
  5640. * FIXME: We can end up here with all power domains off, yet
  5641. * with a CDCLK frequency other than the minimum. To account
  5642. * for this take the PIPE-A power domain, which covers the HW
  5643. * blocks needed for the following programming. This can be
  5644. * removed once it's guaranteed that we get here either with
  5645. * the minimum CDCLK set, or the required power domains
  5646. * enabled.
  5647. */
  5648. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5649. if (IS_CHERRYVIEW(dev_priv))
  5650. cherryview_set_cdclk(dev, req_cdclk);
  5651. else
  5652. valleyview_set_cdclk(dev, req_cdclk);
  5653. vlv_program_pfi_credits(dev_priv);
  5654. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5655. }
  5656. static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
  5657. struct drm_atomic_state *old_state)
  5658. {
  5659. struct drm_crtc *crtc = pipe_config->base.crtc;
  5660. struct drm_device *dev = crtc->dev;
  5661. struct drm_i915_private *dev_priv = to_i915(dev);
  5662. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5663. int pipe = intel_crtc->pipe;
  5664. if (WARN_ON(intel_crtc->active))
  5665. return;
  5666. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5667. intel_dp_set_m_n(intel_crtc, M1_N1);
  5668. intel_set_pipe_timings(intel_crtc);
  5669. intel_set_pipe_src_size(intel_crtc);
  5670. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  5671. struct drm_i915_private *dev_priv = to_i915(dev);
  5672. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5673. I915_WRITE(CHV_CANVAS(pipe), 0);
  5674. }
  5675. i9xx_set_pipeconf(intel_crtc);
  5676. intel_crtc->active = true;
  5677. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5678. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  5679. if (IS_CHERRYVIEW(dev_priv)) {
  5680. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5681. chv_enable_pll(intel_crtc, intel_crtc->config);
  5682. } else {
  5683. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5684. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5685. }
  5686. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  5687. i9xx_pfit_enable(intel_crtc);
  5688. intel_color_load_luts(&pipe_config->base);
  5689. intel_update_watermarks(crtc);
  5690. intel_enable_pipe(intel_crtc);
  5691. assert_vblank_disabled(crtc);
  5692. drm_crtc_vblank_on(crtc);
  5693. intel_encoders_enable(crtc, pipe_config, old_state);
  5694. }
  5695. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5696. {
  5697. struct drm_device *dev = crtc->base.dev;
  5698. struct drm_i915_private *dev_priv = to_i915(dev);
  5699. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5700. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5701. }
  5702. static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
  5703. struct drm_atomic_state *old_state)
  5704. {
  5705. struct drm_crtc *crtc = pipe_config->base.crtc;
  5706. struct drm_device *dev = crtc->dev;
  5707. struct drm_i915_private *dev_priv = to_i915(dev);
  5708. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5709. enum pipe pipe = intel_crtc->pipe;
  5710. if (WARN_ON(intel_crtc->active))
  5711. return;
  5712. i9xx_set_pll_dividers(intel_crtc);
  5713. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5714. intel_dp_set_m_n(intel_crtc, M1_N1);
  5715. intel_set_pipe_timings(intel_crtc);
  5716. intel_set_pipe_src_size(intel_crtc);
  5717. i9xx_set_pipeconf(intel_crtc);
  5718. intel_crtc->active = true;
  5719. if (!IS_GEN2(dev_priv))
  5720. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5721. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  5722. i9xx_enable_pll(intel_crtc);
  5723. i9xx_pfit_enable(intel_crtc);
  5724. intel_color_load_luts(&pipe_config->base);
  5725. intel_update_watermarks(crtc);
  5726. intel_enable_pipe(intel_crtc);
  5727. assert_vblank_disabled(crtc);
  5728. drm_crtc_vblank_on(crtc);
  5729. intel_encoders_enable(crtc, pipe_config, old_state);
  5730. }
  5731. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5732. {
  5733. struct drm_device *dev = crtc->base.dev;
  5734. struct drm_i915_private *dev_priv = to_i915(dev);
  5735. if (!crtc->config->gmch_pfit.control)
  5736. return;
  5737. assert_pipe_disabled(dev_priv, crtc->pipe);
  5738. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5739. I915_READ(PFIT_CONTROL));
  5740. I915_WRITE(PFIT_CONTROL, 0);
  5741. }
  5742. static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
  5743. struct drm_atomic_state *old_state)
  5744. {
  5745. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  5746. struct drm_device *dev = crtc->dev;
  5747. struct drm_i915_private *dev_priv = to_i915(dev);
  5748. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5749. int pipe = intel_crtc->pipe;
  5750. /*
  5751. * On gen2 planes are double buffered but the pipe isn't, so we must
  5752. * wait for planes to fully turn off before disabling the pipe.
  5753. */
  5754. if (IS_GEN2(dev_priv))
  5755. intel_wait_for_vblank(dev, pipe);
  5756. intel_encoders_disable(crtc, old_crtc_state, old_state);
  5757. drm_crtc_vblank_off(crtc);
  5758. assert_vblank_disabled(crtc);
  5759. intel_disable_pipe(intel_crtc);
  5760. i9xx_pfit_disable(intel_crtc);
  5761. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  5762. if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
  5763. if (IS_CHERRYVIEW(dev_priv))
  5764. chv_disable_pll(dev_priv, pipe);
  5765. else if (IS_VALLEYVIEW(dev_priv))
  5766. vlv_disable_pll(dev_priv, pipe);
  5767. else
  5768. i9xx_disable_pll(intel_crtc);
  5769. }
  5770. intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
  5771. if (!IS_GEN2(dev_priv))
  5772. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5773. }
  5774. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5775. {
  5776. struct intel_encoder *encoder;
  5777. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5778. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5779. enum intel_display_power_domain domain;
  5780. unsigned long domains;
  5781. struct drm_atomic_state *state;
  5782. struct intel_crtc_state *crtc_state;
  5783. int ret;
  5784. if (!intel_crtc->active)
  5785. return;
  5786. if (to_intel_plane_state(crtc->primary->state)->base.visible) {
  5787. WARN_ON(intel_crtc->flip_work);
  5788. intel_pre_disable_primary_noatomic(crtc);
  5789. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  5790. to_intel_plane_state(crtc->primary->state)->base.visible = false;
  5791. }
  5792. state = drm_atomic_state_alloc(crtc->dev);
  5793. state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
  5794. /* Everything's already locked, -EDEADLK can't happen. */
  5795. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  5796. ret = drm_atomic_add_affected_connectors(state, crtc);
  5797. WARN_ON(IS_ERR(crtc_state) || ret);
  5798. dev_priv->display.crtc_disable(crtc_state, state);
  5799. drm_atomic_state_free(state);
  5800. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  5801. crtc->base.id, crtc->name);
  5802. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  5803. crtc->state->active = false;
  5804. intel_crtc->active = false;
  5805. crtc->enabled = false;
  5806. crtc->state->connector_mask = 0;
  5807. crtc->state->encoder_mask = 0;
  5808. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  5809. encoder->base.crtc = NULL;
  5810. intel_fbc_disable(intel_crtc);
  5811. intel_update_watermarks(crtc);
  5812. intel_disable_shared_dpll(intel_crtc);
  5813. domains = intel_crtc->enabled_power_domains;
  5814. for_each_power_domain(domain, domains)
  5815. intel_display_power_put(dev_priv, domain);
  5816. intel_crtc->enabled_power_domains = 0;
  5817. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5818. dev_priv->min_pixclk[intel_crtc->pipe] = 0;
  5819. }
  5820. /*
  5821. * turn all crtc's off, but do not adjust state
  5822. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5823. */
  5824. int intel_display_suspend(struct drm_device *dev)
  5825. {
  5826. struct drm_i915_private *dev_priv = to_i915(dev);
  5827. struct drm_atomic_state *state;
  5828. int ret;
  5829. state = drm_atomic_helper_suspend(dev);
  5830. ret = PTR_ERR_OR_ZERO(state);
  5831. if (ret)
  5832. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5833. else
  5834. dev_priv->modeset_restore_state = state;
  5835. return ret;
  5836. }
  5837. void intel_encoder_destroy(struct drm_encoder *encoder)
  5838. {
  5839. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5840. drm_encoder_cleanup(encoder);
  5841. kfree(intel_encoder);
  5842. }
  5843. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5844. * internal consistency). */
  5845. static void intel_connector_verify_state(struct intel_connector *connector)
  5846. {
  5847. struct drm_crtc *crtc = connector->base.state->crtc;
  5848. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5849. connector->base.base.id,
  5850. connector->base.name);
  5851. if (connector->get_hw_state(connector)) {
  5852. struct intel_encoder *encoder = connector->encoder;
  5853. struct drm_connector_state *conn_state = connector->base.state;
  5854. I915_STATE_WARN(!crtc,
  5855. "connector enabled without attached crtc\n");
  5856. if (!crtc)
  5857. return;
  5858. I915_STATE_WARN(!crtc->state->active,
  5859. "connector is active, but attached crtc isn't\n");
  5860. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5861. return;
  5862. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5863. "atomic encoder doesn't match attached encoder\n");
  5864. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5865. "attached encoder crtc differs from connector crtc\n");
  5866. } else {
  5867. I915_STATE_WARN(crtc && crtc->state->active,
  5868. "attached crtc is active, but connector isn't\n");
  5869. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5870. "best encoder set without crtc!\n");
  5871. }
  5872. }
  5873. int intel_connector_init(struct intel_connector *connector)
  5874. {
  5875. drm_atomic_helper_connector_reset(&connector->base);
  5876. if (!connector->base.state)
  5877. return -ENOMEM;
  5878. return 0;
  5879. }
  5880. struct intel_connector *intel_connector_alloc(void)
  5881. {
  5882. struct intel_connector *connector;
  5883. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5884. if (!connector)
  5885. return NULL;
  5886. if (intel_connector_init(connector) < 0) {
  5887. kfree(connector);
  5888. return NULL;
  5889. }
  5890. return connector;
  5891. }
  5892. /* Simple connector->get_hw_state implementation for encoders that support only
  5893. * one connector and no cloning and hence the encoder state determines the state
  5894. * of the connector. */
  5895. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5896. {
  5897. enum pipe pipe = 0;
  5898. struct intel_encoder *encoder = connector->encoder;
  5899. return encoder->get_hw_state(encoder, &pipe);
  5900. }
  5901. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5902. {
  5903. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5904. return crtc_state->fdi_lanes;
  5905. return 0;
  5906. }
  5907. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5908. struct intel_crtc_state *pipe_config)
  5909. {
  5910. struct drm_i915_private *dev_priv = to_i915(dev);
  5911. struct drm_atomic_state *state = pipe_config->base.state;
  5912. struct intel_crtc *other_crtc;
  5913. struct intel_crtc_state *other_crtc_state;
  5914. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5915. pipe_name(pipe), pipe_config->fdi_lanes);
  5916. if (pipe_config->fdi_lanes > 4) {
  5917. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5918. pipe_name(pipe), pipe_config->fdi_lanes);
  5919. return -EINVAL;
  5920. }
  5921. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  5922. if (pipe_config->fdi_lanes > 2) {
  5923. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5924. pipe_config->fdi_lanes);
  5925. return -EINVAL;
  5926. } else {
  5927. return 0;
  5928. }
  5929. }
  5930. if (INTEL_INFO(dev)->num_pipes == 2)
  5931. return 0;
  5932. /* Ivybridge 3 pipe is really complicated */
  5933. switch (pipe) {
  5934. case PIPE_A:
  5935. return 0;
  5936. case PIPE_B:
  5937. if (pipe_config->fdi_lanes <= 2)
  5938. return 0;
  5939. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
  5940. other_crtc_state =
  5941. intel_atomic_get_crtc_state(state, other_crtc);
  5942. if (IS_ERR(other_crtc_state))
  5943. return PTR_ERR(other_crtc_state);
  5944. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5945. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5946. pipe_name(pipe), pipe_config->fdi_lanes);
  5947. return -EINVAL;
  5948. }
  5949. return 0;
  5950. case PIPE_C:
  5951. if (pipe_config->fdi_lanes > 2) {
  5952. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5953. pipe_name(pipe), pipe_config->fdi_lanes);
  5954. return -EINVAL;
  5955. }
  5956. other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
  5957. other_crtc_state =
  5958. intel_atomic_get_crtc_state(state, other_crtc);
  5959. if (IS_ERR(other_crtc_state))
  5960. return PTR_ERR(other_crtc_state);
  5961. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5962. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5963. return -EINVAL;
  5964. }
  5965. return 0;
  5966. default:
  5967. BUG();
  5968. }
  5969. }
  5970. #define RETRY 1
  5971. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5972. struct intel_crtc_state *pipe_config)
  5973. {
  5974. struct drm_device *dev = intel_crtc->base.dev;
  5975. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5976. int lane, link_bw, fdi_dotclock, ret;
  5977. bool needs_recompute = false;
  5978. retry:
  5979. /* FDI is a binary signal running at ~2.7GHz, encoding
  5980. * each output octet as 10 bits. The actual frequency
  5981. * is stored as a divider into a 100MHz clock, and the
  5982. * mode pixel clock is stored in units of 1KHz.
  5983. * Hence the bw of each lane in terms of the mode signal
  5984. * is:
  5985. */
  5986. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5987. fdi_dotclock = adjusted_mode->crtc_clock;
  5988. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5989. pipe_config->pipe_bpp);
  5990. pipe_config->fdi_lanes = lane;
  5991. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5992. link_bw, &pipe_config->fdi_m_n);
  5993. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5994. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5995. pipe_config->pipe_bpp -= 2*3;
  5996. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5997. pipe_config->pipe_bpp);
  5998. needs_recompute = true;
  5999. pipe_config->bw_constrained = true;
  6000. goto retry;
  6001. }
  6002. if (needs_recompute)
  6003. return RETRY;
  6004. return ret;
  6005. }
  6006. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  6007. struct intel_crtc_state *pipe_config)
  6008. {
  6009. if (pipe_config->pipe_bpp > 24)
  6010. return false;
  6011. /* HSW can handle pixel rate up to cdclk? */
  6012. if (IS_HASWELL(dev_priv))
  6013. return true;
  6014. /*
  6015. * We compare against max which means we must take
  6016. * the increased cdclk requirement into account when
  6017. * calculating the new cdclk.
  6018. *
  6019. * Should measure whether using a lower cdclk w/o IPS
  6020. */
  6021. return ilk_pipe_pixel_rate(pipe_config) <=
  6022. dev_priv->max_cdclk_freq * 95 / 100;
  6023. }
  6024. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  6025. struct intel_crtc_state *pipe_config)
  6026. {
  6027. struct drm_device *dev = crtc->base.dev;
  6028. struct drm_i915_private *dev_priv = to_i915(dev);
  6029. pipe_config->ips_enabled = i915.enable_ips &&
  6030. hsw_crtc_supports_ips(crtc) &&
  6031. pipe_config_supports_ips(dev_priv, pipe_config);
  6032. }
  6033. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  6034. {
  6035. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6036. /* GDG double wide on either pipe, otherwise pipe A only */
  6037. return INTEL_INFO(dev_priv)->gen < 4 &&
  6038. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  6039. }
  6040. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  6041. struct intel_crtc_state *pipe_config)
  6042. {
  6043. struct drm_device *dev = crtc->base.dev;
  6044. struct drm_i915_private *dev_priv = to_i915(dev);
  6045. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  6046. int clock_limit = dev_priv->max_dotclk_freq;
  6047. if (INTEL_INFO(dev)->gen < 4) {
  6048. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  6049. /*
  6050. * Enable double wide mode when the dot clock
  6051. * is > 90% of the (display) core speed.
  6052. */
  6053. if (intel_crtc_supports_double_wide(crtc) &&
  6054. adjusted_mode->crtc_clock > clock_limit) {
  6055. clock_limit = dev_priv->max_dotclk_freq;
  6056. pipe_config->double_wide = true;
  6057. }
  6058. }
  6059. if (adjusted_mode->crtc_clock > clock_limit) {
  6060. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  6061. adjusted_mode->crtc_clock, clock_limit,
  6062. yesno(pipe_config->double_wide));
  6063. return -EINVAL;
  6064. }
  6065. /*
  6066. * Pipe horizontal size must be even in:
  6067. * - DVO ganged mode
  6068. * - LVDS dual channel mode
  6069. * - Double wide pipe
  6070. */
  6071. if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  6072. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  6073. pipe_config->pipe_src_w &= ~1;
  6074. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  6075. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  6076. */
  6077. if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
  6078. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  6079. return -EINVAL;
  6080. if (HAS_IPS(dev_priv))
  6081. hsw_compute_ips_config(crtc, pipe_config);
  6082. if (pipe_config->has_pch_encoder)
  6083. return ironlake_fdi_compute_config(crtc, pipe_config);
  6084. return 0;
  6085. }
  6086. static int skylake_get_display_clock_speed(struct drm_device *dev)
  6087. {
  6088. struct drm_i915_private *dev_priv = to_i915(dev);
  6089. uint32_t cdctl;
  6090. skl_dpll0_update(dev_priv);
  6091. if (dev_priv->cdclk_pll.vco == 0)
  6092. return dev_priv->cdclk_pll.ref;
  6093. cdctl = I915_READ(CDCLK_CTL);
  6094. if (dev_priv->cdclk_pll.vco == 8640000) {
  6095. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  6096. case CDCLK_FREQ_450_432:
  6097. return 432000;
  6098. case CDCLK_FREQ_337_308:
  6099. return 308571;
  6100. case CDCLK_FREQ_540:
  6101. return 540000;
  6102. case CDCLK_FREQ_675_617:
  6103. return 617143;
  6104. default:
  6105. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  6106. }
  6107. } else {
  6108. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  6109. case CDCLK_FREQ_450_432:
  6110. return 450000;
  6111. case CDCLK_FREQ_337_308:
  6112. return 337500;
  6113. case CDCLK_FREQ_540:
  6114. return 540000;
  6115. case CDCLK_FREQ_675_617:
  6116. return 675000;
  6117. default:
  6118. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  6119. }
  6120. }
  6121. return dev_priv->cdclk_pll.ref;
  6122. }
  6123. static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
  6124. {
  6125. u32 val;
  6126. dev_priv->cdclk_pll.ref = 19200;
  6127. dev_priv->cdclk_pll.vco = 0;
  6128. val = I915_READ(BXT_DE_PLL_ENABLE);
  6129. if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
  6130. return;
  6131. if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
  6132. return;
  6133. val = I915_READ(BXT_DE_PLL_CTL);
  6134. dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
  6135. dev_priv->cdclk_pll.ref;
  6136. }
  6137. static int broxton_get_display_clock_speed(struct drm_device *dev)
  6138. {
  6139. struct drm_i915_private *dev_priv = to_i915(dev);
  6140. u32 divider;
  6141. int div, vco;
  6142. bxt_de_pll_update(dev_priv);
  6143. vco = dev_priv->cdclk_pll.vco;
  6144. if (vco == 0)
  6145. return dev_priv->cdclk_pll.ref;
  6146. divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
  6147. switch (divider) {
  6148. case BXT_CDCLK_CD2X_DIV_SEL_1:
  6149. div = 2;
  6150. break;
  6151. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  6152. div = 3;
  6153. break;
  6154. case BXT_CDCLK_CD2X_DIV_SEL_2:
  6155. div = 4;
  6156. break;
  6157. case BXT_CDCLK_CD2X_DIV_SEL_4:
  6158. div = 8;
  6159. break;
  6160. default:
  6161. MISSING_CASE(divider);
  6162. return dev_priv->cdclk_pll.ref;
  6163. }
  6164. return DIV_ROUND_CLOSEST(vco, div);
  6165. }
  6166. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  6167. {
  6168. struct drm_i915_private *dev_priv = to_i915(dev);
  6169. uint32_t lcpll = I915_READ(LCPLL_CTL);
  6170. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  6171. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  6172. return 800000;
  6173. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  6174. return 450000;
  6175. else if (freq == LCPLL_CLK_FREQ_450)
  6176. return 450000;
  6177. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  6178. return 540000;
  6179. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  6180. return 337500;
  6181. else
  6182. return 675000;
  6183. }
  6184. static int haswell_get_display_clock_speed(struct drm_device *dev)
  6185. {
  6186. struct drm_i915_private *dev_priv = to_i915(dev);
  6187. uint32_t lcpll = I915_READ(LCPLL_CTL);
  6188. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  6189. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  6190. return 800000;
  6191. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  6192. return 450000;
  6193. else if (freq == LCPLL_CLK_FREQ_450)
  6194. return 450000;
  6195. else if (IS_HSW_ULT(dev_priv))
  6196. return 337500;
  6197. else
  6198. return 540000;
  6199. }
  6200. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  6201. {
  6202. return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
  6203. CCK_DISPLAY_CLOCK_CONTROL);
  6204. }
  6205. static int ilk_get_display_clock_speed(struct drm_device *dev)
  6206. {
  6207. return 450000;
  6208. }
  6209. static int i945_get_display_clock_speed(struct drm_device *dev)
  6210. {
  6211. return 400000;
  6212. }
  6213. static int i915_get_display_clock_speed(struct drm_device *dev)
  6214. {
  6215. return 333333;
  6216. }
  6217. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  6218. {
  6219. return 200000;
  6220. }
  6221. static int pnv_get_display_clock_speed(struct drm_device *dev)
  6222. {
  6223. struct pci_dev *pdev = dev->pdev;
  6224. u16 gcfgc = 0;
  6225. pci_read_config_word(pdev, GCFGC, &gcfgc);
  6226. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  6227. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  6228. return 266667;
  6229. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  6230. return 333333;
  6231. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  6232. return 444444;
  6233. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  6234. return 200000;
  6235. default:
  6236. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  6237. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  6238. return 133333;
  6239. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  6240. return 166667;
  6241. }
  6242. }
  6243. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  6244. {
  6245. struct pci_dev *pdev = dev->pdev;
  6246. u16 gcfgc = 0;
  6247. pci_read_config_word(pdev, GCFGC, &gcfgc);
  6248. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  6249. return 133333;
  6250. else {
  6251. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  6252. case GC_DISPLAY_CLOCK_333_MHZ:
  6253. return 333333;
  6254. default:
  6255. case GC_DISPLAY_CLOCK_190_200_MHZ:
  6256. return 190000;
  6257. }
  6258. }
  6259. }
  6260. static int i865_get_display_clock_speed(struct drm_device *dev)
  6261. {
  6262. return 266667;
  6263. }
  6264. static int i85x_get_display_clock_speed(struct drm_device *dev)
  6265. {
  6266. struct pci_dev *pdev = dev->pdev;
  6267. u16 hpllcc = 0;
  6268. /*
  6269. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  6270. * encoding is different :(
  6271. * FIXME is this the right way to detect 852GM/852GMV?
  6272. */
  6273. if (pdev->revision == 0x1)
  6274. return 133333;
  6275. pci_bus_read_config_word(pdev->bus,
  6276. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  6277. /* Assume that the hardware is in the high speed state. This
  6278. * should be the default.
  6279. */
  6280. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  6281. case GC_CLOCK_133_200:
  6282. case GC_CLOCK_133_200_2:
  6283. case GC_CLOCK_100_200:
  6284. return 200000;
  6285. case GC_CLOCK_166_250:
  6286. return 250000;
  6287. case GC_CLOCK_100_133:
  6288. return 133333;
  6289. case GC_CLOCK_133_266:
  6290. case GC_CLOCK_133_266_2:
  6291. case GC_CLOCK_166_266:
  6292. return 266667;
  6293. }
  6294. /* Shouldn't happen */
  6295. return 0;
  6296. }
  6297. static int i830_get_display_clock_speed(struct drm_device *dev)
  6298. {
  6299. return 133333;
  6300. }
  6301. static unsigned int intel_hpll_vco(struct drm_device *dev)
  6302. {
  6303. struct drm_i915_private *dev_priv = to_i915(dev);
  6304. static const unsigned int blb_vco[8] = {
  6305. [0] = 3200000,
  6306. [1] = 4000000,
  6307. [2] = 5333333,
  6308. [3] = 4800000,
  6309. [4] = 6400000,
  6310. };
  6311. static const unsigned int pnv_vco[8] = {
  6312. [0] = 3200000,
  6313. [1] = 4000000,
  6314. [2] = 5333333,
  6315. [3] = 4800000,
  6316. [4] = 2666667,
  6317. };
  6318. static const unsigned int cl_vco[8] = {
  6319. [0] = 3200000,
  6320. [1] = 4000000,
  6321. [2] = 5333333,
  6322. [3] = 6400000,
  6323. [4] = 3333333,
  6324. [5] = 3566667,
  6325. [6] = 4266667,
  6326. };
  6327. static const unsigned int elk_vco[8] = {
  6328. [0] = 3200000,
  6329. [1] = 4000000,
  6330. [2] = 5333333,
  6331. [3] = 4800000,
  6332. };
  6333. static const unsigned int ctg_vco[8] = {
  6334. [0] = 3200000,
  6335. [1] = 4000000,
  6336. [2] = 5333333,
  6337. [3] = 6400000,
  6338. [4] = 2666667,
  6339. [5] = 4266667,
  6340. };
  6341. const unsigned int *vco_table;
  6342. unsigned int vco;
  6343. uint8_t tmp = 0;
  6344. /* FIXME other chipsets? */
  6345. if (IS_GM45(dev_priv))
  6346. vco_table = ctg_vco;
  6347. else if (IS_G4X(dev_priv))
  6348. vco_table = elk_vco;
  6349. else if (IS_CRESTLINE(dev))
  6350. vco_table = cl_vco;
  6351. else if (IS_PINEVIEW(dev))
  6352. vco_table = pnv_vco;
  6353. else if (IS_G33(dev))
  6354. vco_table = blb_vco;
  6355. else
  6356. return 0;
  6357. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  6358. vco = vco_table[tmp & 0x7];
  6359. if (vco == 0)
  6360. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  6361. else
  6362. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  6363. return vco;
  6364. }
  6365. static int gm45_get_display_clock_speed(struct drm_device *dev)
  6366. {
  6367. struct pci_dev *pdev = dev->pdev;
  6368. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  6369. uint16_t tmp = 0;
  6370. pci_read_config_word(pdev, GCFGC, &tmp);
  6371. cdclk_sel = (tmp >> 12) & 0x1;
  6372. switch (vco) {
  6373. case 2666667:
  6374. case 4000000:
  6375. case 5333333:
  6376. return cdclk_sel ? 333333 : 222222;
  6377. case 3200000:
  6378. return cdclk_sel ? 320000 : 228571;
  6379. default:
  6380. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  6381. return 222222;
  6382. }
  6383. }
  6384. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  6385. {
  6386. struct pci_dev *pdev = dev->pdev;
  6387. static const uint8_t div_3200[] = { 16, 10, 8 };
  6388. static const uint8_t div_4000[] = { 20, 12, 10 };
  6389. static const uint8_t div_5333[] = { 24, 16, 14 };
  6390. const uint8_t *div_table;
  6391. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  6392. uint16_t tmp = 0;
  6393. pci_read_config_word(pdev, GCFGC, &tmp);
  6394. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  6395. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  6396. goto fail;
  6397. switch (vco) {
  6398. case 3200000:
  6399. div_table = div_3200;
  6400. break;
  6401. case 4000000:
  6402. div_table = div_4000;
  6403. break;
  6404. case 5333333:
  6405. div_table = div_5333;
  6406. break;
  6407. default:
  6408. goto fail;
  6409. }
  6410. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  6411. fail:
  6412. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  6413. return 200000;
  6414. }
  6415. static int g33_get_display_clock_speed(struct drm_device *dev)
  6416. {
  6417. struct pci_dev *pdev = dev->pdev;
  6418. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  6419. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  6420. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  6421. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  6422. const uint8_t *div_table;
  6423. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  6424. uint16_t tmp = 0;
  6425. pci_read_config_word(pdev, GCFGC, &tmp);
  6426. cdclk_sel = (tmp >> 4) & 0x7;
  6427. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  6428. goto fail;
  6429. switch (vco) {
  6430. case 3200000:
  6431. div_table = div_3200;
  6432. break;
  6433. case 4000000:
  6434. div_table = div_4000;
  6435. break;
  6436. case 4800000:
  6437. div_table = div_4800;
  6438. break;
  6439. case 5333333:
  6440. div_table = div_5333;
  6441. break;
  6442. default:
  6443. goto fail;
  6444. }
  6445. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  6446. fail:
  6447. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  6448. return 190476;
  6449. }
  6450. static void
  6451. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  6452. {
  6453. while (*num > DATA_LINK_M_N_MASK ||
  6454. *den > DATA_LINK_M_N_MASK) {
  6455. *num >>= 1;
  6456. *den >>= 1;
  6457. }
  6458. }
  6459. static void compute_m_n(unsigned int m, unsigned int n,
  6460. uint32_t *ret_m, uint32_t *ret_n)
  6461. {
  6462. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  6463. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  6464. intel_reduce_m_n_ratio(ret_m, ret_n);
  6465. }
  6466. void
  6467. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  6468. int pixel_clock, int link_clock,
  6469. struct intel_link_m_n *m_n)
  6470. {
  6471. m_n->tu = 64;
  6472. compute_m_n(bits_per_pixel * pixel_clock,
  6473. link_clock * nlanes * 8,
  6474. &m_n->gmch_m, &m_n->gmch_n);
  6475. compute_m_n(pixel_clock, link_clock,
  6476. &m_n->link_m, &m_n->link_n);
  6477. }
  6478. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  6479. {
  6480. if (i915.panel_use_ssc >= 0)
  6481. return i915.panel_use_ssc != 0;
  6482. return dev_priv->vbt.lvds_use_ssc
  6483. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  6484. }
  6485. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  6486. {
  6487. return (1 << dpll->n) << 16 | dpll->m2;
  6488. }
  6489. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  6490. {
  6491. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  6492. }
  6493. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  6494. struct intel_crtc_state *crtc_state,
  6495. struct dpll *reduced_clock)
  6496. {
  6497. struct drm_device *dev = crtc->base.dev;
  6498. u32 fp, fp2 = 0;
  6499. if (IS_PINEVIEW(dev)) {
  6500. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  6501. if (reduced_clock)
  6502. fp2 = pnv_dpll_compute_fp(reduced_clock);
  6503. } else {
  6504. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6505. if (reduced_clock)
  6506. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6507. }
  6508. crtc_state->dpll_hw_state.fp0 = fp;
  6509. crtc->lowfreq_avail = false;
  6510. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6511. reduced_clock) {
  6512. crtc_state->dpll_hw_state.fp1 = fp2;
  6513. crtc->lowfreq_avail = true;
  6514. } else {
  6515. crtc_state->dpll_hw_state.fp1 = fp;
  6516. }
  6517. }
  6518. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  6519. pipe)
  6520. {
  6521. u32 reg_val;
  6522. /*
  6523. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  6524. * and set it to a reasonable value instead.
  6525. */
  6526. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6527. reg_val &= 0xffffff00;
  6528. reg_val |= 0x00000030;
  6529. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6530. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6531. reg_val &= 0x8cffffff;
  6532. reg_val = 0x8c000000;
  6533. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6534. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6535. reg_val &= 0xffffff00;
  6536. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6537. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6538. reg_val &= 0x00ffffff;
  6539. reg_val |= 0xb0000000;
  6540. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6541. }
  6542. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  6543. struct intel_link_m_n *m_n)
  6544. {
  6545. struct drm_device *dev = crtc->base.dev;
  6546. struct drm_i915_private *dev_priv = to_i915(dev);
  6547. int pipe = crtc->pipe;
  6548. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6549. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  6550. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  6551. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  6552. }
  6553. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6554. struct intel_link_m_n *m_n,
  6555. struct intel_link_m_n *m2_n2)
  6556. {
  6557. struct drm_device *dev = crtc->base.dev;
  6558. struct drm_i915_private *dev_priv = to_i915(dev);
  6559. int pipe = crtc->pipe;
  6560. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6561. if (INTEL_INFO(dev)->gen >= 5) {
  6562. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6563. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6564. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6565. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6566. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6567. * for gen < 8) and if DRRS is supported (to make sure the
  6568. * registers are not unnecessarily accessed).
  6569. */
  6570. if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
  6571. INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
  6572. I915_WRITE(PIPE_DATA_M2(transcoder),
  6573. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6574. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6575. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6576. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6577. }
  6578. } else {
  6579. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6580. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6581. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6582. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6583. }
  6584. }
  6585. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6586. {
  6587. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6588. if (m_n == M1_N1) {
  6589. dp_m_n = &crtc->config->dp_m_n;
  6590. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6591. } else if (m_n == M2_N2) {
  6592. /*
  6593. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6594. * needs to be programmed into M1_N1.
  6595. */
  6596. dp_m_n = &crtc->config->dp_m2_n2;
  6597. } else {
  6598. DRM_ERROR("Unsupported divider value\n");
  6599. return;
  6600. }
  6601. if (crtc->config->has_pch_encoder)
  6602. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6603. else
  6604. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6605. }
  6606. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6607. struct intel_crtc_state *pipe_config)
  6608. {
  6609. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  6610. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6611. if (crtc->pipe != PIPE_A)
  6612. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6613. /* DPLL not used with DSI, but still need the rest set up */
  6614. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  6615. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  6616. DPLL_EXT_BUFFER_ENABLE_VLV;
  6617. pipe_config->dpll_hw_state.dpll_md =
  6618. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6619. }
  6620. static void chv_compute_dpll(struct intel_crtc *crtc,
  6621. struct intel_crtc_state *pipe_config)
  6622. {
  6623. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  6624. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6625. if (crtc->pipe != PIPE_A)
  6626. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6627. /* DPLL not used with DSI, but still need the rest set up */
  6628. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  6629. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  6630. pipe_config->dpll_hw_state.dpll_md =
  6631. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6632. }
  6633. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6634. const struct intel_crtc_state *pipe_config)
  6635. {
  6636. struct drm_device *dev = crtc->base.dev;
  6637. struct drm_i915_private *dev_priv = to_i915(dev);
  6638. enum pipe pipe = crtc->pipe;
  6639. u32 mdiv;
  6640. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6641. u32 coreclk, reg_val;
  6642. /* Enable Refclk */
  6643. I915_WRITE(DPLL(pipe),
  6644. pipe_config->dpll_hw_state.dpll &
  6645. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  6646. /* No need to actually set up the DPLL with DSI */
  6647. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6648. return;
  6649. mutex_lock(&dev_priv->sb_lock);
  6650. bestn = pipe_config->dpll.n;
  6651. bestm1 = pipe_config->dpll.m1;
  6652. bestm2 = pipe_config->dpll.m2;
  6653. bestp1 = pipe_config->dpll.p1;
  6654. bestp2 = pipe_config->dpll.p2;
  6655. /* See eDP HDMI DPIO driver vbios notes doc */
  6656. /* PLL B needs special handling */
  6657. if (pipe == PIPE_B)
  6658. vlv_pllb_recal_opamp(dev_priv, pipe);
  6659. /* Set up Tx target for periodic Rcomp update */
  6660. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6661. /* Disable target IRef on PLL */
  6662. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6663. reg_val &= 0x00ffffff;
  6664. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6665. /* Disable fast lock */
  6666. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6667. /* Set idtafcrecal before PLL is enabled */
  6668. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6669. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6670. mdiv |= ((bestn << DPIO_N_SHIFT));
  6671. mdiv |= (1 << DPIO_K_SHIFT);
  6672. /*
  6673. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6674. * but we don't support that).
  6675. * Note: don't use the DAC post divider as it seems unstable.
  6676. */
  6677. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6678. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6679. mdiv |= DPIO_ENABLE_CALIBRATION;
  6680. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6681. /* Set HBR and RBR LPF coefficients */
  6682. if (pipe_config->port_clock == 162000 ||
  6683. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
  6684. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
  6685. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6686. 0x009f0003);
  6687. else
  6688. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6689. 0x00d0000f);
  6690. if (intel_crtc_has_dp_encoder(pipe_config)) {
  6691. /* Use SSC source */
  6692. if (pipe == PIPE_A)
  6693. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6694. 0x0df40000);
  6695. else
  6696. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6697. 0x0df70000);
  6698. } else { /* HDMI or VGA */
  6699. /* Use bend source */
  6700. if (pipe == PIPE_A)
  6701. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6702. 0x0df70000);
  6703. else
  6704. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6705. 0x0df40000);
  6706. }
  6707. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6708. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6709. if (intel_crtc_has_dp_encoder(crtc->config))
  6710. coreclk |= 0x01000000;
  6711. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6712. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6713. mutex_unlock(&dev_priv->sb_lock);
  6714. }
  6715. static void chv_prepare_pll(struct intel_crtc *crtc,
  6716. const struct intel_crtc_state *pipe_config)
  6717. {
  6718. struct drm_device *dev = crtc->base.dev;
  6719. struct drm_i915_private *dev_priv = to_i915(dev);
  6720. enum pipe pipe = crtc->pipe;
  6721. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6722. u32 loopfilter, tribuf_calcntr;
  6723. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6724. u32 dpio_val;
  6725. int vco;
  6726. /* Enable Refclk and SSC */
  6727. I915_WRITE(DPLL(pipe),
  6728. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6729. /* No need to actually set up the DPLL with DSI */
  6730. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6731. return;
  6732. bestn = pipe_config->dpll.n;
  6733. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6734. bestm1 = pipe_config->dpll.m1;
  6735. bestm2 = pipe_config->dpll.m2 >> 22;
  6736. bestp1 = pipe_config->dpll.p1;
  6737. bestp2 = pipe_config->dpll.p2;
  6738. vco = pipe_config->dpll.vco;
  6739. dpio_val = 0;
  6740. loopfilter = 0;
  6741. mutex_lock(&dev_priv->sb_lock);
  6742. /* p1 and p2 divider */
  6743. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6744. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6745. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6746. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6747. 1 << DPIO_CHV_K_DIV_SHIFT);
  6748. /* Feedback post-divider - m2 */
  6749. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6750. /* Feedback refclk divider - n and m1 */
  6751. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6752. DPIO_CHV_M1_DIV_BY_2 |
  6753. 1 << DPIO_CHV_N_DIV_SHIFT);
  6754. /* M2 fraction division */
  6755. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6756. /* M2 fraction division enable */
  6757. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6758. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6759. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6760. if (bestm2_frac)
  6761. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6762. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6763. /* Program digital lock detect threshold */
  6764. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6765. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6766. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6767. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6768. if (!bestm2_frac)
  6769. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6770. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6771. /* Loop filter */
  6772. if (vco == 5400000) {
  6773. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6774. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6775. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6776. tribuf_calcntr = 0x9;
  6777. } else if (vco <= 6200000) {
  6778. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6779. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6780. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6781. tribuf_calcntr = 0x9;
  6782. } else if (vco <= 6480000) {
  6783. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6784. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6785. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6786. tribuf_calcntr = 0x8;
  6787. } else {
  6788. /* Not supported. Apply the same limits as in the max case */
  6789. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6790. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6791. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6792. tribuf_calcntr = 0;
  6793. }
  6794. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6795. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6796. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6797. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6798. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6799. /* AFC Recal */
  6800. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6801. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6802. DPIO_AFC_RECAL);
  6803. mutex_unlock(&dev_priv->sb_lock);
  6804. }
  6805. /**
  6806. * vlv_force_pll_on - forcibly enable just the PLL
  6807. * @dev_priv: i915 private structure
  6808. * @pipe: pipe PLL to enable
  6809. * @dpll: PLL configuration
  6810. *
  6811. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6812. * in cases where we need the PLL enabled even when @pipe is not going to
  6813. * be enabled.
  6814. */
  6815. int vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
  6816. const struct dpll *dpll)
  6817. {
  6818. struct intel_crtc *crtc =
  6819. to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
  6820. struct intel_crtc_state *pipe_config;
  6821. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  6822. if (!pipe_config)
  6823. return -ENOMEM;
  6824. pipe_config->base.crtc = &crtc->base;
  6825. pipe_config->pixel_multiplier = 1;
  6826. pipe_config->dpll = *dpll;
  6827. if (IS_CHERRYVIEW(to_i915(dev))) {
  6828. chv_compute_dpll(crtc, pipe_config);
  6829. chv_prepare_pll(crtc, pipe_config);
  6830. chv_enable_pll(crtc, pipe_config);
  6831. } else {
  6832. vlv_compute_dpll(crtc, pipe_config);
  6833. vlv_prepare_pll(crtc, pipe_config);
  6834. vlv_enable_pll(crtc, pipe_config);
  6835. }
  6836. kfree(pipe_config);
  6837. return 0;
  6838. }
  6839. /**
  6840. * vlv_force_pll_off - forcibly disable just the PLL
  6841. * @dev_priv: i915 private structure
  6842. * @pipe: pipe PLL to disable
  6843. *
  6844. * Disable the PLL for @pipe. To be used in cases where we need
  6845. * the PLL enabled even when @pipe is not going to be enabled.
  6846. */
  6847. void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
  6848. {
  6849. if (IS_CHERRYVIEW(to_i915(dev)))
  6850. chv_disable_pll(to_i915(dev), pipe);
  6851. else
  6852. vlv_disable_pll(to_i915(dev), pipe);
  6853. }
  6854. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6855. struct intel_crtc_state *crtc_state,
  6856. struct dpll *reduced_clock)
  6857. {
  6858. struct drm_device *dev = crtc->base.dev;
  6859. struct drm_i915_private *dev_priv = to_i915(dev);
  6860. u32 dpll;
  6861. struct dpll *clock = &crtc_state->dpll;
  6862. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6863. dpll = DPLL_VGA_MODE_DIS;
  6864. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  6865. dpll |= DPLLB_MODE_LVDS;
  6866. else
  6867. dpll |= DPLLB_MODE_DAC_SERIAL;
  6868. if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) || IS_G33(dev_priv)) {
  6869. dpll |= (crtc_state->pixel_multiplier - 1)
  6870. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6871. }
  6872. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6873. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  6874. dpll |= DPLL_SDVO_HIGH_SPEED;
  6875. if (intel_crtc_has_dp_encoder(crtc_state))
  6876. dpll |= DPLL_SDVO_HIGH_SPEED;
  6877. /* compute bitmask from p1 value */
  6878. if (IS_PINEVIEW(dev))
  6879. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6880. else {
  6881. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6882. if (IS_G4X(dev_priv) && reduced_clock)
  6883. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6884. }
  6885. switch (clock->p2) {
  6886. case 5:
  6887. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6888. break;
  6889. case 7:
  6890. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6891. break;
  6892. case 10:
  6893. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6894. break;
  6895. case 14:
  6896. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6897. break;
  6898. }
  6899. if (INTEL_INFO(dev)->gen >= 4)
  6900. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6901. if (crtc_state->sdvo_tv_clock)
  6902. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6903. else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6904. intel_panel_use_ssc(dev_priv))
  6905. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6906. else
  6907. dpll |= PLL_REF_INPUT_DREFCLK;
  6908. dpll |= DPLL_VCO_ENABLE;
  6909. crtc_state->dpll_hw_state.dpll = dpll;
  6910. if (INTEL_INFO(dev)->gen >= 4) {
  6911. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6912. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6913. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6914. }
  6915. }
  6916. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6917. struct intel_crtc_state *crtc_state,
  6918. struct dpll *reduced_clock)
  6919. {
  6920. struct drm_device *dev = crtc->base.dev;
  6921. struct drm_i915_private *dev_priv = to_i915(dev);
  6922. u32 dpll;
  6923. struct dpll *clock = &crtc_state->dpll;
  6924. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6925. dpll = DPLL_VGA_MODE_DIS;
  6926. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6927. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6928. } else {
  6929. if (clock->p1 == 2)
  6930. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6931. else
  6932. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6933. if (clock->p2 == 4)
  6934. dpll |= PLL_P2_DIVIDE_BY_4;
  6935. }
  6936. if (!IS_I830(dev_priv) &&
  6937. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
  6938. dpll |= DPLL_DVO_2X_MODE;
  6939. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6940. intel_panel_use_ssc(dev_priv))
  6941. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6942. else
  6943. dpll |= PLL_REF_INPUT_DREFCLK;
  6944. dpll |= DPLL_VCO_ENABLE;
  6945. crtc_state->dpll_hw_state.dpll = dpll;
  6946. }
  6947. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6948. {
  6949. struct drm_device *dev = intel_crtc->base.dev;
  6950. struct drm_i915_private *dev_priv = to_i915(dev);
  6951. enum pipe pipe = intel_crtc->pipe;
  6952. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6953. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  6954. uint32_t crtc_vtotal, crtc_vblank_end;
  6955. int vsyncshift = 0;
  6956. /* We need to be careful not to changed the adjusted mode, for otherwise
  6957. * the hw state checker will get angry at the mismatch. */
  6958. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6959. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6960. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6961. /* the chip adds 2 halflines automatically */
  6962. crtc_vtotal -= 1;
  6963. crtc_vblank_end -= 1;
  6964. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6965. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6966. else
  6967. vsyncshift = adjusted_mode->crtc_hsync_start -
  6968. adjusted_mode->crtc_htotal / 2;
  6969. if (vsyncshift < 0)
  6970. vsyncshift += adjusted_mode->crtc_htotal;
  6971. }
  6972. if (INTEL_INFO(dev)->gen > 3)
  6973. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6974. I915_WRITE(HTOTAL(cpu_transcoder),
  6975. (adjusted_mode->crtc_hdisplay - 1) |
  6976. ((adjusted_mode->crtc_htotal - 1) << 16));
  6977. I915_WRITE(HBLANK(cpu_transcoder),
  6978. (adjusted_mode->crtc_hblank_start - 1) |
  6979. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6980. I915_WRITE(HSYNC(cpu_transcoder),
  6981. (adjusted_mode->crtc_hsync_start - 1) |
  6982. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6983. I915_WRITE(VTOTAL(cpu_transcoder),
  6984. (adjusted_mode->crtc_vdisplay - 1) |
  6985. ((crtc_vtotal - 1) << 16));
  6986. I915_WRITE(VBLANK(cpu_transcoder),
  6987. (adjusted_mode->crtc_vblank_start - 1) |
  6988. ((crtc_vblank_end - 1) << 16));
  6989. I915_WRITE(VSYNC(cpu_transcoder),
  6990. (adjusted_mode->crtc_vsync_start - 1) |
  6991. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6992. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6993. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6994. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6995. * bits. */
  6996. if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
  6997. (pipe == PIPE_B || pipe == PIPE_C))
  6998. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6999. }
  7000. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  7001. {
  7002. struct drm_device *dev = intel_crtc->base.dev;
  7003. struct drm_i915_private *dev_priv = to_i915(dev);
  7004. enum pipe pipe = intel_crtc->pipe;
  7005. /* pipesrc controls the size that is scaled from, which should
  7006. * always be the user's requested size.
  7007. */
  7008. I915_WRITE(PIPESRC(pipe),
  7009. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  7010. (intel_crtc->config->pipe_src_h - 1));
  7011. }
  7012. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  7013. struct intel_crtc_state *pipe_config)
  7014. {
  7015. struct drm_device *dev = crtc->base.dev;
  7016. struct drm_i915_private *dev_priv = to_i915(dev);
  7017. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  7018. uint32_t tmp;
  7019. tmp = I915_READ(HTOTAL(cpu_transcoder));
  7020. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  7021. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  7022. tmp = I915_READ(HBLANK(cpu_transcoder));
  7023. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  7024. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  7025. tmp = I915_READ(HSYNC(cpu_transcoder));
  7026. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  7027. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  7028. tmp = I915_READ(VTOTAL(cpu_transcoder));
  7029. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  7030. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  7031. tmp = I915_READ(VBLANK(cpu_transcoder));
  7032. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  7033. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  7034. tmp = I915_READ(VSYNC(cpu_transcoder));
  7035. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  7036. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  7037. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  7038. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  7039. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  7040. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  7041. }
  7042. }
  7043. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  7044. struct intel_crtc_state *pipe_config)
  7045. {
  7046. struct drm_device *dev = crtc->base.dev;
  7047. struct drm_i915_private *dev_priv = to_i915(dev);
  7048. u32 tmp;
  7049. tmp = I915_READ(PIPESRC(crtc->pipe));
  7050. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  7051. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  7052. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  7053. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  7054. }
  7055. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  7056. struct intel_crtc_state *pipe_config)
  7057. {
  7058. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  7059. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  7060. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  7061. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  7062. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  7063. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  7064. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  7065. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  7066. mode->flags = pipe_config->base.adjusted_mode.flags;
  7067. mode->type = DRM_MODE_TYPE_DRIVER;
  7068. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  7069. mode->flags |= pipe_config->base.adjusted_mode.flags;
  7070. mode->hsync = drm_mode_hsync(mode);
  7071. mode->vrefresh = drm_mode_vrefresh(mode);
  7072. drm_mode_set_name(mode);
  7073. }
  7074. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  7075. {
  7076. struct drm_device *dev = intel_crtc->base.dev;
  7077. struct drm_i915_private *dev_priv = to_i915(dev);
  7078. uint32_t pipeconf;
  7079. pipeconf = 0;
  7080. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  7081. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  7082. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  7083. if (intel_crtc->config->double_wide)
  7084. pipeconf |= PIPECONF_DOUBLE_WIDE;
  7085. /* only g4x and later have fancy bpc/dither controls */
  7086. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  7087. IS_CHERRYVIEW(dev_priv)) {
  7088. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  7089. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  7090. pipeconf |= PIPECONF_DITHER_EN |
  7091. PIPECONF_DITHER_TYPE_SP;
  7092. switch (intel_crtc->config->pipe_bpp) {
  7093. case 18:
  7094. pipeconf |= PIPECONF_6BPC;
  7095. break;
  7096. case 24:
  7097. pipeconf |= PIPECONF_8BPC;
  7098. break;
  7099. case 30:
  7100. pipeconf |= PIPECONF_10BPC;
  7101. break;
  7102. default:
  7103. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7104. BUG();
  7105. }
  7106. }
  7107. if (HAS_PIPE_CXSR(dev)) {
  7108. if (intel_crtc->lowfreq_avail) {
  7109. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  7110. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  7111. } else {
  7112. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  7113. }
  7114. }
  7115. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  7116. if (INTEL_INFO(dev)->gen < 4 ||
  7117. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  7118. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  7119. else
  7120. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  7121. } else
  7122. pipeconf |= PIPECONF_PROGRESSIVE;
  7123. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  7124. intel_crtc->config->limited_color_range)
  7125. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  7126. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  7127. POSTING_READ(PIPECONF(intel_crtc->pipe));
  7128. }
  7129. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  7130. struct intel_crtc_state *crtc_state)
  7131. {
  7132. struct drm_device *dev = crtc->base.dev;
  7133. struct drm_i915_private *dev_priv = to_i915(dev);
  7134. const struct intel_limit *limit;
  7135. int refclk = 48000;
  7136. memset(&crtc_state->dpll_hw_state, 0,
  7137. sizeof(crtc_state->dpll_hw_state));
  7138. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7139. if (intel_panel_use_ssc(dev_priv)) {
  7140. refclk = dev_priv->vbt.lvds_ssc_freq;
  7141. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7142. }
  7143. limit = &intel_limits_i8xx_lvds;
  7144. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
  7145. limit = &intel_limits_i8xx_dvo;
  7146. } else {
  7147. limit = &intel_limits_i8xx_dac;
  7148. }
  7149. if (!crtc_state->clock_set &&
  7150. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7151. refclk, NULL, &crtc_state->dpll)) {
  7152. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7153. return -EINVAL;
  7154. }
  7155. i8xx_compute_dpll(crtc, crtc_state, NULL);
  7156. return 0;
  7157. }
  7158. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  7159. struct intel_crtc_state *crtc_state)
  7160. {
  7161. struct drm_device *dev = crtc->base.dev;
  7162. struct drm_i915_private *dev_priv = to_i915(dev);
  7163. const struct intel_limit *limit;
  7164. int refclk = 96000;
  7165. memset(&crtc_state->dpll_hw_state, 0,
  7166. sizeof(crtc_state->dpll_hw_state));
  7167. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7168. if (intel_panel_use_ssc(dev_priv)) {
  7169. refclk = dev_priv->vbt.lvds_ssc_freq;
  7170. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7171. }
  7172. if (intel_is_dual_link_lvds(dev))
  7173. limit = &intel_limits_g4x_dual_channel_lvds;
  7174. else
  7175. limit = &intel_limits_g4x_single_channel_lvds;
  7176. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  7177. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  7178. limit = &intel_limits_g4x_hdmi;
  7179. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  7180. limit = &intel_limits_g4x_sdvo;
  7181. } else {
  7182. /* The option is for other outputs */
  7183. limit = &intel_limits_i9xx_sdvo;
  7184. }
  7185. if (!crtc_state->clock_set &&
  7186. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7187. refclk, NULL, &crtc_state->dpll)) {
  7188. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7189. return -EINVAL;
  7190. }
  7191. i9xx_compute_dpll(crtc, crtc_state, NULL);
  7192. return 0;
  7193. }
  7194. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  7195. struct intel_crtc_state *crtc_state)
  7196. {
  7197. struct drm_device *dev = crtc->base.dev;
  7198. struct drm_i915_private *dev_priv = to_i915(dev);
  7199. const struct intel_limit *limit;
  7200. int refclk = 96000;
  7201. memset(&crtc_state->dpll_hw_state, 0,
  7202. sizeof(crtc_state->dpll_hw_state));
  7203. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7204. if (intel_panel_use_ssc(dev_priv)) {
  7205. refclk = dev_priv->vbt.lvds_ssc_freq;
  7206. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7207. }
  7208. limit = &intel_limits_pineview_lvds;
  7209. } else {
  7210. limit = &intel_limits_pineview_sdvo;
  7211. }
  7212. if (!crtc_state->clock_set &&
  7213. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7214. refclk, NULL, &crtc_state->dpll)) {
  7215. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7216. return -EINVAL;
  7217. }
  7218. i9xx_compute_dpll(crtc, crtc_state, NULL);
  7219. return 0;
  7220. }
  7221. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  7222. struct intel_crtc_state *crtc_state)
  7223. {
  7224. struct drm_device *dev = crtc->base.dev;
  7225. struct drm_i915_private *dev_priv = to_i915(dev);
  7226. const struct intel_limit *limit;
  7227. int refclk = 96000;
  7228. memset(&crtc_state->dpll_hw_state, 0,
  7229. sizeof(crtc_state->dpll_hw_state));
  7230. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7231. if (intel_panel_use_ssc(dev_priv)) {
  7232. refclk = dev_priv->vbt.lvds_ssc_freq;
  7233. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7234. }
  7235. limit = &intel_limits_i9xx_lvds;
  7236. } else {
  7237. limit = &intel_limits_i9xx_sdvo;
  7238. }
  7239. if (!crtc_state->clock_set &&
  7240. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7241. refclk, NULL, &crtc_state->dpll)) {
  7242. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7243. return -EINVAL;
  7244. }
  7245. i9xx_compute_dpll(crtc, crtc_state, NULL);
  7246. return 0;
  7247. }
  7248. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  7249. struct intel_crtc_state *crtc_state)
  7250. {
  7251. int refclk = 100000;
  7252. const struct intel_limit *limit = &intel_limits_chv;
  7253. memset(&crtc_state->dpll_hw_state, 0,
  7254. sizeof(crtc_state->dpll_hw_state));
  7255. if (!crtc_state->clock_set &&
  7256. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7257. refclk, NULL, &crtc_state->dpll)) {
  7258. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7259. return -EINVAL;
  7260. }
  7261. chv_compute_dpll(crtc, crtc_state);
  7262. return 0;
  7263. }
  7264. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  7265. struct intel_crtc_state *crtc_state)
  7266. {
  7267. int refclk = 100000;
  7268. const struct intel_limit *limit = &intel_limits_vlv;
  7269. memset(&crtc_state->dpll_hw_state, 0,
  7270. sizeof(crtc_state->dpll_hw_state));
  7271. if (!crtc_state->clock_set &&
  7272. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7273. refclk, NULL, &crtc_state->dpll)) {
  7274. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7275. return -EINVAL;
  7276. }
  7277. vlv_compute_dpll(crtc, crtc_state);
  7278. return 0;
  7279. }
  7280. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  7281. struct intel_crtc_state *pipe_config)
  7282. {
  7283. struct drm_device *dev = crtc->base.dev;
  7284. struct drm_i915_private *dev_priv = to_i915(dev);
  7285. uint32_t tmp;
  7286. if (INTEL_GEN(dev_priv) <= 3 &&
  7287. (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
  7288. return;
  7289. tmp = I915_READ(PFIT_CONTROL);
  7290. if (!(tmp & PFIT_ENABLE))
  7291. return;
  7292. /* Check whether the pfit is attached to our pipe. */
  7293. if (INTEL_INFO(dev)->gen < 4) {
  7294. if (crtc->pipe != PIPE_B)
  7295. return;
  7296. } else {
  7297. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  7298. return;
  7299. }
  7300. pipe_config->gmch_pfit.control = tmp;
  7301. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  7302. }
  7303. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  7304. struct intel_crtc_state *pipe_config)
  7305. {
  7306. struct drm_device *dev = crtc->base.dev;
  7307. struct drm_i915_private *dev_priv = to_i915(dev);
  7308. int pipe = pipe_config->cpu_transcoder;
  7309. struct dpll clock;
  7310. u32 mdiv;
  7311. int refclk = 100000;
  7312. /* In case of DSI, DPLL will not be used */
  7313. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  7314. return;
  7315. mutex_lock(&dev_priv->sb_lock);
  7316. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  7317. mutex_unlock(&dev_priv->sb_lock);
  7318. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  7319. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  7320. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  7321. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  7322. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  7323. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  7324. }
  7325. static void
  7326. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  7327. struct intel_initial_plane_config *plane_config)
  7328. {
  7329. struct drm_device *dev = crtc->base.dev;
  7330. struct drm_i915_private *dev_priv = to_i915(dev);
  7331. u32 val, base, offset;
  7332. int pipe = crtc->pipe, plane = crtc->plane;
  7333. int fourcc, pixel_format;
  7334. unsigned int aligned_height;
  7335. struct drm_framebuffer *fb;
  7336. struct intel_framebuffer *intel_fb;
  7337. val = I915_READ(DSPCNTR(plane));
  7338. if (!(val & DISPLAY_PLANE_ENABLE))
  7339. return;
  7340. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7341. if (!intel_fb) {
  7342. DRM_DEBUG_KMS("failed to alloc fb\n");
  7343. return;
  7344. }
  7345. fb = &intel_fb->base;
  7346. if (INTEL_INFO(dev)->gen >= 4) {
  7347. if (val & DISPPLANE_TILED) {
  7348. plane_config->tiling = I915_TILING_X;
  7349. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7350. }
  7351. }
  7352. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7353. fourcc = i9xx_format_to_fourcc(pixel_format);
  7354. fb->pixel_format = fourcc;
  7355. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7356. if (INTEL_INFO(dev)->gen >= 4) {
  7357. if (plane_config->tiling)
  7358. offset = I915_READ(DSPTILEOFF(plane));
  7359. else
  7360. offset = I915_READ(DSPLINOFF(plane));
  7361. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  7362. } else {
  7363. base = I915_READ(DSPADDR(plane));
  7364. }
  7365. plane_config->base = base;
  7366. val = I915_READ(PIPESRC(pipe));
  7367. fb->width = ((val >> 16) & 0xfff) + 1;
  7368. fb->height = ((val >> 0) & 0xfff) + 1;
  7369. val = I915_READ(DSPSTRIDE(pipe));
  7370. fb->pitches[0] = val & 0xffffffc0;
  7371. aligned_height = intel_fb_align_height(dev, fb->height,
  7372. fb->pixel_format,
  7373. fb->modifier[0]);
  7374. plane_config->size = fb->pitches[0] * aligned_height;
  7375. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7376. pipe_name(pipe), plane, fb->width, fb->height,
  7377. fb->bits_per_pixel, base, fb->pitches[0],
  7378. plane_config->size);
  7379. plane_config->fb = intel_fb;
  7380. }
  7381. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  7382. struct intel_crtc_state *pipe_config)
  7383. {
  7384. struct drm_device *dev = crtc->base.dev;
  7385. struct drm_i915_private *dev_priv = to_i915(dev);
  7386. int pipe = pipe_config->cpu_transcoder;
  7387. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  7388. struct dpll clock;
  7389. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  7390. int refclk = 100000;
  7391. /* In case of DSI, DPLL will not be used */
  7392. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  7393. return;
  7394. mutex_lock(&dev_priv->sb_lock);
  7395. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  7396. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  7397. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  7398. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  7399. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  7400. mutex_unlock(&dev_priv->sb_lock);
  7401. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  7402. clock.m2 = (pll_dw0 & 0xff) << 22;
  7403. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  7404. clock.m2 |= pll_dw2 & 0x3fffff;
  7405. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  7406. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  7407. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  7408. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  7409. }
  7410. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  7411. struct intel_crtc_state *pipe_config)
  7412. {
  7413. struct drm_device *dev = crtc->base.dev;
  7414. struct drm_i915_private *dev_priv = to_i915(dev);
  7415. enum intel_display_power_domain power_domain;
  7416. uint32_t tmp;
  7417. bool ret;
  7418. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7419. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7420. return false;
  7421. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7422. pipe_config->shared_dpll = NULL;
  7423. ret = false;
  7424. tmp = I915_READ(PIPECONF(crtc->pipe));
  7425. if (!(tmp & PIPECONF_ENABLE))
  7426. goto out;
  7427. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  7428. IS_CHERRYVIEW(dev_priv)) {
  7429. switch (tmp & PIPECONF_BPC_MASK) {
  7430. case PIPECONF_6BPC:
  7431. pipe_config->pipe_bpp = 18;
  7432. break;
  7433. case PIPECONF_8BPC:
  7434. pipe_config->pipe_bpp = 24;
  7435. break;
  7436. case PIPECONF_10BPC:
  7437. pipe_config->pipe_bpp = 30;
  7438. break;
  7439. default:
  7440. break;
  7441. }
  7442. }
  7443. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  7444. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  7445. pipe_config->limited_color_range = true;
  7446. if (INTEL_INFO(dev)->gen < 4)
  7447. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  7448. intel_get_pipe_timings(crtc, pipe_config);
  7449. intel_get_pipe_src_size(crtc, pipe_config);
  7450. i9xx_get_pfit_config(crtc, pipe_config);
  7451. if (INTEL_INFO(dev)->gen >= 4) {
  7452. /* No way to read it out on pipes B and C */
  7453. if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
  7454. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  7455. else
  7456. tmp = I915_READ(DPLL_MD(crtc->pipe));
  7457. pipe_config->pixel_multiplier =
  7458. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  7459. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  7460. pipe_config->dpll_hw_state.dpll_md = tmp;
  7461. } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  7462. IS_G33(dev_priv)) {
  7463. tmp = I915_READ(DPLL(crtc->pipe));
  7464. pipe_config->pixel_multiplier =
  7465. ((tmp & SDVO_MULTIPLIER_MASK)
  7466. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  7467. } else {
  7468. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  7469. * port and will be fixed up in the encoder->get_config
  7470. * function. */
  7471. pipe_config->pixel_multiplier = 1;
  7472. }
  7473. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  7474. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  7475. /*
  7476. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  7477. * on 830. Filter it out here so that we don't
  7478. * report errors due to that.
  7479. */
  7480. if (IS_I830(dev_priv))
  7481. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  7482. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  7483. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  7484. } else {
  7485. /* Mask out read-only status bits. */
  7486. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  7487. DPLL_PORTC_READY_MASK |
  7488. DPLL_PORTB_READY_MASK);
  7489. }
  7490. if (IS_CHERRYVIEW(dev_priv))
  7491. chv_crtc_clock_get(crtc, pipe_config);
  7492. else if (IS_VALLEYVIEW(dev_priv))
  7493. vlv_crtc_clock_get(crtc, pipe_config);
  7494. else
  7495. i9xx_crtc_clock_get(crtc, pipe_config);
  7496. /*
  7497. * Normally the dotclock is filled in by the encoder .get_config()
  7498. * but in case the pipe is enabled w/o any ports we need a sane
  7499. * default.
  7500. */
  7501. pipe_config->base.adjusted_mode.crtc_clock =
  7502. pipe_config->port_clock / pipe_config->pixel_multiplier;
  7503. ret = true;
  7504. out:
  7505. intel_display_power_put(dev_priv, power_domain);
  7506. return ret;
  7507. }
  7508. static void ironlake_init_pch_refclk(struct drm_device *dev)
  7509. {
  7510. struct drm_i915_private *dev_priv = to_i915(dev);
  7511. struct intel_encoder *encoder;
  7512. int i;
  7513. u32 val, final;
  7514. bool has_lvds = false;
  7515. bool has_cpu_edp = false;
  7516. bool has_panel = false;
  7517. bool has_ck505 = false;
  7518. bool can_ssc = false;
  7519. bool using_ssc_source = false;
  7520. /* We need to take the global config into account */
  7521. for_each_intel_encoder(dev, encoder) {
  7522. switch (encoder->type) {
  7523. case INTEL_OUTPUT_LVDS:
  7524. has_panel = true;
  7525. has_lvds = true;
  7526. break;
  7527. case INTEL_OUTPUT_EDP:
  7528. has_panel = true;
  7529. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  7530. has_cpu_edp = true;
  7531. break;
  7532. default:
  7533. break;
  7534. }
  7535. }
  7536. if (HAS_PCH_IBX(dev_priv)) {
  7537. has_ck505 = dev_priv->vbt.display_clock_mode;
  7538. can_ssc = has_ck505;
  7539. } else {
  7540. has_ck505 = false;
  7541. can_ssc = true;
  7542. }
  7543. /* Check if any DPLLs are using the SSC source */
  7544. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  7545. u32 temp = I915_READ(PCH_DPLL(i));
  7546. if (!(temp & DPLL_VCO_ENABLE))
  7547. continue;
  7548. if ((temp & PLL_REF_INPUT_MASK) ==
  7549. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  7550. using_ssc_source = true;
  7551. break;
  7552. }
  7553. }
  7554. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  7555. has_panel, has_lvds, has_ck505, using_ssc_source);
  7556. /* Ironlake: try to setup display ref clock before DPLL
  7557. * enabling. This is only under driver's control after
  7558. * PCH B stepping, previous chipset stepping should be
  7559. * ignoring this setting.
  7560. */
  7561. val = I915_READ(PCH_DREF_CONTROL);
  7562. /* As we must carefully and slowly disable/enable each source in turn,
  7563. * compute the final state we want first and check if we need to
  7564. * make any changes at all.
  7565. */
  7566. final = val;
  7567. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  7568. if (has_ck505)
  7569. final |= DREF_NONSPREAD_CK505_ENABLE;
  7570. else
  7571. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  7572. final &= ~DREF_SSC_SOURCE_MASK;
  7573. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7574. final &= ~DREF_SSC1_ENABLE;
  7575. if (has_panel) {
  7576. final |= DREF_SSC_SOURCE_ENABLE;
  7577. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7578. final |= DREF_SSC1_ENABLE;
  7579. if (has_cpu_edp) {
  7580. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7581. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7582. else
  7583. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7584. } else
  7585. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7586. } else if (using_ssc_source) {
  7587. final |= DREF_SSC_SOURCE_ENABLE;
  7588. final |= DREF_SSC1_ENABLE;
  7589. }
  7590. if (final == val)
  7591. return;
  7592. /* Always enable nonspread source */
  7593. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  7594. if (has_ck505)
  7595. val |= DREF_NONSPREAD_CK505_ENABLE;
  7596. else
  7597. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  7598. if (has_panel) {
  7599. val &= ~DREF_SSC_SOURCE_MASK;
  7600. val |= DREF_SSC_SOURCE_ENABLE;
  7601. /* SSC must be turned on before enabling the CPU output */
  7602. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7603. DRM_DEBUG_KMS("Using SSC on panel\n");
  7604. val |= DREF_SSC1_ENABLE;
  7605. } else
  7606. val &= ~DREF_SSC1_ENABLE;
  7607. /* Get SSC going before enabling the outputs */
  7608. I915_WRITE(PCH_DREF_CONTROL, val);
  7609. POSTING_READ(PCH_DREF_CONTROL);
  7610. udelay(200);
  7611. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7612. /* Enable CPU source on CPU attached eDP */
  7613. if (has_cpu_edp) {
  7614. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7615. DRM_DEBUG_KMS("Using SSC on eDP\n");
  7616. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7617. } else
  7618. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7619. } else
  7620. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7621. I915_WRITE(PCH_DREF_CONTROL, val);
  7622. POSTING_READ(PCH_DREF_CONTROL);
  7623. udelay(200);
  7624. } else {
  7625. DRM_DEBUG_KMS("Disabling CPU source output\n");
  7626. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7627. /* Turn off CPU output */
  7628. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7629. I915_WRITE(PCH_DREF_CONTROL, val);
  7630. POSTING_READ(PCH_DREF_CONTROL);
  7631. udelay(200);
  7632. if (!using_ssc_source) {
  7633. DRM_DEBUG_KMS("Disabling SSC source\n");
  7634. /* Turn off the SSC source */
  7635. val &= ~DREF_SSC_SOURCE_MASK;
  7636. val |= DREF_SSC_SOURCE_DISABLE;
  7637. /* Turn off SSC1 */
  7638. val &= ~DREF_SSC1_ENABLE;
  7639. I915_WRITE(PCH_DREF_CONTROL, val);
  7640. POSTING_READ(PCH_DREF_CONTROL);
  7641. udelay(200);
  7642. }
  7643. }
  7644. BUG_ON(val != final);
  7645. }
  7646. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  7647. {
  7648. uint32_t tmp;
  7649. tmp = I915_READ(SOUTH_CHICKEN2);
  7650. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  7651. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7652. if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
  7653. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  7654. DRM_ERROR("FDI mPHY reset assert timeout\n");
  7655. tmp = I915_READ(SOUTH_CHICKEN2);
  7656. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  7657. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7658. if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
  7659. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  7660. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  7661. }
  7662. /* WaMPhyProgramming:hsw */
  7663. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  7664. {
  7665. uint32_t tmp;
  7666. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  7667. tmp &= ~(0xFF << 24);
  7668. tmp |= (0x12 << 24);
  7669. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  7670. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  7671. tmp |= (1 << 11);
  7672. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  7673. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  7674. tmp |= (1 << 11);
  7675. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  7676. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  7677. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7678. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7679. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7680. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7681. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7682. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7683. tmp &= ~(7 << 13);
  7684. tmp |= (5 << 13);
  7685. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7686. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7687. tmp &= ~(7 << 13);
  7688. tmp |= (5 << 13);
  7689. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7690. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7691. tmp &= ~0xFF;
  7692. tmp |= 0x1C;
  7693. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7694. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7695. tmp &= ~0xFF;
  7696. tmp |= 0x1C;
  7697. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7698. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7699. tmp &= ~(0xFF << 16);
  7700. tmp |= (0x1C << 16);
  7701. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7702. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7703. tmp &= ~(0xFF << 16);
  7704. tmp |= (0x1C << 16);
  7705. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7706. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7707. tmp |= (1 << 27);
  7708. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7709. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7710. tmp |= (1 << 27);
  7711. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7712. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7713. tmp &= ~(0xF << 28);
  7714. tmp |= (4 << 28);
  7715. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7716. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7717. tmp &= ~(0xF << 28);
  7718. tmp |= (4 << 28);
  7719. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7720. }
  7721. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7722. * Programming" based on the parameters passed:
  7723. * - Sequence to enable CLKOUT_DP
  7724. * - Sequence to enable CLKOUT_DP without spread
  7725. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7726. */
  7727. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7728. bool with_fdi)
  7729. {
  7730. struct drm_i915_private *dev_priv = to_i915(dev);
  7731. uint32_t reg, tmp;
  7732. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7733. with_spread = true;
  7734. if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
  7735. with_fdi, "LP PCH doesn't have FDI\n"))
  7736. with_fdi = false;
  7737. mutex_lock(&dev_priv->sb_lock);
  7738. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7739. tmp &= ~SBI_SSCCTL_DISABLE;
  7740. tmp |= SBI_SSCCTL_PATHALT;
  7741. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7742. udelay(24);
  7743. if (with_spread) {
  7744. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7745. tmp &= ~SBI_SSCCTL_PATHALT;
  7746. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7747. if (with_fdi) {
  7748. lpt_reset_fdi_mphy(dev_priv);
  7749. lpt_program_fdi_mphy(dev_priv);
  7750. }
  7751. }
  7752. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  7753. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7754. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7755. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7756. mutex_unlock(&dev_priv->sb_lock);
  7757. }
  7758. /* Sequence to disable CLKOUT_DP */
  7759. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7760. {
  7761. struct drm_i915_private *dev_priv = to_i915(dev);
  7762. uint32_t reg, tmp;
  7763. mutex_lock(&dev_priv->sb_lock);
  7764. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  7765. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7766. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7767. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7768. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7769. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7770. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7771. tmp |= SBI_SSCCTL_PATHALT;
  7772. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7773. udelay(32);
  7774. }
  7775. tmp |= SBI_SSCCTL_DISABLE;
  7776. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7777. }
  7778. mutex_unlock(&dev_priv->sb_lock);
  7779. }
  7780. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  7781. static const uint16_t sscdivintphase[] = {
  7782. [BEND_IDX( 50)] = 0x3B23,
  7783. [BEND_IDX( 45)] = 0x3B23,
  7784. [BEND_IDX( 40)] = 0x3C23,
  7785. [BEND_IDX( 35)] = 0x3C23,
  7786. [BEND_IDX( 30)] = 0x3D23,
  7787. [BEND_IDX( 25)] = 0x3D23,
  7788. [BEND_IDX( 20)] = 0x3E23,
  7789. [BEND_IDX( 15)] = 0x3E23,
  7790. [BEND_IDX( 10)] = 0x3F23,
  7791. [BEND_IDX( 5)] = 0x3F23,
  7792. [BEND_IDX( 0)] = 0x0025,
  7793. [BEND_IDX( -5)] = 0x0025,
  7794. [BEND_IDX(-10)] = 0x0125,
  7795. [BEND_IDX(-15)] = 0x0125,
  7796. [BEND_IDX(-20)] = 0x0225,
  7797. [BEND_IDX(-25)] = 0x0225,
  7798. [BEND_IDX(-30)] = 0x0325,
  7799. [BEND_IDX(-35)] = 0x0325,
  7800. [BEND_IDX(-40)] = 0x0425,
  7801. [BEND_IDX(-45)] = 0x0425,
  7802. [BEND_IDX(-50)] = 0x0525,
  7803. };
  7804. /*
  7805. * Bend CLKOUT_DP
  7806. * steps -50 to 50 inclusive, in steps of 5
  7807. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  7808. * change in clock period = -(steps / 10) * 5.787 ps
  7809. */
  7810. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  7811. {
  7812. uint32_t tmp;
  7813. int idx = BEND_IDX(steps);
  7814. if (WARN_ON(steps % 5 != 0))
  7815. return;
  7816. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  7817. return;
  7818. mutex_lock(&dev_priv->sb_lock);
  7819. if (steps % 10 != 0)
  7820. tmp = 0xAAAAAAAB;
  7821. else
  7822. tmp = 0x00000000;
  7823. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  7824. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  7825. tmp &= 0xffff0000;
  7826. tmp |= sscdivintphase[idx];
  7827. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  7828. mutex_unlock(&dev_priv->sb_lock);
  7829. }
  7830. #undef BEND_IDX
  7831. static void lpt_init_pch_refclk(struct drm_device *dev)
  7832. {
  7833. struct intel_encoder *encoder;
  7834. bool has_vga = false;
  7835. for_each_intel_encoder(dev, encoder) {
  7836. switch (encoder->type) {
  7837. case INTEL_OUTPUT_ANALOG:
  7838. has_vga = true;
  7839. break;
  7840. default:
  7841. break;
  7842. }
  7843. }
  7844. if (has_vga) {
  7845. lpt_bend_clkout_dp(to_i915(dev), 0);
  7846. lpt_enable_clkout_dp(dev, true, true);
  7847. } else {
  7848. lpt_disable_clkout_dp(dev);
  7849. }
  7850. }
  7851. /*
  7852. * Initialize reference clocks when the driver loads
  7853. */
  7854. void intel_init_pch_refclk(struct drm_device *dev)
  7855. {
  7856. struct drm_i915_private *dev_priv = to_i915(dev);
  7857. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
  7858. ironlake_init_pch_refclk(dev);
  7859. else if (HAS_PCH_LPT(dev_priv))
  7860. lpt_init_pch_refclk(dev);
  7861. }
  7862. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7863. {
  7864. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7865. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7866. int pipe = intel_crtc->pipe;
  7867. uint32_t val;
  7868. val = 0;
  7869. switch (intel_crtc->config->pipe_bpp) {
  7870. case 18:
  7871. val |= PIPECONF_6BPC;
  7872. break;
  7873. case 24:
  7874. val |= PIPECONF_8BPC;
  7875. break;
  7876. case 30:
  7877. val |= PIPECONF_10BPC;
  7878. break;
  7879. case 36:
  7880. val |= PIPECONF_12BPC;
  7881. break;
  7882. default:
  7883. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7884. BUG();
  7885. }
  7886. if (intel_crtc->config->dither)
  7887. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7888. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7889. val |= PIPECONF_INTERLACED_ILK;
  7890. else
  7891. val |= PIPECONF_PROGRESSIVE;
  7892. if (intel_crtc->config->limited_color_range)
  7893. val |= PIPECONF_COLOR_RANGE_SELECT;
  7894. I915_WRITE(PIPECONF(pipe), val);
  7895. POSTING_READ(PIPECONF(pipe));
  7896. }
  7897. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7898. {
  7899. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7900. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7901. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7902. u32 val = 0;
  7903. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  7904. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7905. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7906. val |= PIPECONF_INTERLACED_ILK;
  7907. else
  7908. val |= PIPECONF_PROGRESSIVE;
  7909. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7910. POSTING_READ(PIPECONF(cpu_transcoder));
  7911. }
  7912. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  7913. {
  7914. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7915. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7916. if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
  7917. u32 val = 0;
  7918. switch (intel_crtc->config->pipe_bpp) {
  7919. case 18:
  7920. val |= PIPEMISC_DITHER_6_BPC;
  7921. break;
  7922. case 24:
  7923. val |= PIPEMISC_DITHER_8_BPC;
  7924. break;
  7925. case 30:
  7926. val |= PIPEMISC_DITHER_10_BPC;
  7927. break;
  7928. case 36:
  7929. val |= PIPEMISC_DITHER_12_BPC;
  7930. break;
  7931. default:
  7932. /* Case prevented by pipe_config_set_bpp. */
  7933. BUG();
  7934. }
  7935. if (intel_crtc->config->dither)
  7936. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7937. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  7938. }
  7939. }
  7940. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7941. {
  7942. /*
  7943. * Account for spread spectrum to avoid
  7944. * oversubscribing the link. Max center spread
  7945. * is 2.5%; use 5% for safety's sake.
  7946. */
  7947. u32 bps = target_clock * bpp * 21 / 20;
  7948. return DIV_ROUND_UP(bps, link_bw * 8);
  7949. }
  7950. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7951. {
  7952. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7953. }
  7954. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7955. struct intel_crtc_state *crtc_state,
  7956. struct dpll *reduced_clock)
  7957. {
  7958. struct drm_crtc *crtc = &intel_crtc->base;
  7959. struct drm_device *dev = crtc->dev;
  7960. struct drm_i915_private *dev_priv = to_i915(dev);
  7961. u32 dpll, fp, fp2;
  7962. int factor;
  7963. /* Enable autotuning of the PLL clock (if permissible) */
  7964. factor = 21;
  7965. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7966. if ((intel_panel_use_ssc(dev_priv) &&
  7967. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7968. (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
  7969. factor = 25;
  7970. } else if (crtc_state->sdvo_tv_clock)
  7971. factor = 20;
  7972. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7973. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7974. fp |= FP_CB_TUNE;
  7975. if (reduced_clock) {
  7976. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  7977. if (reduced_clock->m < factor * reduced_clock->n)
  7978. fp2 |= FP_CB_TUNE;
  7979. } else {
  7980. fp2 = fp;
  7981. }
  7982. dpll = 0;
  7983. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  7984. dpll |= DPLLB_MODE_LVDS;
  7985. else
  7986. dpll |= DPLLB_MODE_DAC_SERIAL;
  7987. dpll |= (crtc_state->pixel_multiplier - 1)
  7988. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7989. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  7990. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  7991. dpll |= DPLL_SDVO_HIGH_SPEED;
  7992. if (intel_crtc_has_dp_encoder(crtc_state))
  7993. dpll |= DPLL_SDVO_HIGH_SPEED;
  7994. /*
  7995. * The high speed IO clock is only really required for
  7996. * SDVO/HDMI/DP, but we also enable it for CRT to make it
  7997. * possible to share the DPLL between CRT and HDMI. Enabling
  7998. * the clock needlessly does no real harm, except use up a
  7999. * bit of power potentially.
  8000. *
  8001. * We'll limit this to IVB with 3 pipes, since it has only two
  8002. * DPLLs and so DPLL sharing is the only way to get three pipes
  8003. * driving PCH ports at the same time. On SNB we could do this,
  8004. * and potentially avoid enabling the second DPLL, but it's not
  8005. * clear if it''s a win or loss power wise. No point in doing
  8006. * this on ILK at all since it has a fixed DPLL<->pipe mapping.
  8007. */
  8008. if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
  8009. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
  8010. dpll |= DPLL_SDVO_HIGH_SPEED;
  8011. /* compute bitmask from p1 value */
  8012. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  8013. /* also FPA1 */
  8014. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  8015. switch (crtc_state->dpll.p2) {
  8016. case 5:
  8017. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  8018. break;
  8019. case 7:
  8020. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  8021. break;
  8022. case 10:
  8023. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  8024. break;
  8025. case 14:
  8026. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  8027. break;
  8028. }
  8029. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  8030. intel_panel_use_ssc(dev_priv))
  8031. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  8032. else
  8033. dpll |= PLL_REF_INPUT_DREFCLK;
  8034. dpll |= DPLL_VCO_ENABLE;
  8035. crtc_state->dpll_hw_state.dpll = dpll;
  8036. crtc_state->dpll_hw_state.fp0 = fp;
  8037. crtc_state->dpll_hw_state.fp1 = fp2;
  8038. }
  8039. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  8040. struct intel_crtc_state *crtc_state)
  8041. {
  8042. struct drm_device *dev = crtc->base.dev;
  8043. struct drm_i915_private *dev_priv = to_i915(dev);
  8044. struct dpll reduced_clock;
  8045. bool has_reduced_clock = false;
  8046. struct intel_shared_dpll *pll;
  8047. const struct intel_limit *limit;
  8048. int refclk = 120000;
  8049. memset(&crtc_state->dpll_hw_state, 0,
  8050. sizeof(crtc_state->dpll_hw_state));
  8051. crtc->lowfreq_avail = false;
  8052. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  8053. if (!crtc_state->has_pch_encoder)
  8054. return 0;
  8055. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  8056. if (intel_panel_use_ssc(dev_priv)) {
  8057. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  8058. dev_priv->vbt.lvds_ssc_freq);
  8059. refclk = dev_priv->vbt.lvds_ssc_freq;
  8060. }
  8061. if (intel_is_dual_link_lvds(dev)) {
  8062. if (refclk == 100000)
  8063. limit = &intel_limits_ironlake_dual_lvds_100m;
  8064. else
  8065. limit = &intel_limits_ironlake_dual_lvds;
  8066. } else {
  8067. if (refclk == 100000)
  8068. limit = &intel_limits_ironlake_single_lvds_100m;
  8069. else
  8070. limit = &intel_limits_ironlake_single_lvds;
  8071. }
  8072. } else {
  8073. limit = &intel_limits_ironlake_dac;
  8074. }
  8075. if (!crtc_state->clock_set &&
  8076. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  8077. refclk, NULL, &crtc_state->dpll)) {
  8078. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  8079. return -EINVAL;
  8080. }
  8081. ironlake_compute_dpll(crtc, crtc_state,
  8082. has_reduced_clock ? &reduced_clock : NULL);
  8083. pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
  8084. if (pll == NULL) {
  8085. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  8086. pipe_name(crtc->pipe));
  8087. return -EINVAL;
  8088. }
  8089. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  8090. has_reduced_clock)
  8091. crtc->lowfreq_avail = true;
  8092. return 0;
  8093. }
  8094. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  8095. struct intel_link_m_n *m_n)
  8096. {
  8097. struct drm_device *dev = crtc->base.dev;
  8098. struct drm_i915_private *dev_priv = to_i915(dev);
  8099. enum pipe pipe = crtc->pipe;
  8100. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  8101. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  8102. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  8103. & ~TU_SIZE_MASK;
  8104. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  8105. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  8106. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8107. }
  8108. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  8109. enum transcoder transcoder,
  8110. struct intel_link_m_n *m_n,
  8111. struct intel_link_m_n *m2_n2)
  8112. {
  8113. struct drm_device *dev = crtc->base.dev;
  8114. struct drm_i915_private *dev_priv = to_i915(dev);
  8115. enum pipe pipe = crtc->pipe;
  8116. if (INTEL_INFO(dev)->gen >= 5) {
  8117. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  8118. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  8119. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  8120. & ~TU_SIZE_MASK;
  8121. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  8122. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  8123. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8124. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  8125. * gen < 8) and if DRRS is supported (to make sure the
  8126. * registers are not unnecessarily read).
  8127. */
  8128. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  8129. crtc->config->has_drrs) {
  8130. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  8131. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  8132. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  8133. & ~TU_SIZE_MASK;
  8134. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  8135. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  8136. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8137. }
  8138. } else {
  8139. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  8140. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  8141. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  8142. & ~TU_SIZE_MASK;
  8143. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  8144. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  8145. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8146. }
  8147. }
  8148. void intel_dp_get_m_n(struct intel_crtc *crtc,
  8149. struct intel_crtc_state *pipe_config)
  8150. {
  8151. if (pipe_config->has_pch_encoder)
  8152. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  8153. else
  8154. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  8155. &pipe_config->dp_m_n,
  8156. &pipe_config->dp_m2_n2);
  8157. }
  8158. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  8159. struct intel_crtc_state *pipe_config)
  8160. {
  8161. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  8162. &pipe_config->fdi_m_n, NULL);
  8163. }
  8164. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  8165. struct intel_crtc_state *pipe_config)
  8166. {
  8167. struct drm_device *dev = crtc->base.dev;
  8168. struct drm_i915_private *dev_priv = to_i915(dev);
  8169. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  8170. uint32_t ps_ctrl = 0;
  8171. int id = -1;
  8172. int i;
  8173. /* find scaler attached to this pipe */
  8174. for (i = 0; i < crtc->num_scalers; i++) {
  8175. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  8176. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  8177. id = i;
  8178. pipe_config->pch_pfit.enabled = true;
  8179. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  8180. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  8181. break;
  8182. }
  8183. }
  8184. scaler_state->scaler_id = id;
  8185. if (id >= 0) {
  8186. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  8187. } else {
  8188. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8189. }
  8190. }
  8191. static void
  8192. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  8193. struct intel_initial_plane_config *plane_config)
  8194. {
  8195. struct drm_device *dev = crtc->base.dev;
  8196. struct drm_i915_private *dev_priv = to_i915(dev);
  8197. u32 val, base, offset, stride_mult, tiling;
  8198. int pipe = crtc->pipe;
  8199. int fourcc, pixel_format;
  8200. unsigned int aligned_height;
  8201. struct drm_framebuffer *fb;
  8202. struct intel_framebuffer *intel_fb;
  8203. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8204. if (!intel_fb) {
  8205. DRM_DEBUG_KMS("failed to alloc fb\n");
  8206. return;
  8207. }
  8208. fb = &intel_fb->base;
  8209. val = I915_READ(PLANE_CTL(pipe, 0));
  8210. if (!(val & PLANE_CTL_ENABLE))
  8211. goto error;
  8212. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  8213. fourcc = skl_format_to_fourcc(pixel_format,
  8214. val & PLANE_CTL_ORDER_RGBX,
  8215. val & PLANE_CTL_ALPHA_MASK);
  8216. fb->pixel_format = fourcc;
  8217. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  8218. tiling = val & PLANE_CTL_TILED_MASK;
  8219. switch (tiling) {
  8220. case PLANE_CTL_TILED_LINEAR:
  8221. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  8222. break;
  8223. case PLANE_CTL_TILED_X:
  8224. plane_config->tiling = I915_TILING_X;
  8225. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  8226. break;
  8227. case PLANE_CTL_TILED_Y:
  8228. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  8229. break;
  8230. case PLANE_CTL_TILED_YF:
  8231. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  8232. break;
  8233. default:
  8234. MISSING_CASE(tiling);
  8235. goto error;
  8236. }
  8237. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  8238. plane_config->base = base;
  8239. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  8240. val = I915_READ(PLANE_SIZE(pipe, 0));
  8241. fb->height = ((val >> 16) & 0xfff) + 1;
  8242. fb->width = ((val >> 0) & 0x1fff) + 1;
  8243. val = I915_READ(PLANE_STRIDE(pipe, 0));
  8244. stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  8245. fb->pixel_format);
  8246. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  8247. aligned_height = intel_fb_align_height(dev, fb->height,
  8248. fb->pixel_format,
  8249. fb->modifier[0]);
  8250. plane_config->size = fb->pitches[0] * aligned_height;
  8251. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  8252. pipe_name(pipe), fb->width, fb->height,
  8253. fb->bits_per_pixel, base, fb->pitches[0],
  8254. plane_config->size);
  8255. plane_config->fb = intel_fb;
  8256. return;
  8257. error:
  8258. kfree(intel_fb);
  8259. }
  8260. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  8261. struct intel_crtc_state *pipe_config)
  8262. {
  8263. struct drm_device *dev = crtc->base.dev;
  8264. struct drm_i915_private *dev_priv = to_i915(dev);
  8265. uint32_t tmp;
  8266. tmp = I915_READ(PF_CTL(crtc->pipe));
  8267. if (tmp & PF_ENABLE) {
  8268. pipe_config->pch_pfit.enabled = true;
  8269. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  8270. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  8271. /* We currently do not free assignements of panel fitters on
  8272. * ivb/hsw (since we don't use the higher upscaling modes which
  8273. * differentiates them) so just WARN about this case for now. */
  8274. if (IS_GEN7(dev_priv)) {
  8275. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  8276. PF_PIPE_SEL_IVB(crtc->pipe));
  8277. }
  8278. }
  8279. }
  8280. static void
  8281. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  8282. struct intel_initial_plane_config *plane_config)
  8283. {
  8284. struct drm_device *dev = crtc->base.dev;
  8285. struct drm_i915_private *dev_priv = to_i915(dev);
  8286. u32 val, base, offset;
  8287. int pipe = crtc->pipe;
  8288. int fourcc, pixel_format;
  8289. unsigned int aligned_height;
  8290. struct drm_framebuffer *fb;
  8291. struct intel_framebuffer *intel_fb;
  8292. val = I915_READ(DSPCNTR(pipe));
  8293. if (!(val & DISPLAY_PLANE_ENABLE))
  8294. return;
  8295. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8296. if (!intel_fb) {
  8297. DRM_DEBUG_KMS("failed to alloc fb\n");
  8298. return;
  8299. }
  8300. fb = &intel_fb->base;
  8301. if (INTEL_INFO(dev)->gen >= 4) {
  8302. if (val & DISPPLANE_TILED) {
  8303. plane_config->tiling = I915_TILING_X;
  8304. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  8305. }
  8306. }
  8307. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  8308. fourcc = i9xx_format_to_fourcc(pixel_format);
  8309. fb->pixel_format = fourcc;
  8310. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  8311. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  8312. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  8313. offset = I915_READ(DSPOFFSET(pipe));
  8314. } else {
  8315. if (plane_config->tiling)
  8316. offset = I915_READ(DSPTILEOFF(pipe));
  8317. else
  8318. offset = I915_READ(DSPLINOFF(pipe));
  8319. }
  8320. plane_config->base = base;
  8321. val = I915_READ(PIPESRC(pipe));
  8322. fb->width = ((val >> 16) & 0xfff) + 1;
  8323. fb->height = ((val >> 0) & 0xfff) + 1;
  8324. val = I915_READ(DSPSTRIDE(pipe));
  8325. fb->pitches[0] = val & 0xffffffc0;
  8326. aligned_height = intel_fb_align_height(dev, fb->height,
  8327. fb->pixel_format,
  8328. fb->modifier[0]);
  8329. plane_config->size = fb->pitches[0] * aligned_height;
  8330. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  8331. pipe_name(pipe), fb->width, fb->height,
  8332. fb->bits_per_pixel, base, fb->pitches[0],
  8333. plane_config->size);
  8334. plane_config->fb = intel_fb;
  8335. }
  8336. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  8337. struct intel_crtc_state *pipe_config)
  8338. {
  8339. struct drm_device *dev = crtc->base.dev;
  8340. struct drm_i915_private *dev_priv = to_i915(dev);
  8341. enum intel_display_power_domain power_domain;
  8342. uint32_t tmp;
  8343. bool ret;
  8344. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  8345. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8346. return false;
  8347. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8348. pipe_config->shared_dpll = NULL;
  8349. ret = false;
  8350. tmp = I915_READ(PIPECONF(crtc->pipe));
  8351. if (!(tmp & PIPECONF_ENABLE))
  8352. goto out;
  8353. switch (tmp & PIPECONF_BPC_MASK) {
  8354. case PIPECONF_6BPC:
  8355. pipe_config->pipe_bpp = 18;
  8356. break;
  8357. case PIPECONF_8BPC:
  8358. pipe_config->pipe_bpp = 24;
  8359. break;
  8360. case PIPECONF_10BPC:
  8361. pipe_config->pipe_bpp = 30;
  8362. break;
  8363. case PIPECONF_12BPC:
  8364. pipe_config->pipe_bpp = 36;
  8365. break;
  8366. default:
  8367. break;
  8368. }
  8369. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  8370. pipe_config->limited_color_range = true;
  8371. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  8372. struct intel_shared_dpll *pll;
  8373. enum intel_dpll_id pll_id;
  8374. pipe_config->has_pch_encoder = true;
  8375. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  8376. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8377. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8378. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8379. if (HAS_PCH_IBX(dev_priv)) {
  8380. /*
  8381. * The pipe->pch transcoder and pch transcoder->pll
  8382. * mapping is fixed.
  8383. */
  8384. pll_id = (enum intel_dpll_id) crtc->pipe;
  8385. } else {
  8386. tmp = I915_READ(PCH_DPLL_SEL);
  8387. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  8388. pll_id = DPLL_ID_PCH_PLL_B;
  8389. else
  8390. pll_id= DPLL_ID_PCH_PLL_A;
  8391. }
  8392. pipe_config->shared_dpll =
  8393. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  8394. pll = pipe_config->shared_dpll;
  8395. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  8396. &pipe_config->dpll_hw_state));
  8397. tmp = pipe_config->dpll_hw_state.dpll;
  8398. pipe_config->pixel_multiplier =
  8399. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  8400. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  8401. ironlake_pch_clock_get(crtc, pipe_config);
  8402. } else {
  8403. pipe_config->pixel_multiplier = 1;
  8404. }
  8405. intel_get_pipe_timings(crtc, pipe_config);
  8406. intel_get_pipe_src_size(crtc, pipe_config);
  8407. ironlake_get_pfit_config(crtc, pipe_config);
  8408. ret = true;
  8409. out:
  8410. intel_display_power_put(dev_priv, power_domain);
  8411. return ret;
  8412. }
  8413. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  8414. {
  8415. struct drm_device *dev = &dev_priv->drm;
  8416. struct intel_crtc *crtc;
  8417. for_each_intel_crtc(dev, crtc)
  8418. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  8419. pipe_name(crtc->pipe));
  8420. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  8421. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  8422. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  8423. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  8424. I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
  8425. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  8426. "CPU PWM1 enabled\n");
  8427. if (IS_HASWELL(dev_priv))
  8428. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  8429. "CPU PWM2 enabled\n");
  8430. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  8431. "PCH PWM1 enabled\n");
  8432. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  8433. "Utility pin enabled\n");
  8434. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  8435. /*
  8436. * In theory we can still leave IRQs enabled, as long as only the HPD
  8437. * interrupts remain enabled. We used to check for that, but since it's
  8438. * gen-specific and since we only disable LCPLL after we fully disable
  8439. * the interrupts, the check below should be enough.
  8440. */
  8441. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  8442. }
  8443. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  8444. {
  8445. if (IS_HASWELL(dev_priv))
  8446. return I915_READ(D_COMP_HSW);
  8447. else
  8448. return I915_READ(D_COMP_BDW);
  8449. }
  8450. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  8451. {
  8452. if (IS_HASWELL(dev_priv)) {
  8453. mutex_lock(&dev_priv->rps.hw_lock);
  8454. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  8455. val))
  8456. DRM_DEBUG_KMS("Failed to write to D_COMP\n");
  8457. mutex_unlock(&dev_priv->rps.hw_lock);
  8458. } else {
  8459. I915_WRITE(D_COMP_BDW, val);
  8460. POSTING_READ(D_COMP_BDW);
  8461. }
  8462. }
  8463. /*
  8464. * This function implements pieces of two sequences from BSpec:
  8465. * - Sequence for display software to disable LCPLL
  8466. * - Sequence for display software to allow package C8+
  8467. * The steps implemented here are just the steps that actually touch the LCPLL
  8468. * register. Callers should take care of disabling all the display engine
  8469. * functions, doing the mode unset, fixing interrupts, etc.
  8470. */
  8471. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  8472. bool switch_to_fclk, bool allow_power_down)
  8473. {
  8474. uint32_t val;
  8475. assert_can_disable_lcpll(dev_priv);
  8476. val = I915_READ(LCPLL_CTL);
  8477. if (switch_to_fclk) {
  8478. val |= LCPLL_CD_SOURCE_FCLK;
  8479. I915_WRITE(LCPLL_CTL, val);
  8480. if (wait_for_us(I915_READ(LCPLL_CTL) &
  8481. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8482. DRM_ERROR("Switching to FCLK failed\n");
  8483. val = I915_READ(LCPLL_CTL);
  8484. }
  8485. val |= LCPLL_PLL_DISABLE;
  8486. I915_WRITE(LCPLL_CTL, val);
  8487. POSTING_READ(LCPLL_CTL);
  8488. if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
  8489. DRM_ERROR("LCPLL still locked\n");
  8490. val = hsw_read_dcomp(dev_priv);
  8491. val |= D_COMP_COMP_DISABLE;
  8492. hsw_write_dcomp(dev_priv, val);
  8493. ndelay(100);
  8494. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  8495. 1))
  8496. DRM_ERROR("D_COMP RCOMP still in progress\n");
  8497. if (allow_power_down) {
  8498. val = I915_READ(LCPLL_CTL);
  8499. val |= LCPLL_POWER_DOWN_ALLOW;
  8500. I915_WRITE(LCPLL_CTL, val);
  8501. POSTING_READ(LCPLL_CTL);
  8502. }
  8503. }
  8504. /*
  8505. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  8506. * source.
  8507. */
  8508. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  8509. {
  8510. uint32_t val;
  8511. val = I915_READ(LCPLL_CTL);
  8512. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  8513. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  8514. return;
  8515. /*
  8516. * Make sure we're not on PC8 state before disabling PC8, otherwise
  8517. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  8518. */
  8519. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  8520. if (val & LCPLL_POWER_DOWN_ALLOW) {
  8521. val &= ~LCPLL_POWER_DOWN_ALLOW;
  8522. I915_WRITE(LCPLL_CTL, val);
  8523. POSTING_READ(LCPLL_CTL);
  8524. }
  8525. val = hsw_read_dcomp(dev_priv);
  8526. val |= D_COMP_COMP_FORCE;
  8527. val &= ~D_COMP_COMP_DISABLE;
  8528. hsw_write_dcomp(dev_priv, val);
  8529. val = I915_READ(LCPLL_CTL);
  8530. val &= ~LCPLL_PLL_DISABLE;
  8531. I915_WRITE(LCPLL_CTL, val);
  8532. if (intel_wait_for_register(dev_priv,
  8533. LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  8534. 5))
  8535. DRM_ERROR("LCPLL not locked yet\n");
  8536. if (val & LCPLL_CD_SOURCE_FCLK) {
  8537. val = I915_READ(LCPLL_CTL);
  8538. val &= ~LCPLL_CD_SOURCE_FCLK;
  8539. I915_WRITE(LCPLL_CTL, val);
  8540. if (wait_for_us((I915_READ(LCPLL_CTL) &
  8541. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8542. DRM_ERROR("Switching back to LCPLL failed\n");
  8543. }
  8544. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  8545. intel_update_cdclk(&dev_priv->drm);
  8546. }
  8547. /*
  8548. * Package states C8 and deeper are really deep PC states that can only be
  8549. * reached when all the devices on the system allow it, so even if the graphics
  8550. * device allows PC8+, it doesn't mean the system will actually get to these
  8551. * states. Our driver only allows PC8+ when going into runtime PM.
  8552. *
  8553. * The requirements for PC8+ are that all the outputs are disabled, the power
  8554. * well is disabled and most interrupts are disabled, and these are also
  8555. * requirements for runtime PM. When these conditions are met, we manually do
  8556. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  8557. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  8558. * hang the machine.
  8559. *
  8560. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  8561. * the state of some registers, so when we come back from PC8+ we need to
  8562. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  8563. * need to take care of the registers kept by RC6. Notice that this happens even
  8564. * if we don't put the device in PCI D3 state (which is what currently happens
  8565. * because of the runtime PM support).
  8566. *
  8567. * For more, read "Display Sequences for Package C8" on the hardware
  8568. * documentation.
  8569. */
  8570. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  8571. {
  8572. struct drm_device *dev = &dev_priv->drm;
  8573. uint32_t val;
  8574. DRM_DEBUG_KMS("Enabling package C8+\n");
  8575. if (HAS_PCH_LPT_LP(dev_priv)) {
  8576. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8577. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  8578. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8579. }
  8580. lpt_disable_clkout_dp(dev);
  8581. hsw_disable_lcpll(dev_priv, true, true);
  8582. }
  8583. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  8584. {
  8585. struct drm_device *dev = &dev_priv->drm;
  8586. uint32_t val;
  8587. DRM_DEBUG_KMS("Disabling package C8+\n");
  8588. hsw_restore_lcpll(dev_priv);
  8589. lpt_init_pch_refclk(dev);
  8590. if (HAS_PCH_LPT_LP(dev_priv)) {
  8591. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8592. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  8593. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8594. }
  8595. }
  8596. static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8597. {
  8598. struct drm_device *dev = old_state->dev;
  8599. struct intel_atomic_state *old_intel_state =
  8600. to_intel_atomic_state(old_state);
  8601. unsigned int req_cdclk = old_intel_state->dev_cdclk;
  8602. bxt_set_cdclk(to_i915(dev), req_cdclk);
  8603. }
  8604. /* compute the max rate for new configuration */
  8605. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  8606. {
  8607. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8608. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8609. struct drm_crtc *crtc;
  8610. struct drm_crtc_state *cstate;
  8611. struct intel_crtc_state *crtc_state;
  8612. unsigned max_pixel_rate = 0, i;
  8613. enum pipe pipe;
  8614. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  8615. sizeof(intel_state->min_pixclk));
  8616. for_each_crtc_in_state(state, crtc, cstate, i) {
  8617. int pixel_rate;
  8618. crtc_state = to_intel_crtc_state(cstate);
  8619. if (!crtc_state->base.enable) {
  8620. intel_state->min_pixclk[i] = 0;
  8621. continue;
  8622. }
  8623. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  8624. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  8625. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  8626. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  8627. intel_state->min_pixclk[i] = pixel_rate;
  8628. }
  8629. for_each_pipe(dev_priv, pipe)
  8630. max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
  8631. return max_pixel_rate;
  8632. }
  8633. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  8634. {
  8635. struct drm_i915_private *dev_priv = to_i915(dev);
  8636. uint32_t val, data;
  8637. int ret;
  8638. if (WARN((I915_READ(LCPLL_CTL) &
  8639. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  8640. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  8641. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  8642. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  8643. "trying to change cdclk frequency with cdclk not enabled\n"))
  8644. return;
  8645. mutex_lock(&dev_priv->rps.hw_lock);
  8646. ret = sandybridge_pcode_write(dev_priv,
  8647. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  8648. mutex_unlock(&dev_priv->rps.hw_lock);
  8649. if (ret) {
  8650. DRM_ERROR("failed to inform pcode about cdclk change\n");
  8651. return;
  8652. }
  8653. val = I915_READ(LCPLL_CTL);
  8654. val |= LCPLL_CD_SOURCE_FCLK;
  8655. I915_WRITE(LCPLL_CTL, val);
  8656. if (wait_for_us(I915_READ(LCPLL_CTL) &
  8657. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8658. DRM_ERROR("Switching to FCLK failed\n");
  8659. val = I915_READ(LCPLL_CTL);
  8660. val &= ~LCPLL_CLK_FREQ_MASK;
  8661. switch (cdclk) {
  8662. case 450000:
  8663. val |= LCPLL_CLK_FREQ_450;
  8664. data = 0;
  8665. break;
  8666. case 540000:
  8667. val |= LCPLL_CLK_FREQ_54O_BDW;
  8668. data = 1;
  8669. break;
  8670. case 337500:
  8671. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8672. data = 2;
  8673. break;
  8674. case 675000:
  8675. val |= LCPLL_CLK_FREQ_675_BDW;
  8676. data = 3;
  8677. break;
  8678. default:
  8679. WARN(1, "invalid cdclk frequency\n");
  8680. return;
  8681. }
  8682. I915_WRITE(LCPLL_CTL, val);
  8683. val = I915_READ(LCPLL_CTL);
  8684. val &= ~LCPLL_CD_SOURCE_FCLK;
  8685. I915_WRITE(LCPLL_CTL, val);
  8686. if (wait_for_us((I915_READ(LCPLL_CTL) &
  8687. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8688. DRM_ERROR("Switching back to LCPLL failed\n");
  8689. mutex_lock(&dev_priv->rps.hw_lock);
  8690. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8691. mutex_unlock(&dev_priv->rps.hw_lock);
  8692. I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
  8693. intel_update_cdclk(dev);
  8694. WARN(cdclk != dev_priv->cdclk_freq,
  8695. "cdclk requested %d kHz but got %d kHz\n",
  8696. cdclk, dev_priv->cdclk_freq);
  8697. }
  8698. static int broadwell_calc_cdclk(int max_pixclk)
  8699. {
  8700. if (max_pixclk > 540000)
  8701. return 675000;
  8702. else if (max_pixclk > 450000)
  8703. return 540000;
  8704. else if (max_pixclk > 337500)
  8705. return 450000;
  8706. else
  8707. return 337500;
  8708. }
  8709. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8710. {
  8711. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8712. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8713. int max_pixclk = ilk_max_pixel_rate(state);
  8714. int cdclk;
  8715. /*
  8716. * FIXME should also account for plane ratio
  8717. * once 64bpp pixel formats are supported.
  8718. */
  8719. cdclk = broadwell_calc_cdclk(max_pixclk);
  8720. if (cdclk > dev_priv->max_cdclk_freq) {
  8721. DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8722. cdclk, dev_priv->max_cdclk_freq);
  8723. return -EINVAL;
  8724. }
  8725. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8726. if (!intel_state->active_crtcs)
  8727. intel_state->dev_cdclk = broadwell_calc_cdclk(0);
  8728. return 0;
  8729. }
  8730. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8731. {
  8732. struct drm_device *dev = old_state->dev;
  8733. struct intel_atomic_state *old_intel_state =
  8734. to_intel_atomic_state(old_state);
  8735. unsigned req_cdclk = old_intel_state->dev_cdclk;
  8736. broadwell_set_cdclk(dev, req_cdclk);
  8737. }
  8738. static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
  8739. {
  8740. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8741. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8742. const int max_pixclk = ilk_max_pixel_rate(state);
  8743. int vco = intel_state->cdclk_pll_vco;
  8744. int cdclk;
  8745. /*
  8746. * FIXME should also account for plane ratio
  8747. * once 64bpp pixel formats are supported.
  8748. */
  8749. cdclk = skl_calc_cdclk(max_pixclk, vco);
  8750. /*
  8751. * FIXME move the cdclk caclulation to
  8752. * compute_config() so we can fail gracegully.
  8753. */
  8754. if (cdclk > dev_priv->max_cdclk_freq) {
  8755. DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8756. cdclk, dev_priv->max_cdclk_freq);
  8757. cdclk = dev_priv->max_cdclk_freq;
  8758. }
  8759. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8760. if (!intel_state->active_crtcs)
  8761. intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
  8762. return 0;
  8763. }
  8764. static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8765. {
  8766. struct drm_i915_private *dev_priv = to_i915(old_state->dev);
  8767. struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
  8768. unsigned int req_cdclk = intel_state->dev_cdclk;
  8769. unsigned int req_vco = intel_state->cdclk_pll_vco;
  8770. skl_set_cdclk(dev_priv, req_cdclk, req_vco);
  8771. }
  8772. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8773. struct intel_crtc_state *crtc_state)
  8774. {
  8775. if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
  8776. if (!intel_ddi_pll_select(crtc, crtc_state))
  8777. return -EINVAL;
  8778. }
  8779. crtc->lowfreq_avail = false;
  8780. return 0;
  8781. }
  8782. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8783. enum port port,
  8784. struct intel_crtc_state *pipe_config)
  8785. {
  8786. enum intel_dpll_id id;
  8787. switch (port) {
  8788. case PORT_A:
  8789. id = DPLL_ID_SKL_DPLL0;
  8790. break;
  8791. case PORT_B:
  8792. id = DPLL_ID_SKL_DPLL1;
  8793. break;
  8794. case PORT_C:
  8795. id = DPLL_ID_SKL_DPLL2;
  8796. break;
  8797. default:
  8798. DRM_ERROR("Incorrect port type\n");
  8799. return;
  8800. }
  8801. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8802. }
  8803. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8804. enum port port,
  8805. struct intel_crtc_state *pipe_config)
  8806. {
  8807. enum intel_dpll_id id;
  8808. u32 temp;
  8809. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8810. id = temp >> (port * 3 + 1);
  8811. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
  8812. return;
  8813. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8814. }
  8815. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8816. enum port port,
  8817. struct intel_crtc_state *pipe_config)
  8818. {
  8819. enum intel_dpll_id id;
  8820. uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8821. switch (ddi_pll_sel) {
  8822. case PORT_CLK_SEL_WRPLL1:
  8823. id = DPLL_ID_WRPLL1;
  8824. break;
  8825. case PORT_CLK_SEL_WRPLL2:
  8826. id = DPLL_ID_WRPLL2;
  8827. break;
  8828. case PORT_CLK_SEL_SPLL:
  8829. id = DPLL_ID_SPLL;
  8830. break;
  8831. case PORT_CLK_SEL_LCPLL_810:
  8832. id = DPLL_ID_LCPLL_810;
  8833. break;
  8834. case PORT_CLK_SEL_LCPLL_1350:
  8835. id = DPLL_ID_LCPLL_1350;
  8836. break;
  8837. case PORT_CLK_SEL_LCPLL_2700:
  8838. id = DPLL_ID_LCPLL_2700;
  8839. break;
  8840. default:
  8841. MISSING_CASE(ddi_pll_sel);
  8842. /* fall through */
  8843. case PORT_CLK_SEL_NONE:
  8844. return;
  8845. }
  8846. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8847. }
  8848. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  8849. struct intel_crtc_state *pipe_config,
  8850. unsigned long *power_domain_mask)
  8851. {
  8852. struct drm_device *dev = crtc->base.dev;
  8853. struct drm_i915_private *dev_priv = to_i915(dev);
  8854. enum intel_display_power_domain power_domain;
  8855. u32 tmp;
  8856. /*
  8857. * The pipe->transcoder mapping is fixed with the exception of the eDP
  8858. * transcoder handled below.
  8859. */
  8860. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8861. /*
  8862. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  8863. * consistency and less surprising code; it's in always on power).
  8864. */
  8865. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8866. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8867. enum pipe trans_edp_pipe;
  8868. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8869. default:
  8870. WARN(1, "unknown pipe linked to edp transcoder\n");
  8871. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8872. case TRANS_DDI_EDP_INPUT_A_ON:
  8873. trans_edp_pipe = PIPE_A;
  8874. break;
  8875. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8876. trans_edp_pipe = PIPE_B;
  8877. break;
  8878. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8879. trans_edp_pipe = PIPE_C;
  8880. break;
  8881. }
  8882. if (trans_edp_pipe == crtc->pipe)
  8883. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8884. }
  8885. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  8886. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8887. return false;
  8888. *power_domain_mask |= BIT(power_domain);
  8889. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8890. return tmp & PIPECONF_ENABLE;
  8891. }
  8892. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  8893. struct intel_crtc_state *pipe_config,
  8894. unsigned long *power_domain_mask)
  8895. {
  8896. struct drm_device *dev = crtc->base.dev;
  8897. struct drm_i915_private *dev_priv = to_i915(dev);
  8898. enum intel_display_power_domain power_domain;
  8899. enum port port;
  8900. enum transcoder cpu_transcoder;
  8901. u32 tmp;
  8902. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  8903. if (port == PORT_A)
  8904. cpu_transcoder = TRANSCODER_DSI_A;
  8905. else
  8906. cpu_transcoder = TRANSCODER_DSI_C;
  8907. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  8908. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8909. continue;
  8910. *power_domain_mask |= BIT(power_domain);
  8911. /*
  8912. * The PLL needs to be enabled with a valid divider
  8913. * configuration, otherwise accessing DSI registers will hang
  8914. * the machine. See BSpec North Display Engine
  8915. * registers/MIPI[BXT]. We can break out here early, since we
  8916. * need the same DSI PLL to be enabled for both DSI ports.
  8917. */
  8918. if (!intel_dsi_pll_is_enabled(dev_priv))
  8919. break;
  8920. /* XXX: this works for video mode only */
  8921. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  8922. if (!(tmp & DPI_ENABLE))
  8923. continue;
  8924. tmp = I915_READ(MIPI_CTRL(port));
  8925. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  8926. continue;
  8927. pipe_config->cpu_transcoder = cpu_transcoder;
  8928. break;
  8929. }
  8930. return transcoder_is_dsi(pipe_config->cpu_transcoder);
  8931. }
  8932. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8933. struct intel_crtc_state *pipe_config)
  8934. {
  8935. struct drm_device *dev = crtc->base.dev;
  8936. struct drm_i915_private *dev_priv = to_i915(dev);
  8937. struct intel_shared_dpll *pll;
  8938. enum port port;
  8939. uint32_t tmp;
  8940. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8941. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8942. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  8943. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8944. else if (IS_BROXTON(dev_priv))
  8945. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8946. else
  8947. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8948. pll = pipe_config->shared_dpll;
  8949. if (pll) {
  8950. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  8951. &pipe_config->dpll_hw_state));
  8952. }
  8953. /*
  8954. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8955. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8956. * the PCH transcoder is on.
  8957. */
  8958. if (INTEL_INFO(dev)->gen < 9 &&
  8959. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8960. pipe_config->has_pch_encoder = true;
  8961. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8962. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8963. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8964. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8965. }
  8966. }
  8967. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8968. struct intel_crtc_state *pipe_config)
  8969. {
  8970. struct drm_device *dev = crtc->base.dev;
  8971. struct drm_i915_private *dev_priv = to_i915(dev);
  8972. enum intel_display_power_domain power_domain;
  8973. unsigned long power_domain_mask;
  8974. bool active;
  8975. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  8976. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8977. return false;
  8978. power_domain_mask = BIT(power_domain);
  8979. pipe_config->shared_dpll = NULL;
  8980. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  8981. if (IS_BROXTON(dev_priv) &&
  8982. bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
  8983. WARN_ON(active);
  8984. active = true;
  8985. }
  8986. if (!active)
  8987. goto out;
  8988. if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  8989. haswell_get_ddi_port_state(crtc, pipe_config);
  8990. intel_get_pipe_timings(crtc, pipe_config);
  8991. }
  8992. intel_get_pipe_src_size(crtc, pipe_config);
  8993. pipe_config->gamma_mode =
  8994. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  8995. if (INTEL_INFO(dev)->gen >= 9) {
  8996. skl_init_scalers(dev, crtc, pipe_config);
  8997. }
  8998. if (INTEL_INFO(dev)->gen >= 9) {
  8999. pipe_config->scaler_state.scaler_id = -1;
  9000. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  9001. }
  9002. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  9003. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  9004. power_domain_mask |= BIT(power_domain);
  9005. if (INTEL_INFO(dev)->gen >= 9)
  9006. skylake_get_pfit_config(crtc, pipe_config);
  9007. else
  9008. ironlake_get_pfit_config(crtc, pipe_config);
  9009. }
  9010. if (IS_HASWELL(dev_priv))
  9011. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  9012. (I915_READ(IPS_CTL) & IPS_ENABLE);
  9013. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  9014. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  9015. pipe_config->pixel_multiplier =
  9016. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  9017. } else {
  9018. pipe_config->pixel_multiplier = 1;
  9019. }
  9020. out:
  9021. for_each_power_domain(power_domain, power_domain_mask)
  9022. intel_display_power_put(dev_priv, power_domain);
  9023. return active;
  9024. }
  9025. static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
  9026. const struct intel_plane_state *plane_state)
  9027. {
  9028. struct drm_device *dev = crtc->dev;
  9029. struct drm_i915_private *dev_priv = to_i915(dev);
  9030. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9031. uint32_t cntl = 0, size = 0;
  9032. if (plane_state && plane_state->base.visible) {
  9033. unsigned int width = plane_state->base.crtc_w;
  9034. unsigned int height = plane_state->base.crtc_h;
  9035. unsigned int stride = roundup_pow_of_two(width) * 4;
  9036. switch (stride) {
  9037. default:
  9038. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  9039. width, stride);
  9040. stride = 256;
  9041. /* fallthrough */
  9042. case 256:
  9043. case 512:
  9044. case 1024:
  9045. case 2048:
  9046. break;
  9047. }
  9048. cntl |= CURSOR_ENABLE |
  9049. CURSOR_GAMMA_ENABLE |
  9050. CURSOR_FORMAT_ARGB |
  9051. CURSOR_STRIDE(stride);
  9052. size = (height << 12) | width;
  9053. }
  9054. if (intel_crtc->cursor_cntl != 0 &&
  9055. (intel_crtc->cursor_base != base ||
  9056. intel_crtc->cursor_size != size ||
  9057. intel_crtc->cursor_cntl != cntl)) {
  9058. /* On these chipsets we can only modify the base/size/stride
  9059. * whilst the cursor is disabled.
  9060. */
  9061. I915_WRITE(CURCNTR(PIPE_A), 0);
  9062. POSTING_READ(CURCNTR(PIPE_A));
  9063. intel_crtc->cursor_cntl = 0;
  9064. }
  9065. if (intel_crtc->cursor_base != base) {
  9066. I915_WRITE(CURBASE(PIPE_A), base);
  9067. intel_crtc->cursor_base = base;
  9068. }
  9069. if (intel_crtc->cursor_size != size) {
  9070. I915_WRITE(CURSIZE, size);
  9071. intel_crtc->cursor_size = size;
  9072. }
  9073. if (intel_crtc->cursor_cntl != cntl) {
  9074. I915_WRITE(CURCNTR(PIPE_A), cntl);
  9075. POSTING_READ(CURCNTR(PIPE_A));
  9076. intel_crtc->cursor_cntl = cntl;
  9077. }
  9078. }
  9079. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
  9080. const struct intel_plane_state *plane_state)
  9081. {
  9082. struct drm_device *dev = crtc->dev;
  9083. struct drm_i915_private *dev_priv = to_i915(dev);
  9084. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9085. struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
  9086. const struct skl_wm_values *wm = &dev_priv->wm.skl_results;
  9087. const struct skl_plane_wm *p_wm =
  9088. &cstate->wm.skl.optimal.planes[PLANE_CURSOR];
  9089. int pipe = intel_crtc->pipe;
  9090. uint32_t cntl = 0;
  9091. if (INTEL_GEN(dev_priv) >= 9 && wm->dirty_pipes & drm_crtc_mask(crtc))
  9092. skl_write_cursor_wm(intel_crtc, p_wm, &wm->ddb);
  9093. if (plane_state && plane_state->base.visible) {
  9094. cntl = MCURSOR_GAMMA_ENABLE;
  9095. switch (plane_state->base.crtc_w) {
  9096. case 64:
  9097. cntl |= CURSOR_MODE_64_ARGB_AX;
  9098. break;
  9099. case 128:
  9100. cntl |= CURSOR_MODE_128_ARGB_AX;
  9101. break;
  9102. case 256:
  9103. cntl |= CURSOR_MODE_256_ARGB_AX;
  9104. break;
  9105. default:
  9106. MISSING_CASE(plane_state->base.crtc_w);
  9107. return;
  9108. }
  9109. cntl |= pipe << 28; /* Connect to correct pipe */
  9110. if (HAS_DDI(dev_priv))
  9111. cntl |= CURSOR_PIPE_CSC_ENABLE;
  9112. if (plane_state->base.rotation == DRM_ROTATE_180)
  9113. cntl |= CURSOR_ROTATE_180;
  9114. }
  9115. if (intel_crtc->cursor_cntl != cntl) {
  9116. I915_WRITE(CURCNTR(pipe), cntl);
  9117. POSTING_READ(CURCNTR(pipe));
  9118. intel_crtc->cursor_cntl = cntl;
  9119. }
  9120. /* and commit changes on next vblank */
  9121. I915_WRITE(CURBASE(pipe), base);
  9122. POSTING_READ(CURBASE(pipe));
  9123. intel_crtc->cursor_base = base;
  9124. }
  9125. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  9126. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  9127. const struct intel_plane_state *plane_state)
  9128. {
  9129. struct drm_device *dev = crtc->dev;
  9130. struct drm_i915_private *dev_priv = to_i915(dev);
  9131. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9132. int pipe = intel_crtc->pipe;
  9133. u32 base = intel_crtc->cursor_addr;
  9134. u32 pos = 0;
  9135. if (plane_state) {
  9136. int x = plane_state->base.crtc_x;
  9137. int y = plane_state->base.crtc_y;
  9138. if (x < 0) {
  9139. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  9140. x = -x;
  9141. }
  9142. pos |= x << CURSOR_X_SHIFT;
  9143. if (y < 0) {
  9144. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  9145. y = -y;
  9146. }
  9147. pos |= y << CURSOR_Y_SHIFT;
  9148. /* ILK+ do this automagically */
  9149. if (HAS_GMCH_DISPLAY(dev_priv) &&
  9150. plane_state->base.rotation == DRM_ROTATE_180) {
  9151. base += (plane_state->base.crtc_h *
  9152. plane_state->base.crtc_w - 1) * 4;
  9153. }
  9154. }
  9155. I915_WRITE(CURPOS(pipe), pos);
  9156. if (IS_845G(dev_priv) || IS_I865G(dev_priv))
  9157. i845_update_cursor(crtc, base, plane_state);
  9158. else
  9159. i9xx_update_cursor(crtc, base, plane_state);
  9160. }
  9161. static bool cursor_size_ok(struct drm_i915_private *dev_priv,
  9162. uint32_t width, uint32_t height)
  9163. {
  9164. if (width == 0 || height == 0)
  9165. return false;
  9166. /*
  9167. * 845g/865g are special in that they are only limited by
  9168. * the width of their cursors, the height is arbitrary up to
  9169. * the precision of the register. Everything else requires
  9170. * square cursors, limited to a few power-of-two sizes.
  9171. */
  9172. if (IS_845G(dev_priv) || IS_I865G(dev_priv)) {
  9173. if ((width & 63) != 0)
  9174. return false;
  9175. if (width > (IS_845G(dev_priv) ? 64 : 512))
  9176. return false;
  9177. if (height > 1023)
  9178. return false;
  9179. } else {
  9180. switch (width | height) {
  9181. case 256:
  9182. case 128:
  9183. if (IS_GEN2(dev_priv))
  9184. return false;
  9185. case 64:
  9186. break;
  9187. default:
  9188. return false;
  9189. }
  9190. }
  9191. return true;
  9192. }
  9193. /* VESA 640x480x72Hz mode to set on the pipe */
  9194. static struct drm_display_mode load_detect_mode = {
  9195. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  9196. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  9197. };
  9198. struct drm_framebuffer *
  9199. __intel_framebuffer_create(struct drm_device *dev,
  9200. struct drm_mode_fb_cmd2 *mode_cmd,
  9201. struct drm_i915_gem_object *obj)
  9202. {
  9203. struct intel_framebuffer *intel_fb;
  9204. int ret;
  9205. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  9206. if (!intel_fb)
  9207. return ERR_PTR(-ENOMEM);
  9208. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  9209. if (ret)
  9210. goto err;
  9211. return &intel_fb->base;
  9212. err:
  9213. kfree(intel_fb);
  9214. return ERR_PTR(ret);
  9215. }
  9216. static struct drm_framebuffer *
  9217. intel_framebuffer_create(struct drm_device *dev,
  9218. struct drm_mode_fb_cmd2 *mode_cmd,
  9219. struct drm_i915_gem_object *obj)
  9220. {
  9221. struct drm_framebuffer *fb;
  9222. int ret;
  9223. ret = i915_mutex_lock_interruptible(dev);
  9224. if (ret)
  9225. return ERR_PTR(ret);
  9226. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  9227. mutex_unlock(&dev->struct_mutex);
  9228. return fb;
  9229. }
  9230. static u32
  9231. intel_framebuffer_pitch_for_width(int width, int bpp)
  9232. {
  9233. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  9234. return ALIGN(pitch, 64);
  9235. }
  9236. static u32
  9237. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  9238. {
  9239. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  9240. return PAGE_ALIGN(pitch * mode->vdisplay);
  9241. }
  9242. static struct drm_framebuffer *
  9243. intel_framebuffer_create_for_mode(struct drm_device *dev,
  9244. struct drm_display_mode *mode,
  9245. int depth, int bpp)
  9246. {
  9247. struct drm_framebuffer *fb;
  9248. struct drm_i915_gem_object *obj;
  9249. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  9250. obj = i915_gem_object_create(dev,
  9251. intel_framebuffer_size_for_mode(mode, bpp));
  9252. if (IS_ERR(obj))
  9253. return ERR_CAST(obj);
  9254. mode_cmd.width = mode->hdisplay;
  9255. mode_cmd.height = mode->vdisplay;
  9256. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  9257. bpp);
  9258. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  9259. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  9260. if (IS_ERR(fb))
  9261. i915_gem_object_put_unlocked(obj);
  9262. return fb;
  9263. }
  9264. static struct drm_framebuffer *
  9265. mode_fits_in_fbdev(struct drm_device *dev,
  9266. struct drm_display_mode *mode)
  9267. {
  9268. #ifdef CONFIG_DRM_FBDEV_EMULATION
  9269. struct drm_i915_private *dev_priv = to_i915(dev);
  9270. struct drm_i915_gem_object *obj;
  9271. struct drm_framebuffer *fb;
  9272. if (!dev_priv->fbdev)
  9273. return NULL;
  9274. if (!dev_priv->fbdev->fb)
  9275. return NULL;
  9276. obj = dev_priv->fbdev->fb->obj;
  9277. BUG_ON(!obj);
  9278. fb = &dev_priv->fbdev->fb->base;
  9279. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  9280. fb->bits_per_pixel))
  9281. return NULL;
  9282. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  9283. return NULL;
  9284. drm_framebuffer_reference(fb);
  9285. return fb;
  9286. #else
  9287. return NULL;
  9288. #endif
  9289. }
  9290. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  9291. struct drm_crtc *crtc,
  9292. struct drm_display_mode *mode,
  9293. struct drm_framebuffer *fb,
  9294. int x, int y)
  9295. {
  9296. struct drm_plane_state *plane_state;
  9297. int hdisplay, vdisplay;
  9298. int ret;
  9299. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  9300. if (IS_ERR(plane_state))
  9301. return PTR_ERR(plane_state);
  9302. if (mode)
  9303. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  9304. else
  9305. hdisplay = vdisplay = 0;
  9306. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  9307. if (ret)
  9308. return ret;
  9309. drm_atomic_set_fb_for_plane(plane_state, fb);
  9310. plane_state->crtc_x = 0;
  9311. plane_state->crtc_y = 0;
  9312. plane_state->crtc_w = hdisplay;
  9313. plane_state->crtc_h = vdisplay;
  9314. plane_state->src_x = x << 16;
  9315. plane_state->src_y = y << 16;
  9316. plane_state->src_w = hdisplay << 16;
  9317. plane_state->src_h = vdisplay << 16;
  9318. return 0;
  9319. }
  9320. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  9321. struct drm_display_mode *mode,
  9322. struct intel_load_detect_pipe *old,
  9323. struct drm_modeset_acquire_ctx *ctx)
  9324. {
  9325. struct intel_crtc *intel_crtc;
  9326. struct intel_encoder *intel_encoder =
  9327. intel_attached_encoder(connector);
  9328. struct drm_crtc *possible_crtc;
  9329. struct drm_encoder *encoder = &intel_encoder->base;
  9330. struct drm_crtc *crtc = NULL;
  9331. struct drm_device *dev = encoder->dev;
  9332. struct drm_framebuffer *fb;
  9333. struct drm_mode_config *config = &dev->mode_config;
  9334. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  9335. struct drm_connector_state *connector_state;
  9336. struct intel_crtc_state *crtc_state;
  9337. int ret, i = -1;
  9338. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  9339. connector->base.id, connector->name,
  9340. encoder->base.id, encoder->name);
  9341. old->restore_state = NULL;
  9342. retry:
  9343. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  9344. if (ret)
  9345. goto fail;
  9346. /*
  9347. * Algorithm gets a little messy:
  9348. *
  9349. * - if the connector already has an assigned crtc, use it (but make
  9350. * sure it's on first)
  9351. *
  9352. * - try to find the first unused crtc that can drive this connector,
  9353. * and use that if we find one
  9354. */
  9355. /* See if we already have a CRTC for this connector */
  9356. if (connector->state->crtc) {
  9357. crtc = connector->state->crtc;
  9358. ret = drm_modeset_lock(&crtc->mutex, ctx);
  9359. if (ret)
  9360. goto fail;
  9361. /* Make sure the crtc and connector are running */
  9362. goto found;
  9363. }
  9364. /* Find an unused one (if possible) */
  9365. for_each_crtc(dev, possible_crtc) {
  9366. i++;
  9367. if (!(encoder->possible_crtcs & (1 << i)))
  9368. continue;
  9369. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  9370. if (ret)
  9371. goto fail;
  9372. if (possible_crtc->state->enable) {
  9373. drm_modeset_unlock(&possible_crtc->mutex);
  9374. continue;
  9375. }
  9376. crtc = possible_crtc;
  9377. break;
  9378. }
  9379. /*
  9380. * If we didn't find an unused CRTC, don't use any.
  9381. */
  9382. if (!crtc) {
  9383. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  9384. goto fail;
  9385. }
  9386. found:
  9387. intel_crtc = to_intel_crtc(crtc);
  9388. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  9389. if (ret)
  9390. goto fail;
  9391. state = drm_atomic_state_alloc(dev);
  9392. restore_state = drm_atomic_state_alloc(dev);
  9393. if (!state || !restore_state) {
  9394. ret = -ENOMEM;
  9395. goto fail;
  9396. }
  9397. state->acquire_ctx = ctx;
  9398. restore_state->acquire_ctx = ctx;
  9399. connector_state = drm_atomic_get_connector_state(state, connector);
  9400. if (IS_ERR(connector_state)) {
  9401. ret = PTR_ERR(connector_state);
  9402. goto fail;
  9403. }
  9404. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  9405. if (ret)
  9406. goto fail;
  9407. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  9408. if (IS_ERR(crtc_state)) {
  9409. ret = PTR_ERR(crtc_state);
  9410. goto fail;
  9411. }
  9412. crtc_state->base.active = crtc_state->base.enable = true;
  9413. if (!mode)
  9414. mode = &load_detect_mode;
  9415. /* We need a framebuffer large enough to accommodate all accesses
  9416. * that the plane may generate whilst we perform load detection.
  9417. * We can not rely on the fbcon either being present (we get called
  9418. * during its initialisation to detect all boot displays, or it may
  9419. * not even exist) or that it is large enough to satisfy the
  9420. * requested mode.
  9421. */
  9422. fb = mode_fits_in_fbdev(dev, mode);
  9423. if (fb == NULL) {
  9424. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  9425. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  9426. } else
  9427. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  9428. if (IS_ERR(fb)) {
  9429. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  9430. goto fail;
  9431. }
  9432. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  9433. if (ret)
  9434. goto fail;
  9435. drm_framebuffer_unreference(fb);
  9436. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  9437. if (ret)
  9438. goto fail;
  9439. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  9440. if (!ret)
  9441. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  9442. if (!ret)
  9443. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
  9444. if (ret) {
  9445. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  9446. goto fail;
  9447. }
  9448. ret = drm_atomic_commit(state);
  9449. if (ret) {
  9450. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  9451. goto fail;
  9452. }
  9453. old->restore_state = restore_state;
  9454. /* let the connector get through one full cycle before testing */
  9455. intel_wait_for_vblank(dev, intel_crtc->pipe);
  9456. return true;
  9457. fail:
  9458. drm_atomic_state_free(state);
  9459. drm_atomic_state_free(restore_state);
  9460. restore_state = state = NULL;
  9461. if (ret == -EDEADLK) {
  9462. drm_modeset_backoff(ctx);
  9463. goto retry;
  9464. }
  9465. return false;
  9466. }
  9467. void intel_release_load_detect_pipe(struct drm_connector *connector,
  9468. struct intel_load_detect_pipe *old,
  9469. struct drm_modeset_acquire_ctx *ctx)
  9470. {
  9471. struct intel_encoder *intel_encoder =
  9472. intel_attached_encoder(connector);
  9473. struct drm_encoder *encoder = &intel_encoder->base;
  9474. struct drm_atomic_state *state = old->restore_state;
  9475. int ret;
  9476. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  9477. connector->base.id, connector->name,
  9478. encoder->base.id, encoder->name);
  9479. if (!state)
  9480. return;
  9481. ret = drm_atomic_commit(state);
  9482. if (ret) {
  9483. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  9484. drm_atomic_state_free(state);
  9485. }
  9486. }
  9487. static int i9xx_pll_refclk(struct drm_device *dev,
  9488. const struct intel_crtc_state *pipe_config)
  9489. {
  9490. struct drm_i915_private *dev_priv = to_i915(dev);
  9491. u32 dpll = pipe_config->dpll_hw_state.dpll;
  9492. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  9493. return dev_priv->vbt.lvds_ssc_freq;
  9494. else if (HAS_PCH_SPLIT(dev_priv))
  9495. return 120000;
  9496. else if (!IS_GEN2(dev_priv))
  9497. return 96000;
  9498. else
  9499. return 48000;
  9500. }
  9501. /* Returns the clock of the currently programmed mode of the given pipe. */
  9502. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  9503. struct intel_crtc_state *pipe_config)
  9504. {
  9505. struct drm_device *dev = crtc->base.dev;
  9506. struct drm_i915_private *dev_priv = to_i915(dev);
  9507. int pipe = pipe_config->cpu_transcoder;
  9508. u32 dpll = pipe_config->dpll_hw_state.dpll;
  9509. u32 fp;
  9510. struct dpll clock;
  9511. int port_clock;
  9512. int refclk = i9xx_pll_refclk(dev, pipe_config);
  9513. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  9514. fp = pipe_config->dpll_hw_state.fp0;
  9515. else
  9516. fp = pipe_config->dpll_hw_state.fp1;
  9517. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  9518. if (IS_PINEVIEW(dev)) {
  9519. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  9520. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  9521. } else {
  9522. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  9523. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  9524. }
  9525. if (!IS_GEN2(dev_priv)) {
  9526. if (IS_PINEVIEW(dev))
  9527. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  9528. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  9529. else
  9530. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  9531. DPLL_FPA01_P1_POST_DIV_SHIFT);
  9532. switch (dpll & DPLL_MODE_MASK) {
  9533. case DPLLB_MODE_DAC_SERIAL:
  9534. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  9535. 5 : 10;
  9536. break;
  9537. case DPLLB_MODE_LVDS:
  9538. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  9539. 7 : 14;
  9540. break;
  9541. default:
  9542. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  9543. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  9544. return;
  9545. }
  9546. if (IS_PINEVIEW(dev))
  9547. port_clock = pnv_calc_dpll_params(refclk, &clock);
  9548. else
  9549. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  9550. } else {
  9551. u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
  9552. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  9553. if (is_lvds) {
  9554. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  9555. DPLL_FPA01_P1_POST_DIV_SHIFT);
  9556. if (lvds & LVDS_CLKB_POWER_UP)
  9557. clock.p2 = 7;
  9558. else
  9559. clock.p2 = 14;
  9560. } else {
  9561. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  9562. clock.p1 = 2;
  9563. else {
  9564. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  9565. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  9566. }
  9567. if (dpll & PLL_P2_DIVIDE_BY_4)
  9568. clock.p2 = 4;
  9569. else
  9570. clock.p2 = 2;
  9571. }
  9572. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  9573. }
  9574. /*
  9575. * This value includes pixel_multiplier. We will use
  9576. * port_clock to compute adjusted_mode.crtc_clock in the
  9577. * encoder's get_config() function.
  9578. */
  9579. pipe_config->port_clock = port_clock;
  9580. }
  9581. int intel_dotclock_calculate(int link_freq,
  9582. const struct intel_link_m_n *m_n)
  9583. {
  9584. /*
  9585. * The calculation for the data clock is:
  9586. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  9587. * But we want to avoid losing precison if possible, so:
  9588. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  9589. *
  9590. * and the link clock is simpler:
  9591. * link_clock = (m * link_clock) / n
  9592. */
  9593. if (!m_n->link_n)
  9594. return 0;
  9595. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  9596. }
  9597. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  9598. struct intel_crtc_state *pipe_config)
  9599. {
  9600. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9601. /* read out port_clock from the DPLL */
  9602. i9xx_crtc_clock_get(crtc, pipe_config);
  9603. /*
  9604. * In case there is an active pipe without active ports,
  9605. * we may need some idea for the dotclock anyway.
  9606. * Calculate one based on the FDI configuration.
  9607. */
  9608. pipe_config->base.adjusted_mode.crtc_clock =
  9609. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9610. &pipe_config->fdi_m_n);
  9611. }
  9612. /** Returns the currently programmed mode of the given pipe. */
  9613. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  9614. struct drm_crtc *crtc)
  9615. {
  9616. struct drm_i915_private *dev_priv = to_i915(dev);
  9617. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9618. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  9619. struct drm_display_mode *mode;
  9620. struct intel_crtc_state *pipe_config;
  9621. int htot = I915_READ(HTOTAL(cpu_transcoder));
  9622. int hsync = I915_READ(HSYNC(cpu_transcoder));
  9623. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  9624. int vsync = I915_READ(VSYNC(cpu_transcoder));
  9625. enum pipe pipe = intel_crtc->pipe;
  9626. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  9627. if (!mode)
  9628. return NULL;
  9629. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  9630. if (!pipe_config) {
  9631. kfree(mode);
  9632. return NULL;
  9633. }
  9634. /*
  9635. * Construct a pipe_config sufficient for getting the clock info
  9636. * back out of crtc_clock_get.
  9637. *
  9638. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  9639. * to use a real value here instead.
  9640. */
  9641. pipe_config->cpu_transcoder = (enum transcoder) pipe;
  9642. pipe_config->pixel_multiplier = 1;
  9643. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  9644. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  9645. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  9646. i9xx_crtc_clock_get(intel_crtc, pipe_config);
  9647. mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
  9648. mode->hdisplay = (htot & 0xffff) + 1;
  9649. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  9650. mode->hsync_start = (hsync & 0xffff) + 1;
  9651. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  9652. mode->vdisplay = (vtot & 0xffff) + 1;
  9653. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  9654. mode->vsync_start = (vsync & 0xffff) + 1;
  9655. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  9656. drm_mode_set_name(mode);
  9657. kfree(pipe_config);
  9658. return mode;
  9659. }
  9660. static void intel_crtc_destroy(struct drm_crtc *crtc)
  9661. {
  9662. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9663. struct drm_device *dev = crtc->dev;
  9664. struct intel_flip_work *work;
  9665. spin_lock_irq(&dev->event_lock);
  9666. work = intel_crtc->flip_work;
  9667. intel_crtc->flip_work = NULL;
  9668. spin_unlock_irq(&dev->event_lock);
  9669. if (work) {
  9670. cancel_work_sync(&work->mmio_work);
  9671. cancel_work_sync(&work->unpin_work);
  9672. kfree(work);
  9673. }
  9674. drm_crtc_cleanup(crtc);
  9675. kfree(intel_crtc);
  9676. }
  9677. static void intel_unpin_work_fn(struct work_struct *__work)
  9678. {
  9679. struct intel_flip_work *work =
  9680. container_of(__work, struct intel_flip_work, unpin_work);
  9681. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  9682. struct drm_device *dev = crtc->base.dev;
  9683. struct drm_plane *primary = crtc->base.primary;
  9684. if (is_mmio_work(work))
  9685. flush_work(&work->mmio_work);
  9686. mutex_lock(&dev->struct_mutex);
  9687. intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
  9688. i915_gem_object_put(work->pending_flip_obj);
  9689. mutex_unlock(&dev->struct_mutex);
  9690. i915_gem_request_put(work->flip_queued_req);
  9691. intel_frontbuffer_flip_complete(to_i915(dev),
  9692. to_intel_plane(primary)->frontbuffer_bit);
  9693. intel_fbc_post_update(crtc);
  9694. drm_framebuffer_unreference(work->old_fb);
  9695. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  9696. atomic_dec(&crtc->unpin_work_count);
  9697. kfree(work);
  9698. }
  9699. /* Is 'a' after or equal to 'b'? */
  9700. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9701. {
  9702. return !((a - b) & 0x80000000);
  9703. }
  9704. static bool __pageflip_finished_cs(struct intel_crtc *crtc,
  9705. struct intel_flip_work *work)
  9706. {
  9707. struct drm_device *dev = crtc->base.dev;
  9708. struct drm_i915_private *dev_priv = to_i915(dev);
  9709. if (abort_flip_on_reset(crtc))
  9710. return true;
  9711. /*
  9712. * The relevant registers doen't exist on pre-ctg.
  9713. * As the flip done interrupt doesn't trigger for mmio
  9714. * flips on gmch platforms, a flip count check isn't
  9715. * really needed there. But since ctg has the registers,
  9716. * include it in the check anyway.
  9717. */
  9718. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  9719. return true;
  9720. /*
  9721. * BDW signals flip done immediately if the plane
  9722. * is disabled, even if the plane enable is already
  9723. * armed to occur at the next vblank :(
  9724. */
  9725. /*
  9726. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9727. * used the same base address. In that case the mmio flip might
  9728. * have completed, but the CS hasn't even executed the flip yet.
  9729. *
  9730. * A flip count check isn't enough as the CS might have updated
  9731. * the base address just after start of vblank, but before we
  9732. * managed to process the interrupt. This means we'd complete the
  9733. * CS flip too soon.
  9734. *
  9735. * Combining both checks should get us a good enough result. It may
  9736. * still happen that the CS flip has been executed, but has not
  9737. * yet actually completed. But in case the base address is the same
  9738. * anyway, we don't really care.
  9739. */
  9740. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9741. crtc->flip_work->gtt_offset &&
  9742. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
  9743. crtc->flip_work->flip_count);
  9744. }
  9745. static bool
  9746. __pageflip_finished_mmio(struct intel_crtc *crtc,
  9747. struct intel_flip_work *work)
  9748. {
  9749. /*
  9750. * MMIO work completes when vblank is different from
  9751. * flip_queued_vblank.
  9752. *
  9753. * Reset counter value doesn't matter, this is handled by
  9754. * i915_wait_request finishing early, so no need to handle
  9755. * reset here.
  9756. */
  9757. return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
  9758. }
  9759. static bool pageflip_finished(struct intel_crtc *crtc,
  9760. struct intel_flip_work *work)
  9761. {
  9762. if (!atomic_read(&work->pending))
  9763. return false;
  9764. smp_rmb();
  9765. if (is_mmio_work(work))
  9766. return __pageflip_finished_mmio(crtc, work);
  9767. else
  9768. return __pageflip_finished_cs(crtc, work);
  9769. }
  9770. void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
  9771. {
  9772. struct drm_device *dev = &dev_priv->drm;
  9773. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9774. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9775. struct intel_flip_work *work;
  9776. unsigned long flags;
  9777. /* Ignore early vblank irqs */
  9778. if (!crtc)
  9779. return;
  9780. /*
  9781. * This is called both by irq handlers and the reset code (to complete
  9782. * lost pageflips) so needs the full irqsave spinlocks.
  9783. */
  9784. spin_lock_irqsave(&dev->event_lock, flags);
  9785. work = intel_crtc->flip_work;
  9786. if (work != NULL &&
  9787. !is_mmio_work(work) &&
  9788. pageflip_finished(intel_crtc, work))
  9789. page_flip_completed(intel_crtc);
  9790. spin_unlock_irqrestore(&dev->event_lock, flags);
  9791. }
  9792. void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
  9793. {
  9794. struct drm_device *dev = &dev_priv->drm;
  9795. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  9796. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9797. struct intel_flip_work *work;
  9798. unsigned long flags;
  9799. /* Ignore early vblank irqs */
  9800. if (!crtc)
  9801. return;
  9802. /*
  9803. * This is called both by irq handlers and the reset code (to complete
  9804. * lost pageflips) so needs the full irqsave spinlocks.
  9805. */
  9806. spin_lock_irqsave(&dev->event_lock, flags);
  9807. work = intel_crtc->flip_work;
  9808. if (work != NULL &&
  9809. is_mmio_work(work) &&
  9810. pageflip_finished(intel_crtc, work))
  9811. page_flip_completed(intel_crtc);
  9812. spin_unlock_irqrestore(&dev->event_lock, flags);
  9813. }
  9814. static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
  9815. struct intel_flip_work *work)
  9816. {
  9817. work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
  9818. /* Ensure that the work item is consistent when activating it ... */
  9819. smp_mb__before_atomic();
  9820. atomic_set(&work->pending, 1);
  9821. }
  9822. static int intel_gen2_queue_flip(struct drm_device *dev,
  9823. struct drm_crtc *crtc,
  9824. struct drm_framebuffer *fb,
  9825. struct drm_i915_gem_object *obj,
  9826. struct drm_i915_gem_request *req,
  9827. uint32_t flags)
  9828. {
  9829. struct intel_ring *ring = req->ring;
  9830. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9831. u32 flip_mask;
  9832. int ret;
  9833. ret = intel_ring_begin(req, 6);
  9834. if (ret)
  9835. return ret;
  9836. /* Can't queue multiple flips, so wait for the previous
  9837. * one to finish before executing the next.
  9838. */
  9839. if (intel_crtc->plane)
  9840. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9841. else
  9842. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9843. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9844. intel_ring_emit(ring, MI_NOOP);
  9845. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9846. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9847. intel_ring_emit(ring, fb->pitches[0]);
  9848. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  9849. intel_ring_emit(ring, 0); /* aux display base address, unused */
  9850. return 0;
  9851. }
  9852. static int intel_gen3_queue_flip(struct drm_device *dev,
  9853. struct drm_crtc *crtc,
  9854. struct drm_framebuffer *fb,
  9855. struct drm_i915_gem_object *obj,
  9856. struct drm_i915_gem_request *req,
  9857. uint32_t flags)
  9858. {
  9859. struct intel_ring *ring = req->ring;
  9860. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9861. u32 flip_mask;
  9862. int ret;
  9863. ret = intel_ring_begin(req, 6);
  9864. if (ret)
  9865. return ret;
  9866. if (intel_crtc->plane)
  9867. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9868. else
  9869. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9870. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9871. intel_ring_emit(ring, MI_NOOP);
  9872. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  9873. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9874. intel_ring_emit(ring, fb->pitches[0]);
  9875. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  9876. intel_ring_emit(ring, MI_NOOP);
  9877. return 0;
  9878. }
  9879. static int intel_gen4_queue_flip(struct drm_device *dev,
  9880. struct drm_crtc *crtc,
  9881. struct drm_framebuffer *fb,
  9882. struct drm_i915_gem_object *obj,
  9883. struct drm_i915_gem_request *req,
  9884. uint32_t flags)
  9885. {
  9886. struct intel_ring *ring = req->ring;
  9887. struct drm_i915_private *dev_priv = to_i915(dev);
  9888. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9889. uint32_t pf, pipesrc;
  9890. int ret;
  9891. ret = intel_ring_begin(req, 4);
  9892. if (ret)
  9893. return ret;
  9894. /* i965+ uses the linear or tiled offsets from the
  9895. * Display Registers (which do not change across a page-flip)
  9896. * so we need only reprogram the base address.
  9897. */
  9898. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9899. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9900. intel_ring_emit(ring, fb->pitches[0]);
  9901. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset |
  9902. intel_fb_modifier_to_tiling(fb->modifier[0]));
  9903. /* XXX Enabling the panel-fitter across page-flip is so far
  9904. * untested on non-native modes, so ignore it for now.
  9905. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9906. */
  9907. pf = 0;
  9908. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9909. intel_ring_emit(ring, pf | pipesrc);
  9910. return 0;
  9911. }
  9912. static int intel_gen6_queue_flip(struct drm_device *dev,
  9913. struct drm_crtc *crtc,
  9914. struct drm_framebuffer *fb,
  9915. struct drm_i915_gem_object *obj,
  9916. struct drm_i915_gem_request *req,
  9917. uint32_t flags)
  9918. {
  9919. struct intel_ring *ring = req->ring;
  9920. struct drm_i915_private *dev_priv = to_i915(dev);
  9921. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9922. uint32_t pf, pipesrc;
  9923. int ret;
  9924. ret = intel_ring_begin(req, 4);
  9925. if (ret)
  9926. return ret;
  9927. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9928. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9929. intel_ring_emit(ring, fb->pitches[0] |
  9930. intel_fb_modifier_to_tiling(fb->modifier[0]));
  9931. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  9932. /* Contrary to the suggestions in the documentation,
  9933. * "Enable Panel Fitter" does not seem to be required when page
  9934. * flipping with a non-native mode, and worse causes a normal
  9935. * modeset to fail.
  9936. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9937. */
  9938. pf = 0;
  9939. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9940. intel_ring_emit(ring, pf | pipesrc);
  9941. return 0;
  9942. }
  9943. static int intel_gen7_queue_flip(struct drm_device *dev,
  9944. struct drm_crtc *crtc,
  9945. struct drm_framebuffer *fb,
  9946. struct drm_i915_gem_object *obj,
  9947. struct drm_i915_gem_request *req,
  9948. uint32_t flags)
  9949. {
  9950. struct drm_i915_private *dev_priv = to_i915(dev);
  9951. struct intel_ring *ring = req->ring;
  9952. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9953. uint32_t plane_bit = 0;
  9954. int len, ret;
  9955. switch (intel_crtc->plane) {
  9956. case PLANE_A:
  9957. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9958. break;
  9959. case PLANE_B:
  9960. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9961. break;
  9962. case PLANE_C:
  9963. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9964. break;
  9965. default:
  9966. WARN_ONCE(1, "unknown plane in flip command\n");
  9967. return -ENODEV;
  9968. }
  9969. len = 4;
  9970. if (req->engine->id == RCS) {
  9971. len += 6;
  9972. /*
  9973. * On Gen 8, SRM is now taking an extra dword to accommodate
  9974. * 48bits addresses, and we need a NOOP for the batch size to
  9975. * stay even.
  9976. */
  9977. if (IS_GEN8(dev_priv))
  9978. len += 2;
  9979. }
  9980. /*
  9981. * BSpec MI_DISPLAY_FLIP for IVB:
  9982. * "The full packet must be contained within the same cache line."
  9983. *
  9984. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9985. * cacheline, if we ever start emitting more commands before
  9986. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9987. * then do the cacheline alignment, and finally emit the
  9988. * MI_DISPLAY_FLIP.
  9989. */
  9990. ret = intel_ring_cacheline_align(req);
  9991. if (ret)
  9992. return ret;
  9993. ret = intel_ring_begin(req, len);
  9994. if (ret)
  9995. return ret;
  9996. /* Unmask the flip-done completion message. Note that the bspec says that
  9997. * we should do this for both the BCS and RCS, and that we must not unmask
  9998. * more than one flip event at any time (or ensure that one flip message
  9999. * can be sent by waiting for flip-done prior to queueing new flips).
  10000. * Experimentation says that BCS works despite DERRMR masking all
  10001. * flip-done completion events and that unmasking all planes at once
  10002. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  10003. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  10004. */
  10005. if (req->engine->id == RCS) {
  10006. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  10007. intel_ring_emit_reg(ring, DERRMR);
  10008. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  10009. DERRMR_PIPEB_PRI_FLIP_DONE |
  10010. DERRMR_PIPEC_PRI_FLIP_DONE));
  10011. if (IS_GEN8(dev_priv))
  10012. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
  10013. MI_SRM_LRM_GLOBAL_GTT);
  10014. else
  10015. intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
  10016. MI_SRM_LRM_GLOBAL_GTT);
  10017. intel_ring_emit_reg(ring, DERRMR);
  10018. intel_ring_emit(ring,
  10019. i915_ggtt_offset(req->engine->scratch) + 256);
  10020. if (IS_GEN8(dev_priv)) {
  10021. intel_ring_emit(ring, 0);
  10022. intel_ring_emit(ring, MI_NOOP);
  10023. }
  10024. }
  10025. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  10026. intel_ring_emit(ring, fb->pitches[0] |
  10027. intel_fb_modifier_to_tiling(fb->modifier[0]));
  10028. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  10029. intel_ring_emit(ring, (MI_NOOP));
  10030. return 0;
  10031. }
  10032. static bool use_mmio_flip(struct intel_engine_cs *engine,
  10033. struct drm_i915_gem_object *obj)
  10034. {
  10035. struct reservation_object *resv;
  10036. /*
  10037. * This is not being used for older platforms, because
  10038. * non-availability of flip done interrupt forces us to use
  10039. * CS flips. Older platforms derive flip done using some clever
  10040. * tricks involving the flip_pending status bits and vblank irqs.
  10041. * So using MMIO flips there would disrupt this mechanism.
  10042. */
  10043. if (engine == NULL)
  10044. return true;
  10045. if (INTEL_GEN(engine->i915) < 5)
  10046. return false;
  10047. if (i915.use_mmio_flip < 0)
  10048. return false;
  10049. else if (i915.use_mmio_flip > 0)
  10050. return true;
  10051. else if (i915.enable_execlists)
  10052. return true;
  10053. resv = i915_gem_object_get_dmabuf_resv(obj);
  10054. if (resv && !reservation_object_test_signaled_rcu(resv, false))
  10055. return true;
  10056. return engine != i915_gem_active_get_engine(&obj->last_write,
  10057. &obj->base.dev->struct_mutex);
  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[0]) {
  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[0]);
  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[0] == 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. struct reservation_object *resv;
  10121. if (work->flip_queued_req)
  10122. WARN_ON(i915_wait_request(work->flip_queued_req,
  10123. 0, NULL, NO_WAITBOOST));
  10124. /* For framebuffer backed by dmabuf, wait for fence */
  10125. resv = i915_gem_object_get_dmabuf_resv(obj);
  10126. if (resv)
  10127. WARN_ON(reservation_object_wait_timeout_rcu(resv, false, false,
  10128. MAX_SCHEDULE_TIMEOUT) < 0);
  10129. intel_pipe_update_start(crtc);
  10130. if (INTEL_GEN(dev_priv) >= 9)
  10131. skl_do_mmio_flip(crtc, work->rotation, work);
  10132. else
  10133. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  10134. ilk_do_mmio_flip(crtc, work);
  10135. intel_pipe_update_end(crtc, work);
  10136. }
  10137. static int intel_default_queue_flip(struct drm_device *dev,
  10138. struct drm_crtc *crtc,
  10139. struct drm_framebuffer *fb,
  10140. struct drm_i915_gem_object *obj,
  10141. struct drm_i915_gem_request *req,
  10142. uint32_t flags)
  10143. {
  10144. return -ENODEV;
  10145. }
  10146. static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
  10147. struct intel_crtc *intel_crtc,
  10148. struct intel_flip_work *work)
  10149. {
  10150. u32 addr, vblank;
  10151. if (!atomic_read(&work->pending))
  10152. return false;
  10153. smp_rmb();
  10154. vblank = intel_crtc_get_vblank_counter(intel_crtc);
  10155. if (work->flip_ready_vblank == 0) {
  10156. if (work->flip_queued_req &&
  10157. !i915_gem_request_completed(work->flip_queued_req))
  10158. return false;
  10159. work->flip_ready_vblank = vblank;
  10160. }
  10161. if (vblank - work->flip_ready_vblank < 3)
  10162. return false;
  10163. /* Potential stall - if we see that the flip has happened,
  10164. * assume a missed interrupt. */
  10165. if (INTEL_GEN(dev_priv) >= 4)
  10166. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  10167. else
  10168. addr = I915_READ(DSPADDR(intel_crtc->plane));
  10169. /* There is a potential issue here with a false positive after a flip
  10170. * to the same address. We could address this by checking for a
  10171. * non-incrementing frame counter.
  10172. */
  10173. return addr == work->gtt_offset;
  10174. }
  10175. void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
  10176. {
  10177. struct drm_device *dev = &dev_priv->drm;
  10178. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  10179. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10180. struct intel_flip_work *work;
  10181. WARN_ON(!in_interrupt());
  10182. if (crtc == NULL)
  10183. return;
  10184. spin_lock(&dev->event_lock);
  10185. work = intel_crtc->flip_work;
  10186. if (work != NULL && !is_mmio_work(work) &&
  10187. __pageflip_stall_check_cs(dev_priv, intel_crtc, work)) {
  10188. WARN_ONCE(1,
  10189. "Kicking stuck page flip: queued at %d, now %d\n",
  10190. work->flip_queued_vblank, intel_crtc_get_vblank_counter(intel_crtc));
  10191. page_flip_completed(intel_crtc);
  10192. work = NULL;
  10193. }
  10194. if (work != NULL && !is_mmio_work(work) &&
  10195. intel_crtc_get_vblank_counter(intel_crtc) - work->flip_queued_vblank > 1)
  10196. intel_queue_rps_boost_for_request(work->flip_queued_req);
  10197. spin_unlock(&dev->event_lock);
  10198. }
  10199. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  10200. struct drm_framebuffer *fb,
  10201. struct drm_pending_vblank_event *event,
  10202. uint32_t page_flip_flags)
  10203. {
  10204. struct drm_device *dev = crtc->dev;
  10205. struct drm_i915_private *dev_priv = to_i915(dev);
  10206. struct drm_framebuffer *old_fb = crtc->primary->fb;
  10207. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10208. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10209. struct drm_plane *primary = crtc->primary;
  10210. enum pipe pipe = intel_crtc->pipe;
  10211. struct intel_flip_work *work;
  10212. struct intel_engine_cs *engine;
  10213. bool mmio_flip;
  10214. struct drm_i915_gem_request *request;
  10215. struct i915_vma *vma;
  10216. int ret;
  10217. /*
  10218. * drm_mode_page_flip_ioctl() should already catch this, but double
  10219. * check to be safe. In the future we may enable pageflipping from
  10220. * a disabled primary plane.
  10221. */
  10222. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  10223. return -EBUSY;
  10224. /* Can't change pixel format via MI display flips. */
  10225. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  10226. return -EINVAL;
  10227. /*
  10228. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  10229. * Note that pitch changes could also affect these register.
  10230. */
  10231. if (INTEL_INFO(dev)->gen > 3 &&
  10232. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  10233. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  10234. return -EINVAL;
  10235. if (i915_terminally_wedged(&dev_priv->gpu_error))
  10236. goto out_hang;
  10237. work = kzalloc(sizeof(*work), GFP_KERNEL);
  10238. if (work == NULL)
  10239. return -ENOMEM;
  10240. work->event = event;
  10241. work->crtc = crtc;
  10242. work->old_fb = old_fb;
  10243. INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
  10244. ret = drm_crtc_vblank_get(crtc);
  10245. if (ret)
  10246. goto free_work;
  10247. /* We borrow the event spin lock for protecting flip_work */
  10248. spin_lock_irq(&dev->event_lock);
  10249. if (intel_crtc->flip_work) {
  10250. /* Before declaring the flip queue wedged, check if
  10251. * the hardware completed the operation behind our backs.
  10252. */
  10253. if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
  10254. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  10255. page_flip_completed(intel_crtc);
  10256. } else {
  10257. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  10258. spin_unlock_irq(&dev->event_lock);
  10259. drm_crtc_vblank_put(crtc);
  10260. kfree(work);
  10261. return -EBUSY;
  10262. }
  10263. }
  10264. intel_crtc->flip_work = work;
  10265. spin_unlock_irq(&dev->event_lock);
  10266. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  10267. flush_workqueue(dev_priv->wq);
  10268. /* Reference the objects for the scheduled work. */
  10269. drm_framebuffer_reference(work->old_fb);
  10270. crtc->primary->fb = fb;
  10271. update_state_fb(crtc->primary);
  10272. work->pending_flip_obj = i915_gem_object_get(obj);
  10273. ret = i915_mutex_lock_interruptible(dev);
  10274. if (ret)
  10275. goto cleanup;
  10276. intel_crtc->reset_count = i915_reset_count(&dev_priv->gpu_error);
  10277. if (i915_reset_in_progress_or_wedged(&dev_priv->gpu_error)) {
  10278. ret = -EIO;
  10279. goto cleanup;
  10280. }
  10281. atomic_inc(&intel_crtc->unpin_work_count);
  10282. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  10283. work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
  10284. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  10285. engine = dev_priv->engine[BCS];
  10286. if (fb->modifier[0] != old_fb->modifier[0])
  10287. /* vlv: DISPLAY_FLIP fails to change tiling */
  10288. engine = NULL;
  10289. } else if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
  10290. engine = dev_priv->engine[BCS];
  10291. } else if (INTEL_INFO(dev)->gen >= 7) {
  10292. engine = i915_gem_active_get_engine(&obj->last_write,
  10293. &obj->base.dev->struct_mutex);
  10294. if (engine == NULL || engine->id != RCS)
  10295. engine = dev_priv->engine[BCS];
  10296. } else {
  10297. engine = dev_priv->engine[RCS];
  10298. }
  10299. mmio_flip = use_mmio_flip(engine, obj);
  10300. vma = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  10301. if (IS_ERR(vma)) {
  10302. ret = PTR_ERR(vma);
  10303. goto cleanup_pending;
  10304. }
  10305. work->gtt_offset = intel_fb_gtt_offset(fb, primary->state->rotation);
  10306. work->gtt_offset += intel_crtc->dspaddr_offset;
  10307. work->rotation = crtc->primary->state->rotation;
  10308. /*
  10309. * There's the potential that the next frame will not be compatible with
  10310. * FBC, so we want to call pre_update() before the actual page flip.
  10311. * The problem is that pre_update() caches some information about the fb
  10312. * object, so we want to do this only after the object is pinned. Let's
  10313. * be on the safe side and do this immediately before scheduling the
  10314. * flip.
  10315. */
  10316. intel_fbc_pre_update(intel_crtc, intel_crtc->config,
  10317. to_intel_plane_state(primary->state));
  10318. if (mmio_flip) {
  10319. INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
  10320. work->flip_queued_req = i915_gem_active_get(&obj->last_write,
  10321. &obj->base.dev->struct_mutex);
  10322. queue_work(system_unbound_wq, &work->mmio_work);
  10323. } else {
  10324. request = i915_gem_request_alloc(engine, engine->last_context);
  10325. if (IS_ERR(request)) {
  10326. ret = PTR_ERR(request);
  10327. goto cleanup_unpin;
  10328. }
  10329. ret = i915_gem_request_await_object(request, obj, false);
  10330. if (ret)
  10331. goto cleanup_request;
  10332. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  10333. page_flip_flags);
  10334. if (ret)
  10335. goto cleanup_request;
  10336. intel_mark_page_flip_active(intel_crtc, work);
  10337. work->flip_queued_req = i915_gem_request_get(request);
  10338. i915_add_request_no_flush(request);
  10339. }
  10340. i915_gem_track_fb(intel_fb_obj(old_fb), obj,
  10341. to_intel_plane(primary)->frontbuffer_bit);
  10342. mutex_unlock(&dev->struct_mutex);
  10343. intel_frontbuffer_flip_prepare(to_i915(dev),
  10344. to_intel_plane(primary)->frontbuffer_bit);
  10345. trace_i915_flip_request(intel_crtc->plane, obj);
  10346. return 0;
  10347. cleanup_request:
  10348. i915_add_request_no_flush(request);
  10349. cleanup_unpin:
  10350. intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
  10351. cleanup_pending:
  10352. atomic_dec(&intel_crtc->unpin_work_count);
  10353. mutex_unlock(&dev->struct_mutex);
  10354. cleanup:
  10355. crtc->primary->fb = old_fb;
  10356. update_state_fb(crtc->primary);
  10357. i915_gem_object_put_unlocked(obj);
  10358. drm_framebuffer_unreference(work->old_fb);
  10359. spin_lock_irq(&dev->event_lock);
  10360. intel_crtc->flip_work = NULL;
  10361. spin_unlock_irq(&dev->event_lock);
  10362. drm_crtc_vblank_put(crtc);
  10363. free_work:
  10364. kfree(work);
  10365. if (ret == -EIO) {
  10366. struct drm_atomic_state *state;
  10367. struct drm_plane_state *plane_state;
  10368. out_hang:
  10369. state = drm_atomic_state_alloc(dev);
  10370. if (!state)
  10371. return -ENOMEM;
  10372. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  10373. retry:
  10374. plane_state = drm_atomic_get_plane_state(state, primary);
  10375. ret = PTR_ERR_OR_ZERO(plane_state);
  10376. if (!ret) {
  10377. drm_atomic_set_fb_for_plane(plane_state, fb);
  10378. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  10379. if (!ret)
  10380. ret = drm_atomic_commit(state);
  10381. }
  10382. if (ret == -EDEADLK) {
  10383. drm_modeset_backoff(state->acquire_ctx);
  10384. drm_atomic_state_clear(state);
  10385. goto retry;
  10386. }
  10387. if (ret)
  10388. drm_atomic_state_free(state);
  10389. if (ret == 0 && event) {
  10390. spin_lock_irq(&dev->event_lock);
  10391. drm_crtc_send_vblank_event(crtc, event);
  10392. spin_unlock_irq(&dev->event_lock);
  10393. }
  10394. }
  10395. return ret;
  10396. }
  10397. /**
  10398. * intel_wm_need_update - Check whether watermarks need updating
  10399. * @plane: drm plane
  10400. * @state: new plane state
  10401. *
  10402. * Check current plane state versus the new one to determine whether
  10403. * watermarks need to be recalculated.
  10404. *
  10405. * Returns true or false.
  10406. */
  10407. static bool intel_wm_need_update(struct drm_plane *plane,
  10408. struct drm_plane_state *state)
  10409. {
  10410. struct intel_plane_state *new = to_intel_plane_state(state);
  10411. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  10412. /* Update watermarks on tiling or size changes. */
  10413. if (new->base.visible != cur->base.visible)
  10414. return true;
  10415. if (!cur->base.fb || !new->base.fb)
  10416. return false;
  10417. if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
  10418. cur->base.rotation != new->base.rotation ||
  10419. drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
  10420. drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
  10421. drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
  10422. drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
  10423. return true;
  10424. return false;
  10425. }
  10426. static bool needs_scaling(struct intel_plane_state *state)
  10427. {
  10428. int src_w = drm_rect_width(&state->base.src) >> 16;
  10429. int src_h = drm_rect_height(&state->base.src) >> 16;
  10430. int dst_w = drm_rect_width(&state->base.dst);
  10431. int dst_h = drm_rect_height(&state->base.dst);
  10432. return (src_w != dst_w || src_h != dst_h);
  10433. }
  10434. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  10435. struct drm_plane_state *plane_state)
  10436. {
  10437. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  10438. struct drm_crtc *crtc = crtc_state->crtc;
  10439. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10440. struct drm_plane *plane = plane_state->plane;
  10441. struct drm_device *dev = crtc->dev;
  10442. struct drm_i915_private *dev_priv = to_i915(dev);
  10443. struct intel_plane_state *old_plane_state =
  10444. to_intel_plane_state(plane->state);
  10445. bool mode_changed = needs_modeset(crtc_state);
  10446. bool was_crtc_enabled = crtc->state->active;
  10447. bool is_crtc_enabled = crtc_state->active;
  10448. bool turn_off, turn_on, visible, was_visible;
  10449. struct drm_framebuffer *fb = plane_state->fb;
  10450. int ret;
  10451. if (INTEL_GEN(dev_priv) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
  10452. ret = skl_update_scaler_plane(
  10453. to_intel_crtc_state(crtc_state),
  10454. to_intel_plane_state(plane_state));
  10455. if (ret)
  10456. return ret;
  10457. }
  10458. was_visible = old_plane_state->base.visible;
  10459. visible = to_intel_plane_state(plane_state)->base.visible;
  10460. if (!was_crtc_enabled && WARN_ON(was_visible))
  10461. was_visible = false;
  10462. /*
  10463. * Visibility is calculated as if the crtc was on, but
  10464. * after scaler setup everything depends on it being off
  10465. * when the crtc isn't active.
  10466. *
  10467. * FIXME this is wrong for watermarks. Watermarks should also
  10468. * be computed as if the pipe would be active. Perhaps move
  10469. * per-plane wm computation to the .check_plane() hook, and
  10470. * only combine the results from all planes in the current place?
  10471. */
  10472. if (!is_crtc_enabled)
  10473. to_intel_plane_state(plane_state)->base.visible = visible = false;
  10474. if (!was_visible && !visible)
  10475. return 0;
  10476. if (fb != old_plane_state->base.fb)
  10477. pipe_config->fb_changed = true;
  10478. turn_off = was_visible && (!visible || mode_changed);
  10479. turn_on = visible && (!was_visible || mode_changed);
  10480. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  10481. intel_crtc->base.base.id,
  10482. intel_crtc->base.name,
  10483. plane->base.id, plane->name,
  10484. fb ? fb->base.id : -1);
  10485. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  10486. plane->base.id, plane->name,
  10487. was_visible, visible,
  10488. turn_off, turn_on, mode_changed);
  10489. if (turn_on) {
  10490. pipe_config->update_wm_pre = true;
  10491. /* must disable cxsr around plane enable/disable */
  10492. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  10493. pipe_config->disable_cxsr = true;
  10494. } else if (turn_off) {
  10495. pipe_config->update_wm_post = true;
  10496. /* must disable cxsr around plane enable/disable */
  10497. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  10498. pipe_config->disable_cxsr = true;
  10499. } else if (intel_wm_need_update(plane, plane_state)) {
  10500. /* FIXME bollocks */
  10501. pipe_config->update_wm_pre = true;
  10502. pipe_config->update_wm_post = true;
  10503. }
  10504. /* Pre-gen9 platforms need two-step watermark updates */
  10505. if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
  10506. INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
  10507. to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
  10508. if (visible || was_visible)
  10509. pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
  10510. /*
  10511. * WaCxSRDisabledForSpriteScaling:ivb
  10512. *
  10513. * cstate->update_wm was already set above, so this flag will
  10514. * take effect when we commit and program watermarks.
  10515. */
  10516. if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev_priv) &&
  10517. needs_scaling(to_intel_plane_state(plane_state)) &&
  10518. !needs_scaling(old_plane_state))
  10519. pipe_config->disable_lp_wm = true;
  10520. return 0;
  10521. }
  10522. static bool encoders_cloneable(const struct intel_encoder *a,
  10523. const struct intel_encoder *b)
  10524. {
  10525. /* masks could be asymmetric, so check both ways */
  10526. return a == b || (a->cloneable & (1 << b->type) &&
  10527. b->cloneable & (1 << a->type));
  10528. }
  10529. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  10530. struct intel_crtc *crtc,
  10531. struct intel_encoder *encoder)
  10532. {
  10533. struct intel_encoder *source_encoder;
  10534. struct drm_connector *connector;
  10535. struct drm_connector_state *connector_state;
  10536. int i;
  10537. for_each_connector_in_state(state, connector, connector_state, i) {
  10538. if (connector_state->crtc != &crtc->base)
  10539. continue;
  10540. source_encoder =
  10541. to_intel_encoder(connector_state->best_encoder);
  10542. if (!encoders_cloneable(encoder, source_encoder))
  10543. return false;
  10544. }
  10545. return true;
  10546. }
  10547. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  10548. struct drm_crtc_state *crtc_state)
  10549. {
  10550. struct drm_device *dev = crtc->dev;
  10551. struct drm_i915_private *dev_priv = to_i915(dev);
  10552. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10553. struct intel_crtc_state *pipe_config =
  10554. to_intel_crtc_state(crtc_state);
  10555. struct drm_atomic_state *state = crtc_state->state;
  10556. int ret;
  10557. bool mode_changed = needs_modeset(crtc_state);
  10558. if (mode_changed && !crtc_state->active)
  10559. pipe_config->update_wm_post = true;
  10560. if (mode_changed && crtc_state->enable &&
  10561. dev_priv->display.crtc_compute_clock &&
  10562. !WARN_ON(pipe_config->shared_dpll)) {
  10563. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  10564. pipe_config);
  10565. if (ret)
  10566. return ret;
  10567. }
  10568. if (crtc_state->color_mgmt_changed) {
  10569. ret = intel_color_check(crtc, crtc_state);
  10570. if (ret)
  10571. return ret;
  10572. /*
  10573. * Changing color management on Intel hardware is
  10574. * handled as part of planes update.
  10575. */
  10576. crtc_state->planes_changed = true;
  10577. }
  10578. ret = 0;
  10579. if (dev_priv->display.compute_pipe_wm) {
  10580. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  10581. if (ret) {
  10582. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  10583. return ret;
  10584. }
  10585. }
  10586. if (dev_priv->display.compute_intermediate_wm &&
  10587. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  10588. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  10589. return 0;
  10590. /*
  10591. * Calculate 'intermediate' watermarks that satisfy both the
  10592. * old state and the new state. We can program these
  10593. * immediately.
  10594. */
  10595. ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
  10596. intel_crtc,
  10597. pipe_config);
  10598. if (ret) {
  10599. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  10600. return ret;
  10601. }
  10602. } else if (dev_priv->display.compute_intermediate_wm) {
  10603. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  10604. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  10605. }
  10606. if (INTEL_INFO(dev)->gen >= 9) {
  10607. if (mode_changed)
  10608. ret = skl_update_scaler_crtc(pipe_config);
  10609. if (!ret)
  10610. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  10611. pipe_config);
  10612. }
  10613. return ret;
  10614. }
  10615. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  10616. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  10617. .atomic_begin = intel_begin_crtc_commit,
  10618. .atomic_flush = intel_finish_crtc_commit,
  10619. .atomic_check = intel_crtc_atomic_check,
  10620. };
  10621. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  10622. {
  10623. struct intel_connector *connector;
  10624. for_each_intel_connector(dev, connector) {
  10625. if (connector->base.state->crtc)
  10626. drm_connector_unreference(&connector->base);
  10627. if (connector->base.encoder) {
  10628. connector->base.state->best_encoder =
  10629. connector->base.encoder;
  10630. connector->base.state->crtc =
  10631. connector->base.encoder->crtc;
  10632. drm_connector_reference(&connector->base);
  10633. } else {
  10634. connector->base.state->best_encoder = NULL;
  10635. connector->base.state->crtc = NULL;
  10636. }
  10637. }
  10638. }
  10639. static void
  10640. connected_sink_compute_bpp(struct intel_connector *connector,
  10641. struct intel_crtc_state *pipe_config)
  10642. {
  10643. const struct drm_display_info *info = &connector->base.display_info;
  10644. int bpp = pipe_config->pipe_bpp;
  10645. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  10646. connector->base.base.id,
  10647. connector->base.name);
  10648. /* Don't use an invalid EDID bpc value */
  10649. if (info->bpc != 0 && info->bpc * 3 < bpp) {
  10650. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  10651. bpp, info->bpc * 3);
  10652. pipe_config->pipe_bpp = info->bpc * 3;
  10653. }
  10654. /* Clamp bpp to 8 on screens without EDID 1.4 */
  10655. if (info->bpc == 0 && bpp > 24) {
  10656. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  10657. bpp);
  10658. pipe_config->pipe_bpp = 24;
  10659. }
  10660. }
  10661. static int
  10662. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  10663. struct intel_crtc_state *pipe_config)
  10664. {
  10665. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  10666. struct drm_atomic_state *state;
  10667. struct drm_connector *connector;
  10668. struct drm_connector_state *connector_state;
  10669. int bpp, i;
  10670. if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  10671. IS_CHERRYVIEW(dev_priv)))
  10672. bpp = 10*3;
  10673. else if (INTEL_GEN(dev_priv) >= 5)
  10674. bpp = 12*3;
  10675. else
  10676. bpp = 8*3;
  10677. pipe_config->pipe_bpp = bpp;
  10678. state = pipe_config->base.state;
  10679. /* Clamp display bpp to EDID value */
  10680. for_each_connector_in_state(state, connector, connector_state, i) {
  10681. if (connector_state->crtc != &crtc->base)
  10682. continue;
  10683. connected_sink_compute_bpp(to_intel_connector(connector),
  10684. pipe_config);
  10685. }
  10686. return bpp;
  10687. }
  10688. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  10689. {
  10690. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  10691. "type: 0x%x flags: 0x%x\n",
  10692. mode->crtc_clock,
  10693. mode->crtc_hdisplay, mode->crtc_hsync_start,
  10694. mode->crtc_hsync_end, mode->crtc_htotal,
  10695. mode->crtc_vdisplay, mode->crtc_vsync_start,
  10696. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  10697. }
  10698. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  10699. struct intel_crtc_state *pipe_config,
  10700. const char *context)
  10701. {
  10702. struct drm_device *dev = crtc->base.dev;
  10703. struct drm_i915_private *dev_priv = to_i915(dev);
  10704. struct drm_plane *plane;
  10705. struct intel_plane *intel_plane;
  10706. struct intel_plane_state *state;
  10707. struct drm_framebuffer *fb;
  10708. DRM_DEBUG_KMS("[CRTC:%d:%s]%s config %p for pipe %c\n",
  10709. crtc->base.base.id, crtc->base.name,
  10710. context, pipe_config, pipe_name(crtc->pipe));
  10711. DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
  10712. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  10713. pipe_config->pipe_bpp, pipe_config->dither);
  10714. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10715. pipe_config->has_pch_encoder,
  10716. pipe_config->fdi_lanes,
  10717. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  10718. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  10719. pipe_config->fdi_m_n.tu);
  10720. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10721. intel_crtc_has_dp_encoder(pipe_config),
  10722. pipe_config->lane_count,
  10723. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  10724. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  10725. pipe_config->dp_m_n.tu);
  10726. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  10727. intel_crtc_has_dp_encoder(pipe_config),
  10728. pipe_config->lane_count,
  10729. pipe_config->dp_m2_n2.gmch_m,
  10730. pipe_config->dp_m2_n2.gmch_n,
  10731. pipe_config->dp_m2_n2.link_m,
  10732. pipe_config->dp_m2_n2.link_n,
  10733. pipe_config->dp_m2_n2.tu);
  10734. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  10735. pipe_config->has_audio,
  10736. pipe_config->has_infoframe);
  10737. DRM_DEBUG_KMS("requested mode:\n");
  10738. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  10739. DRM_DEBUG_KMS("adjusted mode:\n");
  10740. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  10741. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  10742. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  10743. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  10744. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  10745. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  10746. crtc->num_scalers,
  10747. pipe_config->scaler_state.scaler_users,
  10748. pipe_config->scaler_state.scaler_id);
  10749. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10750. pipe_config->gmch_pfit.control,
  10751. pipe_config->gmch_pfit.pgm_ratios,
  10752. pipe_config->gmch_pfit.lvds_border_bits);
  10753. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10754. pipe_config->pch_pfit.pos,
  10755. pipe_config->pch_pfit.size,
  10756. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  10757. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  10758. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  10759. if (IS_BROXTON(dev_priv)) {
  10760. DRM_DEBUG_KMS("dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
  10761. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10762. "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
  10763. pipe_config->dpll_hw_state.ebb0,
  10764. pipe_config->dpll_hw_state.ebb4,
  10765. pipe_config->dpll_hw_state.pll0,
  10766. pipe_config->dpll_hw_state.pll1,
  10767. pipe_config->dpll_hw_state.pll2,
  10768. pipe_config->dpll_hw_state.pll3,
  10769. pipe_config->dpll_hw_state.pll6,
  10770. pipe_config->dpll_hw_state.pll8,
  10771. pipe_config->dpll_hw_state.pll9,
  10772. pipe_config->dpll_hw_state.pll10,
  10773. pipe_config->dpll_hw_state.pcsdw12);
  10774. } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
  10775. DRM_DEBUG_KMS("dpll_hw_state: "
  10776. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10777. pipe_config->dpll_hw_state.ctrl1,
  10778. pipe_config->dpll_hw_state.cfgcr1,
  10779. pipe_config->dpll_hw_state.cfgcr2);
  10780. } else if (HAS_DDI(dev_priv)) {
  10781. DRM_DEBUG_KMS("dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
  10782. pipe_config->dpll_hw_state.wrpll,
  10783. pipe_config->dpll_hw_state.spll);
  10784. } else {
  10785. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10786. "fp0: 0x%x, fp1: 0x%x\n",
  10787. pipe_config->dpll_hw_state.dpll,
  10788. pipe_config->dpll_hw_state.dpll_md,
  10789. pipe_config->dpll_hw_state.fp0,
  10790. pipe_config->dpll_hw_state.fp1);
  10791. }
  10792. DRM_DEBUG_KMS("planes on this crtc\n");
  10793. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10794. char *format_name;
  10795. intel_plane = to_intel_plane(plane);
  10796. if (intel_plane->pipe != crtc->pipe)
  10797. continue;
  10798. state = to_intel_plane_state(plane->state);
  10799. fb = state->base.fb;
  10800. if (!fb) {
  10801. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  10802. plane->base.id, plane->name, state->scaler_id);
  10803. continue;
  10804. }
  10805. format_name = drm_get_format_name(fb->pixel_format);
  10806. DRM_DEBUG_KMS("[PLANE:%d:%s] enabled",
  10807. plane->base.id, plane->name);
  10808. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = %s",
  10809. fb->base.id, fb->width, fb->height, format_name);
  10810. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  10811. state->scaler_id,
  10812. state->base.src.x1 >> 16,
  10813. state->base.src.y1 >> 16,
  10814. drm_rect_width(&state->base.src) >> 16,
  10815. drm_rect_height(&state->base.src) >> 16,
  10816. state->base.dst.x1, state->base.dst.y1,
  10817. drm_rect_width(&state->base.dst),
  10818. drm_rect_height(&state->base.dst));
  10819. kfree(format_name);
  10820. }
  10821. }
  10822. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10823. {
  10824. struct drm_device *dev = state->dev;
  10825. struct drm_connector *connector;
  10826. unsigned int used_ports = 0;
  10827. unsigned int used_mst_ports = 0;
  10828. /*
  10829. * Walk the connector list instead of the encoder
  10830. * list to detect the problem on ddi platforms
  10831. * where there's just one encoder per digital port.
  10832. */
  10833. drm_for_each_connector(connector, dev) {
  10834. struct drm_connector_state *connector_state;
  10835. struct intel_encoder *encoder;
  10836. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  10837. if (!connector_state)
  10838. connector_state = connector->state;
  10839. if (!connector_state->best_encoder)
  10840. continue;
  10841. encoder = to_intel_encoder(connector_state->best_encoder);
  10842. WARN_ON(!connector_state->crtc);
  10843. switch (encoder->type) {
  10844. unsigned int port_mask;
  10845. case INTEL_OUTPUT_UNKNOWN:
  10846. if (WARN_ON(!HAS_DDI(to_i915(dev))))
  10847. break;
  10848. case INTEL_OUTPUT_DP:
  10849. case INTEL_OUTPUT_HDMI:
  10850. case INTEL_OUTPUT_EDP:
  10851. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10852. /* the same port mustn't appear more than once */
  10853. if (used_ports & port_mask)
  10854. return false;
  10855. used_ports |= port_mask;
  10856. break;
  10857. case INTEL_OUTPUT_DP_MST:
  10858. used_mst_ports |=
  10859. 1 << enc_to_mst(&encoder->base)->primary->port;
  10860. break;
  10861. default:
  10862. break;
  10863. }
  10864. }
  10865. /* can't mix MST and SST/HDMI on the same port */
  10866. if (used_ports & used_mst_ports)
  10867. return false;
  10868. return true;
  10869. }
  10870. static void
  10871. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10872. {
  10873. struct drm_crtc_state tmp_state;
  10874. struct intel_crtc_scaler_state scaler_state;
  10875. struct intel_dpll_hw_state dpll_hw_state;
  10876. struct intel_shared_dpll *shared_dpll;
  10877. bool force_thru;
  10878. /* FIXME: before the switch to atomic started, a new pipe_config was
  10879. * kzalloc'd. Code that depends on any field being zero should be
  10880. * fixed, so that the crtc_state can be safely duplicated. For now,
  10881. * only fields that are know to not cause problems are preserved. */
  10882. tmp_state = crtc_state->base;
  10883. scaler_state = crtc_state->scaler_state;
  10884. shared_dpll = crtc_state->shared_dpll;
  10885. dpll_hw_state = crtc_state->dpll_hw_state;
  10886. force_thru = crtc_state->pch_pfit.force_thru;
  10887. memset(crtc_state, 0, sizeof *crtc_state);
  10888. crtc_state->base = tmp_state;
  10889. crtc_state->scaler_state = scaler_state;
  10890. crtc_state->shared_dpll = shared_dpll;
  10891. crtc_state->dpll_hw_state = dpll_hw_state;
  10892. crtc_state->pch_pfit.force_thru = force_thru;
  10893. }
  10894. static int
  10895. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10896. struct intel_crtc_state *pipe_config)
  10897. {
  10898. struct drm_atomic_state *state = pipe_config->base.state;
  10899. struct intel_encoder *encoder;
  10900. struct drm_connector *connector;
  10901. struct drm_connector_state *connector_state;
  10902. int base_bpp, ret = -EINVAL;
  10903. int i;
  10904. bool retry = true;
  10905. clear_intel_crtc_state(pipe_config);
  10906. pipe_config->cpu_transcoder =
  10907. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10908. /*
  10909. * Sanitize sync polarity flags based on requested ones. If neither
  10910. * positive or negative polarity is requested, treat this as meaning
  10911. * negative polarity.
  10912. */
  10913. if (!(pipe_config->base.adjusted_mode.flags &
  10914. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10915. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10916. if (!(pipe_config->base.adjusted_mode.flags &
  10917. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10918. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10919. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10920. pipe_config);
  10921. if (base_bpp < 0)
  10922. goto fail;
  10923. /*
  10924. * Determine the real pipe dimensions. Note that stereo modes can
  10925. * increase the actual pipe size due to the frame doubling and
  10926. * insertion of additional space for blanks between the frame. This
  10927. * is stored in the crtc timings. We use the requested mode to do this
  10928. * computation to clearly distinguish it from the adjusted mode, which
  10929. * can be changed by the connectors in the below retry loop.
  10930. */
  10931. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10932. &pipe_config->pipe_src_w,
  10933. &pipe_config->pipe_src_h);
  10934. for_each_connector_in_state(state, connector, connector_state, i) {
  10935. if (connector_state->crtc != crtc)
  10936. continue;
  10937. encoder = to_intel_encoder(connector_state->best_encoder);
  10938. if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
  10939. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  10940. goto fail;
  10941. }
  10942. /*
  10943. * Determine output_types before calling the .compute_config()
  10944. * hooks so that the hooks can use this information safely.
  10945. */
  10946. pipe_config->output_types |= 1 << encoder->type;
  10947. }
  10948. encoder_retry:
  10949. /* Ensure the port clock defaults are reset when retrying. */
  10950. pipe_config->port_clock = 0;
  10951. pipe_config->pixel_multiplier = 1;
  10952. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10953. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10954. CRTC_STEREO_DOUBLE);
  10955. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10956. * adjust it according to limitations or connector properties, and also
  10957. * a chance to reject the mode entirely.
  10958. */
  10959. for_each_connector_in_state(state, connector, connector_state, i) {
  10960. if (connector_state->crtc != crtc)
  10961. continue;
  10962. encoder = to_intel_encoder(connector_state->best_encoder);
  10963. if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
  10964. DRM_DEBUG_KMS("Encoder config failure\n");
  10965. goto fail;
  10966. }
  10967. }
  10968. /* Set default port clock if not overwritten by the encoder. Needs to be
  10969. * done afterwards in case the encoder adjusts the mode. */
  10970. if (!pipe_config->port_clock)
  10971. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10972. * pipe_config->pixel_multiplier;
  10973. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10974. if (ret < 0) {
  10975. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10976. goto fail;
  10977. }
  10978. if (ret == RETRY) {
  10979. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10980. ret = -EINVAL;
  10981. goto fail;
  10982. }
  10983. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10984. retry = false;
  10985. goto encoder_retry;
  10986. }
  10987. /* Dithering seems to not pass-through bits correctly when it should, so
  10988. * only enable it on 6bpc panels. */
  10989. pipe_config->dither = pipe_config->pipe_bpp == 6*3;
  10990. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  10991. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10992. fail:
  10993. return ret;
  10994. }
  10995. static void
  10996. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  10997. {
  10998. struct drm_crtc *crtc;
  10999. struct drm_crtc_state *crtc_state;
  11000. int i;
  11001. /* Double check state. */
  11002. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11003. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  11004. /* Update hwmode for vblank functions */
  11005. if (crtc->state->active)
  11006. crtc->hwmode = crtc->state->adjusted_mode;
  11007. else
  11008. crtc->hwmode.crtc_clock = 0;
  11009. /*
  11010. * Update legacy state to satisfy fbc code. This can
  11011. * be removed when fbc uses the atomic state.
  11012. */
  11013. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  11014. struct drm_plane_state *plane_state = crtc->primary->state;
  11015. crtc->primary->fb = plane_state->fb;
  11016. crtc->x = plane_state->src_x >> 16;
  11017. crtc->y = plane_state->src_y >> 16;
  11018. }
  11019. }
  11020. }
  11021. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  11022. {
  11023. int diff;
  11024. if (clock1 == clock2)
  11025. return true;
  11026. if (!clock1 || !clock2)
  11027. return false;
  11028. diff = abs(clock1 - clock2);
  11029. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  11030. return true;
  11031. return false;
  11032. }
  11033. static bool
  11034. intel_compare_m_n(unsigned int m, unsigned int n,
  11035. unsigned int m2, unsigned int n2,
  11036. bool exact)
  11037. {
  11038. if (m == m2 && n == n2)
  11039. return true;
  11040. if (exact || !m || !n || !m2 || !n2)
  11041. return false;
  11042. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  11043. if (n > n2) {
  11044. while (n > n2) {
  11045. m2 <<= 1;
  11046. n2 <<= 1;
  11047. }
  11048. } else if (n < n2) {
  11049. while (n < n2) {
  11050. m <<= 1;
  11051. n <<= 1;
  11052. }
  11053. }
  11054. if (n != n2)
  11055. return false;
  11056. return intel_fuzzy_clock_check(m, m2);
  11057. }
  11058. static bool
  11059. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  11060. struct intel_link_m_n *m2_n2,
  11061. bool adjust)
  11062. {
  11063. if (m_n->tu == m2_n2->tu &&
  11064. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  11065. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  11066. intel_compare_m_n(m_n->link_m, m_n->link_n,
  11067. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  11068. if (adjust)
  11069. *m2_n2 = *m_n;
  11070. return true;
  11071. }
  11072. return false;
  11073. }
  11074. static bool
  11075. intel_pipe_config_compare(struct drm_device *dev,
  11076. struct intel_crtc_state *current_config,
  11077. struct intel_crtc_state *pipe_config,
  11078. bool adjust)
  11079. {
  11080. struct drm_i915_private *dev_priv = to_i915(dev);
  11081. bool ret = true;
  11082. #define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
  11083. do { \
  11084. if (!adjust) \
  11085. DRM_ERROR(fmt, ##__VA_ARGS__); \
  11086. else \
  11087. DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
  11088. } while (0)
  11089. #define PIPE_CONF_CHECK_X(name) \
  11090. if (current_config->name != pipe_config->name) { \
  11091. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  11092. "(expected 0x%08x, found 0x%08x)\n", \
  11093. current_config->name, \
  11094. pipe_config->name); \
  11095. ret = false; \
  11096. }
  11097. #define PIPE_CONF_CHECK_I(name) \
  11098. if (current_config->name != pipe_config->name) { \
  11099. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  11100. "(expected %i, found %i)\n", \
  11101. current_config->name, \
  11102. pipe_config->name); \
  11103. ret = false; \
  11104. }
  11105. #define PIPE_CONF_CHECK_P(name) \
  11106. if (current_config->name != pipe_config->name) { \
  11107. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  11108. "(expected %p, found %p)\n", \
  11109. current_config->name, \
  11110. pipe_config->name); \
  11111. ret = false; \
  11112. }
  11113. #define PIPE_CONF_CHECK_M_N(name) \
  11114. if (!intel_compare_link_m_n(&current_config->name, \
  11115. &pipe_config->name,\
  11116. adjust)) { \
  11117. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  11118. "(expected tu %i gmch %i/%i link %i/%i, " \
  11119. "found tu %i, gmch %i/%i link %i/%i)\n", \
  11120. current_config->name.tu, \
  11121. current_config->name.gmch_m, \
  11122. current_config->name.gmch_n, \
  11123. current_config->name.link_m, \
  11124. current_config->name.link_n, \
  11125. pipe_config->name.tu, \
  11126. pipe_config->name.gmch_m, \
  11127. pipe_config->name.gmch_n, \
  11128. pipe_config->name.link_m, \
  11129. pipe_config->name.link_n); \
  11130. ret = false; \
  11131. }
  11132. /* This is required for BDW+ where there is only one set of registers for
  11133. * switching between high and low RR.
  11134. * This macro can be used whenever a comparison has to be made between one
  11135. * hw state and multiple sw state variables.
  11136. */
  11137. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  11138. if (!intel_compare_link_m_n(&current_config->name, \
  11139. &pipe_config->name, adjust) && \
  11140. !intel_compare_link_m_n(&current_config->alt_name, \
  11141. &pipe_config->name, adjust)) { \
  11142. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  11143. "(expected tu %i gmch %i/%i link %i/%i, " \
  11144. "or tu %i gmch %i/%i link %i/%i, " \
  11145. "found tu %i, gmch %i/%i link %i/%i)\n", \
  11146. current_config->name.tu, \
  11147. current_config->name.gmch_m, \
  11148. current_config->name.gmch_n, \
  11149. current_config->name.link_m, \
  11150. current_config->name.link_n, \
  11151. current_config->alt_name.tu, \
  11152. current_config->alt_name.gmch_m, \
  11153. current_config->alt_name.gmch_n, \
  11154. current_config->alt_name.link_m, \
  11155. current_config->alt_name.link_n, \
  11156. pipe_config->name.tu, \
  11157. pipe_config->name.gmch_m, \
  11158. pipe_config->name.gmch_n, \
  11159. pipe_config->name.link_m, \
  11160. pipe_config->name.link_n); \
  11161. ret = false; \
  11162. }
  11163. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  11164. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  11165. INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
  11166. "(expected %i, found %i)\n", \
  11167. current_config->name & (mask), \
  11168. pipe_config->name & (mask)); \
  11169. ret = false; \
  11170. }
  11171. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  11172. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  11173. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  11174. "(expected %i, found %i)\n", \
  11175. current_config->name, \
  11176. pipe_config->name); \
  11177. ret = false; \
  11178. }
  11179. #define PIPE_CONF_QUIRK(quirk) \
  11180. ((current_config->quirks | pipe_config->quirks) & (quirk))
  11181. PIPE_CONF_CHECK_I(cpu_transcoder);
  11182. PIPE_CONF_CHECK_I(has_pch_encoder);
  11183. PIPE_CONF_CHECK_I(fdi_lanes);
  11184. PIPE_CONF_CHECK_M_N(fdi_m_n);
  11185. PIPE_CONF_CHECK_I(lane_count);
  11186. PIPE_CONF_CHECK_X(lane_lat_optim_mask);
  11187. if (INTEL_INFO(dev)->gen < 8) {
  11188. PIPE_CONF_CHECK_M_N(dp_m_n);
  11189. if (current_config->has_drrs)
  11190. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  11191. } else
  11192. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  11193. PIPE_CONF_CHECK_X(output_types);
  11194. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  11195. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  11196. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  11197. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  11198. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  11199. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  11200. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  11201. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  11202. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  11203. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  11204. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  11205. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  11206. PIPE_CONF_CHECK_I(pixel_multiplier);
  11207. PIPE_CONF_CHECK_I(has_hdmi_sink);
  11208. if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
  11209. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11210. PIPE_CONF_CHECK_I(limited_color_range);
  11211. PIPE_CONF_CHECK_I(has_infoframe);
  11212. PIPE_CONF_CHECK_I(has_audio);
  11213. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11214. DRM_MODE_FLAG_INTERLACE);
  11215. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  11216. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11217. DRM_MODE_FLAG_PHSYNC);
  11218. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11219. DRM_MODE_FLAG_NHSYNC);
  11220. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11221. DRM_MODE_FLAG_PVSYNC);
  11222. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11223. DRM_MODE_FLAG_NVSYNC);
  11224. }
  11225. PIPE_CONF_CHECK_X(gmch_pfit.control);
  11226. /* pfit ratios are autocomputed by the hw on gen4+ */
  11227. if (INTEL_INFO(dev)->gen < 4)
  11228. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  11229. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  11230. if (!adjust) {
  11231. PIPE_CONF_CHECK_I(pipe_src_w);
  11232. PIPE_CONF_CHECK_I(pipe_src_h);
  11233. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  11234. if (current_config->pch_pfit.enabled) {
  11235. PIPE_CONF_CHECK_X(pch_pfit.pos);
  11236. PIPE_CONF_CHECK_X(pch_pfit.size);
  11237. }
  11238. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  11239. }
  11240. /* BDW+ don't expose a synchronous way to read the state */
  11241. if (IS_HASWELL(dev_priv))
  11242. PIPE_CONF_CHECK_I(ips_enabled);
  11243. PIPE_CONF_CHECK_I(double_wide);
  11244. PIPE_CONF_CHECK_P(shared_dpll);
  11245. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  11246. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  11247. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  11248. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  11249. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  11250. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  11251. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  11252. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  11253. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  11254. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  11255. PIPE_CONF_CHECK_X(dsi_pll.div);
  11256. if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
  11257. PIPE_CONF_CHECK_I(pipe_bpp);
  11258. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  11259. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  11260. #undef PIPE_CONF_CHECK_X
  11261. #undef PIPE_CONF_CHECK_I
  11262. #undef PIPE_CONF_CHECK_P
  11263. #undef PIPE_CONF_CHECK_FLAGS
  11264. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  11265. #undef PIPE_CONF_QUIRK
  11266. #undef INTEL_ERR_OR_DBG_KMS
  11267. return ret;
  11268. }
  11269. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  11270. const struct intel_crtc_state *pipe_config)
  11271. {
  11272. if (pipe_config->has_pch_encoder) {
  11273. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  11274. &pipe_config->fdi_m_n);
  11275. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  11276. /*
  11277. * FDI already provided one idea for the dotclock.
  11278. * Yell if the encoder disagrees.
  11279. */
  11280. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  11281. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  11282. fdi_dotclock, dotclock);
  11283. }
  11284. }
  11285. static void verify_wm_state(struct drm_crtc *crtc,
  11286. struct drm_crtc_state *new_state)
  11287. {
  11288. struct drm_device *dev = crtc->dev;
  11289. struct drm_i915_private *dev_priv = to_i915(dev);
  11290. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  11291. struct skl_pipe_wm hw_wm, *sw_wm;
  11292. struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
  11293. struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
  11294. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11295. const enum pipe pipe = intel_crtc->pipe;
  11296. int plane, level, max_level = ilk_wm_max_level(dev_priv);
  11297. if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
  11298. return;
  11299. skl_pipe_wm_get_hw_state(crtc, &hw_wm);
  11300. sw_wm = &intel_crtc->wm.active.skl;
  11301. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  11302. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  11303. /* planes */
  11304. for_each_plane(dev_priv, pipe, plane) {
  11305. hw_plane_wm = &hw_wm.planes[plane];
  11306. sw_plane_wm = &sw_wm->planes[plane];
  11307. /* Watermarks */
  11308. for (level = 0; level <= max_level; level++) {
  11309. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  11310. &sw_plane_wm->wm[level]))
  11311. continue;
  11312. 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",
  11313. pipe_name(pipe), plane + 1, level,
  11314. sw_plane_wm->wm[level].plane_en,
  11315. sw_plane_wm->wm[level].plane_res_b,
  11316. sw_plane_wm->wm[level].plane_res_l,
  11317. hw_plane_wm->wm[level].plane_en,
  11318. hw_plane_wm->wm[level].plane_res_b,
  11319. hw_plane_wm->wm[level].plane_res_l);
  11320. }
  11321. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  11322. &sw_plane_wm->trans_wm)) {
  11323. 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",
  11324. pipe_name(pipe), plane + 1,
  11325. sw_plane_wm->trans_wm.plane_en,
  11326. sw_plane_wm->trans_wm.plane_res_b,
  11327. sw_plane_wm->trans_wm.plane_res_l,
  11328. hw_plane_wm->trans_wm.plane_en,
  11329. hw_plane_wm->trans_wm.plane_res_b,
  11330. hw_plane_wm->trans_wm.plane_res_l);
  11331. }
  11332. /* DDB */
  11333. hw_ddb_entry = &hw_ddb.plane[pipe][plane];
  11334. sw_ddb_entry = &sw_ddb->plane[pipe][plane];
  11335. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  11336. DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
  11337. pipe_name(pipe), plane + 1,
  11338. sw_ddb_entry->start, sw_ddb_entry->end,
  11339. hw_ddb_entry->start, hw_ddb_entry->end);
  11340. }
  11341. }
  11342. /*
  11343. * cursor
  11344. * If the cursor plane isn't active, we may not have updated it's ddb
  11345. * allocation. In that case since the ddb allocation will be updated
  11346. * once the plane becomes visible, we can skip this check
  11347. */
  11348. if (intel_crtc->cursor_addr) {
  11349. hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
  11350. sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
  11351. /* Watermarks */
  11352. for (level = 0; level <= max_level; level++) {
  11353. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  11354. &sw_plane_wm->wm[level]))
  11355. continue;
  11356. 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",
  11357. pipe_name(pipe), level,
  11358. sw_plane_wm->wm[level].plane_en,
  11359. sw_plane_wm->wm[level].plane_res_b,
  11360. sw_plane_wm->wm[level].plane_res_l,
  11361. hw_plane_wm->wm[level].plane_en,
  11362. hw_plane_wm->wm[level].plane_res_b,
  11363. hw_plane_wm->wm[level].plane_res_l);
  11364. }
  11365. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  11366. &sw_plane_wm->trans_wm)) {
  11367. DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  11368. pipe_name(pipe),
  11369. sw_plane_wm->trans_wm.plane_en,
  11370. sw_plane_wm->trans_wm.plane_res_b,
  11371. sw_plane_wm->trans_wm.plane_res_l,
  11372. hw_plane_wm->trans_wm.plane_en,
  11373. hw_plane_wm->trans_wm.plane_res_b,
  11374. hw_plane_wm->trans_wm.plane_res_l);
  11375. }
  11376. /* DDB */
  11377. hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  11378. sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  11379. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  11380. DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
  11381. pipe_name(pipe),
  11382. sw_ddb_entry->start, sw_ddb_entry->end,
  11383. hw_ddb_entry->start, hw_ddb_entry->end);
  11384. }
  11385. }
  11386. }
  11387. static void
  11388. verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
  11389. {
  11390. struct drm_connector *connector;
  11391. drm_for_each_connector(connector, dev) {
  11392. struct drm_encoder *encoder = connector->encoder;
  11393. struct drm_connector_state *state = connector->state;
  11394. if (state->crtc != crtc)
  11395. continue;
  11396. intel_connector_verify_state(to_intel_connector(connector));
  11397. I915_STATE_WARN(state->best_encoder != encoder,
  11398. "connector's atomic encoder doesn't match legacy encoder\n");
  11399. }
  11400. }
  11401. static void
  11402. verify_encoder_state(struct drm_device *dev)
  11403. {
  11404. struct intel_encoder *encoder;
  11405. struct intel_connector *connector;
  11406. for_each_intel_encoder(dev, encoder) {
  11407. bool enabled = false;
  11408. enum pipe pipe;
  11409. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  11410. encoder->base.base.id,
  11411. encoder->base.name);
  11412. for_each_intel_connector(dev, connector) {
  11413. if (connector->base.state->best_encoder != &encoder->base)
  11414. continue;
  11415. enabled = true;
  11416. I915_STATE_WARN(connector->base.state->crtc !=
  11417. encoder->base.crtc,
  11418. "connector's crtc doesn't match encoder crtc\n");
  11419. }
  11420. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  11421. "encoder's enabled state mismatch "
  11422. "(expected %i, found %i)\n",
  11423. !!encoder->base.crtc, enabled);
  11424. if (!encoder->base.crtc) {
  11425. bool active;
  11426. active = encoder->get_hw_state(encoder, &pipe);
  11427. I915_STATE_WARN(active,
  11428. "encoder detached but still enabled on pipe %c.\n",
  11429. pipe_name(pipe));
  11430. }
  11431. }
  11432. }
  11433. static void
  11434. verify_crtc_state(struct drm_crtc *crtc,
  11435. struct drm_crtc_state *old_crtc_state,
  11436. struct drm_crtc_state *new_crtc_state)
  11437. {
  11438. struct drm_device *dev = crtc->dev;
  11439. struct drm_i915_private *dev_priv = to_i915(dev);
  11440. struct intel_encoder *encoder;
  11441. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11442. struct intel_crtc_state *pipe_config, *sw_config;
  11443. struct drm_atomic_state *old_state;
  11444. bool active;
  11445. old_state = old_crtc_state->state;
  11446. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  11447. pipe_config = to_intel_crtc_state(old_crtc_state);
  11448. memset(pipe_config, 0, sizeof(*pipe_config));
  11449. pipe_config->base.crtc = crtc;
  11450. pipe_config->base.state = old_state;
  11451. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  11452. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  11453. /* hw state is inconsistent with the pipe quirk */
  11454. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  11455. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  11456. active = new_crtc_state->active;
  11457. I915_STATE_WARN(new_crtc_state->active != active,
  11458. "crtc active state doesn't match with hw state "
  11459. "(expected %i, found %i)\n", new_crtc_state->active, active);
  11460. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  11461. "transitional active state does not match atomic hw state "
  11462. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  11463. for_each_encoder_on_crtc(dev, crtc, encoder) {
  11464. enum pipe pipe;
  11465. active = encoder->get_hw_state(encoder, &pipe);
  11466. I915_STATE_WARN(active != new_crtc_state->active,
  11467. "[ENCODER:%i] active %i with crtc active %i\n",
  11468. encoder->base.base.id, active, new_crtc_state->active);
  11469. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  11470. "Encoder connected to wrong pipe %c\n",
  11471. pipe_name(pipe));
  11472. if (active) {
  11473. pipe_config->output_types |= 1 << encoder->type;
  11474. encoder->get_config(encoder, pipe_config);
  11475. }
  11476. }
  11477. if (!new_crtc_state->active)
  11478. return;
  11479. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  11480. sw_config = to_intel_crtc_state(crtc->state);
  11481. if (!intel_pipe_config_compare(dev, sw_config,
  11482. pipe_config, false)) {
  11483. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  11484. intel_dump_pipe_config(intel_crtc, pipe_config,
  11485. "[hw state]");
  11486. intel_dump_pipe_config(intel_crtc, sw_config,
  11487. "[sw state]");
  11488. }
  11489. }
  11490. static void
  11491. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  11492. struct intel_shared_dpll *pll,
  11493. struct drm_crtc *crtc,
  11494. struct drm_crtc_state *new_state)
  11495. {
  11496. struct intel_dpll_hw_state dpll_hw_state;
  11497. unsigned crtc_mask;
  11498. bool active;
  11499. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  11500. DRM_DEBUG_KMS("%s\n", pll->name);
  11501. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  11502. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  11503. I915_STATE_WARN(!pll->on && pll->active_mask,
  11504. "pll in active use but not on in sw tracking\n");
  11505. I915_STATE_WARN(pll->on && !pll->active_mask,
  11506. "pll is on but not used by any active crtc\n");
  11507. I915_STATE_WARN(pll->on != active,
  11508. "pll on state mismatch (expected %i, found %i)\n",
  11509. pll->on, active);
  11510. }
  11511. if (!crtc) {
  11512. I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
  11513. "more active pll users than references: %x vs %x\n",
  11514. pll->active_mask, pll->config.crtc_mask);
  11515. return;
  11516. }
  11517. crtc_mask = 1 << drm_crtc_index(crtc);
  11518. if (new_state->active)
  11519. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  11520. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  11521. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  11522. else
  11523. I915_STATE_WARN(pll->active_mask & crtc_mask,
  11524. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  11525. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  11526. I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
  11527. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  11528. crtc_mask, pll->config.crtc_mask);
  11529. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
  11530. &dpll_hw_state,
  11531. sizeof(dpll_hw_state)),
  11532. "pll hw state mismatch\n");
  11533. }
  11534. static void
  11535. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  11536. struct drm_crtc_state *old_crtc_state,
  11537. struct drm_crtc_state *new_crtc_state)
  11538. {
  11539. struct drm_i915_private *dev_priv = to_i915(dev);
  11540. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  11541. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  11542. if (new_state->shared_dpll)
  11543. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  11544. if (old_state->shared_dpll &&
  11545. old_state->shared_dpll != new_state->shared_dpll) {
  11546. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  11547. struct intel_shared_dpll *pll = old_state->shared_dpll;
  11548. I915_STATE_WARN(pll->active_mask & crtc_mask,
  11549. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  11550. pipe_name(drm_crtc_index(crtc)));
  11551. I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
  11552. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  11553. pipe_name(drm_crtc_index(crtc)));
  11554. }
  11555. }
  11556. static void
  11557. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  11558. struct drm_crtc_state *old_state,
  11559. struct drm_crtc_state *new_state)
  11560. {
  11561. if (!needs_modeset(new_state) &&
  11562. !to_intel_crtc_state(new_state)->update_pipe)
  11563. return;
  11564. verify_wm_state(crtc, new_state);
  11565. verify_connector_state(crtc->dev, crtc);
  11566. verify_crtc_state(crtc, old_state, new_state);
  11567. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  11568. }
  11569. static void
  11570. verify_disabled_dpll_state(struct drm_device *dev)
  11571. {
  11572. struct drm_i915_private *dev_priv = to_i915(dev);
  11573. int i;
  11574. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  11575. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  11576. }
  11577. static void
  11578. intel_modeset_verify_disabled(struct drm_device *dev)
  11579. {
  11580. verify_encoder_state(dev);
  11581. verify_connector_state(dev, NULL);
  11582. verify_disabled_dpll_state(dev);
  11583. }
  11584. static void update_scanline_offset(struct intel_crtc *crtc)
  11585. {
  11586. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  11587. /*
  11588. * The scanline counter increments at the leading edge of hsync.
  11589. *
  11590. * On most platforms it starts counting from vtotal-1 on the
  11591. * first active line. That means the scanline counter value is
  11592. * always one less than what we would expect. Ie. just after
  11593. * start of vblank, which also occurs at start of hsync (on the
  11594. * last active line), the scanline counter will read vblank_start-1.
  11595. *
  11596. * On gen2 the scanline counter starts counting from 1 instead
  11597. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  11598. * to keep the value positive), instead of adding one.
  11599. *
  11600. * On HSW+ the behaviour of the scanline counter depends on the output
  11601. * type. For DP ports it behaves like most other platforms, but on HDMI
  11602. * there's an extra 1 line difference. So we need to add two instead of
  11603. * one to the value.
  11604. */
  11605. if (IS_GEN2(dev_priv)) {
  11606. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  11607. int vtotal;
  11608. vtotal = adjusted_mode->crtc_vtotal;
  11609. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  11610. vtotal /= 2;
  11611. crtc->scanline_offset = vtotal - 1;
  11612. } else if (HAS_DDI(dev_priv) &&
  11613. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
  11614. crtc->scanline_offset = 2;
  11615. } else
  11616. crtc->scanline_offset = 1;
  11617. }
  11618. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  11619. {
  11620. struct drm_device *dev = state->dev;
  11621. struct drm_i915_private *dev_priv = to_i915(dev);
  11622. struct intel_shared_dpll_config *shared_dpll = NULL;
  11623. struct drm_crtc *crtc;
  11624. struct drm_crtc_state *crtc_state;
  11625. int i;
  11626. if (!dev_priv->display.crtc_compute_clock)
  11627. return;
  11628. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11629. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11630. struct intel_shared_dpll *old_dpll =
  11631. to_intel_crtc_state(crtc->state)->shared_dpll;
  11632. if (!needs_modeset(crtc_state))
  11633. continue;
  11634. to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
  11635. if (!old_dpll)
  11636. continue;
  11637. if (!shared_dpll)
  11638. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  11639. intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
  11640. }
  11641. }
  11642. /*
  11643. * This implements the workaround described in the "notes" section of the mode
  11644. * set sequence documentation. When going from no pipes or single pipe to
  11645. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  11646. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  11647. */
  11648. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  11649. {
  11650. struct drm_crtc_state *crtc_state;
  11651. struct intel_crtc *intel_crtc;
  11652. struct drm_crtc *crtc;
  11653. struct intel_crtc_state *first_crtc_state = NULL;
  11654. struct intel_crtc_state *other_crtc_state = NULL;
  11655. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  11656. int i;
  11657. /* look at all crtc's that are going to be enabled in during modeset */
  11658. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11659. intel_crtc = to_intel_crtc(crtc);
  11660. if (!crtc_state->active || !needs_modeset(crtc_state))
  11661. continue;
  11662. if (first_crtc_state) {
  11663. other_crtc_state = to_intel_crtc_state(crtc_state);
  11664. break;
  11665. } else {
  11666. first_crtc_state = to_intel_crtc_state(crtc_state);
  11667. first_pipe = intel_crtc->pipe;
  11668. }
  11669. }
  11670. /* No workaround needed? */
  11671. if (!first_crtc_state)
  11672. return 0;
  11673. /* w/a possibly needed, check how many crtc's are already enabled. */
  11674. for_each_intel_crtc(state->dev, intel_crtc) {
  11675. struct intel_crtc_state *pipe_config;
  11676. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  11677. if (IS_ERR(pipe_config))
  11678. return PTR_ERR(pipe_config);
  11679. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  11680. if (!pipe_config->base.active ||
  11681. needs_modeset(&pipe_config->base))
  11682. continue;
  11683. /* 2 or more enabled crtcs means no need for w/a */
  11684. if (enabled_pipe != INVALID_PIPE)
  11685. return 0;
  11686. enabled_pipe = intel_crtc->pipe;
  11687. }
  11688. if (enabled_pipe != INVALID_PIPE)
  11689. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  11690. else if (other_crtc_state)
  11691. other_crtc_state->hsw_workaround_pipe = first_pipe;
  11692. return 0;
  11693. }
  11694. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  11695. {
  11696. struct drm_crtc *crtc;
  11697. struct drm_crtc_state *crtc_state;
  11698. int ret = 0;
  11699. /* add all active pipes to the state */
  11700. for_each_crtc(state->dev, crtc) {
  11701. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11702. if (IS_ERR(crtc_state))
  11703. return PTR_ERR(crtc_state);
  11704. if (!crtc_state->active || needs_modeset(crtc_state))
  11705. continue;
  11706. crtc_state->mode_changed = true;
  11707. ret = drm_atomic_add_affected_connectors(state, crtc);
  11708. if (ret)
  11709. break;
  11710. ret = drm_atomic_add_affected_planes(state, crtc);
  11711. if (ret)
  11712. break;
  11713. }
  11714. return ret;
  11715. }
  11716. static int intel_modeset_checks(struct drm_atomic_state *state)
  11717. {
  11718. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11719. struct drm_i915_private *dev_priv = to_i915(state->dev);
  11720. struct drm_crtc *crtc;
  11721. struct drm_crtc_state *crtc_state;
  11722. int ret = 0, i;
  11723. if (!check_digital_port_conflicts(state)) {
  11724. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  11725. return -EINVAL;
  11726. }
  11727. intel_state->modeset = true;
  11728. intel_state->active_crtcs = dev_priv->active_crtcs;
  11729. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11730. if (crtc_state->active)
  11731. intel_state->active_crtcs |= 1 << i;
  11732. else
  11733. intel_state->active_crtcs &= ~(1 << i);
  11734. if (crtc_state->active != crtc->state->active)
  11735. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  11736. }
  11737. /*
  11738. * See if the config requires any additional preparation, e.g.
  11739. * to adjust global state with pipes off. We need to do this
  11740. * here so we can get the modeset_pipe updated config for the new
  11741. * mode set on this crtc. For other crtcs we need to use the
  11742. * adjusted_mode bits in the crtc directly.
  11743. */
  11744. if (dev_priv->display.modeset_calc_cdclk) {
  11745. if (!intel_state->cdclk_pll_vco)
  11746. intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
  11747. if (!intel_state->cdclk_pll_vco)
  11748. intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
  11749. ret = dev_priv->display.modeset_calc_cdclk(state);
  11750. if (ret < 0)
  11751. return ret;
  11752. if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
  11753. intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
  11754. ret = intel_modeset_all_pipes(state);
  11755. if (ret < 0)
  11756. return ret;
  11757. DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
  11758. intel_state->cdclk, intel_state->dev_cdclk);
  11759. } else
  11760. to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
  11761. intel_modeset_clear_plls(state);
  11762. if (IS_HASWELL(dev_priv))
  11763. return haswell_mode_set_planes_workaround(state);
  11764. return 0;
  11765. }
  11766. /*
  11767. * Handle calculation of various watermark data at the end of the atomic check
  11768. * phase. The code here should be run after the per-crtc and per-plane 'check'
  11769. * handlers to ensure that all derived state has been updated.
  11770. */
  11771. static int calc_watermark_data(struct drm_atomic_state *state)
  11772. {
  11773. struct drm_device *dev = state->dev;
  11774. struct drm_i915_private *dev_priv = to_i915(dev);
  11775. /* Is there platform-specific watermark information to calculate? */
  11776. if (dev_priv->display.compute_global_watermarks)
  11777. return dev_priv->display.compute_global_watermarks(state);
  11778. return 0;
  11779. }
  11780. /**
  11781. * intel_atomic_check - validate state object
  11782. * @dev: drm device
  11783. * @state: state to validate
  11784. */
  11785. static int intel_atomic_check(struct drm_device *dev,
  11786. struct drm_atomic_state *state)
  11787. {
  11788. struct drm_i915_private *dev_priv = to_i915(dev);
  11789. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11790. struct drm_crtc *crtc;
  11791. struct drm_crtc_state *crtc_state;
  11792. int ret, i;
  11793. bool any_ms = false;
  11794. ret = drm_atomic_helper_check_modeset(dev, state);
  11795. if (ret)
  11796. return ret;
  11797. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11798. struct intel_crtc_state *pipe_config =
  11799. to_intel_crtc_state(crtc_state);
  11800. /* Catch I915_MODE_FLAG_INHERITED */
  11801. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  11802. crtc_state->mode_changed = true;
  11803. if (!needs_modeset(crtc_state))
  11804. continue;
  11805. if (!crtc_state->enable) {
  11806. any_ms = true;
  11807. continue;
  11808. }
  11809. /* FIXME: For only active_changed we shouldn't need to do any
  11810. * state recomputation at all. */
  11811. ret = drm_atomic_add_affected_connectors(state, crtc);
  11812. if (ret)
  11813. return ret;
  11814. ret = intel_modeset_pipe_config(crtc, pipe_config);
  11815. if (ret) {
  11816. intel_dump_pipe_config(to_intel_crtc(crtc),
  11817. pipe_config, "[failed]");
  11818. return ret;
  11819. }
  11820. if (i915.fastboot &&
  11821. intel_pipe_config_compare(dev,
  11822. to_intel_crtc_state(crtc->state),
  11823. pipe_config, true)) {
  11824. crtc_state->mode_changed = false;
  11825. to_intel_crtc_state(crtc_state)->update_pipe = true;
  11826. }
  11827. if (needs_modeset(crtc_state))
  11828. any_ms = true;
  11829. ret = drm_atomic_add_affected_planes(state, crtc);
  11830. if (ret)
  11831. return ret;
  11832. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  11833. needs_modeset(crtc_state) ?
  11834. "[modeset]" : "[fastset]");
  11835. }
  11836. if (any_ms) {
  11837. ret = intel_modeset_checks(state);
  11838. if (ret)
  11839. return ret;
  11840. } else
  11841. intel_state->cdclk = dev_priv->cdclk_freq;
  11842. ret = drm_atomic_helper_check_planes(dev, state);
  11843. if (ret)
  11844. return ret;
  11845. intel_fbc_choose_crtc(dev_priv, state);
  11846. return calc_watermark_data(state);
  11847. }
  11848. static int intel_atomic_prepare_commit(struct drm_device *dev,
  11849. struct drm_atomic_state *state,
  11850. bool nonblock)
  11851. {
  11852. struct drm_i915_private *dev_priv = to_i915(dev);
  11853. struct drm_plane_state *plane_state;
  11854. struct drm_crtc_state *crtc_state;
  11855. struct drm_plane *plane;
  11856. struct drm_crtc *crtc;
  11857. int i, ret;
  11858. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11859. if (state->legacy_cursor_update)
  11860. continue;
  11861. ret = intel_crtc_wait_for_pending_flips(crtc);
  11862. if (ret)
  11863. return ret;
  11864. if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
  11865. flush_workqueue(dev_priv->wq);
  11866. }
  11867. ret = mutex_lock_interruptible(&dev->struct_mutex);
  11868. if (ret)
  11869. return ret;
  11870. ret = drm_atomic_helper_prepare_planes(dev, state);
  11871. mutex_unlock(&dev->struct_mutex);
  11872. if (!ret && !nonblock) {
  11873. for_each_plane_in_state(state, plane, plane_state, i) {
  11874. struct intel_plane_state *intel_plane_state =
  11875. to_intel_plane_state(plane_state);
  11876. if (!intel_plane_state->wait_req)
  11877. continue;
  11878. ret = i915_wait_request(intel_plane_state->wait_req,
  11879. I915_WAIT_INTERRUPTIBLE,
  11880. NULL, NULL);
  11881. if (ret) {
  11882. /* Any hang should be swallowed by the wait */
  11883. WARN_ON(ret == -EIO);
  11884. mutex_lock(&dev->struct_mutex);
  11885. drm_atomic_helper_cleanup_planes(dev, state);
  11886. mutex_unlock(&dev->struct_mutex);
  11887. break;
  11888. }
  11889. }
  11890. }
  11891. return ret;
  11892. }
  11893. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  11894. {
  11895. struct drm_device *dev = crtc->base.dev;
  11896. if (!dev->max_vblank_count)
  11897. return drm_accurate_vblank_count(&crtc->base);
  11898. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  11899. }
  11900. static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
  11901. struct drm_i915_private *dev_priv,
  11902. unsigned crtc_mask)
  11903. {
  11904. unsigned last_vblank_count[I915_MAX_PIPES];
  11905. enum pipe pipe;
  11906. int ret;
  11907. if (!crtc_mask)
  11908. return;
  11909. for_each_pipe(dev_priv, pipe) {
  11910. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  11911. if (!((1 << pipe) & crtc_mask))
  11912. continue;
  11913. ret = drm_crtc_vblank_get(crtc);
  11914. if (WARN_ON(ret != 0)) {
  11915. crtc_mask &= ~(1 << pipe);
  11916. continue;
  11917. }
  11918. last_vblank_count[pipe] = drm_crtc_vblank_count(crtc);
  11919. }
  11920. for_each_pipe(dev_priv, pipe) {
  11921. struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
  11922. long lret;
  11923. if (!((1 << pipe) & crtc_mask))
  11924. continue;
  11925. lret = wait_event_timeout(dev->vblank[pipe].queue,
  11926. last_vblank_count[pipe] !=
  11927. drm_crtc_vblank_count(crtc),
  11928. msecs_to_jiffies(50));
  11929. WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
  11930. drm_crtc_vblank_put(crtc);
  11931. }
  11932. }
  11933. static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
  11934. {
  11935. /* fb updated, need to unpin old fb */
  11936. if (crtc_state->fb_changed)
  11937. return true;
  11938. /* wm changes, need vblank before final wm's */
  11939. if (crtc_state->update_wm_post)
  11940. return true;
  11941. /*
  11942. * cxsr is re-enabled after vblank.
  11943. * This is already handled by crtc_state->update_wm_post,
  11944. * but added for clarity.
  11945. */
  11946. if (crtc_state->disable_cxsr)
  11947. return true;
  11948. return false;
  11949. }
  11950. static void intel_update_crtc(struct drm_crtc *crtc,
  11951. struct drm_atomic_state *state,
  11952. struct drm_crtc_state *old_crtc_state,
  11953. unsigned int *crtc_vblank_mask)
  11954. {
  11955. struct drm_device *dev = crtc->dev;
  11956. struct drm_i915_private *dev_priv = to_i915(dev);
  11957. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11958. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc->state);
  11959. bool modeset = needs_modeset(crtc->state);
  11960. if (modeset) {
  11961. update_scanline_offset(intel_crtc);
  11962. dev_priv->display.crtc_enable(pipe_config, state);
  11963. } else {
  11964. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  11965. }
  11966. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  11967. intel_fbc_enable(
  11968. intel_crtc, pipe_config,
  11969. to_intel_plane_state(crtc->primary->state));
  11970. }
  11971. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  11972. if (needs_vblank_wait(pipe_config))
  11973. *crtc_vblank_mask |= drm_crtc_mask(crtc);
  11974. }
  11975. static void intel_update_crtcs(struct drm_atomic_state *state,
  11976. unsigned int *crtc_vblank_mask)
  11977. {
  11978. struct drm_crtc *crtc;
  11979. struct drm_crtc_state *old_crtc_state;
  11980. int i;
  11981. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11982. if (!crtc->state->active)
  11983. continue;
  11984. intel_update_crtc(crtc, state, old_crtc_state,
  11985. crtc_vblank_mask);
  11986. }
  11987. }
  11988. static void skl_update_crtcs(struct drm_atomic_state *state,
  11989. unsigned int *crtc_vblank_mask)
  11990. {
  11991. struct drm_device *dev = state->dev;
  11992. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11993. struct drm_crtc *crtc;
  11994. struct intel_crtc *intel_crtc;
  11995. struct drm_crtc_state *old_crtc_state;
  11996. struct intel_crtc_state *cstate;
  11997. unsigned int updated = 0;
  11998. bool progress;
  11999. enum pipe pipe;
  12000. /*
  12001. * Whenever the number of active pipes changes, we need to make sure we
  12002. * update the pipes in the right order so that their ddb allocations
  12003. * never overlap with eachother inbetween CRTC updates. Otherwise we'll
  12004. * cause pipe underruns and other bad stuff.
  12005. */
  12006. do {
  12007. int i;
  12008. progress = false;
  12009. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12010. bool vbl_wait = false;
  12011. unsigned int cmask = drm_crtc_mask(crtc);
  12012. intel_crtc = to_intel_crtc(crtc);
  12013. cstate = to_intel_crtc_state(crtc->state);
  12014. pipe = intel_crtc->pipe;
  12015. if (updated & cmask || !crtc->state->active)
  12016. continue;
  12017. if (skl_ddb_allocation_overlaps(state, intel_crtc))
  12018. continue;
  12019. updated |= cmask;
  12020. /*
  12021. * If this is an already active pipe, it's DDB changed,
  12022. * and this isn't the last pipe that needs updating
  12023. * then we need to wait for a vblank to pass for the
  12024. * new ddb allocation to take effect.
  12025. */
  12026. if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
  12027. &intel_crtc->hw_ddb) &&
  12028. !crtc->state->active_changed &&
  12029. intel_state->wm_results.dirty_pipes != updated)
  12030. vbl_wait = true;
  12031. intel_update_crtc(crtc, state, old_crtc_state,
  12032. crtc_vblank_mask);
  12033. if (vbl_wait)
  12034. intel_wait_for_vblank(dev, pipe);
  12035. progress = true;
  12036. }
  12037. } while (progress);
  12038. }
  12039. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  12040. {
  12041. struct drm_device *dev = state->dev;
  12042. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  12043. struct drm_i915_private *dev_priv = to_i915(dev);
  12044. struct drm_crtc_state *old_crtc_state;
  12045. struct drm_crtc *crtc;
  12046. struct intel_crtc_state *intel_cstate;
  12047. struct drm_plane *plane;
  12048. struct drm_plane_state *plane_state;
  12049. bool hw_check = intel_state->modeset;
  12050. unsigned long put_domains[I915_MAX_PIPES] = {};
  12051. unsigned crtc_vblank_mask = 0;
  12052. int i, ret;
  12053. for_each_plane_in_state(state, plane, plane_state, i) {
  12054. struct intel_plane_state *intel_plane_state =
  12055. to_intel_plane_state(plane->state);
  12056. if (!intel_plane_state->wait_req)
  12057. continue;
  12058. ret = i915_wait_request(intel_plane_state->wait_req,
  12059. 0, NULL, NULL);
  12060. /* EIO should be eaten, and we can't get interrupted in the
  12061. * worker, and blocking commits have waited already. */
  12062. WARN_ON(ret);
  12063. }
  12064. drm_atomic_helper_wait_for_dependencies(state);
  12065. if (intel_state->modeset) {
  12066. memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
  12067. sizeof(intel_state->min_pixclk));
  12068. dev_priv->active_crtcs = intel_state->active_crtcs;
  12069. dev_priv->atomic_cdclk_freq = intel_state->cdclk;
  12070. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  12071. }
  12072. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12073. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12074. if (needs_modeset(crtc->state) ||
  12075. to_intel_crtc_state(crtc->state)->update_pipe) {
  12076. hw_check = true;
  12077. put_domains[to_intel_crtc(crtc)->pipe] =
  12078. modeset_get_crtc_power_domains(crtc,
  12079. to_intel_crtc_state(crtc->state));
  12080. }
  12081. if (!needs_modeset(crtc->state))
  12082. continue;
  12083. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  12084. if (old_crtc_state->active) {
  12085. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  12086. dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
  12087. intel_crtc->active = false;
  12088. intel_fbc_disable(intel_crtc);
  12089. intel_disable_shared_dpll(intel_crtc);
  12090. /*
  12091. * Underruns don't always raise
  12092. * interrupts, so check manually.
  12093. */
  12094. intel_check_cpu_fifo_underruns(dev_priv);
  12095. intel_check_pch_fifo_underruns(dev_priv);
  12096. if (!crtc->state->active)
  12097. intel_update_watermarks(crtc);
  12098. }
  12099. }
  12100. /* Only after disabling all output pipelines that will be changed can we
  12101. * update the the output configuration. */
  12102. intel_modeset_update_crtc_state(state);
  12103. if (intel_state->modeset) {
  12104. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  12105. if (dev_priv->display.modeset_commit_cdclk &&
  12106. (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
  12107. intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
  12108. dev_priv->display.modeset_commit_cdclk(state);
  12109. /*
  12110. * SKL workaround: bspec recommends we disable the SAGV when we
  12111. * have more then one pipe enabled
  12112. */
  12113. if (!intel_can_enable_sagv(state))
  12114. intel_disable_sagv(dev_priv);
  12115. intel_modeset_verify_disabled(dev);
  12116. }
  12117. /* Complete the events for pipes that have now been disabled */
  12118. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12119. bool modeset = needs_modeset(crtc->state);
  12120. /* Complete events for now disable pipes here. */
  12121. if (modeset && !crtc->state->active && crtc->state->event) {
  12122. spin_lock_irq(&dev->event_lock);
  12123. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  12124. spin_unlock_irq(&dev->event_lock);
  12125. crtc->state->event = NULL;
  12126. }
  12127. }
  12128. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  12129. dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
  12130. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  12131. * already, but still need the state for the delayed optimization. To
  12132. * fix this:
  12133. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  12134. * - schedule that vblank worker _before_ calling hw_done
  12135. * - at the start of commit_tail, cancel it _synchrously
  12136. * - switch over to the vblank wait helper in the core after that since
  12137. * we don't need out special handling any more.
  12138. */
  12139. if (!state->legacy_cursor_update)
  12140. intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
  12141. /*
  12142. * Now that the vblank has passed, we can go ahead and program the
  12143. * optimal watermarks on platforms that need two-step watermark
  12144. * programming.
  12145. *
  12146. * TODO: Move this (and other cleanup) to an async worker eventually.
  12147. */
  12148. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12149. intel_cstate = to_intel_crtc_state(crtc->state);
  12150. if (dev_priv->display.optimize_watermarks)
  12151. dev_priv->display.optimize_watermarks(intel_cstate);
  12152. }
  12153. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12154. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  12155. if (put_domains[i])
  12156. modeset_put_power_domains(dev_priv, put_domains[i]);
  12157. intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
  12158. }
  12159. if (intel_state->modeset && intel_can_enable_sagv(state))
  12160. intel_enable_sagv(dev_priv);
  12161. drm_atomic_helper_commit_hw_done(state);
  12162. if (intel_state->modeset)
  12163. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  12164. mutex_lock(&dev->struct_mutex);
  12165. drm_atomic_helper_cleanup_planes(dev, state);
  12166. mutex_unlock(&dev->struct_mutex);
  12167. drm_atomic_helper_commit_cleanup_done(state);
  12168. drm_atomic_state_free(state);
  12169. /* As one of the primary mmio accessors, KMS has a high likelihood
  12170. * of triggering bugs in unclaimed access. After we finish
  12171. * modesetting, see if an error has been flagged, and if so
  12172. * enable debugging for the next modeset - and hope we catch
  12173. * the culprit.
  12174. *
  12175. * XXX note that we assume display power is on at this point.
  12176. * This might hold true now but we need to add pm helper to check
  12177. * unclaimed only when the hardware is on, as atomic commits
  12178. * can happen also when the device is completely off.
  12179. */
  12180. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  12181. }
  12182. static void intel_atomic_commit_work(struct work_struct *work)
  12183. {
  12184. struct drm_atomic_state *state = container_of(work,
  12185. struct drm_atomic_state,
  12186. commit_work);
  12187. intel_atomic_commit_tail(state);
  12188. }
  12189. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  12190. {
  12191. struct drm_plane_state *old_plane_state;
  12192. struct drm_plane *plane;
  12193. int i;
  12194. for_each_plane_in_state(state, plane, old_plane_state, i)
  12195. i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
  12196. intel_fb_obj(plane->state->fb),
  12197. to_intel_plane(plane)->frontbuffer_bit);
  12198. }
  12199. /**
  12200. * intel_atomic_commit - commit validated state object
  12201. * @dev: DRM device
  12202. * @state: the top-level driver state object
  12203. * @nonblock: nonblocking commit
  12204. *
  12205. * This function commits a top-level state object that has been validated
  12206. * with drm_atomic_helper_check().
  12207. *
  12208. * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
  12209. * nonblocking commits are only safe for pure plane updates. Everything else
  12210. * should work though.
  12211. *
  12212. * RETURNS
  12213. * Zero for success or -errno.
  12214. */
  12215. static int intel_atomic_commit(struct drm_device *dev,
  12216. struct drm_atomic_state *state,
  12217. bool nonblock)
  12218. {
  12219. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  12220. struct drm_i915_private *dev_priv = to_i915(dev);
  12221. int ret = 0;
  12222. if (intel_state->modeset && nonblock) {
  12223. DRM_DEBUG_KMS("nonblocking commit for modeset not yet implemented.\n");
  12224. return -EINVAL;
  12225. }
  12226. ret = drm_atomic_helper_setup_commit(state, nonblock);
  12227. if (ret)
  12228. return ret;
  12229. INIT_WORK(&state->commit_work, intel_atomic_commit_work);
  12230. ret = intel_atomic_prepare_commit(dev, state, nonblock);
  12231. if (ret) {
  12232. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  12233. return ret;
  12234. }
  12235. drm_atomic_helper_swap_state(state, true);
  12236. dev_priv->wm.distrust_bios_wm = false;
  12237. dev_priv->wm.skl_results = intel_state->wm_results;
  12238. intel_shared_dpll_commit(state);
  12239. intel_atomic_track_fbs(state);
  12240. if (nonblock)
  12241. queue_work(system_unbound_wq, &state->commit_work);
  12242. else
  12243. intel_atomic_commit_tail(state);
  12244. return 0;
  12245. }
  12246. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  12247. {
  12248. struct drm_device *dev = crtc->dev;
  12249. struct drm_atomic_state *state;
  12250. struct drm_crtc_state *crtc_state;
  12251. int ret;
  12252. state = drm_atomic_state_alloc(dev);
  12253. if (!state) {
  12254. DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
  12255. crtc->base.id, crtc->name);
  12256. return;
  12257. }
  12258. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  12259. retry:
  12260. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  12261. ret = PTR_ERR_OR_ZERO(crtc_state);
  12262. if (!ret) {
  12263. if (!crtc_state->active)
  12264. goto out;
  12265. crtc_state->mode_changed = true;
  12266. ret = drm_atomic_commit(state);
  12267. }
  12268. if (ret == -EDEADLK) {
  12269. drm_atomic_state_clear(state);
  12270. drm_modeset_backoff(state->acquire_ctx);
  12271. goto retry;
  12272. }
  12273. if (ret)
  12274. out:
  12275. drm_atomic_state_free(state);
  12276. }
  12277. /*
  12278. * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
  12279. * drm_atomic_helper_legacy_gamma_set() directly.
  12280. */
  12281. static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
  12282. u16 *red, u16 *green, u16 *blue,
  12283. uint32_t size)
  12284. {
  12285. struct drm_device *dev = crtc->dev;
  12286. struct drm_mode_config *config = &dev->mode_config;
  12287. struct drm_crtc_state *state;
  12288. int ret;
  12289. ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
  12290. if (ret)
  12291. return ret;
  12292. /*
  12293. * Make sure we update the legacy properties so this works when
  12294. * atomic is not enabled.
  12295. */
  12296. state = crtc->state;
  12297. drm_object_property_set_value(&crtc->base,
  12298. config->degamma_lut_property,
  12299. (state->degamma_lut) ?
  12300. state->degamma_lut->base.id : 0);
  12301. drm_object_property_set_value(&crtc->base,
  12302. config->ctm_property,
  12303. (state->ctm) ?
  12304. state->ctm->base.id : 0);
  12305. drm_object_property_set_value(&crtc->base,
  12306. config->gamma_lut_property,
  12307. (state->gamma_lut) ?
  12308. state->gamma_lut->base.id : 0);
  12309. return 0;
  12310. }
  12311. static const struct drm_crtc_funcs intel_crtc_funcs = {
  12312. .gamma_set = intel_atomic_legacy_gamma_set,
  12313. .set_config = drm_atomic_helper_set_config,
  12314. .set_property = drm_atomic_helper_crtc_set_property,
  12315. .destroy = intel_crtc_destroy,
  12316. .page_flip = intel_crtc_page_flip,
  12317. .atomic_duplicate_state = intel_crtc_duplicate_state,
  12318. .atomic_destroy_state = intel_crtc_destroy_state,
  12319. };
  12320. /**
  12321. * intel_prepare_plane_fb - Prepare fb for usage on plane
  12322. * @plane: drm plane to prepare for
  12323. * @fb: framebuffer to prepare for presentation
  12324. *
  12325. * Prepares a framebuffer for usage on a display plane. Generally this
  12326. * involves pinning the underlying object and updating the frontbuffer tracking
  12327. * bits. Some older platforms need special physical address handling for
  12328. * cursor planes.
  12329. *
  12330. * Must be called with struct_mutex held.
  12331. *
  12332. * Returns 0 on success, negative error code on failure.
  12333. */
  12334. int
  12335. intel_prepare_plane_fb(struct drm_plane *plane,
  12336. struct drm_plane_state *new_state)
  12337. {
  12338. struct drm_device *dev = plane->dev;
  12339. struct drm_i915_private *dev_priv = to_i915(dev);
  12340. struct drm_framebuffer *fb = new_state->fb;
  12341. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  12342. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  12343. struct reservation_object *resv;
  12344. int ret = 0;
  12345. if (!obj && !old_obj)
  12346. return 0;
  12347. if (old_obj) {
  12348. struct drm_crtc_state *crtc_state =
  12349. drm_atomic_get_existing_crtc_state(new_state->state, plane->state->crtc);
  12350. /* Big Hammer, we also need to ensure that any pending
  12351. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  12352. * current scanout is retired before unpinning the old
  12353. * framebuffer. Note that we rely on userspace rendering
  12354. * into the buffer attached to the pipe they are waiting
  12355. * on. If not, userspace generates a GPU hang with IPEHR
  12356. * point to the MI_WAIT_FOR_EVENT.
  12357. *
  12358. * This should only fail upon a hung GPU, in which case we
  12359. * can safely continue.
  12360. */
  12361. if (needs_modeset(crtc_state))
  12362. ret = i915_gem_object_wait_rendering(old_obj, true);
  12363. if (ret) {
  12364. /* GPU hangs should have been swallowed by the wait */
  12365. WARN_ON(ret == -EIO);
  12366. return ret;
  12367. }
  12368. }
  12369. if (!obj)
  12370. return 0;
  12371. /* For framebuffer backed by dmabuf, wait for fence */
  12372. resv = i915_gem_object_get_dmabuf_resv(obj);
  12373. if (resv) {
  12374. long lret;
  12375. lret = reservation_object_wait_timeout_rcu(resv, false, true,
  12376. MAX_SCHEDULE_TIMEOUT);
  12377. if (lret == -ERESTARTSYS)
  12378. return lret;
  12379. WARN(lret < 0, "waiting returns %li\n", lret);
  12380. }
  12381. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  12382. INTEL_INFO(dev)->cursor_needs_physical) {
  12383. int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
  12384. ret = i915_gem_object_attach_phys(obj, align);
  12385. if (ret)
  12386. DRM_DEBUG_KMS("failed to attach phys object\n");
  12387. } else {
  12388. struct i915_vma *vma;
  12389. vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  12390. if (IS_ERR(vma))
  12391. ret = PTR_ERR(vma);
  12392. }
  12393. if (ret == 0) {
  12394. to_intel_plane_state(new_state)->wait_req =
  12395. i915_gem_active_get(&obj->last_write,
  12396. &obj->base.dev->struct_mutex);
  12397. }
  12398. return ret;
  12399. }
  12400. /**
  12401. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  12402. * @plane: drm plane to clean up for
  12403. * @fb: old framebuffer that was on plane
  12404. *
  12405. * Cleans up a framebuffer that has just been removed from a plane.
  12406. *
  12407. * Must be called with struct_mutex held.
  12408. */
  12409. void
  12410. intel_cleanup_plane_fb(struct drm_plane *plane,
  12411. struct drm_plane_state *old_state)
  12412. {
  12413. struct drm_device *dev = plane->dev;
  12414. struct intel_plane_state *old_intel_state;
  12415. struct intel_plane_state *intel_state = to_intel_plane_state(plane->state);
  12416. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
  12417. struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
  12418. old_intel_state = to_intel_plane_state(old_state);
  12419. if (!obj && !old_obj)
  12420. return;
  12421. if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
  12422. !INTEL_INFO(dev)->cursor_needs_physical))
  12423. intel_unpin_fb_obj(old_state->fb, old_state->rotation);
  12424. i915_gem_request_assign(&intel_state->wait_req, NULL);
  12425. i915_gem_request_assign(&old_intel_state->wait_req, NULL);
  12426. }
  12427. int
  12428. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  12429. {
  12430. int max_scale;
  12431. int crtc_clock, cdclk;
  12432. if (!intel_crtc || !crtc_state->base.enable)
  12433. return DRM_PLANE_HELPER_NO_SCALING;
  12434. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  12435. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  12436. if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
  12437. return DRM_PLANE_HELPER_NO_SCALING;
  12438. /*
  12439. * skl max scale is lower of:
  12440. * close to 3 but not 3, -1 is for that purpose
  12441. * or
  12442. * cdclk/crtc_clock
  12443. */
  12444. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  12445. return max_scale;
  12446. }
  12447. static int
  12448. intel_check_primary_plane(struct drm_plane *plane,
  12449. struct intel_crtc_state *crtc_state,
  12450. struct intel_plane_state *state)
  12451. {
  12452. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  12453. struct drm_crtc *crtc = state->base.crtc;
  12454. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  12455. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  12456. bool can_position = false;
  12457. int ret;
  12458. if (INTEL_GEN(dev_priv) >= 9) {
  12459. /* use scaler when colorkey is not required */
  12460. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  12461. min_scale = 1;
  12462. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  12463. }
  12464. can_position = true;
  12465. }
  12466. ret = drm_plane_helper_check_state(&state->base,
  12467. &state->clip,
  12468. min_scale, max_scale,
  12469. can_position, true);
  12470. if (ret)
  12471. return ret;
  12472. if (!state->base.fb)
  12473. return 0;
  12474. if (INTEL_GEN(dev_priv) >= 9) {
  12475. ret = skl_check_plane_surface(state);
  12476. if (ret)
  12477. return ret;
  12478. }
  12479. return 0;
  12480. }
  12481. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  12482. struct drm_crtc_state *old_crtc_state)
  12483. {
  12484. struct drm_device *dev = crtc->dev;
  12485. struct drm_i915_private *dev_priv = to_i915(dev);
  12486. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12487. struct intel_crtc_state *intel_cstate =
  12488. to_intel_crtc_state(crtc->state);
  12489. struct intel_crtc_state *old_intel_state =
  12490. to_intel_crtc_state(old_crtc_state);
  12491. bool modeset = needs_modeset(crtc->state);
  12492. enum pipe pipe = intel_crtc->pipe;
  12493. /* Perform vblank evasion around commit operation */
  12494. intel_pipe_update_start(intel_crtc);
  12495. if (modeset)
  12496. return;
  12497. if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
  12498. intel_color_set_csc(crtc->state);
  12499. intel_color_load_luts(crtc->state);
  12500. }
  12501. if (intel_cstate->update_pipe) {
  12502. intel_update_pipe_config(intel_crtc, old_intel_state);
  12503. } else if (INTEL_GEN(dev_priv) >= 9) {
  12504. skl_detach_scalers(intel_crtc);
  12505. I915_WRITE(PIPE_WM_LINETIME(pipe),
  12506. intel_cstate->wm.skl.optimal.linetime);
  12507. }
  12508. }
  12509. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  12510. struct drm_crtc_state *old_crtc_state)
  12511. {
  12512. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12513. intel_pipe_update_end(intel_crtc, NULL);
  12514. }
  12515. /**
  12516. * intel_plane_destroy - destroy a plane
  12517. * @plane: plane to destroy
  12518. *
  12519. * Common destruction function for all types of planes (primary, cursor,
  12520. * sprite).
  12521. */
  12522. void intel_plane_destroy(struct drm_plane *plane)
  12523. {
  12524. if (!plane)
  12525. return;
  12526. drm_plane_cleanup(plane);
  12527. kfree(to_intel_plane(plane));
  12528. }
  12529. const struct drm_plane_funcs intel_plane_funcs = {
  12530. .update_plane = drm_atomic_helper_update_plane,
  12531. .disable_plane = drm_atomic_helper_disable_plane,
  12532. .destroy = intel_plane_destroy,
  12533. .set_property = drm_atomic_helper_plane_set_property,
  12534. .atomic_get_property = intel_plane_atomic_get_property,
  12535. .atomic_set_property = intel_plane_atomic_set_property,
  12536. .atomic_duplicate_state = intel_plane_duplicate_state,
  12537. .atomic_destroy_state = intel_plane_destroy_state,
  12538. };
  12539. static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
  12540. int pipe)
  12541. {
  12542. struct drm_i915_private *dev_priv = to_i915(dev);
  12543. struct intel_plane *primary = NULL;
  12544. struct intel_plane_state *state = NULL;
  12545. const uint32_t *intel_primary_formats;
  12546. unsigned int num_formats;
  12547. int ret;
  12548. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  12549. if (!primary)
  12550. goto fail;
  12551. state = intel_create_plane_state(&primary->base);
  12552. if (!state)
  12553. goto fail;
  12554. primary->base.state = &state->base;
  12555. primary->can_scale = false;
  12556. primary->max_downscale = 1;
  12557. if (INTEL_INFO(dev)->gen >= 9) {
  12558. primary->can_scale = true;
  12559. state->scaler_id = -1;
  12560. }
  12561. primary->pipe = pipe;
  12562. primary->plane = pipe;
  12563. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  12564. primary->check_plane = intel_check_primary_plane;
  12565. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
  12566. primary->plane = !pipe;
  12567. if (INTEL_INFO(dev)->gen >= 9) {
  12568. intel_primary_formats = skl_primary_formats;
  12569. num_formats = ARRAY_SIZE(skl_primary_formats);
  12570. primary->update_plane = skylake_update_primary_plane;
  12571. primary->disable_plane = skylake_disable_primary_plane;
  12572. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12573. intel_primary_formats = i965_primary_formats;
  12574. num_formats = ARRAY_SIZE(i965_primary_formats);
  12575. primary->update_plane = ironlake_update_primary_plane;
  12576. primary->disable_plane = i9xx_disable_primary_plane;
  12577. } else if (INTEL_INFO(dev)->gen >= 4) {
  12578. intel_primary_formats = i965_primary_formats;
  12579. num_formats = ARRAY_SIZE(i965_primary_formats);
  12580. primary->update_plane = i9xx_update_primary_plane;
  12581. primary->disable_plane = i9xx_disable_primary_plane;
  12582. } else {
  12583. intel_primary_formats = i8xx_primary_formats;
  12584. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  12585. primary->update_plane = i9xx_update_primary_plane;
  12586. primary->disable_plane = i9xx_disable_primary_plane;
  12587. }
  12588. if (INTEL_INFO(dev)->gen >= 9)
  12589. ret = drm_universal_plane_init(dev, &primary->base, 0,
  12590. &intel_plane_funcs,
  12591. intel_primary_formats, num_formats,
  12592. DRM_PLANE_TYPE_PRIMARY,
  12593. "plane 1%c", pipe_name(pipe));
  12594. else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  12595. ret = drm_universal_plane_init(dev, &primary->base, 0,
  12596. &intel_plane_funcs,
  12597. intel_primary_formats, num_formats,
  12598. DRM_PLANE_TYPE_PRIMARY,
  12599. "primary %c", pipe_name(pipe));
  12600. else
  12601. ret = drm_universal_plane_init(dev, &primary->base, 0,
  12602. &intel_plane_funcs,
  12603. intel_primary_formats, num_formats,
  12604. DRM_PLANE_TYPE_PRIMARY,
  12605. "plane %c", plane_name(primary->plane));
  12606. if (ret)
  12607. goto fail;
  12608. if (INTEL_INFO(dev)->gen >= 4)
  12609. intel_create_rotation_property(dev, primary);
  12610. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  12611. return &primary->base;
  12612. fail:
  12613. kfree(state);
  12614. kfree(primary);
  12615. return NULL;
  12616. }
  12617. void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
  12618. {
  12619. if (!dev->mode_config.rotation_property) {
  12620. unsigned long flags = DRM_ROTATE_0 |
  12621. DRM_ROTATE_180;
  12622. if (INTEL_INFO(dev)->gen >= 9)
  12623. flags |= DRM_ROTATE_90 | DRM_ROTATE_270;
  12624. dev->mode_config.rotation_property =
  12625. drm_mode_create_rotation_property(dev, flags);
  12626. }
  12627. if (dev->mode_config.rotation_property)
  12628. drm_object_attach_property(&plane->base.base,
  12629. dev->mode_config.rotation_property,
  12630. plane->base.state->rotation);
  12631. }
  12632. static int
  12633. intel_check_cursor_plane(struct drm_plane *plane,
  12634. struct intel_crtc_state *crtc_state,
  12635. struct intel_plane_state *state)
  12636. {
  12637. struct drm_framebuffer *fb = state->base.fb;
  12638. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  12639. enum pipe pipe = to_intel_plane(plane)->pipe;
  12640. unsigned stride;
  12641. int ret;
  12642. ret = drm_plane_helper_check_state(&state->base,
  12643. &state->clip,
  12644. DRM_PLANE_HELPER_NO_SCALING,
  12645. DRM_PLANE_HELPER_NO_SCALING,
  12646. true, true);
  12647. if (ret)
  12648. return ret;
  12649. /* if we want to turn off the cursor ignore width and height */
  12650. if (!obj)
  12651. return 0;
  12652. /* Check for which cursor types we support */
  12653. if (!cursor_size_ok(to_i915(plane->dev), state->base.crtc_w,
  12654. state->base.crtc_h)) {
  12655. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  12656. state->base.crtc_w, state->base.crtc_h);
  12657. return -EINVAL;
  12658. }
  12659. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  12660. if (obj->base.size < stride * state->base.crtc_h) {
  12661. DRM_DEBUG_KMS("buffer is too small\n");
  12662. return -ENOMEM;
  12663. }
  12664. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  12665. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  12666. return -EINVAL;
  12667. }
  12668. /*
  12669. * There's something wrong with the cursor on CHV pipe C.
  12670. * If it straddles the left edge of the screen then
  12671. * moving it away from the edge or disabling it often
  12672. * results in a pipe underrun, and often that can lead to
  12673. * dead pipe (constant underrun reported, and it scans
  12674. * out just a solid color). To recover from that, the
  12675. * display power well must be turned off and on again.
  12676. * Refuse the put the cursor into that compromised position.
  12677. */
  12678. if (IS_CHERRYVIEW(to_i915(plane->dev)) && pipe == PIPE_C &&
  12679. state->base.visible && state->base.crtc_x < 0) {
  12680. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  12681. return -EINVAL;
  12682. }
  12683. return 0;
  12684. }
  12685. static void
  12686. intel_disable_cursor_plane(struct drm_plane *plane,
  12687. struct drm_crtc *crtc)
  12688. {
  12689. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12690. intel_crtc->cursor_addr = 0;
  12691. intel_crtc_update_cursor(crtc, NULL);
  12692. }
  12693. static void
  12694. intel_update_cursor_plane(struct drm_plane *plane,
  12695. const struct intel_crtc_state *crtc_state,
  12696. const struct intel_plane_state *state)
  12697. {
  12698. struct drm_crtc *crtc = crtc_state->base.crtc;
  12699. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12700. struct drm_device *dev = plane->dev;
  12701. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  12702. uint32_t addr;
  12703. if (!obj)
  12704. addr = 0;
  12705. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  12706. addr = i915_gem_object_ggtt_offset(obj, NULL);
  12707. else
  12708. addr = obj->phys_handle->busaddr;
  12709. intel_crtc->cursor_addr = addr;
  12710. intel_crtc_update_cursor(crtc, state);
  12711. }
  12712. static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
  12713. int pipe)
  12714. {
  12715. struct intel_plane *cursor = NULL;
  12716. struct intel_plane_state *state = NULL;
  12717. int ret;
  12718. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  12719. if (!cursor)
  12720. goto fail;
  12721. state = intel_create_plane_state(&cursor->base);
  12722. if (!state)
  12723. goto fail;
  12724. cursor->base.state = &state->base;
  12725. cursor->can_scale = false;
  12726. cursor->max_downscale = 1;
  12727. cursor->pipe = pipe;
  12728. cursor->plane = pipe;
  12729. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  12730. cursor->check_plane = intel_check_cursor_plane;
  12731. cursor->update_plane = intel_update_cursor_plane;
  12732. cursor->disable_plane = intel_disable_cursor_plane;
  12733. ret = drm_universal_plane_init(dev, &cursor->base, 0,
  12734. &intel_plane_funcs,
  12735. intel_cursor_formats,
  12736. ARRAY_SIZE(intel_cursor_formats),
  12737. DRM_PLANE_TYPE_CURSOR,
  12738. "cursor %c", pipe_name(pipe));
  12739. if (ret)
  12740. goto fail;
  12741. if (INTEL_INFO(dev)->gen >= 4) {
  12742. if (!dev->mode_config.rotation_property)
  12743. dev->mode_config.rotation_property =
  12744. drm_mode_create_rotation_property(dev,
  12745. DRM_ROTATE_0 |
  12746. DRM_ROTATE_180);
  12747. if (dev->mode_config.rotation_property)
  12748. drm_object_attach_property(&cursor->base.base,
  12749. dev->mode_config.rotation_property,
  12750. state->base.rotation);
  12751. }
  12752. if (INTEL_INFO(dev)->gen >=9)
  12753. state->scaler_id = -1;
  12754. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  12755. return &cursor->base;
  12756. fail:
  12757. kfree(state);
  12758. kfree(cursor);
  12759. return NULL;
  12760. }
  12761. static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
  12762. struct intel_crtc_state *crtc_state)
  12763. {
  12764. int i;
  12765. struct intel_scaler *intel_scaler;
  12766. struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
  12767. for (i = 0; i < intel_crtc->num_scalers; i++) {
  12768. intel_scaler = &scaler_state->scalers[i];
  12769. intel_scaler->in_use = 0;
  12770. intel_scaler->mode = PS_SCALER_MODE_DYN;
  12771. }
  12772. scaler_state->scaler_id = -1;
  12773. }
  12774. static void intel_crtc_init(struct drm_device *dev, int pipe)
  12775. {
  12776. struct drm_i915_private *dev_priv = to_i915(dev);
  12777. struct intel_crtc *intel_crtc;
  12778. struct intel_crtc_state *crtc_state = NULL;
  12779. struct drm_plane *primary = NULL;
  12780. struct drm_plane *cursor = NULL;
  12781. int ret;
  12782. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  12783. if (intel_crtc == NULL)
  12784. return;
  12785. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  12786. if (!crtc_state)
  12787. goto fail;
  12788. intel_crtc->config = crtc_state;
  12789. intel_crtc->base.state = &crtc_state->base;
  12790. crtc_state->base.crtc = &intel_crtc->base;
  12791. /* initialize shared scalers */
  12792. if (INTEL_INFO(dev)->gen >= 9) {
  12793. if (pipe == PIPE_C)
  12794. intel_crtc->num_scalers = 1;
  12795. else
  12796. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  12797. skl_init_scalers(dev, intel_crtc, crtc_state);
  12798. }
  12799. primary = intel_primary_plane_create(dev, pipe);
  12800. if (!primary)
  12801. goto fail;
  12802. cursor = intel_cursor_plane_create(dev, pipe);
  12803. if (!cursor)
  12804. goto fail;
  12805. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
  12806. cursor, &intel_crtc_funcs,
  12807. "pipe %c", pipe_name(pipe));
  12808. if (ret)
  12809. goto fail;
  12810. /*
  12811. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  12812. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  12813. */
  12814. intel_crtc->pipe = pipe;
  12815. intel_crtc->plane = pipe;
  12816. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  12817. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  12818. intel_crtc->plane = !pipe;
  12819. }
  12820. intel_crtc->cursor_base = ~0;
  12821. intel_crtc->cursor_cntl = ~0;
  12822. intel_crtc->cursor_size = ~0;
  12823. intel_crtc->wm.cxsr_allowed = true;
  12824. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  12825. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  12826. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
  12827. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
  12828. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  12829. intel_color_init(&intel_crtc->base);
  12830. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  12831. return;
  12832. fail:
  12833. intel_plane_destroy(primary);
  12834. intel_plane_destroy(cursor);
  12835. kfree(crtc_state);
  12836. kfree(intel_crtc);
  12837. }
  12838. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  12839. {
  12840. struct drm_encoder *encoder = connector->base.encoder;
  12841. struct drm_device *dev = connector->base.dev;
  12842. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  12843. if (!encoder || WARN_ON(!encoder->crtc))
  12844. return INVALID_PIPE;
  12845. return to_intel_crtc(encoder->crtc)->pipe;
  12846. }
  12847. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  12848. struct drm_file *file)
  12849. {
  12850. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  12851. struct drm_crtc *drmmode_crtc;
  12852. struct intel_crtc *crtc;
  12853. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  12854. if (!drmmode_crtc)
  12855. return -ENOENT;
  12856. crtc = to_intel_crtc(drmmode_crtc);
  12857. pipe_from_crtc_id->pipe = crtc->pipe;
  12858. return 0;
  12859. }
  12860. static int intel_encoder_clones(struct intel_encoder *encoder)
  12861. {
  12862. struct drm_device *dev = encoder->base.dev;
  12863. struct intel_encoder *source_encoder;
  12864. int index_mask = 0;
  12865. int entry = 0;
  12866. for_each_intel_encoder(dev, source_encoder) {
  12867. if (encoders_cloneable(encoder, source_encoder))
  12868. index_mask |= (1 << entry);
  12869. entry++;
  12870. }
  12871. return index_mask;
  12872. }
  12873. static bool has_edp_a(struct drm_device *dev)
  12874. {
  12875. struct drm_i915_private *dev_priv = to_i915(dev);
  12876. if (!IS_MOBILE(dev))
  12877. return false;
  12878. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  12879. return false;
  12880. if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  12881. return false;
  12882. return true;
  12883. }
  12884. static bool intel_crt_present(struct drm_device *dev)
  12885. {
  12886. struct drm_i915_private *dev_priv = to_i915(dev);
  12887. if (INTEL_INFO(dev)->gen >= 9)
  12888. return false;
  12889. if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
  12890. return false;
  12891. if (IS_CHERRYVIEW(dev_priv))
  12892. return false;
  12893. if (HAS_PCH_LPT_H(dev_priv) &&
  12894. I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  12895. return false;
  12896. /* DDI E can't be used if DDI A requires 4 lanes */
  12897. if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  12898. return false;
  12899. if (!dev_priv->vbt.int_crt_support)
  12900. return false;
  12901. return true;
  12902. }
  12903. void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
  12904. {
  12905. int pps_num;
  12906. int pps_idx;
  12907. if (HAS_DDI(dev_priv))
  12908. return;
  12909. /*
  12910. * This w/a is needed at least on CPT/PPT, but to be sure apply it
  12911. * everywhere where registers can be write protected.
  12912. */
  12913. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12914. pps_num = 2;
  12915. else
  12916. pps_num = 1;
  12917. for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
  12918. u32 val = I915_READ(PP_CONTROL(pps_idx));
  12919. val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
  12920. I915_WRITE(PP_CONTROL(pps_idx), val);
  12921. }
  12922. }
  12923. static void intel_pps_init(struct drm_i915_private *dev_priv)
  12924. {
  12925. if (HAS_PCH_SPLIT(dev_priv) || IS_BROXTON(dev_priv))
  12926. dev_priv->pps_mmio_base = PCH_PPS_BASE;
  12927. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12928. dev_priv->pps_mmio_base = VLV_PPS_BASE;
  12929. else
  12930. dev_priv->pps_mmio_base = PPS_BASE;
  12931. intel_pps_unlock_regs_wa(dev_priv);
  12932. }
  12933. static void intel_setup_outputs(struct drm_device *dev)
  12934. {
  12935. struct drm_i915_private *dev_priv = to_i915(dev);
  12936. struct intel_encoder *encoder;
  12937. bool dpd_is_edp = false;
  12938. intel_pps_init(dev_priv);
  12939. /*
  12940. * intel_edp_init_connector() depends on this completing first, to
  12941. * prevent the registeration of both eDP and LVDS and the incorrect
  12942. * sharing of the PPS.
  12943. */
  12944. intel_lvds_init(dev);
  12945. if (intel_crt_present(dev))
  12946. intel_crt_init(dev);
  12947. if (IS_BROXTON(dev_priv)) {
  12948. /*
  12949. * FIXME: Broxton doesn't support port detection via the
  12950. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  12951. * detect the ports.
  12952. */
  12953. intel_ddi_init(dev, PORT_A);
  12954. intel_ddi_init(dev, PORT_B);
  12955. intel_ddi_init(dev, PORT_C);
  12956. intel_dsi_init(dev);
  12957. } else if (HAS_DDI(dev_priv)) {
  12958. int found;
  12959. /*
  12960. * Haswell uses DDI functions to detect digital outputs.
  12961. * On SKL pre-D0 the strap isn't connected, so we assume
  12962. * it's there.
  12963. */
  12964. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  12965. /* WaIgnoreDDIAStrap: skl */
  12966. if (found || IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  12967. intel_ddi_init(dev, PORT_A);
  12968. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  12969. * register */
  12970. found = I915_READ(SFUSE_STRAP);
  12971. if (found & SFUSE_STRAP_DDIB_DETECTED)
  12972. intel_ddi_init(dev, PORT_B);
  12973. if (found & SFUSE_STRAP_DDIC_DETECTED)
  12974. intel_ddi_init(dev, PORT_C);
  12975. if (found & SFUSE_STRAP_DDID_DETECTED)
  12976. intel_ddi_init(dev, PORT_D);
  12977. /*
  12978. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  12979. */
  12980. if ((IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) &&
  12981. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  12982. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  12983. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  12984. intel_ddi_init(dev, PORT_E);
  12985. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12986. int found;
  12987. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  12988. if (has_edp_a(dev))
  12989. intel_dp_init(dev, DP_A, PORT_A);
  12990. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  12991. /* PCH SDVOB multiplex with HDMIB */
  12992. found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
  12993. if (!found)
  12994. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  12995. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  12996. intel_dp_init(dev, PCH_DP_B, PORT_B);
  12997. }
  12998. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  12999. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  13000. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  13001. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  13002. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  13003. intel_dp_init(dev, PCH_DP_C, PORT_C);
  13004. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  13005. intel_dp_init(dev, PCH_DP_D, PORT_D);
  13006. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  13007. bool has_edp, has_port;
  13008. /*
  13009. * The DP_DETECTED bit is the latched state of the DDC
  13010. * SDA pin at boot. However since eDP doesn't require DDC
  13011. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  13012. * eDP ports may have been muxed to an alternate function.
  13013. * Thus we can't rely on the DP_DETECTED bit alone to detect
  13014. * eDP ports. Consult the VBT as well as DP_DETECTED to
  13015. * detect eDP ports.
  13016. *
  13017. * Sadly the straps seem to be missing sometimes even for HDMI
  13018. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  13019. * and VBT for the presence of the port. Additionally we can't
  13020. * trust the port type the VBT declares as we've seen at least
  13021. * HDMI ports that the VBT claim are DP or eDP.
  13022. */
  13023. has_edp = intel_dp_is_edp(dev, PORT_B);
  13024. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  13025. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  13026. has_edp &= intel_dp_init(dev, VLV_DP_B, PORT_B);
  13027. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  13028. intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
  13029. has_edp = intel_dp_is_edp(dev, PORT_C);
  13030. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  13031. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  13032. has_edp &= intel_dp_init(dev, VLV_DP_C, PORT_C);
  13033. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  13034. intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
  13035. if (IS_CHERRYVIEW(dev_priv)) {
  13036. /*
  13037. * eDP not supported on port D,
  13038. * so no need to worry about it
  13039. */
  13040. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  13041. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  13042. intel_dp_init(dev, CHV_DP_D, PORT_D);
  13043. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  13044. intel_hdmi_init(dev, CHV_HDMID, PORT_D);
  13045. }
  13046. intel_dsi_init(dev);
  13047. } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
  13048. bool found = false;
  13049. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  13050. DRM_DEBUG_KMS("probing SDVOB\n");
  13051. found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
  13052. if (!found && IS_G4X(dev_priv)) {
  13053. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  13054. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  13055. }
  13056. if (!found && IS_G4X(dev_priv))
  13057. intel_dp_init(dev, DP_B, PORT_B);
  13058. }
  13059. /* Before G4X SDVOC doesn't have its own detect register */
  13060. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  13061. DRM_DEBUG_KMS("probing SDVOC\n");
  13062. found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
  13063. }
  13064. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  13065. if (IS_G4X(dev_priv)) {
  13066. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  13067. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  13068. }
  13069. if (IS_G4X(dev_priv))
  13070. intel_dp_init(dev, DP_C, PORT_C);
  13071. }
  13072. if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
  13073. intel_dp_init(dev, DP_D, PORT_D);
  13074. } else if (IS_GEN2(dev_priv))
  13075. intel_dvo_init(dev);
  13076. if (SUPPORTS_TV(dev))
  13077. intel_tv_init(dev);
  13078. intel_psr_init(dev);
  13079. for_each_intel_encoder(dev, encoder) {
  13080. encoder->base.possible_crtcs = encoder->crtc_mask;
  13081. encoder->base.possible_clones =
  13082. intel_encoder_clones(encoder);
  13083. }
  13084. intel_init_pch_refclk(dev);
  13085. drm_helper_move_panel_connectors_to_head(dev);
  13086. }
  13087. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  13088. {
  13089. struct drm_device *dev = fb->dev;
  13090. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  13091. drm_framebuffer_cleanup(fb);
  13092. mutex_lock(&dev->struct_mutex);
  13093. WARN_ON(!intel_fb->obj->framebuffer_references--);
  13094. i915_gem_object_put(intel_fb->obj);
  13095. mutex_unlock(&dev->struct_mutex);
  13096. kfree(intel_fb);
  13097. }
  13098. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  13099. struct drm_file *file,
  13100. unsigned int *handle)
  13101. {
  13102. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  13103. struct drm_i915_gem_object *obj = intel_fb->obj;
  13104. if (obj->userptr.mm) {
  13105. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  13106. return -EINVAL;
  13107. }
  13108. return drm_gem_handle_create(file, &obj->base, handle);
  13109. }
  13110. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  13111. struct drm_file *file,
  13112. unsigned flags, unsigned color,
  13113. struct drm_clip_rect *clips,
  13114. unsigned num_clips)
  13115. {
  13116. struct drm_device *dev = fb->dev;
  13117. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  13118. struct drm_i915_gem_object *obj = intel_fb->obj;
  13119. mutex_lock(&dev->struct_mutex);
  13120. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  13121. mutex_unlock(&dev->struct_mutex);
  13122. return 0;
  13123. }
  13124. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  13125. .destroy = intel_user_framebuffer_destroy,
  13126. .create_handle = intel_user_framebuffer_create_handle,
  13127. .dirty = intel_user_framebuffer_dirty,
  13128. };
  13129. static
  13130. u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
  13131. uint64_t fb_modifier, uint32_t pixel_format)
  13132. {
  13133. u32 gen = INTEL_INFO(dev_priv)->gen;
  13134. if (gen >= 9) {
  13135. int cpp = drm_format_plane_cpp(pixel_format, 0);
  13136. /* "The stride in bytes must not exceed the of the size of 8K
  13137. * pixels and 32K bytes."
  13138. */
  13139. return min(8192 * cpp, 32768);
  13140. } else if (gen >= 5 && !IS_VALLEYVIEW(dev_priv) &&
  13141. !IS_CHERRYVIEW(dev_priv)) {
  13142. return 32*1024;
  13143. } else if (gen >= 4) {
  13144. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  13145. return 16*1024;
  13146. else
  13147. return 32*1024;
  13148. } else if (gen >= 3) {
  13149. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  13150. return 8*1024;
  13151. else
  13152. return 16*1024;
  13153. } else {
  13154. /* XXX DSPC is limited to 4k tiled */
  13155. return 8*1024;
  13156. }
  13157. }
  13158. static int intel_framebuffer_init(struct drm_device *dev,
  13159. struct intel_framebuffer *intel_fb,
  13160. struct drm_mode_fb_cmd2 *mode_cmd,
  13161. struct drm_i915_gem_object *obj)
  13162. {
  13163. struct drm_i915_private *dev_priv = to_i915(dev);
  13164. unsigned int tiling = i915_gem_object_get_tiling(obj);
  13165. int ret;
  13166. u32 pitch_limit, stride_alignment;
  13167. char *format_name;
  13168. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  13169. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  13170. /*
  13171. * If there's a fence, enforce that
  13172. * the fb modifier and tiling mode match.
  13173. */
  13174. if (tiling != I915_TILING_NONE &&
  13175. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  13176. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  13177. return -EINVAL;
  13178. }
  13179. } else {
  13180. if (tiling == I915_TILING_X) {
  13181. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  13182. } else if (tiling == I915_TILING_Y) {
  13183. DRM_DEBUG("No Y tiling for legacy addfb\n");
  13184. return -EINVAL;
  13185. }
  13186. }
  13187. /* Passed in modifier sanity checking. */
  13188. switch (mode_cmd->modifier[0]) {
  13189. case I915_FORMAT_MOD_Y_TILED:
  13190. case I915_FORMAT_MOD_Yf_TILED:
  13191. if (INTEL_INFO(dev)->gen < 9) {
  13192. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  13193. mode_cmd->modifier[0]);
  13194. return -EINVAL;
  13195. }
  13196. case DRM_FORMAT_MOD_NONE:
  13197. case I915_FORMAT_MOD_X_TILED:
  13198. break;
  13199. default:
  13200. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  13201. mode_cmd->modifier[0]);
  13202. return -EINVAL;
  13203. }
  13204. /*
  13205. * gen2/3 display engine uses the fence if present,
  13206. * so the tiling mode must match the fb modifier exactly.
  13207. */
  13208. if (INTEL_INFO(dev_priv)->gen < 4 &&
  13209. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  13210. DRM_DEBUG("tiling_mode must match fb modifier exactly on gen2/3\n");
  13211. return -EINVAL;
  13212. }
  13213. stride_alignment = intel_fb_stride_alignment(dev_priv,
  13214. mode_cmd->modifier[0],
  13215. mode_cmd->pixel_format);
  13216. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  13217. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  13218. mode_cmd->pitches[0], stride_alignment);
  13219. return -EINVAL;
  13220. }
  13221. pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
  13222. mode_cmd->pixel_format);
  13223. if (mode_cmd->pitches[0] > pitch_limit) {
  13224. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  13225. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  13226. "tiled" : "linear",
  13227. mode_cmd->pitches[0], pitch_limit);
  13228. return -EINVAL;
  13229. }
  13230. /*
  13231. * If there's a fence, enforce that
  13232. * the fb pitch and fence stride match.
  13233. */
  13234. if (tiling != I915_TILING_NONE &&
  13235. mode_cmd->pitches[0] != i915_gem_object_get_stride(obj)) {
  13236. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  13237. mode_cmd->pitches[0],
  13238. i915_gem_object_get_stride(obj));
  13239. return -EINVAL;
  13240. }
  13241. /* Reject formats not supported by any plane early. */
  13242. switch (mode_cmd->pixel_format) {
  13243. case DRM_FORMAT_C8:
  13244. case DRM_FORMAT_RGB565:
  13245. case DRM_FORMAT_XRGB8888:
  13246. case DRM_FORMAT_ARGB8888:
  13247. break;
  13248. case DRM_FORMAT_XRGB1555:
  13249. if (INTEL_INFO(dev)->gen > 3) {
  13250. format_name = drm_get_format_name(mode_cmd->pixel_format);
  13251. DRM_DEBUG("unsupported pixel format: %s\n", format_name);
  13252. kfree(format_name);
  13253. return -EINVAL;
  13254. }
  13255. break;
  13256. case DRM_FORMAT_ABGR8888:
  13257. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  13258. INTEL_INFO(dev)->gen < 9) {
  13259. format_name = drm_get_format_name(mode_cmd->pixel_format);
  13260. DRM_DEBUG("unsupported pixel format: %s\n", format_name);
  13261. kfree(format_name);
  13262. return -EINVAL;
  13263. }
  13264. break;
  13265. case DRM_FORMAT_XBGR8888:
  13266. case DRM_FORMAT_XRGB2101010:
  13267. case DRM_FORMAT_XBGR2101010:
  13268. if (INTEL_INFO(dev)->gen < 4) {
  13269. format_name = drm_get_format_name(mode_cmd->pixel_format);
  13270. DRM_DEBUG("unsupported pixel format: %s\n", format_name);
  13271. kfree(format_name);
  13272. return -EINVAL;
  13273. }
  13274. break;
  13275. case DRM_FORMAT_ABGR2101010:
  13276. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  13277. format_name = drm_get_format_name(mode_cmd->pixel_format);
  13278. DRM_DEBUG("unsupported pixel format: %s\n", format_name);
  13279. kfree(format_name);
  13280. return -EINVAL;
  13281. }
  13282. break;
  13283. case DRM_FORMAT_YUYV:
  13284. case DRM_FORMAT_UYVY:
  13285. case DRM_FORMAT_YVYU:
  13286. case DRM_FORMAT_VYUY:
  13287. if (INTEL_INFO(dev)->gen < 5) {
  13288. format_name = drm_get_format_name(mode_cmd->pixel_format);
  13289. DRM_DEBUG("unsupported pixel format: %s\n", format_name);
  13290. kfree(format_name);
  13291. return -EINVAL;
  13292. }
  13293. break;
  13294. default:
  13295. format_name = drm_get_format_name(mode_cmd->pixel_format);
  13296. DRM_DEBUG("unsupported pixel format: %s\n", format_name);
  13297. kfree(format_name);
  13298. return -EINVAL;
  13299. }
  13300. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  13301. if (mode_cmd->offsets[0] != 0)
  13302. return -EINVAL;
  13303. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  13304. intel_fb->obj = obj;
  13305. ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
  13306. if (ret)
  13307. return ret;
  13308. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  13309. if (ret) {
  13310. DRM_ERROR("framebuffer init failed %d\n", ret);
  13311. return ret;
  13312. }
  13313. intel_fb->obj->framebuffer_references++;
  13314. return 0;
  13315. }
  13316. static struct drm_framebuffer *
  13317. intel_user_framebuffer_create(struct drm_device *dev,
  13318. struct drm_file *filp,
  13319. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  13320. {
  13321. struct drm_framebuffer *fb;
  13322. struct drm_i915_gem_object *obj;
  13323. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  13324. obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
  13325. if (!obj)
  13326. return ERR_PTR(-ENOENT);
  13327. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  13328. if (IS_ERR(fb))
  13329. i915_gem_object_put_unlocked(obj);
  13330. return fb;
  13331. }
  13332. static const struct drm_mode_config_funcs intel_mode_funcs = {
  13333. .fb_create = intel_user_framebuffer_create,
  13334. .output_poll_changed = intel_fbdev_output_poll_changed,
  13335. .atomic_check = intel_atomic_check,
  13336. .atomic_commit = intel_atomic_commit,
  13337. .atomic_state_alloc = intel_atomic_state_alloc,
  13338. .atomic_state_clear = intel_atomic_state_clear,
  13339. };
  13340. /**
  13341. * intel_init_display_hooks - initialize the display modesetting hooks
  13342. * @dev_priv: device private
  13343. */
  13344. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  13345. {
  13346. if (INTEL_INFO(dev_priv)->gen >= 9) {
  13347. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  13348. dev_priv->display.get_initial_plane_config =
  13349. skylake_get_initial_plane_config;
  13350. dev_priv->display.crtc_compute_clock =
  13351. haswell_crtc_compute_clock;
  13352. dev_priv->display.crtc_enable = haswell_crtc_enable;
  13353. dev_priv->display.crtc_disable = haswell_crtc_disable;
  13354. } else if (HAS_DDI(dev_priv)) {
  13355. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  13356. dev_priv->display.get_initial_plane_config =
  13357. ironlake_get_initial_plane_config;
  13358. dev_priv->display.crtc_compute_clock =
  13359. haswell_crtc_compute_clock;
  13360. dev_priv->display.crtc_enable = haswell_crtc_enable;
  13361. dev_priv->display.crtc_disable = haswell_crtc_disable;
  13362. } else if (HAS_PCH_SPLIT(dev_priv)) {
  13363. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  13364. dev_priv->display.get_initial_plane_config =
  13365. ironlake_get_initial_plane_config;
  13366. dev_priv->display.crtc_compute_clock =
  13367. ironlake_crtc_compute_clock;
  13368. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  13369. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  13370. } else if (IS_CHERRYVIEW(dev_priv)) {
  13371. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13372. dev_priv->display.get_initial_plane_config =
  13373. i9xx_get_initial_plane_config;
  13374. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  13375. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  13376. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13377. } else if (IS_VALLEYVIEW(dev_priv)) {
  13378. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13379. dev_priv->display.get_initial_plane_config =
  13380. i9xx_get_initial_plane_config;
  13381. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  13382. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  13383. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13384. } else if (IS_G4X(dev_priv)) {
  13385. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13386. dev_priv->display.get_initial_plane_config =
  13387. i9xx_get_initial_plane_config;
  13388. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  13389. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13390. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13391. } else if (IS_PINEVIEW(dev_priv)) {
  13392. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13393. dev_priv->display.get_initial_plane_config =
  13394. i9xx_get_initial_plane_config;
  13395. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  13396. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13397. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13398. } else if (!IS_GEN2(dev_priv)) {
  13399. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13400. dev_priv->display.get_initial_plane_config =
  13401. i9xx_get_initial_plane_config;
  13402. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  13403. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13404. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13405. } else {
  13406. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13407. dev_priv->display.get_initial_plane_config =
  13408. i9xx_get_initial_plane_config;
  13409. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  13410. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13411. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13412. }
  13413. /* Returns the core display clock speed */
  13414. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  13415. dev_priv->display.get_display_clock_speed =
  13416. skylake_get_display_clock_speed;
  13417. else if (IS_BROXTON(dev_priv))
  13418. dev_priv->display.get_display_clock_speed =
  13419. broxton_get_display_clock_speed;
  13420. else if (IS_BROADWELL(dev_priv))
  13421. dev_priv->display.get_display_clock_speed =
  13422. broadwell_get_display_clock_speed;
  13423. else if (IS_HASWELL(dev_priv))
  13424. dev_priv->display.get_display_clock_speed =
  13425. haswell_get_display_clock_speed;
  13426. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  13427. dev_priv->display.get_display_clock_speed =
  13428. valleyview_get_display_clock_speed;
  13429. else if (IS_GEN5(dev_priv))
  13430. dev_priv->display.get_display_clock_speed =
  13431. ilk_get_display_clock_speed;
  13432. else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
  13433. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  13434. dev_priv->display.get_display_clock_speed =
  13435. i945_get_display_clock_speed;
  13436. else if (IS_GM45(dev_priv))
  13437. dev_priv->display.get_display_clock_speed =
  13438. gm45_get_display_clock_speed;
  13439. else if (IS_CRESTLINE(dev_priv))
  13440. dev_priv->display.get_display_clock_speed =
  13441. i965gm_get_display_clock_speed;
  13442. else if (IS_PINEVIEW(dev_priv))
  13443. dev_priv->display.get_display_clock_speed =
  13444. pnv_get_display_clock_speed;
  13445. else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
  13446. dev_priv->display.get_display_clock_speed =
  13447. g33_get_display_clock_speed;
  13448. else if (IS_I915G(dev_priv))
  13449. dev_priv->display.get_display_clock_speed =
  13450. i915_get_display_clock_speed;
  13451. else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
  13452. dev_priv->display.get_display_clock_speed =
  13453. i9xx_misc_get_display_clock_speed;
  13454. else if (IS_I915GM(dev_priv))
  13455. dev_priv->display.get_display_clock_speed =
  13456. i915gm_get_display_clock_speed;
  13457. else if (IS_I865G(dev_priv))
  13458. dev_priv->display.get_display_clock_speed =
  13459. i865_get_display_clock_speed;
  13460. else if (IS_I85X(dev_priv))
  13461. dev_priv->display.get_display_clock_speed =
  13462. i85x_get_display_clock_speed;
  13463. else { /* 830 */
  13464. WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
  13465. dev_priv->display.get_display_clock_speed =
  13466. i830_get_display_clock_speed;
  13467. }
  13468. if (IS_GEN5(dev_priv)) {
  13469. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  13470. } else if (IS_GEN6(dev_priv)) {
  13471. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  13472. } else if (IS_IVYBRIDGE(dev_priv)) {
  13473. /* FIXME: detect B0+ stepping and use auto training */
  13474. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  13475. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  13476. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  13477. }
  13478. if (IS_BROADWELL(dev_priv)) {
  13479. dev_priv->display.modeset_commit_cdclk =
  13480. broadwell_modeset_commit_cdclk;
  13481. dev_priv->display.modeset_calc_cdclk =
  13482. broadwell_modeset_calc_cdclk;
  13483. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  13484. dev_priv->display.modeset_commit_cdclk =
  13485. valleyview_modeset_commit_cdclk;
  13486. dev_priv->display.modeset_calc_cdclk =
  13487. valleyview_modeset_calc_cdclk;
  13488. } else if (IS_BROXTON(dev_priv)) {
  13489. dev_priv->display.modeset_commit_cdclk =
  13490. bxt_modeset_commit_cdclk;
  13491. dev_priv->display.modeset_calc_cdclk =
  13492. bxt_modeset_calc_cdclk;
  13493. } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
  13494. dev_priv->display.modeset_commit_cdclk =
  13495. skl_modeset_commit_cdclk;
  13496. dev_priv->display.modeset_calc_cdclk =
  13497. skl_modeset_calc_cdclk;
  13498. }
  13499. if (dev_priv->info.gen >= 9)
  13500. dev_priv->display.update_crtcs = skl_update_crtcs;
  13501. else
  13502. dev_priv->display.update_crtcs = intel_update_crtcs;
  13503. switch (INTEL_INFO(dev_priv)->gen) {
  13504. case 2:
  13505. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  13506. break;
  13507. case 3:
  13508. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  13509. break;
  13510. case 4:
  13511. case 5:
  13512. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  13513. break;
  13514. case 6:
  13515. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  13516. break;
  13517. case 7:
  13518. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  13519. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  13520. break;
  13521. case 9:
  13522. /* Drop through - unsupported since execlist only. */
  13523. default:
  13524. /* Default just returns -ENODEV to indicate unsupported */
  13525. dev_priv->display.queue_flip = intel_default_queue_flip;
  13526. }
  13527. }
  13528. /*
  13529. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  13530. * resume, or other times. This quirk makes sure that's the case for
  13531. * affected systems.
  13532. */
  13533. static void quirk_pipea_force(struct drm_device *dev)
  13534. {
  13535. struct drm_i915_private *dev_priv = to_i915(dev);
  13536. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  13537. DRM_INFO("applying pipe a force quirk\n");
  13538. }
  13539. static void quirk_pipeb_force(struct drm_device *dev)
  13540. {
  13541. struct drm_i915_private *dev_priv = to_i915(dev);
  13542. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  13543. DRM_INFO("applying pipe b force quirk\n");
  13544. }
  13545. /*
  13546. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  13547. */
  13548. static void quirk_ssc_force_disable(struct drm_device *dev)
  13549. {
  13550. struct drm_i915_private *dev_priv = to_i915(dev);
  13551. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  13552. DRM_INFO("applying lvds SSC disable quirk\n");
  13553. }
  13554. /*
  13555. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  13556. * brightness value
  13557. */
  13558. static void quirk_invert_brightness(struct drm_device *dev)
  13559. {
  13560. struct drm_i915_private *dev_priv = to_i915(dev);
  13561. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  13562. DRM_INFO("applying inverted panel brightness quirk\n");
  13563. }
  13564. /* Some VBT's incorrectly indicate no backlight is present */
  13565. static void quirk_backlight_present(struct drm_device *dev)
  13566. {
  13567. struct drm_i915_private *dev_priv = to_i915(dev);
  13568. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  13569. DRM_INFO("applying backlight present quirk\n");
  13570. }
  13571. struct intel_quirk {
  13572. int device;
  13573. int subsystem_vendor;
  13574. int subsystem_device;
  13575. void (*hook)(struct drm_device *dev);
  13576. };
  13577. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  13578. struct intel_dmi_quirk {
  13579. void (*hook)(struct drm_device *dev);
  13580. const struct dmi_system_id (*dmi_id_list)[];
  13581. };
  13582. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  13583. {
  13584. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  13585. return 1;
  13586. }
  13587. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  13588. {
  13589. .dmi_id_list = &(const struct dmi_system_id[]) {
  13590. {
  13591. .callback = intel_dmi_reverse_brightness,
  13592. .ident = "NCR Corporation",
  13593. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  13594. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  13595. },
  13596. },
  13597. { } /* terminating entry */
  13598. },
  13599. .hook = quirk_invert_brightness,
  13600. },
  13601. };
  13602. static struct intel_quirk intel_quirks[] = {
  13603. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  13604. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  13605. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  13606. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  13607. /* 830 needs to leave pipe A & dpll A up */
  13608. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  13609. /* 830 needs to leave pipe B & dpll B up */
  13610. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  13611. /* Lenovo U160 cannot use SSC on LVDS */
  13612. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  13613. /* Sony Vaio Y cannot use SSC on LVDS */
  13614. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  13615. /* Acer Aspire 5734Z must invert backlight brightness */
  13616. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  13617. /* Acer/eMachines G725 */
  13618. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  13619. /* Acer/eMachines e725 */
  13620. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  13621. /* Acer/Packard Bell NCL20 */
  13622. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  13623. /* Acer Aspire 4736Z */
  13624. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  13625. /* Acer Aspire 5336 */
  13626. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  13627. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  13628. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  13629. /* Acer C720 Chromebook (Core i3 4005U) */
  13630. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  13631. /* Apple Macbook 2,1 (Core 2 T7400) */
  13632. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  13633. /* Apple Macbook 4,1 */
  13634. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  13635. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  13636. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  13637. /* HP Chromebook 14 (Celeron 2955U) */
  13638. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  13639. /* Dell Chromebook 11 */
  13640. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  13641. /* Dell Chromebook 11 (2015 version) */
  13642. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  13643. };
  13644. static void intel_init_quirks(struct drm_device *dev)
  13645. {
  13646. struct pci_dev *d = dev->pdev;
  13647. int i;
  13648. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  13649. struct intel_quirk *q = &intel_quirks[i];
  13650. if (d->device == q->device &&
  13651. (d->subsystem_vendor == q->subsystem_vendor ||
  13652. q->subsystem_vendor == PCI_ANY_ID) &&
  13653. (d->subsystem_device == q->subsystem_device ||
  13654. q->subsystem_device == PCI_ANY_ID))
  13655. q->hook(dev);
  13656. }
  13657. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  13658. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  13659. intel_dmi_quirks[i].hook(dev);
  13660. }
  13661. }
  13662. /* Disable the VGA plane that we never use */
  13663. static void i915_disable_vga(struct drm_device *dev)
  13664. {
  13665. struct drm_i915_private *dev_priv = to_i915(dev);
  13666. struct pci_dev *pdev = dev_priv->drm.pdev;
  13667. u8 sr1;
  13668. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  13669. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  13670. vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
  13671. outb(SR01, VGA_SR_INDEX);
  13672. sr1 = inb(VGA_SR_DATA);
  13673. outb(sr1 | 1<<5, VGA_SR_DATA);
  13674. vga_put(pdev, VGA_RSRC_LEGACY_IO);
  13675. udelay(300);
  13676. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  13677. POSTING_READ(vga_reg);
  13678. }
  13679. void intel_modeset_init_hw(struct drm_device *dev)
  13680. {
  13681. struct drm_i915_private *dev_priv = to_i915(dev);
  13682. intel_update_cdclk(dev);
  13683. dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
  13684. intel_init_clock_gating(dev);
  13685. }
  13686. /*
  13687. * Calculate what we think the watermarks should be for the state we've read
  13688. * out of the hardware and then immediately program those watermarks so that
  13689. * we ensure the hardware settings match our internal state.
  13690. *
  13691. * We can calculate what we think WM's should be by creating a duplicate of the
  13692. * current state (which was constructed during hardware readout) and running it
  13693. * through the atomic check code to calculate new watermark values in the
  13694. * state object.
  13695. */
  13696. static void sanitize_watermarks(struct drm_device *dev)
  13697. {
  13698. struct drm_i915_private *dev_priv = to_i915(dev);
  13699. struct drm_atomic_state *state;
  13700. struct drm_crtc *crtc;
  13701. struct drm_crtc_state *cstate;
  13702. struct drm_modeset_acquire_ctx ctx;
  13703. int ret;
  13704. int i;
  13705. /* Only supported on platforms that use atomic watermark design */
  13706. if (!dev_priv->display.optimize_watermarks)
  13707. return;
  13708. /*
  13709. * We need to hold connection_mutex before calling duplicate_state so
  13710. * that the connector loop is protected.
  13711. */
  13712. drm_modeset_acquire_init(&ctx, 0);
  13713. retry:
  13714. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  13715. if (ret == -EDEADLK) {
  13716. drm_modeset_backoff(&ctx);
  13717. goto retry;
  13718. } else if (WARN_ON(ret)) {
  13719. goto fail;
  13720. }
  13721. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  13722. if (WARN_ON(IS_ERR(state)))
  13723. goto fail;
  13724. /*
  13725. * Hardware readout is the only time we don't want to calculate
  13726. * intermediate watermarks (since we don't trust the current
  13727. * watermarks).
  13728. */
  13729. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  13730. ret = intel_atomic_check(dev, state);
  13731. if (ret) {
  13732. /*
  13733. * If we fail here, it means that the hardware appears to be
  13734. * programmed in a way that shouldn't be possible, given our
  13735. * understanding of watermark requirements. This might mean a
  13736. * mistake in the hardware readout code or a mistake in the
  13737. * watermark calculations for a given platform. Raise a WARN
  13738. * so that this is noticeable.
  13739. *
  13740. * If this actually happens, we'll have to just leave the
  13741. * BIOS-programmed watermarks untouched and hope for the best.
  13742. */
  13743. WARN(true, "Could not determine valid watermarks for inherited state\n");
  13744. goto fail;
  13745. }
  13746. /* Write calculated watermark values back */
  13747. for_each_crtc_in_state(state, crtc, cstate, i) {
  13748. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  13749. cs->wm.need_postvbl_update = true;
  13750. dev_priv->display.optimize_watermarks(cs);
  13751. }
  13752. drm_atomic_state_free(state);
  13753. fail:
  13754. drm_modeset_drop_locks(&ctx);
  13755. drm_modeset_acquire_fini(&ctx);
  13756. }
  13757. void intel_modeset_init(struct drm_device *dev)
  13758. {
  13759. struct drm_i915_private *dev_priv = to_i915(dev);
  13760. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  13761. int sprite, ret;
  13762. enum pipe pipe;
  13763. struct intel_crtc *crtc;
  13764. drm_mode_config_init(dev);
  13765. dev->mode_config.min_width = 0;
  13766. dev->mode_config.min_height = 0;
  13767. dev->mode_config.preferred_depth = 24;
  13768. dev->mode_config.prefer_shadow = 1;
  13769. dev->mode_config.allow_fb_modifiers = true;
  13770. dev->mode_config.funcs = &intel_mode_funcs;
  13771. intel_init_quirks(dev);
  13772. intel_init_pm(dev);
  13773. if (INTEL_INFO(dev)->num_pipes == 0)
  13774. return;
  13775. /*
  13776. * There may be no VBT; and if the BIOS enabled SSC we can
  13777. * just keep using it to avoid unnecessary flicker. Whereas if the
  13778. * BIOS isn't using it, don't assume it will work even if the VBT
  13779. * indicates as much.
  13780. */
  13781. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
  13782. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  13783. DREF_SSC1_ENABLE);
  13784. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  13785. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  13786. bios_lvds_use_ssc ? "en" : "dis",
  13787. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  13788. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  13789. }
  13790. }
  13791. if (IS_GEN2(dev_priv)) {
  13792. dev->mode_config.max_width = 2048;
  13793. dev->mode_config.max_height = 2048;
  13794. } else if (IS_GEN3(dev_priv)) {
  13795. dev->mode_config.max_width = 4096;
  13796. dev->mode_config.max_height = 4096;
  13797. } else {
  13798. dev->mode_config.max_width = 8192;
  13799. dev->mode_config.max_height = 8192;
  13800. }
  13801. if (IS_845G(dev_priv) || IS_I865G(dev_priv)) {
  13802. dev->mode_config.cursor_width = IS_845G(dev_priv) ? 64 : 512;
  13803. dev->mode_config.cursor_height = 1023;
  13804. } else if (IS_GEN2(dev_priv)) {
  13805. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  13806. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  13807. } else {
  13808. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  13809. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  13810. }
  13811. dev->mode_config.fb_base = ggtt->mappable_base;
  13812. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  13813. INTEL_INFO(dev)->num_pipes,
  13814. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  13815. for_each_pipe(dev_priv, pipe) {
  13816. intel_crtc_init(dev, pipe);
  13817. for_each_sprite(dev_priv, pipe, sprite) {
  13818. ret = intel_plane_init(dev, pipe, sprite);
  13819. if (ret)
  13820. DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
  13821. pipe_name(pipe), sprite_name(pipe, sprite), ret);
  13822. }
  13823. }
  13824. intel_update_czclk(dev_priv);
  13825. intel_update_cdclk(dev);
  13826. intel_shared_dpll_init(dev);
  13827. if (dev_priv->max_cdclk_freq == 0)
  13828. intel_update_max_cdclk(dev);
  13829. /* Just disable it once at startup */
  13830. i915_disable_vga(dev);
  13831. intel_setup_outputs(dev);
  13832. drm_modeset_lock_all(dev);
  13833. intel_modeset_setup_hw_state(dev);
  13834. drm_modeset_unlock_all(dev);
  13835. for_each_intel_crtc(dev, crtc) {
  13836. struct intel_initial_plane_config plane_config = {};
  13837. if (!crtc->active)
  13838. continue;
  13839. /*
  13840. * Note that reserving the BIOS fb up front prevents us
  13841. * from stuffing other stolen allocations like the ring
  13842. * on top. This prevents some ugliness at boot time, and
  13843. * can even allow for smooth boot transitions if the BIOS
  13844. * fb is large enough for the active pipe configuration.
  13845. */
  13846. dev_priv->display.get_initial_plane_config(crtc,
  13847. &plane_config);
  13848. /*
  13849. * If the fb is shared between multiple heads, we'll
  13850. * just get the first one.
  13851. */
  13852. intel_find_initial_plane_obj(crtc, &plane_config);
  13853. }
  13854. /*
  13855. * Make sure hardware watermarks really match the state we read out.
  13856. * Note that we need to do this after reconstructing the BIOS fb's
  13857. * since the watermark calculation done here will use pstate->fb.
  13858. */
  13859. sanitize_watermarks(dev);
  13860. }
  13861. static void intel_enable_pipe_a(struct drm_device *dev)
  13862. {
  13863. struct intel_connector *connector;
  13864. struct drm_connector *crt = NULL;
  13865. struct intel_load_detect_pipe load_detect_temp;
  13866. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  13867. /* We can't just switch on the pipe A, we need to set things up with a
  13868. * proper mode and output configuration. As a gross hack, enable pipe A
  13869. * by enabling the load detect pipe once. */
  13870. for_each_intel_connector(dev, connector) {
  13871. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  13872. crt = &connector->base;
  13873. break;
  13874. }
  13875. }
  13876. if (!crt)
  13877. return;
  13878. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  13879. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  13880. }
  13881. static bool
  13882. intel_check_plane_mapping(struct intel_crtc *crtc)
  13883. {
  13884. struct drm_device *dev = crtc->base.dev;
  13885. struct drm_i915_private *dev_priv = to_i915(dev);
  13886. u32 val;
  13887. if (INTEL_INFO(dev)->num_pipes == 1)
  13888. return true;
  13889. val = I915_READ(DSPCNTR(!crtc->plane));
  13890. if ((val & DISPLAY_PLANE_ENABLE) &&
  13891. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  13892. return false;
  13893. return true;
  13894. }
  13895. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  13896. {
  13897. struct drm_device *dev = crtc->base.dev;
  13898. struct intel_encoder *encoder;
  13899. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  13900. return true;
  13901. return false;
  13902. }
  13903. static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
  13904. {
  13905. struct drm_device *dev = encoder->base.dev;
  13906. struct intel_connector *connector;
  13907. for_each_connector_on_encoder(dev, &encoder->base, connector)
  13908. return connector;
  13909. return NULL;
  13910. }
  13911. static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
  13912. enum transcoder pch_transcoder)
  13913. {
  13914. return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
  13915. (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
  13916. }
  13917. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  13918. {
  13919. struct drm_device *dev = crtc->base.dev;
  13920. struct drm_i915_private *dev_priv = to_i915(dev);
  13921. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  13922. /* Clear any frame start delays used for debugging left by the BIOS */
  13923. if (!transcoder_is_dsi(cpu_transcoder)) {
  13924. i915_reg_t reg = PIPECONF(cpu_transcoder);
  13925. I915_WRITE(reg,
  13926. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  13927. }
  13928. /* restore vblank interrupts to correct state */
  13929. drm_crtc_vblank_reset(&crtc->base);
  13930. if (crtc->active) {
  13931. struct intel_plane *plane;
  13932. drm_crtc_vblank_on(&crtc->base);
  13933. /* Disable everything but the primary plane */
  13934. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  13935. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  13936. continue;
  13937. plane->disable_plane(&plane->base, &crtc->base);
  13938. }
  13939. }
  13940. /* We need to sanitize the plane -> pipe mapping first because this will
  13941. * disable the crtc (and hence change the state) if it is wrong. Note
  13942. * that gen4+ has a fixed plane -> pipe mapping. */
  13943. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  13944. bool plane;
  13945. DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
  13946. crtc->base.base.id, crtc->base.name);
  13947. /* Pipe has the wrong plane attached and the plane is active.
  13948. * Temporarily change the plane mapping and disable everything
  13949. * ... */
  13950. plane = crtc->plane;
  13951. to_intel_plane_state(crtc->base.primary->state)->base.visible = true;
  13952. crtc->plane = !plane;
  13953. intel_crtc_disable_noatomic(&crtc->base);
  13954. crtc->plane = plane;
  13955. }
  13956. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  13957. crtc->pipe == PIPE_A && !crtc->active) {
  13958. /* BIOS forgot to enable pipe A, this mostly happens after
  13959. * resume. Force-enable the pipe to fix this, the update_dpms
  13960. * call below we restore the pipe to the right state, but leave
  13961. * the required bits on. */
  13962. intel_enable_pipe_a(dev);
  13963. }
  13964. /* Adjust the state of the output pipe according to whether we
  13965. * have active connectors/encoders. */
  13966. if (crtc->active && !intel_crtc_has_encoders(crtc))
  13967. intel_crtc_disable_noatomic(&crtc->base);
  13968. if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
  13969. /*
  13970. * We start out with underrun reporting disabled to avoid races.
  13971. * For correct bookkeeping mark this on active crtcs.
  13972. *
  13973. * Also on gmch platforms we dont have any hardware bits to
  13974. * disable the underrun reporting. Which means we need to start
  13975. * out with underrun reporting disabled also on inactive pipes,
  13976. * since otherwise we'll complain about the garbage we read when
  13977. * e.g. coming up after runtime pm.
  13978. *
  13979. * No protection against concurrent access is required - at
  13980. * worst a fifo underrun happens which also sets this to false.
  13981. */
  13982. crtc->cpu_fifo_underrun_disabled = true;
  13983. /*
  13984. * We track the PCH trancoder underrun reporting state
  13985. * within the crtc. With crtc for pipe A housing the underrun
  13986. * reporting state for PCH transcoder A, crtc for pipe B housing
  13987. * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
  13988. * and marking underrun reporting as disabled for the non-existing
  13989. * PCH transcoders B and C would prevent enabling the south
  13990. * error interrupt (see cpt_can_enable_serr_int()).
  13991. */
  13992. if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
  13993. crtc->pch_fifo_underrun_disabled = true;
  13994. }
  13995. }
  13996. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  13997. {
  13998. struct intel_connector *connector;
  13999. /* We need to check both for a crtc link (meaning that the
  14000. * encoder is active and trying to read from a pipe) and the
  14001. * pipe itself being active. */
  14002. bool has_active_crtc = encoder->base.crtc &&
  14003. to_intel_crtc(encoder->base.crtc)->active;
  14004. connector = intel_encoder_find_connector(encoder);
  14005. if (connector && !has_active_crtc) {
  14006. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  14007. encoder->base.base.id,
  14008. encoder->base.name);
  14009. /* Connector is active, but has no active pipe. This is
  14010. * fallout from our resume register restoring. Disable
  14011. * the encoder manually again. */
  14012. if (encoder->base.crtc) {
  14013. struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
  14014. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  14015. encoder->base.base.id,
  14016. encoder->base.name);
  14017. encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  14018. if (encoder->post_disable)
  14019. encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  14020. }
  14021. encoder->base.crtc = NULL;
  14022. /* Inconsistent output/port/pipe state happens presumably due to
  14023. * a bug in one of the get_hw_state functions. Or someplace else
  14024. * in our code, like the register restore mess on resume. Clamp
  14025. * things to off as a safer default. */
  14026. connector->base.dpms = DRM_MODE_DPMS_OFF;
  14027. connector->base.encoder = NULL;
  14028. }
  14029. /* Enabled encoders without active connectors will be fixed in
  14030. * the crtc fixup. */
  14031. }
  14032. void i915_redisable_vga_power_on(struct drm_device *dev)
  14033. {
  14034. struct drm_i915_private *dev_priv = to_i915(dev);
  14035. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  14036. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  14037. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  14038. i915_disable_vga(dev);
  14039. }
  14040. }
  14041. void i915_redisable_vga(struct drm_device *dev)
  14042. {
  14043. struct drm_i915_private *dev_priv = to_i915(dev);
  14044. /* This function can be called both from intel_modeset_setup_hw_state or
  14045. * at a very early point in our resume sequence, where the power well
  14046. * structures are not yet restored. Since this function is at a very
  14047. * paranoid "someone might have enabled VGA while we were not looking"
  14048. * level, just check if the power well is enabled instead of trying to
  14049. * follow the "don't touch the power well if we don't need it" policy
  14050. * the rest of the driver uses. */
  14051. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  14052. return;
  14053. i915_redisable_vga_power_on(dev);
  14054. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  14055. }
  14056. static bool primary_get_hw_state(struct intel_plane *plane)
  14057. {
  14058. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  14059. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  14060. }
  14061. /* FIXME read out full plane state for all planes */
  14062. static void readout_plane_state(struct intel_crtc *crtc)
  14063. {
  14064. struct drm_plane *primary = crtc->base.primary;
  14065. struct intel_plane_state *plane_state =
  14066. to_intel_plane_state(primary->state);
  14067. plane_state->base.visible = crtc->active &&
  14068. primary_get_hw_state(to_intel_plane(primary));
  14069. if (plane_state->base.visible)
  14070. crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
  14071. }
  14072. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  14073. {
  14074. struct drm_i915_private *dev_priv = to_i915(dev);
  14075. enum pipe pipe;
  14076. struct intel_crtc *crtc;
  14077. struct intel_encoder *encoder;
  14078. struct intel_connector *connector;
  14079. int i;
  14080. dev_priv->active_crtcs = 0;
  14081. for_each_intel_crtc(dev, crtc) {
  14082. struct intel_crtc_state *crtc_state = crtc->config;
  14083. int pixclk = 0;
  14084. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  14085. memset(crtc_state, 0, sizeof(*crtc_state));
  14086. crtc_state->base.crtc = &crtc->base;
  14087. crtc_state->base.active = crtc_state->base.enable =
  14088. dev_priv->display.get_pipe_config(crtc, crtc_state);
  14089. crtc->base.enabled = crtc_state->base.enable;
  14090. crtc->active = crtc_state->base.active;
  14091. if (crtc_state->base.active) {
  14092. dev_priv->active_crtcs |= 1 << crtc->pipe;
  14093. if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
  14094. pixclk = ilk_pipe_pixel_rate(crtc_state);
  14095. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  14096. pixclk = crtc_state->base.adjusted_mode.crtc_clock;
  14097. else
  14098. WARN_ON(dev_priv->display.modeset_calc_cdclk);
  14099. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  14100. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  14101. pixclk = DIV_ROUND_UP(pixclk * 100, 95);
  14102. }
  14103. dev_priv->min_pixclk[crtc->pipe] = pixclk;
  14104. readout_plane_state(crtc);
  14105. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  14106. crtc->base.base.id, crtc->base.name,
  14107. crtc->active ? "enabled" : "disabled");
  14108. }
  14109. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  14110. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  14111. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  14112. &pll->config.hw_state);
  14113. pll->config.crtc_mask = 0;
  14114. for_each_intel_crtc(dev, crtc) {
  14115. if (crtc->active && crtc->config->shared_dpll == pll)
  14116. pll->config.crtc_mask |= 1 << crtc->pipe;
  14117. }
  14118. pll->active_mask = pll->config.crtc_mask;
  14119. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  14120. pll->name, pll->config.crtc_mask, pll->on);
  14121. }
  14122. for_each_intel_encoder(dev, encoder) {
  14123. pipe = 0;
  14124. if (encoder->get_hw_state(encoder, &pipe)) {
  14125. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  14126. encoder->base.crtc = &crtc->base;
  14127. crtc->config->output_types |= 1 << encoder->type;
  14128. encoder->get_config(encoder, crtc->config);
  14129. } else {
  14130. encoder->base.crtc = NULL;
  14131. }
  14132. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  14133. encoder->base.base.id,
  14134. encoder->base.name,
  14135. encoder->base.crtc ? "enabled" : "disabled",
  14136. pipe_name(pipe));
  14137. }
  14138. for_each_intel_connector(dev, connector) {
  14139. if (connector->get_hw_state(connector)) {
  14140. connector->base.dpms = DRM_MODE_DPMS_ON;
  14141. encoder = connector->encoder;
  14142. connector->base.encoder = &encoder->base;
  14143. if (encoder->base.crtc &&
  14144. encoder->base.crtc->state->active) {
  14145. /*
  14146. * This has to be done during hardware readout
  14147. * because anything calling .crtc_disable may
  14148. * rely on the connector_mask being accurate.
  14149. */
  14150. encoder->base.crtc->state->connector_mask |=
  14151. 1 << drm_connector_index(&connector->base);
  14152. encoder->base.crtc->state->encoder_mask |=
  14153. 1 << drm_encoder_index(&encoder->base);
  14154. }
  14155. } else {
  14156. connector->base.dpms = DRM_MODE_DPMS_OFF;
  14157. connector->base.encoder = NULL;
  14158. }
  14159. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  14160. connector->base.base.id,
  14161. connector->base.name,
  14162. connector->base.encoder ? "enabled" : "disabled");
  14163. }
  14164. for_each_intel_crtc(dev, crtc) {
  14165. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  14166. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  14167. if (crtc->base.state->active) {
  14168. intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
  14169. intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
  14170. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  14171. /*
  14172. * The initial mode needs to be set in order to keep
  14173. * the atomic core happy. It wants a valid mode if the
  14174. * crtc's enabled, so we do the above call.
  14175. *
  14176. * At this point some state updated by the connectors
  14177. * in their ->detect() callback has not run yet, so
  14178. * no recalculation can be done yet.
  14179. *
  14180. * Even if we could do a recalculation and modeset
  14181. * right now it would cause a double modeset if
  14182. * fbdev or userspace chooses a different initial mode.
  14183. *
  14184. * If that happens, someone indicated they wanted a
  14185. * mode change, which means it's safe to do a full
  14186. * recalculation.
  14187. */
  14188. crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
  14189. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  14190. update_scanline_offset(crtc);
  14191. }
  14192. intel_pipe_config_sanity_check(dev_priv, crtc->config);
  14193. }
  14194. }
  14195. /* Scan out the current hw modeset state,
  14196. * and sanitizes it to the current state
  14197. */
  14198. static void
  14199. intel_modeset_setup_hw_state(struct drm_device *dev)
  14200. {
  14201. struct drm_i915_private *dev_priv = to_i915(dev);
  14202. enum pipe pipe;
  14203. struct intel_crtc *crtc;
  14204. struct intel_encoder *encoder;
  14205. int i;
  14206. intel_modeset_readout_hw_state(dev);
  14207. /* HW state is read out, now we need to sanitize this mess. */
  14208. for_each_intel_encoder(dev, encoder) {
  14209. intel_sanitize_encoder(encoder);
  14210. }
  14211. for_each_pipe(dev_priv, pipe) {
  14212. crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
  14213. intel_sanitize_crtc(crtc);
  14214. intel_dump_pipe_config(crtc, crtc->config,
  14215. "[setup_hw_state]");
  14216. }
  14217. intel_modeset_update_connector_atomic_state(dev);
  14218. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  14219. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  14220. if (!pll->on || pll->active_mask)
  14221. continue;
  14222. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  14223. pll->funcs.disable(dev_priv, pll);
  14224. pll->on = false;
  14225. }
  14226. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  14227. vlv_wm_get_hw_state(dev);
  14228. else if (IS_GEN9(dev_priv))
  14229. skl_wm_get_hw_state(dev);
  14230. else if (HAS_PCH_SPLIT(dev_priv))
  14231. ilk_wm_get_hw_state(dev);
  14232. for_each_intel_crtc(dev, crtc) {
  14233. unsigned long put_domains;
  14234. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  14235. if (WARN_ON(put_domains))
  14236. modeset_put_power_domains(dev_priv, put_domains);
  14237. }
  14238. intel_display_set_init_power(dev_priv, false);
  14239. intel_fbc_init_pipe_state(dev_priv);
  14240. }
  14241. void intel_display_resume(struct drm_device *dev)
  14242. {
  14243. struct drm_i915_private *dev_priv = to_i915(dev);
  14244. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  14245. struct drm_modeset_acquire_ctx ctx;
  14246. int ret;
  14247. dev_priv->modeset_restore_state = NULL;
  14248. if (state)
  14249. state->acquire_ctx = &ctx;
  14250. /*
  14251. * This is a cludge because with real atomic modeset mode_config.mutex
  14252. * won't be taken. Unfortunately some probed state like
  14253. * audio_codec_enable is still protected by mode_config.mutex, so lock
  14254. * it here for now.
  14255. */
  14256. mutex_lock(&dev->mode_config.mutex);
  14257. drm_modeset_acquire_init(&ctx, 0);
  14258. while (1) {
  14259. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  14260. if (ret != -EDEADLK)
  14261. break;
  14262. drm_modeset_backoff(&ctx);
  14263. }
  14264. if (!ret)
  14265. ret = __intel_display_resume(dev, state);
  14266. drm_modeset_drop_locks(&ctx);
  14267. drm_modeset_acquire_fini(&ctx);
  14268. mutex_unlock(&dev->mode_config.mutex);
  14269. if (ret) {
  14270. DRM_ERROR("Restoring old state failed with %i\n", ret);
  14271. drm_atomic_state_free(state);
  14272. }
  14273. }
  14274. void intel_modeset_gem_init(struct drm_device *dev)
  14275. {
  14276. struct drm_i915_private *dev_priv = to_i915(dev);
  14277. struct drm_crtc *c;
  14278. struct drm_i915_gem_object *obj;
  14279. intel_init_gt_powersave(dev_priv);
  14280. intel_modeset_init_hw(dev);
  14281. intel_setup_overlay(dev_priv);
  14282. /*
  14283. * Make sure any fbs we allocated at startup are properly
  14284. * pinned & fenced. When we do the allocation it's too early
  14285. * for this.
  14286. */
  14287. for_each_crtc(dev, c) {
  14288. struct i915_vma *vma;
  14289. obj = intel_fb_obj(c->primary->fb);
  14290. if (obj == NULL)
  14291. continue;
  14292. mutex_lock(&dev->struct_mutex);
  14293. vma = intel_pin_and_fence_fb_obj(c->primary->fb,
  14294. c->primary->state->rotation);
  14295. mutex_unlock(&dev->struct_mutex);
  14296. if (IS_ERR(vma)) {
  14297. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  14298. to_intel_crtc(c)->pipe);
  14299. drm_framebuffer_unreference(c->primary->fb);
  14300. c->primary->fb = NULL;
  14301. c->primary->crtc = c->primary->state->crtc = NULL;
  14302. update_state_fb(c->primary);
  14303. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  14304. }
  14305. }
  14306. }
  14307. int intel_connector_register(struct drm_connector *connector)
  14308. {
  14309. struct intel_connector *intel_connector = to_intel_connector(connector);
  14310. int ret;
  14311. ret = intel_backlight_device_register(intel_connector);
  14312. if (ret)
  14313. goto err;
  14314. return 0;
  14315. err:
  14316. return ret;
  14317. }
  14318. void intel_connector_unregister(struct drm_connector *connector)
  14319. {
  14320. struct intel_connector *intel_connector = to_intel_connector(connector);
  14321. intel_backlight_device_unregister(intel_connector);
  14322. intel_panel_destroy_backlight(connector);
  14323. }
  14324. void intel_modeset_cleanup(struct drm_device *dev)
  14325. {
  14326. struct drm_i915_private *dev_priv = to_i915(dev);
  14327. intel_disable_gt_powersave(dev_priv);
  14328. /*
  14329. * Interrupts and polling as the first thing to avoid creating havoc.
  14330. * Too much stuff here (turning of connectors, ...) would
  14331. * experience fancy races otherwise.
  14332. */
  14333. intel_irq_uninstall(dev_priv);
  14334. /*
  14335. * Due to the hpd irq storm handling the hotplug work can re-arm the
  14336. * poll handlers. Hence disable polling after hpd handling is shut down.
  14337. */
  14338. drm_kms_helper_poll_fini(dev);
  14339. intel_unregister_dsm_handler();
  14340. intel_fbc_global_disable(dev_priv);
  14341. /* flush any delayed tasks or pending work */
  14342. flush_scheduled_work();
  14343. drm_mode_config_cleanup(dev);
  14344. intel_cleanup_overlay(dev_priv);
  14345. intel_cleanup_gt_powersave(dev_priv);
  14346. intel_teardown_gmbus(dev);
  14347. }
  14348. void intel_connector_attach_encoder(struct intel_connector *connector,
  14349. struct intel_encoder *encoder)
  14350. {
  14351. connector->encoder = encoder;
  14352. drm_mode_connector_attach_encoder(&connector->base,
  14353. &encoder->base);
  14354. }
  14355. /*
  14356. * set vga decode state - true == enable VGA decode
  14357. */
  14358. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  14359. {
  14360. struct drm_i915_private *dev_priv = to_i915(dev);
  14361. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  14362. u16 gmch_ctrl;
  14363. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  14364. DRM_ERROR("failed to read control word\n");
  14365. return -EIO;
  14366. }
  14367. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  14368. return 0;
  14369. if (state)
  14370. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  14371. else
  14372. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  14373. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  14374. DRM_ERROR("failed to write control word\n");
  14375. return -EIO;
  14376. }
  14377. return 0;
  14378. }
  14379. #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
  14380. struct intel_display_error_state {
  14381. u32 power_well_driver;
  14382. int num_transcoders;
  14383. struct intel_cursor_error_state {
  14384. u32 control;
  14385. u32 position;
  14386. u32 base;
  14387. u32 size;
  14388. } cursor[I915_MAX_PIPES];
  14389. struct intel_pipe_error_state {
  14390. bool power_domain_on;
  14391. u32 source;
  14392. u32 stat;
  14393. } pipe[I915_MAX_PIPES];
  14394. struct intel_plane_error_state {
  14395. u32 control;
  14396. u32 stride;
  14397. u32 size;
  14398. u32 pos;
  14399. u32 addr;
  14400. u32 surface;
  14401. u32 tile_offset;
  14402. } plane[I915_MAX_PIPES];
  14403. struct intel_transcoder_error_state {
  14404. bool power_domain_on;
  14405. enum transcoder cpu_transcoder;
  14406. u32 conf;
  14407. u32 htotal;
  14408. u32 hblank;
  14409. u32 hsync;
  14410. u32 vtotal;
  14411. u32 vblank;
  14412. u32 vsync;
  14413. } transcoder[4];
  14414. };
  14415. struct intel_display_error_state *
  14416. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  14417. {
  14418. struct intel_display_error_state *error;
  14419. int transcoders[] = {
  14420. TRANSCODER_A,
  14421. TRANSCODER_B,
  14422. TRANSCODER_C,
  14423. TRANSCODER_EDP,
  14424. };
  14425. int i;
  14426. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  14427. return NULL;
  14428. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  14429. if (error == NULL)
  14430. return NULL;
  14431. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  14432. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  14433. for_each_pipe(dev_priv, i) {
  14434. error->pipe[i].power_domain_on =
  14435. __intel_display_power_is_enabled(dev_priv,
  14436. POWER_DOMAIN_PIPE(i));
  14437. if (!error->pipe[i].power_domain_on)
  14438. continue;
  14439. error->cursor[i].control = I915_READ(CURCNTR(i));
  14440. error->cursor[i].position = I915_READ(CURPOS(i));
  14441. error->cursor[i].base = I915_READ(CURBASE(i));
  14442. error->plane[i].control = I915_READ(DSPCNTR(i));
  14443. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  14444. if (INTEL_GEN(dev_priv) <= 3) {
  14445. error->plane[i].size = I915_READ(DSPSIZE(i));
  14446. error->plane[i].pos = I915_READ(DSPPOS(i));
  14447. }
  14448. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  14449. error->plane[i].addr = I915_READ(DSPADDR(i));
  14450. if (INTEL_GEN(dev_priv) >= 4) {
  14451. error->plane[i].surface = I915_READ(DSPSURF(i));
  14452. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  14453. }
  14454. error->pipe[i].source = I915_READ(PIPESRC(i));
  14455. if (HAS_GMCH_DISPLAY(dev_priv))
  14456. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  14457. }
  14458. /* Note: this does not include DSI transcoders. */
  14459. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  14460. if (HAS_DDI(dev_priv))
  14461. error->num_transcoders++; /* Account for eDP. */
  14462. for (i = 0; i < error->num_transcoders; i++) {
  14463. enum transcoder cpu_transcoder = transcoders[i];
  14464. error->transcoder[i].power_domain_on =
  14465. __intel_display_power_is_enabled(dev_priv,
  14466. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  14467. if (!error->transcoder[i].power_domain_on)
  14468. continue;
  14469. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  14470. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  14471. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  14472. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  14473. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  14474. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  14475. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  14476. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  14477. }
  14478. return error;
  14479. }
  14480. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  14481. void
  14482. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  14483. struct drm_device *dev,
  14484. struct intel_display_error_state *error)
  14485. {
  14486. struct drm_i915_private *dev_priv = to_i915(dev);
  14487. int i;
  14488. if (!error)
  14489. return;
  14490. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  14491. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  14492. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  14493. error->power_well_driver);
  14494. for_each_pipe(dev_priv, i) {
  14495. err_printf(m, "Pipe [%d]:\n", i);
  14496. err_printf(m, " Power: %s\n",
  14497. onoff(error->pipe[i].power_domain_on));
  14498. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  14499. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  14500. err_printf(m, "Plane [%d]:\n", i);
  14501. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  14502. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  14503. if (INTEL_INFO(dev)->gen <= 3) {
  14504. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  14505. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  14506. }
  14507. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  14508. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  14509. if (INTEL_INFO(dev)->gen >= 4) {
  14510. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  14511. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  14512. }
  14513. err_printf(m, "Cursor [%d]:\n", i);
  14514. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  14515. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  14516. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  14517. }
  14518. for (i = 0; i < error->num_transcoders; i++) {
  14519. err_printf(m, "CPU transcoder: %s\n",
  14520. transcoder_name(error->transcoder[i].cpu_transcoder));
  14521. err_printf(m, " Power: %s\n",
  14522. onoff(error->transcoder[i].power_domain_on));
  14523. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  14524. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  14525. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  14526. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  14527. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  14528. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  14529. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  14530. }
  14531. }
  14532. #endif