intel_display.c 486 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include "intel_frontbuffer.h"
  37. #include <drm/i915_drm.h>
  38. #include "i915_drv.h"
  39. #include "intel_dsi.h"
  40. #include "i915_trace.h"
  41. #include <drm/drm_atomic.h>
  42. #include <drm/drm_atomic_helper.h>
  43. #include <drm/drm_dp_helper.h>
  44. #include <drm/drm_crtc_helper.h>
  45. #include <drm/drm_plane_helper.h>
  46. #include <drm/drm_rect.h>
  47. #include <linux/dma_remapping.h>
  48. #include <linux/reservation.h>
  49. static bool is_mmio_work(struct intel_flip_work *work)
  50. {
  51. return work->mmio_work.func;
  52. }
  53. /* Primary plane formats for gen <= 3 */
  54. static const uint32_t i8xx_primary_formats[] = {
  55. DRM_FORMAT_C8,
  56. DRM_FORMAT_RGB565,
  57. DRM_FORMAT_XRGB1555,
  58. DRM_FORMAT_XRGB8888,
  59. };
  60. /* Primary plane formats for gen >= 4 */
  61. static const uint32_t i965_primary_formats[] = {
  62. DRM_FORMAT_C8,
  63. DRM_FORMAT_RGB565,
  64. DRM_FORMAT_XRGB8888,
  65. DRM_FORMAT_XBGR8888,
  66. DRM_FORMAT_XRGB2101010,
  67. DRM_FORMAT_XBGR2101010,
  68. };
  69. static const uint32_t skl_primary_formats[] = {
  70. DRM_FORMAT_C8,
  71. DRM_FORMAT_RGB565,
  72. DRM_FORMAT_XRGB8888,
  73. DRM_FORMAT_XBGR8888,
  74. DRM_FORMAT_ARGB8888,
  75. DRM_FORMAT_ABGR8888,
  76. DRM_FORMAT_XRGB2101010,
  77. DRM_FORMAT_XBGR2101010,
  78. DRM_FORMAT_YUYV,
  79. DRM_FORMAT_YVYU,
  80. DRM_FORMAT_UYVY,
  81. DRM_FORMAT_VYUY,
  82. };
  83. /* Cursor formats */
  84. static const uint32_t intel_cursor_formats[] = {
  85. DRM_FORMAT_ARGB8888,
  86. };
  87. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  88. struct intel_crtc_state *pipe_config);
  89. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  90. struct intel_crtc_state *pipe_config);
  91. static int intel_framebuffer_init(struct drm_device *dev,
  92. struct intel_framebuffer *ifb,
  93. struct drm_mode_fb_cmd2 *mode_cmd,
  94. struct drm_i915_gem_object *obj);
  95. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  96. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  97. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
  98. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  99. struct intel_link_m_n *m_n,
  100. struct intel_link_m_n *m2_n2);
  101. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  102. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  103. static void haswell_set_pipemisc(struct drm_crtc *crtc);
  104. static void vlv_prepare_pll(struct intel_crtc *crtc,
  105. const struct intel_crtc_state *pipe_config);
  106. static void chv_prepare_pll(struct intel_crtc *crtc,
  107. const struct intel_crtc_state *pipe_config);
  108. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  109. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  110. static void skl_init_scalers(struct drm_i915_private *dev_priv,
  111. struct intel_crtc *crtc,
  112. struct intel_crtc_state *crtc_state);
  113. static void skylake_pfit_enable(struct intel_crtc *crtc);
  114. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
  115. static void ironlake_pfit_enable(struct intel_crtc *crtc);
  116. static void intel_modeset_setup_hw_state(struct drm_device *dev);
  117. static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
  118. static int ilk_max_pixel_rate(struct drm_atomic_state *state);
  119. static int bxt_calc_cdclk(int max_pixclk);
  120. struct intel_limit {
  121. struct {
  122. int min, max;
  123. } dot, vco, n, m, m1, m2, p, p1;
  124. struct {
  125. int dot_limit;
  126. int p2_slow, p2_fast;
  127. } p2;
  128. };
  129. /* returns HPLL frequency in kHz */
  130. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  131. {
  132. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  133. /* Obtain SKU information */
  134. mutex_lock(&dev_priv->sb_lock);
  135. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  136. CCK_FUSE_HPLL_FREQ_MASK;
  137. mutex_unlock(&dev_priv->sb_lock);
  138. return vco_freq[hpll_freq] * 1000;
  139. }
  140. int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
  141. const char *name, u32 reg, int ref_freq)
  142. {
  143. u32 val;
  144. int divider;
  145. mutex_lock(&dev_priv->sb_lock);
  146. val = vlv_cck_read(dev_priv, reg);
  147. mutex_unlock(&dev_priv->sb_lock);
  148. divider = val & CCK_FREQUENCY_VALUES;
  149. WARN((val & CCK_FREQUENCY_STATUS) !=
  150. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  151. "%s change in progress\n", name);
  152. return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
  153. }
  154. static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  155. const char *name, u32 reg)
  156. {
  157. if (dev_priv->hpll_freq == 0)
  158. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  159. return vlv_get_cck_clock(dev_priv, name, reg,
  160. dev_priv->hpll_freq);
  161. }
  162. static int
  163. intel_pch_rawclk(struct drm_i915_private *dev_priv)
  164. {
  165. return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
  166. }
  167. static int
  168. intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
  169. {
  170. /* RAWCLK_FREQ_VLV register updated from power well code */
  171. return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
  172. CCK_DISPLAY_REF_CLOCK_CONTROL);
  173. }
  174. static int
  175. intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
  176. {
  177. uint32_t clkcfg;
  178. /* hrawclock is 1/4 the FSB frequency */
  179. clkcfg = I915_READ(CLKCFG);
  180. switch (clkcfg & CLKCFG_FSB_MASK) {
  181. case CLKCFG_FSB_400:
  182. return 100000;
  183. case CLKCFG_FSB_533:
  184. return 133333;
  185. case CLKCFG_FSB_667:
  186. return 166667;
  187. case CLKCFG_FSB_800:
  188. return 200000;
  189. case CLKCFG_FSB_1067:
  190. return 266667;
  191. case CLKCFG_FSB_1333:
  192. return 333333;
  193. /* these two are just a guess; one of them might be right */
  194. case CLKCFG_FSB_1600:
  195. case CLKCFG_FSB_1600_ALT:
  196. return 400000;
  197. default:
  198. return 133333;
  199. }
  200. }
  201. void intel_update_rawclk(struct drm_i915_private *dev_priv)
  202. {
  203. if (HAS_PCH_SPLIT(dev_priv))
  204. dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
  205. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  206. dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
  207. else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
  208. dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
  209. else
  210. return; /* no rawclk on other platforms, or no need to know it */
  211. DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
  212. }
  213. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  214. {
  215. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  216. return;
  217. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  218. CCK_CZ_CLOCK_CONTROL);
  219. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  220. }
  221. static inline u32 /* units of 100MHz */
  222. intel_fdi_link_freq(struct drm_i915_private *dev_priv,
  223. const struct intel_crtc_state *pipe_config)
  224. {
  225. if (HAS_DDI(dev_priv))
  226. return pipe_config->port_clock; /* SPLL */
  227. else if (IS_GEN5(dev_priv))
  228. return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
  229. else
  230. return 270000;
  231. }
  232. static const struct intel_limit intel_limits_i8xx_dac = {
  233. .dot = { .min = 25000, .max = 350000 },
  234. .vco = { .min = 908000, .max = 1512000 },
  235. .n = { .min = 2, .max = 16 },
  236. .m = { .min = 96, .max = 140 },
  237. .m1 = { .min = 18, .max = 26 },
  238. .m2 = { .min = 6, .max = 16 },
  239. .p = { .min = 4, .max = 128 },
  240. .p1 = { .min = 2, .max = 33 },
  241. .p2 = { .dot_limit = 165000,
  242. .p2_slow = 4, .p2_fast = 2 },
  243. };
  244. static const struct intel_limit intel_limits_i8xx_dvo = {
  245. .dot = { .min = 25000, .max = 350000 },
  246. .vco = { .min = 908000, .max = 1512000 },
  247. .n = { .min = 2, .max = 16 },
  248. .m = { .min = 96, .max = 140 },
  249. .m1 = { .min = 18, .max = 26 },
  250. .m2 = { .min = 6, .max = 16 },
  251. .p = { .min = 4, .max = 128 },
  252. .p1 = { .min = 2, .max = 33 },
  253. .p2 = { .dot_limit = 165000,
  254. .p2_slow = 4, .p2_fast = 4 },
  255. };
  256. static const struct intel_limit intel_limits_i8xx_lvds = {
  257. .dot = { .min = 25000, .max = 350000 },
  258. .vco = { .min = 908000, .max = 1512000 },
  259. .n = { .min = 2, .max = 16 },
  260. .m = { .min = 96, .max = 140 },
  261. .m1 = { .min = 18, .max = 26 },
  262. .m2 = { .min = 6, .max = 16 },
  263. .p = { .min = 4, .max = 128 },
  264. .p1 = { .min = 1, .max = 6 },
  265. .p2 = { .dot_limit = 165000,
  266. .p2_slow = 14, .p2_fast = 7 },
  267. };
  268. static const struct intel_limit intel_limits_i9xx_sdvo = {
  269. .dot = { .min = 20000, .max = 400000 },
  270. .vco = { .min = 1400000, .max = 2800000 },
  271. .n = { .min = 1, .max = 6 },
  272. .m = { .min = 70, .max = 120 },
  273. .m1 = { .min = 8, .max = 18 },
  274. .m2 = { .min = 3, .max = 7 },
  275. .p = { .min = 5, .max = 80 },
  276. .p1 = { .min = 1, .max = 8 },
  277. .p2 = { .dot_limit = 200000,
  278. .p2_slow = 10, .p2_fast = 5 },
  279. };
  280. static const struct intel_limit intel_limits_i9xx_lvds = {
  281. .dot = { .min = 20000, .max = 400000 },
  282. .vco = { .min = 1400000, .max = 2800000 },
  283. .n = { .min = 1, .max = 6 },
  284. .m = { .min = 70, .max = 120 },
  285. .m1 = { .min = 8, .max = 18 },
  286. .m2 = { .min = 3, .max = 7 },
  287. .p = { .min = 7, .max = 98 },
  288. .p1 = { .min = 1, .max = 8 },
  289. .p2 = { .dot_limit = 112000,
  290. .p2_slow = 14, .p2_fast = 7 },
  291. };
  292. static const struct intel_limit intel_limits_g4x_sdvo = {
  293. .dot = { .min = 25000, .max = 270000 },
  294. .vco = { .min = 1750000, .max = 3500000},
  295. .n = { .min = 1, .max = 4 },
  296. .m = { .min = 104, .max = 138 },
  297. .m1 = { .min = 17, .max = 23 },
  298. .m2 = { .min = 5, .max = 11 },
  299. .p = { .min = 10, .max = 30 },
  300. .p1 = { .min = 1, .max = 3},
  301. .p2 = { .dot_limit = 270000,
  302. .p2_slow = 10,
  303. .p2_fast = 10
  304. },
  305. };
  306. static const struct intel_limit intel_limits_g4x_hdmi = {
  307. .dot = { .min = 22000, .max = 400000 },
  308. .vco = { .min = 1750000, .max = 3500000},
  309. .n = { .min = 1, .max = 4 },
  310. .m = { .min = 104, .max = 138 },
  311. .m1 = { .min = 16, .max = 23 },
  312. .m2 = { .min = 5, .max = 11 },
  313. .p = { .min = 5, .max = 80 },
  314. .p1 = { .min = 1, .max = 8},
  315. .p2 = { .dot_limit = 165000,
  316. .p2_slow = 10, .p2_fast = 5 },
  317. };
  318. static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
  319. .dot = { .min = 20000, .max = 115000 },
  320. .vco = { .min = 1750000, .max = 3500000 },
  321. .n = { .min = 1, .max = 3 },
  322. .m = { .min = 104, .max = 138 },
  323. .m1 = { .min = 17, .max = 23 },
  324. .m2 = { .min = 5, .max = 11 },
  325. .p = { .min = 28, .max = 112 },
  326. .p1 = { .min = 2, .max = 8 },
  327. .p2 = { .dot_limit = 0,
  328. .p2_slow = 14, .p2_fast = 14
  329. },
  330. };
  331. static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
  332. .dot = { .min = 80000, .max = 224000 },
  333. .vco = { .min = 1750000, .max = 3500000 },
  334. .n = { .min = 1, .max = 3 },
  335. .m = { .min = 104, .max = 138 },
  336. .m1 = { .min = 17, .max = 23 },
  337. .m2 = { .min = 5, .max = 11 },
  338. .p = { .min = 14, .max = 42 },
  339. .p1 = { .min = 2, .max = 6 },
  340. .p2 = { .dot_limit = 0,
  341. .p2_slow = 7, .p2_fast = 7
  342. },
  343. };
  344. static const struct intel_limit intel_limits_pineview_sdvo = {
  345. .dot = { .min = 20000, .max = 400000},
  346. .vco = { .min = 1700000, .max = 3500000 },
  347. /* Pineview's Ncounter is a ring counter */
  348. .n = { .min = 3, .max = 6 },
  349. .m = { .min = 2, .max = 256 },
  350. /* Pineview only has one combined m divider, which we treat as m2. */
  351. .m1 = { .min = 0, .max = 0 },
  352. .m2 = { .min = 0, .max = 254 },
  353. .p = { .min = 5, .max = 80 },
  354. .p1 = { .min = 1, .max = 8 },
  355. .p2 = { .dot_limit = 200000,
  356. .p2_slow = 10, .p2_fast = 5 },
  357. };
  358. static const struct intel_limit intel_limits_pineview_lvds = {
  359. .dot = { .min = 20000, .max = 400000 },
  360. .vco = { .min = 1700000, .max = 3500000 },
  361. .n = { .min = 3, .max = 6 },
  362. .m = { .min = 2, .max = 256 },
  363. .m1 = { .min = 0, .max = 0 },
  364. .m2 = { .min = 0, .max = 254 },
  365. .p = { .min = 7, .max = 112 },
  366. .p1 = { .min = 1, .max = 8 },
  367. .p2 = { .dot_limit = 112000,
  368. .p2_slow = 14, .p2_fast = 14 },
  369. };
  370. /* Ironlake / Sandybridge
  371. *
  372. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  373. * the range value for them is (actual_value - 2).
  374. */
  375. static const struct intel_limit intel_limits_ironlake_dac = {
  376. .dot = { .min = 25000, .max = 350000 },
  377. .vco = { .min = 1760000, .max = 3510000 },
  378. .n = { .min = 1, .max = 5 },
  379. .m = { .min = 79, .max = 127 },
  380. .m1 = { .min = 12, .max = 22 },
  381. .m2 = { .min = 5, .max = 9 },
  382. .p = { .min = 5, .max = 80 },
  383. .p1 = { .min = 1, .max = 8 },
  384. .p2 = { .dot_limit = 225000,
  385. .p2_slow = 10, .p2_fast = 5 },
  386. };
  387. static const struct intel_limit intel_limits_ironlake_single_lvds = {
  388. .dot = { .min = 25000, .max = 350000 },
  389. .vco = { .min = 1760000, .max = 3510000 },
  390. .n = { .min = 1, .max = 3 },
  391. .m = { .min = 79, .max = 118 },
  392. .m1 = { .min = 12, .max = 22 },
  393. .m2 = { .min = 5, .max = 9 },
  394. .p = { .min = 28, .max = 112 },
  395. .p1 = { .min = 2, .max = 8 },
  396. .p2 = { .dot_limit = 225000,
  397. .p2_slow = 14, .p2_fast = 14 },
  398. };
  399. static const struct intel_limit intel_limits_ironlake_dual_lvds = {
  400. .dot = { .min = 25000, .max = 350000 },
  401. .vco = { .min = 1760000, .max = 3510000 },
  402. .n = { .min = 1, .max = 3 },
  403. .m = { .min = 79, .max = 127 },
  404. .m1 = { .min = 12, .max = 22 },
  405. .m2 = { .min = 5, .max = 9 },
  406. .p = { .min = 14, .max = 56 },
  407. .p1 = { .min = 2, .max = 8 },
  408. .p2 = { .dot_limit = 225000,
  409. .p2_slow = 7, .p2_fast = 7 },
  410. };
  411. /* LVDS 100mhz refclk limits. */
  412. static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
  413. .dot = { .min = 25000, .max = 350000 },
  414. .vco = { .min = 1760000, .max = 3510000 },
  415. .n = { .min = 1, .max = 2 },
  416. .m = { .min = 79, .max = 126 },
  417. .m1 = { .min = 12, .max = 22 },
  418. .m2 = { .min = 5, .max = 9 },
  419. .p = { .min = 28, .max = 112 },
  420. .p1 = { .min = 2, .max = 8 },
  421. .p2 = { .dot_limit = 225000,
  422. .p2_slow = 14, .p2_fast = 14 },
  423. };
  424. static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
  425. .dot = { .min = 25000, .max = 350000 },
  426. .vco = { .min = 1760000, .max = 3510000 },
  427. .n = { .min = 1, .max = 3 },
  428. .m = { .min = 79, .max = 126 },
  429. .m1 = { .min = 12, .max = 22 },
  430. .m2 = { .min = 5, .max = 9 },
  431. .p = { .min = 14, .max = 42 },
  432. .p1 = { .min = 2, .max = 6 },
  433. .p2 = { .dot_limit = 225000,
  434. .p2_slow = 7, .p2_fast = 7 },
  435. };
  436. static const struct intel_limit intel_limits_vlv = {
  437. /*
  438. * These are the data rate limits (measured in fast clocks)
  439. * since those are the strictest limits we have. The fast
  440. * clock and actual rate limits are more relaxed, so checking
  441. * them would make no difference.
  442. */
  443. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  444. .vco = { .min = 4000000, .max = 6000000 },
  445. .n = { .min = 1, .max = 7 },
  446. .m1 = { .min = 2, .max = 3 },
  447. .m2 = { .min = 11, .max = 156 },
  448. .p1 = { .min = 2, .max = 3 },
  449. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  450. };
  451. static const struct intel_limit intel_limits_chv = {
  452. /*
  453. * These are the data rate limits (measured in fast clocks)
  454. * since those are the strictest limits we have. The fast
  455. * clock and actual rate limits are more relaxed, so checking
  456. * them would make no difference.
  457. */
  458. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  459. .vco = { .min = 4800000, .max = 6480000 },
  460. .n = { .min = 1, .max = 1 },
  461. .m1 = { .min = 2, .max = 2 },
  462. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  463. .p1 = { .min = 2, .max = 4 },
  464. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  465. };
  466. static const struct intel_limit intel_limits_bxt = {
  467. /* FIXME: find real dot limits */
  468. .dot = { .min = 0, .max = INT_MAX },
  469. .vco = { .min = 4800000, .max = 6700000 },
  470. .n = { .min = 1, .max = 1 },
  471. .m1 = { .min = 2, .max = 2 },
  472. /* FIXME: find real m2 limits */
  473. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  474. .p1 = { .min = 2, .max = 4 },
  475. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  476. };
  477. static bool
  478. needs_modeset(struct drm_crtc_state *state)
  479. {
  480. return drm_atomic_crtc_needs_modeset(state);
  481. }
  482. /*
  483. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  484. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  485. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  486. * The helpers' return value is the rate of the clock that is fed to the
  487. * display engine's pipe which can be the above fast dot clock rate or a
  488. * divided-down version of it.
  489. */
  490. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  491. static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
  492. {
  493. clock->m = clock->m2 + 2;
  494. clock->p = clock->p1 * clock->p2;
  495. if (WARN_ON(clock->n == 0 || clock->p == 0))
  496. return 0;
  497. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  498. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  499. return clock->dot;
  500. }
  501. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  502. {
  503. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  504. }
  505. static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
  506. {
  507. clock->m = i9xx_dpll_compute_m(clock);
  508. clock->p = clock->p1 * clock->p2;
  509. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  510. return 0;
  511. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  512. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  513. return clock->dot;
  514. }
  515. static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
  516. {
  517. clock->m = clock->m1 * clock->m2;
  518. clock->p = clock->p1 * clock->p2;
  519. if (WARN_ON(clock->n == 0 || clock->p == 0))
  520. return 0;
  521. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  522. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  523. return clock->dot / 5;
  524. }
  525. int chv_calc_dpll_params(int refclk, struct dpll *clock)
  526. {
  527. clock->m = clock->m1 * clock->m2;
  528. clock->p = clock->p1 * clock->p2;
  529. if (WARN_ON(clock->n == 0 || clock->p == 0))
  530. return 0;
  531. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  532. clock->n << 22);
  533. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  534. return clock->dot / 5;
  535. }
  536. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  537. /**
  538. * Returns whether the given set of divisors are valid for a given refclk with
  539. * the given connectors.
  540. */
  541. static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
  542. const struct intel_limit *limit,
  543. const struct dpll *clock)
  544. {
  545. if (clock->n < limit->n.min || limit->n.max < clock->n)
  546. INTELPllInvalid("n out of range\n");
  547. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  548. INTELPllInvalid("p1 out of range\n");
  549. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  550. INTELPllInvalid("m2 out of range\n");
  551. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  552. INTELPllInvalid("m1 out of range\n");
  553. if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
  554. !IS_CHERRYVIEW(dev_priv) && !IS_BROXTON(dev_priv))
  555. if (clock->m1 <= clock->m2)
  556. INTELPllInvalid("m1 <= m2\n");
  557. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  558. !IS_BROXTON(dev_priv)) {
  559. if (clock->p < limit->p.min || limit->p.max < clock->p)
  560. INTELPllInvalid("p out of range\n");
  561. if (clock->m < limit->m.min || limit->m.max < clock->m)
  562. INTELPllInvalid("m out of range\n");
  563. }
  564. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  565. INTELPllInvalid("vco out of range\n");
  566. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  567. * connector, etc., rather than just a single range.
  568. */
  569. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  570. INTELPllInvalid("dot out of range\n");
  571. return true;
  572. }
  573. static int
  574. i9xx_select_p2_div(const struct intel_limit *limit,
  575. const struct intel_crtc_state *crtc_state,
  576. int target)
  577. {
  578. struct drm_device *dev = crtc_state->base.crtc->dev;
  579. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  580. /*
  581. * For LVDS just rely on its current settings for dual-channel.
  582. * We haven't figured out how to reliably set up different
  583. * single/dual channel state, if we even can.
  584. */
  585. if (intel_is_dual_link_lvds(dev))
  586. return limit->p2.p2_fast;
  587. else
  588. return limit->p2.p2_slow;
  589. } else {
  590. if (target < limit->p2.dot_limit)
  591. return limit->p2.p2_slow;
  592. else
  593. return limit->p2.p2_fast;
  594. }
  595. }
  596. /*
  597. * Returns a set of divisors for the desired target clock with the given
  598. * refclk, or FALSE. The returned values represent the clock equation:
  599. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  600. *
  601. * Target and reference clocks are specified in kHz.
  602. *
  603. * If match_clock is provided, then best_clock P divider must match the P
  604. * divider from @match_clock used for LVDS downclocking.
  605. */
  606. static bool
  607. i9xx_find_best_dpll(const struct intel_limit *limit,
  608. struct intel_crtc_state *crtc_state,
  609. int target, int refclk, struct dpll *match_clock,
  610. struct dpll *best_clock)
  611. {
  612. struct drm_device *dev = crtc_state->base.crtc->dev;
  613. struct dpll clock;
  614. int err = target;
  615. memset(best_clock, 0, sizeof(*best_clock));
  616. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  617. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  618. clock.m1++) {
  619. for (clock.m2 = limit->m2.min;
  620. clock.m2 <= limit->m2.max; clock.m2++) {
  621. if (clock.m2 >= clock.m1)
  622. break;
  623. for (clock.n = limit->n.min;
  624. clock.n <= limit->n.max; clock.n++) {
  625. for (clock.p1 = limit->p1.min;
  626. clock.p1 <= limit->p1.max; clock.p1++) {
  627. int this_err;
  628. i9xx_calc_dpll_params(refclk, &clock);
  629. if (!intel_PLL_is_valid(to_i915(dev),
  630. limit,
  631. &clock))
  632. continue;
  633. if (match_clock &&
  634. clock.p != match_clock->p)
  635. continue;
  636. this_err = abs(clock.dot - target);
  637. if (this_err < err) {
  638. *best_clock = clock;
  639. err = this_err;
  640. }
  641. }
  642. }
  643. }
  644. }
  645. return (err != target);
  646. }
  647. /*
  648. * Returns a set of divisors for the desired target clock with the given
  649. * refclk, or FALSE. The returned values represent the clock equation:
  650. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  651. *
  652. * Target and reference clocks are specified in kHz.
  653. *
  654. * If match_clock is provided, then best_clock P divider must match the P
  655. * divider from @match_clock used for LVDS downclocking.
  656. */
  657. static bool
  658. pnv_find_best_dpll(const struct intel_limit *limit,
  659. struct intel_crtc_state *crtc_state,
  660. int target, int refclk, struct dpll *match_clock,
  661. struct dpll *best_clock)
  662. {
  663. struct drm_device *dev = crtc_state->base.crtc->dev;
  664. struct dpll clock;
  665. int err = target;
  666. memset(best_clock, 0, sizeof(*best_clock));
  667. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  668. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  669. clock.m1++) {
  670. for (clock.m2 = limit->m2.min;
  671. clock.m2 <= limit->m2.max; clock.m2++) {
  672. for (clock.n = limit->n.min;
  673. clock.n <= limit->n.max; clock.n++) {
  674. for (clock.p1 = limit->p1.min;
  675. clock.p1 <= limit->p1.max; clock.p1++) {
  676. int this_err;
  677. pnv_calc_dpll_params(refclk, &clock);
  678. if (!intel_PLL_is_valid(to_i915(dev),
  679. limit,
  680. &clock))
  681. continue;
  682. if (match_clock &&
  683. clock.p != match_clock->p)
  684. continue;
  685. this_err = abs(clock.dot - target);
  686. if (this_err < err) {
  687. *best_clock = clock;
  688. err = this_err;
  689. }
  690. }
  691. }
  692. }
  693. }
  694. return (err != target);
  695. }
  696. /*
  697. * Returns a set of divisors for the desired target clock with the given
  698. * refclk, or FALSE. The returned values represent the clock equation:
  699. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  700. *
  701. * Target and reference clocks are specified in kHz.
  702. *
  703. * If match_clock is provided, then best_clock P divider must match the P
  704. * divider from @match_clock used for LVDS downclocking.
  705. */
  706. static bool
  707. g4x_find_best_dpll(const struct intel_limit *limit,
  708. struct intel_crtc_state *crtc_state,
  709. int target, int refclk, struct dpll *match_clock,
  710. struct dpll *best_clock)
  711. {
  712. struct drm_device *dev = crtc_state->base.crtc->dev;
  713. struct dpll clock;
  714. int max_n;
  715. bool found = false;
  716. /* approximately equals target * 0.00585 */
  717. int err_most = (target >> 8) + (target >> 9);
  718. memset(best_clock, 0, sizeof(*best_clock));
  719. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  720. max_n = limit->n.max;
  721. /* based on hardware requirement, prefer smaller n to precision */
  722. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  723. /* based on hardware requirement, prefere larger m1,m2 */
  724. for (clock.m1 = limit->m1.max;
  725. clock.m1 >= limit->m1.min; clock.m1--) {
  726. for (clock.m2 = limit->m2.max;
  727. clock.m2 >= limit->m2.min; clock.m2--) {
  728. for (clock.p1 = limit->p1.max;
  729. clock.p1 >= limit->p1.min; clock.p1--) {
  730. int this_err;
  731. i9xx_calc_dpll_params(refclk, &clock);
  732. if (!intel_PLL_is_valid(to_i915(dev),
  733. limit,
  734. &clock))
  735. continue;
  736. this_err = abs(clock.dot - target);
  737. if (this_err < err_most) {
  738. *best_clock = clock;
  739. err_most = this_err;
  740. max_n = clock.n;
  741. found = true;
  742. }
  743. }
  744. }
  745. }
  746. }
  747. return found;
  748. }
  749. /*
  750. * Check if the calculated PLL configuration is more optimal compared to the
  751. * best configuration and error found so far. Return the calculated error.
  752. */
  753. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  754. const struct dpll *calculated_clock,
  755. const struct dpll *best_clock,
  756. unsigned int best_error_ppm,
  757. unsigned int *error_ppm)
  758. {
  759. /*
  760. * For CHV ignore the error and consider only the P value.
  761. * Prefer a bigger P value based on HW requirements.
  762. */
  763. if (IS_CHERRYVIEW(to_i915(dev))) {
  764. *error_ppm = 0;
  765. return calculated_clock->p > best_clock->p;
  766. }
  767. if (WARN_ON_ONCE(!target_freq))
  768. return false;
  769. *error_ppm = div_u64(1000000ULL *
  770. abs(target_freq - calculated_clock->dot),
  771. target_freq);
  772. /*
  773. * Prefer a better P value over a better (smaller) error if the error
  774. * is small. Ensure this preference for future configurations too by
  775. * setting the error to 0.
  776. */
  777. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  778. *error_ppm = 0;
  779. return true;
  780. }
  781. return *error_ppm + 10 < best_error_ppm;
  782. }
  783. /*
  784. * Returns a set of divisors for the desired target clock with the given
  785. * refclk, or FALSE. The returned values represent the clock equation:
  786. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  787. */
  788. static bool
  789. vlv_find_best_dpll(const struct intel_limit *limit,
  790. struct intel_crtc_state *crtc_state,
  791. int target, int refclk, struct dpll *match_clock,
  792. struct dpll *best_clock)
  793. {
  794. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  795. struct drm_device *dev = crtc->base.dev;
  796. struct dpll clock;
  797. unsigned int bestppm = 1000000;
  798. /* min update 19.2 MHz */
  799. int max_n = min(limit->n.max, refclk / 19200);
  800. bool found = false;
  801. target *= 5; /* fast clock */
  802. memset(best_clock, 0, sizeof(*best_clock));
  803. /* based on hardware requirement, prefer smaller n to precision */
  804. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  805. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  806. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  807. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  808. clock.p = clock.p1 * clock.p2;
  809. /* based on hardware requirement, prefer bigger m1,m2 values */
  810. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  811. unsigned int ppm;
  812. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  813. refclk * clock.m1);
  814. vlv_calc_dpll_params(refclk, &clock);
  815. if (!intel_PLL_is_valid(to_i915(dev),
  816. limit,
  817. &clock))
  818. continue;
  819. if (!vlv_PLL_is_optimal(dev, target,
  820. &clock,
  821. best_clock,
  822. bestppm, &ppm))
  823. continue;
  824. *best_clock = clock;
  825. bestppm = ppm;
  826. found = true;
  827. }
  828. }
  829. }
  830. }
  831. return found;
  832. }
  833. /*
  834. * Returns a set of divisors for the desired target clock with the given
  835. * refclk, or FALSE. The returned values represent the clock equation:
  836. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  837. */
  838. static bool
  839. chv_find_best_dpll(const struct intel_limit *limit,
  840. struct intel_crtc_state *crtc_state,
  841. int target, int refclk, struct dpll *match_clock,
  842. struct dpll *best_clock)
  843. {
  844. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  845. struct drm_device *dev = crtc->base.dev;
  846. unsigned int best_error_ppm;
  847. struct dpll clock;
  848. uint64_t m2;
  849. int found = false;
  850. memset(best_clock, 0, sizeof(*best_clock));
  851. best_error_ppm = 1000000;
  852. /*
  853. * Based on hardware doc, the n always set to 1, and m1 always
  854. * set to 2. If requires to support 200Mhz refclk, we need to
  855. * revisit this because n may not 1 anymore.
  856. */
  857. clock.n = 1, clock.m1 = 2;
  858. target *= 5; /* fast clock */
  859. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  860. for (clock.p2 = limit->p2.p2_fast;
  861. clock.p2 >= limit->p2.p2_slow;
  862. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  863. unsigned int error_ppm;
  864. clock.p = clock.p1 * clock.p2;
  865. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  866. clock.n) << 22, refclk * clock.m1);
  867. if (m2 > INT_MAX/clock.m1)
  868. continue;
  869. clock.m2 = m2;
  870. chv_calc_dpll_params(refclk, &clock);
  871. if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
  872. continue;
  873. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  874. best_error_ppm, &error_ppm))
  875. continue;
  876. *best_clock = clock;
  877. best_error_ppm = error_ppm;
  878. found = true;
  879. }
  880. }
  881. return found;
  882. }
  883. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  884. struct dpll *best_clock)
  885. {
  886. int refclk = 100000;
  887. const struct intel_limit *limit = &intel_limits_bxt;
  888. return chv_find_best_dpll(limit, crtc_state,
  889. target_clock, refclk, NULL, best_clock);
  890. }
  891. bool intel_crtc_active(struct intel_crtc *crtc)
  892. {
  893. /* Be paranoid as we can arrive here with only partial
  894. * state retrieved from the hardware during setup.
  895. *
  896. * We can ditch the adjusted_mode.crtc_clock check as soon
  897. * as Haswell has gained clock readout/fastboot support.
  898. *
  899. * We can ditch the crtc->primary->fb check as soon as we can
  900. * properly reconstruct framebuffers.
  901. *
  902. * FIXME: The intel_crtc->active here should be switched to
  903. * crtc->state->active once we have proper CRTC states wired up
  904. * for atomic.
  905. */
  906. return crtc->active && crtc->base.primary->state->fb &&
  907. crtc->config->base.adjusted_mode.crtc_clock;
  908. }
  909. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  910. enum pipe pipe)
  911. {
  912. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  913. return crtc->config->cpu_transcoder;
  914. }
  915. static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
  916. {
  917. struct drm_i915_private *dev_priv = to_i915(dev);
  918. i915_reg_t reg = PIPEDSL(pipe);
  919. u32 line1, line2;
  920. u32 line_mask;
  921. if (IS_GEN2(dev_priv))
  922. line_mask = DSL_LINEMASK_GEN2;
  923. else
  924. line_mask = DSL_LINEMASK_GEN3;
  925. line1 = I915_READ(reg) & line_mask;
  926. msleep(5);
  927. line2 = I915_READ(reg) & line_mask;
  928. return line1 == line2;
  929. }
  930. /*
  931. * intel_wait_for_pipe_off - wait for pipe to turn off
  932. * @crtc: crtc whose pipe to wait for
  933. *
  934. * After disabling a pipe, we can't wait for vblank in the usual way,
  935. * spinning on the vblank interrupt status bit, since we won't actually
  936. * see an interrupt when the pipe is disabled.
  937. *
  938. * On Gen4 and above:
  939. * wait for the pipe register state bit to turn off
  940. *
  941. * Otherwise:
  942. * wait for the display line value to settle (it usually
  943. * ends up stopping at the start of the next frame).
  944. *
  945. */
  946. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  947. {
  948. struct drm_device *dev = crtc->base.dev;
  949. struct drm_i915_private *dev_priv = to_i915(dev);
  950. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  951. enum pipe pipe = crtc->pipe;
  952. if (INTEL_INFO(dev)->gen >= 4) {
  953. i915_reg_t reg = PIPECONF(cpu_transcoder);
  954. /* Wait for the Pipe State to go off */
  955. if (intel_wait_for_register(dev_priv,
  956. reg, I965_PIPECONF_ACTIVE, 0,
  957. 100))
  958. WARN(1, "pipe_off wait timed out\n");
  959. } else {
  960. /* Wait for the display line to settle */
  961. if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
  962. WARN(1, "pipe_off wait timed out\n");
  963. }
  964. }
  965. /* Only for pre-ILK configs */
  966. void assert_pll(struct drm_i915_private *dev_priv,
  967. enum pipe pipe, bool state)
  968. {
  969. u32 val;
  970. bool cur_state;
  971. val = I915_READ(DPLL(pipe));
  972. cur_state = !!(val & DPLL_VCO_ENABLE);
  973. I915_STATE_WARN(cur_state != state,
  974. "PLL state assertion failure (expected %s, current %s)\n",
  975. onoff(state), onoff(cur_state));
  976. }
  977. /* XXX: the dsi pll is shared between MIPI DSI ports */
  978. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  979. {
  980. u32 val;
  981. bool cur_state;
  982. mutex_lock(&dev_priv->sb_lock);
  983. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  984. mutex_unlock(&dev_priv->sb_lock);
  985. cur_state = val & DSI_PLL_VCO_EN;
  986. I915_STATE_WARN(cur_state != state,
  987. "DSI PLL state assertion failure (expected %s, current %s)\n",
  988. onoff(state), onoff(cur_state));
  989. }
  990. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  991. enum pipe pipe, bool state)
  992. {
  993. bool cur_state;
  994. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  995. pipe);
  996. if (HAS_DDI(dev_priv)) {
  997. /* DDI does not have a specific FDI_TX register */
  998. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  999. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  1000. } else {
  1001. u32 val = I915_READ(FDI_TX_CTL(pipe));
  1002. cur_state = !!(val & FDI_TX_ENABLE);
  1003. }
  1004. I915_STATE_WARN(cur_state != state,
  1005. "FDI TX state assertion failure (expected %s, current %s)\n",
  1006. onoff(state), onoff(cur_state));
  1007. }
  1008. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1009. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1010. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1011. enum pipe pipe, bool state)
  1012. {
  1013. u32 val;
  1014. bool cur_state;
  1015. val = I915_READ(FDI_RX_CTL(pipe));
  1016. cur_state = !!(val & FDI_RX_ENABLE);
  1017. I915_STATE_WARN(cur_state != state,
  1018. "FDI RX state assertion failure (expected %s, current %s)\n",
  1019. onoff(state), onoff(cur_state));
  1020. }
  1021. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1022. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1023. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1024. enum pipe pipe)
  1025. {
  1026. u32 val;
  1027. /* ILK FDI PLL is always enabled */
  1028. if (IS_GEN5(dev_priv))
  1029. return;
  1030. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1031. if (HAS_DDI(dev_priv))
  1032. return;
  1033. val = I915_READ(FDI_TX_CTL(pipe));
  1034. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1035. }
  1036. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1037. enum pipe pipe, bool state)
  1038. {
  1039. u32 val;
  1040. bool cur_state;
  1041. val = I915_READ(FDI_RX_CTL(pipe));
  1042. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1043. I915_STATE_WARN(cur_state != state,
  1044. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1045. onoff(state), onoff(cur_state));
  1046. }
  1047. void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
  1048. {
  1049. i915_reg_t pp_reg;
  1050. u32 val;
  1051. enum pipe panel_pipe = PIPE_A;
  1052. bool locked = true;
  1053. if (WARN_ON(HAS_DDI(dev_priv)))
  1054. return;
  1055. if (HAS_PCH_SPLIT(dev_priv)) {
  1056. u32 port_sel;
  1057. pp_reg = PP_CONTROL(0);
  1058. port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
  1059. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1060. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1061. panel_pipe = PIPE_B;
  1062. /* XXX: else fix for eDP */
  1063. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1064. /* presumably write lock depends on pipe, not port select */
  1065. pp_reg = PP_CONTROL(pipe);
  1066. panel_pipe = pipe;
  1067. } else {
  1068. pp_reg = PP_CONTROL(0);
  1069. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1070. panel_pipe = PIPE_B;
  1071. }
  1072. val = I915_READ(pp_reg);
  1073. if (!(val & PANEL_POWER_ON) ||
  1074. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1075. locked = false;
  1076. I915_STATE_WARN(panel_pipe == pipe && locked,
  1077. "panel assertion failure, pipe %c regs locked\n",
  1078. pipe_name(pipe));
  1079. }
  1080. static void assert_cursor(struct drm_i915_private *dev_priv,
  1081. enum pipe pipe, bool state)
  1082. {
  1083. bool cur_state;
  1084. if (IS_845G(dev_priv) || IS_I865G(dev_priv))
  1085. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1086. else
  1087. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1088. I915_STATE_WARN(cur_state != state,
  1089. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1090. pipe_name(pipe), onoff(state), onoff(cur_state));
  1091. }
  1092. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1093. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1094. void assert_pipe(struct drm_i915_private *dev_priv,
  1095. enum pipe pipe, bool state)
  1096. {
  1097. bool cur_state;
  1098. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1099. pipe);
  1100. enum intel_display_power_domain power_domain;
  1101. /* if we need the pipe quirk it must be always on */
  1102. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1103. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1104. state = true;
  1105. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1106. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1107. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1108. cur_state = !!(val & PIPECONF_ENABLE);
  1109. intel_display_power_put(dev_priv, power_domain);
  1110. } else {
  1111. cur_state = false;
  1112. }
  1113. I915_STATE_WARN(cur_state != state,
  1114. "pipe %c assertion failure (expected %s, current %s)\n",
  1115. pipe_name(pipe), onoff(state), onoff(cur_state));
  1116. }
  1117. static void assert_plane(struct drm_i915_private *dev_priv,
  1118. enum plane plane, bool state)
  1119. {
  1120. u32 val;
  1121. bool cur_state;
  1122. val = I915_READ(DSPCNTR(plane));
  1123. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1124. I915_STATE_WARN(cur_state != state,
  1125. "plane %c assertion failure (expected %s, current %s)\n",
  1126. plane_name(plane), onoff(state), onoff(cur_state));
  1127. }
  1128. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1129. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1130. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1131. enum pipe pipe)
  1132. {
  1133. struct drm_device *dev = &dev_priv->drm;
  1134. int i;
  1135. /* Primary planes are fixed to pipes on gen4+ */
  1136. if (INTEL_INFO(dev)->gen >= 4) {
  1137. u32 val = I915_READ(DSPCNTR(pipe));
  1138. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1139. "plane %c assertion failure, should be disabled but not\n",
  1140. plane_name(pipe));
  1141. return;
  1142. }
  1143. /* Need to check both planes against the pipe */
  1144. for_each_pipe(dev_priv, i) {
  1145. u32 val = I915_READ(DSPCNTR(i));
  1146. enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1147. DISPPLANE_SEL_PIPE_SHIFT;
  1148. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1149. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1150. plane_name(i), pipe_name(pipe));
  1151. }
  1152. }
  1153. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1154. enum pipe pipe)
  1155. {
  1156. struct drm_device *dev = &dev_priv->drm;
  1157. int sprite;
  1158. if (INTEL_INFO(dev)->gen >= 9) {
  1159. for_each_sprite(dev_priv, pipe, sprite) {
  1160. u32 val = I915_READ(PLANE_CTL(pipe, sprite));
  1161. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1162. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1163. sprite, pipe_name(pipe));
  1164. }
  1165. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1166. for_each_sprite(dev_priv, pipe, sprite) {
  1167. u32 val = I915_READ(SPCNTR(pipe, sprite));
  1168. I915_STATE_WARN(val & SP_ENABLE,
  1169. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1170. sprite_name(pipe, sprite), pipe_name(pipe));
  1171. }
  1172. } else if (INTEL_INFO(dev)->gen >= 7) {
  1173. u32 val = I915_READ(SPRCTL(pipe));
  1174. I915_STATE_WARN(val & SPRITE_ENABLE,
  1175. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1176. plane_name(pipe), pipe_name(pipe));
  1177. } else if (INTEL_INFO(dev)->gen >= 5) {
  1178. u32 val = I915_READ(DVSCNTR(pipe));
  1179. I915_STATE_WARN(val & DVS_ENABLE,
  1180. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1181. plane_name(pipe), pipe_name(pipe));
  1182. }
  1183. }
  1184. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1185. {
  1186. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1187. drm_crtc_vblank_put(crtc);
  1188. }
  1189. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1190. enum pipe pipe)
  1191. {
  1192. u32 val;
  1193. bool enabled;
  1194. val = I915_READ(PCH_TRANSCONF(pipe));
  1195. enabled = !!(val & TRANS_ENABLE);
  1196. I915_STATE_WARN(enabled,
  1197. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1198. pipe_name(pipe));
  1199. }
  1200. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1201. enum pipe pipe, u32 port_sel, u32 val)
  1202. {
  1203. if ((val & DP_PORT_EN) == 0)
  1204. return false;
  1205. if (HAS_PCH_CPT(dev_priv)) {
  1206. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1207. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1208. return false;
  1209. } else if (IS_CHERRYVIEW(dev_priv)) {
  1210. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1211. return false;
  1212. } else {
  1213. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1214. return false;
  1215. }
  1216. return true;
  1217. }
  1218. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1219. enum pipe pipe, u32 val)
  1220. {
  1221. if ((val & SDVO_ENABLE) == 0)
  1222. return false;
  1223. if (HAS_PCH_CPT(dev_priv)) {
  1224. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1225. return false;
  1226. } else if (IS_CHERRYVIEW(dev_priv)) {
  1227. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1228. return false;
  1229. } else {
  1230. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1231. return false;
  1232. }
  1233. return true;
  1234. }
  1235. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1236. enum pipe pipe, u32 val)
  1237. {
  1238. if ((val & LVDS_PORT_EN) == 0)
  1239. return false;
  1240. if (HAS_PCH_CPT(dev_priv)) {
  1241. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1242. return false;
  1243. } else {
  1244. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1245. return false;
  1246. }
  1247. return true;
  1248. }
  1249. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1250. enum pipe pipe, u32 val)
  1251. {
  1252. if ((val & ADPA_DAC_ENABLE) == 0)
  1253. return false;
  1254. if (HAS_PCH_CPT(dev_priv)) {
  1255. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1256. return false;
  1257. } else {
  1258. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1259. return false;
  1260. }
  1261. return true;
  1262. }
  1263. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1264. enum pipe pipe, i915_reg_t reg,
  1265. u32 port_sel)
  1266. {
  1267. u32 val = I915_READ(reg);
  1268. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1269. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1270. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1271. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
  1272. && (val & DP_PIPEB_SELECT),
  1273. "IBX PCH dp port still using transcoder B\n");
  1274. }
  1275. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1276. enum pipe pipe, i915_reg_t reg)
  1277. {
  1278. u32 val = I915_READ(reg);
  1279. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1280. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1281. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1282. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
  1283. && (val & SDVO_PIPE_B_SELECT),
  1284. "IBX PCH hdmi port still using transcoder B\n");
  1285. }
  1286. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1287. enum pipe pipe)
  1288. {
  1289. u32 val;
  1290. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1291. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1292. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1293. val = I915_READ(PCH_ADPA);
  1294. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1295. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1296. pipe_name(pipe));
  1297. val = I915_READ(PCH_LVDS);
  1298. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1299. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1300. pipe_name(pipe));
  1301. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1302. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1303. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1304. }
  1305. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1306. const struct intel_crtc_state *pipe_config)
  1307. {
  1308. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1309. enum pipe pipe = crtc->pipe;
  1310. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1311. POSTING_READ(DPLL(pipe));
  1312. udelay(150);
  1313. if (intel_wait_for_register(dev_priv,
  1314. DPLL(pipe),
  1315. DPLL_LOCK_VLV,
  1316. DPLL_LOCK_VLV,
  1317. 1))
  1318. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1319. }
  1320. static void vlv_enable_pll(struct intel_crtc *crtc,
  1321. const struct intel_crtc_state *pipe_config)
  1322. {
  1323. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1324. enum pipe pipe = crtc->pipe;
  1325. assert_pipe_disabled(dev_priv, pipe);
  1326. /* PLL is protected by panel, make sure we can write it */
  1327. assert_panel_unlocked(dev_priv, pipe);
  1328. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1329. _vlv_enable_pll(crtc, pipe_config);
  1330. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1331. POSTING_READ(DPLL_MD(pipe));
  1332. }
  1333. static void _chv_enable_pll(struct intel_crtc *crtc,
  1334. const struct intel_crtc_state *pipe_config)
  1335. {
  1336. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1337. enum pipe pipe = crtc->pipe;
  1338. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1339. u32 tmp;
  1340. mutex_lock(&dev_priv->sb_lock);
  1341. /* Enable back the 10bit clock to display controller */
  1342. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1343. tmp |= DPIO_DCLKP_EN;
  1344. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1345. mutex_unlock(&dev_priv->sb_lock);
  1346. /*
  1347. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1348. */
  1349. udelay(1);
  1350. /* Enable PLL */
  1351. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1352. /* Check PLL is locked */
  1353. if (intel_wait_for_register(dev_priv,
  1354. DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
  1355. 1))
  1356. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1357. }
  1358. static void chv_enable_pll(struct intel_crtc *crtc,
  1359. const struct intel_crtc_state *pipe_config)
  1360. {
  1361. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1362. enum pipe pipe = crtc->pipe;
  1363. assert_pipe_disabled(dev_priv, pipe);
  1364. /* PLL is protected by panel, make sure we can write it */
  1365. assert_panel_unlocked(dev_priv, pipe);
  1366. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1367. _chv_enable_pll(crtc, pipe_config);
  1368. if (pipe != PIPE_A) {
  1369. /*
  1370. * WaPixelRepeatModeFixForC0:chv
  1371. *
  1372. * DPLLCMD is AWOL. Use chicken bits to propagate
  1373. * the value from DPLLBMD to either pipe B or C.
  1374. */
  1375. I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
  1376. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1377. I915_WRITE(CBR4_VLV, 0);
  1378. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1379. /*
  1380. * DPLLB VGA mode also seems to cause problems.
  1381. * We should always have it disabled.
  1382. */
  1383. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1384. } else {
  1385. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1386. POSTING_READ(DPLL_MD(pipe));
  1387. }
  1388. }
  1389. static int intel_num_dvo_pipes(struct drm_device *dev)
  1390. {
  1391. struct intel_crtc *crtc;
  1392. int count = 0;
  1393. for_each_intel_crtc(dev, crtc) {
  1394. count += crtc->base.state->active &&
  1395. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
  1396. }
  1397. return count;
  1398. }
  1399. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1400. {
  1401. struct drm_device *dev = crtc->base.dev;
  1402. struct drm_i915_private *dev_priv = to_i915(dev);
  1403. i915_reg_t reg = DPLL(crtc->pipe);
  1404. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1405. assert_pipe_disabled(dev_priv, crtc->pipe);
  1406. /* PLL is protected by panel, make sure we can write it */
  1407. if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
  1408. assert_panel_unlocked(dev_priv, crtc->pipe);
  1409. /* Enable DVO 2x clock on both PLLs if necessary */
  1410. if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev) > 0) {
  1411. /*
  1412. * It appears to be important that we don't enable this
  1413. * for the current pipe before otherwise configuring the
  1414. * PLL. No idea how this should be handled if multiple
  1415. * DVO outputs are enabled simultaneosly.
  1416. */
  1417. dpll |= DPLL_DVO_2X_MODE;
  1418. I915_WRITE(DPLL(!crtc->pipe),
  1419. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1420. }
  1421. /*
  1422. * Apparently we need to have VGA mode enabled prior to changing
  1423. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1424. * dividers, even though the register value does change.
  1425. */
  1426. I915_WRITE(reg, 0);
  1427. I915_WRITE(reg, dpll);
  1428. /* Wait for the clocks to stabilize. */
  1429. POSTING_READ(reg);
  1430. udelay(150);
  1431. if (INTEL_INFO(dev)->gen >= 4) {
  1432. I915_WRITE(DPLL_MD(crtc->pipe),
  1433. crtc->config->dpll_hw_state.dpll_md);
  1434. } else {
  1435. /* The pixel multiplier can only be updated once the
  1436. * DPLL is enabled and the clocks are stable.
  1437. *
  1438. * So write it again.
  1439. */
  1440. I915_WRITE(reg, dpll);
  1441. }
  1442. /* We do this three times for luck */
  1443. I915_WRITE(reg, dpll);
  1444. POSTING_READ(reg);
  1445. udelay(150); /* wait for warmup */
  1446. I915_WRITE(reg, dpll);
  1447. POSTING_READ(reg);
  1448. udelay(150); /* wait for warmup */
  1449. I915_WRITE(reg, dpll);
  1450. POSTING_READ(reg);
  1451. udelay(150); /* wait for warmup */
  1452. }
  1453. /**
  1454. * i9xx_disable_pll - disable a PLL
  1455. * @dev_priv: i915 private structure
  1456. * @pipe: pipe PLL to disable
  1457. *
  1458. * Disable the PLL for @pipe, making sure the pipe is off first.
  1459. *
  1460. * Note! This is for pre-ILK only.
  1461. */
  1462. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1463. {
  1464. struct drm_device *dev = crtc->base.dev;
  1465. struct drm_i915_private *dev_priv = to_i915(dev);
  1466. enum pipe pipe = crtc->pipe;
  1467. /* Disable DVO 2x clock on both PLLs if necessary */
  1468. if (IS_I830(dev_priv) &&
  1469. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
  1470. !intel_num_dvo_pipes(dev)) {
  1471. I915_WRITE(DPLL(PIPE_B),
  1472. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1473. I915_WRITE(DPLL(PIPE_A),
  1474. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1475. }
  1476. /* Don't disable pipe or pipe PLLs if needed */
  1477. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1478. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1479. return;
  1480. /* Make sure the pipe isn't still relying on us */
  1481. assert_pipe_disabled(dev_priv, pipe);
  1482. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1483. POSTING_READ(DPLL(pipe));
  1484. }
  1485. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1486. {
  1487. u32 val;
  1488. /* Make sure the pipe isn't still relying on us */
  1489. assert_pipe_disabled(dev_priv, pipe);
  1490. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1491. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1492. if (pipe != PIPE_A)
  1493. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1494. I915_WRITE(DPLL(pipe), val);
  1495. POSTING_READ(DPLL(pipe));
  1496. }
  1497. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1498. {
  1499. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1500. u32 val;
  1501. /* Make sure the pipe isn't still relying on us */
  1502. assert_pipe_disabled(dev_priv, pipe);
  1503. val = DPLL_SSC_REF_CLK_CHV |
  1504. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1505. if (pipe != PIPE_A)
  1506. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1507. I915_WRITE(DPLL(pipe), val);
  1508. POSTING_READ(DPLL(pipe));
  1509. mutex_lock(&dev_priv->sb_lock);
  1510. /* Disable 10bit clock to display controller */
  1511. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1512. val &= ~DPIO_DCLKP_EN;
  1513. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1514. mutex_unlock(&dev_priv->sb_lock);
  1515. }
  1516. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1517. struct intel_digital_port *dport,
  1518. unsigned int expected_mask)
  1519. {
  1520. u32 port_mask;
  1521. i915_reg_t dpll_reg;
  1522. switch (dport->port) {
  1523. case PORT_B:
  1524. port_mask = DPLL_PORTB_READY_MASK;
  1525. dpll_reg = DPLL(0);
  1526. break;
  1527. case PORT_C:
  1528. port_mask = DPLL_PORTC_READY_MASK;
  1529. dpll_reg = DPLL(0);
  1530. expected_mask <<= 4;
  1531. break;
  1532. case PORT_D:
  1533. port_mask = DPLL_PORTD_READY_MASK;
  1534. dpll_reg = DPIO_PHY_STATUS;
  1535. break;
  1536. default:
  1537. BUG();
  1538. }
  1539. if (intel_wait_for_register(dev_priv,
  1540. dpll_reg, port_mask, expected_mask,
  1541. 1000))
  1542. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1543. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1544. }
  1545. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1546. enum pipe pipe)
  1547. {
  1548. struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
  1549. pipe);
  1550. i915_reg_t reg;
  1551. uint32_t val, pipeconf_val;
  1552. /* Make sure PCH DPLL is enabled */
  1553. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1554. /* FDI must be feeding us bits for PCH ports */
  1555. assert_fdi_tx_enabled(dev_priv, pipe);
  1556. assert_fdi_rx_enabled(dev_priv, pipe);
  1557. if (HAS_PCH_CPT(dev_priv)) {
  1558. /* Workaround: Set the timing override bit before enabling the
  1559. * pch transcoder. */
  1560. reg = TRANS_CHICKEN2(pipe);
  1561. val = I915_READ(reg);
  1562. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1563. I915_WRITE(reg, val);
  1564. }
  1565. reg = PCH_TRANSCONF(pipe);
  1566. val = I915_READ(reg);
  1567. pipeconf_val = I915_READ(PIPECONF(pipe));
  1568. if (HAS_PCH_IBX(dev_priv)) {
  1569. /*
  1570. * Make the BPC in transcoder be consistent with
  1571. * that in pipeconf reg. For HDMI we must use 8bpc
  1572. * here for both 8bpc and 12bpc.
  1573. */
  1574. val &= ~PIPECONF_BPC_MASK;
  1575. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
  1576. val |= PIPECONF_8BPC;
  1577. else
  1578. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1579. }
  1580. val &= ~TRANS_INTERLACE_MASK;
  1581. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1582. if (HAS_PCH_IBX(dev_priv) &&
  1583. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  1584. val |= TRANS_LEGACY_INTERLACED_ILK;
  1585. else
  1586. val |= TRANS_INTERLACED;
  1587. else
  1588. val |= TRANS_PROGRESSIVE;
  1589. I915_WRITE(reg, val | TRANS_ENABLE);
  1590. if (intel_wait_for_register(dev_priv,
  1591. reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
  1592. 100))
  1593. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1594. }
  1595. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1596. enum transcoder cpu_transcoder)
  1597. {
  1598. u32 val, pipeconf_val;
  1599. /* FDI must be feeding us bits for PCH ports */
  1600. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1601. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1602. /* Workaround: set timing override bit. */
  1603. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1604. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1605. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1606. val = TRANS_ENABLE;
  1607. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1608. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1609. PIPECONF_INTERLACED_ILK)
  1610. val |= TRANS_INTERLACED;
  1611. else
  1612. val |= TRANS_PROGRESSIVE;
  1613. I915_WRITE(LPT_TRANSCONF, val);
  1614. if (intel_wait_for_register(dev_priv,
  1615. LPT_TRANSCONF,
  1616. TRANS_STATE_ENABLE,
  1617. TRANS_STATE_ENABLE,
  1618. 100))
  1619. DRM_ERROR("Failed to enable PCH transcoder\n");
  1620. }
  1621. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1622. enum pipe pipe)
  1623. {
  1624. i915_reg_t reg;
  1625. uint32_t val;
  1626. /* FDI relies on the transcoder */
  1627. assert_fdi_tx_disabled(dev_priv, pipe);
  1628. assert_fdi_rx_disabled(dev_priv, pipe);
  1629. /* Ports must be off as well */
  1630. assert_pch_ports_disabled(dev_priv, pipe);
  1631. reg = PCH_TRANSCONF(pipe);
  1632. val = I915_READ(reg);
  1633. val &= ~TRANS_ENABLE;
  1634. I915_WRITE(reg, val);
  1635. /* wait for PCH transcoder off, transcoder state */
  1636. if (intel_wait_for_register(dev_priv,
  1637. reg, TRANS_STATE_ENABLE, 0,
  1638. 50))
  1639. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1640. if (HAS_PCH_CPT(dev_priv)) {
  1641. /* Workaround: Clear the timing override chicken bit again. */
  1642. reg = TRANS_CHICKEN2(pipe);
  1643. val = I915_READ(reg);
  1644. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1645. I915_WRITE(reg, val);
  1646. }
  1647. }
  1648. void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1649. {
  1650. u32 val;
  1651. val = I915_READ(LPT_TRANSCONF);
  1652. val &= ~TRANS_ENABLE;
  1653. I915_WRITE(LPT_TRANSCONF, val);
  1654. /* wait for PCH transcoder off, transcoder state */
  1655. if (intel_wait_for_register(dev_priv,
  1656. LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
  1657. 50))
  1658. DRM_ERROR("Failed to disable PCH transcoder\n");
  1659. /* Workaround: clear timing override bit. */
  1660. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1661. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1662. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1663. }
  1664. enum transcoder intel_crtc_pch_transcoder(struct intel_crtc *crtc)
  1665. {
  1666. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1667. WARN_ON(!crtc->config->has_pch_encoder);
  1668. if (HAS_PCH_LPT(dev_priv))
  1669. return TRANSCODER_A;
  1670. else
  1671. return (enum transcoder) crtc->pipe;
  1672. }
  1673. /**
  1674. * intel_enable_pipe - enable a pipe, asserting requirements
  1675. * @crtc: crtc responsible for the pipe
  1676. *
  1677. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1678. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1679. */
  1680. static void intel_enable_pipe(struct intel_crtc *crtc)
  1681. {
  1682. struct drm_device *dev = crtc->base.dev;
  1683. struct drm_i915_private *dev_priv = to_i915(dev);
  1684. enum pipe pipe = crtc->pipe;
  1685. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1686. i915_reg_t reg;
  1687. u32 val;
  1688. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1689. assert_planes_disabled(dev_priv, pipe);
  1690. assert_cursor_disabled(dev_priv, pipe);
  1691. assert_sprites_disabled(dev_priv, pipe);
  1692. /*
  1693. * A pipe without a PLL won't actually be able to drive bits from
  1694. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1695. * need the check.
  1696. */
  1697. if (HAS_GMCH_DISPLAY(dev_priv)) {
  1698. if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
  1699. assert_dsi_pll_enabled(dev_priv);
  1700. else
  1701. assert_pll_enabled(dev_priv, pipe);
  1702. } else {
  1703. if (crtc->config->has_pch_encoder) {
  1704. /* if driving the PCH, we need FDI enabled */
  1705. assert_fdi_rx_pll_enabled(dev_priv,
  1706. (enum pipe) intel_crtc_pch_transcoder(crtc));
  1707. assert_fdi_tx_pll_enabled(dev_priv,
  1708. (enum pipe) cpu_transcoder);
  1709. }
  1710. /* FIXME: assert CPU port conditions for SNB+ */
  1711. }
  1712. reg = PIPECONF(cpu_transcoder);
  1713. val = I915_READ(reg);
  1714. if (val & PIPECONF_ENABLE) {
  1715. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1716. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1717. return;
  1718. }
  1719. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1720. POSTING_READ(reg);
  1721. /*
  1722. * Until the pipe starts DSL will read as 0, which would cause
  1723. * an apparent vblank timestamp jump, which messes up also the
  1724. * frame count when it's derived from the timestamps. So let's
  1725. * wait for the pipe to start properly before we call
  1726. * drm_crtc_vblank_on()
  1727. */
  1728. if (dev->max_vblank_count == 0 &&
  1729. wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
  1730. DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
  1731. }
  1732. /**
  1733. * intel_disable_pipe - disable a pipe, asserting requirements
  1734. * @crtc: crtc whose pipes is to be disabled
  1735. *
  1736. * Disable the pipe of @crtc, making sure that various hardware
  1737. * specific requirements are met, if applicable, e.g. plane
  1738. * disabled, panel fitter off, etc.
  1739. *
  1740. * Will wait until the pipe has shut down before returning.
  1741. */
  1742. static void intel_disable_pipe(struct intel_crtc *crtc)
  1743. {
  1744. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1745. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1746. enum pipe pipe = crtc->pipe;
  1747. i915_reg_t reg;
  1748. u32 val;
  1749. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1750. /*
  1751. * Make sure planes won't keep trying to pump pixels to us,
  1752. * or we might hang the display.
  1753. */
  1754. assert_planes_disabled(dev_priv, pipe);
  1755. assert_cursor_disabled(dev_priv, pipe);
  1756. assert_sprites_disabled(dev_priv, pipe);
  1757. reg = PIPECONF(cpu_transcoder);
  1758. val = I915_READ(reg);
  1759. if ((val & PIPECONF_ENABLE) == 0)
  1760. return;
  1761. /*
  1762. * Double wide has implications for planes
  1763. * so best keep it disabled when not needed.
  1764. */
  1765. if (crtc->config->double_wide)
  1766. val &= ~PIPECONF_DOUBLE_WIDE;
  1767. /* Don't disable pipe or pipe PLLs if needed */
  1768. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1769. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1770. val &= ~PIPECONF_ENABLE;
  1771. I915_WRITE(reg, val);
  1772. if ((val & PIPECONF_ENABLE) == 0)
  1773. intel_wait_for_pipe_off(crtc);
  1774. }
  1775. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1776. {
  1777. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1778. }
  1779. static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
  1780. uint64_t fb_modifier, unsigned int cpp)
  1781. {
  1782. switch (fb_modifier) {
  1783. case DRM_FORMAT_MOD_NONE:
  1784. return cpp;
  1785. case I915_FORMAT_MOD_X_TILED:
  1786. if (IS_GEN2(dev_priv))
  1787. return 128;
  1788. else
  1789. return 512;
  1790. case I915_FORMAT_MOD_Y_TILED:
  1791. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1792. return 128;
  1793. else
  1794. return 512;
  1795. case I915_FORMAT_MOD_Yf_TILED:
  1796. switch (cpp) {
  1797. case 1:
  1798. return 64;
  1799. case 2:
  1800. case 4:
  1801. return 128;
  1802. case 8:
  1803. case 16:
  1804. return 256;
  1805. default:
  1806. MISSING_CASE(cpp);
  1807. return cpp;
  1808. }
  1809. break;
  1810. default:
  1811. MISSING_CASE(fb_modifier);
  1812. return cpp;
  1813. }
  1814. }
  1815. unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
  1816. uint64_t fb_modifier, unsigned int cpp)
  1817. {
  1818. if (fb_modifier == DRM_FORMAT_MOD_NONE)
  1819. return 1;
  1820. else
  1821. return intel_tile_size(dev_priv) /
  1822. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1823. }
  1824. /* Return the tile dimensions in pixel units */
  1825. static void intel_tile_dims(const struct drm_i915_private *dev_priv,
  1826. unsigned int *tile_width,
  1827. unsigned int *tile_height,
  1828. uint64_t fb_modifier,
  1829. unsigned int cpp)
  1830. {
  1831. unsigned int tile_width_bytes =
  1832. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1833. *tile_width = tile_width_bytes / cpp;
  1834. *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
  1835. }
  1836. unsigned int
  1837. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1838. uint32_t pixel_format, uint64_t fb_modifier)
  1839. {
  1840. unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
  1841. unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
  1842. return ALIGN(height, tile_height);
  1843. }
  1844. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1845. {
  1846. unsigned int size = 0;
  1847. int i;
  1848. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1849. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1850. return size;
  1851. }
  1852. static void
  1853. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1854. const struct drm_framebuffer *fb,
  1855. unsigned int rotation)
  1856. {
  1857. if (drm_rotation_90_or_270(rotation)) {
  1858. *view = i915_ggtt_view_rotated;
  1859. view->params.rotated = to_intel_framebuffer(fb)->rot_info;
  1860. } else {
  1861. *view = i915_ggtt_view_normal;
  1862. }
  1863. }
  1864. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1865. {
  1866. if (INTEL_INFO(dev_priv)->gen >= 9)
  1867. return 256 * 1024;
  1868. else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
  1869. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1870. return 128 * 1024;
  1871. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1872. return 4 * 1024;
  1873. else
  1874. return 0;
  1875. }
  1876. static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
  1877. uint64_t fb_modifier)
  1878. {
  1879. switch (fb_modifier) {
  1880. case DRM_FORMAT_MOD_NONE:
  1881. return intel_linear_alignment(dev_priv);
  1882. case I915_FORMAT_MOD_X_TILED:
  1883. if (INTEL_INFO(dev_priv)->gen >= 9)
  1884. return 256 * 1024;
  1885. return 0;
  1886. case I915_FORMAT_MOD_Y_TILED:
  1887. case I915_FORMAT_MOD_Yf_TILED:
  1888. return 1 * 1024 * 1024;
  1889. default:
  1890. MISSING_CASE(fb_modifier);
  1891. return 0;
  1892. }
  1893. }
  1894. struct i915_vma *
  1895. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1896. {
  1897. struct drm_device *dev = fb->dev;
  1898. struct drm_i915_private *dev_priv = to_i915(dev);
  1899. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1900. struct i915_ggtt_view view;
  1901. struct i915_vma *vma;
  1902. u32 alignment;
  1903. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1904. alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
  1905. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1906. /* Note that the w/a also requires 64 PTE of padding following the
  1907. * bo. We currently fill all unused PTE with the shadow page and so
  1908. * we should always have valid PTE following the scanout preventing
  1909. * the VT-d warning.
  1910. */
  1911. if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
  1912. alignment = 256 * 1024;
  1913. /*
  1914. * Global gtt pte registers are special registers which actually forward
  1915. * writes to a chunk of system memory. Which means that there is no risk
  1916. * that the register values disappear as soon as we call
  1917. * intel_runtime_pm_put(), so it is correct to wrap only the
  1918. * pin/unpin/fence and not more.
  1919. */
  1920. intel_runtime_pm_get(dev_priv);
  1921. vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
  1922. if (IS_ERR(vma))
  1923. goto err;
  1924. if (i915_vma_is_map_and_fenceable(vma)) {
  1925. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1926. * fence, whereas 965+ only requires a fence if using
  1927. * framebuffer compression. For simplicity, we always, when
  1928. * possible, install a fence as the cost is not that onerous.
  1929. *
  1930. * If we fail to fence the tiled scanout, then either the
  1931. * modeset will reject the change (which is highly unlikely as
  1932. * the affected systems, all but one, do not have unmappable
  1933. * space) or we will not be able to enable full powersaving
  1934. * techniques (also likely not to apply due to various limits
  1935. * FBC and the like impose on the size of the buffer, which
  1936. * presumably we violated anyway with this unmappable buffer).
  1937. * Anyway, it is presumably better to stumble onwards with
  1938. * something and try to run the system in a "less than optimal"
  1939. * mode that matches the user configuration.
  1940. */
  1941. if (i915_vma_get_fence(vma) == 0)
  1942. i915_vma_pin_fence(vma);
  1943. }
  1944. err:
  1945. intel_runtime_pm_put(dev_priv);
  1946. return vma;
  1947. }
  1948. void intel_unpin_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1949. {
  1950. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1951. struct i915_ggtt_view view;
  1952. struct i915_vma *vma;
  1953. WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
  1954. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1955. vma = i915_gem_object_to_ggtt(obj, &view);
  1956. i915_vma_unpin_fence(vma);
  1957. i915_gem_object_unpin_from_display_plane(vma);
  1958. }
  1959. static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
  1960. unsigned int rotation)
  1961. {
  1962. if (drm_rotation_90_or_270(rotation))
  1963. return to_intel_framebuffer(fb)->rotated[plane].pitch;
  1964. else
  1965. return fb->pitches[plane];
  1966. }
  1967. /*
  1968. * Convert the x/y offsets into a linear offset.
  1969. * Only valid with 0/180 degree rotation, which is fine since linear
  1970. * offset is only used with linear buffers on pre-hsw and tiled buffers
  1971. * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
  1972. */
  1973. u32 intel_fb_xy_to_linear(int x, int y,
  1974. const struct intel_plane_state *state,
  1975. int plane)
  1976. {
  1977. const struct drm_framebuffer *fb = state->base.fb;
  1978. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  1979. unsigned int pitch = fb->pitches[plane];
  1980. return y * pitch + x * cpp;
  1981. }
  1982. /*
  1983. * Add the x/y offsets derived from fb->offsets[] to the user
  1984. * specified plane src x/y offsets. The resulting x/y offsets
  1985. * specify the start of scanout from the beginning of the gtt mapping.
  1986. */
  1987. void intel_add_fb_offsets(int *x, int *y,
  1988. const struct intel_plane_state *state,
  1989. int plane)
  1990. {
  1991. const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
  1992. unsigned int rotation = state->base.rotation;
  1993. if (drm_rotation_90_or_270(rotation)) {
  1994. *x += intel_fb->rotated[plane].x;
  1995. *y += intel_fb->rotated[plane].y;
  1996. } else {
  1997. *x += intel_fb->normal[plane].x;
  1998. *y += intel_fb->normal[plane].y;
  1999. }
  2000. }
  2001. /*
  2002. * Input tile dimensions and pitch must already be
  2003. * rotated to match x and y, and in pixel units.
  2004. */
  2005. static u32 _intel_adjust_tile_offset(int *x, int *y,
  2006. unsigned int tile_width,
  2007. unsigned int tile_height,
  2008. unsigned int tile_size,
  2009. unsigned int pitch_tiles,
  2010. u32 old_offset,
  2011. u32 new_offset)
  2012. {
  2013. unsigned int pitch_pixels = pitch_tiles * tile_width;
  2014. unsigned int tiles;
  2015. WARN_ON(old_offset & (tile_size - 1));
  2016. WARN_ON(new_offset & (tile_size - 1));
  2017. WARN_ON(new_offset > old_offset);
  2018. tiles = (old_offset - new_offset) / tile_size;
  2019. *y += tiles / pitch_tiles * tile_height;
  2020. *x += tiles % pitch_tiles * tile_width;
  2021. /* minimize x in case it got needlessly big */
  2022. *y += *x / pitch_pixels * tile_height;
  2023. *x %= pitch_pixels;
  2024. return new_offset;
  2025. }
  2026. /*
  2027. * Adjust the tile offset by moving the difference into
  2028. * the x/y offsets.
  2029. */
  2030. static u32 intel_adjust_tile_offset(int *x, int *y,
  2031. const struct intel_plane_state *state, int plane,
  2032. u32 old_offset, u32 new_offset)
  2033. {
  2034. const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
  2035. const struct drm_framebuffer *fb = state->base.fb;
  2036. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2037. unsigned int rotation = state->base.rotation;
  2038. unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
  2039. WARN_ON(new_offset > old_offset);
  2040. if (fb->modifier[plane] != DRM_FORMAT_MOD_NONE) {
  2041. unsigned int tile_size, tile_width, tile_height;
  2042. unsigned int pitch_tiles;
  2043. tile_size = intel_tile_size(dev_priv);
  2044. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2045. fb->modifier[plane], cpp);
  2046. if (drm_rotation_90_or_270(rotation)) {
  2047. pitch_tiles = pitch / tile_height;
  2048. swap(tile_width, tile_height);
  2049. } else {
  2050. pitch_tiles = pitch / (tile_width * cpp);
  2051. }
  2052. _intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2053. tile_size, pitch_tiles,
  2054. old_offset, new_offset);
  2055. } else {
  2056. old_offset += *y * pitch + *x * cpp;
  2057. *y = (old_offset - new_offset) / pitch;
  2058. *x = ((old_offset - new_offset) - *y * pitch) / cpp;
  2059. }
  2060. return new_offset;
  2061. }
  2062. /*
  2063. * Computes the linear offset to the base tile and adjusts
  2064. * x, y. bytes per pixel is assumed to be a power-of-two.
  2065. *
  2066. * In the 90/270 rotated case, x and y are assumed
  2067. * to be already rotated to match the rotated GTT view, and
  2068. * pitch is the tile_height aligned framebuffer height.
  2069. *
  2070. * This function is used when computing the derived information
  2071. * under intel_framebuffer, so using any of that information
  2072. * here is not allowed. Anything under drm_framebuffer can be
  2073. * used. This is why the user has to pass in the pitch since it
  2074. * is specified in the rotated orientation.
  2075. */
  2076. static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
  2077. int *x, int *y,
  2078. const struct drm_framebuffer *fb, int plane,
  2079. unsigned int pitch,
  2080. unsigned int rotation,
  2081. u32 alignment)
  2082. {
  2083. uint64_t fb_modifier = fb->modifier[plane];
  2084. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2085. u32 offset, offset_aligned;
  2086. if (alignment)
  2087. alignment--;
  2088. if (fb_modifier != DRM_FORMAT_MOD_NONE) {
  2089. unsigned int tile_size, tile_width, tile_height;
  2090. unsigned int tile_rows, tiles, pitch_tiles;
  2091. tile_size = intel_tile_size(dev_priv);
  2092. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2093. fb_modifier, cpp);
  2094. if (drm_rotation_90_or_270(rotation)) {
  2095. pitch_tiles = pitch / tile_height;
  2096. swap(tile_width, tile_height);
  2097. } else {
  2098. pitch_tiles = pitch / (tile_width * cpp);
  2099. }
  2100. tile_rows = *y / tile_height;
  2101. *y %= tile_height;
  2102. tiles = *x / tile_width;
  2103. *x %= tile_width;
  2104. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  2105. offset_aligned = offset & ~alignment;
  2106. _intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2107. tile_size, pitch_tiles,
  2108. offset, offset_aligned);
  2109. } else {
  2110. offset = *y * pitch + *x * cpp;
  2111. offset_aligned = offset & ~alignment;
  2112. *y = (offset & alignment) / pitch;
  2113. *x = ((offset & alignment) - *y * pitch) / cpp;
  2114. }
  2115. return offset_aligned;
  2116. }
  2117. u32 intel_compute_tile_offset(int *x, int *y,
  2118. const struct intel_plane_state *state,
  2119. int plane)
  2120. {
  2121. const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
  2122. const struct drm_framebuffer *fb = state->base.fb;
  2123. unsigned int rotation = state->base.rotation;
  2124. int pitch = intel_fb_pitch(fb, plane, rotation);
  2125. u32 alignment;
  2126. /* AUX_DIST needs only 4K alignment */
  2127. if (fb->pixel_format == DRM_FORMAT_NV12 && plane == 1)
  2128. alignment = 4096;
  2129. else
  2130. alignment = intel_surf_alignment(dev_priv, fb->modifier[plane]);
  2131. return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
  2132. rotation, alignment);
  2133. }
  2134. /* Convert the fb->offset[] linear offset into x/y offsets */
  2135. static void intel_fb_offset_to_xy(int *x, int *y,
  2136. const struct drm_framebuffer *fb, int plane)
  2137. {
  2138. unsigned int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2139. unsigned int pitch = fb->pitches[plane];
  2140. u32 linear_offset = fb->offsets[plane];
  2141. *y = linear_offset / pitch;
  2142. *x = linear_offset % pitch / cpp;
  2143. }
  2144. static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
  2145. {
  2146. switch (fb_modifier) {
  2147. case I915_FORMAT_MOD_X_TILED:
  2148. return I915_TILING_X;
  2149. case I915_FORMAT_MOD_Y_TILED:
  2150. return I915_TILING_Y;
  2151. default:
  2152. return I915_TILING_NONE;
  2153. }
  2154. }
  2155. static int
  2156. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  2157. struct drm_framebuffer *fb)
  2158. {
  2159. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  2160. struct intel_rotation_info *rot_info = &intel_fb->rot_info;
  2161. u32 gtt_offset_rotated = 0;
  2162. unsigned int max_size = 0;
  2163. uint32_t format = fb->pixel_format;
  2164. int i, num_planes = drm_format_num_planes(format);
  2165. unsigned int tile_size = intel_tile_size(dev_priv);
  2166. for (i = 0; i < num_planes; i++) {
  2167. unsigned int width, height;
  2168. unsigned int cpp, size;
  2169. u32 offset;
  2170. int x, y;
  2171. cpp = drm_format_plane_cpp(format, i);
  2172. width = drm_format_plane_width(fb->width, format, i);
  2173. height = drm_format_plane_height(fb->height, format, i);
  2174. intel_fb_offset_to_xy(&x, &y, fb, i);
  2175. /*
  2176. * The fence (if used) is aligned to the start of the object
  2177. * so having the framebuffer wrap around across the edge of the
  2178. * fenced region doesn't really work. We have no API to configure
  2179. * the fence start offset within the object (nor could we probably
  2180. * on gen2/3). So it's just easier if we just require that the
  2181. * fb layout agrees with the fence layout. We already check that the
  2182. * fb stride matches the fence stride elsewhere.
  2183. */
  2184. if (i915_gem_object_is_tiled(intel_fb->obj) &&
  2185. (x + width) * cpp > fb->pitches[i]) {
  2186. DRM_DEBUG("bad fb plane %d offset: 0x%x\n",
  2187. i, fb->offsets[i]);
  2188. return -EINVAL;
  2189. }
  2190. /*
  2191. * First pixel of the framebuffer from
  2192. * the start of the normal gtt mapping.
  2193. */
  2194. intel_fb->normal[i].x = x;
  2195. intel_fb->normal[i].y = y;
  2196. offset = _intel_compute_tile_offset(dev_priv, &x, &y,
  2197. fb, 0, fb->pitches[i],
  2198. DRM_ROTATE_0, tile_size);
  2199. offset /= tile_size;
  2200. if (fb->modifier[i] != DRM_FORMAT_MOD_NONE) {
  2201. unsigned int tile_width, tile_height;
  2202. unsigned int pitch_tiles;
  2203. struct drm_rect r;
  2204. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2205. fb->modifier[i], cpp);
  2206. rot_info->plane[i].offset = offset;
  2207. rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
  2208. rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
  2209. rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
  2210. intel_fb->rotated[i].pitch =
  2211. rot_info->plane[i].height * tile_height;
  2212. /* how many tiles does this plane need */
  2213. size = rot_info->plane[i].stride * rot_info->plane[i].height;
  2214. /*
  2215. * If the plane isn't horizontally tile aligned,
  2216. * we need one more tile.
  2217. */
  2218. if (x != 0)
  2219. size++;
  2220. /* rotate the x/y offsets to match the GTT view */
  2221. r.x1 = x;
  2222. r.y1 = y;
  2223. r.x2 = x + width;
  2224. r.y2 = y + height;
  2225. drm_rect_rotate(&r,
  2226. rot_info->plane[i].width * tile_width,
  2227. rot_info->plane[i].height * tile_height,
  2228. DRM_ROTATE_270);
  2229. x = r.x1;
  2230. y = r.y1;
  2231. /* rotate the tile dimensions to match the GTT view */
  2232. pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
  2233. swap(tile_width, tile_height);
  2234. /*
  2235. * We only keep the x/y offsets, so push all of the
  2236. * gtt offset into the x/y offsets.
  2237. */
  2238. _intel_adjust_tile_offset(&x, &y, tile_size,
  2239. tile_width, tile_height, pitch_tiles,
  2240. gtt_offset_rotated * tile_size, 0);
  2241. gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
  2242. /*
  2243. * First pixel of the framebuffer from
  2244. * the start of the rotated gtt mapping.
  2245. */
  2246. intel_fb->rotated[i].x = x;
  2247. intel_fb->rotated[i].y = y;
  2248. } else {
  2249. size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
  2250. x * cpp, tile_size);
  2251. }
  2252. /* how many tiles in total needed in the bo */
  2253. max_size = max(max_size, offset + size);
  2254. }
  2255. if (max_size * tile_size > to_intel_framebuffer(fb)->obj->base.size) {
  2256. DRM_DEBUG("fb too big for bo (need %u bytes, have %zu bytes)\n",
  2257. max_size * tile_size, to_intel_framebuffer(fb)->obj->base.size);
  2258. return -EINVAL;
  2259. }
  2260. return 0;
  2261. }
  2262. static int i9xx_format_to_fourcc(int format)
  2263. {
  2264. switch (format) {
  2265. case DISPPLANE_8BPP:
  2266. return DRM_FORMAT_C8;
  2267. case DISPPLANE_BGRX555:
  2268. return DRM_FORMAT_XRGB1555;
  2269. case DISPPLANE_BGRX565:
  2270. return DRM_FORMAT_RGB565;
  2271. default:
  2272. case DISPPLANE_BGRX888:
  2273. return DRM_FORMAT_XRGB8888;
  2274. case DISPPLANE_RGBX888:
  2275. return DRM_FORMAT_XBGR8888;
  2276. case DISPPLANE_BGRX101010:
  2277. return DRM_FORMAT_XRGB2101010;
  2278. case DISPPLANE_RGBX101010:
  2279. return DRM_FORMAT_XBGR2101010;
  2280. }
  2281. }
  2282. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2283. {
  2284. switch (format) {
  2285. case PLANE_CTL_FORMAT_RGB_565:
  2286. return DRM_FORMAT_RGB565;
  2287. default:
  2288. case PLANE_CTL_FORMAT_XRGB_8888:
  2289. if (rgb_order) {
  2290. if (alpha)
  2291. return DRM_FORMAT_ABGR8888;
  2292. else
  2293. return DRM_FORMAT_XBGR8888;
  2294. } else {
  2295. if (alpha)
  2296. return DRM_FORMAT_ARGB8888;
  2297. else
  2298. return DRM_FORMAT_XRGB8888;
  2299. }
  2300. case PLANE_CTL_FORMAT_XRGB_2101010:
  2301. if (rgb_order)
  2302. return DRM_FORMAT_XBGR2101010;
  2303. else
  2304. return DRM_FORMAT_XRGB2101010;
  2305. }
  2306. }
  2307. static bool
  2308. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2309. struct intel_initial_plane_config *plane_config)
  2310. {
  2311. struct drm_device *dev = crtc->base.dev;
  2312. struct drm_i915_private *dev_priv = to_i915(dev);
  2313. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2314. struct drm_i915_gem_object *obj = NULL;
  2315. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2316. struct drm_framebuffer *fb = &plane_config->fb->base;
  2317. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2318. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2319. PAGE_SIZE);
  2320. size_aligned -= base_aligned;
  2321. if (plane_config->size == 0)
  2322. return false;
  2323. /* If the FB is too big, just don't use it since fbdev is not very
  2324. * important and we should probably use that space with FBC or other
  2325. * features. */
  2326. if (size_aligned * 2 > ggtt->stolen_usable_size)
  2327. return false;
  2328. mutex_lock(&dev->struct_mutex);
  2329. obj = i915_gem_object_create_stolen_for_preallocated(dev,
  2330. base_aligned,
  2331. base_aligned,
  2332. size_aligned);
  2333. if (!obj) {
  2334. mutex_unlock(&dev->struct_mutex);
  2335. return false;
  2336. }
  2337. if (plane_config->tiling == I915_TILING_X)
  2338. obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
  2339. mode_cmd.pixel_format = fb->pixel_format;
  2340. mode_cmd.width = fb->width;
  2341. mode_cmd.height = fb->height;
  2342. mode_cmd.pitches[0] = fb->pitches[0];
  2343. mode_cmd.modifier[0] = fb->modifier[0];
  2344. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2345. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2346. &mode_cmd, obj)) {
  2347. DRM_DEBUG_KMS("intel fb init failed\n");
  2348. goto out_unref_obj;
  2349. }
  2350. mutex_unlock(&dev->struct_mutex);
  2351. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2352. return true;
  2353. out_unref_obj:
  2354. i915_gem_object_put(obj);
  2355. mutex_unlock(&dev->struct_mutex);
  2356. return false;
  2357. }
  2358. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2359. static void
  2360. update_state_fb(struct drm_plane *plane)
  2361. {
  2362. if (plane->fb == plane->state->fb)
  2363. return;
  2364. if (plane->state->fb)
  2365. drm_framebuffer_unreference(plane->state->fb);
  2366. plane->state->fb = plane->fb;
  2367. if (plane->state->fb)
  2368. drm_framebuffer_reference(plane->state->fb);
  2369. }
  2370. static void
  2371. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2372. struct intel_initial_plane_config *plane_config)
  2373. {
  2374. struct drm_device *dev = intel_crtc->base.dev;
  2375. struct drm_i915_private *dev_priv = to_i915(dev);
  2376. struct drm_crtc *c;
  2377. struct intel_crtc *i;
  2378. struct drm_i915_gem_object *obj;
  2379. struct drm_plane *primary = intel_crtc->base.primary;
  2380. struct drm_plane_state *plane_state = primary->state;
  2381. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2382. struct intel_plane *intel_plane = to_intel_plane(primary);
  2383. struct intel_plane_state *intel_state =
  2384. to_intel_plane_state(plane_state);
  2385. struct drm_framebuffer *fb;
  2386. if (!plane_config->fb)
  2387. return;
  2388. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2389. fb = &plane_config->fb->base;
  2390. goto valid_fb;
  2391. }
  2392. kfree(plane_config->fb);
  2393. /*
  2394. * Failed to alloc the obj, check to see if we should share
  2395. * an fb with another CRTC instead
  2396. */
  2397. for_each_crtc(dev, c) {
  2398. i = to_intel_crtc(c);
  2399. if (c == &intel_crtc->base)
  2400. continue;
  2401. if (!i->active)
  2402. continue;
  2403. fb = c->primary->fb;
  2404. if (!fb)
  2405. continue;
  2406. obj = intel_fb_obj(fb);
  2407. if (i915_gem_object_ggtt_offset(obj, NULL) == plane_config->base) {
  2408. drm_framebuffer_reference(fb);
  2409. goto valid_fb;
  2410. }
  2411. }
  2412. /*
  2413. * We've failed to reconstruct the BIOS FB. Current display state
  2414. * indicates that the primary plane is visible, but has a NULL FB,
  2415. * which will lead to problems later if we don't fix it up. The
  2416. * simplest solution is to just disable the primary plane now and
  2417. * pretend the BIOS never had it enabled.
  2418. */
  2419. to_intel_plane_state(plane_state)->base.visible = false;
  2420. crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
  2421. intel_pre_disable_primary_noatomic(&intel_crtc->base);
  2422. intel_plane->disable_plane(primary, &intel_crtc->base);
  2423. return;
  2424. valid_fb:
  2425. plane_state->src_x = 0;
  2426. plane_state->src_y = 0;
  2427. plane_state->src_w = fb->width << 16;
  2428. plane_state->src_h = fb->height << 16;
  2429. plane_state->crtc_x = 0;
  2430. plane_state->crtc_y = 0;
  2431. plane_state->crtc_w = fb->width;
  2432. plane_state->crtc_h = fb->height;
  2433. intel_state->base.src.x1 = plane_state->src_x;
  2434. intel_state->base.src.y1 = plane_state->src_y;
  2435. intel_state->base.src.x2 = plane_state->src_x + plane_state->src_w;
  2436. intel_state->base.src.y2 = plane_state->src_y + plane_state->src_h;
  2437. intel_state->base.dst.x1 = plane_state->crtc_x;
  2438. intel_state->base.dst.y1 = plane_state->crtc_y;
  2439. intel_state->base.dst.x2 = plane_state->crtc_x + plane_state->crtc_w;
  2440. intel_state->base.dst.y2 = plane_state->crtc_y + plane_state->crtc_h;
  2441. obj = intel_fb_obj(fb);
  2442. if (i915_gem_object_is_tiled(obj))
  2443. dev_priv->preserve_bios_swizzle = true;
  2444. drm_framebuffer_reference(fb);
  2445. primary->fb = primary->state->fb = fb;
  2446. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2447. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2448. atomic_or(to_intel_plane(primary)->frontbuffer_bit,
  2449. &obj->frontbuffer_bits);
  2450. }
  2451. static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
  2452. unsigned int rotation)
  2453. {
  2454. int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2455. switch (fb->modifier[plane]) {
  2456. case DRM_FORMAT_MOD_NONE:
  2457. case I915_FORMAT_MOD_X_TILED:
  2458. switch (cpp) {
  2459. case 8:
  2460. return 4096;
  2461. case 4:
  2462. case 2:
  2463. case 1:
  2464. return 8192;
  2465. default:
  2466. MISSING_CASE(cpp);
  2467. break;
  2468. }
  2469. break;
  2470. case I915_FORMAT_MOD_Y_TILED:
  2471. case I915_FORMAT_MOD_Yf_TILED:
  2472. switch (cpp) {
  2473. case 8:
  2474. return 2048;
  2475. case 4:
  2476. return 4096;
  2477. case 2:
  2478. case 1:
  2479. return 8192;
  2480. default:
  2481. MISSING_CASE(cpp);
  2482. break;
  2483. }
  2484. break;
  2485. default:
  2486. MISSING_CASE(fb->modifier[plane]);
  2487. }
  2488. return 2048;
  2489. }
  2490. static int skl_check_main_surface(struct intel_plane_state *plane_state)
  2491. {
  2492. const struct drm_i915_private *dev_priv = to_i915(plane_state->base.plane->dev);
  2493. const struct drm_framebuffer *fb = plane_state->base.fb;
  2494. unsigned int rotation = plane_state->base.rotation;
  2495. int x = plane_state->base.src.x1 >> 16;
  2496. int y = plane_state->base.src.y1 >> 16;
  2497. int w = drm_rect_width(&plane_state->base.src) >> 16;
  2498. int h = drm_rect_height(&plane_state->base.src) >> 16;
  2499. int max_width = skl_max_plane_width(fb, 0, rotation);
  2500. int max_height = 4096;
  2501. u32 alignment, offset, aux_offset = plane_state->aux.offset;
  2502. if (w > max_width || h > max_height) {
  2503. DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
  2504. w, h, max_width, max_height);
  2505. return -EINVAL;
  2506. }
  2507. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2508. offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
  2509. alignment = intel_surf_alignment(dev_priv, fb->modifier[0]);
  2510. /*
  2511. * AUX surface offset is specified as the distance from the
  2512. * main surface offset, and it must be non-negative. Make
  2513. * sure that is what we will get.
  2514. */
  2515. if (offset > aux_offset)
  2516. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2517. offset, aux_offset & ~(alignment - 1));
  2518. /*
  2519. * When using an X-tiled surface, the plane blows up
  2520. * if the x offset + width exceed the stride.
  2521. *
  2522. * TODO: linear and Y-tiled seem fine, Yf untested,
  2523. */
  2524. if (fb->modifier[0] == I915_FORMAT_MOD_X_TILED) {
  2525. int cpp = drm_format_plane_cpp(fb->pixel_format, 0);
  2526. while ((x + w) * cpp > fb->pitches[0]) {
  2527. if (offset == 0) {
  2528. DRM_DEBUG_KMS("Unable to find suitable display surface offset\n");
  2529. return -EINVAL;
  2530. }
  2531. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2532. offset, offset - alignment);
  2533. }
  2534. }
  2535. plane_state->main.offset = offset;
  2536. plane_state->main.x = x;
  2537. plane_state->main.y = y;
  2538. return 0;
  2539. }
  2540. static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
  2541. {
  2542. const struct drm_framebuffer *fb = plane_state->base.fb;
  2543. unsigned int rotation = plane_state->base.rotation;
  2544. int max_width = skl_max_plane_width(fb, 1, rotation);
  2545. int max_height = 4096;
  2546. int x = plane_state->base.src.x1 >> 17;
  2547. int y = plane_state->base.src.y1 >> 17;
  2548. int w = drm_rect_width(&plane_state->base.src) >> 17;
  2549. int h = drm_rect_height(&plane_state->base.src) >> 17;
  2550. u32 offset;
  2551. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2552. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2553. /* FIXME not quite sure how/if these apply to the chroma plane */
  2554. if (w > max_width || h > max_height) {
  2555. DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
  2556. w, h, max_width, max_height);
  2557. return -EINVAL;
  2558. }
  2559. plane_state->aux.offset = offset;
  2560. plane_state->aux.x = x;
  2561. plane_state->aux.y = y;
  2562. return 0;
  2563. }
  2564. int skl_check_plane_surface(struct intel_plane_state *plane_state)
  2565. {
  2566. const struct drm_framebuffer *fb = plane_state->base.fb;
  2567. unsigned int rotation = plane_state->base.rotation;
  2568. int ret;
  2569. /* Rotate src coordinates to match rotated GTT view */
  2570. if (drm_rotation_90_or_270(rotation))
  2571. drm_rect_rotate(&plane_state->base.src,
  2572. fb->width << 16, fb->height << 16,
  2573. DRM_ROTATE_270);
  2574. /*
  2575. * Handle the AUX surface first since
  2576. * the main surface setup depends on it.
  2577. */
  2578. if (fb->pixel_format == DRM_FORMAT_NV12) {
  2579. ret = skl_check_nv12_aux_surface(plane_state);
  2580. if (ret)
  2581. return ret;
  2582. } else {
  2583. plane_state->aux.offset = ~0xfff;
  2584. plane_state->aux.x = 0;
  2585. plane_state->aux.y = 0;
  2586. }
  2587. ret = skl_check_main_surface(plane_state);
  2588. if (ret)
  2589. return ret;
  2590. return 0;
  2591. }
  2592. static void i9xx_update_primary_plane(struct drm_plane *primary,
  2593. const struct intel_crtc_state *crtc_state,
  2594. const struct intel_plane_state *plane_state)
  2595. {
  2596. struct drm_device *dev = primary->dev;
  2597. struct drm_i915_private *dev_priv = to_i915(dev);
  2598. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2599. struct drm_framebuffer *fb = plane_state->base.fb;
  2600. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2601. int plane = intel_crtc->plane;
  2602. u32 linear_offset;
  2603. u32 dspcntr;
  2604. i915_reg_t reg = DSPCNTR(plane);
  2605. unsigned int rotation = plane_state->base.rotation;
  2606. int x = plane_state->base.src.x1 >> 16;
  2607. int y = plane_state->base.src.y1 >> 16;
  2608. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2609. dspcntr |= DISPLAY_PLANE_ENABLE;
  2610. if (INTEL_INFO(dev)->gen < 4) {
  2611. if (intel_crtc->pipe == PIPE_B)
  2612. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2613. /* pipesrc and dspsize control the size that is scaled from,
  2614. * which should always be the user's requested size.
  2615. */
  2616. I915_WRITE(DSPSIZE(plane),
  2617. ((crtc_state->pipe_src_h - 1) << 16) |
  2618. (crtc_state->pipe_src_w - 1));
  2619. I915_WRITE(DSPPOS(plane), 0);
  2620. } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
  2621. I915_WRITE(PRIMSIZE(plane),
  2622. ((crtc_state->pipe_src_h - 1) << 16) |
  2623. (crtc_state->pipe_src_w - 1));
  2624. I915_WRITE(PRIMPOS(plane), 0);
  2625. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2626. }
  2627. switch (fb->pixel_format) {
  2628. case DRM_FORMAT_C8:
  2629. dspcntr |= DISPPLANE_8BPP;
  2630. break;
  2631. case DRM_FORMAT_XRGB1555:
  2632. dspcntr |= DISPPLANE_BGRX555;
  2633. break;
  2634. case DRM_FORMAT_RGB565:
  2635. dspcntr |= DISPPLANE_BGRX565;
  2636. break;
  2637. case DRM_FORMAT_XRGB8888:
  2638. dspcntr |= DISPPLANE_BGRX888;
  2639. break;
  2640. case DRM_FORMAT_XBGR8888:
  2641. dspcntr |= DISPPLANE_RGBX888;
  2642. break;
  2643. case DRM_FORMAT_XRGB2101010:
  2644. dspcntr |= DISPPLANE_BGRX101010;
  2645. break;
  2646. case DRM_FORMAT_XBGR2101010:
  2647. dspcntr |= DISPPLANE_RGBX101010;
  2648. break;
  2649. default:
  2650. BUG();
  2651. }
  2652. if (INTEL_GEN(dev_priv) >= 4 &&
  2653. fb->modifier[0] == I915_FORMAT_MOD_X_TILED)
  2654. dspcntr |= DISPPLANE_TILED;
  2655. if (IS_G4X(dev_priv))
  2656. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2657. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2658. if (INTEL_INFO(dev)->gen >= 4)
  2659. intel_crtc->dspaddr_offset =
  2660. intel_compute_tile_offset(&x, &y, plane_state, 0);
  2661. if (rotation == DRM_ROTATE_180) {
  2662. dspcntr |= DISPPLANE_ROTATE_180;
  2663. x += (crtc_state->pipe_src_w - 1);
  2664. y += (crtc_state->pipe_src_h - 1);
  2665. }
  2666. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2667. if (INTEL_INFO(dev)->gen < 4)
  2668. intel_crtc->dspaddr_offset = linear_offset;
  2669. intel_crtc->adjusted_x = x;
  2670. intel_crtc->adjusted_y = y;
  2671. I915_WRITE(reg, dspcntr);
  2672. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2673. if (INTEL_INFO(dev)->gen >= 4) {
  2674. I915_WRITE(DSPSURF(plane),
  2675. intel_fb_gtt_offset(fb, rotation) +
  2676. intel_crtc->dspaddr_offset);
  2677. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2678. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2679. } else
  2680. I915_WRITE(DSPADDR(plane), i915_gem_object_ggtt_offset(obj, NULL) + linear_offset);
  2681. POSTING_READ(reg);
  2682. }
  2683. static void i9xx_disable_primary_plane(struct drm_plane *primary,
  2684. struct drm_crtc *crtc)
  2685. {
  2686. struct drm_device *dev = crtc->dev;
  2687. struct drm_i915_private *dev_priv = to_i915(dev);
  2688. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2689. int plane = intel_crtc->plane;
  2690. I915_WRITE(DSPCNTR(plane), 0);
  2691. if (INTEL_INFO(dev_priv)->gen >= 4)
  2692. I915_WRITE(DSPSURF(plane), 0);
  2693. else
  2694. I915_WRITE(DSPADDR(plane), 0);
  2695. POSTING_READ(DSPCNTR(plane));
  2696. }
  2697. static void ironlake_update_primary_plane(struct drm_plane *primary,
  2698. const struct intel_crtc_state *crtc_state,
  2699. const struct intel_plane_state *plane_state)
  2700. {
  2701. struct drm_device *dev = primary->dev;
  2702. struct drm_i915_private *dev_priv = to_i915(dev);
  2703. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2704. struct drm_framebuffer *fb = plane_state->base.fb;
  2705. int plane = intel_crtc->plane;
  2706. u32 linear_offset;
  2707. u32 dspcntr;
  2708. i915_reg_t reg = DSPCNTR(plane);
  2709. unsigned int rotation = plane_state->base.rotation;
  2710. int x = plane_state->base.src.x1 >> 16;
  2711. int y = plane_state->base.src.y1 >> 16;
  2712. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2713. dspcntr |= DISPLAY_PLANE_ENABLE;
  2714. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  2715. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2716. switch (fb->pixel_format) {
  2717. case DRM_FORMAT_C8:
  2718. dspcntr |= DISPPLANE_8BPP;
  2719. break;
  2720. case DRM_FORMAT_RGB565:
  2721. dspcntr |= DISPPLANE_BGRX565;
  2722. break;
  2723. case DRM_FORMAT_XRGB8888:
  2724. dspcntr |= DISPPLANE_BGRX888;
  2725. break;
  2726. case DRM_FORMAT_XBGR8888:
  2727. dspcntr |= DISPPLANE_RGBX888;
  2728. break;
  2729. case DRM_FORMAT_XRGB2101010:
  2730. dspcntr |= DISPPLANE_BGRX101010;
  2731. break;
  2732. case DRM_FORMAT_XBGR2101010:
  2733. dspcntr |= DISPPLANE_RGBX101010;
  2734. break;
  2735. default:
  2736. BUG();
  2737. }
  2738. if (fb->modifier[0] == I915_FORMAT_MOD_X_TILED)
  2739. dspcntr |= DISPPLANE_TILED;
  2740. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv))
  2741. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2742. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2743. intel_crtc->dspaddr_offset =
  2744. intel_compute_tile_offset(&x, &y, plane_state, 0);
  2745. if (rotation == DRM_ROTATE_180) {
  2746. dspcntr |= DISPPLANE_ROTATE_180;
  2747. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv)) {
  2748. x += (crtc_state->pipe_src_w - 1);
  2749. y += (crtc_state->pipe_src_h - 1);
  2750. }
  2751. }
  2752. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2753. intel_crtc->adjusted_x = x;
  2754. intel_crtc->adjusted_y = y;
  2755. I915_WRITE(reg, dspcntr);
  2756. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2757. I915_WRITE(DSPSURF(plane),
  2758. intel_fb_gtt_offset(fb, rotation) +
  2759. intel_crtc->dspaddr_offset);
  2760. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  2761. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2762. } else {
  2763. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2764. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2765. }
  2766. POSTING_READ(reg);
  2767. }
  2768. u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
  2769. uint64_t fb_modifier, uint32_t pixel_format)
  2770. {
  2771. if (fb_modifier == DRM_FORMAT_MOD_NONE) {
  2772. return 64;
  2773. } else {
  2774. int cpp = drm_format_plane_cpp(pixel_format, 0);
  2775. return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  2776. }
  2777. }
  2778. u32 intel_fb_gtt_offset(struct drm_framebuffer *fb,
  2779. unsigned int rotation)
  2780. {
  2781. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  2782. struct i915_ggtt_view view;
  2783. struct i915_vma *vma;
  2784. intel_fill_fb_ggtt_view(&view, fb, rotation);
  2785. vma = i915_gem_object_to_ggtt(obj, &view);
  2786. if (WARN(!vma, "ggtt vma for display object not found! (view=%u)\n",
  2787. view.type))
  2788. return -1;
  2789. return i915_ggtt_offset(vma);
  2790. }
  2791. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2792. {
  2793. struct drm_device *dev = intel_crtc->base.dev;
  2794. struct drm_i915_private *dev_priv = to_i915(dev);
  2795. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2796. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2797. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2798. }
  2799. /*
  2800. * This function detaches (aka. unbinds) unused scalers in hardware
  2801. */
  2802. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2803. {
  2804. struct intel_crtc_scaler_state *scaler_state;
  2805. int i;
  2806. scaler_state = &intel_crtc->config->scaler_state;
  2807. /* loop through and disable scalers that aren't in use */
  2808. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2809. if (!scaler_state->scalers[i].in_use)
  2810. skl_detach_scaler(intel_crtc, i);
  2811. }
  2812. }
  2813. u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
  2814. unsigned int rotation)
  2815. {
  2816. const struct drm_i915_private *dev_priv = to_i915(fb->dev);
  2817. u32 stride = intel_fb_pitch(fb, plane, rotation);
  2818. /*
  2819. * The stride is either expressed as a multiple of 64 bytes chunks for
  2820. * linear buffers or in number of tiles for tiled buffers.
  2821. */
  2822. if (drm_rotation_90_or_270(rotation)) {
  2823. int cpp = drm_format_plane_cpp(fb->pixel_format, plane);
  2824. stride /= intel_tile_height(dev_priv, fb->modifier[0], cpp);
  2825. } else {
  2826. stride /= intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  2827. fb->pixel_format);
  2828. }
  2829. return stride;
  2830. }
  2831. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2832. {
  2833. switch (pixel_format) {
  2834. case DRM_FORMAT_C8:
  2835. return PLANE_CTL_FORMAT_INDEXED;
  2836. case DRM_FORMAT_RGB565:
  2837. return PLANE_CTL_FORMAT_RGB_565;
  2838. case DRM_FORMAT_XBGR8888:
  2839. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2840. case DRM_FORMAT_XRGB8888:
  2841. return PLANE_CTL_FORMAT_XRGB_8888;
  2842. /*
  2843. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2844. * to be already pre-multiplied. We need to add a knob (or a different
  2845. * DRM_FORMAT) for user-space to configure that.
  2846. */
  2847. case DRM_FORMAT_ABGR8888:
  2848. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2849. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2850. case DRM_FORMAT_ARGB8888:
  2851. return PLANE_CTL_FORMAT_XRGB_8888 |
  2852. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2853. case DRM_FORMAT_XRGB2101010:
  2854. return PLANE_CTL_FORMAT_XRGB_2101010;
  2855. case DRM_FORMAT_XBGR2101010:
  2856. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2857. case DRM_FORMAT_YUYV:
  2858. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2859. case DRM_FORMAT_YVYU:
  2860. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2861. case DRM_FORMAT_UYVY:
  2862. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2863. case DRM_FORMAT_VYUY:
  2864. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2865. default:
  2866. MISSING_CASE(pixel_format);
  2867. }
  2868. return 0;
  2869. }
  2870. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2871. {
  2872. switch (fb_modifier) {
  2873. case DRM_FORMAT_MOD_NONE:
  2874. break;
  2875. case I915_FORMAT_MOD_X_TILED:
  2876. return PLANE_CTL_TILED_X;
  2877. case I915_FORMAT_MOD_Y_TILED:
  2878. return PLANE_CTL_TILED_Y;
  2879. case I915_FORMAT_MOD_Yf_TILED:
  2880. return PLANE_CTL_TILED_YF;
  2881. default:
  2882. MISSING_CASE(fb_modifier);
  2883. }
  2884. return 0;
  2885. }
  2886. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2887. {
  2888. switch (rotation) {
  2889. case DRM_ROTATE_0:
  2890. break;
  2891. /*
  2892. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2893. * while i915 HW rotation is clockwise, thats why this swapping.
  2894. */
  2895. case DRM_ROTATE_90:
  2896. return PLANE_CTL_ROTATE_270;
  2897. case DRM_ROTATE_180:
  2898. return PLANE_CTL_ROTATE_180;
  2899. case DRM_ROTATE_270:
  2900. return PLANE_CTL_ROTATE_90;
  2901. default:
  2902. MISSING_CASE(rotation);
  2903. }
  2904. return 0;
  2905. }
  2906. static void skylake_update_primary_plane(struct drm_plane *plane,
  2907. const struct intel_crtc_state *crtc_state,
  2908. const struct intel_plane_state *plane_state)
  2909. {
  2910. struct drm_device *dev = plane->dev;
  2911. struct drm_i915_private *dev_priv = to_i915(dev);
  2912. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2913. struct drm_framebuffer *fb = plane_state->base.fb;
  2914. const struct skl_wm_values *wm = &dev_priv->wm.skl_results;
  2915. const struct skl_plane_wm *p_wm =
  2916. &crtc_state->wm.skl.optimal.planes[0];
  2917. int pipe = intel_crtc->pipe;
  2918. u32 plane_ctl;
  2919. unsigned int rotation = plane_state->base.rotation;
  2920. u32 stride = skl_plane_stride(fb, 0, rotation);
  2921. u32 surf_addr = plane_state->main.offset;
  2922. int scaler_id = plane_state->scaler_id;
  2923. int src_x = plane_state->main.x;
  2924. int src_y = plane_state->main.y;
  2925. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  2926. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  2927. int dst_x = plane_state->base.dst.x1;
  2928. int dst_y = plane_state->base.dst.y1;
  2929. int dst_w = drm_rect_width(&plane_state->base.dst);
  2930. int dst_h = drm_rect_height(&plane_state->base.dst);
  2931. plane_ctl = PLANE_CTL_ENABLE |
  2932. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2933. PLANE_CTL_PIPE_CSC_ENABLE;
  2934. plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
  2935. plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
  2936. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2937. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2938. /* Sizes are 0 based */
  2939. src_w--;
  2940. src_h--;
  2941. dst_w--;
  2942. dst_h--;
  2943. intel_crtc->dspaddr_offset = surf_addr;
  2944. intel_crtc->adjusted_x = src_x;
  2945. intel_crtc->adjusted_y = src_y;
  2946. if (wm->dirty_pipes & drm_crtc_mask(&intel_crtc->base))
  2947. skl_write_plane_wm(intel_crtc, p_wm, &wm->ddb, 0);
  2948. I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
  2949. I915_WRITE(PLANE_OFFSET(pipe, 0), (src_y << 16) | src_x);
  2950. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  2951. I915_WRITE(PLANE_SIZE(pipe, 0), (src_h << 16) | src_w);
  2952. if (scaler_id >= 0) {
  2953. uint32_t ps_ctrl = 0;
  2954. WARN_ON(!dst_w || !dst_h);
  2955. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
  2956. crtc_state->scaler_state.scalers[scaler_id].mode;
  2957. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2958. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2959. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2960. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2961. I915_WRITE(PLANE_POS(pipe, 0), 0);
  2962. } else {
  2963. I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
  2964. }
  2965. I915_WRITE(PLANE_SURF(pipe, 0),
  2966. intel_fb_gtt_offset(fb, rotation) + surf_addr);
  2967. POSTING_READ(PLANE_SURF(pipe, 0));
  2968. }
  2969. static void skylake_disable_primary_plane(struct drm_plane *primary,
  2970. struct drm_crtc *crtc)
  2971. {
  2972. struct drm_device *dev = crtc->dev;
  2973. struct drm_i915_private *dev_priv = to_i915(dev);
  2974. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2975. struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
  2976. const struct skl_plane_wm *p_wm = &cstate->wm.skl.optimal.planes[0];
  2977. int pipe = intel_crtc->pipe;
  2978. /*
  2979. * We only populate skl_results on watermark updates, and if the
  2980. * plane's visiblity isn't actually changing neither is its watermarks.
  2981. */
  2982. if (!crtc->primary->state->visible)
  2983. skl_write_plane_wm(intel_crtc, p_wm,
  2984. &dev_priv->wm.skl_results.ddb, 0);
  2985. I915_WRITE(PLANE_CTL(pipe, 0), 0);
  2986. I915_WRITE(PLANE_SURF(pipe, 0), 0);
  2987. POSTING_READ(PLANE_SURF(pipe, 0));
  2988. }
  2989. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2990. static int
  2991. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2992. int x, int y, enum mode_set_atomic state)
  2993. {
  2994. /* Support for kgdboc is disabled, this needs a major rework. */
  2995. DRM_ERROR("legacy panic handler not supported any more.\n");
  2996. return -ENODEV;
  2997. }
  2998. static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
  2999. {
  3000. struct intel_crtc *crtc;
  3001. for_each_intel_crtc(&dev_priv->drm, crtc)
  3002. intel_finish_page_flip_cs(dev_priv, crtc->pipe);
  3003. }
  3004. static void intel_update_primary_planes(struct drm_device *dev)
  3005. {
  3006. struct drm_crtc *crtc;
  3007. for_each_crtc(dev, crtc) {
  3008. struct intel_plane *plane = to_intel_plane(crtc->primary);
  3009. struct intel_plane_state *plane_state =
  3010. to_intel_plane_state(plane->base.state);
  3011. if (plane_state->base.visible)
  3012. plane->update_plane(&plane->base,
  3013. to_intel_crtc_state(crtc->state),
  3014. plane_state);
  3015. }
  3016. }
  3017. static int
  3018. __intel_display_resume(struct drm_device *dev,
  3019. struct drm_atomic_state *state)
  3020. {
  3021. struct drm_crtc_state *crtc_state;
  3022. struct drm_crtc *crtc;
  3023. int i, ret;
  3024. intel_modeset_setup_hw_state(dev);
  3025. i915_redisable_vga(dev);
  3026. if (!state)
  3027. return 0;
  3028. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  3029. /*
  3030. * Force recalculation even if we restore
  3031. * current state. With fast modeset this may not result
  3032. * in a modeset when the state is compatible.
  3033. */
  3034. crtc_state->mode_changed = true;
  3035. }
  3036. /* ignore any reset values/BIOS leftovers in the WM registers */
  3037. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  3038. ret = drm_atomic_commit(state);
  3039. WARN_ON(ret == -EDEADLK);
  3040. return ret;
  3041. }
  3042. static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
  3043. {
  3044. return intel_has_gpu_reset(dev_priv) &&
  3045. INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
  3046. }
  3047. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  3048. {
  3049. struct drm_device *dev = &dev_priv->drm;
  3050. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3051. struct drm_atomic_state *state;
  3052. int ret;
  3053. /*
  3054. * Need mode_config.mutex so that we don't
  3055. * trample ongoing ->detect() and whatnot.
  3056. */
  3057. mutex_lock(&dev->mode_config.mutex);
  3058. drm_modeset_acquire_init(ctx, 0);
  3059. while (1) {
  3060. ret = drm_modeset_lock_all_ctx(dev, ctx);
  3061. if (ret != -EDEADLK)
  3062. break;
  3063. drm_modeset_backoff(ctx);
  3064. }
  3065. /* reset doesn't touch the display, but flips might get nuked anyway, */
  3066. if (!i915.force_reset_modeset_test &&
  3067. !gpu_reset_clobbers_display(dev_priv))
  3068. return;
  3069. /*
  3070. * Disabling the crtcs gracefully seems nicer. Also the
  3071. * g33 docs say we should at least disable all the planes.
  3072. */
  3073. state = drm_atomic_helper_duplicate_state(dev, ctx);
  3074. if (IS_ERR(state)) {
  3075. ret = PTR_ERR(state);
  3076. state = NULL;
  3077. DRM_ERROR("Duplicating state failed with %i\n", ret);
  3078. goto err;
  3079. }
  3080. ret = drm_atomic_helper_disable_all(dev, ctx);
  3081. if (ret) {
  3082. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  3083. goto err;
  3084. }
  3085. dev_priv->modeset_restore_state = state;
  3086. state->acquire_ctx = ctx;
  3087. return;
  3088. err:
  3089. drm_atomic_state_put(state);
  3090. }
  3091. void intel_finish_reset(struct drm_i915_private *dev_priv)
  3092. {
  3093. struct drm_device *dev = &dev_priv->drm;
  3094. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3095. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  3096. int ret;
  3097. /*
  3098. * Flips in the rings will be nuked by the reset,
  3099. * so complete all pending flips so that user space
  3100. * will get its events and not get stuck.
  3101. */
  3102. intel_complete_page_flips(dev_priv);
  3103. dev_priv->modeset_restore_state = NULL;
  3104. /* reset doesn't touch the display */
  3105. if (!gpu_reset_clobbers_display(dev_priv)) {
  3106. if (!state) {
  3107. /*
  3108. * Flips in the rings have been nuked by the reset,
  3109. * so update the base address of all primary
  3110. * planes to the the last fb to make sure we're
  3111. * showing the correct fb after a reset.
  3112. *
  3113. * FIXME: Atomic will make this obsolete since we won't schedule
  3114. * CS-based flips (which might get lost in gpu resets) any more.
  3115. */
  3116. intel_update_primary_planes(dev);
  3117. } else {
  3118. ret = __intel_display_resume(dev, state);
  3119. if (ret)
  3120. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3121. }
  3122. } else {
  3123. /*
  3124. * The display has been reset as well,
  3125. * so need a full re-initialization.
  3126. */
  3127. intel_runtime_pm_disable_interrupts(dev_priv);
  3128. intel_runtime_pm_enable_interrupts(dev_priv);
  3129. intel_pps_unlock_regs_wa(dev_priv);
  3130. intel_modeset_init_hw(dev);
  3131. spin_lock_irq(&dev_priv->irq_lock);
  3132. if (dev_priv->display.hpd_irq_setup)
  3133. dev_priv->display.hpd_irq_setup(dev_priv);
  3134. spin_unlock_irq(&dev_priv->irq_lock);
  3135. ret = __intel_display_resume(dev, state);
  3136. if (ret)
  3137. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3138. intel_hpd_init(dev_priv);
  3139. }
  3140. if (state)
  3141. drm_atomic_state_put(state);
  3142. drm_modeset_drop_locks(ctx);
  3143. drm_modeset_acquire_fini(ctx);
  3144. mutex_unlock(&dev->mode_config.mutex);
  3145. }
  3146. static bool abort_flip_on_reset(struct intel_crtc *crtc)
  3147. {
  3148. struct i915_gpu_error *error = &to_i915(crtc->base.dev)->gpu_error;
  3149. if (i915_reset_in_progress(error))
  3150. return true;
  3151. if (crtc->reset_count != i915_reset_count(error))
  3152. return true;
  3153. return false;
  3154. }
  3155. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  3156. {
  3157. struct drm_device *dev = crtc->dev;
  3158. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3159. bool pending;
  3160. if (abort_flip_on_reset(intel_crtc))
  3161. return false;
  3162. spin_lock_irq(&dev->event_lock);
  3163. pending = to_intel_crtc(crtc)->flip_work != NULL;
  3164. spin_unlock_irq(&dev->event_lock);
  3165. return pending;
  3166. }
  3167. static void intel_update_pipe_config(struct intel_crtc *crtc,
  3168. struct intel_crtc_state *old_crtc_state)
  3169. {
  3170. struct drm_device *dev = crtc->base.dev;
  3171. struct drm_i915_private *dev_priv = to_i915(dev);
  3172. struct intel_crtc_state *pipe_config =
  3173. to_intel_crtc_state(crtc->base.state);
  3174. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  3175. crtc->base.mode = crtc->base.state->mode;
  3176. DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
  3177. old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
  3178. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  3179. /*
  3180. * Update pipe size and adjust fitter if needed: the reason for this is
  3181. * that in compute_mode_changes we check the native mode (not the pfit
  3182. * mode) to see if we can flip rather than do a full mode set. In the
  3183. * fastboot case, we'll flip, but if we don't update the pipesrc and
  3184. * pfit state, we'll end up with a big fb scanned out into the wrong
  3185. * sized surface.
  3186. */
  3187. I915_WRITE(PIPESRC(crtc->pipe),
  3188. ((pipe_config->pipe_src_w - 1) << 16) |
  3189. (pipe_config->pipe_src_h - 1));
  3190. /* on skylake this is done by detaching scalers */
  3191. if (INTEL_INFO(dev)->gen >= 9) {
  3192. skl_detach_scalers(crtc);
  3193. if (pipe_config->pch_pfit.enabled)
  3194. skylake_pfit_enable(crtc);
  3195. } else if (HAS_PCH_SPLIT(dev_priv)) {
  3196. if (pipe_config->pch_pfit.enabled)
  3197. ironlake_pfit_enable(crtc);
  3198. else if (old_crtc_state->pch_pfit.enabled)
  3199. ironlake_pfit_disable(crtc, true);
  3200. }
  3201. }
  3202. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  3203. {
  3204. struct drm_device *dev = crtc->dev;
  3205. struct drm_i915_private *dev_priv = to_i915(dev);
  3206. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3207. int pipe = intel_crtc->pipe;
  3208. i915_reg_t reg;
  3209. u32 temp;
  3210. /* enable normal train */
  3211. reg = FDI_TX_CTL(pipe);
  3212. temp = I915_READ(reg);
  3213. if (IS_IVYBRIDGE(dev_priv)) {
  3214. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3215. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  3216. } else {
  3217. temp &= ~FDI_LINK_TRAIN_NONE;
  3218. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  3219. }
  3220. I915_WRITE(reg, temp);
  3221. reg = FDI_RX_CTL(pipe);
  3222. temp = I915_READ(reg);
  3223. if (HAS_PCH_CPT(dev_priv)) {
  3224. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3225. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  3226. } else {
  3227. temp &= ~FDI_LINK_TRAIN_NONE;
  3228. temp |= FDI_LINK_TRAIN_NONE;
  3229. }
  3230. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  3231. /* wait one idle pattern time */
  3232. POSTING_READ(reg);
  3233. udelay(1000);
  3234. /* IVB wants error correction enabled */
  3235. if (IS_IVYBRIDGE(dev_priv))
  3236. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  3237. FDI_FE_ERRC_ENABLE);
  3238. }
  3239. /* The FDI link training functions for ILK/Ibexpeak. */
  3240. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  3241. {
  3242. struct drm_device *dev = crtc->dev;
  3243. struct drm_i915_private *dev_priv = to_i915(dev);
  3244. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3245. int pipe = intel_crtc->pipe;
  3246. i915_reg_t reg;
  3247. u32 temp, tries;
  3248. /* FDI needs bits from pipe first */
  3249. assert_pipe_enabled(dev_priv, pipe);
  3250. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3251. for train result */
  3252. reg = FDI_RX_IMR(pipe);
  3253. temp = I915_READ(reg);
  3254. temp &= ~FDI_RX_SYMBOL_LOCK;
  3255. temp &= ~FDI_RX_BIT_LOCK;
  3256. I915_WRITE(reg, temp);
  3257. I915_READ(reg);
  3258. udelay(150);
  3259. /* enable CPU FDI TX and PCH FDI RX */
  3260. reg = FDI_TX_CTL(pipe);
  3261. temp = I915_READ(reg);
  3262. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3263. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3264. temp &= ~FDI_LINK_TRAIN_NONE;
  3265. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3266. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3267. reg = FDI_RX_CTL(pipe);
  3268. temp = I915_READ(reg);
  3269. temp &= ~FDI_LINK_TRAIN_NONE;
  3270. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3271. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3272. POSTING_READ(reg);
  3273. udelay(150);
  3274. /* Ironlake workaround, enable clock pointer after FDI enable*/
  3275. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3276. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  3277. FDI_RX_PHASE_SYNC_POINTER_EN);
  3278. reg = FDI_RX_IIR(pipe);
  3279. for (tries = 0; tries < 5; tries++) {
  3280. temp = I915_READ(reg);
  3281. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3282. if ((temp & FDI_RX_BIT_LOCK)) {
  3283. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3284. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3285. break;
  3286. }
  3287. }
  3288. if (tries == 5)
  3289. DRM_ERROR("FDI train 1 fail!\n");
  3290. /* Train 2 */
  3291. reg = FDI_TX_CTL(pipe);
  3292. temp = I915_READ(reg);
  3293. temp &= ~FDI_LINK_TRAIN_NONE;
  3294. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3295. I915_WRITE(reg, temp);
  3296. reg = FDI_RX_CTL(pipe);
  3297. temp = I915_READ(reg);
  3298. temp &= ~FDI_LINK_TRAIN_NONE;
  3299. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3300. I915_WRITE(reg, temp);
  3301. POSTING_READ(reg);
  3302. udelay(150);
  3303. reg = FDI_RX_IIR(pipe);
  3304. for (tries = 0; tries < 5; tries++) {
  3305. temp = I915_READ(reg);
  3306. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3307. if (temp & FDI_RX_SYMBOL_LOCK) {
  3308. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3309. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3310. break;
  3311. }
  3312. }
  3313. if (tries == 5)
  3314. DRM_ERROR("FDI train 2 fail!\n");
  3315. DRM_DEBUG_KMS("FDI train done\n");
  3316. }
  3317. static const int snb_b_fdi_train_param[] = {
  3318. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3319. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3320. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3321. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3322. };
  3323. /* The FDI link training functions for SNB/Cougarpoint. */
  3324. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  3325. {
  3326. struct drm_device *dev = crtc->dev;
  3327. struct drm_i915_private *dev_priv = to_i915(dev);
  3328. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3329. int pipe = intel_crtc->pipe;
  3330. i915_reg_t reg;
  3331. u32 temp, i, retry;
  3332. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3333. for train result */
  3334. reg = FDI_RX_IMR(pipe);
  3335. temp = I915_READ(reg);
  3336. temp &= ~FDI_RX_SYMBOL_LOCK;
  3337. temp &= ~FDI_RX_BIT_LOCK;
  3338. I915_WRITE(reg, temp);
  3339. POSTING_READ(reg);
  3340. udelay(150);
  3341. /* enable CPU FDI TX and PCH FDI RX */
  3342. reg = FDI_TX_CTL(pipe);
  3343. temp = I915_READ(reg);
  3344. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3345. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3346. temp &= ~FDI_LINK_TRAIN_NONE;
  3347. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3348. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3349. /* SNB-B */
  3350. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3351. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3352. I915_WRITE(FDI_RX_MISC(pipe),
  3353. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3354. reg = FDI_RX_CTL(pipe);
  3355. temp = I915_READ(reg);
  3356. if (HAS_PCH_CPT(dev_priv)) {
  3357. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3358. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3359. } else {
  3360. temp &= ~FDI_LINK_TRAIN_NONE;
  3361. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3362. }
  3363. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3364. POSTING_READ(reg);
  3365. udelay(150);
  3366. for (i = 0; i < 4; i++) {
  3367. reg = FDI_TX_CTL(pipe);
  3368. temp = I915_READ(reg);
  3369. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3370. temp |= snb_b_fdi_train_param[i];
  3371. I915_WRITE(reg, temp);
  3372. POSTING_READ(reg);
  3373. udelay(500);
  3374. for (retry = 0; retry < 5; retry++) {
  3375. reg = FDI_RX_IIR(pipe);
  3376. temp = I915_READ(reg);
  3377. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3378. if (temp & FDI_RX_BIT_LOCK) {
  3379. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3380. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3381. break;
  3382. }
  3383. udelay(50);
  3384. }
  3385. if (retry < 5)
  3386. break;
  3387. }
  3388. if (i == 4)
  3389. DRM_ERROR("FDI train 1 fail!\n");
  3390. /* Train 2 */
  3391. reg = FDI_TX_CTL(pipe);
  3392. temp = I915_READ(reg);
  3393. temp &= ~FDI_LINK_TRAIN_NONE;
  3394. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3395. if (IS_GEN6(dev_priv)) {
  3396. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3397. /* SNB-B */
  3398. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3399. }
  3400. I915_WRITE(reg, temp);
  3401. reg = FDI_RX_CTL(pipe);
  3402. temp = I915_READ(reg);
  3403. if (HAS_PCH_CPT(dev_priv)) {
  3404. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3405. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3406. } else {
  3407. temp &= ~FDI_LINK_TRAIN_NONE;
  3408. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3409. }
  3410. I915_WRITE(reg, temp);
  3411. POSTING_READ(reg);
  3412. udelay(150);
  3413. for (i = 0; i < 4; i++) {
  3414. reg = FDI_TX_CTL(pipe);
  3415. temp = I915_READ(reg);
  3416. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3417. temp |= snb_b_fdi_train_param[i];
  3418. I915_WRITE(reg, temp);
  3419. POSTING_READ(reg);
  3420. udelay(500);
  3421. for (retry = 0; retry < 5; retry++) {
  3422. reg = FDI_RX_IIR(pipe);
  3423. temp = I915_READ(reg);
  3424. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3425. if (temp & FDI_RX_SYMBOL_LOCK) {
  3426. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3427. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3428. break;
  3429. }
  3430. udelay(50);
  3431. }
  3432. if (retry < 5)
  3433. break;
  3434. }
  3435. if (i == 4)
  3436. DRM_ERROR("FDI train 2 fail!\n");
  3437. DRM_DEBUG_KMS("FDI train done.\n");
  3438. }
  3439. /* Manual link training for Ivy Bridge A0 parts */
  3440. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3441. {
  3442. struct drm_device *dev = crtc->dev;
  3443. struct drm_i915_private *dev_priv = to_i915(dev);
  3444. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3445. int pipe = intel_crtc->pipe;
  3446. i915_reg_t reg;
  3447. u32 temp, i, j;
  3448. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3449. for train result */
  3450. reg = FDI_RX_IMR(pipe);
  3451. temp = I915_READ(reg);
  3452. temp &= ~FDI_RX_SYMBOL_LOCK;
  3453. temp &= ~FDI_RX_BIT_LOCK;
  3454. I915_WRITE(reg, temp);
  3455. POSTING_READ(reg);
  3456. udelay(150);
  3457. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3458. I915_READ(FDI_RX_IIR(pipe)));
  3459. /* Try each vswing and preemphasis setting twice before moving on */
  3460. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3461. /* disable first in case we need to retry */
  3462. reg = FDI_TX_CTL(pipe);
  3463. temp = I915_READ(reg);
  3464. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3465. temp &= ~FDI_TX_ENABLE;
  3466. I915_WRITE(reg, temp);
  3467. reg = FDI_RX_CTL(pipe);
  3468. temp = I915_READ(reg);
  3469. temp &= ~FDI_LINK_TRAIN_AUTO;
  3470. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3471. temp &= ~FDI_RX_ENABLE;
  3472. I915_WRITE(reg, temp);
  3473. /* enable CPU FDI TX and PCH FDI RX */
  3474. reg = FDI_TX_CTL(pipe);
  3475. temp = I915_READ(reg);
  3476. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3477. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3478. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3479. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3480. temp |= snb_b_fdi_train_param[j/2];
  3481. temp |= FDI_COMPOSITE_SYNC;
  3482. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3483. I915_WRITE(FDI_RX_MISC(pipe),
  3484. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3485. reg = FDI_RX_CTL(pipe);
  3486. temp = I915_READ(reg);
  3487. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3488. temp |= FDI_COMPOSITE_SYNC;
  3489. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3490. POSTING_READ(reg);
  3491. udelay(1); /* should be 0.5us */
  3492. for (i = 0; i < 4; i++) {
  3493. reg = FDI_RX_IIR(pipe);
  3494. temp = I915_READ(reg);
  3495. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3496. if (temp & FDI_RX_BIT_LOCK ||
  3497. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3498. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3499. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3500. i);
  3501. break;
  3502. }
  3503. udelay(1); /* should be 0.5us */
  3504. }
  3505. if (i == 4) {
  3506. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3507. continue;
  3508. }
  3509. /* Train 2 */
  3510. reg = FDI_TX_CTL(pipe);
  3511. temp = I915_READ(reg);
  3512. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3513. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3514. I915_WRITE(reg, temp);
  3515. reg = FDI_RX_CTL(pipe);
  3516. temp = I915_READ(reg);
  3517. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3518. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3519. I915_WRITE(reg, temp);
  3520. POSTING_READ(reg);
  3521. udelay(2); /* should be 1.5us */
  3522. for (i = 0; i < 4; i++) {
  3523. reg = FDI_RX_IIR(pipe);
  3524. temp = I915_READ(reg);
  3525. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3526. if (temp & FDI_RX_SYMBOL_LOCK ||
  3527. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3528. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3529. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3530. i);
  3531. goto train_done;
  3532. }
  3533. udelay(2); /* should be 1.5us */
  3534. }
  3535. if (i == 4)
  3536. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3537. }
  3538. train_done:
  3539. DRM_DEBUG_KMS("FDI train done.\n");
  3540. }
  3541. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3542. {
  3543. struct drm_device *dev = intel_crtc->base.dev;
  3544. struct drm_i915_private *dev_priv = to_i915(dev);
  3545. int pipe = intel_crtc->pipe;
  3546. i915_reg_t reg;
  3547. u32 temp;
  3548. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3549. reg = FDI_RX_CTL(pipe);
  3550. temp = I915_READ(reg);
  3551. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3552. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3553. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3554. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3555. POSTING_READ(reg);
  3556. udelay(200);
  3557. /* Switch from Rawclk to PCDclk */
  3558. temp = I915_READ(reg);
  3559. I915_WRITE(reg, temp | FDI_PCDCLK);
  3560. POSTING_READ(reg);
  3561. udelay(200);
  3562. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3563. reg = FDI_TX_CTL(pipe);
  3564. temp = I915_READ(reg);
  3565. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3566. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3567. POSTING_READ(reg);
  3568. udelay(100);
  3569. }
  3570. }
  3571. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3572. {
  3573. struct drm_device *dev = intel_crtc->base.dev;
  3574. struct drm_i915_private *dev_priv = to_i915(dev);
  3575. int pipe = intel_crtc->pipe;
  3576. i915_reg_t reg;
  3577. u32 temp;
  3578. /* Switch from PCDclk to Rawclk */
  3579. reg = FDI_RX_CTL(pipe);
  3580. temp = I915_READ(reg);
  3581. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3582. /* Disable CPU FDI TX PLL */
  3583. reg = FDI_TX_CTL(pipe);
  3584. temp = I915_READ(reg);
  3585. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3586. POSTING_READ(reg);
  3587. udelay(100);
  3588. reg = FDI_RX_CTL(pipe);
  3589. temp = I915_READ(reg);
  3590. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3591. /* Wait for the clocks to turn off. */
  3592. POSTING_READ(reg);
  3593. udelay(100);
  3594. }
  3595. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3596. {
  3597. struct drm_device *dev = crtc->dev;
  3598. struct drm_i915_private *dev_priv = to_i915(dev);
  3599. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3600. int pipe = intel_crtc->pipe;
  3601. i915_reg_t reg;
  3602. u32 temp;
  3603. /* disable CPU FDI tx and PCH FDI rx */
  3604. reg = FDI_TX_CTL(pipe);
  3605. temp = I915_READ(reg);
  3606. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3607. POSTING_READ(reg);
  3608. reg = FDI_RX_CTL(pipe);
  3609. temp = I915_READ(reg);
  3610. temp &= ~(0x7 << 16);
  3611. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3612. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3613. POSTING_READ(reg);
  3614. udelay(100);
  3615. /* Ironlake workaround, disable clock pointer after downing FDI */
  3616. if (HAS_PCH_IBX(dev_priv))
  3617. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3618. /* still set train pattern 1 */
  3619. reg = FDI_TX_CTL(pipe);
  3620. temp = I915_READ(reg);
  3621. temp &= ~FDI_LINK_TRAIN_NONE;
  3622. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3623. I915_WRITE(reg, temp);
  3624. reg = FDI_RX_CTL(pipe);
  3625. temp = I915_READ(reg);
  3626. if (HAS_PCH_CPT(dev_priv)) {
  3627. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3628. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3629. } else {
  3630. temp &= ~FDI_LINK_TRAIN_NONE;
  3631. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3632. }
  3633. /* BPC in FDI rx is consistent with that in PIPECONF */
  3634. temp &= ~(0x07 << 16);
  3635. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3636. I915_WRITE(reg, temp);
  3637. POSTING_READ(reg);
  3638. udelay(100);
  3639. }
  3640. bool intel_has_pending_fb_unpin(struct drm_device *dev)
  3641. {
  3642. struct drm_i915_private *dev_priv = to_i915(dev);
  3643. struct intel_crtc *crtc;
  3644. /* Note that we don't need to be called with mode_config.lock here
  3645. * as our list of CRTC objects is static for the lifetime of the
  3646. * device and so cannot disappear as we iterate. Similarly, we can
  3647. * happily treat the predicates as racy, atomic checks as userspace
  3648. * cannot claim and pin a new fb without at least acquring the
  3649. * struct_mutex and so serialising with us.
  3650. */
  3651. for_each_intel_crtc(dev, crtc) {
  3652. if (atomic_read(&crtc->unpin_work_count) == 0)
  3653. continue;
  3654. if (crtc->flip_work)
  3655. intel_wait_for_vblank(dev_priv, crtc->pipe);
  3656. return true;
  3657. }
  3658. return false;
  3659. }
  3660. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3661. {
  3662. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3663. struct intel_flip_work *work = intel_crtc->flip_work;
  3664. intel_crtc->flip_work = NULL;
  3665. if (work->event)
  3666. drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
  3667. drm_crtc_vblank_put(&intel_crtc->base);
  3668. wake_up_all(&dev_priv->pending_flip_queue);
  3669. queue_work(dev_priv->wq, &work->unpin_work);
  3670. trace_i915_flip_complete(intel_crtc->plane,
  3671. work->pending_flip_obj);
  3672. }
  3673. static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3674. {
  3675. struct drm_device *dev = crtc->dev;
  3676. struct drm_i915_private *dev_priv = to_i915(dev);
  3677. long ret;
  3678. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3679. ret = wait_event_interruptible_timeout(
  3680. dev_priv->pending_flip_queue,
  3681. !intel_crtc_has_pending_flip(crtc),
  3682. 60*HZ);
  3683. if (ret < 0)
  3684. return ret;
  3685. if (ret == 0) {
  3686. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3687. struct intel_flip_work *work;
  3688. spin_lock_irq(&dev->event_lock);
  3689. work = intel_crtc->flip_work;
  3690. if (work && !is_mmio_work(work)) {
  3691. WARN_ONCE(1, "Removing stuck page flip\n");
  3692. page_flip_completed(intel_crtc);
  3693. }
  3694. spin_unlock_irq(&dev->event_lock);
  3695. }
  3696. return 0;
  3697. }
  3698. void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3699. {
  3700. u32 temp;
  3701. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3702. mutex_lock(&dev_priv->sb_lock);
  3703. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3704. temp |= SBI_SSCCTL_DISABLE;
  3705. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3706. mutex_unlock(&dev_priv->sb_lock);
  3707. }
  3708. /* Program iCLKIP clock to the desired frequency */
  3709. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3710. {
  3711. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  3712. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3713. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3714. u32 temp;
  3715. lpt_disable_iclkip(dev_priv);
  3716. /* The iCLK virtual clock root frequency is in MHz,
  3717. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3718. * divisors, it is necessary to divide one by another, so we
  3719. * convert the virtual clock precision to KHz here for higher
  3720. * precision.
  3721. */
  3722. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3723. u32 iclk_virtual_root_freq = 172800 * 1000;
  3724. u32 iclk_pi_range = 64;
  3725. u32 desired_divisor;
  3726. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3727. clock << auxdiv);
  3728. divsel = (desired_divisor / iclk_pi_range) - 2;
  3729. phaseinc = desired_divisor % iclk_pi_range;
  3730. /*
  3731. * Near 20MHz is a corner case which is
  3732. * out of range for the 7-bit divisor
  3733. */
  3734. if (divsel <= 0x7f)
  3735. break;
  3736. }
  3737. /* This should not happen with any sane values */
  3738. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3739. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3740. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3741. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3742. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3743. clock,
  3744. auxdiv,
  3745. divsel,
  3746. phasedir,
  3747. phaseinc);
  3748. mutex_lock(&dev_priv->sb_lock);
  3749. /* Program SSCDIVINTPHASE6 */
  3750. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3751. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3752. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3753. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3754. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3755. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3756. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3757. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3758. /* Program SSCAUXDIV */
  3759. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3760. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3761. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3762. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3763. /* Enable modulator and associated divider */
  3764. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3765. temp &= ~SBI_SSCCTL_DISABLE;
  3766. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3767. mutex_unlock(&dev_priv->sb_lock);
  3768. /* Wait for initialization time */
  3769. udelay(24);
  3770. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3771. }
  3772. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3773. {
  3774. u32 divsel, phaseinc, auxdiv;
  3775. u32 iclk_virtual_root_freq = 172800 * 1000;
  3776. u32 iclk_pi_range = 64;
  3777. u32 desired_divisor;
  3778. u32 temp;
  3779. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3780. return 0;
  3781. mutex_lock(&dev_priv->sb_lock);
  3782. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3783. if (temp & SBI_SSCCTL_DISABLE) {
  3784. mutex_unlock(&dev_priv->sb_lock);
  3785. return 0;
  3786. }
  3787. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3788. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3789. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3790. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3791. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3792. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3793. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3794. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3795. mutex_unlock(&dev_priv->sb_lock);
  3796. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3797. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3798. desired_divisor << auxdiv);
  3799. }
  3800. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3801. enum pipe pch_transcoder)
  3802. {
  3803. struct drm_device *dev = crtc->base.dev;
  3804. struct drm_i915_private *dev_priv = to_i915(dev);
  3805. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3806. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3807. I915_READ(HTOTAL(cpu_transcoder)));
  3808. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3809. I915_READ(HBLANK(cpu_transcoder)));
  3810. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3811. I915_READ(HSYNC(cpu_transcoder)));
  3812. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3813. I915_READ(VTOTAL(cpu_transcoder)));
  3814. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3815. I915_READ(VBLANK(cpu_transcoder)));
  3816. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3817. I915_READ(VSYNC(cpu_transcoder)));
  3818. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3819. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3820. }
  3821. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3822. {
  3823. struct drm_i915_private *dev_priv = to_i915(dev);
  3824. uint32_t temp;
  3825. temp = I915_READ(SOUTH_CHICKEN1);
  3826. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3827. return;
  3828. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3829. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3830. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3831. if (enable)
  3832. temp |= FDI_BC_BIFURCATION_SELECT;
  3833. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3834. I915_WRITE(SOUTH_CHICKEN1, temp);
  3835. POSTING_READ(SOUTH_CHICKEN1);
  3836. }
  3837. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3838. {
  3839. struct drm_device *dev = intel_crtc->base.dev;
  3840. switch (intel_crtc->pipe) {
  3841. case PIPE_A:
  3842. break;
  3843. case PIPE_B:
  3844. if (intel_crtc->config->fdi_lanes > 2)
  3845. cpt_set_fdi_bc_bifurcation(dev, false);
  3846. else
  3847. cpt_set_fdi_bc_bifurcation(dev, true);
  3848. break;
  3849. case PIPE_C:
  3850. cpt_set_fdi_bc_bifurcation(dev, true);
  3851. break;
  3852. default:
  3853. BUG();
  3854. }
  3855. }
  3856. /* Return which DP Port should be selected for Transcoder DP control */
  3857. static enum port
  3858. intel_trans_dp_port_sel(struct drm_crtc *crtc)
  3859. {
  3860. struct drm_device *dev = crtc->dev;
  3861. struct intel_encoder *encoder;
  3862. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3863. if (encoder->type == INTEL_OUTPUT_DP ||
  3864. encoder->type == INTEL_OUTPUT_EDP)
  3865. return enc_to_dig_port(&encoder->base)->port;
  3866. }
  3867. return -1;
  3868. }
  3869. /*
  3870. * Enable PCH resources required for PCH ports:
  3871. * - PCH PLLs
  3872. * - FDI training & RX/TX
  3873. * - update transcoder timings
  3874. * - DP transcoding bits
  3875. * - transcoder
  3876. */
  3877. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3878. {
  3879. struct drm_device *dev = crtc->dev;
  3880. struct drm_i915_private *dev_priv = to_i915(dev);
  3881. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3882. int pipe = intel_crtc->pipe;
  3883. u32 temp;
  3884. assert_pch_transcoder_disabled(dev_priv, pipe);
  3885. if (IS_IVYBRIDGE(dev_priv))
  3886. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3887. /* Write the TU size bits before fdi link training, so that error
  3888. * detection works. */
  3889. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3890. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3891. /* For PCH output, training FDI link */
  3892. dev_priv->display.fdi_link_train(crtc);
  3893. /* We need to program the right clock selection before writing the pixel
  3894. * mutliplier into the DPLL. */
  3895. if (HAS_PCH_CPT(dev_priv)) {
  3896. u32 sel;
  3897. temp = I915_READ(PCH_DPLL_SEL);
  3898. temp |= TRANS_DPLL_ENABLE(pipe);
  3899. sel = TRANS_DPLLB_SEL(pipe);
  3900. if (intel_crtc->config->shared_dpll ==
  3901. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3902. temp |= sel;
  3903. else
  3904. temp &= ~sel;
  3905. I915_WRITE(PCH_DPLL_SEL, temp);
  3906. }
  3907. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3908. * transcoder, and we actually should do this to not upset any PCH
  3909. * transcoder that already use the clock when we share it.
  3910. *
  3911. * Note that enable_shared_dpll tries to do the right thing, but
  3912. * get_shared_dpll unconditionally resets the pll - we need that to have
  3913. * the right LVDS enable sequence. */
  3914. intel_enable_shared_dpll(intel_crtc);
  3915. /* set transcoder timing, panel must allow it */
  3916. assert_panel_unlocked(dev_priv, pipe);
  3917. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3918. intel_fdi_normal_train(crtc);
  3919. /* For PCH DP, enable TRANS_DP_CTL */
  3920. if (HAS_PCH_CPT(dev_priv) &&
  3921. intel_crtc_has_dp_encoder(intel_crtc->config)) {
  3922. const struct drm_display_mode *adjusted_mode =
  3923. &intel_crtc->config->base.adjusted_mode;
  3924. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3925. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3926. temp = I915_READ(reg);
  3927. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3928. TRANS_DP_SYNC_MASK |
  3929. TRANS_DP_BPC_MASK);
  3930. temp |= TRANS_DP_OUTPUT_ENABLE;
  3931. temp |= bpc << 9; /* same format but at 11:9 */
  3932. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3933. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3934. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3935. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3936. switch (intel_trans_dp_port_sel(crtc)) {
  3937. case PORT_B:
  3938. temp |= TRANS_DP_PORT_SEL_B;
  3939. break;
  3940. case PORT_C:
  3941. temp |= TRANS_DP_PORT_SEL_C;
  3942. break;
  3943. case PORT_D:
  3944. temp |= TRANS_DP_PORT_SEL_D;
  3945. break;
  3946. default:
  3947. BUG();
  3948. }
  3949. I915_WRITE(reg, temp);
  3950. }
  3951. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3952. }
  3953. static void lpt_pch_enable(struct drm_crtc *crtc)
  3954. {
  3955. struct drm_device *dev = crtc->dev;
  3956. struct drm_i915_private *dev_priv = to_i915(dev);
  3957. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3958. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3959. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3960. lpt_program_iclkip(crtc);
  3961. /* Set transcoder timing. */
  3962. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3963. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3964. }
  3965. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3966. {
  3967. struct drm_i915_private *dev_priv = to_i915(dev);
  3968. i915_reg_t dslreg = PIPEDSL(pipe);
  3969. u32 temp;
  3970. temp = I915_READ(dslreg);
  3971. udelay(500);
  3972. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3973. if (wait_for(I915_READ(dslreg) != temp, 5))
  3974. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3975. }
  3976. }
  3977. static int
  3978. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3979. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3980. int src_w, int src_h, int dst_w, int dst_h)
  3981. {
  3982. struct intel_crtc_scaler_state *scaler_state =
  3983. &crtc_state->scaler_state;
  3984. struct intel_crtc *intel_crtc =
  3985. to_intel_crtc(crtc_state->base.crtc);
  3986. int need_scaling;
  3987. need_scaling = drm_rotation_90_or_270(rotation) ?
  3988. (src_h != dst_w || src_w != dst_h):
  3989. (src_w != dst_w || src_h != dst_h);
  3990. /*
  3991. * if plane is being disabled or scaler is no more required or force detach
  3992. * - free scaler binded to this plane/crtc
  3993. * - in order to do this, update crtc->scaler_usage
  3994. *
  3995. * Here scaler state in crtc_state is set free so that
  3996. * scaler can be assigned to other user. Actual register
  3997. * update to free the scaler is done in plane/panel-fit programming.
  3998. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3999. */
  4000. if (force_detach || !need_scaling) {
  4001. if (*scaler_id >= 0) {
  4002. scaler_state->scaler_users &= ~(1 << scaler_user);
  4003. scaler_state->scalers[*scaler_id].in_use = 0;
  4004. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  4005. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  4006. intel_crtc->pipe, scaler_user, *scaler_id,
  4007. scaler_state->scaler_users);
  4008. *scaler_id = -1;
  4009. }
  4010. return 0;
  4011. }
  4012. /* range checks */
  4013. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  4014. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  4015. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  4016. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  4017. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  4018. "size is out of scaler range\n",
  4019. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  4020. return -EINVAL;
  4021. }
  4022. /* mark this plane as a scaler user in crtc_state */
  4023. scaler_state->scaler_users |= (1 << scaler_user);
  4024. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  4025. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  4026. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  4027. scaler_state->scaler_users);
  4028. return 0;
  4029. }
  4030. /**
  4031. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  4032. *
  4033. * @state: crtc's scaler state
  4034. *
  4035. * Return
  4036. * 0 - scaler_usage updated successfully
  4037. * error - requested scaling cannot be supported or other error condition
  4038. */
  4039. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  4040. {
  4041. struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
  4042. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  4043. DRM_DEBUG_KMS("Updating scaler for [CRTC:%d:%s] scaler_user index %u.%u\n",
  4044. intel_crtc->base.base.id, intel_crtc->base.name,
  4045. intel_crtc->pipe, SKL_CRTC_INDEX);
  4046. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  4047. &state->scaler_state.scaler_id, DRM_ROTATE_0,
  4048. state->pipe_src_w, state->pipe_src_h,
  4049. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  4050. }
  4051. /**
  4052. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  4053. *
  4054. * @state: crtc's scaler state
  4055. * @plane_state: atomic plane state to update
  4056. *
  4057. * Return
  4058. * 0 - scaler_usage updated successfully
  4059. * error - requested scaling cannot be supported or other error condition
  4060. */
  4061. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  4062. struct intel_plane_state *plane_state)
  4063. {
  4064. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  4065. struct intel_plane *intel_plane =
  4066. to_intel_plane(plane_state->base.plane);
  4067. struct drm_framebuffer *fb = plane_state->base.fb;
  4068. int ret;
  4069. bool force_detach = !fb || !plane_state->base.visible;
  4070. DRM_DEBUG_KMS("Updating scaler for [PLANE:%d:%s] scaler_user index %u.%u\n",
  4071. intel_plane->base.base.id, intel_plane->base.name,
  4072. intel_crtc->pipe, drm_plane_index(&intel_plane->base));
  4073. ret = skl_update_scaler(crtc_state, force_detach,
  4074. drm_plane_index(&intel_plane->base),
  4075. &plane_state->scaler_id,
  4076. plane_state->base.rotation,
  4077. drm_rect_width(&plane_state->base.src) >> 16,
  4078. drm_rect_height(&plane_state->base.src) >> 16,
  4079. drm_rect_width(&plane_state->base.dst),
  4080. drm_rect_height(&plane_state->base.dst));
  4081. if (ret || plane_state->scaler_id < 0)
  4082. return ret;
  4083. /* check colorkey */
  4084. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  4085. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  4086. intel_plane->base.base.id,
  4087. intel_plane->base.name);
  4088. return -EINVAL;
  4089. }
  4090. /* Check src format */
  4091. switch (fb->pixel_format) {
  4092. case DRM_FORMAT_RGB565:
  4093. case DRM_FORMAT_XBGR8888:
  4094. case DRM_FORMAT_XRGB8888:
  4095. case DRM_FORMAT_ABGR8888:
  4096. case DRM_FORMAT_ARGB8888:
  4097. case DRM_FORMAT_XRGB2101010:
  4098. case DRM_FORMAT_XBGR2101010:
  4099. case DRM_FORMAT_YUYV:
  4100. case DRM_FORMAT_YVYU:
  4101. case DRM_FORMAT_UYVY:
  4102. case DRM_FORMAT_VYUY:
  4103. break;
  4104. default:
  4105. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  4106. intel_plane->base.base.id, intel_plane->base.name,
  4107. fb->base.id, fb->pixel_format);
  4108. return -EINVAL;
  4109. }
  4110. return 0;
  4111. }
  4112. static void skylake_scaler_disable(struct intel_crtc *crtc)
  4113. {
  4114. int i;
  4115. for (i = 0; i < crtc->num_scalers; i++)
  4116. skl_detach_scaler(crtc, i);
  4117. }
  4118. static void skylake_pfit_enable(struct intel_crtc *crtc)
  4119. {
  4120. struct drm_device *dev = crtc->base.dev;
  4121. struct drm_i915_private *dev_priv = to_i915(dev);
  4122. int pipe = crtc->pipe;
  4123. struct intel_crtc_scaler_state *scaler_state =
  4124. &crtc->config->scaler_state;
  4125. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  4126. if (crtc->config->pch_pfit.enabled) {
  4127. int id;
  4128. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  4129. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  4130. return;
  4131. }
  4132. id = scaler_state->scaler_id;
  4133. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  4134. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  4135. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  4136. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  4137. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  4138. }
  4139. }
  4140. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  4141. {
  4142. struct drm_device *dev = crtc->base.dev;
  4143. struct drm_i915_private *dev_priv = to_i915(dev);
  4144. int pipe = crtc->pipe;
  4145. if (crtc->config->pch_pfit.enabled) {
  4146. /* Force use of hard-coded filter coefficients
  4147. * as some pre-programmed values are broken,
  4148. * e.g. x201.
  4149. */
  4150. if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
  4151. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  4152. PF_PIPE_SEL_IVB(pipe));
  4153. else
  4154. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  4155. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  4156. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  4157. }
  4158. }
  4159. void hsw_enable_ips(struct intel_crtc *crtc)
  4160. {
  4161. struct drm_device *dev = crtc->base.dev;
  4162. struct drm_i915_private *dev_priv = to_i915(dev);
  4163. if (!crtc->config->ips_enabled)
  4164. return;
  4165. /*
  4166. * We can only enable IPS after we enable a plane and wait for a vblank
  4167. * This function is called from post_plane_update, which is run after
  4168. * a vblank wait.
  4169. */
  4170. assert_plane_enabled(dev_priv, crtc->plane);
  4171. if (IS_BROADWELL(dev_priv)) {
  4172. mutex_lock(&dev_priv->rps.hw_lock);
  4173. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  4174. mutex_unlock(&dev_priv->rps.hw_lock);
  4175. /* Quoting Art Runyan: "its not safe to expect any particular
  4176. * value in IPS_CTL bit 31 after enabling IPS through the
  4177. * mailbox." Moreover, the mailbox may return a bogus state,
  4178. * so we need to just enable it and continue on.
  4179. */
  4180. } else {
  4181. I915_WRITE(IPS_CTL, IPS_ENABLE);
  4182. /* The bit only becomes 1 in the next vblank, so this wait here
  4183. * is essentially intel_wait_for_vblank. If we don't have this
  4184. * and don't wait for vblanks until the end of crtc_enable, then
  4185. * the HW state readout code will complain that the expected
  4186. * IPS_CTL value is not the one we read. */
  4187. if (intel_wait_for_register(dev_priv,
  4188. IPS_CTL, IPS_ENABLE, IPS_ENABLE,
  4189. 50))
  4190. DRM_ERROR("Timed out waiting for IPS enable\n");
  4191. }
  4192. }
  4193. void hsw_disable_ips(struct intel_crtc *crtc)
  4194. {
  4195. struct drm_device *dev = crtc->base.dev;
  4196. struct drm_i915_private *dev_priv = to_i915(dev);
  4197. if (!crtc->config->ips_enabled)
  4198. return;
  4199. assert_plane_enabled(dev_priv, crtc->plane);
  4200. if (IS_BROADWELL(dev_priv)) {
  4201. mutex_lock(&dev_priv->rps.hw_lock);
  4202. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  4203. mutex_unlock(&dev_priv->rps.hw_lock);
  4204. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  4205. if (intel_wait_for_register(dev_priv,
  4206. IPS_CTL, IPS_ENABLE, 0,
  4207. 42))
  4208. DRM_ERROR("Timed out waiting for IPS disable\n");
  4209. } else {
  4210. I915_WRITE(IPS_CTL, 0);
  4211. POSTING_READ(IPS_CTL);
  4212. }
  4213. /* We need to wait for a vblank before we can disable the plane. */
  4214. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4215. }
  4216. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  4217. {
  4218. if (intel_crtc->overlay) {
  4219. struct drm_device *dev = intel_crtc->base.dev;
  4220. struct drm_i915_private *dev_priv = to_i915(dev);
  4221. mutex_lock(&dev->struct_mutex);
  4222. dev_priv->mm.interruptible = false;
  4223. (void) intel_overlay_switch_off(intel_crtc->overlay);
  4224. dev_priv->mm.interruptible = true;
  4225. mutex_unlock(&dev->struct_mutex);
  4226. }
  4227. /* Let userspace switch the overlay on again. In most cases userspace
  4228. * has to recompute where to put it anyway.
  4229. */
  4230. }
  4231. /**
  4232. * intel_post_enable_primary - Perform operations after enabling primary plane
  4233. * @crtc: the CRTC whose primary plane was just enabled
  4234. *
  4235. * Performs potentially sleeping operations that must be done after the primary
  4236. * plane is enabled, such as updating FBC and IPS. Note that this may be
  4237. * called due to an explicit primary plane update, or due to an implicit
  4238. * re-enable that is caused when a sprite plane is updated to no longer
  4239. * completely hide the primary plane.
  4240. */
  4241. static void
  4242. intel_post_enable_primary(struct drm_crtc *crtc)
  4243. {
  4244. struct drm_device *dev = crtc->dev;
  4245. struct drm_i915_private *dev_priv = to_i915(dev);
  4246. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4247. int pipe = intel_crtc->pipe;
  4248. /*
  4249. * FIXME IPS should be fine as long as one plane is
  4250. * enabled, but in practice it seems to have problems
  4251. * when going from primary only to sprite only and vice
  4252. * versa.
  4253. */
  4254. hsw_enable_ips(intel_crtc);
  4255. /*
  4256. * Gen2 reports pipe underruns whenever all planes are disabled.
  4257. * So don't enable underrun reporting before at least some planes
  4258. * are enabled.
  4259. * FIXME: Need to fix the logic to work when we turn off all planes
  4260. * but leave the pipe running.
  4261. */
  4262. if (IS_GEN2(dev_priv))
  4263. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4264. /* Underruns don't always raise interrupts, so check manually. */
  4265. intel_check_cpu_fifo_underruns(dev_priv);
  4266. intel_check_pch_fifo_underruns(dev_priv);
  4267. }
  4268. /* FIXME move all this to pre_plane_update() with proper state tracking */
  4269. static void
  4270. intel_pre_disable_primary(struct drm_crtc *crtc)
  4271. {
  4272. struct drm_device *dev = crtc->dev;
  4273. struct drm_i915_private *dev_priv = to_i915(dev);
  4274. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4275. int pipe = intel_crtc->pipe;
  4276. /*
  4277. * Gen2 reports pipe underruns whenever all planes are disabled.
  4278. * So diasble underrun reporting before all the planes get disabled.
  4279. * FIXME: Need to fix the logic to work when we turn off all planes
  4280. * but leave the pipe running.
  4281. */
  4282. if (IS_GEN2(dev_priv))
  4283. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4284. /*
  4285. * FIXME IPS should be fine as long as one plane is
  4286. * enabled, but in practice it seems to have problems
  4287. * when going from primary only to sprite only and vice
  4288. * versa.
  4289. */
  4290. hsw_disable_ips(intel_crtc);
  4291. }
  4292. /* FIXME get rid of this and use pre_plane_update */
  4293. static void
  4294. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  4295. {
  4296. struct drm_device *dev = crtc->dev;
  4297. struct drm_i915_private *dev_priv = to_i915(dev);
  4298. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4299. int pipe = intel_crtc->pipe;
  4300. intel_pre_disable_primary(crtc);
  4301. /*
  4302. * Vblank time updates from the shadow to live plane control register
  4303. * are blocked if the memory self-refresh mode is active at that
  4304. * moment. So to make sure the plane gets truly disabled, disable
  4305. * first the self-refresh mode. The self-refresh enable bit in turn
  4306. * will be checked/applied by the HW only at the next frame start
  4307. * event which is after the vblank start event, so we need to have a
  4308. * wait-for-vblank between disabling the plane and the pipe.
  4309. */
  4310. if (HAS_GMCH_DISPLAY(dev_priv)) {
  4311. intel_set_memory_cxsr(dev_priv, false);
  4312. dev_priv->wm.vlv.cxsr = false;
  4313. intel_wait_for_vblank(dev_priv, pipe);
  4314. }
  4315. }
  4316. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  4317. {
  4318. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4319. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4320. struct intel_crtc_state *pipe_config =
  4321. to_intel_crtc_state(crtc->base.state);
  4322. struct drm_plane *primary = crtc->base.primary;
  4323. struct drm_plane_state *old_pri_state =
  4324. drm_atomic_get_existing_plane_state(old_state, primary);
  4325. intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
  4326. crtc->wm.cxsr_allowed = true;
  4327. if (pipe_config->update_wm_post && pipe_config->base.active)
  4328. intel_update_watermarks(crtc);
  4329. if (old_pri_state) {
  4330. struct intel_plane_state *primary_state =
  4331. to_intel_plane_state(primary->state);
  4332. struct intel_plane_state *old_primary_state =
  4333. to_intel_plane_state(old_pri_state);
  4334. intel_fbc_post_update(crtc);
  4335. if (primary_state->base.visible &&
  4336. (needs_modeset(&pipe_config->base) ||
  4337. !old_primary_state->base.visible))
  4338. intel_post_enable_primary(&crtc->base);
  4339. }
  4340. }
  4341. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
  4342. {
  4343. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4344. struct drm_device *dev = crtc->base.dev;
  4345. struct drm_i915_private *dev_priv = to_i915(dev);
  4346. struct intel_crtc_state *pipe_config =
  4347. to_intel_crtc_state(crtc->base.state);
  4348. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4349. struct drm_plane *primary = crtc->base.primary;
  4350. struct drm_plane_state *old_pri_state =
  4351. drm_atomic_get_existing_plane_state(old_state, primary);
  4352. bool modeset = needs_modeset(&pipe_config->base);
  4353. if (old_pri_state) {
  4354. struct intel_plane_state *primary_state =
  4355. to_intel_plane_state(primary->state);
  4356. struct intel_plane_state *old_primary_state =
  4357. to_intel_plane_state(old_pri_state);
  4358. intel_fbc_pre_update(crtc, pipe_config, primary_state);
  4359. if (old_primary_state->base.visible &&
  4360. (modeset || !primary_state->base.visible))
  4361. intel_pre_disable_primary(&crtc->base);
  4362. }
  4363. if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev_priv)) {
  4364. crtc->wm.cxsr_allowed = false;
  4365. /*
  4366. * Vblank time updates from the shadow to live plane control register
  4367. * are blocked if the memory self-refresh mode is active at that
  4368. * moment. So to make sure the plane gets truly disabled, disable
  4369. * first the self-refresh mode. The self-refresh enable bit in turn
  4370. * will be checked/applied by the HW only at the next frame start
  4371. * event which is after the vblank start event, so we need to have a
  4372. * wait-for-vblank between disabling the plane and the pipe.
  4373. */
  4374. if (old_crtc_state->base.active) {
  4375. intel_set_memory_cxsr(dev_priv, false);
  4376. dev_priv->wm.vlv.cxsr = false;
  4377. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4378. }
  4379. }
  4380. /*
  4381. * IVB workaround: must disable low power watermarks for at least
  4382. * one frame before enabling scaling. LP watermarks can be re-enabled
  4383. * when scaling is disabled.
  4384. *
  4385. * WaCxSRDisabledForSpriteScaling:ivb
  4386. */
  4387. if (pipe_config->disable_lp_wm) {
  4388. ilk_disable_lp_wm(dev);
  4389. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4390. }
  4391. /*
  4392. * If we're doing a modeset, we're done. No need to do any pre-vblank
  4393. * watermark programming here.
  4394. */
  4395. if (needs_modeset(&pipe_config->base))
  4396. return;
  4397. /*
  4398. * For platforms that support atomic watermarks, program the
  4399. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  4400. * will be the intermediate values that are safe for both pre- and
  4401. * post- vblank; when vblank happens, the 'active' values will be set
  4402. * to the final 'target' values and we'll do this again to get the
  4403. * optimal watermarks. For gen9+ platforms, the values we program here
  4404. * will be the final target values which will get automatically latched
  4405. * at vblank time; no further programming will be necessary.
  4406. *
  4407. * If a platform hasn't been transitioned to atomic watermarks yet,
  4408. * we'll continue to update watermarks the old way, if flags tell
  4409. * us to.
  4410. */
  4411. if (dev_priv->display.initial_watermarks != NULL)
  4412. dev_priv->display.initial_watermarks(pipe_config);
  4413. else if (pipe_config->update_wm_pre)
  4414. intel_update_watermarks(crtc);
  4415. }
  4416. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4417. {
  4418. struct drm_device *dev = crtc->dev;
  4419. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4420. struct drm_plane *p;
  4421. int pipe = intel_crtc->pipe;
  4422. intel_crtc_dpms_overlay_disable(intel_crtc);
  4423. drm_for_each_plane_mask(p, dev, plane_mask)
  4424. to_intel_plane(p)->disable_plane(p, crtc);
  4425. /*
  4426. * FIXME: Once we grow proper nuclear flip support out of this we need
  4427. * to compute the mask of flip planes precisely. For the time being
  4428. * consider this a flip to a NULL plane.
  4429. */
  4430. intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4431. }
  4432. static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
  4433. struct intel_crtc_state *crtc_state,
  4434. struct drm_atomic_state *old_state)
  4435. {
  4436. struct drm_connector_state *old_conn_state;
  4437. struct drm_connector *conn;
  4438. int i;
  4439. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4440. struct drm_connector_state *conn_state = conn->state;
  4441. struct intel_encoder *encoder =
  4442. to_intel_encoder(conn_state->best_encoder);
  4443. if (conn_state->crtc != crtc)
  4444. continue;
  4445. if (encoder->pre_pll_enable)
  4446. encoder->pre_pll_enable(encoder, crtc_state, conn_state);
  4447. }
  4448. }
  4449. static void intel_encoders_pre_enable(struct drm_crtc *crtc,
  4450. struct intel_crtc_state *crtc_state,
  4451. struct drm_atomic_state *old_state)
  4452. {
  4453. struct drm_connector_state *old_conn_state;
  4454. struct drm_connector *conn;
  4455. int i;
  4456. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4457. struct drm_connector_state *conn_state = conn->state;
  4458. struct intel_encoder *encoder =
  4459. to_intel_encoder(conn_state->best_encoder);
  4460. if (conn_state->crtc != crtc)
  4461. continue;
  4462. if (encoder->pre_enable)
  4463. encoder->pre_enable(encoder, crtc_state, conn_state);
  4464. }
  4465. }
  4466. static void intel_encoders_enable(struct drm_crtc *crtc,
  4467. struct intel_crtc_state *crtc_state,
  4468. struct drm_atomic_state *old_state)
  4469. {
  4470. struct drm_connector_state *old_conn_state;
  4471. struct drm_connector *conn;
  4472. int i;
  4473. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4474. struct drm_connector_state *conn_state = conn->state;
  4475. struct intel_encoder *encoder =
  4476. to_intel_encoder(conn_state->best_encoder);
  4477. if (conn_state->crtc != crtc)
  4478. continue;
  4479. encoder->enable(encoder, crtc_state, conn_state);
  4480. intel_opregion_notify_encoder(encoder, true);
  4481. }
  4482. }
  4483. static void intel_encoders_disable(struct drm_crtc *crtc,
  4484. struct intel_crtc_state *old_crtc_state,
  4485. struct drm_atomic_state *old_state)
  4486. {
  4487. struct drm_connector_state *old_conn_state;
  4488. struct drm_connector *conn;
  4489. int i;
  4490. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4491. struct intel_encoder *encoder =
  4492. to_intel_encoder(old_conn_state->best_encoder);
  4493. if (old_conn_state->crtc != crtc)
  4494. continue;
  4495. intel_opregion_notify_encoder(encoder, false);
  4496. encoder->disable(encoder, old_crtc_state, old_conn_state);
  4497. }
  4498. }
  4499. static void intel_encoders_post_disable(struct drm_crtc *crtc,
  4500. struct intel_crtc_state *old_crtc_state,
  4501. struct drm_atomic_state *old_state)
  4502. {
  4503. struct drm_connector_state *old_conn_state;
  4504. struct drm_connector *conn;
  4505. int i;
  4506. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4507. struct intel_encoder *encoder =
  4508. to_intel_encoder(old_conn_state->best_encoder);
  4509. if (old_conn_state->crtc != crtc)
  4510. continue;
  4511. if (encoder->post_disable)
  4512. encoder->post_disable(encoder, old_crtc_state, old_conn_state);
  4513. }
  4514. }
  4515. static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
  4516. struct intel_crtc_state *old_crtc_state,
  4517. struct drm_atomic_state *old_state)
  4518. {
  4519. struct drm_connector_state *old_conn_state;
  4520. struct drm_connector *conn;
  4521. int i;
  4522. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4523. struct intel_encoder *encoder =
  4524. to_intel_encoder(old_conn_state->best_encoder);
  4525. if (old_conn_state->crtc != crtc)
  4526. continue;
  4527. if (encoder->post_pll_disable)
  4528. encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
  4529. }
  4530. }
  4531. static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
  4532. struct drm_atomic_state *old_state)
  4533. {
  4534. struct drm_crtc *crtc = pipe_config->base.crtc;
  4535. struct drm_device *dev = crtc->dev;
  4536. struct drm_i915_private *dev_priv = to_i915(dev);
  4537. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4538. int pipe = intel_crtc->pipe;
  4539. if (WARN_ON(intel_crtc->active))
  4540. return;
  4541. /*
  4542. * Sometimes spurious CPU pipe underruns happen during FDI
  4543. * training, at least with VGA+HDMI cloning. Suppress them.
  4544. *
  4545. * On ILK we get an occasional spurious CPU pipe underruns
  4546. * between eDP port A enable and vdd enable. Also PCH port
  4547. * enable seems to result in the occasional CPU pipe underrun.
  4548. *
  4549. * Spurious PCH underruns also occur during PCH enabling.
  4550. */
  4551. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4552. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4553. if (intel_crtc->config->has_pch_encoder)
  4554. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4555. if (intel_crtc->config->has_pch_encoder)
  4556. intel_prepare_shared_dpll(intel_crtc);
  4557. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4558. intel_dp_set_m_n(intel_crtc, M1_N1);
  4559. intel_set_pipe_timings(intel_crtc);
  4560. intel_set_pipe_src_size(intel_crtc);
  4561. if (intel_crtc->config->has_pch_encoder) {
  4562. intel_cpu_transcoder_set_m_n(intel_crtc,
  4563. &intel_crtc->config->fdi_m_n, NULL);
  4564. }
  4565. ironlake_set_pipeconf(crtc);
  4566. intel_crtc->active = true;
  4567. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4568. if (intel_crtc->config->has_pch_encoder) {
  4569. /* Note: FDI PLL enabling _must_ be done before we enable the
  4570. * cpu pipes, hence this is separate from all the other fdi/pch
  4571. * enabling. */
  4572. ironlake_fdi_pll_enable(intel_crtc);
  4573. } else {
  4574. assert_fdi_tx_disabled(dev_priv, pipe);
  4575. assert_fdi_rx_disabled(dev_priv, pipe);
  4576. }
  4577. ironlake_pfit_enable(intel_crtc);
  4578. /*
  4579. * On ILK+ LUT must be loaded before the pipe is running but with
  4580. * clocks enabled
  4581. */
  4582. intel_color_load_luts(&pipe_config->base);
  4583. if (dev_priv->display.initial_watermarks != NULL)
  4584. dev_priv->display.initial_watermarks(intel_crtc->config);
  4585. intel_enable_pipe(intel_crtc);
  4586. if (intel_crtc->config->has_pch_encoder)
  4587. ironlake_pch_enable(crtc);
  4588. assert_vblank_disabled(crtc);
  4589. drm_crtc_vblank_on(crtc);
  4590. intel_encoders_enable(crtc, pipe_config, old_state);
  4591. if (HAS_PCH_CPT(dev_priv))
  4592. cpt_verify_modeset(dev, intel_crtc->pipe);
  4593. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4594. if (intel_crtc->config->has_pch_encoder)
  4595. intel_wait_for_vblank(dev_priv, pipe);
  4596. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4597. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4598. }
  4599. /* IPS only exists on ULT machines and is tied to pipe A. */
  4600. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4601. {
  4602. return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
  4603. }
  4604. static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
  4605. struct drm_atomic_state *old_state)
  4606. {
  4607. struct drm_crtc *crtc = pipe_config->base.crtc;
  4608. struct drm_device *dev = crtc->dev;
  4609. struct drm_i915_private *dev_priv = to_i915(dev);
  4610. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4611. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4612. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4613. if (WARN_ON(intel_crtc->active))
  4614. return;
  4615. if (intel_crtc->config->has_pch_encoder)
  4616. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4617. false);
  4618. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4619. if (intel_crtc->config->shared_dpll)
  4620. intel_enable_shared_dpll(intel_crtc);
  4621. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4622. intel_dp_set_m_n(intel_crtc, M1_N1);
  4623. if (!transcoder_is_dsi(cpu_transcoder))
  4624. intel_set_pipe_timings(intel_crtc);
  4625. intel_set_pipe_src_size(intel_crtc);
  4626. if (cpu_transcoder != TRANSCODER_EDP &&
  4627. !transcoder_is_dsi(cpu_transcoder)) {
  4628. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4629. intel_crtc->config->pixel_multiplier - 1);
  4630. }
  4631. if (intel_crtc->config->has_pch_encoder) {
  4632. intel_cpu_transcoder_set_m_n(intel_crtc,
  4633. &intel_crtc->config->fdi_m_n, NULL);
  4634. }
  4635. if (!transcoder_is_dsi(cpu_transcoder))
  4636. haswell_set_pipeconf(crtc);
  4637. haswell_set_pipemisc(crtc);
  4638. intel_color_set_csc(&pipe_config->base);
  4639. intel_crtc->active = true;
  4640. if (intel_crtc->config->has_pch_encoder)
  4641. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4642. else
  4643. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4644. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4645. if (intel_crtc->config->has_pch_encoder)
  4646. dev_priv->display.fdi_link_train(crtc);
  4647. if (!transcoder_is_dsi(cpu_transcoder))
  4648. intel_ddi_enable_pipe_clock(intel_crtc);
  4649. if (INTEL_INFO(dev)->gen >= 9)
  4650. skylake_pfit_enable(intel_crtc);
  4651. else
  4652. ironlake_pfit_enable(intel_crtc);
  4653. /*
  4654. * On ILK+ LUT must be loaded before the pipe is running but with
  4655. * clocks enabled
  4656. */
  4657. intel_color_load_luts(&pipe_config->base);
  4658. intel_ddi_set_pipe_settings(crtc);
  4659. if (!transcoder_is_dsi(cpu_transcoder))
  4660. intel_ddi_enable_transcoder_func(crtc);
  4661. if (dev_priv->display.initial_watermarks != NULL)
  4662. dev_priv->display.initial_watermarks(pipe_config);
  4663. else
  4664. intel_update_watermarks(intel_crtc);
  4665. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4666. if (!transcoder_is_dsi(cpu_transcoder))
  4667. intel_enable_pipe(intel_crtc);
  4668. if (intel_crtc->config->has_pch_encoder)
  4669. lpt_pch_enable(crtc);
  4670. if (intel_crtc->config->dp_encoder_is_mst)
  4671. intel_ddi_set_vc_payload_alloc(crtc, true);
  4672. assert_vblank_disabled(crtc);
  4673. drm_crtc_vblank_on(crtc);
  4674. intel_encoders_enable(crtc, pipe_config, old_state);
  4675. if (intel_crtc->config->has_pch_encoder) {
  4676. intel_wait_for_vblank(dev_priv, pipe);
  4677. intel_wait_for_vblank(dev_priv, pipe);
  4678. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4679. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4680. true);
  4681. }
  4682. /* If we change the relative order between pipe/planes enabling, we need
  4683. * to change the workaround. */
  4684. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4685. if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
  4686. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4687. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4688. }
  4689. }
  4690. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4691. {
  4692. struct drm_device *dev = crtc->base.dev;
  4693. struct drm_i915_private *dev_priv = to_i915(dev);
  4694. int pipe = crtc->pipe;
  4695. /* To avoid upsetting the power well on haswell only disable the pfit if
  4696. * it's in use. The hw state code will make sure we get this right. */
  4697. if (force || crtc->config->pch_pfit.enabled) {
  4698. I915_WRITE(PF_CTL(pipe), 0);
  4699. I915_WRITE(PF_WIN_POS(pipe), 0);
  4700. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4701. }
  4702. }
  4703. static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4704. struct drm_atomic_state *old_state)
  4705. {
  4706. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4707. struct drm_device *dev = crtc->dev;
  4708. struct drm_i915_private *dev_priv = to_i915(dev);
  4709. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4710. int pipe = intel_crtc->pipe;
  4711. /*
  4712. * Sometimes spurious CPU pipe underruns happen when the
  4713. * pipe is already disabled, but FDI RX/TX is still enabled.
  4714. * Happens at least with VGA+HDMI cloning. Suppress them.
  4715. */
  4716. if (intel_crtc->config->has_pch_encoder) {
  4717. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4718. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4719. }
  4720. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4721. drm_crtc_vblank_off(crtc);
  4722. assert_vblank_disabled(crtc);
  4723. intel_disable_pipe(intel_crtc);
  4724. ironlake_pfit_disable(intel_crtc, false);
  4725. if (intel_crtc->config->has_pch_encoder)
  4726. ironlake_fdi_disable(crtc);
  4727. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4728. if (intel_crtc->config->has_pch_encoder) {
  4729. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4730. if (HAS_PCH_CPT(dev_priv)) {
  4731. i915_reg_t reg;
  4732. u32 temp;
  4733. /* disable TRANS_DP_CTL */
  4734. reg = TRANS_DP_CTL(pipe);
  4735. temp = I915_READ(reg);
  4736. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4737. TRANS_DP_PORT_SEL_MASK);
  4738. temp |= TRANS_DP_PORT_SEL_NONE;
  4739. I915_WRITE(reg, temp);
  4740. /* disable DPLL_SEL */
  4741. temp = I915_READ(PCH_DPLL_SEL);
  4742. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4743. I915_WRITE(PCH_DPLL_SEL, temp);
  4744. }
  4745. ironlake_fdi_pll_disable(intel_crtc);
  4746. }
  4747. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4748. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4749. }
  4750. static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4751. struct drm_atomic_state *old_state)
  4752. {
  4753. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4754. struct drm_device *dev = crtc->dev;
  4755. struct drm_i915_private *dev_priv = to_i915(dev);
  4756. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4757. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4758. if (intel_crtc->config->has_pch_encoder)
  4759. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4760. false);
  4761. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4762. drm_crtc_vblank_off(crtc);
  4763. assert_vblank_disabled(crtc);
  4764. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4765. if (!transcoder_is_dsi(cpu_transcoder))
  4766. intel_disable_pipe(intel_crtc);
  4767. if (intel_crtc->config->dp_encoder_is_mst)
  4768. intel_ddi_set_vc_payload_alloc(crtc, false);
  4769. if (!transcoder_is_dsi(cpu_transcoder))
  4770. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4771. if (INTEL_INFO(dev)->gen >= 9)
  4772. skylake_scaler_disable(intel_crtc);
  4773. else
  4774. ironlake_pfit_disable(intel_crtc, false);
  4775. if (!transcoder_is_dsi(cpu_transcoder))
  4776. intel_ddi_disable_pipe_clock(intel_crtc);
  4777. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4778. if (old_crtc_state->has_pch_encoder)
  4779. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4780. true);
  4781. }
  4782. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4783. {
  4784. struct drm_device *dev = crtc->base.dev;
  4785. struct drm_i915_private *dev_priv = to_i915(dev);
  4786. struct intel_crtc_state *pipe_config = crtc->config;
  4787. if (!pipe_config->gmch_pfit.control)
  4788. return;
  4789. /*
  4790. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4791. * according to register description and PRM.
  4792. */
  4793. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4794. assert_pipe_disabled(dev_priv, crtc->pipe);
  4795. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4796. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4797. /* Border color in case we don't scale up to the full screen. Black by
  4798. * default, change to something else for debugging. */
  4799. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4800. }
  4801. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4802. {
  4803. switch (port) {
  4804. case PORT_A:
  4805. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4806. case PORT_B:
  4807. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4808. case PORT_C:
  4809. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4810. case PORT_D:
  4811. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4812. case PORT_E:
  4813. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4814. default:
  4815. MISSING_CASE(port);
  4816. return POWER_DOMAIN_PORT_OTHER;
  4817. }
  4818. }
  4819. static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
  4820. {
  4821. switch (port) {
  4822. case PORT_A:
  4823. return POWER_DOMAIN_AUX_A;
  4824. case PORT_B:
  4825. return POWER_DOMAIN_AUX_B;
  4826. case PORT_C:
  4827. return POWER_DOMAIN_AUX_C;
  4828. case PORT_D:
  4829. return POWER_DOMAIN_AUX_D;
  4830. case PORT_E:
  4831. /* FIXME: Check VBT for actual wiring of PORT E */
  4832. return POWER_DOMAIN_AUX_D;
  4833. default:
  4834. MISSING_CASE(port);
  4835. return POWER_DOMAIN_AUX_A;
  4836. }
  4837. }
  4838. enum intel_display_power_domain
  4839. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4840. {
  4841. struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
  4842. struct intel_digital_port *intel_dig_port;
  4843. switch (intel_encoder->type) {
  4844. case INTEL_OUTPUT_UNKNOWN:
  4845. /* Only DDI platforms should ever use this output type */
  4846. WARN_ON_ONCE(!HAS_DDI(dev_priv));
  4847. case INTEL_OUTPUT_DP:
  4848. case INTEL_OUTPUT_HDMI:
  4849. case INTEL_OUTPUT_EDP:
  4850. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4851. return port_to_power_domain(intel_dig_port->port);
  4852. case INTEL_OUTPUT_DP_MST:
  4853. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4854. return port_to_power_domain(intel_dig_port->port);
  4855. case INTEL_OUTPUT_ANALOG:
  4856. return POWER_DOMAIN_PORT_CRT;
  4857. case INTEL_OUTPUT_DSI:
  4858. return POWER_DOMAIN_PORT_DSI;
  4859. default:
  4860. return POWER_DOMAIN_PORT_OTHER;
  4861. }
  4862. }
  4863. enum intel_display_power_domain
  4864. intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
  4865. {
  4866. struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
  4867. struct intel_digital_port *intel_dig_port;
  4868. switch (intel_encoder->type) {
  4869. case INTEL_OUTPUT_UNKNOWN:
  4870. case INTEL_OUTPUT_HDMI:
  4871. /*
  4872. * Only DDI platforms should ever use these output types.
  4873. * We can get here after the HDMI detect code has already set
  4874. * the type of the shared encoder. Since we can't be sure
  4875. * what's the status of the given connectors, play safe and
  4876. * run the DP detection too.
  4877. */
  4878. WARN_ON_ONCE(!HAS_DDI(dev_priv));
  4879. case INTEL_OUTPUT_DP:
  4880. case INTEL_OUTPUT_EDP:
  4881. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4882. return port_to_aux_power_domain(intel_dig_port->port);
  4883. case INTEL_OUTPUT_DP_MST:
  4884. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4885. return port_to_aux_power_domain(intel_dig_port->port);
  4886. default:
  4887. MISSING_CASE(intel_encoder->type);
  4888. return POWER_DOMAIN_AUX_A;
  4889. }
  4890. }
  4891. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
  4892. struct intel_crtc_state *crtc_state)
  4893. {
  4894. struct drm_device *dev = crtc->dev;
  4895. struct drm_encoder *encoder;
  4896. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4897. enum pipe pipe = intel_crtc->pipe;
  4898. unsigned long mask;
  4899. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4900. if (!crtc_state->base.active)
  4901. return 0;
  4902. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4903. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4904. if (crtc_state->pch_pfit.enabled ||
  4905. crtc_state->pch_pfit.force_thru)
  4906. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4907. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4908. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4909. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4910. }
  4911. if (crtc_state->shared_dpll)
  4912. mask |= BIT(POWER_DOMAIN_PLLS);
  4913. return mask;
  4914. }
  4915. static unsigned long
  4916. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4917. struct intel_crtc_state *crtc_state)
  4918. {
  4919. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4920. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4921. enum intel_display_power_domain domain;
  4922. unsigned long domains, new_domains, old_domains;
  4923. old_domains = intel_crtc->enabled_power_domains;
  4924. intel_crtc->enabled_power_domains = new_domains =
  4925. get_crtc_power_domains(crtc, crtc_state);
  4926. domains = new_domains & ~old_domains;
  4927. for_each_power_domain(domain, domains)
  4928. intel_display_power_get(dev_priv, domain);
  4929. return old_domains & ~new_domains;
  4930. }
  4931. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4932. unsigned long domains)
  4933. {
  4934. enum intel_display_power_domain domain;
  4935. for_each_power_domain(domain, domains)
  4936. intel_display_power_put(dev_priv, domain);
  4937. }
  4938. static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
  4939. {
  4940. int max_cdclk_freq = dev_priv->max_cdclk_freq;
  4941. if (INTEL_INFO(dev_priv)->gen >= 9 ||
  4942. IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  4943. return max_cdclk_freq;
  4944. else if (IS_CHERRYVIEW(dev_priv))
  4945. return max_cdclk_freq*95/100;
  4946. else if (INTEL_INFO(dev_priv)->gen < 4)
  4947. return 2*max_cdclk_freq*90/100;
  4948. else
  4949. return max_cdclk_freq*90/100;
  4950. }
  4951. static int skl_calc_cdclk(int max_pixclk, int vco);
  4952. static void intel_update_max_cdclk(struct drm_device *dev)
  4953. {
  4954. struct drm_i915_private *dev_priv = to_i915(dev);
  4955. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
  4956. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4957. int max_cdclk, vco;
  4958. vco = dev_priv->skl_preferred_vco_freq;
  4959. WARN_ON(vco != 8100000 && vco != 8640000);
  4960. /*
  4961. * Use the lower (vco 8640) cdclk values as a
  4962. * first guess. skl_calc_cdclk() will correct it
  4963. * if the preferred vco is 8100 instead.
  4964. */
  4965. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4966. max_cdclk = 617143;
  4967. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4968. max_cdclk = 540000;
  4969. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4970. max_cdclk = 432000;
  4971. else
  4972. max_cdclk = 308571;
  4973. dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
  4974. } else if (IS_BROXTON(dev_priv)) {
  4975. dev_priv->max_cdclk_freq = 624000;
  4976. } else if (IS_BROADWELL(dev_priv)) {
  4977. /*
  4978. * FIXME with extra cooling we can allow
  4979. * 540 MHz for ULX and 675 Mhz for ULT.
  4980. * How can we know if extra cooling is
  4981. * available? PCI ID, VTB, something else?
  4982. */
  4983. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4984. dev_priv->max_cdclk_freq = 450000;
  4985. else if (IS_BDW_ULX(dev_priv))
  4986. dev_priv->max_cdclk_freq = 450000;
  4987. else if (IS_BDW_ULT(dev_priv))
  4988. dev_priv->max_cdclk_freq = 540000;
  4989. else
  4990. dev_priv->max_cdclk_freq = 675000;
  4991. } else if (IS_CHERRYVIEW(dev_priv)) {
  4992. dev_priv->max_cdclk_freq = 320000;
  4993. } else if (IS_VALLEYVIEW(dev_priv)) {
  4994. dev_priv->max_cdclk_freq = 400000;
  4995. } else {
  4996. /* otherwise assume cdclk is fixed */
  4997. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4998. }
  4999. dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
  5000. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  5001. dev_priv->max_cdclk_freq);
  5002. DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
  5003. dev_priv->max_dotclk_freq);
  5004. }
  5005. static void intel_update_cdclk(struct drm_device *dev)
  5006. {
  5007. struct drm_i915_private *dev_priv = to_i915(dev);
  5008. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
  5009. if (INTEL_GEN(dev_priv) >= 9)
  5010. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
  5011. dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
  5012. dev_priv->cdclk_pll.ref);
  5013. else
  5014. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  5015. dev_priv->cdclk_freq);
  5016. /*
  5017. * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
  5018. * Programmng [sic] note: bit[9:2] should be programmed to the number
  5019. * of cdclk that generates 4MHz reference clock freq which is used to
  5020. * generate GMBus clock. This will vary with the cdclk freq.
  5021. */
  5022. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  5023. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  5024. }
  5025. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  5026. static int skl_cdclk_decimal(int cdclk)
  5027. {
  5028. return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
  5029. }
  5030. static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
  5031. {
  5032. int ratio;
  5033. if (cdclk == dev_priv->cdclk_pll.ref)
  5034. return 0;
  5035. switch (cdclk) {
  5036. default:
  5037. MISSING_CASE(cdclk);
  5038. case 144000:
  5039. case 288000:
  5040. case 384000:
  5041. case 576000:
  5042. ratio = 60;
  5043. break;
  5044. case 624000:
  5045. ratio = 65;
  5046. break;
  5047. }
  5048. return dev_priv->cdclk_pll.ref * ratio;
  5049. }
  5050. static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
  5051. {
  5052. I915_WRITE(BXT_DE_PLL_ENABLE, 0);
  5053. /* Timeout 200us */
  5054. if (intel_wait_for_register(dev_priv,
  5055. BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
  5056. 1))
  5057. DRM_ERROR("timeout waiting for DE PLL unlock\n");
  5058. dev_priv->cdclk_pll.vco = 0;
  5059. }
  5060. static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
  5061. {
  5062. int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
  5063. u32 val;
  5064. val = I915_READ(BXT_DE_PLL_CTL);
  5065. val &= ~BXT_DE_PLL_RATIO_MASK;
  5066. val |= BXT_DE_PLL_RATIO(ratio);
  5067. I915_WRITE(BXT_DE_PLL_CTL, val);
  5068. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  5069. /* Timeout 200us */
  5070. if (intel_wait_for_register(dev_priv,
  5071. BXT_DE_PLL_ENABLE,
  5072. BXT_DE_PLL_LOCK,
  5073. BXT_DE_PLL_LOCK,
  5074. 1))
  5075. DRM_ERROR("timeout waiting for DE PLL lock\n");
  5076. dev_priv->cdclk_pll.vco = vco;
  5077. }
  5078. static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
  5079. {
  5080. u32 val, divider;
  5081. int vco, ret;
  5082. vco = bxt_de_pll_vco(dev_priv, cdclk);
  5083. DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
  5084. /* cdclk = vco / 2 / div{1,1.5,2,4} */
  5085. switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
  5086. case 8:
  5087. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  5088. break;
  5089. case 4:
  5090. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  5091. break;
  5092. case 3:
  5093. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  5094. break;
  5095. case 2:
  5096. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  5097. break;
  5098. default:
  5099. WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
  5100. WARN_ON(vco != 0);
  5101. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  5102. break;
  5103. }
  5104. /* Inform power controller of upcoming frequency change */
  5105. mutex_lock(&dev_priv->rps.hw_lock);
  5106. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  5107. 0x80000000);
  5108. mutex_unlock(&dev_priv->rps.hw_lock);
  5109. if (ret) {
  5110. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  5111. ret, cdclk);
  5112. return;
  5113. }
  5114. if (dev_priv->cdclk_pll.vco != 0 &&
  5115. dev_priv->cdclk_pll.vco != vco)
  5116. bxt_de_pll_disable(dev_priv);
  5117. if (dev_priv->cdclk_pll.vco != vco)
  5118. bxt_de_pll_enable(dev_priv, vco);
  5119. val = divider | skl_cdclk_decimal(cdclk);
  5120. /*
  5121. * FIXME if only the cd2x divider needs changing, it could be done
  5122. * without shutting off the pipe (if only one pipe is active).
  5123. */
  5124. val |= BXT_CDCLK_CD2X_PIPE_NONE;
  5125. /*
  5126. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  5127. * enable otherwise.
  5128. */
  5129. if (cdclk >= 500000)
  5130. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  5131. I915_WRITE(CDCLK_CTL, val);
  5132. mutex_lock(&dev_priv->rps.hw_lock);
  5133. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  5134. DIV_ROUND_UP(cdclk, 25000));
  5135. mutex_unlock(&dev_priv->rps.hw_lock);
  5136. if (ret) {
  5137. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  5138. ret, cdclk);
  5139. return;
  5140. }
  5141. intel_update_cdclk(&dev_priv->drm);
  5142. }
  5143. static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
  5144. {
  5145. u32 cdctl, expected;
  5146. intel_update_cdclk(&dev_priv->drm);
  5147. if (dev_priv->cdclk_pll.vco == 0 ||
  5148. dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
  5149. goto sanitize;
  5150. /* DPLL okay; verify the cdclock
  5151. *
  5152. * Some BIOS versions leave an incorrect decimal frequency value and
  5153. * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
  5154. * so sanitize this register.
  5155. */
  5156. cdctl = I915_READ(CDCLK_CTL);
  5157. /*
  5158. * Let's ignore the pipe field, since BIOS could have configured the
  5159. * dividers both synching to an active pipe, or asynchronously
  5160. * (PIPE_NONE).
  5161. */
  5162. cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
  5163. expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
  5164. skl_cdclk_decimal(dev_priv->cdclk_freq);
  5165. /*
  5166. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  5167. * enable otherwise.
  5168. */
  5169. if (dev_priv->cdclk_freq >= 500000)
  5170. expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  5171. if (cdctl == expected)
  5172. /* All well; nothing to sanitize */
  5173. return;
  5174. sanitize:
  5175. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  5176. /* force cdclk programming */
  5177. dev_priv->cdclk_freq = 0;
  5178. /* force full PLL disable + enable */
  5179. dev_priv->cdclk_pll.vco = -1;
  5180. }
  5181. void bxt_init_cdclk(struct drm_i915_private *dev_priv)
  5182. {
  5183. bxt_sanitize_cdclk(dev_priv);
  5184. if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
  5185. return;
  5186. /*
  5187. * FIXME:
  5188. * - The initial CDCLK needs to be read from VBT.
  5189. * Need to make this change after VBT has changes for BXT.
  5190. */
  5191. bxt_set_cdclk(dev_priv, bxt_calc_cdclk(0));
  5192. }
  5193. void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
  5194. {
  5195. bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
  5196. }
  5197. static int skl_calc_cdclk(int max_pixclk, int vco)
  5198. {
  5199. if (vco == 8640000) {
  5200. if (max_pixclk > 540000)
  5201. return 617143;
  5202. else if (max_pixclk > 432000)
  5203. return 540000;
  5204. else if (max_pixclk > 308571)
  5205. return 432000;
  5206. else
  5207. return 308571;
  5208. } else {
  5209. if (max_pixclk > 540000)
  5210. return 675000;
  5211. else if (max_pixclk > 450000)
  5212. return 540000;
  5213. else if (max_pixclk > 337500)
  5214. return 450000;
  5215. else
  5216. return 337500;
  5217. }
  5218. }
  5219. static void
  5220. skl_dpll0_update(struct drm_i915_private *dev_priv)
  5221. {
  5222. u32 val;
  5223. dev_priv->cdclk_pll.ref = 24000;
  5224. dev_priv->cdclk_pll.vco = 0;
  5225. val = I915_READ(LCPLL1_CTL);
  5226. if ((val & LCPLL_PLL_ENABLE) == 0)
  5227. return;
  5228. if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
  5229. return;
  5230. val = I915_READ(DPLL_CTRL1);
  5231. if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
  5232. DPLL_CTRL1_SSC(SKL_DPLL0) |
  5233. DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
  5234. DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
  5235. return;
  5236. switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
  5237. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
  5238. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
  5239. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
  5240. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
  5241. dev_priv->cdclk_pll.vco = 8100000;
  5242. break;
  5243. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
  5244. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
  5245. dev_priv->cdclk_pll.vco = 8640000;
  5246. break;
  5247. default:
  5248. MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  5249. break;
  5250. }
  5251. }
  5252. void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
  5253. {
  5254. bool changed = dev_priv->skl_preferred_vco_freq != vco;
  5255. dev_priv->skl_preferred_vco_freq = vco;
  5256. if (changed)
  5257. intel_update_max_cdclk(&dev_priv->drm);
  5258. }
  5259. static void
  5260. skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
  5261. {
  5262. int min_cdclk = skl_calc_cdclk(0, vco);
  5263. u32 val;
  5264. WARN_ON(vco != 8100000 && vco != 8640000);
  5265. /* select the minimum CDCLK before enabling DPLL 0 */
  5266. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
  5267. I915_WRITE(CDCLK_CTL, val);
  5268. POSTING_READ(CDCLK_CTL);
  5269. /*
  5270. * We always enable DPLL0 with the lowest link rate possible, but still
  5271. * taking into account the VCO required to operate the eDP panel at the
  5272. * desired frequency. The usual DP link rates operate with a VCO of
  5273. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  5274. * The modeset code is responsible for the selection of the exact link
  5275. * rate later on, with the constraint of choosing a frequency that
  5276. * works with vco.
  5277. */
  5278. val = I915_READ(DPLL_CTRL1);
  5279. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  5280. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  5281. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  5282. if (vco == 8640000)
  5283. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  5284. SKL_DPLL0);
  5285. else
  5286. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  5287. SKL_DPLL0);
  5288. I915_WRITE(DPLL_CTRL1, val);
  5289. POSTING_READ(DPLL_CTRL1);
  5290. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  5291. if (intel_wait_for_register(dev_priv,
  5292. LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  5293. 5))
  5294. DRM_ERROR("DPLL0 not locked\n");
  5295. dev_priv->cdclk_pll.vco = vco;
  5296. /* We'll want to keep using the current vco from now on. */
  5297. skl_set_preferred_cdclk_vco(dev_priv, vco);
  5298. }
  5299. static void
  5300. skl_dpll0_disable(struct drm_i915_private *dev_priv)
  5301. {
  5302. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  5303. if (intel_wait_for_register(dev_priv,
  5304. LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
  5305. 1))
  5306. DRM_ERROR("Couldn't disable DPLL0\n");
  5307. dev_priv->cdclk_pll.vco = 0;
  5308. }
  5309. static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
  5310. {
  5311. int ret;
  5312. u32 val;
  5313. /* inform PCU we want to change CDCLK */
  5314. val = SKL_CDCLK_PREPARE_FOR_CHANGE;
  5315. mutex_lock(&dev_priv->rps.hw_lock);
  5316. ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
  5317. mutex_unlock(&dev_priv->rps.hw_lock);
  5318. return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
  5319. }
  5320. static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
  5321. {
  5322. return _wait_for(skl_cdclk_pcu_ready(dev_priv), 3000, 10) == 0;
  5323. }
  5324. static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
  5325. {
  5326. struct drm_device *dev = &dev_priv->drm;
  5327. u32 freq_select, pcu_ack;
  5328. WARN_ON((cdclk == 24000) != (vco == 0));
  5329. DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
  5330. if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
  5331. DRM_ERROR("failed to inform PCU about cdclk change\n");
  5332. return;
  5333. }
  5334. /* set CDCLK_CTL */
  5335. switch (cdclk) {
  5336. case 450000:
  5337. case 432000:
  5338. freq_select = CDCLK_FREQ_450_432;
  5339. pcu_ack = 1;
  5340. break;
  5341. case 540000:
  5342. freq_select = CDCLK_FREQ_540;
  5343. pcu_ack = 2;
  5344. break;
  5345. case 308571:
  5346. case 337500:
  5347. default:
  5348. freq_select = CDCLK_FREQ_337_308;
  5349. pcu_ack = 0;
  5350. break;
  5351. case 617143:
  5352. case 675000:
  5353. freq_select = CDCLK_FREQ_675_617;
  5354. pcu_ack = 3;
  5355. break;
  5356. }
  5357. if (dev_priv->cdclk_pll.vco != 0 &&
  5358. dev_priv->cdclk_pll.vco != vco)
  5359. skl_dpll0_disable(dev_priv);
  5360. if (dev_priv->cdclk_pll.vco != vco)
  5361. skl_dpll0_enable(dev_priv, vco);
  5362. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
  5363. POSTING_READ(CDCLK_CTL);
  5364. /* inform PCU of the change */
  5365. mutex_lock(&dev_priv->rps.hw_lock);
  5366. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  5367. mutex_unlock(&dev_priv->rps.hw_lock);
  5368. intel_update_cdclk(dev);
  5369. }
  5370. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
  5371. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  5372. {
  5373. skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
  5374. }
  5375. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  5376. {
  5377. int cdclk, vco;
  5378. skl_sanitize_cdclk(dev_priv);
  5379. if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
  5380. /*
  5381. * Use the current vco as our initial
  5382. * guess as to what the preferred vco is.
  5383. */
  5384. if (dev_priv->skl_preferred_vco_freq == 0)
  5385. skl_set_preferred_cdclk_vco(dev_priv,
  5386. dev_priv->cdclk_pll.vco);
  5387. return;
  5388. }
  5389. vco = dev_priv->skl_preferred_vco_freq;
  5390. if (vco == 0)
  5391. vco = 8100000;
  5392. cdclk = skl_calc_cdclk(0, vco);
  5393. skl_set_cdclk(dev_priv, cdclk, vco);
  5394. }
  5395. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
  5396. {
  5397. uint32_t cdctl, expected;
  5398. /*
  5399. * check if the pre-os intialized the display
  5400. * There is SWF18 scratchpad register defined which is set by the
  5401. * pre-os which can be used by the OS drivers to check the status
  5402. */
  5403. if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
  5404. goto sanitize;
  5405. intel_update_cdclk(&dev_priv->drm);
  5406. /* Is PLL enabled and locked ? */
  5407. if (dev_priv->cdclk_pll.vco == 0 ||
  5408. dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
  5409. goto sanitize;
  5410. /* DPLL okay; verify the cdclock
  5411. *
  5412. * Noticed in some instances that the freq selection is correct but
  5413. * decimal part is programmed wrong from BIOS where pre-os does not
  5414. * enable display. Verify the same as well.
  5415. */
  5416. cdctl = I915_READ(CDCLK_CTL);
  5417. expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
  5418. skl_cdclk_decimal(dev_priv->cdclk_freq);
  5419. if (cdctl == expected)
  5420. /* All well; nothing to sanitize */
  5421. return;
  5422. sanitize:
  5423. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  5424. /* force cdclk programming */
  5425. dev_priv->cdclk_freq = 0;
  5426. /* force full PLL disable + enable */
  5427. dev_priv->cdclk_pll.vco = -1;
  5428. }
  5429. /* Adjust CDclk dividers to allow high res or save power if possible */
  5430. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  5431. {
  5432. struct drm_i915_private *dev_priv = to_i915(dev);
  5433. u32 val, cmd;
  5434. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  5435. != dev_priv->cdclk_freq);
  5436. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  5437. cmd = 2;
  5438. else if (cdclk == 266667)
  5439. cmd = 1;
  5440. else
  5441. cmd = 0;
  5442. mutex_lock(&dev_priv->rps.hw_lock);
  5443. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  5444. val &= ~DSPFREQGUAR_MASK;
  5445. val |= (cmd << DSPFREQGUAR_SHIFT);
  5446. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  5447. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  5448. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  5449. 50)) {
  5450. DRM_ERROR("timed out waiting for CDclk change\n");
  5451. }
  5452. mutex_unlock(&dev_priv->rps.hw_lock);
  5453. mutex_lock(&dev_priv->sb_lock);
  5454. if (cdclk == 400000) {
  5455. u32 divider;
  5456. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  5457. /* adjust cdclk divider */
  5458. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  5459. val &= ~CCK_FREQUENCY_VALUES;
  5460. val |= divider;
  5461. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  5462. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  5463. CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
  5464. 50))
  5465. DRM_ERROR("timed out waiting for CDclk change\n");
  5466. }
  5467. /* adjust self-refresh exit latency value */
  5468. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  5469. val &= ~0x7f;
  5470. /*
  5471. * For high bandwidth configs, we set a higher latency in the bunit
  5472. * so that the core display fetch happens in time to avoid underruns.
  5473. */
  5474. if (cdclk == 400000)
  5475. val |= 4500 / 250; /* 4.5 usec */
  5476. else
  5477. val |= 3000 / 250; /* 3.0 usec */
  5478. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  5479. mutex_unlock(&dev_priv->sb_lock);
  5480. intel_update_cdclk(dev);
  5481. }
  5482. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  5483. {
  5484. struct drm_i915_private *dev_priv = to_i915(dev);
  5485. u32 val, cmd;
  5486. WARN_ON(dev_priv->display.get_display_clock_speed(dev)
  5487. != dev_priv->cdclk_freq);
  5488. switch (cdclk) {
  5489. case 333333:
  5490. case 320000:
  5491. case 266667:
  5492. case 200000:
  5493. break;
  5494. default:
  5495. MISSING_CASE(cdclk);
  5496. return;
  5497. }
  5498. /*
  5499. * Specs are full of misinformation, but testing on actual
  5500. * hardware has shown that we just need to write the desired
  5501. * CCK divider into the Punit register.
  5502. */
  5503. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  5504. mutex_lock(&dev_priv->rps.hw_lock);
  5505. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  5506. val &= ~DSPFREQGUAR_MASK_CHV;
  5507. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  5508. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  5509. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  5510. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  5511. 50)) {
  5512. DRM_ERROR("timed out waiting for CDclk change\n");
  5513. }
  5514. mutex_unlock(&dev_priv->rps.hw_lock);
  5515. intel_update_cdclk(dev);
  5516. }
  5517. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  5518. int max_pixclk)
  5519. {
  5520. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  5521. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  5522. /*
  5523. * Really only a few cases to deal with, as only 4 CDclks are supported:
  5524. * 200MHz
  5525. * 267MHz
  5526. * 320/333MHz (depends on HPLL freq)
  5527. * 400MHz (VLV only)
  5528. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  5529. * of the lower bin and adjust if needed.
  5530. *
  5531. * We seem to get an unstable or solid color picture at 200MHz.
  5532. * Not sure what's wrong. For now use 200MHz only when all pipes
  5533. * are off.
  5534. */
  5535. if (!IS_CHERRYVIEW(dev_priv) &&
  5536. max_pixclk > freq_320*limit/100)
  5537. return 400000;
  5538. else if (max_pixclk > 266667*limit/100)
  5539. return freq_320;
  5540. else if (max_pixclk > 0)
  5541. return 266667;
  5542. else
  5543. return 200000;
  5544. }
  5545. static int bxt_calc_cdclk(int max_pixclk)
  5546. {
  5547. if (max_pixclk > 576000)
  5548. return 624000;
  5549. else if (max_pixclk > 384000)
  5550. return 576000;
  5551. else if (max_pixclk > 288000)
  5552. return 384000;
  5553. else if (max_pixclk > 144000)
  5554. return 288000;
  5555. else
  5556. return 144000;
  5557. }
  5558. /* Compute the max pixel clock for new configuration. */
  5559. static int intel_mode_max_pixclk(struct drm_device *dev,
  5560. struct drm_atomic_state *state)
  5561. {
  5562. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  5563. struct drm_i915_private *dev_priv = to_i915(dev);
  5564. struct drm_crtc *crtc;
  5565. struct drm_crtc_state *crtc_state;
  5566. unsigned max_pixclk = 0, i;
  5567. enum pipe pipe;
  5568. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  5569. sizeof(intel_state->min_pixclk));
  5570. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  5571. int pixclk = 0;
  5572. if (crtc_state->enable)
  5573. pixclk = crtc_state->adjusted_mode.crtc_clock;
  5574. intel_state->min_pixclk[i] = pixclk;
  5575. }
  5576. for_each_pipe(dev_priv, pipe)
  5577. max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
  5578. return max_pixclk;
  5579. }
  5580. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  5581. {
  5582. struct drm_device *dev = state->dev;
  5583. struct drm_i915_private *dev_priv = to_i915(dev);
  5584. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5585. struct intel_atomic_state *intel_state =
  5586. to_intel_atomic_state(state);
  5587. intel_state->cdclk = intel_state->dev_cdclk =
  5588. valleyview_calc_cdclk(dev_priv, max_pixclk);
  5589. if (!intel_state->active_crtcs)
  5590. intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
  5591. return 0;
  5592. }
  5593. static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
  5594. {
  5595. int max_pixclk = ilk_max_pixel_rate(state);
  5596. struct intel_atomic_state *intel_state =
  5597. to_intel_atomic_state(state);
  5598. intel_state->cdclk = intel_state->dev_cdclk =
  5599. bxt_calc_cdclk(max_pixclk);
  5600. if (!intel_state->active_crtcs)
  5601. intel_state->dev_cdclk = bxt_calc_cdclk(0);
  5602. return 0;
  5603. }
  5604. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5605. {
  5606. unsigned int credits, default_credits;
  5607. if (IS_CHERRYVIEW(dev_priv))
  5608. default_credits = PFI_CREDIT(12);
  5609. else
  5610. default_credits = PFI_CREDIT(8);
  5611. if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
  5612. /* CHV suggested value is 31 or 63 */
  5613. if (IS_CHERRYVIEW(dev_priv))
  5614. credits = PFI_CREDIT_63;
  5615. else
  5616. credits = PFI_CREDIT(15);
  5617. } else {
  5618. credits = default_credits;
  5619. }
  5620. /*
  5621. * WA - write default credits before re-programming
  5622. * FIXME: should we also set the resend bit here?
  5623. */
  5624. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5625. default_credits);
  5626. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5627. credits | PFI_CREDIT_RESEND);
  5628. /*
  5629. * FIXME is this guaranteed to clear
  5630. * immediately or should we poll for it?
  5631. */
  5632. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5633. }
  5634. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5635. {
  5636. struct drm_device *dev = old_state->dev;
  5637. struct drm_i915_private *dev_priv = to_i915(dev);
  5638. struct intel_atomic_state *old_intel_state =
  5639. to_intel_atomic_state(old_state);
  5640. unsigned req_cdclk = old_intel_state->dev_cdclk;
  5641. /*
  5642. * FIXME: We can end up here with all power domains off, yet
  5643. * with a CDCLK frequency other than the minimum. To account
  5644. * for this take the PIPE-A power domain, which covers the HW
  5645. * blocks needed for the following programming. This can be
  5646. * removed once it's guaranteed that we get here either with
  5647. * the minimum CDCLK set, or the required power domains
  5648. * enabled.
  5649. */
  5650. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5651. if (IS_CHERRYVIEW(dev_priv))
  5652. cherryview_set_cdclk(dev, req_cdclk);
  5653. else
  5654. valleyview_set_cdclk(dev, req_cdclk);
  5655. vlv_program_pfi_credits(dev_priv);
  5656. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5657. }
  5658. static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
  5659. struct drm_atomic_state *old_state)
  5660. {
  5661. struct drm_crtc *crtc = pipe_config->base.crtc;
  5662. struct drm_device *dev = crtc->dev;
  5663. struct drm_i915_private *dev_priv = to_i915(dev);
  5664. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5665. int pipe = intel_crtc->pipe;
  5666. if (WARN_ON(intel_crtc->active))
  5667. return;
  5668. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5669. intel_dp_set_m_n(intel_crtc, M1_N1);
  5670. intel_set_pipe_timings(intel_crtc);
  5671. intel_set_pipe_src_size(intel_crtc);
  5672. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  5673. struct drm_i915_private *dev_priv = to_i915(dev);
  5674. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5675. I915_WRITE(CHV_CANVAS(pipe), 0);
  5676. }
  5677. i9xx_set_pipeconf(intel_crtc);
  5678. intel_crtc->active = true;
  5679. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5680. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  5681. if (IS_CHERRYVIEW(dev_priv)) {
  5682. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5683. chv_enable_pll(intel_crtc, intel_crtc->config);
  5684. } else {
  5685. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5686. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5687. }
  5688. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  5689. i9xx_pfit_enable(intel_crtc);
  5690. intel_color_load_luts(&pipe_config->base);
  5691. intel_update_watermarks(intel_crtc);
  5692. intel_enable_pipe(intel_crtc);
  5693. assert_vblank_disabled(crtc);
  5694. drm_crtc_vblank_on(crtc);
  5695. intel_encoders_enable(crtc, pipe_config, old_state);
  5696. }
  5697. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5698. {
  5699. struct drm_device *dev = crtc->base.dev;
  5700. struct drm_i915_private *dev_priv = to_i915(dev);
  5701. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5702. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5703. }
  5704. static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
  5705. struct drm_atomic_state *old_state)
  5706. {
  5707. struct drm_crtc *crtc = pipe_config->base.crtc;
  5708. struct drm_device *dev = crtc->dev;
  5709. struct drm_i915_private *dev_priv = to_i915(dev);
  5710. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5711. enum pipe pipe = intel_crtc->pipe;
  5712. if (WARN_ON(intel_crtc->active))
  5713. return;
  5714. i9xx_set_pll_dividers(intel_crtc);
  5715. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5716. intel_dp_set_m_n(intel_crtc, M1_N1);
  5717. intel_set_pipe_timings(intel_crtc);
  5718. intel_set_pipe_src_size(intel_crtc);
  5719. i9xx_set_pipeconf(intel_crtc);
  5720. intel_crtc->active = true;
  5721. if (!IS_GEN2(dev_priv))
  5722. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5723. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  5724. i9xx_enable_pll(intel_crtc);
  5725. i9xx_pfit_enable(intel_crtc);
  5726. intel_color_load_luts(&pipe_config->base);
  5727. intel_update_watermarks(intel_crtc);
  5728. intel_enable_pipe(intel_crtc);
  5729. assert_vblank_disabled(crtc);
  5730. drm_crtc_vblank_on(crtc);
  5731. intel_encoders_enable(crtc, pipe_config, old_state);
  5732. }
  5733. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5734. {
  5735. struct drm_device *dev = crtc->base.dev;
  5736. struct drm_i915_private *dev_priv = to_i915(dev);
  5737. if (!crtc->config->gmch_pfit.control)
  5738. return;
  5739. assert_pipe_disabled(dev_priv, crtc->pipe);
  5740. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5741. I915_READ(PFIT_CONTROL));
  5742. I915_WRITE(PFIT_CONTROL, 0);
  5743. }
  5744. static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
  5745. struct drm_atomic_state *old_state)
  5746. {
  5747. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  5748. struct drm_device *dev = crtc->dev;
  5749. struct drm_i915_private *dev_priv = to_i915(dev);
  5750. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5751. int pipe = intel_crtc->pipe;
  5752. /*
  5753. * On gen2 planes are double buffered but the pipe isn't, so we must
  5754. * wait for planes to fully turn off before disabling the pipe.
  5755. */
  5756. if (IS_GEN2(dev_priv))
  5757. intel_wait_for_vblank(dev_priv, pipe);
  5758. intel_encoders_disable(crtc, old_crtc_state, old_state);
  5759. drm_crtc_vblank_off(crtc);
  5760. assert_vblank_disabled(crtc);
  5761. intel_disable_pipe(intel_crtc);
  5762. i9xx_pfit_disable(intel_crtc);
  5763. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  5764. if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
  5765. if (IS_CHERRYVIEW(dev_priv))
  5766. chv_disable_pll(dev_priv, pipe);
  5767. else if (IS_VALLEYVIEW(dev_priv))
  5768. vlv_disable_pll(dev_priv, pipe);
  5769. else
  5770. i9xx_disable_pll(intel_crtc);
  5771. }
  5772. intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
  5773. if (!IS_GEN2(dev_priv))
  5774. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5775. }
  5776. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5777. {
  5778. struct intel_encoder *encoder;
  5779. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5780. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5781. enum intel_display_power_domain domain;
  5782. unsigned long domains;
  5783. struct drm_atomic_state *state;
  5784. struct intel_crtc_state *crtc_state;
  5785. int ret;
  5786. if (!intel_crtc->active)
  5787. return;
  5788. if (to_intel_plane_state(crtc->primary->state)->base.visible) {
  5789. WARN_ON(intel_crtc->flip_work);
  5790. intel_pre_disable_primary_noatomic(crtc);
  5791. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  5792. to_intel_plane_state(crtc->primary->state)->base.visible = false;
  5793. }
  5794. state = drm_atomic_state_alloc(crtc->dev);
  5795. state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
  5796. /* Everything's already locked, -EDEADLK can't happen. */
  5797. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  5798. ret = drm_atomic_add_affected_connectors(state, crtc);
  5799. WARN_ON(IS_ERR(crtc_state) || ret);
  5800. dev_priv->display.crtc_disable(crtc_state, state);
  5801. drm_atomic_state_put(state);
  5802. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  5803. crtc->base.id, crtc->name);
  5804. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  5805. crtc->state->active = false;
  5806. intel_crtc->active = false;
  5807. crtc->enabled = false;
  5808. crtc->state->connector_mask = 0;
  5809. crtc->state->encoder_mask = 0;
  5810. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  5811. encoder->base.crtc = NULL;
  5812. intel_fbc_disable(intel_crtc);
  5813. intel_update_watermarks(intel_crtc);
  5814. intel_disable_shared_dpll(intel_crtc);
  5815. domains = intel_crtc->enabled_power_domains;
  5816. for_each_power_domain(domain, domains)
  5817. intel_display_power_put(dev_priv, domain);
  5818. intel_crtc->enabled_power_domains = 0;
  5819. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5820. dev_priv->min_pixclk[intel_crtc->pipe] = 0;
  5821. }
  5822. /*
  5823. * turn all crtc's off, but do not adjust state
  5824. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5825. */
  5826. int intel_display_suspend(struct drm_device *dev)
  5827. {
  5828. struct drm_i915_private *dev_priv = to_i915(dev);
  5829. struct drm_atomic_state *state;
  5830. int ret;
  5831. state = drm_atomic_helper_suspend(dev);
  5832. ret = PTR_ERR_OR_ZERO(state);
  5833. if (ret)
  5834. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5835. else
  5836. dev_priv->modeset_restore_state = state;
  5837. return ret;
  5838. }
  5839. void intel_encoder_destroy(struct drm_encoder *encoder)
  5840. {
  5841. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5842. drm_encoder_cleanup(encoder);
  5843. kfree(intel_encoder);
  5844. }
  5845. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5846. * internal consistency). */
  5847. static void intel_connector_verify_state(struct intel_connector *connector)
  5848. {
  5849. struct drm_crtc *crtc = connector->base.state->crtc;
  5850. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5851. connector->base.base.id,
  5852. connector->base.name);
  5853. if (connector->get_hw_state(connector)) {
  5854. struct intel_encoder *encoder = connector->encoder;
  5855. struct drm_connector_state *conn_state = connector->base.state;
  5856. I915_STATE_WARN(!crtc,
  5857. "connector enabled without attached crtc\n");
  5858. if (!crtc)
  5859. return;
  5860. I915_STATE_WARN(!crtc->state->active,
  5861. "connector is active, but attached crtc isn't\n");
  5862. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5863. return;
  5864. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5865. "atomic encoder doesn't match attached encoder\n");
  5866. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5867. "attached encoder crtc differs from connector crtc\n");
  5868. } else {
  5869. I915_STATE_WARN(crtc && crtc->state->active,
  5870. "attached crtc is active, but connector isn't\n");
  5871. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5872. "best encoder set without crtc!\n");
  5873. }
  5874. }
  5875. int intel_connector_init(struct intel_connector *connector)
  5876. {
  5877. drm_atomic_helper_connector_reset(&connector->base);
  5878. if (!connector->base.state)
  5879. return -ENOMEM;
  5880. return 0;
  5881. }
  5882. struct intel_connector *intel_connector_alloc(void)
  5883. {
  5884. struct intel_connector *connector;
  5885. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5886. if (!connector)
  5887. return NULL;
  5888. if (intel_connector_init(connector) < 0) {
  5889. kfree(connector);
  5890. return NULL;
  5891. }
  5892. return connector;
  5893. }
  5894. /* Simple connector->get_hw_state implementation for encoders that support only
  5895. * one connector and no cloning and hence the encoder state determines the state
  5896. * of the connector. */
  5897. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5898. {
  5899. enum pipe pipe = 0;
  5900. struct intel_encoder *encoder = connector->encoder;
  5901. return encoder->get_hw_state(encoder, &pipe);
  5902. }
  5903. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5904. {
  5905. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5906. return crtc_state->fdi_lanes;
  5907. return 0;
  5908. }
  5909. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5910. struct intel_crtc_state *pipe_config)
  5911. {
  5912. struct drm_i915_private *dev_priv = to_i915(dev);
  5913. struct drm_atomic_state *state = pipe_config->base.state;
  5914. struct intel_crtc *other_crtc;
  5915. struct intel_crtc_state *other_crtc_state;
  5916. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5917. pipe_name(pipe), pipe_config->fdi_lanes);
  5918. if (pipe_config->fdi_lanes > 4) {
  5919. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5920. pipe_name(pipe), pipe_config->fdi_lanes);
  5921. return -EINVAL;
  5922. }
  5923. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  5924. if (pipe_config->fdi_lanes > 2) {
  5925. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5926. pipe_config->fdi_lanes);
  5927. return -EINVAL;
  5928. } else {
  5929. return 0;
  5930. }
  5931. }
  5932. if (INTEL_INFO(dev)->num_pipes == 2)
  5933. return 0;
  5934. /* Ivybridge 3 pipe is really complicated */
  5935. switch (pipe) {
  5936. case PIPE_A:
  5937. return 0;
  5938. case PIPE_B:
  5939. if (pipe_config->fdi_lanes <= 2)
  5940. return 0;
  5941. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
  5942. other_crtc_state =
  5943. intel_atomic_get_crtc_state(state, other_crtc);
  5944. if (IS_ERR(other_crtc_state))
  5945. return PTR_ERR(other_crtc_state);
  5946. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5947. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5948. pipe_name(pipe), pipe_config->fdi_lanes);
  5949. return -EINVAL;
  5950. }
  5951. return 0;
  5952. case PIPE_C:
  5953. if (pipe_config->fdi_lanes > 2) {
  5954. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5955. pipe_name(pipe), pipe_config->fdi_lanes);
  5956. return -EINVAL;
  5957. }
  5958. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
  5959. other_crtc_state =
  5960. intel_atomic_get_crtc_state(state, other_crtc);
  5961. if (IS_ERR(other_crtc_state))
  5962. return PTR_ERR(other_crtc_state);
  5963. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5964. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5965. return -EINVAL;
  5966. }
  5967. return 0;
  5968. default:
  5969. BUG();
  5970. }
  5971. }
  5972. #define RETRY 1
  5973. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5974. struct intel_crtc_state *pipe_config)
  5975. {
  5976. struct drm_device *dev = intel_crtc->base.dev;
  5977. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5978. int lane, link_bw, fdi_dotclock, ret;
  5979. bool needs_recompute = false;
  5980. retry:
  5981. /* FDI is a binary signal running at ~2.7GHz, encoding
  5982. * each output octet as 10 bits. The actual frequency
  5983. * is stored as a divider into a 100MHz clock, and the
  5984. * mode pixel clock is stored in units of 1KHz.
  5985. * Hence the bw of each lane in terms of the mode signal
  5986. * is:
  5987. */
  5988. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5989. fdi_dotclock = adjusted_mode->crtc_clock;
  5990. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5991. pipe_config->pipe_bpp);
  5992. pipe_config->fdi_lanes = lane;
  5993. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5994. link_bw, &pipe_config->fdi_m_n);
  5995. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5996. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5997. pipe_config->pipe_bpp -= 2*3;
  5998. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5999. pipe_config->pipe_bpp);
  6000. needs_recompute = true;
  6001. pipe_config->bw_constrained = true;
  6002. goto retry;
  6003. }
  6004. if (needs_recompute)
  6005. return RETRY;
  6006. return ret;
  6007. }
  6008. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  6009. struct intel_crtc_state *pipe_config)
  6010. {
  6011. if (pipe_config->pipe_bpp > 24)
  6012. return false;
  6013. /* HSW can handle pixel rate up to cdclk? */
  6014. if (IS_HASWELL(dev_priv))
  6015. return true;
  6016. /*
  6017. * We compare against max which means we must take
  6018. * the increased cdclk requirement into account when
  6019. * calculating the new cdclk.
  6020. *
  6021. * Should measure whether using a lower cdclk w/o IPS
  6022. */
  6023. return ilk_pipe_pixel_rate(pipe_config) <=
  6024. dev_priv->max_cdclk_freq * 95 / 100;
  6025. }
  6026. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  6027. struct intel_crtc_state *pipe_config)
  6028. {
  6029. struct drm_device *dev = crtc->base.dev;
  6030. struct drm_i915_private *dev_priv = to_i915(dev);
  6031. pipe_config->ips_enabled = i915.enable_ips &&
  6032. hsw_crtc_supports_ips(crtc) &&
  6033. pipe_config_supports_ips(dev_priv, pipe_config);
  6034. }
  6035. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  6036. {
  6037. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6038. /* GDG double wide on either pipe, otherwise pipe A only */
  6039. return INTEL_INFO(dev_priv)->gen < 4 &&
  6040. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  6041. }
  6042. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  6043. struct intel_crtc_state *pipe_config)
  6044. {
  6045. struct drm_device *dev = crtc->base.dev;
  6046. struct drm_i915_private *dev_priv = to_i915(dev);
  6047. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  6048. int clock_limit = dev_priv->max_dotclk_freq;
  6049. if (INTEL_INFO(dev)->gen < 4) {
  6050. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  6051. /*
  6052. * Enable double wide mode when the dot clock
  6053. * is > 90% of the (display) core speed.
  6054. */
  6055. if (intel_crtc_supports_double_wide(crtc) &&
  6056. adjusted_mode->crtc_clock > clock_limit) {
  6057. clock_limit = dev_priv->max_dotclk_freq;
  6058. pipe_config->double_wide = true;
  6059. }
  6060. }
  6061. if (adjusted_mode->crtc_clock > clock_limit) {
  6062. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  6063. adjusted_mode->crtc_clock, clock_limit,
  6064. yesno(pipe_config->double_wide));
  6065. return -EINVAL;
  6066. }
  6067. /*
  6068. * Pipe horizontal size must be even in:
  6069. * - DVO ganged mode
  6070. * - LVDS dual channel mode
  6071. * - Double wide pipe
  6072. */
  6073. if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  6074. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  6075. pipe_config->pipe_src_w &= ~1;
  6076. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  6077. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  6078. */
  6079. if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
  6080. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  6081. return -EINVAL;
  6082. if (HAS_IPS(dev_priv))
  6083. hsw_compute_ips_config(crtc, pipe_config);
  6084. if (pipe_config->has_pch_encoder)
  6085. return ironlake_fdi_compute_config(crtc, pipe_config);
  6086. return 0;
  6087. }
  6088. static int skylake_get_display_clock_speed(struct drm_device *dev)
  6089. {
  6090. struct drm_i915_private *dev_priv = to_i915(dev);
  6091. uint32_t cdctl;
  6092. skl_dpll0_update(dev_priv);
  6093. if (dev_priv->cdclk_pll.vco == 0)
  6094. return dev_priv->cdclk_pll.ref;
  6095. cdctl = I915_READ(CDCLK_CTL);
  6096. if (dev_priv->cdclk_pll.vco == 8640000) {
  6097. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  6098. case CDCLK_FREQ_450_432:
  6099. return 432000;
  6100. case CDCLK_FREQ_337_308:
  6101. return 308571;
  6102. case CDCLK_FREQ_540:
  6103. return 540000;
  6104. case CDCLK_FREQ_675_617:
  6105. return 617143;
  6106. default:
  6107. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  6108. }
  6109. } else {
  6110. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  6111. case CDCLK_FREQ_450_432:
  6112. return 450000;
  6113. case CDCLK_FREQ_337_308:
  6114. return 337500;
  6115. case CDCLK_FREQ_540:
  6116. return 540000;
  6117. case CDCLK_FREQ_675_617:
  6118. return 675000;
  6119. default:
  6120. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  6121. }
  6122. }
  6123. return dev_priv->cdclk_pll.ref;
  6124. }
  6125. static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
  6126. {
  6127. u32 val;
  6128. dev_priv->cdclk_pll.ref = 19200;
  6129. dev_priv->cdclk_pll.vco = 0;
  6130. val = I915_READ(BXT_DE_PLL_ENABLE);
  6131. if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
  6132. return;
  6133. if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
  6134. return;
  6135. val = I915_READ(BXT_DE_PLL_CTL);
  6136. dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
  6137. dev_priv->cdclk_pll.ref;
  6138. }
  6139. static int broxton_get_display_clock_speed(struct drm_device *dev)
  6140. {
  6141. struct drm_i915_private *dev_priv = to_i915(dev);
  6142. u32 divider;
  6143. int div, vco;
  6144. bxt_de_pll_update(dev_priv);
  6145. vco = dev_priv->cdclk_pll.vco;
  6146. if (vco == 0)
  6147. return dev_priv->cdclk_pll.ref;
  6148. divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
  6149. switch (divider) {
  6150. case BXT_CDCLK_CD2X_DIV_SEL_1:
  6151. div = 2;
  6152. break;
  6153. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  6154. div = 3;
  6155. break;
  6156. case BXT_CDCLK_CD2X_DIV_SEL_2:
  6157. div = 4;
  6158. break;
  6159. case BXT_CDCLK_CD2X_DIV_SEL_4:
  6160. div = 8;
  6161. break;
  6162. default:
  6163. MISSING_CASE(divider);
  6164. return dev_priv->cdclk_pll.ref;
  6165. }
  6166. return DIV_ROUND_CLOSEST(vco, div);
  6167. }
  6168. static int broadwell_get_display_clock_speed(struct drm_device *dev)
  6169. {
  6170. struct drm_i915_private *dev_priv = to_i915(dev);
  6171. uint32_t lcpll = I915_READ(LCPLL_CTL);
  6172. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  6173. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  6174. return 800000;
  6175. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  6176. return 450000;
  6177. else if (freq == LCPLL_CLK_FREQ_450)
  6178. return 450000;
  6179. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  6180. return 540000;
  6181. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  6182. return 337500;
  6183. else
  6184. return 675000;
  6185. }
  6186. static int haswell_get_display_clock_speed(struct drm_device *dev)
  6187. {
  6188. struct drm_i915_private *dev_priv = to_i915(dev);
  6189. uint32_t lcpll = I915_READ(LCPLL_CTL);
  6190. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  6191. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  6192. return 800000;
  6193. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  6194. return 450000;
  6195. else if (freq == LCPLL_CLK_FREQ_450)
  6196. return 450000;
  6197. else if (IS_HSW_ULT(dev_priv))
  6198. return 337500;
  6199. else
  6200. return 540000;
  6201. }
  6202. static int valleyview_get_display_clock_speed(struct drm_device *dev)
  6203. {
  6204. return vlv_get_cck_clock_hpll(to_i915(dev), "cdclk",
  6205. CCK_DISPLAY_CLOCK_CONTROL);
  6206. }
  6207. static int ilk_get_display_clock_speed(struct drm_device *dev)
  6208. {
  6209. return 450000;
  6210. }
  6211. static int i945_get_display_clock_speed(struct drm_device *dev)
  6212. {
  6213. return 400000;
  6214. }
  6215. static int i915_get_display_clock_speed(struct drm_device *dev)
  6216. {
  6217. return 333333;
  6218. }
  6219. static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
  6220. {
  6221. return 200000;
  6222. }
  6223. static int pnv_get_display_clock_speed(struct drm_device *dev)
  6224. {
  6225. struct pci_dev *pdev = dev->pdev;
  6226. u16 gcfgc = 0;
  6227. pci_read_config_word(pdev, GCFGC, &gcfgc);
  6228. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  6229. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  6230. return 266667;
  6231. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  6232. return 333333;
  6233. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  6234. return 444444;
  6235. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  6236. return 200000;
  6237. default:
  6238. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  6239. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  6240. return 133333;
  6241. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  6242. return 166667;
  6243. }
  6244. }
  6245. static int i915gm_get_display_clock_speed(struct drm_device *dev)
  6246. {
  6247. struct pci_dev *pdev = dev->pdev;
  6248. u16 gcfgc = 0;
  6249. pci_read_config_word(pdev, GCFGC, &gcfgc);
  6250. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  6251. return 133333;
  6252. else {
  6253. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  6254. case GC_DISPLAY_CLOCK_333_MHZ:
  6255. return 333333;
  6256. default:
  6257. case GC_DISPLAY_CLOCK_190_200_MHZ:
  6258. return 190000;
  6259. }
  6260. }
  6261. }
  6262. static int i865_get_display_clock_speed(struct drm_device *dev)
  6263. {
  6264. return 266667;
  6265. }
  6266. static int i85x_get_display_clock_speed(struct drm_device *dev)
  6267. {
  6268. struct pci_dev *pdev = dev->pdev;
  6269. u16 hpllcc = 0;
  6270. /*
  6271. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  6272. * encoding is different :(
  6273. * FIXME is this the right way to detect 852GM/852GMV?
  6274. */
  6275. if (pdev->revision == 0x1)
  6276. return 133333;
  6277. pci_bus_read_config_word(pdev->bus,
  6278. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  6279. /* Assume that the hardware is in the high speed state. This
  6280. * should be the default.
  6281. */
  6282. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  6283. case GC_CLOCK_133_200:
  6284. case GC_CLOCK_133_200_2:
  6285. case GC_CLOCK_100_200:
  6286. return 200000;
  6287. case GC_CLOCK_166_250:
  6288. return 250000;
  6289. case GC_CLOCK_100_133:
  6290. return 133333;
  6291. case GC_CLOCK_133_266:
  6292. case GC_CLOCK_133_266_2:
  6293. case GC_CLOCK_166_266:
  6294. return 266667;
  6295. }
  6296. /* Shouldn't happen */
  6297. return 0;
  6298. }
  6299. static int i830_get_display_clock_speed(struct drm_device *dev)
  6300. {
  6301. return 133333;
  6302. }
  6303. static unsigned int intel_hpll_vco(struct drm_device *dev)
  6304. {
  6305. struct drm_i915_private *dev_priv = to_i915(dev);
  6306. static const unsigned int blb_vco[8] = {
  6307. [0] = 3200000,
  6308. [1] = 4000000,
  6309. [2] = 5333333,
  6310. [3] = 4800000,
  6311. [4] = 6400000,
  6312. };
  6313. static const unsigned int pnv_vco[8] = {
  6314. [0] = 3200000,
  6315. [1] = 4000000,
  6316. [2] = 5333333,
  6317. [3] = 4800000,
  6318. [4] = 2666667,
  6319. };
  6320. static const unsigned int cl_vco[8] = {
  6321. [0] = 3200000,
  6322. [1] = 4000000,
  6323. [2] = 5333333,
  6324. [3] = 6400000,
  6325. [4] = 3333333,
  6326. [5] = 3566667,
  6327. [6] = 4266667,
  6328. };
  6329. static const unsigned int elk_vco[8] = {
  6330. [0] = 3200000,
  6331. [1] = 4000000,
  6332. [2] = 5333333,
  6333. [3] = 4800000,
  6334. };
  6335. static const unsigned int ctg_vco[8] = {
  6336. [0] = 3200000,
  6337. [1] = 4000000,
  6338. [2] = 5333333,
  6339. [3] = 6400000,
  6340. [4] = 2666667,
  6341. [5] = 4266667,
  6342. };
  6343. const unsigned int *vco_table;
  6344. unsigned int vco;
  6345. uint8_t tmp = 0;
  6346. /* FIXME other chipsets? */
  6347. if (IS_GM45(dev_priv))
  6348. vco_table = ctg_vco;
  6349. else if (IS_G4X(dev_priv))
  6350. vco_table = elk_vco;
  6351. else if (IS_CRESTLINE(dev))
  6352. vco_table = cl_vco;
  6353. else if (IS_PINEVIEW(dev))
  6354. vco_table = pnv_vco;
  6355. else if (IS_G33(dev))
  6356. vco_table = blb_vco;
  6357. else
  6358. return 0;
  6359. tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
  6360. vco = vco_table[tmp & 0x7];
  6361. if (vco == 0)
  6362. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  6363. else
  6364. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  6365. return vco;
  6366. }
  6367. static int gm45_get_display_clock_speed(struct drm_device *dev)
  6368. {
  6369. struct pci_dev *pdev = dev->pdev;
  6370. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  6371. uint16_t tmp = 0;
  6372. pci_read_config_word(pdev, GCFGC, &tmp);
  6373. cdclk_sel = (tmp >> 12) & 0x1;
  6374. switch (vco) {
  6375. case 2666667:
  6376. case 4000000:
  6377. case 5333333:
  6378. return cdclk_sel ? 333333 : 222222;
  6379. case 3200000:
  6380. return cdclk_sel ? 320000 : 228571;
  6381. default:
  6382. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  6383. return 222222;
  6384. }
  6385. }
  6386. static int i965gm_get_display_clock_speed(struct drm_device *dev)
  6387. {
  6388. struct pci_dev *pdev = dev->pdev;
  6389. static const uint8_t div_3200[] = { 16, 10, 8 };
  6390. static const uint8_t div_4000[] = { 20, 12, 10 };
  6391. static const uint8_t div_5333[] = { 24, 16, 14 };
  6392. const uint8_t *div_table;
  6393. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  6394. uint16_t tmp = 0;
  6395. pci_read_config_word(pdev, GCFGC, &tmp);
  6396. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  6397. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  6398. goto fail;
  6399. switch (vco) {
  6400. case 3200000:
  6401. div_table = div_3200;
  6402. break;
  6403. case 4000000:
  6404. div_table = div_4000;
  6405. break;
  6406. case 5333333:
  6407. div_table = div_5333;
  6408. break;
  6409. default:
  6410. goto fail;
  6411. }
  6412. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  6413. fail:
  6414. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  6415. return 200000;
  6416. }
  6417. static int g33_get_display_clock_speed(struct drm_device *dev)
  6418. {
  6419. struct pci_dev *pdev = dev->pdev;
  6420. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  6421. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  6422. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  6423. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  6424. const uint8_t *div_table;
  6425. unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
  6426. uint16_t tmp = 0;
  6427. pci_read_config_word(pdev, GCFGC, &tmp);
  6428. cdclk_sel = (tmp >> 4) & 0x7;
  6429. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  6430. goto fail;
  6431. switch (vco) {
  6432. case 3200000:
  6433. div_table = div_3200;
  6434. break;
  6435. case 4000000:
  6436. div_table = div_4000;
  6437. break;
  6438. case 4800000:
  6439. div_table = div_4800;
  6440. break;
  6441. case 5333333:
  6442. div_table = div_5333;
  6443. break;
  6444. default:
  6445. goto fail;
  6446. }
  6447. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  6448. fail:
  6449. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  6450. return 190476;
  6451. }
  6452. static void
  6453. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  6454. {
  6455. while (*num > DATA_LINK_M_N_MASK ||
  6456. *den > DATA_LINK_M_N_MASK) {
  6457. *num >>= 1;
  6458. *den >>= 1;
  6459. }
  6460. }
  6461. static void compute_m_n(unsigned int m, unsigned int n,
  6462. uint32_t *ret_m, uint32_t *ret_n)
  6463. {
  6464. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  6465. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  6466. intel_reduce_m_n_ratio(ret_m, ret_n);
  6467. }
  6468. void
  6469. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  6470. int pixel_clock, int link_clock,
  6471. struct intel_link_m_n *m_n)
  6472. {
  6473. m_n->tu = 64;
  6474. compute_m_n(bits_per_pixel * pixel_clock,
  6475. link_clock * nlanes * 8,
  6476. &m_n->gmch_m, &m_n->gmch_n);
  6477. compute_m_n(pixel_clock, link_clock,
  6478. &m_n->link_m, &m_n->link_n);
  6479. }
  6480. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  6481. {
  6482. if (i915.panel_use_ssc >= 0)
  6483. return i915.panel_use_ssc != 0;
  6484. return dev_priv->vbt.lvds_use_ssc
  6485. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  6486. }
  6487. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  6488. {
  6489. return (1 << dpll->n) << 16 | dpll->m2;
  6490. }
  6491. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  6492. {
  6493. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  6494. }
  6495. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  6496. struct intel_crtc_state *crtc_state,
  6497. struct dpll *reduced_clock)
  6498. {
  6499. struct drm_device *dev = crtc->base.dev;
  6500. u32 fp, fp2 = 0;
  6501. if (IS_PINEVIEW(dev)) {
  6502. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  6503. if (reduced_clock)
  6504. fp2 = pnv_dpll_compute_fp(reduced_clock);
  6505. } else {
  6506. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6507. if (reduced_clock)
  6508. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6509. }
  6510. crtc_state->dpll_hw_state.fp0 = fp;
  6511. crtc->lowfreq_avail = false;
  6512. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6513. reduced_clock) {
  6514. crtc_state->dpll_hw_state.fp1 = fp2;
  6515. crtc->lowfreq_avail = true;
  6516. } else {
  6517. crtc_state->dpll_hw_state.fp1 = fp;
  6518. }
  6519. }
  6520. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  6521. pipe)
  6522. {
  6523. u32 reg_val;
  6524. /*
  6525. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  6526. * and set it to a reasonable value instead.
  6527. */
  6528. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6529. reg_val &= 0xffffff00;
  6530. reg_val |= 0x00000030;
  6531. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6532. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6533. reg_val &= 0x8cffffff;
  6534. reg_val = 0x8c000000;
  6535. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6536. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6537. reg_val &= 0xffffff00;
  6538. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6539. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6540. reg_val &= 0x00ffffff;
  6541. reg_val |= 0xb0000000;
  6542. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6543. }
  6544. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  6545. struct intel_link_m_n *m_n)
  6546. {
  6547. struct drm_device *dev = crtc->base.dev;
  6548. struct drm_i915_private *dev_priv = to_i915(dev);
  6549. int pipe = crtc->pipe;
  6550. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6551. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  6552. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  6553. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  6554. }
  6555. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6556. struct intel_link_m_n *m_n,
  6557. struct intel_link_m_n *m2_n2)
  6558. {
  6559. struct drm_device *dev = crtc->base.dev;
  6560. struct drm_i915_private *dev_priv = to_i915(dev);
  6561. int pipe = crtc->pipe;
  6562. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6563. if (INTEL_INFO(dev)->gen >= 5) {
  6564. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6565. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6566. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6567. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6568. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6569. * for gen < 8) and if DRRS is supported (to make sure the
  6570. * registers are not unnecessarily accessed).
  6571. */
  6572. if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
  6573. INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
  6574. I915_WRITE(PIPE_DATA_M2(transcoder),
  6575. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6576. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6577. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6578. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6579. }
  6580. } else {
  6581. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6582. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6583. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6584. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6585. }
  6586. }
  6587. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6588. {
  6589. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6590. if (m_n == M1_N1) {
  6591. dp_m_n = &crtc->config->dp_m_n;
  6592. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6593. } else if (m_n == M2_N2) {
  6594. /*
  6595. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6596. * needs to be programmed into M1_N1.
  6597. */
  6598. dp_m_n = &crtc->config->dp_m2_n2;
  6599. } else {
  6600. DRM_ERROR("Unsupported divider value\n");
  6601. return;
  6602. }
  6603. if (crtc->config->has_pch_encoder)
  6604. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6605. else
  6606. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6607. }
  6608. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6609. struct intel_crtc_state *pipe_config)
  6610. {
  6611. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  6612. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6613. if (crtc->pipe != PIPE_A)
  6614. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6615. /* DPLL not used with DSI, but still need the rest set up */
  6616. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  6617. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  6618. DPLL_EXT_BUFFER_ENABLE_VLV;
  6619. pipe_config->dpll_hw_state.dpll_md =
  6620. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6621. }
  6622. static void chv_compute_dpll(struct intel_crtc *crtc,
  6623. struct intel_crtc_state *pipe_config)
  6624. {
  6625. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  6626. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6627. if (crtc->pipe != PIPE_A)
  6628. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6629. /* DPLL not used with DSI, but still need the rest set up */
  6630. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  6631. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  6632. pipe_config->dpll_hw_state.dpll_md =
  6633. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6634. }
  6635. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6636. const struct intel_crtc_state *pipe_config)
  6637. {
  6638. struct drm_device *dev = crtc->base.dev;
  6639. struct drm_i915_private *dev_priv = to_i915(dev);
  6640. enum pipe pipe = crtc->pipe;
  6641. u32 mdiv;
  6642. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6643. u32 coreclk, reg_val;
  6644. /* Enable Refclk */
  6645. I915_WRITE(DPLL(pipe),
  6646. pipe_config->dpll_hw_state.dpll &
  6647. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  6648. /* No need to actually set up the DPLL with DSI */
  6649. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6650. return;
  6651. mutex_lock(&dev_priv->sb_lock);
  6652. bestn = pipe_config->dpll.n;
  6653. bestm1 = pipe_config->dpll.m1;
  6654. bestm2 = pipe_config->dpll.m2;
  6655. bestp1 = pipe_config->dpll.p1;
  6656. bestp2 = pipe_config->dpll.p2;
  6657. /* See eDP HDMI DPIO driver vbios notes doc */
  6658. /* PLL B needs special handling */
  6659. if (pipe == PIPE_B)
  6660. vlv_pllb_recal_opamp(dev_priv, pipe);
  6661. /* Set up Tx target for periodic Rcomp update */
  6662. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6663. /* Disable target IRef on PLL */
  6664. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6665. reg_val &= 0x00ffffff;
  6666. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6667. /* Disable fast lock */
  6668. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6669. /* Set idtafcrecal before PLL is enabled */
  6670. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6671. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6672. mdiv |= ((bestn << DPIO_N_SHIFT));
  6673. mdiv |= (1 << DPIO_K_SHIFT);
  6674. /*
  6675. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6676. * but we don't support that).
  6677. * Note: don't use the DAC post divider as it seems unstable.
  6678. */
  6679. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6680. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6681. mdiv |= DPIO_ENABLE_CALIBRATION;
  6682. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6683. /* Set HBR and RBR LPF coefficients */
  6684. if (pipe_config->port_clock == 162000 ||
  6685. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
  6686. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
  6687. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6688. 0x009f0003);
  6689. else
  6690. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6691. 0x00d0000f);
  6692. if (intel_crtc_has_dp_encoder(pipe_config)) {
  6693. /* Use SSC source */
  6694. if (pipe == PIPE_A)
  6695. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6696. 0x0df40000);
  6697. else
  6698. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6699. 0x0df70000);
  6700. } else { /* HDMI or VGA */
  6701. /* Use bend source */
  6702. if (pipe == PIPE_A)
  6703. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6704. 0x0df70000);
  6705. else
  6706. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6707. 0x0df40000);
  6708. }
  6709. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6710. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6711. if (intel_crtc_has_dp_encoder(crtc->config))
  6712. coreclk |= 0x01000000;
  6713. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6714. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6715. mutex_unlock(&dev_priv->sb_lock);
  6716. }
  6717. static void chv_prepare_pll(struct intel_crtc *crtc,
  6718. const struct intel_crtc_state *pipe_config)
  6719. {
  6720. struct drm_device *dev = crtc->base.dev;
  6721. struct drm_i915_private *dev_priv = to_i915(dev);
  6722. enum pipe pipe = crtc->pipe;
  6723. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6724. u32 loopfilter, tribuf_calcntr;
  6725. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6726. u32 dpio_val;
  6727. int vco;
  6728. /* Enable Refclk and SSC */
  6729. I915_WRITE(DPLL(pipe),
  6730. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6731. /* No need to actually set up the DPLL with DSI */
  6732. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6733. return;
  6734. bestn = pipe_config->dpll.n;
  6735. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6736. bestm1 = pipe_config->dpll.m1;
  6737. bestm2 = pipe_config->dpll.m2 >> 22;
  6738. bestp1 = pipe_config->dpll.p1;
  6739. bestp2 = pipe_config->dpll.p2;
  6740. vco = pipe_config->dpll.vco;
  6741. dpio_val = 0;
  6742. loopfilter = 0;
  6743. mutex_lock(&dev_priv->sb_lock);
  6744. /* p1 and p2 divider */
  6745. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6746. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6747. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6748. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6749. 1 << DPIO_CHV_K_DIV_SHIFT);
  6750. /* Feedback post-divider - m2 */
  6751. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6752. /* Feedback refclk divider - n and m1 */
  6753. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6754. DPIO_CHV_M1_DIV_BY_2 |
  6755. 1 << DPIO_CHV_N_DIV_SHIFT);
  6756. /* M2 fraction division */
  6757. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6758. /* M2 fraction division enable */
  6759. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6760. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6761. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6762. if (bestm2_frac)
  6763. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6764. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6765. /* Program digital lock detect threshold */
  6766. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6767. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6768. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6769. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6770. if (!bestm2_frac)
  6771. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6772. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6773. /* Loop filter */
  6774. if (vco == 5400000) {
  6775. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6776. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6777. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6778. tribuf_calcntr = 0x9;
  6779. } else if (vco <= 6200000) {
  6780. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6781. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6782. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6783. tribuf_calcntr = 0x9;
  6784. } else if (vco <= 6480000) {
  6785. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6786. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6787. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6788. tribuf_calcntr = 0x8;
  6789. } else {
  6790. /* Not supported. Apply the same limits as in the max case */
  6791. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6792. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6793. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6794. tribuf_calcntr = 0;
  6795. }
  6796. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6797. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6798. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6799. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6800. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6801. /* AFC Recal */
  6802. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6803. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6804. DPIO_AFC_RECAL);
  6805. mutex_unlock(&dev_priv->sb_lock);
  6806. }
  6807. /**
  6808. * vlv_force_pll_on - forcibly enable just the PLL
  6809. * @dev_priv: i915 private structure
  6810. * @pipe: pipe PLL to enable
  6811. * @dpll: PLL configuration
  6812. *
  6813. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6814. * in cases where we need the PLL enabled even when @pipe is not going to
  6815. * be enabled.
  6816. */
  6817. int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
  6818. const struct dpll *dpll)
  6819. {
  6820. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  6821. struct intel_crtc_state *pipe_config;
  6822. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  6823. if (!pipe_config)
  6824. return -ENOMEM;
  6825. pipe_config->base.crtc = &crtc->base;
  6826. pipe_config->pixel_multiplier = 1;
  6827. pipe_config->dpll = *dpll;
  6828. if (IS_CHERRYVIEW(dev_priv)) {
  6829. chv_compute_dpll(crtc, pipe_config);
  6830. chv_prepare_pll(crtc, pipe_config);
  6831. chv_enable_pll(crtc, pipe_config);
  6832. } else {
  6833. vlv_compute_dpll(crtc, pipe_config);
  6834. vlv_prepare_pll(crtc, pipe_config);
  6835. vlv_enable_pll(crtc, pipe_config);
  6836. }
  6837. kfree(pipe_config);
  6838. return 0;
  6839. }
  6840. /**
  6841. * vlv_force_pll_off - forcibly disable just the PLL
  6842. * @dev_priv: i915 private structure
  6843. * @pipe: pipe PLL to disable
  6844. *
  6845. * Disable the PLL for @pipe. To be used in cases where we need
  6846. * the PLL enabled even when @pipe is not going to be enabled.
  6847. */
  6848. void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
  6849. {
  6850. if (IS_CHERRYVIEW(dev_priv))
  6851. chv_disable_pll(dev_priv, pipe);
  6852. else
  6853. vlv_disable_pll(dev_priv, pipe);
  6854. }
  6855. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6856. struct intel_crtc_state *crtc_state,
  6857. struct dpll *reduced_clock)
  6858. {
  6859. struct drm_device *dev = crtc->base.dev;
  6860. struct drm_i915_private *dev_priv = to_i915(dev);
  6861. u32 dpll;
  6862. struct dpll *clock = &crtc_state->dpll;
  6863. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6864. dpll = DPLL_VGA_MODE_DIS;
  6865. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  6866. dpll |= DPLLB_MODE_LVDS;
  6867. else
  6868. dpll |= DPLLB_MODE_DAC_SERIAL;
  6869. if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) || IS_G33(dev_priv)) {
  6870. dpll |= (crtc_state->pixel_multiplier - 1)
  6871. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6872. }
  6873. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6874. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  6875. dpll |= DPLL_SDVO_HIGH_SPEED;
  6876. if (intel_crtc_has_dp_encoder(crtc_state))
  6877. dpll |= DPLL_SDVO_HIGH_SPEED;
  6878. /* compute bitmask from p1 value */
  6879. if (IS_PINEVIEW(dev))
  6880. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6881. else {
  6882. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6883. if (IS_G4X(dev_priv) && reduced_clock)
  6884. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6885. }
  6886. switch (clock->p2) {
  6887. case 5:
  6888. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6889. break;
  6890. case 7:
  6891. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6892. break;
  6893. case 10:
  6894. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6895. break;
  6896. case 14:
  6897. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6898. break;
  6899. }
  6900. if (INTEL_INFO(dev)->gen >= 4)
  6901. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6902. if (crtc_state->sdvo_tv_clock)
  6903. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6904. else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6905. intel_panel_use_ssc(dev_priv))
  6906. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6907. else
  6908. dpll |= PLL_REF_INPUT_DREFCLK;
  6909. dpll |= DPLL_VCO_ENABLE;
  6910. crtc_state->dpll_hw_state.dpll = dpll;
  6911. if (INTEL_INFO(dev)->gen >= 4) {
  6912. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6913. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6914. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6915. }
  6916. }
  6917. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6918. struct intel_crtc_state *crtc_state,
  6919. struct dpll *reduced_clock)
  6920. {
  6921. struct drm_device *dev = crtc->base.dev;
  6922. struct drm_i915_private *dev_priv = to_i915(dev);
  6923. u32 dpll;
  6924. struct dpll *clock = &crtc_state->dpll;
  6925. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6926. dpll = DPLL_VGA_MODE_DIS;
  6927. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6928. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6929. } else {
  6930. if (clock->p1 == 2)
  6931. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6932. else
  6933. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6934. if (clock->p2 == 4)
  6935. dpll |= PLL_P2_DIVIDE_BY_4;
  6936. }
  6937. if (!IS_I830(dev_priv) &&
  6938. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
  6939. dpll |= DPLL_DVO_2X_MODE;
  6940. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6941. intel_panel_use_ssc(dev_priv))
  6942. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6943. else
  6944. dpll |= PLL_REF_INPUT_DREFCLK;
  6945. dpll |= DPLL_VCO_ENABLE;
  6946. crtc_state->dpll_hw_state.dpll = dpll;
  6947. }
  6948. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6949. {
  6950. struct drm_device *dev = intel_crtc->base.dev;
  6951. struct drm_i915_private *dev_priv = to_i915(dev);
  6952. enum pipe pipe = intel_crtc->pipe;
  6953. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6954. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  6955. uint32_t crtc_vtotal, crtc_vblank_end;
  6956. int vsyncshift = 0;
  6957. /* We need to be careful not to changed the adjusted mode, for otherwise
  6958. * the hw state checker will get angry at the mismatch. */
  6959. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6960. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6961. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6962. /* the chip adds 2 halflines automatically */
  6963. crtc_vtotal -= 1;
  6964. crtc_vblank_end -= 1;
  6965. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6966. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6967. else
  6968. vsyncshift = adjusted_mode->crtc_hsync_start -
  6969. adjusted_mode->crtc_htotal / 2;
  6970. if (vsyncshift < 0)
  6971. vsyncshift += adjusted_mode->crtc_htotal;
  6972. }
  6973. if (INTEL_INFO(dev)->gen > 3)
  6974. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6975. I915_WRITE(HTOTAL(cpu_transcoder),
  6976. (adjusted_mode->crtc_hdisplay - 1) |
  6977. ((adjusted_mode->crtc_htotal - 1) << 16));
  6978. I915_WRITE(HBLANK(cpu_transcoder),
  6979. (adjusted_mode->crtc_hblank_start - 1) |
  6980. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6981. I915_WRITE(HSYNC(cpu_transcoder),
  6982. (adjusted_mode->crtc_hsync_start - 1) |
  6983. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6984. I915_WRITE(VTOTAL(cpu_transcoder),
  6985. (adjusted_mode->crtc_vdisplay - 1) |
  6986. ((crtc_vtotal - 1) << 16));
  6987. I915_WRITE(VBLANK(cpu_transcoder),
  6988. (adjusted_mode->crtc_vblank_start - 1) |
  6989. ((crtc_vblank_end - 1) << 16));
  6990. I915_WRITE(VSYNC(cpu_transcoder),
  6991. (adjusted_mode->crtc_vsync_start - 1) |
  6992. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6993. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6994. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6995. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6996. * bits. */
  6997. if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
  6998. (pipe == PIPE_B || pipe == PIPE_C))
  6999. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  7000. }
  7001. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  7002. {
  7003. struct drm_device *dev = intel_crtc->base.dev;
  7004. struct drm_i915_private *dev_priv = to_i915(dev);
  7005. enum pipe pipe = intel_crtc->pipe;
  7006. /* pipesrc controls the size that is scaled from, which should
  7007. * always be the user's requested size.
  7008. */
  7009. I915_WRITE(PIPESRC(pipe),
  7010. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  7011. (intel_crtc->config->pipe_src_h - 1));
  7012. }
  7013. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  7014. struct intel_crtc_state *pipe_config)
  7015. {
  7016. struct drm_device *dev = crtc->base.dev;
  7017. struct drm_i915_private *dev_priv = to_i915(dev);
  7018. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  7019. uint32_t tmp;
  7020. tmp = I915_READ(HTOTAL(cpu_transcoder));
  7021. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  7022. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  7023. tmp = I915_READ(HBLANK(cpu_transcoder));
  7024. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  7025. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  7026. tmp = I915_READ(HSYNC(cpu_transcoder));
  7027. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  7028. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  7029. tmp = I915_READ(VTOTAL(cpu_transcoder));
  7030. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  7031. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  7032. tmp = I915_READ(VBLANK(cpu_transcoder));
  7033. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  7034. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  7035. tmp = I915_READ(VSYNC(cpu_transcoder));
  7036. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  7037. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  7038. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  7039. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  7040. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  7041. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  7042. }
  7043. }
  7044. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  7045. struct intel_crtc_state *pipe_config)
  7046. {
  7047. struct drm_device *dev = crtc->base.dev;
  7048. struct drm_i915_private *dev_priv = to_i915(dev);
  7049. u32 tmp;
  7050. tmp = I915_READ(PIPESRC(crtc->pipe));
  7051. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  7052. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  7053. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  7054. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  7055. }
  7056. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  7057. struct intel_crtc_state *pipe_config)
  7058. {
  7059. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  7060. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  7061. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  7062. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  7063. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  7064. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  7065. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  7066. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  7067. mode->flags = pipe_config->base.adjusted_mode.flags;
  7068. mode->type = DRM_MODE_TYPE_DRIVER;
  7069. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  7070. mode->flags |= pipe_config->base.adjusted_mode.flags;
  7071. mode->hsync = drm_mode_hsync(mode);
  7072. mode->vrefresh = drm_mode_vrefresh(mode);
  7073. drm_mode_set_name(mode);
  7074. }
  7075. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  7076. {
  7077. struct drm_device *dev = intel_crtc->base.dev;
  7078. struct drm_i915_private *dev_priv = to_i915(dev);
  7079. uint32_t pipeconf;
  7080. pipeconf = 0;
  7081. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  7082. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  7083. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  7084. if (intel_crtc->config->double_wide)
  7085. pipeconf |= PIPECONF_DOUBLE_WIDE;
  7086. /* only g4x and later have fancy bpc/dither controls */
  7087. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  7088. IS_CHERRYVIEW(dev_priv)) {
  7089. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  7090. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  7091. pipeconf |= PIPECONF_DITHER_EN |
  7092. PIPECONF_DITHER_TYPE_SP;
  7093. switch (intel_crtc->config->pipe_bpp) {
  7094. case 18:
  7095. pipeconf |= PIPECONF_6BPC;
  7096. break;
  7097. case 24:
  7098. pipeconf |= PIPECONF_8BPC;
  7099. break;
  7100. case 30:
  7101. pipeconf |= PIPECONF_10BPC;
  7102. break;
  7103. default:
  7104. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7105. BUG();
  7106. }
  7107. }
  7108. if (HAS_PIPE_CXSR(dev)) {
  7109. if (intel_crtc->lowfreq_avail) {
  7110. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  7111. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  7112. } else {
  7113. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  7114. }
  7115. }
  7116. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  7117. if (INTEL_INFO(dev)->gen < 4 ||
  7118. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  7119. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  7120. else
  7121. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  7122. } else
  7123. pipeconf |= PIPECONF_PROGRESSIVE;
  7124. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  7125. intel_crtc->config->limited_color_range)
  7126. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  7127. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  7128. POSTING_READ(PIPECONF(intel_crtc->pipe));
  7129. }
  7130. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  7131. struct intel_crtc_state *crtc_state)
  7132. {
  7133. struct drm_device *dev = crtc->base.dev;
  7134. struct drm_i915_private *dev_priv = to_i915(dev);
  7135. const struct intel_limit *limit;
  7136. int refclk = 48000;
  7137. memset(&crtc_state->dpll_hw_state, 0,
  7138. sizeof(crtc_state->dpll_hw_state));
  7139. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7140. if (intel_panel_use_ssc(dev_priv)) {
  7141. refclk = dev_priv->vbt.lvds_ssc_freq;
  7142. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7143. }
  7144. limit = &intel_limits_i8xx_lvds;
  7145. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
  7146. limit = &intel_limits_i8xx_dvo;
  7147. } else {
  7148. limit = &intel_limits_i8xx_dac;
  7149. }
  7150. if (!crtc_state->clock_set &&
  7151. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7152. refclk, NULL, &crtc_state->dpll)) {
  7153. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7154. return -EINVAL;
  7155. }
  7156. i8xx_compute_dpll(crtc, crtc_state, NULL);
  7157. return 0;
  7158. }
  7159. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  7160. struct intel_crtc_state *crtc_state)
  7161. {
  7162. struct drm_device *dev = crtc->base.dev;
  7163. struct drm_i915_private *dev_priv = to_i915(dev);
  7164. const struct intel_limit *limit;
  7165. int refclk = 96000;
  7166. memset(&crtc_state->dpll_hw_state, 0,
  7167. sizeof(crtc_state->dpll_hw_state));
  7168. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7169. if (intel_panel_use_ssc(dev_priv)) {
  7170. refclk = dev_priv->vbt.lvds_ssc_freq;
  7171. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7172. }
  7173. if (intel_is_dual_link_lvds(dev))
  7174. limit = &intel_limits_g4x_dual_channel_lvds;
  7175. else
  7176. limit = &intel_limits_g4x_single_channel_lvds;
  7177. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  7178. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  7179. limit = &intel_limits_g4x_hdmi;
  7180. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  7181. limit = &intel_limits_g4x_sdvo;
  7182. } else {
  7183. /* The option is for other outputs */
  7184. limit = &intel_limits_i9xx_sdvo;
  7185. }
  7186. if (!crtc_state->clock_set &&
  7187. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7188. refclk, NULL, &crtc_state->dpll)) {
  7189. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7190. return -EINVAL;
  7191. }
  7192. i9xx_compute_dpll(crtc, crtc_state, NULL);
  7193. return 0;
  7194. }
  7195. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  7196. struct intel_crtc_state *crtc_state)
  7197. {
  7198. struct drm_device *dev = crtc->base.dev;
  7199. struct drm_i915_private *dev_priv = to_i915(dev);
  7200. const struct intel_limit *limit;
  7201. int refclk = 96000;
  7202. memset(&crtc_state->dpll_hw_state, 0,
  7203. sizeof(crtc_state->dpll_hw_state));
  7204. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7205. if (intel_panel_use_ssc(dev_priv)) {
  7206. refclk = dev_priv->vbt.lvds_ssc_freq;
  7207. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7208. }
  7209. limit = &intel_limits_pineview_lvds;
  7210. } else {
  7211. limit = &intel_limits_pineview_sdvo;
  7212. }
  7213. if (!crtc_state->clock_set &&
  7214. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7215. refclk, NULL, &crtc_state->dpll)) {
  7216. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7217. return -EINVAL;
  7218. }
  7219. i9xx_compute_dpll(crtc, crtc_state, NULL);
  7220. return 0;
  7221. }
  7222. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  7223. struct intel_crtc_state *crtc_state)
  7224. {
  7225. struct drm_device *dev = crtc->base.dev;
  7226. struct drm_i915_private *dev_priv = to_i915(dev);
  7227. const struct intel_limit *limit;
  7228. int refclk = 96000;
  7229. memset(&crtc_state->dpll_hw_state, 0,
  7230. sizeof(crtc_state->dpll_hw_state));
  7231. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7232. if (intel_panel_use_ssc(dev_priv)) {
  7233. refclk = dev_priv->vbt.lvds_ssc_freq;
  7234. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7235. }
  7236. limit = &intel_limits_i9xx_lvds;
  7237. } else {
  7238. limit = &intel_limits_i9xx_sdvo;
  7239. }
  7240. if (!crtc_state->clock_set &&
  7241. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7242. refclk, NULL, &crtc_state->dpll)) {
  7243. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7244. return -EINVAL;
  7245. }
  7246. i9xx_compute_dpll(crtc, crtc_state, NULL);
  7247. return 0;
  7248. }
  7249. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  7250. struct intel_crtc_state *crtc_state)
  7251. {
  7252. int refclk = 100000;
  7253. const struct intel_limit *limit = &intel_limits_chv;
  7254. memset(&crtc_state->dpll_hw_state, 0,
  7255. sizeof(crtc_state->dpll_hw_state));
  7256. if (!crtc_state->clock_set &&
  7257. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7258. refclk, NULL, &crtc_state->dpll)) {
  7259. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7260. return -EINVAL;
  7261. }
  7262. chv_compute_dpll(crtc, crtc_state);
  7263. return 0;
  7264. }
  7265. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  7266. struct intel_crtc_state *crtc_state)
  7267. {
  7268. int refclk = 100000;
  7269. const struct intel_limit *limit = &intel_limits_vlv;
  7270. memset(&crtc_state->dpll_hw_state, 0,
  7271. sizeof(crtc_state->dpll_hw_state));
  7272. if (!crtc_state->clock_set &&
  7273. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7274. refclk, NULL, &crtc_state->dpll)) {
  7275. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7276. return -EINVAL;
  7277. }
  7278. vlv_compute_dpll(crtc, crtc_state);
  7279. return 0;
  7280. }
  7281. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  7282. struct intel_crtc_state *pipe_config)
  7283. {
  7284. struct drm_device *dev = crtc->base.dev;
  7285. struct drm_i915_private *dev_priv = to_i915(dev);
  7286. uint32_t tmp;
  7287. if (INTEL_GEN(dev_priv) <= 3 &&
  7288. (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
  7289. return;
  7290. tmp = I915_READ(PFIT_CONTROL);
  7291. if (!(tmp & PFIT_ENABLE))
  7292. return;
  7293. /* Check whether the pfit is attached to our pipe. */
  7294. if (INTEL_INFO(dev)->gen < 4) {
  7295. if (crtc->pipe != PIPE_B)
  7296. return;
  7297. } else {
  7298. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  7299. return;
  7300. }
  7301. pipe_config->gmch_pfit.control = tmp;
  7302. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  7303. }
  7304. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  7305. struct intel_crtc_state *pipe_config)
  7306. {
  7307. struct drm_device *dev = crtc->base.dev;
  7308. struct drm_i915_private *dev_priv = to_i915(dev);
  7309. int pipe = pipe_config->cpu_transcoder;
  7310. struct dpll clock;
  7311. u32 mdiv;
  7312. int refclk = 100000;
  7313. /* In case of DSI, DPLL will not be used */
  7314. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  7315. return;
  7316. mutex_lock(&dev_priv->sb_lock);
  7317. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  7318. mutex_unlock(&dev_priv->sb_lock);
  7319. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  7320. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  7321. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  7322. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  7323. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  7324. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  7325. }
  7326. static void
  7327. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  7328. struct intel_initial_plane_config *plane_config)
  7329. {
  7330. struct drm_device *dev = crtc->base.dev;
  7331. struct drm_i915_private *dev_priv = to_i915(dev);
  7332. u32 val, base, offset;
  7333. int pipe = crtc->pipe, plane = crtc->plane;
  7334. int fourcc, pixel_format;
  7335. unsigned int aligned_height;
  7336. struct drm_framebuffer *fb;
  7337. struct intel_framebuffer *intel_fb;
  7338. val = I915_READ(DSPCNTR(plane));
  7339. if (!(val & DISPLAY_PLANE_ENABLE))
  7340. return;
  7341. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7342. if (!intel_fb) {
  7343. DRM_DEBUG_KMS("failed to alloc fb\n");
  7344. return;
  7345. }
  7346. fb = &intel_fb->base;
  7347. if (INTEL_INFO(dev)->gen >= 4) {
  7348. if (val & DISPPLANE_TILED) {
  7349. plane_config->tiling = I915_TILING_X;
  7350. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  7351. }
  7352. }
  7353. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7354. fourcc = i9xx_format_to_fourcc(pixel_format);
  7355. fb->pixel_format = fourcc;
  7356. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  7357. if (INTEL_INFO(dev)->gen >= 4) {
  7358. if (plane_config->tiling)
  7359. offset = I915_READ(DSPTILEOFF(plane));
  7360. else
  7361. offset = I915_READ(DSPLINOFF(plane));
  7362. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  7363. } else {
  7364. base = I915_READ(DSPADDR(plane));
  7365. }
  7366. plane_config->base = base;
  7367. val = I915_READ(PIPESRC(pipe));
  7368. fb->width = ((val >> 16) & 0xfff) + 1;
  7369. fb->height = ((val >> 0) & 0xfff) + 1;
  7370. val = I915_READ(DSPSTRIDE(pipe));
  7371. fb->pitches[0] = val & 0xffffffc0;
  7372. aligned_height = intel_fb_align_height(dev, fb->height,
  7373. fb->pixel_format,
  7374. fb->modifier[0]);
  7375. plane_config->size = fb->pitches[0] * aligned_height;
  7376. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7377. pipe_name(pipe), plane, fb->width, fb->height,
  7378. fb->bits_per_pixel, base, fb->pitches[0],
  7379. plane_config->size);
  7380. plane_config->fb = intel_fb;
  7381. }
  7382. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  7383. struct intel_crtc_state *pipe_config)
  7384. {
  7385. struct drm_device *dev = crtc->base.dev;
  7386. struct drm_i915_private *dev_priv = to_i915(dev);
  7387. int pipe = pipe_config->cpu_transcoder;
  7388. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  7389. struct dpll clock;
  7390. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  7391. int refclk = 100000;
  7392. /* In case of DSI, DPLL will not be used */
  7393. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  7394. return;
  7395. mutex_lock(&dev_priv->sb_lock);
  7396. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  7397. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  7398. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  7399. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  7400. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  7401. mutex_unlock(&dev_priv->sb_lock);
  7402. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  7403. clock.m2 = (pll_dw0 & 0xff) << 22;
  7404. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  7405. clock.m2 |= pll_dw2 & 0x3fffff;
  7406. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  7407. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  7408. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  7409. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  7410. }
  7411. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  7412. struct intel_crtc_state *pipe_config)
  7413. {
  7414. struct drm_device *dev = crtc->base.dev;
  7415. struct drm_i915_private *dev_priv = to_i915(dev);
  7416. enum intel_display_power_domain power_domain;
  7417. uint32_t tmp;
  7418. bool ret;
  7419. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7420. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7421. return false;
  7422. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7423. pipe_config->shared_dpll = NULL;
  7424. ret = false;
  7425. tmp = I915_READ(PIPECONF(crtc->pipe));
  7426. if (!(tmp & PIPECONF_ENABLE))
  7427. goto out;
  7428. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  7429. IS_CHERRYVIEW(dev_priv)) {
  7430. switch (tmp & PIPECONF_BPC_MASK) {
  7431. case PIPECONF_6BPC:
  7432. pipe_config->pipe_bpp = 18;
  7433. break;
  7434. case PIPECONF_8BPC:
  7435. pipe_config->pipe_bpp = 24;
  7436. break;
  7437. case PIPECONF_10BPC:
  7438. pipe_config->pipe_bpp = 30;
  7439. break;
  7440. default:
  7441. break;
  7442. }
  7443. }
  7444. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  7445. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  7446. pipe_config->limited_color_range = true;
  7447. if (INTEL_INFO(dev)->gen < 4)
  7448. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  7449. intel_get_pipe_timings(crtc, pipe_config);
  7450. intel_get_pipe_src_size(crtc, pipe_config);
  7451. i9xx_get_pfit_config(crtc, pipe_config);
  7452. if (INTEL_INFO(dev)->gen >= 4) {
  7453. /* No way to read it out on pipes B and C */
  7454. if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
  7455. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  7456. else
  7457. tmp = I915_READ(DPLL_MD(crtc->pipe));
  7458. pipe_config->pixel_multiplier =
  7459. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  7460. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  7461. pipe_config->dpll_hw_state.dpll_md = tmp;
  7462. } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  7463. IS_G33(dev_priv)) {
  7464. tmp = I915_READ(DPLL(crtc->pipe));
  7465. pipe_config->pixel_multiplier =
  7466. ((tmp & SDVO_MULTIPLIER_MASK)
  7467. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  7468. } else {
  7469. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  7470. * port and will be fixed up in the encoder->get_config
  7471. * function. */
  7472. pipe_config->pixel_multiplier = 1;
  7473. }
  7474. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  7475. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  7476. /*
  7477. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  7478. * on 830. Filter it out here so that we don't
  7479. * report errors due to that.
  7480. */
  7481. if (IS_I830(dev_priv))
  7482. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  7483. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  7484. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  7485. } else {
  7486. /* Mask out read-only status bits. */
  7487. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  7488. DPLL_PORTC_READY_MASK |
  7489. DPLL_PORTB_READY_MASK);
  7490. }
  7491. if (IS_CHERRYVIEW(dev_priv))
  7492. chv_crtc_clock_get(crtc, pipe_config);
  7493. else if (IS_VALLEYVIEW(dev_priv))
  7494. vlv_crtc_clock_get(crtc, pipe_config);
  7495. else
  7496. i9xx_crtc_clock_get(crtc, pipe_config);
  7497. /*
  7498. * Normally the dotclock is filled in by the encoder .get_config()
  7499. * but in case the pipe is enabled w/o any ports we need a sane
  7500. * default.
  7501. */
  7502. pipe_config->base.adjusted_mode.crtc_clock =
  7503. pipe_config->port_clock / pipe_config->pixel_multiplier;
  7504. ret = true;
  7505. out:
  7506. intel_display_power_put(dev_priv, power_domain);
  7507. return ret;
  7508. }
  7509. static void ironlake_init_pch_refclk(struct drm_device *dev)
  7510. {
  7511. struct drm_i915_private *dev_priv = to_i915(dev);
  7512. struct intel_encoder *encoder;
  7513. int i;
  7514. u32 val, final;
  7515. bool has_lvds = false;
  7516. bool has_cpu_edp = false;
  7517. bool has_panel = false;
  7518. bool has_ck505 = false;
  7519. bool can_ssc = false;
  7520. bool using_ssc_source = false;
  7521. /* We need to take the global config into account */
  7522. for_each_intel_encoder(dev, encoder) {
  7523. switch (encoder->type) {
  7524. case INTEL_OUTPUT_LVDS:
  7525. has_panel = true;
  7526. has_lvds = true;
  7527. break;
  7528. case INTEL_OUTPUT_EDP:
  7529. has_panel = true;
  7530. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  7531. has_cpu_edp = true;
  7532. break;
  7533. default:
  7534. break;
  7535. }
  7536. }
  7537. if (HAS_PCH_IBX(dev_priv)) {
  7538. has_ck505 = dev_priv->vbt.display_clock_mode;
  7539. can_ssc = has_ck505;
  7540. } else {
  7541. has_ck505 = false;
  7542. can_ssc = true;
  7543. }
  7544. /* Check if any DPLLs are using the SSC source */
  7545. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  7546. u32 temp = I915_READ(PCH_DPLL(i));
  7547. if (!(temp & DPLL_VCO_ENABLE))
  7548. continue;
  7549. if ((temp & PLL_REF_INPUT_MASK) ==
  7550. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  7551. using_ssc_source = true;
  7552. break;
  7553. }
  7554. }
  7555. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  7556. has_panel, has_lvds, has_ck505, using_ssc_source);
  7557. /* Ironlake: try to setup display ref clock before DPLL
  7558. * enabling. This is only under driver's control after
  7559. * PCH B stepping, previous chipset stepping should be
  7560. * ignoring this setting.
  7561. */
  7562. val = I915_READ(PCH_DREF_CONTROL);
  7563. /* As we must carefully and slowly disable/enable each source in turn,
  7564. * compute the final state we want first and check if we need to
  7565. * make any changes at all.
  7566. */
  7567. final = val;
  7568. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  7569. if (has_ck505)
  7570. final |= DREF_NONSPREAD_CK505_ENABLE;
  7571. else
  7572. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  7573. final &= ~DREF_SSC_SOURCE_MASK;
  7574. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7575. final &= ~DREF_SSC1_ENABLE;
  7576. if (has_panel) {
  7577. final |= DREF_SSC_SOURCE_ENABLE;
  7578. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7579. final |= DREF_SSC1_ENABLE;
  7580. if (has_cpu_edp) {
  7581. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7582. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7583. else
  7584. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7585. } else
  7586. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7587. } else if (using_ssc_source) {
  7588. final |= DREF_SSC_SOURCE_ENABLE;
  7589. final |= DREF_SSC1_ENABLE;
  7590. }
  7591. if (final == val)
  7592. return;
  7593. /* Always enable nonspread source */
  7594. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  7595. if (has_ck505)
  7596. val |= DREF_NONSPREAD_CK505_ENABLE;
  7597. else
  7598. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  7599. if (has_panel) {
  7600. val &= ~DREF_SSC_SOURCE_MASK;
  7601. val |= DREF_SSC_SOURCE_ENABLE;
  7602. /* SSC must be turned on before enabling the CPU output */
  7603. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7604. DRM_DEBUG_KMS("Using SSC on panel\n");
  7605. val |= DREF_SSC1_ENABLE;
  7606. } else
  7607. val &= ~DREF_SSC1_ENABLE;
  7608. /* Get SSC going before enabling the outputs */
  7609. I915_WRITE(PCH_DREF_CONTROL, val);
  7610. POSTING_READ(PCH_DREF_CONTROL);
  7611. udelay(200);
  7612. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7613. /* Enable CPU source on CPU attached eDP */
  7614. if (has_cpu_edp) {
  7615. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7616. DRM_DEBUG_KMS("Using SSC on eDP\n");
  7617. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7618. } else
  7619. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7620. } else
  7621. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7622. I915_WRITE(PCH_DREF_CONTROL, val);
  7623. POSTING_READ(PCH_DREF_CONTROL);
  7624. udelay(200);
  7625. } else {
  7626. DRM_DEBUG_KMS("Disabling CPU source output\n");
  7627. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7628. /* Turn off CPU output */
  7629. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7630. I915_WRITE(PCH_DREF_CONTROL, val);
  7631. POSTING_READ(PCH_DREF_CONTROL);
  7632. udelay(200);
  7633. if (!using_ssc_source) {
  7634. DRM_DEBUG_KMS("Disabling SSC source\n");
  7635. /* Turn off the SSC source */
  7636. val &= ~DREF_SSC_SOURCE_MASK;
  7637. val |= DREF_SSC_SOURCE_DISABLE;
  7638. /* Turn off SSC1 */
  7639. val &= ~DREF_SSC1_ENABLE;
  7640. I915_WRITE(PCH_DREF_CONTROL, val);
  7641. POSTING_READ(PCH_DREF_CONTROL);
  7642. udelay(200);
  7643. }
  7644. }
  7645. BUG_ON(val != final);
  7646. }
  7647. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  7648. {
  7649. uint32_t tmp;
  7650. tmp = I915_READ(SOUTH_CHICKEN2);
  7651. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  7652. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7653. if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
  7654. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  7655. DRM_ERROR("FDI mPHY reset assert timeout\n");
  7656. tmp = I915_READ(SOUTH_CHICKEN2);
  7657. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  7658. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7659. if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
  7660. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  7661. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  7662. }
  7663. /* WaMPhyProgramming:hsw */
  7664. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  7665. {
  7666. uint32_t tmp;
  7667. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  7668. tmp &= ~(0xFF << 24);
  7669. tmp |= (0x12 << 24);
  7670. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  7671. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  7672. tmp |= (1 << 11);
  7673. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  7674. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  7675. tmp |= (1 << 11);
  7676. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  7677. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  7678. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7679. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7680. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7681. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7682. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7683. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7684. tmp &= ~(7 << 13);
  7685. tmp |= (5 << 13);
  7686. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7687. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7688. tmp &= ~(7 << 13);
  7689. tmp |= (5 << 13);
  7690. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7691. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7692. tmp &= ~0xFF;
  7693. tmp |= 0x1C;
  7694. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7695. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7696. tmp &= ~0xFF;
  7697. tmp |= 0x1C;
  7698. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7699. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7700. tmp &= ~(0xFF << 16);
  7701. tmp |= (0x1C << 16);
  7702. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7703. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7704. tmp &= ~(0xFF << 16);
  7705. tmp |= (0x1C << 16);
  7706. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7707. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7708. tmp |= (1 << 27);
  7709. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7710. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7711. tmp |= (1 << 27);
  7712. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7713. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7714. tmp &= ~(0xF << 28);
  7715. tmp |= (4 << 28);
  7716. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7717. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7718. tmp &= ~(0xF << 28);
  7719. tmp |= (4 << 28);
  7720. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7721. }
  7722. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7723. * Programming" based on the parameters passed:
  7724. * - Sequence to enable CLKOUT_DP
  7725. * - Sequence to enable CLKOUT_DP without spread
  7726. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7727. */
  7728. static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
  7729. bool with_fdi)
  7730. {
  7731. struct drm_i915_private *dev_priv = to_i915(dev);
  7732. uint32_t reg, tmp;
  7733. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7734. with_spread = true;
  7735. if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
  7736. with_fdi, "LP PCH doesn't have FDI\n"))
  7737. with_fdi = false;
  7738. mutex_lock(&dev_priv->sb_lock);
  7739. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7740. tmp &= ~SBI_SSCCTL_DISABLE;
  7741. tmp |= SBI_SSCCTL_PATHALT;
  7742. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7743. udelay(24);
  7744. if (with_spread) {
  7745. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7746. tmp &= ~SBI_SSCCTL_PATHALT;
  7747. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7748. if (with_fdi) {
  7749. lpt_reset_fdi_mphy(dev_priv);
  7750. lpt_program_fdi_mphy(dev_priv);
  7751. }
  7752. }
  7753. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  7754. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7755. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7756. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7757. mutex_unlock(&dev_priv->sb_lock);
  7758. }
  7759. /* Sequence to disable CLKOUT_DP */
  7760. static void lpt_disable_clkout_dp(struct drm_device *dev)
  7761. {
  7762. struct drm_i915_private *dev_priv = to_i915(dev);
  7763. uint32_t reg, tmp;
  7764. mutex_lock(&dev_priv->sb_lock);
  7765. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  7766. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7767. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7768. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7769. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7770. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7771. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7772. tmp |= SBI_SSCCTL_PATHALT;
  7773. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7774. udelay(32);
  7775. }
  7776. tmp |= SBI_SSCCTL_DISABLE;
  7777. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7778. }
  7779. mutex_unlock(&dev_priv->sb_lock);
  7780. }
  7781. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  7782. static const uint16_t sscdivintphase[] = {
  7783. [BEND_IDX( 50)] = 0x3B23,
  7784. [BEND_IDX( 45)] = 0x3B23,
  7785. [BEND_IDX( 40)] = 0x3C23,
  7786. [BEND_IDX( 35)] = 0x3C23,
  7787. [BEND_IDX( 30)] = 0x3D23,
  7788. [BEND_IDX( 25)] = 0x3D23,
  7789. [BEND_IDX( 20)] = 0x3E23,
  7790. [BEND_IDX( 15)] = 0x3E23,
  7791. [BEND_IDX( 10)] = 0x3F23,
  7792. [BEND_IDX( 5)] = 0x3F23,
  7793. [BEND_IDX( 0)] = 0x0025,
  7794. [BEND_IDX( -5)] = 0x0025,
  7795. [BEND_IDX(-10)] = 0x0125,
  7796. [BEND_IDX(-15)] = 0x0125,
  7797. [BEND_IDX(-20)] = 0x0225,
  7798. [BEND_IDX(-25)] = 0x0225,
  7799. [BEND_IDX(-30)] = 0x0325,
  7800. [BEND_IDX(-35)] = 0x0325,
  7801. [BEND_IDX(-40)] = 0x0425,
  7802. [BEND_IDX(-45)] = 0x0425,
  7803. [BEND_IDX(-50)] = 0x0525,
  7804. };
  7805. /*
  7806. * Bend CLKOUT_DP
  7807. * steps -50 to 50 inclusive, in steps of 5
  7808. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  7809. * change in clock period = -(steps / 10) * 5.787 ps
  7810. */
  7811. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  7812. {
  7813. uint32_t tmp;
  7814. int idx = BEND_IDX(steps);
  7815. if (WARN_ON(steps % 5 != 0))
  7816. return;
  7817. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  7818. return;
  7819. mutex_lock(&dev_priv->sb_lock);
  7820. if (steps % 10 != 0)
  7821. tmp = 0xAAAAAAAB;
  7822. else
  7823. tmp = 0x00000000;
  7824. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  7825. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  7826. tmp &= 0xffff0000;
  7827. tmp |= sscdivintphase[idx];
  7828. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  7829. mutex_unlock(&dev_priv->sb_lock);
  7830. }
  7831. #undef BEND_IDX
  7832. static void lpt_init_pch_refclk(struct drm_device *dev)
  7833. {
  7834. struct intel_encoder *encoder;
  7835. bool has_vga = false;
  7836. for_each_intel_encoder(dev, encoder) {
  7837. switch (encoder->type) {
  7838. case INTEL_OUTPUT_ANALOG:
  7839. has_vga = true;
  7840. break;
  7841. default:
  7842. break;
  7843. }
  7844. }
  7845. if (has_vga) {
  7846. lpt_bend_clkout_dp(to_i915(dev), 0);
  7847. lpt_enable_clkout_dp(dev, true, true);
  7848. } else {
  7849. lpt_disable_clkout_dp(dev);
  7850. }
  7851. }
  7852. /*
  7853. * Initialize reference clocks when the driver loads
  7854. */
  7855. void intel_init_pch_refclk(struct drm_device *dev)
  7856. {
  7857. struct drm_i915_private *dev_priv = to_i915(dev);
  7858. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
  7859. ironlake_init_pch_refclk(dev);
  7860. else if (HAS_PCH_LPT(dev_priv))
  7861. lpt_init_pch_refclk(dev);
  7862. }
  7863. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7864. {
  7865. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7866. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7867. int pipe = intel_crtc->pipe;
  7868. uint32_t val;
  7869. val = 0;
  7870. switch (intel_crtc->config->pipe_bpp) {
  7871. case 18:
  7872. val |= PIPECONF_6BPC;
  7873. break;
  7874. case 24:
  7875. val |= PIPECONF_8BPC;
  7876. break;
  7877. case 30:
  7878. val |= PIPECONF_10BPC;
  7879. break;
  7880. case 36:
  7881. val |= PIPECONF_12BPC;
  7882. break;
  7883. default:
  7884. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7885. BUG();
  7886. }
  7887. if (intel_crtc->config->dither)
  7888. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7889. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7890. val |= PIPECONF_INTERLACED_ILK;
  7891. else
  7892. val |= PIPECONF_PROGRESSIVE;
  7893. if (intel_crtc->config->limited_color_range)
  7894. val |= PIPECONF_COLOR_RANGE_SELECT;
  7895. I915_WRITE(PIPECONF(pipe), val);
  7896. POSTING_READ(PIPECONF(pipe));
  7897. }
  7898. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7899. {
  7900. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7901. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7902. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7903. u32 val = 0;
  7904. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  7905. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7906. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7907. val |= PIPECONF_INTERLACED_ILK;
  7908. else
  7909. val |= PIPECONF_PROGRESSIVE;
  7910. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7911. POSTING_READ(PIPECONF(cpu_transcoder));
  7912. }
  7913. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  7914. {
  7915. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7916. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7917. if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
  7918. u32 val = 0;
  7919. switch (intel_crtc->config->pipe_bpp) {
  7920. case 18:
  7921. val |= PIPEMISC_DITHER_6_BPC;
  7922. break;
  7923. case 24:
  7924. val |= PIPEMISC_DITHER_8_BPC;
  7925. break;
  7926. case 30:
  7927. val |= PIPEMISC_DITHER_10_BPC;
  7928. break;
  7929. case 36:
  7930. val |= PIPEMISC_DITHER_12_BPC;
  7931. break;
  7932. default:
  7933. /* Case prevented by pipe_config_set_bpp. */
  7934. BUG();
  7935. }
  7936. if (intel_crtc->config->dither)
  7937. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7938. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  7939. }
  7940. }
  7941. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7942. {
  7943. /*
  7944. * Account for spread spectrum to avoid
  7945. * oversubscribing the link. Max center spread
  7946. * is 2.5%; use 5% for safety's sake.
  7947. */
  7948. u32 bps = target_clock * bpp * 21 / 20;
  7949. return DIV_ROUND_UP(bps, link_bw * 8);
  7950. }
  7951. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7952. {
  7953. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7954. }
  7955. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7956. struct intel_crtc_state *crtc_state,
  7957. struct dpll *reduced_clock)
  7958. {
  7959. struct drm_crtc *crtc = &intel_crtc->base;
  7960. struct drm_device *dev = crtc->dev;
  7961. struct drm_i915_private *dev_priv = to_i915(dev);
  7962. u32 dpll, fp, fp2;
  7963. int factor;
  7964. /* Enable autotuning of the PLL clock (if permissible) */
  7965. factor = 21;
  7966. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7967. if ((intel_panel_use_ssc(dev_priv) &&
  7968. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7969. (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
  7970. factor = 25;
  7971. } else if (crtc_state->sdvo_tv_clock)
  7972. factor = 20;
  7973. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7974. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7975. fp |= FP_CB_TUNE;
  7976. if (reduced_clock) {
  7977. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  7978. if (reduced_clock->m < factor * reduced_clock->n)
  7979. fp2 |= FP_CB_TUNE;
  7980. } else {
  7981. fp2 = fp;
  7982. }
  7983. dpll = 0;
  7984. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  7985. dpll |= DPLLB_MODE_LVDS;
  7986. else
  7987. dpll |= DPLLB_MODE_DAC_SERIAL;
  7988. dpll |= (crtc_state->pixel_multiplier - 1)
  7989. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7990. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  7991. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  7992. dpll |= DPLL_SDVO_HIGH_SPEED;
  7993. if (intel_crtc_has_dp_encoder(crtc_state))
  7994. dpll |= DPLL_SDVO_HIGH_SPEED;
  7995. /*
  7996. * The high speed IO clock is only really required for
  7997. * SDVO/HDMI/DP, but we also enable it for CRT to make it
  7998. * possible to share the DPLL between CRT and HDMI. Enabling
  7999. * the clock needlessly does no real harm, except use up a
  8000. * bit of power potentially.
  8001. *
  8002. * We'll limit this to IVB with 3 pipes, since it has only two
  8003. * DPLLs and so DPLL sharing is the only way to get three pipes
  8004. * driving PCH ports at the same time. On SNB we could do this,
  8005. * and potentially avoid enabling the second DPLL, but it's not
  8006. * clear if it''s a win or loss power wise. No point in doing
  8007. * this on ILK at all since it has a fixed DPLL<->pipe mapping.
  8008. */
  8009. if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
  8010. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
  8011. dpll |= DPLL_SDVO_HIGH_SPEED;
  8012. /* compute bitmask from p1 value */
  8013. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  8014. /* also FPA1 */
  8015. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  8016. switch (crtc_state->dpll.p2) {
  8017. case 5:
  8018. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  8019. break;
  8020. case 7:
  8021. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  8022. break;
  8023. case 10:
  8024. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  8025. break;
  8026. case 14:
  8027. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  8028. break;
  8029. }
  8030. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  8031. intel_panel_use_ssc(dev_priv))
  8032. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  8033. else
  8034. dpll |= PLL_REF_INPUT_DREFCLK;
  8035. dpll |= DPLL_VCO_ENABLE;
  8036. crtc_state->dpll_hw_state.dpll = dpll;
  8037. crtc_state->dpll_hw_state.fp0 = fp;
  8038. crtc_state->dpll_hw_state.fp1 = fp2;
  8039. }
  8040. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  8041. struct intel_crtc_state *crtc_state)
  8042. {
  8043. struct drm_device *dev = crtc->base.dev;
  8044. struct drm_i915_private *dev_priv = to_i915(dev);
  8045. struct dpll reduced_clock;
  8046. bool has_reduced_clock = false;
  8047. struct intel_shared_dpll *pll;
  8048. const struct intel_limit *limit;
  8049. int refclk = 120000;
  8050. memset(&crtc_state->dpll_hw_state, 0,
  8051. sizeof(crtc_state->dpll_hw_state));
  8052. crtc->lowfreq_avail = false;
  8053. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  8054. if (!crtc_state->has_pch_encoder)
  8055. return 0;
  8056. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  8057. if (intel_panel_use_ssc(dev_priv)) {
  8058. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  8059. dev_priv->vbt.lvds_ssc_freq);
  8060. refclk = dev_priv->vbt.lvds_ssc_freq;
  8061. }
  8062. if (intel_is_dual_link_lvds(dev)) {
  8063. if (refclk == 100000)
  8064. limit = &intel_limits_ironlake_dual_lvds_100m;
  8065. else
  8066. limit = &intel_limits_ironlake_dual_lvds;
  8067. } else {
  8068. if (refclk == 100000)
  8069. limit = &intel_limits_ironlake_single_lvds_100m;
  8070. else
  8071. limit = &intel_limits_ironlake_single_lvds;
  8072. }
  8073. } else {
  8074. limit = &intel_limits_ironlake_dac;
  8075. }
  8076. if (!crtc_state->clock_set &&
  8077. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  8078. refclk, NULL, &crtc_state->dpll)) {
  8079. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  8080. return -EINVAL;
  8081. }
  8082. ironlake_compute_dpll(crtc, crtc_state,
  8083. has_reduced_clock ? &reduced_clock : NULL);
  8084. pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
  8085. if (pll == NULL) {
  8086. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  8087. pipe_name(crtc->pipe));
  8088. return -EINVAL;
  8089. }
  8090. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  8091. has_reduced_clock)
  8092. crtc->lowfreq_avail = true;
  8093. return 0;
  8094. }
  8095. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  8096. struct intel_link_m_n *m_n)
  8097. {
  8098. struct drm_device *dev = crtc->base.dev;
  8099. struct drm_i915_private *dev_priv = to_i915(dev);
  8100. enum pipe pipe = crtc->pipe;
  8101. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  8102. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  8103. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  8104. & ~TU_SIZE_MASK;
  8105. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  8106. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  8107. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8108. }
  8109. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  8110. enum transcoder transcoder,
  8111. struct intel_link_m_n *m_n,
  8112. struct intel_link_m_n *m2_n2)
  8113. {
  8114. struct drm_device *dev = crtc->base.dev;
  8115. struct drm_i915_private *dev_priv = to_i915(dev);
  8116. enum pipe pipe = crtc->pipe;
  8117. if (INTEL_INFO(dev)->gen >= 5) {
  8118. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  8119. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  8120. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  8121. & ~TU_SIZE_MASK;
  8122. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  8123. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  8124. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8125. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  8126. * gen < 8) and if DRRS is supported (to make sure the
  8127. * registers are not unnecessarily read).
  8128. */
  8129. if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
  8130. crtc->config->has_drrs) {
  8131. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  8132. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  8133. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  8134. & ~TU_SIZE_MASK;
  8135. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  8136. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  8137. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8138. }
  8139. } else {
  8140. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  8141. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  8142. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  8143. & ~TU_SIZE_MASK;
  8144. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  8145. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  8146. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8147. }
  8148. }
  8149. void intel_dp_get_m_n(struct intel_crtc *crtc,
  8150. struct intel_crtc_state *pipe_config)
  8151. {
  8152. if (pipe_config->has_pch_encoder)
  8153. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  8154. else
  8155. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  8156. &pipe_config->dp_m_n,
  8157. &pipe_config->dp_m2_n2);
  8158. }
  8159. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  8160. struct intel_crtc_state *pipe_config)
  8161. {
  8162. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  8163. &pipe_config->fdi_m_n, NULL);
  8164. }
  8165. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  8166. struct intel_crtc_state *pipe_config)
  8167. {
  8168. struct drm_device *dev = crtc->base.dev;
  8169. struct drm_i915_private *dev_priv = to_i915(dev);
  8170. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  8171. uint32_t ps_ctrl = 0;
  8172. int id = -1;
  8173. int i;
  8174. /* find scaler attached to this pipe */
  8175. for (i = 0; i < crtc->num_scalers; i++) {
  8176. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  8177. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  8178. id = i;
  8179. pipe_config->pch_pfit.enabled = true;
  8180. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  8181. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  8182. break;
  8183. }
  8184. }
  8185. scaler_state->scaler_id = id;
  8186. if (id >= 0) {
  8187. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  8188. } else {
  8189. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8190. }
  8191. }
  8192. static void
  8193. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  8194. struct intel_initial_plane_config *plane_config)
  8195. {
  8196. struct drm_device *dev = crtc->base.dev;
  8197. struct drm_i915_private *dev_priv = to_i915(dev);
  8198. u32 val, base, offset, stride_mult, tiling;
  8199. int pipe = crtc->pipe;
  8200. int fourcc, pixel_format;
  8201. unsigned int aligned_height;
  8202. struct drm_framebuffer *fb;
  8203. struct intel_framebuffer *intel_fb;
  8204. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8205. if (!intel_fb) {
  8206. DRM_DEBUG_KMS("failed to alloc fb\n");
  8207. return;
  8208. }
  8209. fb = &intel_fb->base;
  8210. val = I915_READ(PLANE_CTL(pipe, 0));
  8211. if (!(val & PLANE_CTL_ENABLE))
  8212. goto error;
  8213. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  8214. fourcc = skl_format_to_fourcc(pixel_format,
  8215. val & PLANE_CTL_ORDER_RGBX,
  8216. val & PLANE_CTL_ALPHA_MASK);
  8217. fb->pixel_format = fourcc;
  8218. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  8219. tiling = val & PLANE_CTL_TILED_MASK;
  8220. switch (tiling) {
  8221. case PLANE_CTL_TILED_LINEAR:
  8222. fb->modifier[0] = DRM_FORMAT_MOD_NONE;
  8223. break;
  8224. case PLANE_CTL_TILED_X:
  8225. plane_config->tiling = I915_TILING_X;
  8226. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  8227. break;
  8228. case PLANE_CTL_TILED_Y:
  8229. fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
  8230. break;
  8231. case PLANE_CTL_TILED_YF:
  8232. fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
  8233. break;
  8234. default:
  8235. MISSING_CASE(tiling);
  8236. goto error;
  8237. }
  8238. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  8239. plane_config->base = base;
  8240. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  8241. val = I915_READ(PLANE_SIZE(pipe, 0));
  8242. fb->height = ((val >> 16) & 0xfff) + 1;
  8243. fb->width = ((val >> 0) & 0x1fff) + 1;
  8244. val = I915_READ(PLANE_STRIDE(pipe, 0));
  8245. stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier[0],
  8246. fb->pixel_format);
  8247. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  8248. aligned_height = intel_fb_align_height(dev, fb->height,
  8249. fb->pixel_format,
  8250. fb->modifier[0]);
  8251. plane_config->size = fb->pitches[0] * aligned_height;
  8252. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  8253. pipe_name(pipe), fb->width, fb->height,
  8254. fb->bits_per_pixel, base, fb->pitches[0],
  8255. plane_config->size);
  8256. plane_config->fb = intel_fb;
  8257. return;
  8258. error:
  8259. kfree(intel_fb);
  8260. }
  8261. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  8262. struct intel_crtc_state *pipe_config)
  8263. {
  8264. struct drm_device *dev = crtc->base.dev;
  8265. struct drm_i915_private *dev_priv = to_i915(dev);
  8266. uint32_t tmp;
  8267. tmp = I915_READ(PF_CTL(crtc->pipe));
  8268. if (tmp & PF_ENABLE) {
  8269. pipe_config->pch_pfit.enabled = true;
  8270. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  8271. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  8272. /* We currently do not free assignements of panel fitters on
  8273. * ivb/hsw (since we don't use the higher upscaling modes which
  8274. * differentiates them) so just WARN about this case for now. */
  8275. if (IS_GEN7(dev_priv)) {
  8276. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  8277. PF_PIPE_SEL_IVB(crtc->pipe));
  8278. }
  8279. }
  8280. }
  8281. static void
  8282. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  8283. struct intel_initial_plane_config *plane_config)
  8284. {
  8285. struct drm_device *dev = crtc->base.dev;
  8286. struct drm_i915_private *dev_priv = to_i915(dev);
  8287. u32 val, base, offset;
  8288. int pipe = crtc->pipe;
  8289. int fourcc, pixel_format;
  8290. unsigned int aligned_height;
  8291. struct drm_framebuffer *fb;
  8292. struct intel_framebuffer *intel_fb;
  8293. val = I915_READ(DSPCNTR(pipe));
  8294. if (!(val & DISPLAY_PLANE_ENABLE))
  8295. return;
  8296. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8297. if (!intel_fb) {
  8298. DRM_DEBUG_KMS("failed to alloc fb\n");
  8299. return;
  8300. }
  8301. fb = &intel_fb->base;
  8302. if (INTEL_INFO(dev)->gen >= 4) {
  8303. if (val & DISPPLANE_TILED) {
  8304. plane_config->tiling = I915_TILING_X;
  8305. fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
  8306. }
  8307. }
  8308. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  8309. fourcc = i9xx_format_to_fourcc(pixel_format);
  8310. fb->pixel_format = fourcc;
  8311. fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
  8312. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  8313. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  8314. offset = I915_READ(DSPOFFSET(pipe));
  8315. } else {
  8316. if (plane_config->tiling)
  8317. offset = I915_READ(DSPTILEOFF(pipe));
  8318. else
  8319. offset = I915_READ(DSPLINOFF(pipe));
  8320. }
  8321. plane_config->base = base;
  8322. val = I915_READ(PIPESRC(pipe));
  8323. fb->width = ((val >> 16) & 0xfff) + 1;
  8324. fb->height = ((val >> 0) & 0xfff) + 1;
  8325. val = I915_READ(DSPSTRIDE(pipe));
  8326. fb->pitches[0] = val & 0xffffffc0;
  8327. aligned_height = intel_fb_align_height(dev, fb->height,
  8328. fb->pixel_format,
  8329. fb->modifier[0]);
  8330. plane_config->size = fb->pitches[0] * aligned_height;
  8331. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  8332. pipe_name(pipe), fb->width, fb->height,
  8333. fb->bits_per_pixel, base, fb->pitches[0],
  8334. plane_config->size);
  8335. plane_config->fb = intel_fb;
  8336. }
  8337. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  8338. struct intel_crtc_state *pipe_config)
  8339. {
  8340. struct drm_device *dev = crtc->base.dev;
  8341. struct drm_i915_private *dev_priv = to_i915(dev);
  8342. enum intel_display_power_domain power_domain;
  8343. uint32_t tmp;
  8344. bool ret;
  8345. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  8346. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8347. return false;
  8348. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8349. pipe_config->shared_dpll = NULL;
  8350. ret = false;
  8351. tmp = I915_READ(PIPECONF(crtc->pipe));
  8352. if (!(tmp & PIPECONF_ENABLE))
  8353. goto out;
  8354. switch (tmp & PIPECONF_BPC_MASK) {
  8355. case PIPECONF_6BPC:
  8356. pipe_config->pipe_bpp = 18;
  8357. break;
  8358. case PIPECONF_8BPC:
  8359. pipe_config->pipe_bpp = 24;
  8360. break;
  8361. case PIPECONF_10BPC:
  8362. pipe_config->pipe_bpp = 30;
  8363. break;
  8364. case PIPECONF_12BPC:
  8365. pipe_config->pipe_bpp = 36;
  8366. break;
  8367. default:
  8368. break;
  8369. }
  8370. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  8371. pipe_config->limited_color_range = true;
  8372. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  8373. struct intel_shared_dpll *pll;
  8374. enum intel_dpll_id pll_id;
  8375. pipe_config->has_pch_encoder = true;
  8376. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  8377. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8378. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8379. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8380. if (HAS_PCH_IBX(dev_priv)) {
  8381. /*
  8382. * The pipe->pch transcoder and pch transcoder->pll
  8383. * mapping is fixed.
  8384. */
  8385. pll_id = (enum intel_dpll_id) crtc->pipe;
  8386. } else {
  8387. tmp = I915_READ(PCH_DPLL_SEL);
  8388. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  8389. pll_id = DPLL_ID_PCH_PLL_B;
  8390. else
  8391. pll_id= DPLL_ID_PCH_PLL_A;
  8392. }
  8393. pipe_config->shared_dpll =
  8394. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  8395. pll = pipe_config->shared_dpll;
  8396. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  8397. &pipe_config->dpll_hw_state));
  8398. tmp = pipe_config->dpll_hw_state.dpll;
  8399. pipe_config->pixel_multiplier =
  8400. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  8401. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  8402. ironlake_pch_clock_get(crtc, pipe_config);
  8403. } else {
  8404. pipe_config->pixel_multiplier = 1;
  8405. }
  8406. intel_get_pipe_timings(crtc, pipe_config);
  8407. intel_get_pipe_src_size(crtc, pipe_config);
  8408. ironlake_get_pfit_config(crtc, pipe_config);
  8409. ret = true;
  8410. out:
  8411. intel_display_power_put(dev_priv, power_domain);
  8412. return ret;
  8413. }
  8414. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  8415. {
  8416. struct drm_device *dev = &dev_priv->drm;
  8417. struct intel_crtc *crtc;
  8418. for_each_intel_crtc(dev, crtc)
  8419. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  8420. pipe_name(crtc->pipe));
  8421. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  8422. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  8423. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  8424. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  8425. I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
  8426. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  8427. "CPU PWM1 enabled\n");
  8428. if (IS_HASWELL(dev_priv))
  8429. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  8430. "CPU PWM2 enabled\n");
  8431. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  8432. "PCH PWM1 enabled\n");
  8433. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  8434. "Utility pin enabled\n");
  8435. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  8436. /*
  8437. * In theory we can still leave IRQs enabled, as long as only the HPD
  8438. * interrupts remain enabled. We used to check for that, but since it's
  8439. * gen-specific and since we only disable LCPLL after we fully disable
  8440. * the interrupts, the check below should be enough.
  8441. */
  8442. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  8443. }
  8444. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  8445. {
  8446. if (IS_HASWELL(dev_priv))
  8447. return I915_READ(D_COMP_HSW);
  8448. else
  8449. return I915_READ(D_COMP_BDW);
  8450. }
  8451. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  8452. {
  8453. if (IS_HASWELL(dev_priv)) {
  8454. mutex_lock(&dev_priv->rps.hw_lock);
  8455. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  8456. val))
  8457. DRM_DEBUG_KMS("Failed to write to D_COMP\n");
  8458. mutex_unlock(&dev_priv->rps.hw_lock);
  8459. } else {
  8460. I915_WRITE(D_COMP_BDW, val);
  8461. POSTING_READ(D_COMP_BDW);
  8462. }
  8463. }
  8464. /*
  8465. * This function implements pieces of two sequences from BSpec:
  8466. * - Sequence for display software to disable LCPLL
  8467. * - Sequence for display software to allow package C8+
  8468. * The steps implemented here are just the steps that actually touch the LCPLL
  8469. * register. Callers should take care of disabling all the display engine
  8470. * functions, doing the mode unset, fixing interrupts, etc.
  8471. */
  8472. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  8473. bool switch_to_fclk, bool allow_power_down)
  8474. {
  8475. uint32_t val;
  8476. assert_can_disable_lcpll(dev_priv);
  8477. val = I915_READ(LCPLL_CTL);
  8478. if (switch_to_fclk) {
  8479. val |= LCPLL_CD_SOURCE_FCLK;
  8480. I915_WRITE(LCPLL_CTL, val);
  8481. if (wait_for_us(I915_READ(LCPLL_CTL) &
  8482. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8483. DRM_ERROR("Switching to FCLK failed\n");
  8484. val = I915_READ(LCPLL_CTL);
  8485. }
  8486. val |= LCPLL_PLL_DISABLE;
  8487. I915_WRITE(LCPLL_CTL, val);
  8488. POSTING_READ(LCPLL_CTL);
  8489. if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
  8490. DRM_ERROR("LCPLL still locked\n");
  8491. val = hsw_read_dcomp(dev_priv);
  8492. val |= D_COMP_COMP_DISABLE;
  8493. hsw_write_dcomp(dev_priv, val);
  8494. ndelay(100);
  8495. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  8496. 1))
  8497. DRM_ERROR("D_COMP RCOMP still in progress\n");
  8498. if (allow_power_down) {
  8499. val = I915_READ(LCPLL_CTL);
  8500. val |= LCPLL_POWER_DOWN_ALLOW;
  8501. I915_WRITE(LCPLL_CTL, val);
  8502. POSTING_READ(LCPLL_CTL);
  8503. }
  8504. }
  8505. /*
  8506. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  8507. * source.
  8508. */
  8509. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  8510. {
  8511. uint32_t val;
  8512. val = I915_READ(LCPLL_CTL);
  8513. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  8514. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  8515. return;
  8516. /*
  8517. * Make sure we're not on PC8 state before disabling PC8, otherwise
  8518. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  8519. */
  8520. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  8521. if (val & LCPLL_POWER_DOWN_ALLOW) {
  8522. val &= ~LCPLL_POWER_DOWN_ALLOW;
  8523. I915_WRITE(LCPLL_CTL, val);
  8524. POSTING_READ(LCPLL_CTL);
  8525. }
  8526. val = hsw_read_dcomp(dev_priv);
  8527. val |= D_COMP_COMP_FORCE;
  8528. val &= ~D_COMP_COMP_DISABLE;
  8529. hsw_write_dcomp(dev_priv, val);
  8530. val = I915_READ(LCPLL_CTL);
  8531. val &= ~LCPLL_PLL_DISABLE;
  8532. I915_WRITE(LCPLL_CTL, val);
  8533. if (intel_wait_for_register(dev_priv,
  8534. LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  8535. 5))
  8536. DRM_ERROR("LCPLL not locked yet\n");
  8537. if (val & LCPLL_CD_SOURCE_FCLK) {
  8538. val = I915_READ(LCPLL_CTL);
  8539. val &= ~LCPLL_CD_SOURCE_FCLK;
  8540. I915_WRITE(LCPLL_CTL, val);
  8541. if (wait_for_us((I915_READ(LCPLL_CTL) &
  8542. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8543. DRM_ERROR("Switching back to LCPLL failed\n");
  8544. }
  8545. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  8546. intel_update_cdclk(&dev_priv->drm);
  8547. }
  8548. /*
  8549. * Package states C8 and deeper are really deep PC states that can only be
  8550. * reached when all the devices on the system allow it, so even if the graphics
  8551. * device allows PC8+, it doesn't mean the system will actually get to these
  8552. * states. Our driver only allows PC8+ when going into runtime PM.
  8553. *
  8554. * The requirements for PC8+ are that all the outputs are disabled, the power
  8555. * well is disabled and most interrupts are disabled, and these are also
  8556. * requirements for runtime PM. When these conditions are met, we manually do
  8557. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  8558. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  8559. * hang the machine.
  8560. *
  8561. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  8562. * the state of some registers, so when we come back from PC8+ we need to
  8563. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  8564. * need to take care of the registers kept by RC6. Notice that this happens even
  8565. * if we don't put the device in PCI D3 state (which is what currently happens
  8566. * because of the runtime PM support).
  8567. *
  8568. * For more, read "Display Sequences for Package C8" on the hardware
  8569. * documentation.
  8570. */
  8571. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  8572. {
  8573. struct drm_device *dev = &dev_priv->drm;
  8574. uint32_t val;
  8575. DRM_DEBUG_KMS("Enabling package C8+\n");
  8576. if (HAS_PCH_LPT_LP(dev_priv)) {
  8577. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8578. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  8579. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8580. }
  8581. lpt_disable_clkout_dp(dev);
  8582. hsw_disable_lcpll(dev_priv, true, true);
  8583. }
  8584. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  8585. {
  8586. struct drm_device *dev = &dev_priv->drm;
  8587. uint32_t val;
  8588. DRM_DEBUG_KMS("Disabling package C8+\n");
  8589. hsw_restore_lcpll(dev_priv);
  8590. lpt_init_pch_refclk(dev);
  8591. if (HAS_PCH_LPT_LP(dev_priv)) {
  8592. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8593. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  8594. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8595. }
  8596. }
  8597. static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8598. {
  8599. struct drm_device *dev = old_state->dev;
  8600. struct intel_atomic_state *old_intel_state =
  8601. to_intel_atomic_state(old_state);
  8602. unsigned int req_cdclk = old_intel_state->dev_cdclk;
  8603. bxt_set_cdclk(to_i915(dev), req_cdclk);
  8604. }
  8605. /* compute the max rate for new configuration */
  8606. static int ilk_max_pixel_rate(struct drm_atomic_state *state)
  8607. {
  8608. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8609. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8610. struct drm_crtc *crtc;
  8611. struct drm_crtc_state *cstate;
  8612. struct intel_crtc_state *crtc_state;
  8613. unsigned max_pixel_rate = 0, i;
  8614. enum pipe pipe;
  8615. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  8616. sizeof(intel_state->min_pixclk));
  8617. for_each_crtc_in_state(state, crtc, cstate, i) {
  8618. int pixel_rate;
  8619. crtc_state = to_intel_crtc_state(cstate);
  8620. if (!crtc_state->base.enable) {
  8621. intel_state->min_pixclk[i] = 0;
  8622. continue;
  8623. }
  8624. pixel_rate = ilk_pipe_pixel_rate(crtc_state);
  8625. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  8626. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  8627. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  8628. intel_state->min_pixclk[i] = pixel_rate;
  8629. }
  8630. for_each_pipe(dev_priv, pipe)
  8631. max_pixel_rate = max(intel_state->min_pixclk[pipe], max_pixel_rate);
  8632. return max_pixel_rate;
  8633. }
  8634. static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
  8635. {
  8636. struct drm_i915_private *dev_priv = to_i915(dev);
  8637. uint32_t val, data;
  8638. int ret;
  8639. if (WARN((I915_READ(LCPLL_CTL) &
  8640. (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
  8641. LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
  8642. LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
  8643. LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
  8644. "trying to change cdclk frequency with cdclk not enabled\n"))
  8645. return;
  8646. mutex_lock(&dev_priv->rps.hw_lock);
  8647. ret = sandybridge_pcode_write(dev_priv,
  8648. BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
  8649. mutex_unlock(&dev_priv->rps.hw_lock);
  8650. if (ret) {
  8651. DRM_ERROR("failed to inform pcode about cdclk change\n");
  8652. return;
  8653. }
  8654. val = I915_READ(LCPLL_CTL);
  8655. val |= LCPLL_CD_SOURCE_FCLK;
  8656. I915_WRITE(LCPLL_CTL, val);
  8657. if (wait_for_us(I915_READ(LCPLL_CTL) &
  8658. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8659. DRM_ERROR("Switching to FCLK failed\n");
  8660. val = I915_READ(LCPLL_CTL);
  8661. val &= ~LCPLL_CLK_FREQ_MASK;
  8662. switch (cdclk) {
  8663. case 450000:
  8664. val |= LCPLL_CLK_FREQ_450;
  8665. data = 0;
  8666. break;
  8667. case 540000:
  8668. val |= LCPLL_CLK_FREQ_54O_BDW;
  8669. data = 1;
  8670. break;
  8671. case 337500:
  8672. val |= LCPLL_CLK_FREQ_337_5_BDW;
  8673. data = 2;
  8674. break;
  8675. case 675000:
  8676. val |= LCPLL_CLK_FREQ_675_BDW;
  8677. data = 3;
  8678. break;
  8679. default:
  8680. WARN(1, "invalid cdclk frequency\n");
  8681. return;
  8682. }
  8683. I915_WRITE(LCPLL_CTL, val);
  8684. val = I915_READ(LCPLL_CTL);
  8685. val &= ~LCPLL_CD_SOURCE_FCLK;
  8686. I915_WRITE(LCPLL_CTL, val);
  8687. if (wait_for_us((I915_READ(LCPLL_CTL) &
  8688. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8689. DRM_ERROR("Switching back to LCPLL failed\n");
  8690. mutex_lock(&dev_priv->rps.hw_lock);
  8691. sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
  8692. mutex_unlock(&dev_priv->rps.hw_lock);
  8693. I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
  8694. intel_update_cdclk(dev);
  8695. WARN(cdclk != dev_priv->cdclk_freq,
  8696. "cdclk requested %d kHz but got %d kHz\n",
  8697. cdclk, dev_priv->cdclk_freq);
  8698. }
  8699. static int broadwell_calc_cdclk(int max_pixclk)
  8700. {
  8701. if (max_pixclk > 540000)
  8702. return 675000;
  8703. else if (max_pixclk > 450000)
  8704. return 540000;
  8705. else if (max_pixclk > 337500)
  8706. return 450000;
  8707. else
  8708. return 337500;
  8709. }
  8710. static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
  8711. {
  8712. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8713. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8714. int max_pixclk = ilk_max_pixel_rate(state);
  8715. int cdclk;
  8716. /*
  8717. * FIXME should also account for plane ratio
  8718. * once 64bpp pixel formats are supported.
  8719. */
  8720. cdclk = broadwell_calc_cdclk(max_pixclk);
  8721. if (cdclk > dev_priv->max_cdclk_freq) {
  8722. DRM_DEBUG_KMS("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8723. cdclk, dev_priv->max_cdclk_freq);
  8724. return -EINVAL;
  8725. }
  8726. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8727. if (!intel_state->active_crtcs)
  8728. intel_state->dev_cdclk = broadwell_calc_cdclk(0);
  8729. return 0;
  8730. }
  8731. static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8732. {
  8733. struct drm_device *dev = old_state->dev;
  8734. struct intel_atomic_state *old_intel_state =
  8735. to_intel_atomic_state(old_state);
  8736. unsigned req_cdclk = old_intel_state->dev_cdclk;
  8737. broadwell_set_cdclk(dev, req_cdclk);
  8738. }
  8739. static int skl_modeset_calc_cdclk(struct drm_atomic_state *state)
  8740. {
  8741. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  8742. struct drm_i915_private *dev_priv = to_i915(state->dev);
  8743. const int max_pixclk = ilk_max_pixel_rate(state);
  8744. int vco = intel_state->cdclk_pll_vco;
  8745. int cdclk;
  8746. /*
  8747. * FIXME should also account for plane ratio
  8748. * once 64bpp pixel formats are supported.
  8749. */
  8750. cdclk = skl_calc_cdclk(max_pixclk, vco);
  8751. /*
  8752. * FIXME move the cdclk caclulation to
  8753. * compute_config() so we can fail gracegully.
  8754. */
  8755. if (cdclk > dev_priv->max_cdclk_freq) {
  8756. DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
  8757. cdclk, dev_priv->max_cdclk_freq);
  8758. cdclk = dev_priv->max_cdclk_freq;
  8759. }
  8760. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  8761. if (!intel_state->active_crtcs)
  8762. intel_state->dev_cdclk = skl_calc_cdclk(0, vco);
  8763. return 0;
  8764. }
  8765. static void skl_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8766. {
  8767. struct drm_i915_private *dev_priv = to_i915(old_state->dev);
  8768. struct intel_atomic_state *intel_state = to_intel_atomic_state(old_state);
  8769. unsigned int req_cdclk = intel_state->dev_cdclk;
  8770. unsigned int req_vco = intel_state->cdclk_pll_vco;
  8771. skl_set_cdclk(dev_priv, req_cdclk, req_vco);
  8772. }
  8773. static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
  8774. struct intel_crtc_state *crtc_state)
  8775. {
  8776. if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI)) {
  8777. if (!intel_ddi_pll_select(crtc, crtc_state))
  8778. return -EINVAL;
  8779. }
  8780. crtc->lowfreq_avail = false;
  8781. return 0;
  8782. }
  8783. static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
  8784. enum port port,
  8785. struct intel_crtc_state *pipe_config)
  8786. {
  8787. enum intel_dpll_id id;
  8788. switch (port) {
  8789. case PORT_A:
  8790. id = DPLL_ID_SKL_DPLL0;
  8791. break;
  8792. case PORT_B:
  8793. id = DPLL_ID_SKL_DPLL1;
  8794. break;
  8795. case PORT_C:
  8796. id = DPLL_ID_SKL_DPLL2;
  8797. break;
  8798. default:
  8799. DRM_ERROR("Incorrect port type\n");
  8800. return;
  8801. }
  8802. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8803. }
  8804. static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
  8805. enum port port,
  8806. struct intel_crtc_state *pipe_config)
  8807. {
  8808. enum intel_dpll_id id;
  8809. u32 temp;
  8810. temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
  8811. id = temp >> (port * 3 + 1);
  8812. if (WARN_ON(id < SKL_DPLL0 || id > SKL_DPLL3))
  8813. return;
  8814. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8815. }
  8816. static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
  8817. enum port port,
  8818. struct intel_crtc_state *pipe_config)
  8819. {
  8820. enum intel_dpll_id id;
  8821. uint32_t ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
  8822. switch (ddi_pll_sel) {
  8823. case PORT_CLK_SEL_WRPLL1:
  8824. id = DPLL_ID_WRPLL1;
  8825. break;
  8826. case PORT_CLK_SEL_WRPLL2:
  8827. id = DPLL_ID_WRPLL2;
  8828. break;
  8829. case PORT_CLK_SEL_SPLL:
  8830. id = DPLL_ID_SPLL;
  8831. break;
  8832. case PORT_CLK_SEL_LCPLL_810:
  8833. id = DPLL_ID_LCPLL_810;
  8834. break;
  8835. case PORT_CLK_SEL_LCPLL_1350:
  8836. id = DPLL_ID_LCPLL_1350;
  8837. break;
  8838. case PORT_CLK_SEL_LCPLL_2700:
  8839. id = DPLL_ID_LCPLL_2700;
  8840. break;
  8841. default:
  8842. MISSING_CASE(ddi_pll_sel);
  8843. /* fall through */
  8844. case PORT_CLK_SEL_NONE:
  8845. return;
  8846. }
  8847. pipe_config->shared_dpll = intel_get_shared_dpll_by_id(dev_priv, id);
  8848. }
  8849. static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
  8850. struct intel_crtc_state *pipe_config,
  8851. unsigned long *power_domain_mask)
  8852. {
  8853. struct drm_device *dev = crtc->base.dev;
  8854. struct drm_i915_private *dev_priv = to_i915(dev);
  8855. enum intel_display_power_domain power_domain;
  8856. u32 tmp;
  8857. /*
  8858. * The pipe->transcoder mapping is fixed with the exception of the eDP
  8859. * transcoder handled below.
  8860. */
  8861. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8862. /*
  8863. * XXX: Do intel_display_power_get_if_enabled before reading this (for
  8864. * consistency and less surprising code; it's in always on power).
  8865. */
  8866. tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
  8867. if (tmp & TRANS_DDI_FUNC_ENABLE) {
  8868. enum pipe trans_edp_pipe;
  8869. switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
  8870. default:
  8871. WARN(1, "unknown pipe linked to edp transcoder\n");
  8872. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  8873. case TRANS_DDI_EDP_INPUT_A_ON:
  8874. trans_edp_pipe = PIPE_A;
  8875. break;
  8876. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  8877. trans_edp_pipe = PIPE_B;
  8878. break;
  8879. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  8880. trans_edp_pipe = PIPE_C;
  8881. break;
  8882. }
  8883. if (trans_edp_pipe == crtc->pipe)
  8884. pipe_config->cpu_transcoder = TRANSCODER_EDP;
  8885. }
  8886. power_domain = POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder);
  8887. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8888. return false;
  8889. *power_domain_mask |= BIT(power_domain);
  8890. tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
  8891. return tmp & PIPECONF_ENABLE;
  8892. }
  8893. static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
  8894. struct intel_crtc_state *pipe_config,
  8895. unsigned long *power_domain_mask)
  8896. {
  8897. struct drm_device *dev = crtc->base.dev;
  8898. struct drm_i915_private *dev_priv = to_i915(dev);
  8899. enum intel_display_power_domain power_domain;
  8900. enum port port;
  8901. enum transcoder cpu_transcoder;
  8902. u32 tmp;
  8903. for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
  8904. if (port == PORT_A)
  8905. cpu_transcoder = TRANSCODER_DSI_A;
  8906. else
  8907. cpu_transcoder = TRANSCODER_DSI_C;
  8908. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  8909. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8910. continue;
  8911. *power_domain_mask |= BIT(power_domain);
  8912. /*
  8913. * The PLL needs to be enabled with a valid divider
  8914. * configuration, otherwise accessing DSI registers will hang
  8915. * the machine. See BSpec North Display Engine
  8916. * registers/MIPI[BXT]. We can break out here early, since we
  8917. * need the same DSI PLL to be enabled for both DSI ports.
  8918. */
  8919. if (!intel_dsi_pll_is_enabled(dev_priv))
  8920. break;
  8921. /* XXX: this works for video mode only */
  8922. tmp = I915_READ(BXT_MIPI_PORT_CTRL(port));
  8923. if (!(tmp & DPI_ENABLE))
  8924. continue;
  8925. tmp = I915_READ(MIPI_CTRL(port));
  8926. if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
  8927. continue;
  8928. pipe_config->cpu_transcoder = cpu_transcoder;
  8929. break;
  8930. }
  8931. return transcoder_is_dsi(pipe_config->cpu_transcoder);
  8932. }
  8933. static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
  8934. struct intel_crtc_state *pipe_config)
  8935. {
  8936. struct drm_device *dev = crtc->base.dev;
  8937. struct drm_i915_private *dev_priv = to_i915(dev);
  8938. struct intel_shared_dpll *pll;
  8939. enum port port;
  8940. uint32_t tmp;
  8941. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8942. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8943. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  8944. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8945. else if (IS_BROXTON(dev_priv))
  8946. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8947. else
  8948. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8949. pll = pipe_config->shared_dpll;
  8950. if (pll) {
  8951. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  8952. &pipe_config->dpll_hw_state));
  8953. }
  8954. /*
  8955. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8956. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8957. * the PCH transcoder is on.
  8958. */
  8959. if (INTEL_INFO(dev)->gen < 9 &&
  8960. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8961. pipe_config->has_pch_encoder = true;
  8962. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8963. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8964. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8965. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8966. }
  8967. }
  8968. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8969. struct intel_crtc_state *pipe_config)
  8970. {
  8971. struct drm_device *dev = crtc->base.dev;
  8972. struct drm_i915_private *dev_priv = to_i915(dev);
  8973. enum intel_display_power_domain power_domain;
  8974. unsigned long power_domain_mask;
  8975. bool active;
  8976. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  8977. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8978. return false;
  8979. power_domain_mask = BIT(power_domain);
  8980. pipe_config->shared_dpll = NULL;
  8981. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  8982. if (IS_BROXTON(dev_priv) &&
  8983. bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
  8984. WARN_ON(active);
  8985. active = true;
  8986. }
  8987. if (!active)
  8988. goto out;
  8989. if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  8990. haswell_get_ddi_port_state(crtc, pipe_config);
  8991. intel_get_pipe_timings(crtc, pipe_config);
  8992. }
  8993. intel_get_pipe_src_size(crtc, pipe_config);
  8994. pipe_config->gamma_mode =
  8995. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  8996. if (INTEL_INFO(dev)->gen >= 9) {
  8997. skl_init_scalers(dev_priv, crtc, pipe_config);
  8998. pipe_config->scaler_state.scaler_id = -1;
  8999. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  9000. }
  9001. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  9002. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  9003. power_domain_mask |= BIT(power_domain);
  9004. if (INTEL_INFO(dev)->gen >= 9)
  9005. skylake_get_pfit_config(crtc, pipe_config);
  9006. else
  9007. ironlake_get_pfit_config(crtc, pipe_config);
  9008. }
  9009. if (IS_HASWELL(dev_priv))
  9010. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  9011. (I915_READ(IPS_CTL) & IPS_ENABLE);
  9012. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  9013. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  9014. pipe_config->pixel_multiplier =
  9015. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  9016. } else {
  9017. pipe_config->pixel_multiplier = 1;
  9018. }
  9019. out:
  9020. for_each_power_domain(power_domain, power_domain_mask)
  9021. intel_display_power_put(dev_priv, power_domain);
  9022. return active;
  9023. }
  9024. static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
  9025. const struct intel_plane_state *plane_state)
  9026. {
  9027. struct drm_device *dev = crtc->dev;
  9028. struct drm_i915_private *dev_priv = to_i915(dev);
  9029. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9030. uint32_t cntl = 0, size = 0;
  9031. if (plane_state && plane_state->base.visible) {
  9032. unsigned int width = plane_state->base.crtc_w;
  9033. unsigned int height = plane_state->base.crtc_h;
  9034. unsigned int stride = roundup_pow_of_two(width) * 4;
  9035. switch (stride) {
  9036. default:
  9037. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  9038. width, stride);
  9039. stride = 256;
  9040. /* fallthrough */
  9041. case 256:
  9042. case 512:
  9043. case 1024:
  9044. case 2048:
  9045. break;
  9046. }
  9047. cntl |= CURSOR_ENABLE |
  9048. CURSOR_GAMMA_ENABLE |
  9049. CURSOR_FORMAT_ARGB |
  9050. CURSOR_STRIDE(stride);
  9051. size = (height << 12) | width;
  9052. }
  9053. if (intel_crtc->cursor_cntl != 0 &&
  9054. (intel_crtc->cursor_base != base ||
  9055. intel_crtc->cursor_size != size ||
  9056. intel_crtc->cursor_cntl != cntl)) {
  9057. /* On these chipsets we can only modify the base/size/stride
  9058. * whilst the cursor is disabled.
  9059. */
  9060. I915_WRITE(CURCNTR(PIPE_A), 0);
  9061. POSTING_READ(CURCNTR(PIPE_A));
  9062. intel_crtc->cursor_cntl = 0;
  9063. }
  9064. if (intel_crtc->cursor_base != base) {
  9065. I915_WRITE(CURBASE(PIPE_A), base);
  9066. intel_crtc->cursor_base = base;
  9067. }
  9068. if (intel_crtc->cursor_size != size) {
  9069. I915_WRITE(CURSIZE, size);
  9070. intel_crtc->cursor_size = size;
  9071. }
  9072. if (intel_crtc->cursor_cntl != cntl) {
  9073. I915_WRITE(CURCNTR(PIPE_A), cntl);
  9074. POSTING_READ(CURCNTR(PIPE_A));
  9075. intel_crtc->cursor_cntl = cntl;
  9076. }
  9077. }
  9078. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
  9079. const struct intel_plane_state *plane_state)
  9080. {
  9081. struct drm_device *dev = crtc->dev;
  9082. struct drm_i915_private *dev_priv = to_i915(dev);
  9083. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9084. struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
  9085. const struct skl_wm_values *wm = &dev_priv->wm.skl_results;
  9086. const struct skl_plane_wm *p_wm =
  9087. &cstate->wm.skl.optimal.planes[PLANE_CURSOR];
  9088. int pipe = intel_crtc->pipe;
  9089. uint32_t cntl = 0;
  9090. if (INTEL_GEN(dev_priv) >= 9 && wm->dirty_pipes & drm_crtc_mask(crtc))
  9091. skl_write_cursor_wm(intel_crtc, p_wm, &wm->ddb);
  9092. if (plane_state && plane_state->base.visible) {
  9093. cntl = MCURSOR_GAMMA_ENABLE;
  9094. switch (plane_state->base.crtc_w) {
  9095. case 64:
  9096. cntl |= CURSOR_MODE_64_ARGB_AX;
  9097. break;
  9098. case 128:
  9099. cntl |= CURSOR_MODE_128_ARGB_AX;
  9100. break;
  9101. case 256:
  9102. cntl |= CURSOR_MODE_256_ARGB_AX;
  9103. break;
  9104. default:
  9105. MISSING_CASE(plane_state->base.crtc_w);
  9106. return;
  9107. }
  9108. cntl |= pipe << 28; /* Connect to correct pipe */
  9109. if (HAS_DDI(dev_priv))
  9110. cntl |= CURSOR_PIPE_CSC_ENABLE;
  9111. if (plane_state->base.rotation == DRM_ROTATE_180)
  9112. cntl |= CURSOR_ROTATE_180;
  9113. }
  9114. if (intel_crtc->cursor_cntl != cntl) {
  9115. I915_WRITE(CURCNTR(pipe), cntl);
  9116. POSTING_READ(CURCNTR(pipe));
  9117. intel_crtc->cursor_cntl = cntl;
  9118. }
  9119. /* and commit changes on next vblank */
  9120. I915_WRITE(CURBASE(pipe), base);
  9121. POSTING_READ(CURBASE(pipe));
  9122. intel_crtc->cursor_base = base;
  9123. }
  9124. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  9125. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  9126. const struct intel_plane_state *plane_state)
  9127. {
  9128. struct drm_device *dev = crtc->dev;
  9129. struct drm_i915_private *dev_priv = to_i915(dev);
  9130. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9131. int pipe = intel_crtc->pipe;
  9132. u32 base = intel_crtc->cursor_addr;
  9133. u32 pos = 0;
  9134. if (plane_state) {
  9135. int x = plane_state->base.crtc_x;
  9136. int y = plane_state->base.crtc_y;
  9137. if (x < 0) {
  9138. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  9139. x = -x;
  9140. }
  9141. pos |= x << CURSOR_X_SHIFT;
  9142. if (y < 0) {
  9143. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  9144. y = -y;
  9145. }
  9146. pos |= y << CURSOR_Y_SHIFT;
  9147. /* ILK+ do this automagically */
  9148. if (HAS_GMCH_DISPLAY(dev_priv) &&
  9149. plane_state->base.rotation == DRM_ROTATE_180) {
  9150. base += (plane_state->base.crtc_h *
  9151. plane_state->base.crtc_w - 1) * 4;
  9152. }
  9153. }
  9154. I915_WRITE(CURPOS(pipe), pos);
  9155. if (IS_845G(dev_priv) || IS_I865G(dev_priv))
  9156. i845_update_cursor(crtc, base, plane_state);
  9157. else
  9158. i9xx_update_cursor(crtc, base, plane_state);
  9159. }
  9160. static bool cursor_size_ok(struct drm_i915_private *dev_priv,
  9161. uint32_t width, uint32_t height)
  9162. {
  9163. if (width == 0 || height == 0)
  9164. return false;
  9165. /*
  9166. * 845g/865g are special in that they are only limited by
  9167. * the width of their cursors, the height is arbitrary up to
  9168. * the precision of the register. Everything else requires
  9169. * square cursors, limited to a few power-of-two sizes.
  9170. */
  9171. if (IS_845G(dev_priv) || IS_I865G(dev_priv)) {
  9172. if ((width & 63) != 0)
  9173. return false;
  9174. if (width > (IS_845G(dev_priv) ? 64 : 512))
  9175. return false;
  9176. if (height > 1023)
  9177. return false;
  9178. } else {
  9179. switch (width | height) {
  9180. case 256:
  9181. case 128:
  9182. if (IS_GEN2(dev_priv))
  9183. return false;
  9184. case 64:
  9185. break;
  9186. default:
  9187. return false;
  9188. }
  9189. }
  9190. return true;
  9191. }
  9192. /* VESA 640x480x72Hz mode to set on the pipe */
  9193. static struct drm_display_mode load_detect_mode = {
  9194. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  9195. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  9196. };
  9197. struct drm_framebuffer *
  9198. __intel_framebuffer_create(struct drm_device *dev,
  9199. struct drm_mode_fb_cmd2 *mode_cmd,
  9200. struct drm_i915_gem_object *obj)
  9201. {
  9202. struct intel_framebuffer *intel_fb;
  9203. int ret;
  9204. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  9205. if (!intel_fb)
  9206. return ERR_PTR(-ENOMEM);
  9207. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  9208. if (ret)
  9209. goto err;
  9210. return &intel_fb->base;
  9211. err:
  9212. kfree(intel_fb);
  9213. return ERR_PTR(ret);
  9214. }
  9215. static struct drm_framebuffer *
  9216. intel_framebuffer_create(struct drm_device *dev,
  9217. struct drm_mode_fb_cmd2 *mode_cmd,
  9218. struct drm_i915_gem_object *obj)
  9219. {
  9220. struct drm_framebuffer *fb;
  9221. int ret;
  9222. ret = i915_mutex_lock_interruptible(dev);
  9223. if (ret)
  9224. return ERR_PTR(ret);
  9225. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  9226. mutex_unlock(&dev->struct_mutex);
  9227. return fb;
  9228. }
  9229. static u32
  9230. intel_framebuffer_pitch_for_width(int width, int bpp)
  9231. {
  9232. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  9233. return ALIGN(pitch, 64);
  9234. }
  9235. static u32
  9236. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  9237. {
  9238. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  9239. return PAGE_ALIGN(pitch * mode->vdisplay);
  9240. }
  9241. static struct drm_framebuffer *
  9242. intel_framebuffer_create_for_mode(struct drm_device *dev,
  9243. struct drm_display_mode *mode,
  9244. int depth, int bpp)
  9245. {
  9246. struct drm_framebuffer *fb;
  9247. struct drm_i915_gem_object *obj;
  9248. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  9249. obj = i915_gem_object_create(dev,
  9250. intel_framebuffer_size_for_mode(mode, bpp));
  9251. if (IS_ERR(obj))
  9252. return ERR_CAST(obj);
  9253. mode_cmd.width = mode->hdisplay;
  9254. mode_cmd.height = mode->vdisplay;
  9255. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  9256. bpp);
  9257. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  9258. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  9259. if (IS_ERR(fb))
  9260. i915_gem_object_put(obj);
  9261. return fb;
  9262. }
  9263. static struct drm_framebuffer *
  9264. mode_fits_in_fbdev(struct drm_device *dev,
  9265. struct drm_display_mode *mode)
  9266. {
  9267. #ifdef CONFIG_DRM_FBDEV_EMULATION
  9268. struct drm_i915_private *dev_priv = to_i915(dev);
  9269. struct drm_i915_gem_object *obj;
  9270. struct drm_framebuffer *fb;
  9271. if (!dev_priv->fbdev)
  9272. return NULL;
  9273. if (!dev_priv->fbdev->fb)
  9274. return NULL;
  9275. obj = dev_priv->fbdev->fb->obj;
  9276. BUG_ON(!obj);
  9277. fb = &dev_priv->fbdev->fb->base;
  9278. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  9279. fb->bits_per_pixel))
  9280. return NULL;
  9281. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  9282. return NULL;
  9283. drm_framebuffer_reference(fb);
  9284. return fb;
  9285. #else
  9286. return NULL;
  9287. #endif
  9288. }
  9289. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  9290. struct drm_crtc *crtc,
  9291. struct drm_display_mode *mode,
  9292. struct drm_framebuffer *fb,
  9293. int x, int y)
  9294. {
  9295. struct drm_plane_state *plane_state;
  9296. int hdisplay, vdisplay;
  9297. int ret;
  9298. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  9299. if (IS_ERR(plane_state))
  9300. return PTR_ERR(plane_state);
  9301. if (mode)
  9302. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  9303. else
  9304. hdisplay = vdisplay = 0;
  9305. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  9306. if (ret)
  9307. return ret;
  9308. drm_atomic_set_fb_for_plane(plane_state, fb);
  9309. plane_state->crtc_x = 0;
  9310. plane_state->crtc_y = 0;
  9311. plane_state->crtc_w = hdisplay;
  9312. plane_state->crtc_h = vdisplay;
  9313. plane_state->src_x = x << 16;
  9314. plane_state->src_y = y << 16;
  9315. plane_state->src_w = hdisplay << 16;
  9316. plane_state->src_h = vdisplay << 16;
  9317. return 0;
  9318. }
  9319. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  9320. struct drm_display_mode *mode,
  9321. struct intel_load_detect_pipe *old,
  9322. struct drm_modeset_acquire_ctx *ctx)
  9323. {
  9324. struct intel_crtc *intel_crtc;
  9325. struct intel_encoder *intel_encoder =
  9326. intel_attached_encoder(connector);
  9327. struct drm_crtc *possible_crtc;
  9328. struct drm_encoder *encoder = &intel_encoder->base;
  9329. struct drm_crtc *crtc = NULL;
  9330. struct drm_device *dev = encoder->dev;
  9331. struct drm_i915_private *dev_priv = to_i915(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_priv, intel_crtc->pipe);
  9456. return true;
  9457. fail:
  9458. if (state) {
  9459. drm_atomic_state_put(state);
  9460. state = NULL;
  9461. }
  9462. if (restore_state) {
  9463. drm_atomic_state_put(restore_state);
  9464. restore_state = NULL;
  9465. }
  9466. if (ret == -EDEADLK) {
  9467. drm_modeset_backoff(ctx);
  9468. goto retry;
  9469. }
  9470. return false;
  9471. }
  9472. void intel_release_load_detect_pipe(struct drm_connector *connector,
  9473. struct intel_load_detect_pipe *old,
  9474. struct drm_modeset_acquire_ctx *ctx)
  9475. {
  9476. struct intel_encoder *intel_encoder =
  9477. intel_attached_encoder(connector);
  9478. struct drm_encoder *encoder = &intel_encoder->base;
  9479. struct drm_atomic_state *state = old->restore_state;
  9480. int ret;
  9481. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  9482. connector->base.id, connector->name,
  9483. encoder->base.id, encoder->name);
  9484. if (!state)
  9485. return;
  9486. ret = drm_atomic_commit(state);
  9487. if (ret)
  9488. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  9489. drm_atomic_state_put(state);
  9490. }
  9491. static int i9xx_pll_refclk(struct drm_device *dev,
  9492. const struct intel_crtc_state *pipe_config)
  9493. {
  9494. struct drm_i915_private *dev_priv = to_i915(dev);
  9495. u32 dpll = pipe_config->dpll_hw_state.dpll;
  9496. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  9497. return dev_priv->vbt.lvds_ssc_freq;
  9498. else if (HAS_PCH_SPLIT(dev_priv))
  9499. return 120000;
  9500. else if (!IS_GEN2(dev_priv))
  9501. return 96000;
  9502. else
  9503. return 48000;
  9504. }
  9505. /* Returns the clock of the currently programmed mode of the given pipe. */
  9506. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  9507. struct intel_crtc_state *pipe_config)
  9508. {
  9509. struct drm_device *dev = crtc->base.dev;
  9510. struct drm_i915_private *dev_priv = to_i915(dev);
  9511. int pipe = pipe_config->cpu_transcoder;
  9512. u32 dpll = pipe_config->dpll_hw_state.dpll;
  9513. u32 fp;
  9514. struct dpll clock;
  9515. int port_clock;
  9516. int refclk = i9xx_pll_refclk(dev, pipe_config);
  9517. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  9518. fp = pipe_config->dpll_hw_state.fp0;
  9519. else
  9520. fp = pipe_config->dpll_hw_state.fp1;
  9521. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  9522. if (IS_PINEVIEW(dev)) {
  9523. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  9524. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  9525. } else {
  9526. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  9527. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  9528. }
  9529. if (!IS_GEN2(dev_priv)) {
  9530. if (IS_PINEVIEW(dev))
  9531. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  9532. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  9533. else
  9534. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  9535. DPLL_FPA01_P1_POST_DIV_SHIFT);
  9536. switch (dpll & DPLL_MODE_MASK) {
  9537. case DPLLB_MODE_DAC_SERIAL:
  9538. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  9539. 5 : 10;
  9540. break;
  9541. case DPLLB_MODE_LVDS:
  9542. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  9543. 7 : 14;
  9544. break;
  9545. default:
  9546. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  9547. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  9548. return;
  9549. }
  9550. if (IS_PINEVIEW(dev))
  9551. port_clock = pnv_calc_dpll_params(refclk, &clock);
  9552. else
  9553. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  9554. } else {
  9555. u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
  9556. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  9557. if (is_lvds) {
  9558. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  9559. DPLL_FPA01_P1_POST_DIV_SHIFT);
  9560. if (lvds & LVDS_CLKB_POWER_UP)
  9561. clock.p2 = 7;
  9562. else
  9563. clock.p2 = 14;
  9564. } else {
  9565. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  9566. clock.p1 = 2;
  9567. else {
  9568. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  9569. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  9570. }
  9571. if (dpll & PLL_P2_DIVIDE_BY_4)
  9572. clock.p2 = 4;
  9573. else
  9574. clock.p2 = 2;
  9575. }
  9576. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  9577. }
  9578. /*
  9579. * This value includes pixel_multiplier. We will use
  9580. * port_clock to compute adjusted_mode.crtc_clock in the
  9581. * encoder's get_config() function.
  9582. */
  9583. pipe_config->port_clock = port_clock;
  9584. }
  9585. int intel_dotclock_calculate(int link_freq,
  9586. const struct intel_link_m_n *m_n)
  9587. {
  9588. /*
  9589. * The calculation for the data clock is:
  9590. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  9591. * But we want to avoid losing precison if possible, so:
  9592. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  9593. *
  9594. * and the link clock is simpler:
  9595. * link_clock = (m * link_clock) / n
  9596. */
  9597. if (!m_n->link_n)
  9598. return 0;
  9599. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  9600. }
  9601. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  9602. struct intel_crtc_state *pipe_config)
  9603. {
  9604. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9605. /* read out port_clock from the DPLL */
  9606. i9xx_crtc_clock_get(crtc, pipe_config);
  9607. /*
  9608. * In case there is an active pipe without active ports,
  9609. * we may need some idea for the dotclock anyway.
  9610. * Calculate one based on the FDI configuration.
  9611. */
  9612. pipe_config->base.adjusted_mode.crtc_clock =
  9613. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9614. &pipe_config->fdi_m_n);
  9615. }
  9616. /** Returns the currently programmed mode of the given pipe. */
  9617. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  9618. struct drm_crtc *crtc)
  9619. {
  9620. struct drm_i915_private *dev_priv = to_i915(dev);
  9621. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9622. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  9623. struct drm_display_mode *mode;
  9624. struct intel_crtc_state *pipe_config;
  9625. int htot = I915_READ(HTOTAL(cpu_transcoder));
  9626. int hsync = I915_READ(HSYNC(cpu_transcoder));
  9627. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  9628. int vsync = I915_READ(VSYNC(cpu_transcoder));
  9629. enum pipe pipe = intel_crtc->pipe;
  9630. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  9631. if (!mode)
  9632. return NULL;
  9633. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  9634. if (!pipe_config) {
  9635. kfree(mode);
  9636. return NULL;
  9637. }
  9638. /*
  9639. * Construct a pipe_config sufficient for getting the clock info
  9640. * back out of crtc_clock_get.
  9641. *
  9642. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  9643. * to use a real value here instead.
  9644. */
  9645. pipe_config->cpu_transcoder = (enum transcoder) pipe;
  9646. pipe_config->pixel_multiplier = 1;
  9647. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  9648. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  9649. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  9650. i9xx_crtc_clock_get(intel_crtc, pipe_config);
  9651. mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
  9652. mode->hdisplay = (htot & 0xffff) + 1;
  9653. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  9654. mode->hsync_start = (hsync & 0xffff) + 1;
  9655. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  9656. mode->vdisplay = (vtot & 0xffff) + 1;
  9657. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  9658. mode->vsync_start = (vsync & 0xffff) + 1;
  9659. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  9660. drm_mode_set_name(mode);
  9661. kfree(pipe_config);
  9662. return mode;
  9663. }
  9664. static void intel_crtc_destroy(struct drm_crtc *crtc)
  9665. {
  9666. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9667. struct drm_device *dev = crtc->dev;
  9668. struct intel_flip_work *work;
  9669. spin_lock_irq(&dev->event_lock);
  9670. work = intel_crtc->flip_work;
  9671. intel_crtc->flip_work = NULL;
  9672. spin_unlock_irq(&dev->event_lock);
  9673. if (work) {
  9674. cancel_work_sync(&work->mmio_work);
  9675. cancel_work_sync(&work->unpin_work);
  9676. kfree(work);
  9677. }
  9678. drm_crtc_cleanup(crtc);
  9679. kfree(intel_crtc);
  9680. }
  9681. static void intel_unpin_work_fn(struct work_struct *__work)
  9682. {
  9683. struct intel_flip_work *work =
  9684. container_of(__work, struct intel_flip_work, unpin_work);
  9685. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  9686. struct drm_device *dev = crtc->base.dev;
  9687. struct drm_plane *primary = crtc->base.primary;
  9688. if (is_mmio_work(work))
  9689. flush_work(&work->mmio_work);
  9690. mutex_lock(&dev->struct_mutex);
  9691. intel_unpin_fb_obj(work->old_fb, primary->state->rotation);
  9692. i915_gem_object_put(work->pending_flip_obj);
  9693. mutex_unlock(&dev->struct_mutex);
  9694. i915_gem_request_put(work->flip_queued_req);
  9695. intel_frontbuffer_flip_complete(to_i915(dev),
  9696. to_intel_plane(primary)->frontbuffer_bit);
  9697. intel_fbc_post_update(crtc);
  9698. drm_framebuffer_unreference(work->old_fb);
  9699. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  9700. atomic_dec(&crtc->unpin_work_count);
  9701. kfree(work);
  9702. }
  9703. /* Is 'a' after or equal to 'b'? */
  9704. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9705. {
  9706. return !((a - b) & 0x80000000);
  9707. }
  9708. static bool __pageflip_finished_cs(struct intel_crtc *crtc,
  9709. struct intel_flip_work *work)
  9710. {
  9711. struct drm_device *dev = crtc->base.dev;
  9712. struct drm_i915_private *dev_priv = to_i915(dev);
  9713. if (abort_flip_on_reset(crtc))
  9714. return true;
  9715. /*
  9716. * The relevant registers doen't exist on pre-ctg.
  9717. * As the flip done interrupt doesn't trigger for mmio
  9718. * flips on gmch platforms, a flip count check isn't
  9719. * really needed there. But since ctg has the registers,
  9720. * include it in the check anyway.
  9721. */
  9722. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  9723. return true;
  9724. /*
  9725. * BDW signals flip done immediately if the plane
  9726. * is disabled, even if the plane enable is already
  9727. * armed to occur at the next vblank :(
  9728. */
  9729. /*
  9730. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9731. * used the same base address. In that case the mmio flip might
  9732. * have completed, but the CS hasn't even executed the flip yet.
  9733. *
  9734. * A flip count check isn't enough as the CS might have updated
  9735. * the base address just after start of vblank, but before we
  9736. * managed to process the interrupt. This means we'd complete the
  9737. * CS flip too soon.
  9738. *
  9739. * Combining both checks should get us a good enough result. It may
  9740. * still happen that the CS flip has been executed, but has not
  9741. * yet actually completed. But in case the base address is the same
  9742. * anyway, we don't really care.
  9743. */
  9744. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9745. crtc->flip_work->gtt_offset &&
  9746. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
  9747. crtc->flip_work->flip_count);
  9748. }
  9749. static bool
  9750. __pageflip_finished_mmio(struct intel_crtc *crtc,
  9751. struct intel_flip_work *work)
  9752. {
  9753. /*
  9754. * MMIO work completes when vblank is different from
  9755. * flip_queued_vblank.
  9756. *
  9757. * Reset counter value doesn't matter, this is handled by
  9758. * i915_wait_request finishing early, so no need to handle
  9759. * reset here.
  9760. */
  9761. return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
  9762. }
  9763. static bool pageflip_finished(struct intel_crtc *crtc,
  9764. struct intel_flip_work *work)
  9765. {
  9766. if (!atomic_read(&work->pending))
  9767. return false;
  9768. smp_rmb();
  9769. if (is_mmio_work(work))
  9770. return __pageflip_finished_mmio(crtc, work);
  9771. else
  9772. return __pageflip_finished_cs(crtc, work);
  9773. }
  9774. void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
  9775. {
  9776. struct drm_device *dev = &dev_priv->drm;
  9777. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  9778. struct intel_flip_work *work;
  9779. unsigned long flags;
  9780. /* Ignore early vblank irqs */
  9781. if (!crtc)
  9782. return;
  9783. /*
  9784. * This is called both by irq handlers and the reset code (to complete
  9785. * lost pageflips) so needs the full irqsave spinlocks.
  9786. */
  9787. spin_lock_irqsave(&dev->event_lock, flags);
  9788. work = crtc->flip_work;
  9789. if (work != NULL &&
  9790. !is_mmio_work(work) &&
  9791. pageflip_finished(crtc, work))
  9792. page_flip_completed(crtc);
  9793. spin_unlock_irqrestore(&dev->event_lock, flags);
  9794. }
  9795. void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
  9796. {
  9797. struct drm_device *dev = &dev_priv->drm;
  9798. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  9799. struct intel_flip_work *work;
  9800. unsigned long flags;
  9801. /* Ignore early vblank irqs */
  9802. if (!crtc)
  9803. return;
  9804. /*
  9805. * This is called both by irq handlers and the reset code (to complete
  9806. * lost pageflips) so needs the full irqsave spinlocks.
  9807. */
  9808. spin_lock_irqsave(&dev->event_lock, flags);
  9809. work = crtc->flip_work;
  9810. if (work != NULL &&
  9811. is_mmio_work(work) &&
  9812. pageflip_finished(crtc, work))
  9813. page_flip_completed(crtc);
  9814. spin_unlock_irqrestore(&dev->event_lock, flags);
  9815. }
  9816. static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
  9817. struct intel_flip_work *work)
  9818. {
  9819. work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
  9820. /* Ensure that the work item is consistent when activating it ... */
  9821. smp_mb__before_atomic();
  9822. atomic_set(&work->pending, 1);
  9823. }
  9824. static int intel_gen2_queue_flip(struct drm_device *dev,
  9825. struct drm_crtc *crtc,
  9826. struct drm_framebuffer *fb,
  9827. struct drm_i915_gem_object *obj,
  9828. struct drm_i915_gem_request *req,
  9829. uint32_t flags)
  9830. {
  9831. struct intel_ring *ring = req->ring;
  9832. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9833. u32 flip_mask;
  9834. int ret;
  9835. ret = intel_ring_begin(req, 6);
  9836. if (ret)
  9837. return ret;
  9838. /* Can't queue multiple flips, so wait for the previous
  9839. * one to finish before executing the next.
  9840. */
  9841. if (intel_crtc->plane)
  9842. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9843. else
  9844. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9845. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9846. intel_ring_emit(ring, MI_NOOP);
  9847. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9848. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9849. intel_ring_emit(ring, fb->pitches[0]);
  9850. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  9851. intel_ring_emit(ring, 0); /* aux display base address, unused */
  9852. return 0;
  9853. }
  9854. static int intel_gen3_queue_flip(struct drm_device *dev,
  9855. struct drm_crtc *crtc,
  9856. struct drm_framebuffer *fb,
  9857. struct drm_i915_gem_object *obj,
  9858. struct drm_i915_gem_request *req,
  9859. uint32_t flags)
  9860. {
  9861. struct intel_ring *ring = req->ring;
  9862. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9863. u32 flip_mask;
  9864. int ret;
  9865. ret = intel_ring_begin(req, 6);
  9866. if (ret)
  9867. return ret;
  9868. if (intel_crtc->plane)
  9869. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9870. else
  9871. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9872. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9873. intel_ring_emit(ring, MI_NOOP);
  9874. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  9875. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9876. intel_ring_emit(ring, fb->pitches[0]);
  9877. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  9878. intel_ring_emit(ring, MI_NOOP);
  9879. return 0;
  9880. }
  9881. static int intel_gen4_queue_flip(struct drm_device *dev,
  9882. struct drm_crtc *crtc,
  9883. struct drm_framebuffer *fb,
  9884. struct drm_i915_gem_object *obj,
  9885. struct drm_i915_gem_request *req,
  9886. uint32_t flags)
  9887. {
  9888. struct intel_ring *ring = req->ring;
  9889. struct drm_i915_private *dev_priv = to_i915(dev);
  9890. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9891. uint32_t pf, pipesrc;
  9892. int ret;
  9893. ret = intel_ring_begin(req, 4);
  9894. if (ret)
  9895. return ret;
  9896. /* i965+ uses the linear or tiled offsets from the
  9897. * Display Registers (which do not change across a page-flip)
  9898. * so we need only reprogram the base address.
  9899. */
  9900. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9901. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9902. intel_ring_emit(ring, fb->pitches[0]);
  9903. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset |
  9904. intel_fb_modifier_to_tiling(fb->modifier[0]));
  9905. /* XXX Enabling the panel-fitter across page-flip is so far
  9906. * untested on non-native modes, so ignore it for now.
  9907. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9908. */
  9909. pf = 0;
  9910. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9911. intel_ring_emit(ring, pf | pipesrc);
  9912. return 0;
  9913. }
  9914. static int intel_gen6_queue_flip(struct drm_device *dev,
  9915. struct drm_crtc *crtc,
  9916. struct drm_framebuffer *fb,
  9917. struct drm_i915_gem_object *obj,
  9918. struct drm_i915_gem_request *req,
  9919. uint32_t flags)
  9920. {
  9921. struct intel_ring *ring = req->ring;
  9922. struct drm_i915_private *dev_priv = to_i915(dev);
  9923. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9924. uint32_t pf, pipesrc;
  9925. int ret;
  9926. ret = intel_ring_begin(req, 4);
  9927. if (ret)
  9928. return ret;
  9929. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9930. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9931. intel_ring_emit(ring, fb->pitches[0] |
  9932. intel_fb_modifier_to_tiling(fb->modifier[0]));
  9933. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  9934. /* Contrary to the suggestions in the documentation,
  9935. * "Enable Panel Fitter" does not seem to be required when page
  9936. * flipping with a non-native mode, and worse causes a normal
  9937. * modeset to fail.
  9938. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9939. */
  9940. pf = 0;
  9941. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9942. intel_ring_emit(ring, pf | pipesrc);
  9943. return 0;
  9944. }
  9945. static int intel_gen7_queue_flip(struct drm_device *dev,
  9946. struct drm_crtc *crtc,
  9947. struct drm_framebuffer *fb,
  9948. struct drm_i915_gem_object *obj,
  9949. struct drm_i915_gem_request *req,
  9950. uint32_t flags)
  9951. {
  9952. struct drm_i915_private *dev_priv = to_i915(dev);
  9953. struct intel_ring *ring = req->ring;
  9954. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9955. uint32_t plane_bit = 0;
  9956. int len, ret;
  9957. switch (intel_crtc->plane) {
  9958. case PLANE_A:
  9959. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9960. break;
  9961. case PLANE_B:
  9962. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9963. break;
  9964. case PLANE_C:
  9965. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9966. break;
  9967. default:
  9968. WARN_ONCE(1, "unknown plane in flip command\n");
  9969. return -ENODEV;
  9970. }
  9971. len = 4;
  9972. if (req->engine->id == RCS) {
  9973. len += 6;
  9974. /*
  9975. * On Gen 8, SRM is now taking an extra dword to accommodate
  9976. * 48bits addresses, and we need a NOOP for the batch size to
  9977. * stay even.
  9978. */
  9979. if (IS_GEN8(dev_priv))
  9980. len += 2;
  9981. }
  9982. /*
  9983. * BSpec MI_DISPLAY_FLIP for IVB:
  9984. * "The full packet must be contained within the same cache line."
  9985. *
  9986. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9987. * cacheline, if we ever start emitting more commands before
  9988. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9989. * then do the cacheline alignment, and finally emit the
  9990. * MI_DISPLAY_FLIP.
  9991. */
  9992. ret = intel_ring_cacheline_align(req);
  9993. if (ret)
  9994. return ret;
  9995. ret = intel_ring_begin(req, len);
  9996. if (ret)
  9997. return ret;
  9998. /* Unmask the flip-done completion message. Note that the bspec says that
  9999. * we should do this for both the BCS and RCS, and that we must not unmask
  10000. * more than one flip event at any time (or ensure that one flip message
  10001. * can be sent by waiting for flip-done prior to queueing new flips).
  10002. * Experimentation says that BCS works despite DERRMR masking all
  10003. * flip-done completion events and that unmasking all planes at once
  10004. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  10005. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  10006. */
  10007. if (req->engine->id == RCS) {
  10008. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  10009. intel_ring_emit_reg(ring, DERRMR);
  10010. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  10011. DERRMR_PIPEB_PRI_FLIP_DONE |
  10012. DERRMR_PIPEC_PRI_FLIP_DONE));
  10013. if (IS_GEN8(dev_priv))
  10014. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
  10015. MI_SRM_LRM_GLOBAL_GTT);
  10016. else
  10017. intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
  10018. MI_SRM_LRM_GLOBAL_GTT);
  10019. intel_ring_emit_reg(ring, DERRMR);
  10020. intel_ring_emit(ring,
  10021. i915_ggtt_offset(req->engine->scratch) + 256);
  10022. if (IS_GEN8(dev_priv)) {
  10023. intel_ring_emit(ring, 0);
  10024. intel_ring_emit(ring, MI_NOOP);
  10025. }
  10026. }
  10027. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  10028. intel_ring_emit(ring, fb->pitches[0] |
  10029. intel_fb_modifier_to_tiling(fb->modifier[0]));
  10030. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  10031. intel_ring_emit(ring, (MI_NOOP));
  10032. return 0;
  10033. }
  10034. static bool use_mmio_flip(struct intel_engine_cs *engine,
  10035. struct drm_i915_gem_object *obj)
  10036. {
  10037. /*
  10038. * This is not being used for older platforms, because
  10039. * non-availability of flip done interrupt forces us to use
  10040. * CS flips. Older platforms derive flip done using some clever
  10041. * tricks involving the flip_pending status bits and vblank irqs.
  10042. * So using MMIO flips there would disrupt this mechanism.
  10043. */
  10044. if (engine == NULL)
  10045. return true;
  10046. if (INTEL_GEN(engine->i915) < 5)
  10047. return false;
  10048. if (i915.use_mmio_flip < 0)
  10049. return false;
  10050. else if (i915.use_mmio_flip > 0)
  10051. return true;
  10052. else if (i915.enable_execlists)
  10053. return true;
  10054. return engine != i915_gem_object_last_write_engine(obj);
  10055. }
  10056. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
  10057. unsigned int rotation,
  10058. struct intel_flip_work *work)
  10059. {
  10060. struct drm_device *dev = intel_crtc->base.dev;
  10061. struct drm_i915_private *dev_priv = to_i915(dev);
  10062. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  10063. const enum pipe pipe = intel_crtc->pipe;
  10064. u32 ctl, stride = skl_plane_stride(fb, 0, rotation);
  10065. ctl = I915_READ(PLANE_CTL(pipe, 0));
  10066. ctl &= ~PLANE_CTL_TILED_MASK;
  10067. switch (fb->modifier[0]) {
  10068. case DRM_FORMAT_MOD_NONE:
  10069. break;
  10070. case I915_FORMAT_MOD_X_TILED:
  10071. ctl |= PLANE_CTL_TILED_X;
  10072. break;
  10073. case I915_FORMAT_MOD_Y_TILED:
  10074. ctl |= PLANE_CTL_TILED_Y;
  10075. break;
  10076. case I915_FORMAT_MOD_Yf_TILED:
  10077. ctl |= PLANE_CTL_TILED_YF;
  10078. break;
  10079. default:
  10080. MISSING_CASE(fb->modifier[0]);
  10081. }
  10082. /*
  10083. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  10084. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  10085. */
  10086. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  10087. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  10088. I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
  10089. POSTING_READ(PLANE_SURF(pipe, 0));
  10090. }
  10091. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
  10092. struct intel_flip_work *work)
  10093. {
  10094. struct drm_device *dev = intel_crtc->base.dev;
  10095. struct drm_i915_private *dev_priv = to_i915(dev);
  10096. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  10097. i915_reg_t reg = DSPCNTR(intel_crtc->plane);
  10098. u32 dspcntr;
  10099. dspcntr = I915_READ(reg);
  10100. if (fb->modifier[0] == I915_FORMAT_MOD_X_TILED)
  10101. dspcntr |= DISPPLANE_TILED;
  10102. else
  10103. dspcntr &= ~DISPPLANE_TILED;
  10104. I915_WRITE(reg, dspcntr);
  10105. I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
  10106. POSTING_READ(DSPSURF(intel_crtc->plane));
  10107. }
  10108. static void intel_mmio_flip_work_func(struct work_struct *w)
  10109. {
  10110. struct intel_flip_work *work =
  10111. container_of(w, struct intel_flip_work, mmio_work);
  10112. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  10113. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  10114. struct intel_framebuffer *intel_fb =
  10115. to_intel_framebuffer(crtc->base.primary->fb);
  10116. struct drm_i915_gem_object *obj = intel_fb->obj;
  10117. WARN_ON(i915_gem_object_wait(obj, 0, MAX_SCHEDULE_TIMEOUT, NULL) < 0);
  10118. intel_pipe_update_start(crtc);
  10119. if (INTEL_GEN(dev_priv) >= 9)
  10120. skl_do_mmio_flip(crtc, work->rotation, work);
  10121. else
  10122. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  10123. ilk_do_mmio_flip(crtc, work);
  10124. intel_pipe_update_end(crtc, work);
  10125. }
  10126. static int intel_default_queue_flip(struct drm_device *dev,
  10127. struct drm_crtc *crtc,
  10128. struct drm_framebuffer *fb,
  10129. struct drm_i915_gem_object *obj,
  10130. struct drm_i915_gem_request *req,
  10131. uint32_t flags)
  10132. {
  10133. return -ENODEV;
  10134. }
  10135. static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
  10136. struct intel_crtc *intel_crtc,
  10137. struct intel_flip_work *work)
  10138. {
  10139. u32 addr, vblank;
  10140. if (!atomic_read(&work->pending))
  10141. return false;
  10142. smp_rmb();
  10143. vblank = intel_crtc_get_vblank_counter(intel_crtc);
  10144. if (work->flip_ready_vblank == 0) {
  10145. if (work->flip_queued_req &&
  10146. !i915_gem_request_completed(work->flip_queued_req))
  10147. return false;
  10148. work->flip_ready_vblank = vblank;
  10149. }
  10150. if (vblank - work->flip_ready_vblank < 3)
  10151. return false;
  10152. /* Potential stall - if we see that the flip has happened,
  10153. * assume a missed interrupt. */
  10154. if (INTEL_GEN(dev_priv) >= 4)
  10155. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  10156. else
  10157. addr = I915_READ(DSPADDR(intel_crtc->plane));
  10158. /* There is a potential issue here with a false positive after a flip
  10159. * to the same address. We could address this by checking for a
  10160. * non-incrementing frame counter.
  10161. */
  10162. return addr == work->gtt_offset;
  10163. }
  10164. void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
  10165. {
  10166. struct drm_device *dev = &dev_priv->drm;
  10167. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  10168. struct intel_flip_work *work;
  10169. WARN_ON(!in_interrupt());
  10170. if (crtc == NULL)
  10171. return;
  10172. spin_lock(&dev->event_lock);
  10173. work = crtc->flip_work;
  10174. if (work != NULL && !is_mmio_work(work) &&
  10175. __pageflip_stall_check_cs(dev_priv, crtc, work)) {
  10176. WARN_ONCE(1,
  10177. "Kicking stuck page flip: queued at %d, now %d\n",
  10178. work->flip_queued_vblank, intel_crtc_get_vblank_counter(crtc));
  10179. page_flip_completed(crtc);
  10180. work = NULL;
  10181. }
  10182. if (work != NULL && !is_mmio_work(work) &&
  10183. intel_crtc_get_vblank_counter(crtc) - work->flip_queued_vblank > 1)
  10184. intel_queue_rps_boost_for_request(work->flip_queued_req);
  10185. spin_unlock(&dev->event_lock);
  10186. }
  10187. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  10188. struct drm_framebuffer *fb,
  10189. struct drm_pending_vblank_event *event,
  10190. uint32_t page_flip_flags)
  10191. {
  10192. struct drm_device *dev = crtc->dev;
  10193. struct drm_i915_private *dev_priv = to_i915(dev);
  10194. struct drm_framebuffer *old_fb = crtc->primary->fb;
  10195. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10196. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10197. struct drm_plane *primary = crtc->primary;
  10198. enum pipe pipe = intel_crtc->pipe;
  10199. struct intel_flip_work *work;
  10200. struct intel_engine_cs *engine;
  10201. bool mmio_flip;
  10202. struct drm_i915_gem_request *request;
  10203. struct i915_vma *vma;
  10204. int ret;
  10205. /*
  10206. * drm_mode_page_flip_ioctl() should already catch this, but double
  10207. * check to be safe. In the future we may enable pageflipping from
  10208. * a disabled primary plane.
  10209. */
  10210. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  10211. return -EBUSY;
  10212. /* Can't change pixel format via MI display flips. */
  10213. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  10214. return -EINVAL;
  10215. /*
  10216. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  10217. * Note that pitch changes could also affect these register.
  10218. */
  10219. if (INTEL_INFO(dev)->gen > 3 &&
  10220. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  10221. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  10222. return -EINVAL;
  10223. if (i915_terminally_wedged(&dev_priv->gpu_error))
  10224. goto out_hang;
  10225. work = kzalloc(sizeof(*work), GFP_KERNEL);
  10226. if (work == NULL)
  10227. return -ENOMEM;
  10228. work->event = event;
  10229. work->crtc = crtc;
  10230. work->old_fb = old_fb;
  10231. INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
  10232. ret = drm_crtc_vblank_get(crtc);
  10233. if (ret)
  10234. goto free_work;
  10235. /* We borrow the event spin lock for protecting flip_work */
  10236. spin_lock_irq(&dev->event_lock);
  10237. if (intel_crtc->flip_work) {
  10238. /* Before declaring the flip queue wedged, check if
  10239. * the hardware completed the operation behind our backs.
  10240. */
  10241. if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
  10242. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  10243. page_flip_completed(intel_crtc);
  10244. } else {
  10245. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  10246. spin_unlock_irq(&dev->event_lock);
  10247. drm_crtc_vblank_put(crtc);
  10248. kfree(work);
  10249. return -EBUSY;
  10250. }
  10251. }
  10252. intel_crtc->flip_work = work;
  10253. spin_unlock_irq(&dev->event_lock);
  10254. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  10255. flush_workqueue(dev_priv->wq);
  10256. /* Reference the objects for the scheduled work. */
  10257. drm_framebuffer_reference(work->old_fb);
  10258. crtc->primary->fb = fb;
  10259. update_state_fb(crtc->primary);
  10260. work->pending_flip_obj = i915_gem_object_get(obj);
  10261. ret = i915_mutex_lock_interruptible(dev);
  10262. if (ret)
  10263. goto cleanup;
  10264. intel_crtc->reset_count = i915_reset_count(&dev_priv->gpu_error);
  10265. if (i915_reset_in_progress_or_wedged(&dev_priv->gpu_error)) {
  10266. ret = -EIO;
  10267. goto cleanup;
  10268. }
  10269. atomic_inc(&intel_crtc->unpin_work_count);
  10270. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  10271. work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
  10272. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  10273. engine = dev_priv->engine[BCS];
  10274. if (fb->modifier[0] != old_fb->modifier[0])
  10275. /* vlv: DISPLAY_FLIP fails to change tiling */
  10276. engine = NULL;
  10277. } else if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
  10278. engine = dev_priv->engine[BCS];
  10279. } else if (INTEL_INFO(dev)->gen >= 7) {
  10280. engine = i915_gem_object_last_write_engine(obj);
  10281. if (engine == NULL || engine->id != RCS)
  10282. engine = dev_priv->engine[BCS];
  10283. } else {
  10284. engine = dev_priv->engine[RCS];
  10285. }
  10286. mmio_flip = use_mmio_flip(engine, obj);
  10287. vma = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  10288. if (IS_ERR(vma)) {
  10289. ret = PTR_ERR(vma);
  10290. goto cleanup_pending;
  10291. }
  10292. work->gtt_offset = intel_fb_gtt_offset(fb, primary->state->rotation);
  10293. work->gtt_offset += intel_crtc->dspaddr_offset;
  10294. work->rotation = crtc->primary->state->rotation;
  10295. /*
  10296. * There's the potential that the next frame will not be compatible with
  10297. * FBC, so we want to call pre_update() before the actual page flip.
  10298. * The problem is that pre_update() caches some information about the fb
  10299. * object, so we want to do this only after the object is pinned. Let's
  10300. * be on the safe side and do this immediately before scheduling the
  10301. * flip.
  10302. */
  10303. intel_fbc_pre_update(intel_crtc, intel_crtc->config,
  10304. to_intel_plane_state(primary->state));
  10305. if (mmio_flip) {
  10306. INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
  10307. queue_work(system_unbound_wq, &work->mmio_work);
  10308. } else {
  10309. request = i915_gem_request_alloc(engine, engine->last_context);
  10310. if (IS_ERR(request)) {
  10311. ret = PTR_ERR(request);
  10312. goto cleanup_unpin;
  10313. }
  10314. ret = i915_gem_request_await_object(request, obj, false);
  10315. if (ret)
  10316. goto cleanup_request;
  10317. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  10318. page_flip_flags);
  10319. if (ret)
  10320. goto cleanup_request;
  10321. intel_mark_page_flip_active(intel_crtc, work);
  10322. work->flip_queued_req = i915_gem_request_get(request);
  10323. i915_add_request_no_flush(request);
  10324. }
  10325. i915_gem_track_fb(intel_fb_obj(old_fb), obj,
  10326. to_intel_plane(primary)->frontbuffer_bit);
  10327. mutex_unlock(&dev->struct_mutex);
  10328. intel_frontbuffer_flip_prepare(to_i915(dev),
  10329. to_intel_plane(primary)->frontbuffer_bit);
  10330. trace_i915_flip_request(intel_crtc->plane, obj);
  10331. return 0;
  10332. cleanup_request:
  10333. i915_add_request_no_flush(request);
  10334. cleanup_unpin:
  10335. intel_unpin_fb_obj(fb, crtc->primary->state->rotation);
  10336. cleanup_pending:
  10337. atomic_dec(&intel_crtc->unpin_work_count);
  10338. mutex_unlock(&dev->struct_mutex);
  10339. cleanup:
  10340. crtc->primary->fb = old_fb;
  10341. update_state_fb(crtc->primary);
  10342. i915_gem_object_put(obj);
  10343. drm_framebuffer_unreference(work->old_fb);
  10344. spin_lock_irq(&dev->event_lock);
  10345. intel_crtc->flip_work = NULL;
  10346. spin_unlock_irq(&dev->event_lock);
  10347. drm_crtc_vblank_put(crtc);
  10348. free_work:
  10349. kfree(work);
  10350. if (ret == -EIO) {
  10351. struct drm_atomic_state *state;
  10352. struct drm_plane_state *plane_state;
  10353. out_hang:
  10354. state = drm_atomic_state_alloc(dev);
  10355. if (!state)
  10356. return -ENOMEM;
  10357. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  10358. retry:
  10359. plane_state = drm_atomic_get_plane_state(state, primary);
  10360. ret = PTR_ERR_OR_ZERO(plane_state);
  10361. if (!ret) {
  10362. drm_atomic_set_fb_for_plane(plane_state, fb);
  10363. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  10364. if (!ret)
  10365. ret = drm_atomic_commit(state);
  10366. }
  10367. if (ret == -EDEADLK) {
  10368. drm_modeset_backoff(state->acquire_ctx);
  10369. drm_atomic_state_clear(state);
  10370. goto retry;
  10371. }
  10372. drm_atomic_state_put(state);
  10373. if (ret == 0 && event) {
  10374. spin_lock_irq(&dev->event_lock);
  10375. drm_crtc_send_vblank_event(crtc, event);
  10376. spin_unlock_irq(&dev->event_lock);
  10377. }
  10378. }
  10379. return ret;
  10380. }
  10381. /**
  10382. * intel_wm_need_update - Check whether watermarks need updating
  10383. * @plane: drm plane
  10384. * @state: new plane state
  10385. *
  10386. * Check current plane state versus the new one to determine whether
  10387. * watermarks need to be recalculated.
  10388. *
  10389. * Returns true or false.
  10390. */
  10391. static bool intel_wm_need_update(struct drm_plane *plane,
  10392. struct drm_plane_state *state)
  10393. {
  10394. struct intel_plane_state *new = to_intel_plane_state(state);
  10395. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  10396. /* Update watermarks on tiling or size changes. */
  10397. if (new->base.visible != cur->base.visible)
  10398. return true;
  10399. if (!cur->base.fb || !new->base.fb)
  10400. return false;
  10401. if (cur->base.fb->modifier[0] != new->base.fb->modifier[0] ||
  10402. cur->base.rotation != new->base.rotation ||
  10403. drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
  10404. drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
  10405. drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
  10406. drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
  10407. return true;
  10408. return false;
  10409. }
  10410. static bool needs_scaling(struct intel_plane_state *state)
  10411. {
  10412. int src_w = drm_rect_width(&state->base.src) >> 16;
  10413. int src_h = drm_rect_height(&state->base.src) >> 16;
  10414. int dst_w = drm_rect_width(&state->base.dst);
  10415. int dst_h = drm_rect_height(&state->base.dst);
  10416. return (src_w != dst_w || src_h != dst_h);
  10417. }
  10418. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  10419. struct drm_plane_state *plane_state)
  10420. {
  10421. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  10422. struct drm_crtc *crtc = crtc_state->crtc;
  10423. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10424. struct drm_plane *plane = plane_state->plane;
  10425. struct drm_device *dev = crtc->dev;
  10426. struct drm_i915_private *dev_priv = to_i915(dev);
  10427. struct intel_plane_state *old_plane_state =
  10428. to_intel_plane_state(plane->state);
  10429. bool mode_changed = needs_modeset(crtc_state);
  10430. bool was_crtc_enabled = crtc->state->active;
  10431. bool is_crtc_enabled = crtc_state->active;
  10432. bool turn_off, turn_on, visible, was_visible;
  10433. struct drm_framebuffer *fb = plane_state->fb;
  10434. int ret;
  10435. if (INTEL_GEN(dev_priv) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
  10436. ret = skl_update_scaler_plane(
  10437. to_intel_crtc_state(crtc_state),
  10438. to_intel_plane_state(plane_state));
  10439. if (ret)
  10440. return ret;
  10441. }
  10442. was_visible = old_plane_state->base.visible;
  10443. visible = to_intel_plane_state(plane_state)->base.visible;
  10444. if (!was_crtc_enabled && WARN_ON(was_visible))
  10445. was_visible = false;
  10446. /*
  10447. * Visibility is calculated as if the crtc was on, but
  10448. * after scaler setup everything depends on it being off
  10449. * when the crtc isn't active.
  10450. *
  10451. * FIXME this is wrong for watermarks. Watermarks should also
  10452. * be computed as if the pipe would be active. Perhaps move
  10453. * per-plane wm computation to the .check_plane() hook, and
  10454. * only combine the results from all planes in the current place?
  10455. */
  10456. if (!is_crtc_enabled)
  10457. to_intel_plane_state(plane_state)->base.visible = visible = false;
  10458. if (!was_visible && !visible)
  10459. return 0;
  10460. if (fb != old_plane_state->base.fb)
  10461. pipe_config->fb_changed = true;
  10462. turn_off = was_visible && (!visible || mode_changed);
  10463. turn_on = visible && (!was_visible || mode_changed);
  10464. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  10465. intel_crtc->base.base.id,
  10466. intel_crtc->base.name,
  10467. plane->base.id, plane->name,
  10468. fb ? fb->base.id : -1);
  10469. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  10470. plane->base.id, plane->name,
  10471. was_visible, visible,
  10472. turn_off, turn_on, mode_changed);
  10473. if (turn_on) {
  10474. pipe_config->update_wm_pre = true;
  10475. /* must disable cxsr around plane enable/disable */
  10476. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  10477. pipe_config->disable_cxsr = true;
  10478. } else if (turn_off) {
  10479. pipe_config->update_wm_post = true;
  10480. /* must disable cxsr around plane enable/disable */
  10481. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  10482. pipe_config->disable_cxsr = true;
  10483. } else if (intel_wm_need_update(plane, plane_state)) {
  10484. /* FIXME bollocks */
  10485. pipe_config->update_wm_pre = true;
  10486. pipe_config->update_wm_post = true;
  10487. }
  10488. /* Pre-gen9 platforms need two-step watermark updates */
  10489. if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
  10490. INTEL_INFO(dev)->gen < 9 && dev_priv->display.optimize_watermarks)
  10491. to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
  10492. if (visible || was_visible)
  10493. pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
  10494. /*
  10495. * WaCxSRDisabledForSpriteScaling:ivb
  10496. *
  10497. * cstate->update_wm was already set above, so this flag will
  10498. * take effect when we commit and program watermarks.
  10499. */
  10500. if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev_priv) &&
  10501. needs_scaling(to_intel_plane_state(plane_state)) &&
  10502. !needs_scaling(old_plane_state))
  10503. pipe_config->disable_lp_wm = true;
  10504. return 0;
  10505. }
  10506. static bool encoders_cloneable(const struct intel_encoder *a,
  10507. const struct intel_encoder *b)
  10508. {
  10509. /* masks could be asymmetric, so check both ways */
  10510. return a == b || (a->cloneable & (1 << b->type) &&
  10511. b->cloneable & (1 << a->type));
  10512. }
  10513. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  10514. struct intel_crtc *crtc,
  10515. struct intel_encoder *encoder)
  10516. {
  10517. struct intel_encoder *source_encoder;
  10518. struct drm_connector *connector;
  10519. struct drm_connector_state *connector_state;
  10520. int i;
  10521. for_each_connector_in_state(state, connector, connector_state, i) {
  10522. if (connector_state->crtc != &crtc->base)
  10523. continue;
  10524. source_encoder =
  10525. to_intel_encoder(connector_state->best_encoder);
  10526. if (!encoders_cloneable(encoder, source_encoder))
  10527. return false;
  10528. }
  10529. return true;
  10530. }
  10531. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  10532. struct drm_crtc_state *crtc_state)
  10533. {
  10534. struct drm_device *dev = crtc->dev;
  10535. struct drm_i915_private *dev_priv = to_i915(dev);
  10536. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10537. struct intel_crtc_state *pipe_config =
  10538. to_intel_crtc_state(crtc_state);
  10539. struct drm_atomic_state *state = crtc_state->state;
  10540. int ret;
  10541. bool mode_changed = needs_modeset(crtc_state);
  10542. if (mode_changed && !crtc_state->active)
  10543. pipe_config->update_wm_post = true;
  10544. if (mode_changed && crtc_state->enable &&
  10545. dev_priv->display.crtc_compute_clock &&
  10546. !WARN_ON(pipe_config->shared_dpll)) {
  10547. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  10548. pipe_config);
  10549. if (ret)
  10550. return ret;
  10551. }
  10552. if (crtc_state->color_mgmt_changed) {
  10553. ret = intel_color_check(crtc, crtc_state);
  10554. if (ret)
  10555. return ret;
  10556. /*
  10557. * Changing color management on Intel hardware is
  10558. * handled as part of planes update.
  10559. */
  10560. crtc_state->planes_changed = true;
  10561. }
  10562. ret = 0;
  10563. if (dev_priv->display.compute_pipe_wm) {
  10564. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  10565. if (ret) {
  10566. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  10567. return ret;
  10568. }
  10569. }
  10570. if (dev_priv->display.compute_intermediate_wm &&
  10571. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  10572. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  10573. return 0;
  10574. /*
  10575. * Calculate 'intermediate' watermarks that satisfy both the
  10576. * old state and the new state. We can program these
  10577. * immediately.
  10578. */
  10579. ret = dev_priv->display.compute_intermediate_wm(crtc->dev,
  10580. intel_crtc,
  10581. pipe_config);
  10582. if (ret) {
  10583. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  10584. return ret;
  10585. }
  10586. } else if (dev_priv->display.compute_intermediate_wm) {
  10587. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  10588. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  10589. }
  10590. if (INTEL_INFO(dev)->gen >= 9) {
  10591. if (mode_changed)
  10592. ret = skl_update_scaler_crtc(pipe_config);
  10593. if (!ret)
  10594. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  10595. pipe_config);
  10596. }
  10597. return ret;
  10598. }
  10599. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  10600. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  10601. .atomic_begin = intel_begin_crtc_commit,
  10602. .atomic_flush = intel_finish_crtc_commit,
  10603. .atomic_check = intel_crtc_atomic_check,
  10604. };
  10605. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  10606. {
  10607. struct intel_connector *connector;
  10608. for_each_intel_connector(dev, connector) {
  10609. if (connector->base.state->crtc)
  10610. drm_connector_unreference(&connector->base);
  10611. if (connector->base.encoder) {
  10612. connector->base.state->best_encoder =
  10613. connector->base.encoder;
  10614. connector->base.state->crtc =
  10615. connector->base.encoder->crtc;
  10616. drm_connector_reference(&connector->base);
  10617. } else {
  10618. connector->base.state->best_encoder = NULL;
  10619. connector->base.state->crtc = NULL;
  10620. }
  10621. }
  10622. }
  10623. static void
  10624. connected_sink_compute_bpp(struct intel_connector *connector,
  10625. struct intel_crtc_state *pipe_config)
  10626. {
  10627. const struct drm_display_info *info = &connector->base.display_info;
  10628. int bpp = pipe_config->pipe_bpp;
  10629. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  10630. connector->base.base.id,
  10631. connector->base.name);
  10632. /* Don't use an invalid EDID bpc value */
  10633. if (info->bpc != 0 && info->bpc * 3 < bpp) {
  10634. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  10635. bpp, info->bpc * 3);
  10636. pipe_config->pipe_bpp = info->bpc * 3;
  10637. }
  10638. /* Clamp bpp to 8 on screens without EDID 1.4 */
  10639. if (info->bpc == 0 && bpp > 24) {
  10640. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  10641. bpp);
  10642. pipe_config->pipe_bpp = 24;
  10643. }
  10644. }
  10645. static int
  10646. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  10647. struct intel_crtc_state *pipe_config)
  10648. {
  10649. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  10650. struct drm_atomic_state *state;
  10651. struct drm_connector *connector;
  10652. struct drm_connector_state *connector_state;
  10653. int bpp, i;
  10654. if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  10655. IS_CHERRYVIEW(dev_priv)))
  10656. bpp = 10*3;
  10657. else if (INTEL_GEN(dev_priv) >= 5)
  10658. bpp = 12*3;
  10659. else
  10660. bpp = 8*3;
  10661. pipe_config->pipe_bpp = bpp;
  10662. state = pipe_config->base.state;
  10663. /* Clamp display bpp to EDID value */
  10664. for_each_connector_in_state(state, connector, connector_state, i) {
  10665. if (connector_state->crtc != &crtc->base)
  10666. continue;
  10667. connected_sink_compute_bpp(to_intel_connector(connector),
  10668. pipe_config);
  10669. }
  10670. return bpp;
  10671. }
  10672. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  10673. {
  10674. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  10675. "type: 0x%x flags: 0x%x\n",
  10676. mode->crtc_clock,
  10677. mode->crtc_hdisplay, mode->crtc_hsync_start,
  10678. mode->crtc_hsync_end, mode->crtc_htotal,
  10679. mode->crtc_vdisplay, mode->crtc_vsync_start,
  10680. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  10681. }
  10682. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  10683. struct intel_crtc_state *pipe_config,
  10684. const char *context)
  10685. {
  10686. struct drm_device *dev = crtc->base.dev;
  10687. struct drm_i915_private *dev_priv = to_i915(dev);
  10688. struct drm_plane *plane;
  10689. struct intel_plane *intel_plane;
  10690. struct intel_plane_state *state;
  10691. struct drm_framebuffer *fb;
  10692. DRM_DEBUG_KMS("[CRTC:%d:%s]%s config %p for pipe %c\n",
  10693. crtc->base.base.id, crtc->base.name,
  10694. context, pipe_config, pipe_name(crtc->pipe));
  10695. DRM_DEBUG_KMS("cpu_transcoder: %s\n", transcoder_name(pipe_config->cpu_transcoder));
  10696. DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
  10697. pipe_config->pipe_bpp, pipe_config->dither);
  10698. DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10699. pipe_config->has_pch_encoder,
  10700. pipe_config->fdi_lanes,
  10701. pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
  10702. pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
  10703. pipe_config->fdi_m_n.tu);
  10704. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10705. intel_crtc_has_dp_encoder(pipe_config),
  10706. pipe_config->lane_count,
  10707. pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
  10708. pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
  10709. pipe_config->dp_m_n.tu);
  10710. DRM_DEBUG_KMS("dp: %i, lanes: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
  10711. intel_crtc_has_dp_encoder(pipe_config),
  10712. pipe_config->lane_count,
  10713. pipe_config->dp_m2_n2.gmch_m,
  10714. pipe_config->dp_m2_n2.gmch_n,
  10715. pipe_config->dp_m2_n2.link_m,
  10716. pipe_config->dp_m2_n2.link_n,
  10717. pipe_config->dp_m2_n2.tu);
  10718. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  10719. pipe_config->has_audio,
  10720. pipe_config->has_infoframe);
  10721. DRM_DEBUG_KMS("requested mode:\n");
  10722. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  10723. DRM_DEBUG_KMS("adjusted mode:\n");
  10724. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  10725. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  10726. DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
  10727. DRM_DEBUG_KMS("pipe src size: %dx%d\n",
  10728. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  10729. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  10730. crtc->num_scalers,
  10731. pipe_config->scaler_state.scaler_users,
  10732. pipe_config->scaler_state.scaler_id);
  10733. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10734. pipe_config->gmch_pfit.control,
  10735. pipe_config->gmch_pfit.pgm_ratios,
  10736. pipe_config->gmch_pfit.lvds_border_bits);
  10737. DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
  10738. pipe_config->pch_pfit.pos,
  10739. pipe_config->pch_pfit.size,
  10740. pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
  10741. DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
  10742. DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
  10743. if (IS_BROXTON(dev_priv)) {
  10744. DRM_DEBUG_KMS("dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
  10745. "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
  10746. "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
  10747. pipe_config->dpll_hw_state.ebb0,
  10748. pipe_config->dpll_hw_state.ebb4,
  10749. pipe_config->dpll_hw_state.pll0,
  10750. pipe_config->dpll_hw_state.pll1,
  10751. pipe_config->dpll_hw_state.pll2,
  10752. pipe_config->dpll_hw_state.pll3,
  10753. pipe_config->dpll_hw_state.pll6,
  10754. pipe_config->dpll_hw_state.pll8,
  10755. pipe_config->dpll_hw_state.pll9,
  10756. pipe_config->dpll_hw_state.pll10,
  10757. pipe_config->dpll_hw_state.pcsdw12);
  10758. } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
  10759. DRM_DEBUG_KMS("dpll_hw_state: "
  10760. "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
  10761. pipe_config->dpll_hw_state.ctrl1,
  10762. pipe_config->dpll_hw_state.cfgcr1,
  10763. pipe_config->dpll_hw_state.cfgcr2);
  10764. } else if (HAS_DDI(dev_priv)) {
  10765. DRM_DEBUG_KMS("dpll_hw_state: wrpll: 0x%x spll: 0x%x\n",
  10766. pipe_config->dpll_hw_state.wrpll,
  10767. pipe_config->dpll_hw_state.spll);
  10768. } else {
  10769. DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
  10770. "fp0: 0x%x, fp1: 0x%x\n",
  10771. pipe_config->dpll_hw_state.dpll,
  10772. pipe_config->dpll_hw_state.dpll_md,
  10773. pipe_config->dpll_hw_state.fp0,
  10774. pipe_config->dpll_hw_state.fp1);
  10775. }
  10776. DRM_DEBUG_KMS("planes on this crtc\n");
  10777. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10778. char *format_name;
  10779. intel_plane = to_intel_plane(plane);
  10780. if (intel_plane->pipe != crtc->pipe)
  10781. continue;
  10782. state = to_intel_plane_state(plane->state);
  10783. fb = state->base.fb;
  10784. if (!fb) {
  10785. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  10786. plane->base.id, plane->name, state->scaler_id);
  10787. continue;
  10788. }
  10789. format_name = drm_get_format_name(fb->pixel_format);
  10790. DRM_DEBUG_KMS("[PLANE:%d:%s] enabled",
  10791. plane->base.id, plane->name);
  10792. DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = %s",
  10793. fb->base.id, fb->width, fb->height, format_name);
  10794. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  10795. state->scaler_id,
  10796. state->base.src.x1 >> 16,
  10797. state->base.src.y1 >> 16,
  10798. drm_rect_width(&state->base.src) >> 16,
  10799. drm_rect_height(&state->base.src) >> 16,
  10800. state->base.dst.x1, state->base.dst.y1,
  10801. drm_rect_width(&state->base.dst),
  10802. drm_rect_height(&state->base.dst));
  10803. kfree(format_name);
  10804. }
  10805. }
  10806. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10807. {
  10808. struct drm_device *dev = state->dev;
  10809. struct drm_connector *connector;
  10810. unsigned int used_ports = 0;
  10811. unsigned int used_mst_ports = 0;
  10812. /*
  10813. * Walk the connector list instead of the encoder
  10814. * list to detect the problem on ddi platforms
  10815. * where there's just one encoder per digital port.
  10816. */
  10817. drm_for_each_connector(connector, dev) {
  10818. struct drm_connector_state *connector_state;
  10819. struct intel_encoder *encoder;
  10820. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  10821. if (!connector_state)
  10822. connector_state = connector->state;
  10823. if (!connector_state->best_encoder)
  10824. continue;
  10825. encoder = to_intel_encoder(connector_state->best_encoder);
  10826. WARN_ON(!connector_state->crtc);
  10827. switch (encoder->type) {
  10828. unsigned int port_mask;
  10829. case INTEL_OUTPUT_UNKNOWN:
  10830. if (WARN_ON(!HAS_DDI(to_i915(dev))))
  10831. break;
  10832. case INTEL_OUTPUT_DP:
  10833. case INTEL_OUTPUT_HDMI:
  10834. case INTEL_OUTPUT_EDP:
  10835. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10836. /* the same port mustn't appear more than once */
  10837. if (used_ports & port_mask)
  10838. return false;
  10839. used_ports |= port_mask;
  10840. break;
  10841. case INTEL_OUTPUT_DP_MST:
  10842. used_mst_ports |=
  10843. 1 << enc_to_mst(&encoder->base)->primary->port;
  10844. break;
  10845. default:
  10846. break;
  10847. }
  10848. }
  10849. /* can't mix MST and SST/HDMI on the same port */
  10850. if (used_ports & used_mst_ports)
  10851. return false;
  10852. return true;
  10853. }
  10854. static void
  10855. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10856. {
  10857. struct drm_crtc_state tmp_state;
  10858. struct intel_crtc_scaler_state scaler_state;
  10859. struct intel_dpll_hw_state dpll_hw_state;
  10860. struct intel_shared_dpll *shared_dpll;
  10861. bool force_thru;
  10862. /* FIXME: before the switch to atomic started, a new pipe_config was
  10863. * kzalloc'd. Code that depends on any field being zero should be
  10864. * fixed, so that the crtc_state can be safely duplicated. For now,
  10865. * only fields that are know to not cause problems are preserved. */
  10866. tmp_state = crtc_state->base;
  10867. scaler_state = crtc_state->scaler_state;
  10868. shared_dpll = crtc_state->shared_dpll;
  10869. dpll_hw_state = crtc_state->dpll_hw_state;
  10870. force_thru = crtc_state->pch_pfit.force_thru;
  10871. memset(crtc_state, 0, sizeof *crtc_state);
  10872. crtc_state->base = tmp_state;
  10873. crtc_state->scaler_state = scaler_state;
  10874. crtc_state->shared_dpll = shared_dpll;
  10875. crtc_state->dpll_hw_state = dpll_hw_state;
  10876. crtc_state->pch_pfit.force_thru = force_thru;
  10877. }
  10878. static int
  10879. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10880. struct intel_crtc_state *pipe_config)
  10881. {
  10882. struct drm_atomic_state *state = pipe_config->base.state;
  10883. struct intel_encoder *encoder;
  10884. struct drm_connector *connector;
  10885. struct drm_connector_state *connector_state;
  10886. int base_bpp, ret = -EINVAL;
  10887. int i;
  10888. bool retry = true;
  10889. clear_intel_crtc_state(pipe_config);
  10890. pipe_config->cpu_transcoder =
  10891. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10892. /*
  10893. * Sanitize sync polarity flags based on requested ones. If neither
  10894. * positive or negative polarity is requested, treat this as meaning
  10895. * negative polarity.
  10896. */
  10897. if (!(pipe_config->base.adjusted_mode.flags &
  10898. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10899. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10900. if (!(pipe_config->base.adjusted_mode.flags &
  10901. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10902. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10903. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10904. pipe_config);
  10905. if (base_bpp < 0)
  10906. goto fail;
  10907. /*
  10908. * Determine the real pipe dimensions. Note that stereo modes can
  10909. * increase the actual pipe size due to the frame doubling and
  10910. * insertion of additional space for blanks between the frame. This
  10911. * is stored in the crtc timings. We use the requested mode to do this
  10912. * computation to clearly distinguish it from the adjusted mode, which
  10913. * can be changed by the connectors in the below retry loop.
  10914. */
  10915. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10916. &pipe_config->pipe_src_w,
  10917. &pipe_config->pipe_src_h);
  10918. for_each_connector_in_state(state, connector, connector_state, i) {
  10919. if (connector_state->crtc != crtc)
  10920. continue;
  10921. encoder = to_intel_encoder(connector_state->best_encoder);
  10922. if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
  10923. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  10924. goto fail;
  10925. }
  10926. /*
  10927. * Determine output_types before calling the .compute_config()
  10928. * hooks so that the hooks can use this information safely.
  10929. */
  10930. pipe_config->output_types |= 1 << encoder->type;
  10931. }
  10932. encoder_retry:
  10933. /* Ensure the port clock defaults are reset when retrying. */
  10934. pipe_config->port_clock = 0;
  10935. pipe_config->pixel_multiplier = 1;
  10936. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10937. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10938. CRTC_STEREO_DOUBLE);
  10939. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10940. * adjust it according to limitations or connector properties, and also
  10941. * a chance to reject the mode entirely.
  10942. */
  10943. for_each_connector_in_state(state, connector, connector_state, i) {
  10944. if (connector_state->crtc != crtc)
  10945. continue;
  10946. encoder = to_intel_encoder(connector_state->best_encoder);
  10947. if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
  10948. DRM_DEBUG_KMS("Encoder config failure\n");
  10949. goto fail;
  10950. }
  10951. }
  10952. /* Set default port clock if not overwritten by the encoder. Needs to be
  10953. * done afterwards in case the encoder adjusts the mode. */
  10954. if (!pipe_config->port_clock)
  10955. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10956. * pipe_config->pixel_multiplier;
  10957. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10958. if (ret < 0) {
  10959. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10960. goto fail;
  10961. }
  10962. if (ret == RETRY) {
  10963. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10964. ret = -EINVAL;
  10965. goto fail;
  10966. }
  10967. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10968. retry = false;
  10969. goto encoder_retry;
  10970. }
  10971. /* Dithering seems to not pass-through bits correctly when it should, so
  10972. * only enable it on 6bpc panels. */
  10973. pipe_config->dither = pipe_config->pipe_bpp == 6*3;
  10974. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  10975. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10976. fail:
  10977. return ret;
  10978. }
  10979. static void
  10980. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  10981. {
  10982. struct drm_crtc *crtc;
  10983. struct drm_crtc_state *crtc_state;
  10984. int i;
  10985. /* Double check state. */
  10986. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10987. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10988. /* Update hwmode for vblank functions */
  10989. if (crtc->state->active)
  10990. crtc->hwmode = crtc->state->adjusted_mode;
  10991. else
  10992. crtc->hwmode.crtc_clock = 0;
  10993. /*
  10994. * Update legacy state to satisfy fbc code. This can
  10995. * be removed when fbc uses the atomic state.
  10996. */
  10997. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10998. struct drm_plane_state *plane_state = crtc->primary->state;
  10999. crtc->primary->fb = plane_state->fb;
  11000. crtc->x = plane_state->src_x >> 16;
  11001. crtc->y = plane_state->src_y >> 16;
  11002. }
  11003. }
  11004. }
  11005. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  11006. {
  11007. int diff;
  11008. if (clock1 == clock2)
  11009. return true;
  11010. if (!clock1 || !clock2)
  11011. return false;
  11012. diff = abs(clock1 - clock2);
  11013. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  11014. return true;
  11015. return false;
  11016. }
  11017. static bool
  11018. intel_compare_m_n(unsigned int m, unsigned int n,
  11019. unsigned int m2, unsigned int n2,
  11020. bool exact)
  11021. {
  11022. if (m == m2 && n == n2)
  11023. return true;
  11024. if (exact || !m || !n || !m2 || !n2)
  11025. return false;
  11026. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  11027. if (n > n2) {
  11028. while (n > n2) {
  11029. m2 <<= 1;
  11030. n2 <<= 1;
  11031. }
  11032. } else if (n < n2) {
  11033. while (n < n2) {
  11034. m <<= 1;
  11035. n <<= 1;
  11036. }
  11037. }
  11038. if (n != n2)
  11039. return false;
  11040. return intel_fuzzy_clock_check(m, m2);
  11041. }
  11042. static bool
  11043. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  11044. struct intel_link_m_n *m2_n2,
  11045. bool adjust)
  11046. {
  11047. if (m_n->tu == m2_n2->tu &&
  11048. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  11049. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  11050. intel_compare_m_n(m_n->link_m, m_n->link_n,
  11051. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  11052. if (adjust)
  11053. *m2_n2 = *m_n;
  11054. return true;
  11055. }
  11056. return false;
  11057. }
  11058. static bool
  11059. intel_pipe_config_compare(struct drm_device *dev,
  11060. struct intel_crtc_state *current_config,
  11061. struct intel_crtc_state *pipe_config,
  11062. bool adjust)
  11063. {
  11064. struct drm_i915_private *dev_priv = to_i915(dev);
  11065. bool ret = true;
  11066. #define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
  11067. do { \
  11068. if (!adjust) \
  11069. DRM_ERROR(fmt, ##__VA_ARGS__); \
  11070. else \
  11071. DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
  11072. } while (0)
  11073. #define PIPE_CONF_CHECK_X(name) \
  11074. if (current_config->name != pipe_config->name) { \
  11075. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  11076. "(expected 0x%08x, found 0x%08x)\n", \
  11077. current_config->name, \
  11078. pipe_config->name); \
  11079. ret = false; \
  11080. }
  11081. #define PIPE_CONF_CHECK_I(name) \
  11082. if (current_config->name != pipe_config->name) { \
  11083. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  11084. "(expected %i, found %i)\n", \
  11085. current_config->name, \
  11086. pipe_config->name); \
  11087. ret = false; \
  11088. }
  11089. #define PIPE_CONF_CHECK_P(name) \
  11090. if (current_config->name != pipe_config->name) { \
  11091. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  11092. "(expected %p, found %p)\n", \
  11093. current_config->name, \
  11094. pipe_config->name); \
  11095. ret = false; \
  11096. }
  11097. #define PIPE_CONF_CHECK_M_N(name) \
  11098. if (!intel_compare_link_m_n(&current_config->name, \
  11099. &pipe_config->name,\
  11100. adjust)) { \
  11101. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  11102. "(expected tu %i gmch %i/%i link %i/%i, " \
  11103. "found tu %i, gmch %i/%i link %i/%i)\n", \
  11104. current_config->name.tu, \
  11105. current_config->name.gmch_m, \
  11106. current_config->name.gmch_n, \
  11107. current_config->name.link_m, \
  11108. current_config->name.link_n, \
  11109. pipe_config->name.tu, \
  11110. pipe_config->name.gmch_m, \
  11111. pipe_config->name.gmch_n, \
  11112. pipe_config->name.link_m, \
  11113. pipe_config->name.link_n); \
  11114. ret = false; \
  11115. }
  11116. /* This is required for BDW+ where there is only one set of registers for
  11117. * switching between high and low RR.
  11118. * This macro can be used whenever a comparison has to be made between one
  11119. * hw state and multiple sw state variables.
  11120. */
  11121. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  11122. if (!intel_compare_link_m_n(&current_config->name, \
  11123. &pipe_config->name, adjust) && \
  11124. !intel_compare_link_m_n(&current_config->alt_name, \
  11125. &pipe_config->name, adjust)) { \
  11126. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  11127. "(expected tu %i gmch %i/%i link %i/%i, " \
  11128. "or tu %i gmch %i/%i link %i/%i, " \
  11129. "found tu %i, gmch %i/%i link %i/%i)\n", \
  11130. current_config->name.tu, \
  11131. current_config->name.gmch_m, \
  11132. current_config->name.gmch_n, \
  11133. current_config->name.link_m, \
  11134. current_config->name.link_n, \
  11135. current_config->alt_name.tu, \
  11136. current_config->alt_name.gmch_m, \
  11137. current_config->alt_name.gmch_n, \
  11138. current_config->alt_name.link_m, \
  11139. current_config->alt_name.link_n, \
  11140. pipe_config->name.tu, \
  11141. pipe_config->name.gmch_m, \
  11142. pipe_config->name.gmch_n, \
  11143. pipe_config->name.link_m, \
  11144. pipe_config->name.link_n); \
  11145. ret = false; \
  11146. }
  11147. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  11148. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  11149. INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
  11150. "(expected %i, found %i)\n", \
  11151. current_config->name & (mask), \
  11152. pipe_config->name & (mask)); \
  11153. ret = false; \
  11154. }
  11155. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  11156. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  11157. INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
  11158. "(expected %i, found %i)\n", \
  11159. current_config->name, \
  11160. pipe_config->name); \
  11161. ret = false; \
  11162. }
  11163. #define PIPE_CONF_QUIRK(quirk) \
  11164. ((current_config->quirks | pipe_config->quirks) & (quirk))
  11165. PIPE_CONF_CHECK_I(cpu_transcoder);
  11166. PIPE_CONF_CHECK_I(has_pch_encoder);
  11167. PIPE_CONF_CHECK_I(fdi_lanes);
  11168. PIPE_CONF_CHECK_M_N(fdi_m_n);
  11169. PIPE_CONF_CHECK_I(lane_count);
  11170. PIPE_CONF_CHECK_X(lane_lat_optim_mask);
  11171. if (INTEL_INFO(dev)->gen < 8) {
  11172. PIPE_CONF_CHECK_M_N(dp_m_n);
  11173. if (current_config->has_drrs)
  11174. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  11175. } else
  11176. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  11177. PIPE_CONF_CHECK_X(output_types);
  11178. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  11179. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  11180. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  11181. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  11182. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  11183. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  11184. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  11185. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  11186. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  11187. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  11188. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  11189. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  11190. PIPE_CONF_CHECK_I(pixel_multiplier);
  11191. PIPE_CONF_CHECK_I(has_hdmi_sink);
  11192. if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
  11193. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11194. PIPE_CONF_CHECK_I(limited_color_range);
  11195. PIPE_CONF_CHECK_I(has_infoframe);
  11196. PIPE_CONF_CHECK_I(has_audio);
  11197. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11198. DRM_MODE_FLAG_INTERLACE);
  11199. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  11200. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11201. DRM_MODE_FLAG_PHSYNC);
  11202. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11203. DRM_MODE_FLAG_NHSYNC);
  11204. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11205. DRM_MODE_FLAG_PVSYNC);
  11206. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11207. DRM_MODE_FLAG_NVSYNC);
  11208. }
  11209. PIPE_CONF_CHECK_X(gmch_pfit.control);
  11210. /* pfit ratios are autocomputed by the hw on gen4+ */
  11211. if (INTEL_INFO(dev)->gen < 4)
  11212. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  11213. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  11214. if (!adjust) {
  11215. PIPE_CONF_CHECK_I(pipe_src_w);
  11216. PIPE_CONF_CHECK_I(pipe_src_h);
  11217. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  11218. if (current_config->pch_pfit.enabled) {
  11219. PIPE_CONF_CHECK_X(pch_pfit.pos);
  11220. PIPE_CONF_CHECK_X(pch_pfit.size);
  11221. }
  11222. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  11223. }
  11224. /* BDW+ don't expose a synchronous way to read the state */
  11225. if (IS_HASWELL(dev_priv))
  11226. PIPE_CONF_CHECK_I(ips_enabled);
  11227. PIPE_CONF_CHECK_I(double_wide);
  11228. PIPE_CONF_CHECK_P(shared_dpll);
  11229. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  11230. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  11231. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  11232. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  11233. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  11234. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  11235. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  11236. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  11237. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  11238. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  11239. PIPE_CONF_CHECK_X(dsi_pll.div);
  11240. if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
  11241. PIPE_CONF_CHECK_I(pipe_bpp);
  11242. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  11243. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  11244. #undef PIPE_CONF_CHECK_X
  11245. #undef PIPE_CONF_CHECK_I
  11246. #undef PIPE_CONF_CHECK_P
  11247. #undef PIPE_CONF_CHECK_FLAGS
  11248. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  11249. #undef PIPE_CONF_QUIRK
  11250. #undef INTEL_ERR_OR_DBG_KMS
  11251. return ret;
  11252. }
  11253. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  11254. const struct intel_crtc_state *pipe_config)
  11255. {
  11256. if (pipe_config->has_pch_encoder) {
  11257. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  11258. &pipe_config->fdi_m_n);
  11259. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  11260. /*
  11261. * FDI already provided one idea for the dotclock.
  11262. * Yell if the encoder disagrees.
  11263. */
  11264. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  11265. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  11266. fdi_dotclock, dotclock);
  11267. }
  11268. }
  11269. static void verify_wm_state(struct drm_crtc *crtc,
  11270. struct drm_crtc_state *new_state)
  11271. {
  11272. struct drm_device *dev = crtc->dev;
  11273. struct drm_i915_private *dev_priv = to_i915(dev);
  11274. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  11275. struct skl_pipe_wm hw_wm, *sw_wm;
  11276. struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
  11277. struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
  11278. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11279. const enum pipe pipe = intel_crtc->pipe;
  11280. int plane, level, max_level = ilk_wm_max_level(dev_priv);
  11281. if (INTEL_INFO(dev)->gen < 9 || !new_state->active)
  11282. return;
  11283. skl_pipe_wm_get_hw_state(crtc, &hw_wm);
  11284. sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
  11285. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  11286. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  11287. /* planes */
  11288. for_each_universal_plane(dev_priv, pipe, plane) {
  11289. hw_plane_wm = &hw_wm.planes[plane];
  11290. sw_plane_wm = &sw_wm->planes[plane];
  11291. /* Watermarks */
  11292. for (level = 0; level <= max_level; level++) {
  11293. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  11294. &sw_plane_wm->wm[level]))
  11295. continue;
  11296. 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",
  11297. pipe_name(pipe), plane + 1, level,
  11298. sw_plane_wm->wm[level].plane_en,
  11299. sw_plane_wm->wm[level].plane_res_b,
  11300. sw_plane_wm->wm[level].plane_res_l,
  11301. hw_plane_wm->wm[level].plane_en,
  11302. hw_plane_wm->wm[level].plane_res_b,
  11303. hw_plane_wm->wm[level].plane_res_l);
  11304. }
  11305. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  11306. &sw_plane_wm->trans_wm)) {
  11307. 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",
  11308. pipe_name(pipe), plane + 1,
  11309. sw_plane_wm->trans_wm.plane_en,
  11310. sw_plane_wm->trans_wm.plane_res_b,
  11311. sw_plane_wm->trans_wm.plane_res_l,
  11312. hw_plane_wm->trans_wm.plane_en,
  11313. hw_plane_wm->trans_wm.plane_res_b,
  11314. hw_plane_wm->trans_wm.plane_res_l);
  11315. }
  11316. /* DDB */
  11317. hw_ddb_entry = &hw_ddb.plane[pipe][plane];
  11318. sw_ddb_entry = &sw_ddb->plane[pipe][plane];
  11319. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  11320. DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
  11321. pipe_name(pipe), plane + 1,
  11322. sw_ddb_entry->start, sw_ddb_entry->end,
  11323. hw_ddb_entry->start, hw_ddb_entry->end);
  11324. }
  11325. }
  11326. /*
  11327. * cursor
  11328. * If the cursor plane isn't active, we may not have updated it's ddb
  11329. * allocation. In that case since the ddb allocation will be updated
  11330. * once the plane becomes visible, we can skip this check
  11331. */
  11332. if (intel_crtc->cursor_addr) {
  11333. hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
  11334. sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
  11335. /* Watermarks */
  11336. for (level = 0; level <= max_level; level++) {
  11337. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  11338. &sw_plane_wm->wm[level]))
  11339. continue;
  11340. 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",
  11341. pipe_name(pipe), level,
  11342. sw_plane_wm->wm[level].plane_en,
  11343. sw_plane_wm->wm[level].plane_res_b,
  11344. sw_plane_wm->wm[level].plane_res_l,
  11345. hw_plane_wm->wm[level].plane_en,
  11346. hw_plane_wm->wm[level].plane_res_b,
  11347. hw_plane_wm->wm[level].plane_res_l);
  11348. }
  11349. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  11350. &sw_plane_wm->trans_wm)) {
  11351. DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  11352. pipe_name(pipe),
  11353. sw_plane_wm->trans_wm.plane_en,
  11354. sw_plane_wm->trans_wm.plane_res_b,
  11355. sw_plane_wm->trans_wm.plane_res_l,
  11356. hw_plane_wm->trans_wm.plane_en,
  11357. hw_plane_wm->trans_wm.plane_res_b,
  11358. hw_plane_wm->trans_wm.plane_res_l);
  11359. }
  11360. /* DDB */
  11361. hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  11362. sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  11363. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  11364. DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
  11365. pipe_name(pipe),
  11366. sw_ddb_entry->start, sw_ddb_entry->end,
  11367. hw_ddb_entry->start, hw_ddb_entry->end);
  11368. }
  11369. }
  11370. }
  11371. static void
  11372. verify_connector_state(struct drm_device *dev, struct drm_crtc *crtc)
  11373. {
  11374. struct drm_connector *connector;
  11375. drm_for_each_connector(connector, dev) {
  11376. struct drm_encoder *encoder = connector->encoder;
  11377. struct drm_connector_state *state = connector->state;
  11378. if (state->crtc != crtc)
  11379. continue;
  11380. intel_connector_verify_state(to_intel_connector(connector));
  11381. I915_STATE_WARN(state->best_encoder != encoder,
  11382. "connector's atomic encoder doesn't match legacy encoder\n");
  11383. }
  11384. }
  11385. static void
  11386. verify_encoder_state(struct drm_device *dev)
  11387. {
  11388. struct intel_encoder *encoder;
  11389. struct intel_connector *connector;
  11390. for_each_intel_encoder(dev, encoder) {
  11391. bool enabled = false;
  11392. enum pipe pipe;
  11393. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  11394. encoder->base.base.id,
  11395. encoder->base.name);
  11396. for_each_intel_connector(dev, connector) {
  11397. if (connector->base.state->best_encoder != &encoder->base)
  11398. continue;
  11399. enabled = true;
  11400. I915_STATE_WARN(connector->base.state->crtc !=
  11401. encoder->base.crtc,
  11402. "connector's crtc doesn't match encoder crtc\n");
  11403. }
  11404. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  11405. "encoder's enabled state mismatch "
  11406. "(expected %i, found %i)\n",
  11407. !!encoder->base.crtc, enabled);
  11408. if (!encoder->base.crtc) {
  11409. bool active;
  11410. active = encoder->get_hw_state(encoder, &pipe);
  11411. I915_STATE_WARN(active,
  11412. "encoder detached but still enabled on pipe %c.\n",
  11413. pipe_name(pipe));
  11414. }
  11415. }
  11416. }
  11417. static void
  11418. verify_crtc_state(struct drm_crtc *crtc,
  11419. struct drm_crtc_state *old_crtc_state,
  11420. struct drm_crtc_state *new_crtc_state)
  11421. {
  11422. struct drm_device *dev = crtc->dev;
  11423. struct drm_i915_private *dev_priv = to_i915(dev);
  11424. struct intel_encoder *encoder;
  11425. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11426. struct intel_crtc_state *pipe_config, *sw_config;
  11427. struct drm_atomic_state *old_state;
  11428. bool active;
  11429. old_state = old_crtc_state->state;
  11430. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  11431. pipe_config = to_intel_crtc_state(old_crtc_state);
  11432. memset(pipe_config, 0, sizeof(*pipe_config));
  11433. pipe_config->base.crtc = crtc;
  11434. pipe_config->base.state = old_state;
  11435. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  11436. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  11437. /* hw state is inconsistent with the pipe quirk */
  11438. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  11439. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  11440. active = new_crtc_state->active;
  11441. I915_STATE_WARN(new_crtc_state->active != active,
  11442. "crtc active state doesn't match with hw state "
  11443. "(expected %i, found %i)\n", new_crtc_state->active, active);
  11444. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  11445. "transitional active state does not match atomic hw state "
  11446. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  11447. for_each_encoder_on_crtc(dev, crtc, encoder) {
  11448. enum pipe pipe;
  11449. active = encoder->get_hw_state(encoder, &pipe);
  11450. I915_STATE_WARN(active != new_crtc_state->active,
  11451. "[ENCODER:%i] active %i with crtc active %i\n",
  11452. encoder->base.base.id, active, new_crtc_state->active);
  11453. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  11454. "Encoder connected to wrong pipe %c\n",
  11455. pipe_name(pipe));
  11456. if (active) {
  11457. pipe_config->output_types |= 1 << encoder->type;
  11458. encoder->get_config(encoder, pipe_config);
  11459. }
  11460. }
  11461. if (!new_crtc_state->active)
  11462. return;
  11463. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  11464. sw_config = to_intel_crtc_state(crtc->state);
  11465. if (!intel_pipe_config_compare(dev, sw_config,
  11466. pipe_config, false)) {
  11467. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  11468. intel_dump_pipe_config(intel_crtc, pipe_config,
  11469. "[hw state]");
  11470. intel_dump_pipe_config(intel_crtc, sw_config,
  11471. "[sw state]");
  11472. }
  11473. }
  11474. static void
  11475. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  11476. struct intel_shared_dpll *pll,
  11477. struct drm_crtc *crtc,
  11478. struct drm_crtc_state *new_state)
  11479. {
  11480. struct intel_dpll_hw_state dpll_hw_state;
  11481. unsigned crtc_mask;
  11482. bool active;
  11483. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  11484. DRM_DEBUG_KMS("%s\n", pll->name);
  11485. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  11486. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  11487. I915_STATE_WARN(!pll->on && pll->active_mask,
  11488. "pll in active use but not on in sw tracking\n");
  11489. I915_STATE_WARN(pll->on && !pll->active_mask,
  11490. "pll is on but not used by any active crtc\n");
  11491. I915_STATE_WARN(pll->on != active,
  11492. "pll on state mismatch (expected %i, found %i)\n",
  11493. pll->on, active);
  11494. }
  11495. if (!crtc) {
  11496. I915_STATE_WARN(pll->active_mask & ~pll->config.crtc_mask,
  11497. "more active pll users than references: %x vs %x\n",
  11498. pll->active_mask, pll->config.crtc_mask);
  11499. return;
  11500. }
  11501. crtc_mask = 1 << drm_crtc_index(crtc);
  11502. if (new_state->active)
  11503. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  11504. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  11505. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  11506. else
  11507. I915_STATE_WARN(pll->active_mask & crtc_mask,
  11508. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  11509. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  11510. I915_STATE_WARN(!(pll->config.crtc_mask & crtc_mask),
  11511. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  11512. crtc_mask, pll->config.crtc_mask);
  11513. I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state,
  11514. &dpll_hw_state,
  11515. sizeof(dpll_hw_state)),
  11516. "pll hw state mismatch\n");
  11517. }
  11518. static void
  11519. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  11520. struct drm_crtc_state *old_crtc_state,
  11521. struct drm_crtc_state *new_crtc_state)
  11522. {
  11523. struct drm_i915_private *dev_priv = to_i915(dev);
  11524. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  11525. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  11526. if (new_state->shared_dpll)
  11527. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  11528. if (old_state->shared_dpll &&
  11529. old_state->shared_dpll != new_state->shared_dpll) {
  11530. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  11531. struct intel_shared_dpll *pll = old_state->shared_dpll;
  11532. I915_STATE_WARN(pll->active_mask & crtc_mask,
  11533. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  11534. pipe_name(drm_crtc_index(crtc)));
  11535. I915_STATE_WARN(pll->config.crtc_mask & crtc_mask,
  11536. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  11537. pipe_name(drm_crtc_index(crtc)));
  11538. }
  11539. }
  11540. static void
  11541. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  11542. struct drm_crtc_state *old_state,
  11543. struct drm_crtc_state *new_state)
  11544. {
  11545. if (!needs_modeset(new_state) &&
  11546. !to_intel_crtc_state(new_state)->update_pipe)
  11547. return;
  11548. verify_wm_state(crtc, new_state);
  11549. verify_connector_state(crtc->dev, crtc);
  11550. verify_crtc_state(crtc, old_state, new_state);
  11551. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  11552. }
  11553. static void
  11554. verify_disabled_dpll_state(struct drm_device *dev)
  11555. {
  11556. struct drm_i915_private *dev_priv = to_i915(dev);
  11557. int i;
  11558. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  11559. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  11560. }
  11561. static void
  11562. intel_modeset_verify_disabled(struct drm_device *dev)
  11563. {
  11564. verify_encoder_state(dev);
  11565. verify_connector_state(dev, NULL);
  11566. verify_disabled_dpll_state(dev);
  11567. }
  11568. static void update_scanline_offset(struct intel_crtc *crtc)
  11569. {
  11570. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  11571. /*
  11572. * The scanline counter increments at the leading edge of hsync.
  11573. *
  11574. * On most platforms it starts counting from vtotal-1 on the
  11575. * first active line. That means the scanline counter value is
  11576. * always one less than what we would expect. Ie. just after
  11577. * start of vblank, which also occurs at start of hsync (on the
  11578. * last active line), the scanline counter will read vblank_start-1.
  11579. *
  11580. * On gen2 the scanline counter starts counting from 1 instead
  11581. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  11582. * to keep the value positive), instead of adding one.
  11583. *
  11584. * On HSW+ the behaviour of the scanline counter depends on the output
  11585. * type. For DP ports it behaves like most other platforms, but on HDMI
  11586. * there's an extra 1 line difference. So we need to add two instead of
  11587. * one to the value.
  11588. */
  11589. if (IS_GEN2(dev_priv)) {
  11590. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  11591. int vtotal;
  11592. vtotal = adjusted_mode->crtc_vtotal;
  11593. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  11594. vtotal /= 2;
  11595. crtc->scanline_offset = vtotal - 1;
  11596. } else if (HAS_DDI(dev_priv) &&
  11597. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
  11598. crtc->scanline_offset = 2;
  11599. } else
  11600. crtc->scanline_offset = 1;
  11601. }
  11602. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  11603. {
  11604. struct drm_device *dev = state->dev;
  11605. struct drm_i915_private *dev_priv = to_i915(dev);
  11606. struct intel_shared_dpll_config *shared_dpll = NULL;
  11607. struct drm_crtc *crtc;
  11608. struct drm_crtc_state *crtc_state;
  11609. int i;
  11610. if (!dev_priv->display.crtc_compute_clock)
  11611. return;
  11612. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11613. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11614. struct intel_shared_dpll *old_dpll =
  11615. to_intel_crtc_state(crtc->state)->shared_dpll;
  11616. if (!needs_modeset(crtc_state))
  11617. continue;
  11618. to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
  11619. if (!old_dpll)
  11620. continue;
  11621. if (!shared_dpll)
  11622. shared_dpll = intel_atomic_get_shared_dpll_state(state);
  11623. intel_shared_dpll_config_put(shared_dpll, old_dpll, intel_crtc);
  11624. }
  11625. }
  11626. /*
  11627. * This implements the workaround described in the "notes" section of the mode
  11628. * set sequence documentation. When going from no pipes or single pipe to
  11629. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  11630. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  11631. */
  11632. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  11633. {
  11634. struct drm_crtc_state *crtc_state;
  11635. struct intel_crtc *intel_crtc;
  11636. struct drm_crtc *crtc;
  11637. struct intel_crtc_state *first_crtc_state = NULL;
  11638. struct intel_crtc_state *other_crtc_state = NULL;
  11639. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  11640. int i;
  11641. /* look at all crtc's that are going to be enabled in during modeset */
  11642. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11643. intel_crtc = to_intel_crtc(crtc);
  11644. if (!crtc_state->active || !needs_modeset(crtc_state))
  11645. continue;
  11646. if (first_crtc_state) {
  11647. other_crtc_state = to_intel_crtc_state(crtc_state);
  11648. break;
  11649. } else {
  11650. first_crtc_state = to_intel_crtc_state(crtc_state);
  11651. first_pipe = intel_crtc->pipe;
  11652. }
  11653. }
  11654. /* No workaround needed? */
  11655. if (!first_crtc_state)
  11656. return 0;
  11657. /* w/a possibly needed, check how many crtc's are already enabled. */
  11658. for_each_intel_crtc(state->dev, intel_crtc) {
  11659. struct intel_crtc_state *pipe_config;
  11660. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  11661. if (IS_ERR(pipe_config))
  11662. return PTR_ERR(pipe_config);
  11663. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  11664. if (!pipe_config->base.active ||
  11665. needs_modeset(&pipe_config->base))
  11666. continue;
  11667. /* 2 or more enabled crtcs means no need for w/a */
  11668. if (enabled_pipe != INVALID_PIPE)
  11669. return 0;
  11670. enabled_pipe = intel_crtc->pipe;
  11671. }
  11672. if (enabled_pipe != INVALID_PIPE)
  11673. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  11674. else if (other_crtc_state)
  11675. other_crtc_state->hsw_workaround_pipe = first_pipe;
  11676. return 0;
  11677. }
  11678. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  11679. {
  11680. struct drm_crtc *crtc;
  11681. struct drm_crtc_state *crtc_state;
  11682. int ret = 0;
  11683. /* add all active pipes to the state */
  11684. for_each_crtc(state->dev, crtc) {
  11685. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11686. if (IS_ERR(crtc_state))
  11687. return PTR_ERR(crtc_state);
  11688. if (!crtc_state->active || needs_modeset(crtc_state))
  11689. continue;
  11690. crtc_state->mode_changed = true;
  11691. ret = drm_atomic_add_affected_connectors(state, crtc);
  11692. if (ret)
  11693. break;
  11694. ret = drm_atomic_add_affected_planes(state, crtc);
  11695. if (ret)
  11696. break;
  11697. }
  11698. return ret;
  11699. }
  11700. static int intel_modeset_checks(struct drm_atomic_state *state)
  11701. {
  11702. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11703. struct drm_i915_private *dev_priv = to_i915(state->dev);
  11704. struct drm_crtc *crtc;
  11705. struct drm_crtc_state *crtc_state;
  11706. int ret = 0, i;
  11707. if (!check_digital_port_conflicts(state)) {
  11708. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  11709. return -EINVAL;
  11710. }
  11711. intel_state->modeset = true;
  11712. intel_state->active_crtcs = dev_priv->active_crtcs;
  11713. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11714. if (crtc_state->active)
  11715. intel_state->active_crtcs |= 1 << i;
  11716. else
  11717. intel_state->active_crtcs &= ~(1 << i);
  11718. if (crtc_state->active != crtc->state->active)
  11719. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  11720. }
  11721. /*
  11722. * See if the config requires any additional preparation, e.g.
  11723. * to adjust global state with pipes off. We need to do this
  11724. * here so we can get the modeset_pipe updated config for the new
  11725. * mode set on this crtc. For other crtcs we need to use the
  11726. * adjusted_mode bits in the crtc directly.
  11727. */
  11728. if (dev_priv->display.modeset_calc_cdclk) {
  11729. if (!intel_state->cdclk_pll_vco)
  11730. intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
  11731. if (!intel_state->cdclk_pll_vco)
  11732. intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
  11733. ret = dev_priv->display.modeset_calc_cdclk(state);
  11734. if (ret < 0)
  11735. return ret;
  11736. if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
  11737. intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco)
  11738. ret = intel_modeset_all_pipes(state);
  11739. if (ret < 0)
  11740. return ret;
  11741. DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
  11742. intel_state->cdclk, intel_state->dev_cdclk);
  11743. } else
  11744. to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
  11745. intel_modeset_clear_plls(state);
  11746. if (IS_HASWELL(dev_priv))
  11747. return haswell_mode_set_planes_workaround(state);
  11748. return 0;
  11749. }
  11750. /*
  11751. * Handle calculation of various watermark data at the end of the atomic check
  11752. * phase. The code here should be run after the per-crtc and per-plane 'check'
  11753. * handlers to ensure that all derived state has been updated.
  11754. */
  11755. static int calc_watermark_data(struct drm_atomic_state *state)
  11756. {
  11757. struct drm_device *dev = state->dev;
  11758. struct drm_i915_private *dev_priv = to_i915(dev);
  11759. /* Is there platform-specific watermark information to calculate? */
  11760. if (dev_priv->display.compute_global_watermarks)
  11761. return dev_priv->display.compute_global_watermarks(state);
  11762. return 0;
  11763. }
  11764. /**
  11765. * intel_atomic_check - validate state object
  11766. * @dev: drm device
  11767. * @state: state to validate
  11768. */
  11769. static int intel_atomic_check(struct drm_device *dev,
  11770. struct drm_atomic_state *state)
  11771. {
  11772. struct drm_i915_private *dev_priv = to_i915(dev);
  11773. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11774. struct drm_crtc *crtc;
  11775. struct drm_crtc_state *crtc_state;
  11776. int ret, i;
  11777. bool any_ms = false;
  11778. ret = drm_atomic_helper_check_modeset(dev, state);
  11779. if (ret)
  11780. return ret;
  11781. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11782. struct intel_crtc_state *pipe_config =
  11783. to_intel_crtc_state(crtc_state);
  11784. /* Catch I915_MODE_FLAG_INHERITED */
  11785. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  11786. crtc_state->mode_changed = true;
  11787. if (!needs_modeset(crtc_state))
  11788. continue;
  11789. if (!crtc_state->enable) {
  11790. any_ms = true;
  11791. continue;
  11792. }
  11793. /* FIXME: For only active_changed we shouldn't need to do any
  11794. * state recomputation at all. */
  11795. ret = drm_atomic_add_affected_connectors(state, crtc);
  11796. if (ret)
  11797. return ret;
  11798. ret = intel_modeset_pipe_config(crtc, pipe_config);
  11799. if (ret) {
  11800. intel_dump_pipe_config(to_intel_crtc(crtc),
  11801. pipe_config, "[failed]");
  11802. return ret;
  11803. }
  11804. if (i915.fastboot &&
  11805. intel_pipe_config_compare(dev,
  11806. to_intel_crtc_state(crtc->state),
  11807. pipe_config, true)) {
  11808. crtc_state->mode_changed = false;
  11809. to_intel_crtc_state(crtc_state)->update_pipe = true;
  11810. }
  11811. if (needs_modeset(crtc_state))
  11812. any_ms = true;
  11813. ret = drm_atomic_add_affected_planes(state, crtc);
  11814. if (ret)
  11815. return ret;
  11816. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  11817. needs_modeset(crtc_state) ?
  11818. "[modeset]" : "[fastset]");
  11819. }
  11820. if (any_ms) {
  11821. ret = intel_modeset_checks(state);
  11822. if (ret)
  11823. return ret;
  11824. } else
  11825. intel_state->cdclk = dev_priv->cdclk_freq;
  11826. ret = drm_atomic_helper_check_planes(dev, state);
  11827. if (ret)
  11828. return ret;
  11829. intel_fbc_choose_crtc(dev_priv, state);
  11830. return calc_watermark_data(state);
  11831. }
  11832. static int intel_atomic_prepare_commit(struct drm_device *dev,
  11833. struct drm_atomic_state *state)
  11834. {
  11835. struct drm_i915_private *dev_priv = to_i915(dev);
  11836. struct drm_crtc_state *crtc_state;
  11837. struct drm_crtc *crtc;
  11838. int i, ret;
  11839. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11840. if (state->legacy_cursor_update)
  11841. continue;
  11842. ret = intel_crtc_wait_for_pending_flips(crtc);
  11843. if (ret)
  11844. return ret;
  11845. if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
  11846. flush_workqueue(dev_priv->wq);
  11847. }
  11848. ret = mutex_lock_interruptible(&dev->struct_mutex);
  11849. if (ret)
  11850. return ret;
  11851. ret = drm_atomic_helper_prepare_planes(dev, state);
  11852. mutex_unlock(&dev->struct_mutex);
  11853. return ret;
  11854. }
  11855. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  11856. {
  11857. struct drm_device *dev = crtc->base.dev;
  11858. if (!dev->max_vblank_count)
  11859. return drm_accurate_vblank_count(&crtc->base);
  11860. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  11861. }
  11862. static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
  11863. struct drm_i915_private *dev_priv,
  11864. unsigned crtc_mask)
  11865. {
  11866. unsigned last_vblank_count[I915_MAX_PIPES];
  11867. enum pipe pipe;
  11868. int ret;
  11869. if (!crtc_mask)
  11870. return;
  11871. for_each_pipe(dev_priv, pipe) {
  11872. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  11873. pipe);
  11874. if (!((1 << pipe) & crtc_mask))
  11875. continue;
  11876. ret = drm_crtc_vblank_get(&crtc->base);
  11877. if (WARN_ON(ret != 0)) {
  11878. crtc_mask &= ~(1 << pipe);
  11879. continue;
  11880. }
  11881. last_vblank_count[pipe] = drm_crtc_vblank_count(&crtc->base);
  11882. }
  11883. for_each_pipe(dev_priv, pipe) {
  11884. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  11885. pipe);
  11886. long lret;
  11887. if (!((1 << pipe) & crtc_mask))
  11888. continue;
  11889. lret = wait_event_timeout(dev->vblank[pipe].queue,
  11890. last_vblank_count[pipe] !=
  11891. drm_crtc_vblank_count(&crtc->base),
  11892. msecs_to_jiffies(50));
  11893. WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
  11894. drm_crtc_vblank_put(&crtc->base);
  11895. }
  11896. }
  11897. static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
  11898. {
  11899. /* fb updated, need to unpin old fb */
  11900. if (crtc_state->fb_changed)
  11901. return true;
  11902. /* wm changes, need vblank before final wm's */
  11903. if (crtc_state->update_wm_post)
  11904. return true;
  11905. /*
  11906. * cxsr is re-enabled after vblank.
  11907. * This is already handled by crtc_state->update_wm_post,
  11908. * but added for clarity.
  11909. */
  11910. if (crtc_state->disable_cxsr)
  11911. return true;
  11912. return false;
  11913. }
  11914. static void intel_update_crtc(struct drm_crtc *crtc,
  11915. struct drm_atomic_state *state,
  11916. struct drm_crtc_state *old_crtc_state,
  11917. unsigned int *crtc_vblank_mask)
  11918. {
  11919. struct drm_device *dev = crtc->dev;
  11920. struct drm_i915_private *dev_priv = to_i915(dev);
  11921. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11922. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc->state);
  11923. bool modeset = needs_modeset(crtc->state);
  11924. if (modeset) {
  11925. update_scanline_offset(intel_crtc);
  11926. dev_priv->display.crtc_enable(pipe_config, state);
  11927. } else {
  11928. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  11929. }
  11930. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  11931. intel_fbc_enable(
  11932. intel_crtc, pipe_config,
  11933. to_intel_plane_state(crtc->primary->state));
  11934. }
  11935. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  11936. if (needs_vblank_wait(pipe_config))
  11937. *crtc_vblank_mask |= drm_crtc_mask(crtc);
  11938. }
  11939. static void intel_update_crtcs(struct drm_atomic_state *state,
  11940. unsigned int *crtc_vblank_mask)
  11941. {
  11942. struct drm_crtc *crtc;
  11943. struct drm_crtc_state *old_crtc_state;
  11944. int i;
  11945. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11946. if (!crtc->state->active)
  11947. continue;
  11948. intel_update_crtc(crtc, state, old_crtc_state,
  11949. crtc_vblank_mask);
  11950. }
  11951. }
  11952. static void skl_update_crtcs(struct drm_atomic_state *state,
  11953. unsigned int *crtc_vblank_mask)
  11954. {
  11955. struct drm_i915_private *dev_priv = to_i915(state->dev);
  11956. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11957. struct drm_crtc *crtc;
  11958. struct intel_crtc *intel_crtc;
  11959. struct drm_crtc_state *old_crtc_state;
  11960. struct intel_crtc_state *cstate;
  11961. unsigned int updated = 0;
  11962. bool progress;
  11963. enum pipe pipe;
  11964. /*
  11965. * Whenever the number of active pipes changes, we need to make sure we
  11966. * update the pipes in the right order so that their ddb allocations
  11967. * never overlap with eachother inbetween CRTC updates. Otherwise we'll
  11968. * cause pipe underruns and other bad stuff.
  11969. */
  11970. do {
  11971. int i;
  11972. progress = false;
  11973. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11974. bool vbl_wait = false;
  11975. unsigned int cmask = drm_crtc_mask(crtc);
  11976. intel_crtc = to_intel_crtc(crtc);
  11977. cstate = to_intel_crtc_state(crtc->state);
  11978. pipe = intel_crtc->pipe;
  11979. if (updated & cmask || !crtc->state->active)
  11980. continue;
  11981. if (skl_ddb_allocation_overlaps(state, intel_crtc))
  11982. continue;
  11983. updated |= cmask;
  11984. /*
  11985. * If this is an already active pipe, it's DDB changed,
  11986. * and this isn't the last pipe that needs updating
  11987. * then we need to wait for a vblank to pass for the
  11988. * new ddb allocation to take effect.
  11989. */
  11990. if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
  11991. &intel_crtc->hw_ddb) &&
  11992. !crtc->state->active_changed &&
  11993. intel_state->wm_results.dirty_pipes != updated)
  11994. vbl_wait = true;
  11995. intel_update_crtc(crtc, state, old_crtc_state,
  11996. crtc_vblank_mask);
  11997. if (vbl_wait)
  11998. intel_wait_for_vblank(dev_priv, pipe);
  11999. progress = true;
  12000. }
  12001. } while (progress);
  12002. }
  12003. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  12004. {
  12005. struct drm_device *dev = state->dev;
  12006. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  12007. struct drm_i915_private *dev_priv = to_i915(dev);
  12008. struct drm_crtc_state *old_crtc_state;
  12009. struct drm_crtc *crtc;
  12010. struct intel_crtc_state *intel_cstate;
  12011. bool hw_check = intel_state->modeset;
  12012. unsigned long put_domains[I915_MAX_PIPES] = {};
  12013. unsigned crtc_vblank_mask = 0;
  12014. int i;
  12015. drm_atomic_helper_wait_for_dependencies(state);
  12016. if (intel_state->modeset) {
  12017. memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
  12018. sizeof(intel_state->min_pixclk));
  12019. dev_priv->active_crtcs = intel_state->active_crtcs;
  12020. dev_priv->atomic_cdclk_freq = intel_state->cdclk;
  12021. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  12022. }
  12023. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12024. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12025. if (needs_modeset(crtc->state) ||
  12026. to_intel_crtc_state(crtc->state)->update_pipe) {
  12027. hw_check = true;
  12028. put_domains[to_intel_crtc(crtc)->pipe] =
  12029. modeset_get_crtc_power_domains(crtc,
  12030. to_intel_crtc_state(crtc->state));
  12031. }
  12032. if (!needs_modeset(crtc->state))
  12033. continue;
  12034. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  12035. if (old_crtc_state->active) {
  12036. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  12037. dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
  12038. intel_crtc->active = false;
  12039. intel_fbc_disable(intel_crtc);
  12040. intel_disable_shared_dpll(intel_crtc);
  12041. /*
  12042. * Underruns don't always raise
  12043. * interrupts, so check manually.
  12044. */
  12045. intel_check_cpu_fifo_underruns(dev_priv);
  12046. intel_check_pch_fifo_underruns(dev_priv);
  12047. if (!crtc->state->active)
  12048. intel_update_watermarks(intel_crtc);
  12049. }
  12050. }
  12051. /* Only after disabling all output pipelines that will be changed can we
  12052. * update the the output configuration. */
  12053. intel_modeset_update_crtc_state(state);
  12054. if (intel_state->modeset) {
  12055. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  12056. if (dev_priv->display.modeset_commit_cdclk &&
  12057. (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
  12058. intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
  12059. dev_priv->display.modeset_commit_cdclk(state);
  12060. /*
  12061. * SKL workaround: bspec recommends we disable the SAGV when we
  12062. * have more then one pipe enabled
  12063. */
  12064. if (!intel_can_enable_sagv(state))
  12065. intel_disable_sagv(dev_priv);
  12066. intel_modeset_verify_disabled(dev);
  12067. }
  12068. /* Complete the events for pipes that have now been disabled */
  12069. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12070. bool modeset = needs_modeset(crtc->state);
  12071. /* Complete events for now disable pipes here. */
  12072. if (modeset && !crtc->state->active && crtc->state->event) {
  12073. spin_lock_irq(&dev->event_lock);
  12074. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  12075. spin_unlock_irq(&dev->event_lock);
  12076. crtc->state->event = NULL;
  12077. }
  12078. }
  12079. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  12080. dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
  12081. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  12082. * already, but still need the state for the delayed optimization. To
  12083. * fix this:
  12084. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  12085. * - schedule that vblank worker _before_ calling hw_done
  12086. * - at the start of commit_tail, cancel it _synchrously
  12087. * - switch over to the vblank wait helper in the core after that since
  12088. * we don't need out special handling any more.
  12089. */
  12090. if (!state->legacy_cursor_update)
  12091. intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
  12092. /*
  12093. * Now that the vblank has passed, we can go ahead and program the
  12094. * optimal watermarks on platforms that need two-step watermark
  12095. * programming.
  12096. *
  12097. * TODO: Move this (and other cleanup) to an async worker eventually.
  12098. */
  12099. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12100. intel_cstate = to_intel_crtc_state(crtc->state);
  12101. if (dev_priv->display.optimize_watermarks)
  12102. dev_priv->display.optimize_watermarks(intel_cstate);
  12103. }
  12104. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12105. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  12106. if (put_domains[i])
  12107. modeset_put_power_domains(dev_priv, put_domains[i]);
  12108. intel_modeset_verify_crtc(crtc, old_crtc_state, crtc->state);
  12109. }
  12110. if (intel_state->modeset && intel_can_enable_sagv(state))
  12111. intel_enable_sagv(dev_priv);
  12112. drm_atomic_helper_commit_hw_done(state);
  12113. if (intel_state->modeset)
  12114. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  12115. mutex_lock(&dev->struct_mutex);
  12116. drm_atomic_helper_cleanup_planes(dev, state);
  12117. mutex_unlock(&dev->struct_mutex);
  12118. drm_atomic_helper_commit_cleanup_done(state);
  12119. drm_atomic_state_put(state);
  12120. /* As one of the primary mmio accessors, KMS has a high likelihood
  12121. * of triggering bugs in unclaimed access. After we finish
  12122. * modesetting, see if an error has been flagged, and if so
  12123. * enable debugging for the next modeset - and hope we catch
  12124. * the culprit.
  12125. *
  12126. * XXX note that we assume display power is on at this point.
  12127. * This might hold true now but we need to add pm helper to check
  12128. * unclaimed only when the hardware is on, as atomic commits
  12129. * can happen also when the device is completely off.
  12130. */
  12131. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  12132. }
  12133. static void intel_atomic_commit_work(struct work_struct *work)
  12134. {
  12135. struct drm_atomic_state *state =
  12136. container_of(work, struct drm_atomic_state, commit_work);
  12137. intel_atomic_commit_tail(state);
  12138. }
  12139. static int __i915_sw_fence_call
  12140. intel_atomic_commit_ready(struct i915_sw_fence *fence,
  12141. enum i915_sw_fence_notify notify)
  12142. {
  12143. struct intel_atomic_state *state =
  12144. container_of(fence, struct intel_atomic_state, commit_ready);
  12145. switch (notify) {
  12146. case FENCE_COMPLETE:
  12147. if (state->base.commit_work.func)
  12148. queue_work(system_unbound_wq, &state->base.commit_work);
  12149. break;
  12150. case FENCE_FREE:
  12151. drm_atomic_state_put(&state->base);
  12152. break;
  12153. }
  12154. return NOTIFY_DONE;
  12155. }
  12156. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  12157. {
  12158. struct drm_plane_state *old_plane_state;
  12159. struct drm_plane *plane;
  12160. int i;
  12161. for_each_plane_in_state(state, plane, old_plane_state, i)
  12162. i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
  12163. intel_fb_obj(plane->state->fb),
  12164. to_intel_plane(plane)->frontbuffer_bit);
  12165. }
  12166. /**
  12167. * intel_atomic_commit - commit validated state object
  12168. * @dev: DRM device
  12169. * @state: the top-level driver state object
  12170. * @nonblock: nonblocking commit
  12171. *
  12172. * This function commits a top-level state object that has been validated
  12173. * with drm_atomic_helper_check().
  12174. *
  12175. * FIXME: Atomic modeset support for i915 is not yet complete. At the moment
  12176. * nonblocking commits are only safe for pure plane updates. Everything else
  12177. * should work though.
  12178. *
  12179. * RETURNS
  12180. * Zero for success or -errno.
  12181. */
  12182. static int intel_atomic_commit(struct drm_device *dev,
  12183. struct drm_atomic_state *state,
  12184. bool nonblock)
  12185. {
  12186. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  12187. struct drm_i915_private *dev_priv = to_i915(dev);
  12188. int ret = 0;
  12189. if (intel_state->modeset && nonblock) {
  12190. DRM_DEBUG_KMS("nonblocking commit for modeset not yet implemented.\n");
  12191. return -EINVAL;
  12192. }
  12193. ret = drm_atomic_helper_setup_commit(state, nonblock);
  12194. if (ret)
  12195. return ret;
  12196. drm_atomic_state_get(state);
  12197. i915_sw_fence_init(&intel_state->commit_ready,
  12198. intel_atomic_commit_ready);
  12199. ret = intel_atomic_prepare_commit(dev, state);
  12200. if (ret) {
  12201. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  12202. i915_sw_fence_commit(&intel_state->commit_ready);
  12203. return ret;
  12204. }
  12205. drm_atomic_helper_swap_state(state, true);
  12206. dev_priv->wm.distrust_bios_wm = false;
  12207. dev_priv->wm.skl_results = intel_state->wm_results;
  12208. intel_shared_dpll_commit(state);
  12209. intel_atomic_track_fbs(state);
  12210. drm_atomic_state_get(state);
  12211. INIT_WORK(&state->commit_work,
  12212. nonblock ? intel_atomic_commit_work : NULL);
  12213. i915_sw_fence_commit(&intel_state->commit_ready);
  12214. if (!nonblock) {
  12215. i915_sw_fence_wait(&intel_state->commit_ready);
  12216. intel_atomic_commit_tail(state);
  12217. }
  12218. return 0;
  12219. }
  12220. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  12221. {
  12222. struct drm_device *dev = crtc->dev;
  12223. struct drm_atomic_state *state;
  12224. struct drm_crtc_state *crtc_state;
  12225. int ret;
  12226. state = drm_atomic_state_alloc(dev);
  12227. if (!state) {
  12228. DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
  12229. crtc->base.id, crtc->name);
  12230. return;
  12231. }
  12232. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  12233. retry:
  12234. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  12235. ret = PTR_ERR_OR_ZERO(crtc_state);
  12236. if (!ret) {
  12237. if (!crtc_state->active)
  12238. goto out;
  12239. crtc_state->mode_changed = true;
  12240. ret = drm_atomic_commit(state);
  12241. }
  12242. if (ret == -EDEADLK) {
  12243. drm_atomic_state_clear(state);
  12244. drm_modeset_backoff(state->acquire_ctx);
  12245. goto retry;
  12246. }
  12247. out:
  12248. drm_atomic_state_put(state);
  12249. }
  12250. /*
  12251. * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
  12252. * drm_atomic_helper_legacy_gamma_set() directly.
  12253. */
  12254. static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
  12255. u16 *red, u16 *green, u16 *blue,
  12256. uint32_t size)
  12257. {
  12258. struct drm_device *dev = crtc->dev;
  12259. struct drm_mode_config *config = &dev->mode_config;
  12260. struct drm_crtc_state *state;
  12261. int ret;
  12262. ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
  12263. if (ret)
  12264. return ret;
  12265. /*
  12266. * Make sure we update the legacy properties so this works when
  12267. * atomic is not enabled.
  12268. */
  12269. state = crtc->state;
  12270. drm_object_property_set_value(&crtc->base,
  12271. config->degamma_lut_property,
  12272. (state->degamma_lut) ?
  12273. state->degamma_lut->base.id : 0);
  12274. drm_object_property_set_value(&crtc->base,
  12275. config->ctm_property,
  12276. (state->ctm) ?
  12277. state->ctm->base.id : 0);
  12278. drm_object_property_set_value(&crtc->base,
  12279. config->gamma_lut_property,
  12280. (state->gamma_lut) ?
  12281. state->gamma_lut->base.id : 0);
  12282. return 0;
  12283. }
  12284. static const struct drm_crtc_funcs intel_crtc_funcs = {
  12285. .gamma_set = intel_atomic_legacy_gamma_set,
  12286. .set_config = drm_atomic_helper_set_config,
  12287. .set_property = drm_atomic_helper_crtc_set_property,
  12288. .destroy = intel_crtc_destroy,
  12289. .page_flip = intel_crtc_page_flip,
  12290. .atomic_duplicate_state = intel_crtc_duplicate_state,
  12291. .atomic_destroy_state = intel_crtc_destroy_state,
  12292. };
  12293. /**
  12294. * intel_prepare_plane_fb - Prepare fb for usage on plane
  12295. * @plane: drm plane to prepare for
  12296. * @fb: framebuffer to prepare for presentation
  12297. *
  12298. * Prepares a framebuffer for usage on a display plane. Generally this
  12299. * involves pinning the underlying object and updating the frontbuffer tracking
  12300. * bits. Some older platforms need special physical address handling for
  12301. * cursor planes.
  12302. *
  12303. * Must be called with struct_mutex held.
  12304. *
  12305. * Returns 0 on success, negative error code on failure.
  12306. */
  12307. int
  12308. intel_prepare_plane_fb(struct drm_plane *plane,
  12309. struct drm_plane_state *new_state)
  12310. {
  12311. struct intel_atomic_state *intel_state =
  12312. to_intel_atomic_state(new_state->state);
  12313. struct drm_device *dev = plane->dev;
  12314. struct drm_i915_private *dev_priv = to_i915(dev);
  12315. struct drm_framebuffer *fb = new_state->fb;
  12316. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  12317. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  12318. int ret;
  12319. if (!obj && !old_obj)
  12320. return 0;
  12321. if (old_obj) {
  12322. struct drm_crtc_state *crtc_state =
  12323. drm_atomic_get_existing_crtc_state(new_state->state,
  12324. plane->state->crtc);
  12325. /* Big Hammer, we also need to ensure that any pending
  12326. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  12327. * current scanout is retired before unpinning the old
  12328. * framebuffer. Note that we rely on userspace rendering
  12329. * into the buffer attached to the pipe they are waiting
  12330. * on. If not, userspace generates a GPU hang with IPEHR
  12331. * point to the MI_WAIT_FOR_EVENT.
  12332. *
  12333. * This should only fail upon a hung GPU, in which case we
  12334. * can safely continue.
  12335. */
  12336. if (needs_modeset(crtc_state)) {
  12337. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  12338. old_obj->resv, NULL,
  12339. false, 0,
  12340. GFP_KERNEL);
  12341. if (ret < 0)
  12342. return ret;
  12343. }
  12344. }
  12345. if (new_state->fence) { /* explicit fencing */
  12346. ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
  12347. new_state->fence,
  12348. I915_FENCE_TIMEOUT,
  12349. GFP_KERNEL);
  12350. if (ret < 0)
  12351. return ret;
  12352. }
  12353. if (!obj)
  12354. return 0;
  12355. if (!new_state->fence) { /* implicit fencing */
  12356. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  12357. obj->resv, NULL,
  12358. false, I915_FENCE_TIMEOUT,
  12359. GFP_KERNEL);
  12360. if (ret < 0)
  12361. return ret;
  12362. }
  12363. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  12364. INTEL_INFO(dev)->cursor_needs_physical) {
  12365. int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
  12366. ret = i915_gem_object_attach_phys(obj, align);
  12367. if (ret) {
  12368. DRM_DEBUG_KMS("failed to attach phys object\n");
  12369. return ret;
  12370. }
  12371. } else {
  12372. struct i915_vma *vma;
  12373. vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  12374. if (IS_ERR(vma)) {
  12375. DRM_DEBUG_KMS("failed to pin object\n");
  12376. return PTR_ERR(vma);
  12377. }
  12378. }
  12379. return 0;
  12380. }
  12381. /**
  12382. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  12383. * @plane: drm plane to clean up for
  12384. * @fb: old framebuffer that was on plane
  12385. *
  12386. * Cleans up a framebuffer that has just been removed from a plane.
  12387. *
  12388. * Must be called with struct_mutex held.
  12389. */
  12390. void
  12391. intel_cleanup_plane_fb(struct drm_plane *plane,
  12392. struct drm_plane_state *old_state)
  12393. {
  12394. struct drm_device *dev = plane->dev;
  12395. struct intel_plane_state *old_intel_state;
  12396. struct drm_i915_gem_object *old_obj = intel_fb_obj(old_state->fb);
  12397. struct drm_i915_gem_object *obj = intel_fb_obj(plane->state->fb);
  12398. old_intel_state = to_intel_plane_state(old_state);
  12399. if (!obj && !old_obj)
  12400. return;
  12401. if (old_obj && (plane->type != DRM_PLANE_TYPE_CURSOR ||
  12402. !INTEL_INFO(dev)->cursor_needs_physical))
  12403. intel_unpin_fb_obj(old_state->fb, old_state->rotation);
  12404. }
  12405. int
  12406. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  12407. {
  12408. int max_scale;
  12409. int crtc_clock, cdclk;
  12410. if (!intel_crtc || !crtc_state->base.enable)
  12411. return DRM_PLANE_HELPER_NO_SCALING;
  12412. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  12413. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  12414. if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
  12415. return DRM_PLANE_HELPER_NO_SCALING;
  12416. /*
  12417. * skl max scale is lower of:
  12418. * close to 3 but not 3, -1 is for that purpose
  12419. * or
  12420. * cdclk/crtc_clock
  12421. */
  12422. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  12423. return max_scale;
  12424. }
  12425. static int
  12426. intel_check_primary_plane(struct drm_plane *plane,
  12427. struct intel_crtc_state *crtc_state,
  12428. struct intel_plane_state *state)
  12429. {
  12430. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  12431. struct drm_crtc *crtc = state->base.crtc;
  12432. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  12433. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  12434. bool can_position = false;
  12435. int ret;
  12436. if (INTEL_GEN(dev_priv) >= 9) {
  12437. /* use scaler when colorkey is not required */
  12438. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  12439. min_scale = 1;
  12440. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  12441. }
  12442. can_position = true;
  12443. }
  12444. ret = drm_plane_helper_check_state(&state->base,
  12445. &state->clip,
  12446. min_scale, max_scale,
  12447. can_position, true);
  12448. if (ret)
  12449. return ret;
  12450. if (!state->base.fb)
  12451. return 0;
  12452. if (INTEL_GEN(dev_priv) >= 9) {
  12453. ret = skl_check_plane_surface(state);
  12454. if (ret)
  12455. return ret;
  12456. }
  12457. return 0;
  12458. }
  12459. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  12460. struct drm_crtc_state *old_crtc_state)
  12461. {
  12462. struct drm_device *dev = crtc->dev;
  12463. struct drm_i915_private *dev_priv = to_i915(dev);
  12464. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12465. struct intel_crtc_state *intel_cstate =
  12466. to_intel_crtc_state(crtc->state);
  12467. struct intel_crtc_state *old_intel_state =
  12468. to_intel_crtc_state(old_crtc_state);
  12469. bool modeset = needs_modeset(crtc->state);
  12470. enum pipe pipe = intel_crtc->pipe;
  12471. /* Perform vblank evasion around commit operation */
  12472. intel_pipe_update_start(intel_crtc);
  12473. if (modeset)
  12474. return;
  12475. if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
  12476. intel_color_set_csc(crtc->state);
  12477. intel_color_load_luts(crtc->state);
  12478. }
  12479. if (intel_cstate->update_pipe) {
  12480. intel_update_pipe_config(intel_crtc, old_intel_state);
  12481. } else if (INTEL_GEN(dev_priv) >= 9) {
  12482. skl_detach_scalers(intel_crtc);
  12483. I915_WRITE(PIPE_WM_LINETIME(pipe),
  12484. intel_cstate->wm.skl.optimal.linetime);
  12485. }
  12486. }
  12487. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  12488. struct drm_crtc_state *old_crtc_state)
  12489. {
  12490. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12491. intel_pipe_update_end(intel_crtc, NULL);
  12492. }
  12493. /**
  12494. * intel_plane_destroy - destroy a plane
  12495. * @plane: plane to destroy
  12496. *
  12497. * Common destruction function for all types of planes (primary, cursor,
  12498. * sprite).
  12499. */
  12500. void intel_plane_destroy(struct drm_plane *plane)
  12501. {
  12502. drm_plane_cleanup(plane);
  12503. kfree(to_intel_plane(plane));
  12504. }
  12505. const struct drm_plane_funcs intel_plane_funcs = {
  12506. .update_plane = drm_atomic_helper_update_plane,
  12507. .disable_plane = drm_atomic_helper_disable_plane,
  12508. .destroy = intel_plane_destroy,
  12509. .set_property = drm_atomic_helper_plane_set_property,
  12510. .atomic_get_property = intel_plane_atomic_get_property,
  12511. .atomic_set_property = intel_plane_atomic_set_property,
  12512. .atomic_duplicate_state = intel_plane_duplicate_state,
  12513. .atomic_destroy_state = intel_plane_destroy_state,
  12514. };
  12515. static struct intel_plane *
  12516. intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  12517. {
  12518. struct intel_plane *primary = NULL;
  12519. struct intel_plane_state *state = NULL;
  12520. const uint32_t *intel_primary_formats;
  12521. unsigned int supported_rotations;
  12522. unsigned int num_formats;
  12523. int ret;
  12524. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  12525. if (!primary) {
  12526. ret = -ENOMEM;
  12527. goto fail;
  12528. }
  12529. state = intel_create_plane_state(&primary->base);
  12530. if (!state) {
  12531. ret = -ENOMEM;
  12532. goto fail;
  12533. }
  12534. primary->base.state = &state->base;
  12535. primary->can_scale = false;
  12536. primary->max_downscale = 1;
  12537. if (INTEL_GEN(dev_priv) >= 9) {
  12538. primary->can_scale = true;
  12539. state->scaler_id = -1;
  12540. }
  12541. primary->pipe = pipe;
  12542. primary->plane = pipe;
  12543. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  12544. primary->check_plane = intel_check_primary_plane;
  12545. if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
  12546. primary->plane = !pipe;
  12547. if (INTEL_GEN(dev_priv) >= 9) {
  12548. intel_primary_formats = skl_primary_formats;
  12549. num_formats = ARRAY_SIZE(skl_primary_formats);
  12550. primary->update_plane = skylake_update_primary_plane;
  12551. primary->disable_plane = skylake_disable_primary_plane;
  12552. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12553. intel_primary_formats = i965_primary_formats;
  12554. num_formats = ARRAY_SIZE(i965_primary_formats);
  12555. primary->update_plane = ironlake_update_primary_plane;
  12556. primary->disable_plane = i9xx_disable_primary_plane;
  12557. } else if (INTEL_GEN(dev_priv) >= 4) {
  12558. intel_primary_formats = i965_primary_formats;
  12559. num_formats = ARRAY_SIZE(i965_primary_formats);
  12560. primary->update_plane = i9xx_update_primary_plane;
  12561. primary->disable_plane = i9xx_disable_primary_plane;
  12562. } else {
  12563. intel_primary_formats = i8xx_primary_formats;
  12564. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  12565. primary->update_plane = i9xx_update_primary_plane;
  12566. primary->disable_plane = i9xx_disable_primary_plane;
  12567. }
  12568. if (INTEL_GEN(dev_priv) >= 9)
  12569. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  12570. 0, &intel_plane_funcs,
  12571. intel_primary_formats, num_formats,
  12572. DRM_PLANE_TYPE_PRIMARY,
  12573. "plane 1%c", pipe_name(pipe));
  12574. else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  12575. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  12576. 0, &intel_plane_funcs,
  12577. intel_primary_formats, num_formats,
  12578. DRM_PLANE_TYPE_PRIMARY,
  12579. "primary %c", pipe_name(pipe));
  12580. else
  12581. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  12582. 0, &intel_plane_funcs,
  12583. intel_primary_formats, num_formats,
  12584. DRM_PLANE_TYPE_PRIMARY,
  12585. "plane %c", plane_name(primary->plane));
  12586. if (ret)
  12587. goto fail;
  12588. if (INTEL_GEN(dev_priv) >= 9) {
  12589. supported_rotations =
  12590. DRM_ROTATE_0 | DRM_ROTATE_90 |
  12591. DRM_ROTATE_180 | DRM_ROTATE_270;
  12592. } else if (INTEL_GEN(dev_priv) >= 4) {
  12593. supported_rotations =
  12594. DRM_ROTATE_0 | DRM_ROTATE_180;
  12595. } else {
  12596. supported_rotations = DRM_ROTATE_0;
  12597. }
  12598. if (INTEL_GEN(dev_priv) >= 4)
  12599. drm_plane_create_rotation_property(&primary->base,
  12600. DRM_ROTATE_0,
  12601. supported_rotations);
  12602. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  12603. return primary;
  12604. fail:
  12605. kfree(state);
  12606. kfree(primary);
  12607. return ERR_PTR(ret);
  12608. }
  12609. static int
  12610. intel_check_cursor_plane(struct drm_plane *plane,
  12611. struct intel_crtc_state *crtc_state,
  12612. struct intel_plane_state *state)
  12613. {
  12614. struct drm_framebuffer *fb = state->base.fb;
  12615. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  12616. enum pipe pipe = to_intel_plane(plane)->pipe;
  12617. unsigned stride;
  12618. int ret;
  12619. ret = drm_plane_helper_check_state(&state->base,
  12620. &state->clip,
  12621. DRM_PLANE_HELPER_NO_SCALING,
  12622. DRM_PLANE_HELPER_NO_SCALING,
  12623. true, true);
  12624. if (ret)
  12625. return ret;
  12626. /* if we want to turn off the cursor ignore width and height */
  12627. if (!obj)
  12628. return 0;
  12629. /* Check for which cursor types we support */
  12630. if (!cursor_size_ok(to_i915(plane->dev), state->base.crtc_w,
  12631. state->base.crtc_h)) {
  12632. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  12633. state->base.crtc_w, state->base.crtc_h);
  12634. return -EINVAL;
  12635. }
  12636. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  12637. if (obj->base.size < stride * state->base.crtc_h) {
  12638. DRM_DEBUG_KMS("buffer is too small\n");
  12639. return -ENOMEM;
  12640. }
  12641. if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
  12642. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  12643. return -EINVAL;
  12644. }
  12645. /*
  12646. * There's something wrong with the cursor on CHV pipe C.
  12647. * If it straddles the left edge of the screen then
  12648. * moving it away from the edge or disabling it often
  12649. * results in a pipe underrun, and often that can lead to
  12650. * dead pipe (constant underrun reported, and it scans
  12651. * out just a solid color). To recover from that, the
  12652. * display power well must be turned off and on again.
  12653. * Refuse the put the cursor into that compromised position.
  12654. */
  12655. if (IS_CHERRYVIEW(to_i915(plane->dev)) && pipe == PIPE_C &&
  12656. state->base.visible && state->base.crtc_x < 0) {
  12657. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  12658. return -EINVAL;
  12659. }
  12660. return 0;
  12661. }
  12662. static void
  12663. intel_disable_cursor_plane(struct drm_plane *plane,
  12664. struct drm_crtc *crtc)
  12665. {
  12666. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12667. intel_crtc->cursor_addr = 0;
  12668. intel_crtc_update_cursor(crtc, NULL);
  12669. }
  12670. static void
  12671. intel_update_cursor_plane(struct drm_plane *plane,
  12672. const struct intel_crtc_state *crtc_state,
  12673. const struct intel_plane_state *state)
  12674. {
  12675. struct drm_crtc *crtc = crtc_state->base.crtc;
  12676. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12677. struct drm_device *dev = plane->dev;
  12678. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  12679. uint32_t addr;
  12680. if (!obj)
  12681. addr = 0;
  12682. else if (!INTEL_INFO(dev)->cursor_needs_physical)
  12683. addr = i915_gem_object_ggtt_offset(obj, NULL);
  12684. else
  12685. addr = obj->phys_handle->busaddr;
  12686. intel_crtc->cursor_addr = addr;
  12687. intel_crtc_update_cursor(crtc, state);
  12688. }
  12689. static struct intel_plane *
  12690. intel_cursor_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  12691. {
  12692. struct intel_plane *cursor = NULL;
  12693. struct intel_plane_state *state = NULL;
  12694. int ret;
  12695. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  12696. if (!cursor) {
  12697. ret = -ENOMEM;
  12698. goto fail;
  12699. }
  12700. state = intel_create_plane_state(&cursor->base);
  12701. if (!state) {
  12702. ret = -ENOMEM;
  12703. goto fail;
  12704. }
  12705. cursor->base.state = &state->base;
  12706. cursor->can_scale = false;
  12707. cursor->max_downscale = 1;
  12708. cursor->pipe = pipe;
  12709. cursor->plane = pipe;
  12710. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  12711. cursor->check_plane = intel_check_cursor_plane;
  12712. cursor->update_plane = intel_update_cursor_plane;
  12713. cursor->disable_plane = intel_disable_cursor_plane;
  12714. ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
  12715. 0, &intel_plane_funcs,
  12716. intel_cursor_formats,
  12717. ARRAY_SIZE(intel_cursor_formats),
  12718. DRM_PLANE_TYPE_CURSOR,
  12719. "cursor %c", pipe_name(pipe));
  12720. if (ret)
  12721. goto fail;
  12722. if (INTEL_GEN(dev_priv) >= 4)
  12723. drm_plane_create_rotation_property(&cursor->base,
  12724. DRM_ROTATE_0,
  12725. DRM_ROTATE_0 |
  12726. DRM_ROTATE_180);
  12727. if (INTEL_GEN(dev_priv) >= 9)
  12728. state->scaler_id = -1;
  12729. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  12730. return cursor;
  12731. fail:
  12732. kfree(state);
  12733. kfree(cursor);
  12734. return ERR_PTR(ret);
  12735. }
  12736. static void skl_init_scalers(struct drm_i915_private *dev_priv,
  12737. struct intel_crtc *crtc,
  12738. struct intel_crtc_state *crtc_state)
  12739. {
  12740. struct intel_crtc_scaler_state *scaler_state =
  12741. &crtc_state->scaler_state;
  12742. int i;
  12743. for (i = 0; i < crtc->num_scalers; i++) {
  12744. struct intel_scaler *scaler = &scaler_state->scalers[i];
  12745. scaler->in_use = 0;
  12746. scaler->mode = PS_SCALER_MODE_DYN;
  12747. }
  12748. scaler_state->scaler_id = -1;
  12749. }
  12750. static int intel_crtc_init(struct drm_device *dev, enum pipe pipe)
  12751. {
  12752. struct drm_i915_private *dev_priv = to_i915(dev);
  12753. struct intel_crtc *intel_crtc;
  12754. struct intel_crtc_state *crtc_state = NULL;
  12755. struct intel_plane *primary = NULL;
  12756. struct intel_plane *cursor = NULL;
  12757. int sprite, ret;
  12758. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  12759. if (!intel_crtc)
  12760. return -ENOMEM;
  12761. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  12762. if (!crtc_state) {
  12763. ret = -ENOMEM;
  12764. goto fail;
  12765. }
  12766. intel_crtc->config = crtc_state;
  12767. intel_crtc->base.state = &crtc_state->base;
  12768. crtc_state->base.crtc = &intel_crtc->base;
  12769. /* initialize shared scalers */
  12770. if (INTEL_INFO(dev)->gen >= 9) {
  12771. if (pipe == PIPE_C)
  12772. intel_crtc->num_scalers = 1;
  12773. else
  12774. intel_crtc->num_scalers = SKL_NUM_SCALERS;
  12775. skl_init_scalers(dev_priv, intel_crtc, crtc_state);
  12776. }
  12777. primary = intel_primary_plane_create(dev_priv, pipe);
  12778. if (IS_ERR(primary)) {
  12779. ret = PTR_ERR(primary);
  12780. goto fail;
  12781. }
  12782. for_each_sprite(dev_priv, pipe, sprite) {
  12783. struct intel_plane *plane;
  12784. plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
  12785. if (!plane) {
  12786. ret = PTR_ERR(plane);
  12787. goto fail;
  12788. }
  12789. }
  12790. cursor = intel_cursor_plane_create(dev_priv, pipe);
  12791. if (!cursor) {
  12792. ret = PTR_ERR(cursor);
  12793. goto fail;
  12794. }
  12795. ret = drm_crtc_init_with_planes(dev, &intel_crtc->base,
  12796. &primary->base, &cursor->base,
  12797. &intel_crtc_funcs,
  12798. "pipe %c", pipe_name(pipe));
  12799. if (ret)
  12800. goto fail;
  12801. /*
  12802. * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
  12803. * is hooked to pipe B. Hence we want plane A feeding pipe B.
  12804. */
  12805. intel_crtc->pipe = pipe;
  12806. intel_crtc->plane = (enum plane) pipe;
  12807. if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
  12808. DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
  12809. intel_crtc->plane = !pipe;
  12810. }
  12811. intel_crtc->cursor_base = ~0;
  12812. intel_crtc->cursor_cntl = ~0;
  12813. intel_crtc->cursor_size = ~0;
  12814. intel_crtc->wm.cxsr_allowed = true;
  12815. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  12816. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  12817. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
  12818. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
  12819. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  12820. intel_color_init(&intel_crtc->base);
  12821. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  12822. return 0;
  12823. fail:
  12824. /*
  12825. * drm_mode_config_cleanup() will free up any
  12826. * crtcs/planes already initialized.
  12827. */
  12828. kfree(crtc_state);
  12829. kfree(intel_crtc);
  12830. return ret;
  12831. }
  12832. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  12833. {
  12834. struct drm_encoder *encoder = connector->base.encoder;
  12835. struct drm_device *dev = connector->base.dev;
  12836. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  12837. if (!encoder || WARN_ON(!encoder->crtc))
  12838. return INVALID_PIPE;
  12839. return to_intel_crtc(encoder->crtc)->pipe;
  12840. }
  12841. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  12842. struct drm_file *file)
  12843. {
  12844. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  12845. struct drm_crtc *drmmode_crtc;
  12846. struct intel_crtc *crtc;
  12847. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  12848. if (!drmmode_crtc)
  12849. return -ENOENT;
  12850. crtc = to_intel_crtc(drmmode_crtc);
  12851. pipe_from_crtc_id->pipe = crtc->pipe;
  12852. return 0;
  12853. }
  12854. static int intel_encoder_clones(struct intel_encoder *encoder)
  12855. {
  12856. struct drm_device *dev = encoder->base.dev;
  12857. struct intel_encoder *source_encoder;
  12858. int index_mask = 0;
  12859. int entry = 0;
  12860. for_each_intel_encoder(dev, source_encoder) {
  12861. if (encoders_cloneable(encoder, source_encoder))
  12862. index_mask |= (1 << entry);
  12863. entry++;
  12864. }
  12865. return index_mask;
  12866. }
  12867. static bool has_edp_a(struct drm_device *dev)
  12868. {
  12869. struct drm_i915_private *dev_priv = to_i915(dev);
  12870. if (!IS_MOBILE(dev))
  12871. return false;
  12872. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  12873. return false;
  12874. if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  12875. return false;
  12876. return true;
  12877. }
  12878. static bool intel_crt_present(struct drm_device *dev)
  12879. {
  12880. struct drm_i915_private *dev_priv = to_i915(dev);
  12881. if (INTEL_INFO(dev)->gen >= 9)
  12882. return false;
  12883. if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
  12884. return false;
  12885. if (IS_CHERRYVIEW(dev_priv))
  12886. return false;
  12887. if (HAS_PCH_LPT_H(dev_priv) &&
  12888. I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  12889. return false;
  12890. /* DDI E can't be used if DDI A requires 4 lanes */
  12891. if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  12892. return false;
  12893. if (!dev_priv->vbt.int_crt_support)
  12894. return false;
  12895. return true;
  12896. }
  12897. void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
  12898. {
  12899. int pps_num;
  12900. int pps_idx;
  12901. if (HAS_DDI(dev_priv))
  12902. return;
  12903. /*
  12904. * This w/a is needed at least on CPT/PPT, but to be sure apply it
  12905. * everywhere where registers can be write protected.
  12906. */
  12907. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12908. pps_num = 2;
  12909. else
  12910. pps_num = 1;
  12911. for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
  12912. u32 val = I915_READ(PP_CONTROL(pps_idx));
  12913. val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
  12914. I915_WRITE(PP_CONTROL(pps_idx), val);
  12915. }
  12916. }
  12917. static void intel_pps_init(struct drm_i915_private *dev_priv)
  12918. {
  12919. if (HAS_PCH_SPLIT(dev_priv) || IS_BROXTON(dev_priv))
  12920. dev_priv->pps_mmio_base = PCH_PPS_BASE;
  12921. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  12922. dev_priv->pps_mmio_base = VLV_PPS_BASE;
  12923. else
  12924. dev_priv->pps_mmio_base = PPS_BASE;
  12925. intel_pps_unlock_regs_wa(dev_priv);
  12926. }
  12927. static void intel_setup_outputs(struct drm_device *dev)
  12928. {
  12929. struct drm_i915_private *dev_priv = to_i915(dev);
  12930. struct intel_encoder *encoder;
  12931. bool dpd_is_edp = false;
  12932. intel_pps_init(dev_priv);
  12933. /*
  12934. * intel_edp_init_connector() depends on this completing first, to
  12935. * prevent the registeration of both eDP and LVDS and the incorrect
  12936. * sharing of the PPS.
  12937. */
  12938. intel_lvds_init(dev);
  12939. if (intel_crt_present(dev))
  12940. intel_crt_init(dev);
  12941. if (IS_BROXTON(dev_priv)) {
  12942. /*
  12943. * FIXME: Broxton doesn't support port detection via the
  12944. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  12945. * detect the ports.
  12946. */
  12947. intel_ddi_init(dev, PORT_A);
  12948. intel_ddi_init(dev, PORT_B);
  12949. intel_ddi_init(dev, PORT_C);
  12950. intel_dsi_init(dev);
  12951. } else if (HAS_DDI(dev_priv)) {
  12952. int found;
  12953. /*
  12954. * Haswell uses DDI functions to detect digital outputs.
  12955. * On SKL pre-D0 the strap isn't connected, so we assume
  12956. * it's there.
  12957. */
  12958. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  12959. /* WaIgnoreDDIAStrap: skl */
  12960. if (found || IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  12961. intel_ddi_init(dev, PORT_A);
  12962. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  12963. * register */
  12964. found = I915_READ(SFUSE_STRAP);
  12965. if (found & SFUSE_STRAP_DDIB_DETECTED)
  12966. intel_ddi_init(dev, PORT_B);
  12967. if (found & SFUSE_STRAP_DDIC_DETECTED)
  12968. intel_ddi_init(dev, PORT_C);
  12969. if (found & SFUSE_STRAP_DDID_DETECTED)
  12970. intel_ddi_init(dev, PORT_D);
  12971. /*
  12972. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  12973. */
  12974. if ((IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) &&
  12975. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  12976. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  12977. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  12978. intel_ddi_init(dev, PORT_E);
  12979. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12980. int found;
  12981. dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
  12982. if (has_edp_a(dev))
  12983. intel_dp_init(dev, DP_A, PORT_A);
  12984. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  12985. /* PCH SDVOB multiplex with HDMIB */
  12986. found = intel_sdvo_init(dev, PCH_SDVOB, PORT_B);
  12987. if (!found)
  12988. intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
  12989. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  12990. intel_dp_init(dev, PCH_DP_B, PORT_B);
  12991. }
  12992. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  12993. intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
  12994. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  12995. intel_hdmi_init(dev, PCH_HDMID, PORT_D);
  12996. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  12997. intel_dp_init(dev, PCH_DP_C, PORT_C);
  12998. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  12999. intel_dp_init(dev, PCH_DP_D, PORT_D);
  13000. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  13001. bool has_edp, has_port;
  13002. /*
  13003. * The DP_DETECTED bit is the latched state of the DDC
  13004. * SDA pin at boot. However since eDP doesn't require DDC
  13005. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  13006. * eDP ports may have been muxed to an alternate function.
  13007. * Thus we can't rely on the DP_DETECTED bit alone to detect
  13008. * eDP ports. Consult the VBT as well as DP_DETECTED to
  13009. * detect eDP ports.
  13010. *
  13011. * Sadly the straps seem to be missing sometimes even for HDMI
  13012. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  13013. * and VBT for the presence of the port. Additionally we can't
  13014. * trust the port type the VBT declares as we've seen at least
  13015. * HDMI ports that the VBT claim are DP or eDP.
  13016. */
  13017. has_edp = intel_dp_is_edp(dev, PORT_B);
  13018. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  13019. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  13020. has_edp &= intel_dp_init(dev, VLV_DP_B, PORT_B);
  13021. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  13022. intel_hdmi_init(dev, VLV_HDMIB, PORT_B);
  13023. has_edp = intel_dp_is_edp(dev, PORT_C);
  13024. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  13025. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  13026. has_edp &= intel_dp_init(dev, VLV_DP_C, PORT_C);
  13027. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  13028. intel_hdmi_init(dev, VLV_HDMIC, PORT_C);
  13029. if (IS_CHERRYVIEW(dev_priv)) {
  13030. /*
  13031. * eDP not supported on port D,
  13032. * so no need to worry about it
  13033. */
  13034. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  13035. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  13036. intel_dp_init(dev, CHV_DP_D, PORT_D);
  13037. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  13038. intel_hdmi_init(dev, CHV_HDMID, PORT_D);
  13039. }
  13040. intel_dsi_init(dev);
  13041. } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
  13042. bool found = false;
  13043. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  13044. DRM_DEBUG_KMS("probing SDVOB\n");
  13045. found = intel_sdvo_init(dev, GEN3_SDVOB, PORT_B);
  13046. if (!found && IS_G4X(dev_priv)) {
  13047. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  13048. intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
  13049. }
  13050. if (!found && IS_G4X(dev_priv))
  13051. intel_dp_init(dev, DP_B, PORT_B);
  13052. }
  13053. /* Before G4X SDVOC doesn't have its own detect register */
  13054. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  13055. DRM_DEBUG_KMS("probing SDVOC\n");
  13056. found = intel_sdvo_init(dev, GEN3_SDVOC, PORT_C);
  13057. }
  13058. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  13059. if (IS_G4X(dev_priv)) {
  13060. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  13061. intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
  13062. }
  13063. if (IS_G4X(dev_priv))
  13064. intel_dp_init(dev, DP_C, PORT_C);
  13065. }
  13066. if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
  13067. intel_dp_init(dev, DP_D, PORT_D);
  13068. } else if (IS_GEN2(dev_priv))
  13069. intel_dvo_init(dev);
  13070. if (SUPPORTS_TV(dev))
  13071. intel_tv_init(dev);
  13072. intel_psr_init(dev);
  13073. for_each_intel_encoder(dev, encoder) {
  13074. encoder->base.possible_crtcs = encoder->crtc_mask;
  13075. encoder->base.possible_clones =
  13076. intel_encoder_clones(encoder);
  13077. }
  13078. intel_init_pch_refclk(dev);
  13079. drm_helper_move_panel_connectors_to_head(dev);
  13080. }
  13081. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  13082. {
  13083. struct drm_device *dev = fb->dev;
  13084. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  13085. drm_framebuffer_cleanup(fb);
  13086. mutex_lock(&dev->struct_mutex);
  13087. WARN_ON(!intel_fb->obj->framebuffer_references--);
  13088. i915_gem_object_put(intel_fb->obj);
  13089. mutex_unlock(&dev->struct_mutex);
  13090. kfree(intel_fb);
  13091. }
  13092. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  13093. struct drm_file *file,
  13094. unsigned int *handle)
  13095. {
  13096. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  13097. struct drm_i915_gem_object *obj = intel_fb->obj;
  13098. if (obj->userptr.mm) {
  13099. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  13100. return -EINVAL;
  13101. }
  13102. return drm_gem_handle_create(file, &obj->base, handle);
  13103. }
  13104. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  13105. struct drm_file *file,
  13106. unsigned flags, unsigned color,
  13107. struct drm_clip_rect *clips,
  13108. unsigned num_clips)
  13109. {
  13110. struct drm_device *dev = fb->dev;
  13111. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  13112. struct drm_i915_gem_object *obj = intel_fb->obj;
  13113. mutex_lock(&dev->struct_mutex);
  13114. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  13115. mutex_unlock(&dev->struct_mutex);
  13116. return 0;
  13117. }
  13118. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  13119. .destroy = intel_user_framebuffer_destroy,
  13120. .create_handle = intel_user_framebuffer_create_handle,
  13121. .dirty = intel_user_framebuffer_dirty,
  13122. };
  13123. static
  13124. u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
  13125. uint64_t fb_modifier, uint32_t pixel_format)
  13126. {
  13127. u32 gen = INTEL_INFO(dev_priv)->gen;
  13128. if (gen >= 9) {
  13129. int cpp = drm_format_plane_cpp(pixel_format, 0);
  13130. /* "The stride in bytes must not exceed the of the size of 8K
  13131. * pixels and 32K bytes."
  13132. */
  13133. return min(8192 * cpp, 32768);
  13134. } else if (gen >= 5 && !IS_VALLEYVIEW(dev_priv) &&
  13135. !IS_CHERRYVIEW(dev_priv)) {
  13136. return 32*1024;
  13137. } else if (gen >= 4) {
  13138. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  13139. return 16*1024;
  13140. else
  13141. return 32*1024;
  13142. } else if (gen >= 3) {
  13143. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  13144. return 8*1024;
  13145. else
  13146. return 16*1024;
  13147. } else {
  13148. /* XXX DSPC is limited to 4k tiled */
  13149. return 8*1024;
  13150. }
  13151. }
  13152. static int intel_framebuffer_init(struct drm_device *dev,
  13153. struct intel_framebuffer *intel_fb,
  13154. struct drm_mode_fb_cmd2 *mode_cmd,
  13155. struct drm_i915_gem_object *obj)
  13156. {
  13157. struct drm_i915_private *dev_priv = to_i915(dev);
  13158. unsigned int tiling = i915_gem_object_get_tiling(obj);
  13159. int ret;
  13160. u32 pitch_limit, stride_alignment;
  13161. char *format_name;
  13162. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  13163. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  13164. /*
  13165. * If there's a fence, enforce that
  13166. * the fb modifier and tiling mode match.
  13167. */
  13168. if (tiling != I915_TILING_NONE &&
  13169. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  13170. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  13171. return -EINVAL;
  13172. }
  13173. } else {
  13174. if (tiling == I915_TILING_X) {
  13175. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  13176. } else if (tiling == I915_TILING_Y) {
  13177. DRM_DEBUG("No Y tiling for legacy addfb\n");
  13178. return -EINVAL;
  13179. }
  13180. }
  13181. /* Passed in modifier sanity checking. */
  13182. switch (mode_cmd->modifier[0]) {
  13183. case I915_FORMAT_MOD_Y_TILED:
  13184. case I915_FORMAT_MOD_Yf_TILED:
  13185. if (INTEL_INFO(dev)->gen < 9) {
  13186. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  13187. mode_cmd->modifier[0]);
  13188. return -EINVAL;
  13189. }
  13190. case DRM_FORMAT_MOD_NONE:
  13191. case I915_FORMAT_MOD_X_TILED:
  13192. break;
  13193. default:
  13194. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  13195. mode_cmd->modifier[0]);
  13196. return -EINVAL;
  13197. }
  13198. /*
  13199. * gen2/3 display engine uses the fence if present,
  13200. * so the tiling mode must match the fb modifier exactly.
  13201. */
  13202. if (INTEL_INFO(dev_priv)->gen < 4 &&
  13203. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  13204. DRM_DEBUG("tiling_mode must match fb modifier exactly on gen2/3\n");
  13205. return -EINVAL;
  13206. }
  13207. stride_alignment = intel_fb_stride_alignment(dev_priv,
  13208. mode_cmd->modifier[0],
  13209. mode_cmd->pixel_format);
  13210. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  13211. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  13212. mode_cmd->pitches[0], stride_alignment);
  13213. return -EINVAL;
  13214. }
  13215. pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
  13216. mode_cmd->pixel_format);
  13217. if (mode_cmd->pitches[0] > pitch_limit) {
  13218. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  13219. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  13220. "tiled" : "linear",
  13221. mode_cmd->pitches[0], pitch_limit);
  13222. return -EINVAL;
  13223. }
  13224. /*
  13225. * If there's a fence, enforce that
  13226. * the fb pitch and fence stride match.
  13227. */
  13228. if (tiling != I915_TILING_NONE &&
  13229. mode_cmd->pitches[0] != i915_gem_object_get_stride(obj)) {
  13230. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  13231. mode_cmd->pitches[0],
  13232. i915_gem_object_get_stride(obj));
  13233. return -EINVAL;
  13234. }
  13235. /* Reject formats not supported by any plane early. */
  13236. switch (mode_cmd->pixel_format) {
  13237. case DRM_FORMAT_C8:
  13238. case DRM_FORMAT_RGB565:
  13239. case DRM_FORMAT_XRGB8888:
  13240. case DRM_FORMAT_ARGB8888:
  13241. break;
  13242. case DRM_FORMAT_XRGB1555:
  13243. if (INTEL_INFO(dev)->gen > 3) {
  13244. format_name = drm_get_format_name(mode_cmd->pixel_format);
  13245. DRM_DEBUG("unsupported pixel format: %s\n", format_name);
  13246. kfree(format_name);
  13247. return -EINVAL;
  13248. }
  13249. break;
  13250. case DRM_FORMAT_ABGR8888:
  13251. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  13252. INTEL_INFO(dev)->gen < 9) {
  13253. format_name = drm_get_format_name(mode_cmd->pixel_format);
  13254. DRM_DEBUG("unsupported pixel format: %s\n", format_name);
  13255. kfree(format_name);
  13256. return -EINVAL;
  13257. }
  13258. break;
  13259. case DRM_FORMAT_XBGR8888:
  13260. case DRM_FORMAT_XRGB2101010:
  13261. case DRM_FORMAT_XBGR2101010:
  13262. if (INTEL_INFO(dev)->gen < 4) {
  13263. format_name = drm_get_format_name(mode_cmd->pixel_format);
  13264. DRM_DEBUG("unsupported pixel format: %s\n", format_name);
  13265. kfree(format_name);
  13266. return -EINVAL;
  13267. }
  13268. break;
  13269. case DRM_FORMAT_ABGR2101010:
  13270. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  13271. format_name = drm_get_format_name(mode_cmd->pixel_format);
  13272. DRM_DEBUG("unsupported pixel format: %s\n", format_name);
  13273. kfree(format_name);
  13274. return -EINVAL;
  13275. }
  13276. break;
  13277. case DRM_FORMAT_YUYV:
  13278. case DRM_FORMAT_UYVY:
  13279. case DRM_FORMAT_YVYU:
  13280. case DRM_FORMAT_VYUY:
  13281. if (INTEL_INFO(dev)->gen < 5) {
  13282. format_name = drm_get_format_name(mode_cmd->pixel_format);
  13283. DRM_DEBUG("unsupported pixel format: %s\n", format_name);
  13284. kfree(format_name);
  13285. return -EINVAL;
  13286. }
  13287. break;
  13288. default:
  13289. format_name = drm_get_format_name(mode_cmd->pixel_format);
  13290. DRM_DEBUG("unsupported pixel format: %s\n", format_name);
  13291. kfree(format_name);
  13292. return -EINVAL;
  13293. }
  13294. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  13295. if (mode_cmd->offsets[0] != 0)
  13296. return -EINVAL;
  13297. drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
  13298. intel_fb->obj = obj;
  13299. ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
  13300. if (ret)
  13301. return ret;
  13302. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  13303. if (ret) {
  13304. DRM_ERROR("framebuffer init failed %d\n", ret);
  13305. return ret;
  13306. }
  13307. intel_fb->obj->framebuffer_references++;
  13308. return 0;
  13309. }
  13310. static struct drm_framebuffer *
  13311. intel_user_framebuffer_create(struct drm_device *dev,
  13312. struct drm_file *filp,
  13313. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  13314. {
  13315. struct drm_framebuffer *fb;
  13316. struct drm_i915_gem_object *obj;
  13317. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  13318. obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
  13319. if (!obj)
  13320. return ERR_PTR(-ENOENT);
  13321. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  13322. if (IS_ERR(fb))
  13323. i915_gem_object_put(obj);
  13324. return fb;
  13325. }
  13326. static const struct drm_mode_config_funcs intel_mode_funcs = {
  13327. .fb_create = intel_user_framebuffer_create,
  13328. .output_poll_changed = intel_fbdev_output_poll_changed,
  13329. .atomic_check = intel_atomic_check,
  13330. .atomic_commit = intel_atomic_commit,
  13331. .atomic_state_alloc = intel_atomic_state_alloc,
  13332. .atomic_state_clear = intel_atomic_state_clear,
  13333. };
  13334. /**
  13335. * intel_init_display_hooks - initialize the display modesetting hooks
  13336. * @dev_priv: device private
  13337. */
  13338. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  13339. {
  13340. if (INTEL_INFO(dev_priv)->gen >= 9) {
  13341. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  13342. dev_priv->display.get_initial_plane_config =
  13343. skylake_get_initial_plane_config;
  13344. dev_priv->display.crtc_compute_clock =
  13345. haswell_crtc_compute_clock;
  13346. dev_priv->display.crtc_enable = haswell_crtc_enable;
  13347. dev_priv->display.crtc_disable = haswell_crtc_disable;
  13348. } else if (HAS_DDI(dev_priv)) {
  13349. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  13350. dev_priv->display.get_initial_plane_config =
  13351. ironlake_get_initial_plane_config;
  13352. dev_priv->display.crtc_compute_clock =
  13353. haswell_crtc_compute_clock;
  13354. dev_priv->display.crtc_enable = haswell_crtc_enable;
  13355. dev_priv->display.crtc_disable = haswell_crtc_disable;
  13356. } else if (HAS_PCH_SPLIT(dev_priv)) {
  13357. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  13358. dev_priv->display.get_initial_plane_config =
  13359. ironlake_get_initial_plane_config;
  13360. dev_priv->display.crtc_compute_clock =
  13361. ironlake_crtc_compute_clock;
  13362. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  13363. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  13364. } else if (IS_CHERRYVIEW(dev_priv)) {
  13365. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13366. dev_priv->display.get_initial_plane_config =
  13367. i9xx_get_initial_plane_config;
  13368. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  13369. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  13370. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13371. } else if (IS_VALLEYVIEW(dev_priv)) {
  13372. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13373. dev_priv->display.get_initial_plane_config =
  13374. i9xx_get_initial_plane_config;
  13375. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  13376. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  13377. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13378. } else if (IS_G4X(dev_priv)) {
  13379. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13380. dev_priv->display.get_initial_plane_config =
  13381. i9xx_get_initial_plane_config;
  13382. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  13383. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13384. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13385. } else if (IS_PINEVIEW(dev_priv)) {
  13386. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13387. dev_priv->display.get_initial_plane_config =
  13388. i9xx_get_initial_plane_config;
  13389. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  13390. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13391. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13392. } else if (!IS_GEN2(dev_priv)) {
  13393. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13394. dev_priv->display.get_initial_plane_config =
  13395. i9xx_get_initial_plane_config;
  13396. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  13397. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13398. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13399. } else {
  13400. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13401. dev_priv->display.get_initial_plane_config =
  13402. i9xx_get_initial_plane_config;
  13403. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  13404. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13405. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13406. }
  13407. /* Returns the core display clock speed */
  13408. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  13409. dev_priv->display.get_display_clock_speed =
  13410. skylake_get_display_clock_speed;
  13411. else if (IS_BROXTON(dev_priv))
  13412. dev_priv->display.get_display_clock_speed =
  13413. broxton_get_display_clock_speed;
  13414. else if (IS_BROADWELL(dev_priv))
  13415. dev_priv->display.get_display_clock_speed =
  13416. broadwell_get_display_clock_speed;
  13417. else if (IS_HASWELL(dev_priv))
  13418. dev_priv->display.get_display_clock_speed =
  13419. haswell_get_display_clock_speed;
  13420. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  13421. dev_priv->display.get_display_clock_speed =
  13422. valleyview_get_display_clock_speed;
  13423. else if (IS_GEN5(dev_priv))
  13424. dev_priv->display.get_display_clock_speed =
  13425. ilk_get_display_clock_speed;
  13426. else if (IS_I945G(dev_priv) || IS_BROADWATER(dev_priv) ||
  13427. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  13428. dev_priv->display.get_display_clock_speed =
  13429. i945_get_display_clock_speed;
  13430. else if (IS_GM45(dev_priv))
  13431. dev_priv->display.get_display_clock_speed =
  13432. gm45_get_display_clock_speed;
  13433. else if (IS_CRESTLINE(dev_priv))
  13434. dev_priv->display.get_display_clock_speed =
  13435. i965gm_get_display_clock_speed;
  13436. else if (IS_PINEVIEW(dev_priv))
  13437. dev_priv->display.get_display_clock_speed =
  13438. pnv_get_display_clock_speed;
  13439. else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
  13440. dev_priv->display.get_display_clock_speed =
  13441. g33_get_display_clock_speed;
  13442. else if (IS_I915G(dev_priv))
  13443. dev_priv->display.get_display_clock_speed =
  13444. i915_get_display_clock_speed;
  13445. else if (IS_I945GM(dev_priv) || IS_845G(dev_priv))
  13446. dev_priv->display.get_display_clock_speed =
  13447. i9xx_misc_get_display_clock_speed;
  13448. else if (IS_I915GM(dev_priv))
  13449. dev_priv->display.get_display_clock_speed =
  13450. i915gm_get_display_clock_speed;
  13451. else if (IS_I865G(dev_priv))
  13452. dev_priv->display.get_display_clock_speed =
  13453. i865_get_display_clock_speed;
  13454. else if (IS_I85X(dev_priv))
  13455. dev_priv->display.get_display_clock_speed =
  13456. i85x_get_display_clock_speed;
  13457. else { /* 830 */
  13458. WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
  13459. dev_priv->display.get_display_clock_speed =
  13460. i830_get_display_clock_speed;
  13461. }
  13462. if (IS_GEN5(dev_priv)) {
  13463. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  13464. } else if (IS_GEN6(dev_priv)) {
  13465. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  13466. } else if (IS_IVYBRIDGE(dev_priv)) {
  13467. /* FIXME: detect B0+ stepping and use auto training */
  13468. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  13469. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  13470. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  13471. }
  13472. if (IS_BROADWELL(dev_priv)) {
  13473. dev_priv->display.modeset_commit_cdclk =
  13474. broadwell_modeset_commit_cdclk;
  13475. dev_priv->display.modeset_calc_cdclk =
  13476. broadwell_modeset_calc_cdclk;
  13477. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  13478. dev_priv->display.modeset_commit_cdclk =
  13479. valleyview_modeset_commit_cdclk;
  13480. dev_priv->display.modeset_calc_cdclk =
  13481. valleyview_modeset_calc_cdclk;
  13482. } else if (IS_BROXTON(dev_priv)) {
  13483. dev_priv->display.modeset_commit_cdclk =
  13484. bxt_modeset_commit_cdclk;
  13485. dev_priv->display.modeset_calc_cdclk =
  13486. bxt_modeset_calc_cdclk;
  13487. } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
  13488. dev_priv->display.modeset_commit_cdclk =
  13489. skl_modeset_commit_cdclk;
  13490. dev_priv->display.modeset_calc_cdclk =
  13491. skl_modeset_calc_cdclk;
  13492. }
  13493. if (dev_priv->info.gen >= 9)
  13494. dev_priv->display.update_crtcs = skl_update_crtcs;
  13495. else
  13496. dev_priv->display.update_crtcs = intel_update_crtcs;
  13497. switch (INTEL_INFO(dev_priv)->gen) {
  13498. case 2:
  13499. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  13500. break;
  13501. case 3:
  13502. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  13503. break;
  13504. case 4:
  13505. case 5:
  13506. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  13507. break;
  13508. case 6:
  13509. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  13510. break;
  13511. case 7:
  13512. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  13513. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  13514. break;
  13515. case 9:
  13516. /* Drop through - unsupported since execlist only. */
  13517. default:
  13518. /* Default just returns -ENODEV to indicate unsupported */
  13519. dev_priv->display.queue_flip = intel_default_queue_flip;
  13520. }
  13521. }
  13522. /*
  13523. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  13524. * resume, or other times. This quirk makes sure that's the case for
  13525. * affected systems.
  13526. */
  13527. static void quirk_pipea_force(struct drm_device *dev)
  13528. {
  13529. struct drm_i915_private *dev_priv = to_i915(dev);
  13530. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  13531. DRM_INFO("applying pipe a force quirk\n");
  13532. }
  13533. static void quirk_pipeb_force(struct drm_device *dev)
  13534. {
  13535. struct drm_i915_private *dev_priv = to_i915(dev);
  13536. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  13537. DRM_INFO("applying pipe b force quirk\n");
  13538. }
  13539. /*
  13540. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  13541. */
  13542. static void quirk_ssc_force_disable(struct drm_device *dev)
  13543. {
  13544. struct drm_i915_private *dev_priv = to_i915(dev);
  13545. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  13546. DRM_INFO("applying lvds SSC disable quirk\n");
  13547. }
  13548. /*
  13549. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  13550. * brightness value
  13551. */
  13552. static void quirk_invert_brightness(struct drm_device *dev)
  13553. {
  13554. struct drm_i915_private *dev_priv = to_i915(dev);
  13555. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  13556. DRM_INFO("applying inverted panel brightness quirk\n");
  13557. }
  13558. /* Some VBT's incorrectly indicate no backlight is present */
  13559. static void quirk_backlight_present(struct drm_device *dev)
  13560. {
  13561. struct drm_i915_private *dev_priv = to_i915(dev);
  13562. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  13563. DRM_INFO("applying backlight present quirk\n");
  13564. }
  13565. struct intel_quirk {
  13566. int device;
  13567. int subsystem_vendor;
  13568. int subsystem_device;
  13569. void (*hook)(struct drm_device *dev);
  13570. };
  13571. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  13572. struct intel_dmi_quirk {
  13573. void (*hook)(struct drm_device *dev);
  13574. const struct dmi_system_id (*dmi_id_list)[];
  13575. };
  13576. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  13577. {
  13578. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  13579. return 1;
  13580. }
  13581. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  13582. {
  13583. .dmi_id_list = &(const struct dmi_system_id[]) {
  13584. {
  13585. .callback = intel_dmi_reverse_brightness,
  13586. .ident = "NCR Corporation",
  13587. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  13588. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  13589. },
  13590. },
  13591. { } /* terminating entry */
  13592. },
  13593. .hook = quirk_invert_brightness,
  13594. },
  13595. };
  13596. static struct intel_quirk intel_quirks[] = {
  13597. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  13598. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  13599. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  13600. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  13601. /* 830 needs to leave pipe A & dpll A up */
  13602. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  13603. /* 830 needs to leave pipe B & dpll B up */
  13604. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  13605. /* Lenovo U160 cannot use SSC on LVDS */
  13606. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  13607. /* Sony Vaio Y cannot use SSC on LVDS */
  13608. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  13609. /* Acer Aspire 5734Z must invert backlight brightness */
  13610. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  13611. /* Acer/eMachines G725 */
  13612. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  13613. /* Acer/eMachines e725 */
  13614. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  13615. /* Acer/Packard Bell NCL20 */
  13616. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  13617. /* Acer Aspire 4736Z */
  13618. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  13619. /* Acer Aspire 5336 */
  13620. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  13621. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  13622. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  13623. /* Acer C720 Chromebook (Core i3 4005U) */
  13624. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  13625. /* Apple Macbook 2,1 (Core 2 T7400) */
  13626. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  13627. /* Apple Macbook 4,1 */
  13628. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  13629. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  13630. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  13631. /* HP Chromebook 14 (Celeron 2955U) */
  13632. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  13633. /* Dell Chromebook 11 */
  13634. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  13635. /* Dell Chromebook 11 (2015 version) */
  13636. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  13637. };
  13638. static void intel_init_quirks(struct drm_device *dev)
  13639. {
  13640. struct pci_dev *d = dev->pdev;
  13641. int i;
  13642. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  13643. struct intel_quirk *q = &intel_quirks[i];
  13644. if (d->device == q->device &&
  13645. (d->subsystem_vendor == q->subsystem_vendor ||
  13646. q->subsystem_vendor == PCI_ANY_ID) &&
  13647. (d->subsystem_device == q->subsystem_device ||
  13648. q->subsystem_device == PCI_ANY_ID))
  13649. q->hook(dev);
  13650. }
  13651. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  13652. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  13653. intel_dmi_quirks[i].hook(dev);
  13654. }
  13655. }
  13656. /* Disable the VGA plane that we never use */
  13657. static void i915_disable_vga(struct drm_device *dev)
  13658. {
  13659. struct drm_i915_private *dev_priv = to_i915(dev);
  13660. struct pci_dev *pdev = dev_priv->drm.pdev;
  13661. u8 sr1;
  13662. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  13663. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  13664. vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
  13665. outb(SR01, VGA_SR_INDEX);
  13666. sr1 = inb(VGA_SR_DATA);
  13667. outb(sr1 | 1<<5, VGA_SR_DATA);
  13668. vga_put(pdev, VGA_RSRC_LEGACY_IO);
  13669. udelay(300);
  13670. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  13671. POSTING_READ(vga_reg);
  13672. }
  13673. void intel_modeset_init_hw(struct drm_device *dev)
  13674. {
  13675. struct drm_i915_private *dev_priv = to_i915(dev);
  13676. intel_update_cdclk(dev);
  13677. dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
  13678. intel_init_clock_gating(dev);
  13679. }
  13680. /*
  13681. * Calculate what we think the watermarks should be for the state we've read
  13682. * out of the hardware and then immediately program those watermarks so that
  13683. * we ensure the hardware settings match our internal state.
  13684. *
  13685. * We can calculate what we think WM's should be by creating a duplicate of the
  13686. * current state (which was constructed during hardware readout) and running it
  13687. * through the atomic check code to calculate new watermark values in the
  13688. * state object.
  13689. */
  13690. static void sanitize_watermarks(struct drm_device *dev)
  13691. {
  13692. struct drm_i915_private *dev_priv = to_i915(dev);
  13693. struct drm_atomic_state *state;
  13694. struct drm_crtc *crtc;
  13695. struct drm_crtc_state *cstate;
  13696. struct drm_modeset_acquire_ctx ctx;
  13697. int ret;
  13698. int i;
  13699. /* Only supported on platforms that use atomic watermark design */
  13700. if (!dev_priv->display.optimize_watermarks)
  13701. return;
  13702. /*
  13703. * We need to hold connection_mutex before calling duplicate_state so
  13704. * that the connector loop is protected.
  13705. */
  13706. drm_modeset_acquire_init(&ctx, 0);
  13707. retry:
  13708. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  13709. if (ret == -EDEADLK) {
  13710. drm_modeset_backoff(&ctx);
  13711. goto retry;
  13712. } else if (WARN_ON(ret)) {
  13713. goto fail;
  13714. }
  13715. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  13716. if (WARN_ON(IS_ERR(state)))
  13717. goto fail;
  13718. /*
  13719. * Hardware readout is the only time we don't want to calculate
  13720. * intermediate watermarks (since we don't trust the current
  13721. * watermarks).
  13722. */
  13723. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  13724. ret = intel_atomic_check(dev, state);
  13725. if (ret) {
  13726. /*
  13727. * If we fail here, it means that the hardware appears to be
  13728. * programmed in a way that shouldn't be possible, given our
  13729. * understanding of watermark requirements. This might mean a
  13730. * mistake in the hardware readout code or a mistake in the
  13731. * watermark calculations for a given platform. Raise a WARN
  13732. * so that this is noticeable.
  13733. *
  13734. * If this actually happens, we'll have to just leave the
  13735. * BIOS-programmed watermarks untouched and hope for the best.
  13736. */
  13737. WARN(true, "Could not determine valid watermarks for inherited state\n");
  13738. goto put_state;
  13739. }
  13740. /* Write calculated watermark values back */
  13741. for_each_crtc_in_state(state, crtc, cstate, i) {
  13742. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  13743. cs->wm.need_postvbl_update = true;
  13744. dev_priv->display.optimize_watermarks(cs);
  13745. }
  13746. put_state:
  13747. drm_atomic_state_put(state);
  13748. fail:
  13749. drm_modeset_drop_locks(&ctx);
  13750. drm_modeset_acquire_fini(&ctx);
  13751. }
  13752. int intel_modeset_init(struct drm_device *dev)
  13753. {
  13754. struct drm_i915_private *dev_priv = to_i915(dev);
  13755. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  13756. enum pipe pipe;
  13757. struct intel_crtc *crtc;
  13758. drm_mode_config_init(dev);
  13759. dev->mode_config.min_width = 0;
  13760. dev->mode_config.min_height = 0;
  13761. dev->mode_config.preferred_depth = 24;
  13762. dev->mode_config.prefer_shadow = 1;
  13763. dev->mode_config.allow_fb_modifiers = true;
  13764. dev->mode_config.funcs = &intel_mode_funcs;
  13765. intel_init_quirks(dev);
  13766. intel_init_pm(dev);
  13767. if (INTEL_INFO(dev)->num_pipes == 0)
  13768. return 0;
  13769. /*
  13770. * There may be no VBT; and if the BIOS enabled SSC we can
  13771. * just keep using it to avoid unnecessary flicker. Whereas if the
  13772. * BIOS isn't using it, don't assume it will work even if the VBT
  13773. * indicates as much.
  13774. */
  13775. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
  13776. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  13777. DREF_SSC1_ENABLE);
  13778. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  13779. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  13780. bios_lvds_use_ssc ? "en" : "dis",
  13781. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  13782. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  13783. }
  13784. }
  13785. if (IS_GEN2(dev_priv)) {
  13786. dev->mode_config.max_width = 2048;
  13787. dev->mode_config.max_height = 2048;
  13788. } else if (IS_GEN3(dev_priv)) {
  13789. dev->mode_config.max_width = 4096;
  13790. dev->mode_config.max_height = 4096;
  13791. } else {
  13792. dev->mode_config.max_width = 8192;
  13793. dev->mode_config.max_height = 8192;
  13794. }
  13795. if (IS_845G(dev_priv) || IS_I865G(dev_priv)) {
  13796. dev->mode_config.cursor_width = IS_845G(dev_priv) ? 64 : 512;
  13797. dev->mode_config.cursor_height = 1023;
  13798. } else if (IS_GEN2(dev_priv)) {
  13799. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  13800. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  13801. } else {
  13802. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  13803. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  13804. }
  13805. dev->mode_config.fb_base = ggtt->mappable_base;
  13806. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  13807. INTEL_INFO(dev)->num_pipes,
  13808. INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
  13809. for_each_pipe(dev_priv, pipe) {
  13810. int ret;
  13811. ret = intel_crtc_init(dev, pipe);
  13812. if (ret) {
  13813. drm_mode_config_cleanup(dev);
  13814. return ret;
  13815. }
  13816. }
  13817. intel_update_czclk(dev_priv);
  13818. intel_update_cdclk(dev);
  13819. intel_shared_dpll_init(dev);
  13820. if (dev_priv->max_cdclk_freq == 0)
  13821. intel_update_max_cdclk(dev);
  13822. /* Just disable it once at startup */
  13823. i915_disable_vga(dev);
  13824. intel_setup_outputs(dev);
  13825. drm_modeset_lock_all(dev);
  13826. intel_modeset_setup_hw_state(dev);
  13827. drm_modeset_unlock_all(dev);
  13828. for_each_intel_crtc(dev, crtc) {
  13829. struct intel_initial_plane_config plane_config = {};
  13830. if (!crtc->active)
  13831. continue;
  13832. /*
  13833. * Note that reserving the BIOS fb up front prevents us
  13834. * from stuffing other stolen allocations like the ring
  13835. * on top. This prevents some ugliness at boot time, and
  13836. * can even allow for smooth boot transitions if the BIOS
  13837. * fb is large enough for the active pipe configuration.
  13838. */
  13839. dev_priv->display.get_initial_plane_config(crtc,
  13840. &plane_config);
  13841. /*
  13842. * If the fb is shared between multiple heads, we'll
  13843. * just get the first one.
  13844. */
  13845. intel_find_initial_plane_obj(crtc, &plane_config);
  13846. }
  13847. /*
  13848. * Make sure hardware watermarks really match the state we read out.
  13849. * Note that we need to do this after reconstructing the BIOS fb's
  13850. * since the watermark calculation done here will use pstate->fb.
  13851. */
  13852. sanitize_watermarks(dev);
  13853. return 0;
  13854. }
  13855. static void intel_enable_pipe_a(struct drm_device *dev)
  13856. {
  13857. struct intel_connector *connector;
  13858. struct drm_connector *crt = NULL;
  13859. struct intel_load_detect_pipe load_detect_temp;
  13860. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  13861. /* We can't just switch on the pipe A, we need to set things up with a
  13862. * proper mode and output configuration. As a gross hack, enable pipe A
  13863. * by enabling the load detect pipe once. */
  13864. for_each_intel_connector(dev, connector) {
  13865. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  13866. crt = &connector->base;
  13867. break;
  13868. }
  13869. }
  13870. if (!crt)
  13871. return;
  13872. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  13873. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  13874. }
  13875. static bool
  13876. intel_check_plane_mapping(struct intel_crtc *crtc)
  13877. {
  13878. struct drm_device *dev = crtc->base.dev;
  13879. struct drm_i915_private *dev_priv = to_i915(dev);
  13880. u32 val;
  13881. if (INTEL_INFO(dev)->num_pipes == 1)
  13882. return true;
  13883. val = I915_READ(DSPCNTR(!crtc->plane));
  13884. if ((val & DISPLAY_PLANE_ENABLE) &&
  13885. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  13886. return false;
  13887. return true;
  13888. }
  13889. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  13890. {
  13891. struct drm_device *dev = crtc->base.dev;
  13892. struct intel_encoder *encoder;
  13893. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  13894. return true;
  13895. return false;
  13896. }
  13897. static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
  13898. {
  13899. struct drm_device *dev = encoder->base.dev;
  13900. struct intel_connector *connector;
  13901. for_each_connector_on_encoder(dev, &encoder->base, connector)
  13902. return connector;
  13903. return NULL;
  13904. }
  13905. static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
  13906. enum transcoder pch_transcoder)
  13907. {
  13908. return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
  13909. (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
  13910. }
  13911. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  13912. {
  13913. struct drm_device *dev = crtc->base.dev;
  13914. struct drm_i915_private *dev_priv = to_i915(dev);
  13915. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  13916. /* Clear any frame start delays used for debugging left by the BIOS */
  13917. if (!transcoder_is_dsi(cpu_transcoder)) {
  13918. i915_reg_t reg = PIPECONF(cpu_transcoder);
  13919. I915_WRITE(reg,
  13920. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  13921. }
  13922. /* restore vblank interrupts to correct state */
  13923. drm_crtc_vblank_reset(&crtc->base);
  13924. if (crtc->active) {
  13925. struct intel_plane *plane;
  13926. drm_crtc_vblank_on(&crtc->base);
  13927. /* Disable everything but the primary plane */
  13928. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  13929. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  13930. continue;
  13931. plane->disable_plane(&plane->base, &crtc->base);
  13932. }
  13933. }
  13934. /* We need to sanitize the plane -> pipe mapping first because this will
  13935. * disable the crtc (and hence change the state) if it is wrong. Note
  13936. * that gen4+ has a fixed plane -> pipe mapping. */
  13937. if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
  13938. bool plane;
  13939. DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
  13940. crtc->base.base.id, crtc->base.name);
  13941. /* Pipe has the wrong plane attached and the plane is active.
  13942. * Temporarily change the plane mapping and disable everything
  13943. * ... */
  13944. plane = crtc->plane;
  13945. to_intel_plane_state(crtc->base.primary->state)->base.visible = true;
  13946. crtc->plane = !plane;
  13947. intel_crtc_disable_noatomic(&crtc->base);
  13948. crtc->plane = plane;
  13949. }
  13950. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  13951. crtc->pipe == PIPE_A && !crtc->active) {
  13952. /* BIOS forgot to enable pipe A, this mostly happens after
  13953. * resume. Force-enable the pipe to fix this, the update_dpms
  13954. * call below we restore the pipe to the right state, but leave
  13955. * the required bits on. */
  13956. intel_enable_pipe_a(dev);
  13957. }
  13958. /* Adjust the state of the output pipe according to whether we
  13959. * have active connectors/encoders. */
  13960. if (crtc->active && !intel_crtc_has_encoders(crtc))
  13961. intel_crtc_disable_noatomic(&crtc->base);
  13962. if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
  13963. /*
  13964. * We start out with underrun reporting disabled to avoid races.
  13965. * For correct bookkeeping mark this on active crtcs.
  13966. *
  13967. * Also on gmch platforms we dont have any hardware bits to
  13968. * disable the underrun reporting. Which means we need to start
  13969. * out with underrun reporting disabled also on inactive pipes,
  13970. * since otherwise we'll complain about the garbage we read when
  13971. * e.g. coming up after runtime pm.
  13972. *
  13973. * No protection against concurrent access is required - at
  13974. * worst a fifo underrun happens which also sets this to false.
  13975. */
  13976. crtc->cpu_fifo_underrun_disabled = true;
  13977. /*
  13978. * We track the PCH trancoder underrun reporting state
  13979. * within the crtc. With crtc for pipe A housing the underrun
  13980. * reporting state for PCH transcoder A, crtc for pipe B housing
  13981. * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
  13982. * and marking underrun reporting as disabled for the non-existing
  13983. * PCH transcoders B and C would prevent enabling the south
  13984. * error interrupt (see cpt_can_enable_serr_int()).
  13985. */
  13986. if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
  13987. crtc->pch_fifo_underrun_disabled = true;
  13988. }
  13989. }
  13990. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  13991. {
  13992. struct intel_connector *connector;
  13993. /* We need to check both for a crtc link (meaning that the
  13994. * encoder is active and trying to read from a pipe) and the
  13995. * pipe itself being active. */
  13996. bool has_active_crtc = encoder->base.crtc &&
  13997. to_intel_crtc(encoder->base.crtc)->active;
  13998. connector = intel_encoder_find_connector(encoder);
  13999. if (connector && !has_active_crtc) {
  14000. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  14001. encoder->base.base.id,
  14002. encoder->base.name);
  14003. /* Connector is active, but has no active pipe. This is
  14004. * fallout from our resume register restoring. Disable
  14005. * the encoder manually again. */
  14006. if (encoder->base.crtc) {
  14007. struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
  14008. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  14009. encoder->base.base.id,
  14010. encoder->base.name);
  14011. encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  14012. if (encoder->post_disable)
  14013. encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  14014. }
  14015. encoder->base.crtc = NULL;
  14016. /* Inconsistent output/port/pipe state happens presumably due to
  14017. * a bug in one of the get_hw_state functions. Or someplace else
  14018. * in our code, like the register restore mess on resume. Clamp
  14019. * things to off as a safer default. */
  14020. connector->base.dpms = DRM_MODE_DPMS_OFF;
  14021. connector->base.encoder = NULL;
  14022. }
  14023. /* Enabled encoders without active connectors will be fixed in
  14024. * the crtc fixup. */
  14025. }
  14026. void i915_redisable_vga_power_on(struct drm_device *dev)
  14027. {
  14028. struct drm_i915_private *dev_priv = to_i915(dev);
  14029. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  14030. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  14031. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  14032. i915_disable_vga(dev);
  14033. }
  14034. }
  14035. void i915_redisable_vga(struct drm_device *dev)
  14036. {
  14037. struct drm_i915_private *dev_priv = to_i915(dev);
  14038. /* This function can be called both from intel_modeset_setup_hw_state or
  14039. * at a very early point in our resume sequence, where the power well
  14040. * structures are not yet restored. Since this function is at a very
  14041. * paranoid "someone might have enabled VGA while we were not looking"
  14042. * level, just check if the power well is enabled instead of trying to
  14043. * follow the "don't touch the power well if we don't need it" policy
  14044. * the rest of the driver uses. */
  14045. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  14046. return;
  14047. i915_redisable_vga_power_on(dev);
  14048. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  14049. }
  14050. static bool primary_get_hw_state(struct intel_plane *plane)
  14051. {
  14052. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  14053. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  14054. }
  14055. /* FIXME read out full plane state for all planes */
  14056. static void readout_plane_state(struct intel_crtc *crtc)
  14057. {
  14058. struct drm_plane *primary = crtc->base.primary;
  14059. struct intel_plane_state *plane_state =
  14060. to_intel_plane_state(primary->state);
  14061. plane_state->base.visible = crtc->active &&
  14062. primary_get_hw_state(to_intel_plane(primary));
  14063. if (plane_state->base.visible)
  14064. crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
  14065. }
  14066. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  14067. {
  14068. struct drm_i915_private *dev_priv = to_i915(dev);
  14069. enum pipe pipe;
  14070. struct intel_crtc *crtc;
  14071. struct intel_encoder *encoder;
  14072. struct intel_connector *connector;
  14073. int i;
  14074. dev_priv->active_crtcs = 0;
  14075. for_each_intel_crtc(dev, crtc) {
  14076. struct intel_crtc_state *crtc_state = crtc->config;
  14077. int pixclk = 0;
  14078. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  14079. memset(crtc_state, 0, sizeof(*crtc_state));
  14080. crtc_state->base.crtc = &crtc->base;
  14081. crtc_state->base.active = crtc_state->base.enable =
  14082. dev_priv->display.get_pipe_config(crtc, crtc_state);
  14083. crtc->base.enabled = crtc_state->base.enable;
  14084. crtc->active = crtc_state->base.active;
  14085. if (crtc_state->base.active) {
  14086. dev_priv->active_crtcs |= 1 << crtc->pipe;
  14087. if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
  14088. pixclk = ilk_pipe_pixel_rate(crtc_state);
  14089. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  14090. pixclk = crtc_state->base.adjusted_mode.crtc_clock;
  14091. else
  14092. WARN_ON(dev_priv->display.modeset_calc_cdclk);
  14093. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  14094. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  14095. pixclk = DIV_ROUND_UP(pixclk * 100, 95);
  14096. }
  14097. dev_priv->min_pixclk[crtc->pipe] = pixclk;
  14098. readout_plane_state(crtc);
  14099. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  14100. crtc->base.base.id, crtc->base.name,
  14101. crtc->active ? "enabled" : "disabled");
  14102. }
  14103. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  14104. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  14105. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  14106. &pll->config.hw_state);
  14107. pll->config.crtc_mask = 0;
  14108. for_each_intel_crtc(dev, crtc) {
  14109. if (crtc->active && crtc->config->shared_dpll == pll)
  14110. pll->config.crtc_mask |= 1 << crtc->pipe;
  14111. }
  14112. pll->active_mask = pll->config.crtc_mask;
  14113. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  14114. pll->name, pll->config.crtc_mask, pll->on);
  14115. }
  14116. for_each_intel_encoder(dev, encoder) {
  14117. pipe = 0;
  14118. if (encoder->get_hw_state(encoder, &pipe)) {
  14119. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  14120. encoder->base.crtc = &crtc->base;
  14121. crtc->config->output_types |= 1 << encoder->type;
  14122. encoder->get_config(encoder, crtc->config);
  14123. } else {
  14124. encoder->base.crtc = NULL;
  14125. }
  14126. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  14127. encoder->base.base.id,
  14128. encoder->base.name,
  14129. encoder->base.crtc ? "enabled" : "disabled",
  14130. pipe_name(pipe));
  14131. }
  14132. for_each_intel_connector(dev, connector) {
  14133. if (connector->get_hw_state(connector)) {
  14134. connector->base.dpms = DRM_MODE_DPMS_ON;
  14135. encoder = connector->encoder;
  14136. connector->base.encoder = &encoder->base;
  14137. if (encoder->base.crtc &&
  14138. encoder->base.crtc->state->active) {
  14139. /*
  14140. * This has to be done during hardware readout
  14141. * because anything calling .crtc_disable may
  14142. * rely on the connector_mask being accurate.
  14143. */
  14144. encoder->base.crtc->state->connector_mask |=
  14145. 1 << drm_connector_index(&connector->base);
  14146. encoder->base.crtc->state->encoder_mask |=
  14147. 1 << drm_encoder_index(&encoder->base);
  14148. }
  14149. } else {
  14150. connector->base.dpms = DRM_MODE_DPMS_OFF;
  14151. connector->base.encoder = NULL;
  14152. }
  14153. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  14154. connector->base.base.id,
  14155. connector->base.name,
  14156. connector->base.encoder ? "enabled" : "disabled");
  14157. }
  14158. for_each_intel_crtc(dev, crtc) {
  14159. crtc->base.hwmode = crtc->config->base.adjusted_mode;
  14160. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  14161. if (crtc->base.state->active) {
  14162. intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
  14163. intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
  14164. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  14165. /*
  14166. * The initial mode needs to be set in order to keep
  14167. * the atomic core happy. It wants a valid mode if the
  14168. * crtc's enabled, so we do the above call.
  14169. *
  14170. * At this point some state updated by the connectors
  14171. * in their ->detect() callback has not run yet, so
  14172. * no recalculation can be done yet.
  14173. *
  14174. * Even if we could do a recalculation and modeset
  14175. * right now it would cause a double modeset if
  14176. * fbdev or userspace chooses a different initial mode.
  14177. *
  14178. * If that happens, someone indicated they wanted a
  14179. * mode change, which means it's safe to do a full
  14180. * recalculation.
  14181. */
  14182. crtc->base.state->mode.private_flags = I915_MODE_FLAG_INHERITED;
  14183. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  14184. update_scanline_offset(crtc);
  14185. }
  14186. intel_pipe_config_sanity_check(dev_priv, crtc->config);
  14187. }
  14188. }
  14189. /* Scan out the current hw modeset state,
  14190. * and sanitizes it to the current state
  14191. */
  14192. static void
  14193. intel_modeset_setup_hw_state(struct drm_device *dev)
  14194. {
  14195. struct drm_i915_private *dev_priv = to_i915(dev);
  14196. enum pipe pipe;
  14197. struct intel_crtc *crtc;
  14198. struct intel_encoder *encoder;
  14199. int i;
  14200. intel_modeset_readout_hw_state(dev);
  14201. /* HW state is read out, now we need to sanitize this mess. */
  14202. for_each_intel_encoder(dev, encoder) {
  14203. intel_sanitize_encoder(encoder);
  14204. }
  14205. for_each_pipe(dev_priv, pipe) {
  14206. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  14207. intel_sanitize_crtc(crtc);
  14208. intel_dump_pipe_config(crtc, crtc->config,
  14209. "[setup_hw_state]");
  14210. }
  14211. intel_modeset_update_connector_atomic_state(dev);
  14212. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  14213. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  14214. if (!pll->on || pll->active_mask)
  14215. continue;
  14216. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  14217. pll->funcs.disable(dev_priv, pll);
  14218. pll->on = false;
  14219. }
  14220. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  14221. vlv_wm_get_hw_state(dev);
  14222. else if (IS_GEN9(dev_priv))
  14223. skl_wm_get_hw_state(dev);
  14224. else if (HAS_PCH_SPLIT(dev_priv))
  14225. ilk_wm_get_hw_state(dev);
  14226. for_each_intel_crtc(dev, crtc) {
  14227. unsigned long put_domains;
  14228. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  14229. if (WARN_ON(put_domains))
  14230. modeset_put_power_domains(dev_priv, put_domains);
  14231. }
  14232. intel_display_set_init_power(dev_priv, false);
  14233. intel_fbc_init_pipe_state(dev_priv);
  14234. }
  14235. void intel_display_resume(struct drm_device *dev)
  14236. {
  14237. struct drm_i915_private *dev_priv = to_i915(dev);
  14238. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  14239. struct drm_modeset_acquire_ctx ctx;
  14240. int ret;
  14241. dev_priv->modeset_restore_state = NULL;
  14242. if (state)
  14243. state->acquire_ctx = &ctx;
  14244. /*
  14245. * This is a cludge because with real atomic modeset mode_config.mutex
  14246. * won't be taken. Unfortunately some probed state like
  14247. * audio_codec_enable is still protected by mode_config.mutex, so lock
  14248. * it here for now.
  14249. */
  14250. mutex_lock(&dev->mode_config.mutex);
  14251. drm_modeset_acquire_init(&ctx, 0);
  14252. while (1) {
  14253. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  14254. if (ret != -EDEADLK)
  14255. break;
  14256. drm_modeset_backoff(&ctx);
  14257. }
  14258. if (!ret)
  14259. ret = __intel_display_resume(dev, state);
  14260. drm_modeset_drop_locks(&ctx);
  14261. drm_modeset_acquire_fini(&ctx);
  14262. mutex_unlock(&dev->mode_config.mutex);
  14263. if (ret)
  14264. DRM_ERROR("Restoring old state failed with %i\n", ret);
  14265. drm_atomic_state_put(state);
  14266. }
  14267. void intel_modeset_gem_init(struct drm_device *dev)
  14268. {
  14269. struct drm_i915_private *dev_priv = to_i915(dev);
  14270. struct drm_crtc *c;
  14271. struct drm_i915_gem_object *obj;
  14272. intel_init_gt_powersave(dev_priv);
  14273. intel_modeset_init_hw(dev);
  14274. intel_setup_overlay(dev_priv);
  14275. /*
  14276. * Make sure any fbs we allocated at startup are properly
  14277. * pinned & fenced. When we do the allocation it's too early
  14278. * for this.
  14279. */
  14280. for_each_crtc(dev, c) {
  14281. struct i915_vma *vma;
  14282. obj = intel_fb_obj(c->primary->fb);
  14283. if (obj == NULL)
  14284. continue;
  14285. mutex_lock(&dev->struct_mutex);
  14286. vma = intel_pin_and_fence_fb_obj(c->primary->fb,
  14287. c->primary->state->rotation);
  14288. mutex_unlock(&dev->struct_mutex);
  14289. if (IS_ERR(vma)) {
  14290. DRM_ERROR("failed to pin boot fb on pipe %d\n",
  14291. to_intel_crtc(c)->pipe);
  14292. drm_framebuffer_unreference(c->primary->fb);
  14293. c->primary->fb = NULL;
  14294. c->primary->crtc = c->primary->state->crtc = NULL;
  14295. update_state_fb(c->primary);
  14296. c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
  14297. }
  14298. }
  14299. }
  14300. int intel_connector_register(struct drm_connector *connector)
  14301. {
  14302. struct intel_connector *intel_connector = to_intel_connector(connector);
  14303. int ret;
  14304. ret = intel_backlight_device_register(intel_connector);
  14305. if (ret)
  14306. goto err;
  14307. return 0;
  14308. err:
  14309. return ret;
  14310. }
  14311. void intel_connector_unregister(struct drm_connector *connector)
  14312. {
  14313. struct intel_connector *intel_connector = to_intel_connector(connector);
  14314. intel_backlight_device_unregister(intel_connector);
  14315. intel_panel_destroy_backlight(connector);
  14316. }
  14317. void intel_modeset_cleanup(struct drm_device *dev)
  14318. {
  14319. struct drm_i915_private *dev_priv = to_i915(dev);
  14320. intel_disable_gt_powersave(dev_priv);
  14321. /*
  14322. * Interrupts and polling as the first thing to avoid creating havoc.
  14323. * Too much stuff here (turning of connectors, ...) would
  14324. * experience fancy races otherwise.
  14325. */
  14326. intel_irq_uninstall(dev_priv);
  14327. /*
  14328. * Due to the hpd irq storm handling the hotplug work can re-arm the
  14329. * poll handlers. Hence disable polling after hpd handling is shut down.
  14330. */
  14331. drm_kms_helper_poll_fini(dev);
  14332. intel_unregister_dsm_handler();
  14333. intel_fbc_global_disable(dev_priv);
  14334. /* flush any delayed tasks or pending work */
  14335. flush_scheduled_work();
  14336. drm_mode_config_cleanup(dev);
  14337. intel_cleanup_overlay(dev_priv);
  14338. intel_cleanup_gt_powersave(dev_priv);
  14339. intel_teardown_gmbus(dev);
  14340. }
  14341. void intel_connector_attach_encoder(struct intel_connector *connector,
  14342. struct intel_encoder *encoder)
  14343. {
  14344. connector->encoder = encoder;
  14345. drm_mode_connector_attach_encoder(&connector->base,
  14346. &encoder->base);
  14347. }
  14348. /*
  14349. * set vga decode state - true == enable VGA decode
  14350. */
  14351. int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
  14352. {
  14353. struct drm_i915_private *dev_priv = to_i915(dev);
  14354. unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  14355. u16 gmch_ctrl;
  14356. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  14357. DRM_ERROR("failed to read control word\n");
  14358. return -EIO;
  14359. }
  14360. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  14361. return 0;
  14362. if (state)
  14363. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  14364. else
  14365. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  14366. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  14367. DRM_ERROR("failed to write control word\n");
  14368. return -EIO;
  14369. }
  14370. return 0;
  14371. }
  14372. #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
  14373. struct intel_display_error_state {
  14374. u32 power_well_driver;
  14375. int num_transcoders;
  14376. struct intel_cursor_error_state {
  14377. u32 control;
  14378. u32 position;
  14379. u32 base;
  14380. u32 size;
  14381. } cursor[I915_MAX_PIPES];
  14382. struct intel_pipe_error_state {
  14383. bool power_domain_on;
  14384. u32 source;
  14385. u32 stat;
  14386. } pipe[I915_MAX_PIPES];
  14387. struct intel_plane_error_state {
  14388. u32 control;
  14389. u32 stride;
  14390. u32 size;
  14391. u32 pos;
  14392. u32 addr;
  14393. u32 surface;
  14394. u32 tile_offset;
  14395. } plane[I915_MAX_PIPES];
  14396. struct intel_transcoder_error_state {
  14397. bool power_domain_on;
  14398. enum transcoder cpu_transcoder;
  14399. u32 conf;
  14400. u32 htotal;
  14401. u32 hblank;
  14402. u32 hsync;
  14403. u32 vtotal;
  14404. u32 vblank;
  14405. u32 vsync;
  14406. } transcoder[4];
  14407. };
  14408. struct intel_display_error_state *
  14409. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  14410. {
  14411. struct intel_display_error_state *error;
  14412. int transcoders[] = {
  14413. TRANSCODER_A,
  14414. TRANSCODER_B,
  14415. TRANSCODER_C,
  14416. TRANSCODER_EDP,
  14417. };
  14418. int i;
  14419. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  14420. return NULL;
  14421. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  14422. if (error == NULL)
  14423. return NULL;
  14424. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  14425. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  14426. for_each_pipe(dev_priv, i) {
  14427. error->pipe[i].power_domain_on =
  14428. __intel_display_power_is_enabled(dev_priv,
  14429. POWER_DOMAIN_PIPE(i));
  14430. if (!error->pipe[i].power_domain_on)
  14431. continue;
  14432. error->cursor[i].control = I915_READ(CURCNTR(i));
  14433. error->cursor[i].position = I915_READ(CURPOS(i));
  14434. error->cursor[i].base = I915_READ(CURBASE(i));
  14435. error->plane[i].control = I915_READ(DSPCNTR(i));
  14436. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  14437. if (INTEL_GEN(dev_priv) <= 3) {
  14438. error->plane[i].size = I915_READ(DSPSIZE(i));
  14439. error->plane[i].pos = I915_READ(DSPPOS(i));
  14440. }
  14441. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  14442. error->plane[i].addr = I915_READ(DSPADDR(i));
  14443. if (INTEL_GEN(dev_priv) >= 4) {
  14444. error->plane[i].surface = I915_READ(DSPSURF(i));
  14445. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  14446. }
  14447. error->pipe[i].source = I915_READ(PIPESRC(i));
  14448. if (HAS_GMCH_DISPLAY(dev_priv))
  14449. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  14450. }
  14451. /* Note: this does not include DSI transcoders. */
  14452. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  14453. if (HAS_DDI(dev_priv))
  14454. error->num_transcoders++; /* Account for eDP. */
  14455. for (i = 0; i < error->num_transcoders; i++) {
  14456. enum transcoder cpu_transcoder = transcoders[i];
  14457. error->transcoder[i].power_domain_on =
  14458. __intel_display_power_is_enabled(dev_priv,
  14459. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  14460. if (!error->transcoder[i].power_domain_on)
  14461. continue;
  14462. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  14463. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  14464. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  14465. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  14466. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  14467. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  14468. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  14469. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  14470. }
  14471. return error;
  14472. }
  14473. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  14474. void
  14475. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  14476. struct drm_device *dev,
  14477. struct intel_display_error_state *error)
  14478. {
  14479. struct drm_i915_private *dev_priv = to_i915(dev);
  14480. int i;
  14481. if (!error)
  14482. return;
  14483. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
  14484. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  14485. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  14486. error->power_well_driver);
  14487. for_each_pipe(dev_priv, i) {
  14488. err_printf(m, "Pipe [%d]:\n", i);
  14489. err_printf(m, " Power: %s\n",
  14490. onoff(error->pipe[i].power_domain_on));
  14491. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  14492. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  14493. err_printf(m, "Plane [%d]:\n", i);
  14494. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  14495. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  14496. if (INTEL_INFO(dev)->gen <= 3) {
  14497. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  14498. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  14499. }
  14500. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  14501. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  14502. if (INTEL_INFO(dev)->gen >= 4) {
  14503. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  14504. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  14505. }
  14506. err_printf(m, "Cursor [%d]:\n", i);
  14507. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  14508. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  14509. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  14510. }
  14511. for (i = 0; i < error->num_transcoders; i++) {
  14512. err_printf(m, "CPU transcoder: %s\n",
  14513. transcoder_name(error->transcoder[i].cpu_transcoder));
  14514. err_printf(m, " Power: %s\n",
  14515. onoff(error->transcoder[i].power_domain_on));
  14516. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  14517. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  14518. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  14519. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  14520. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  14521. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  14522. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  14523. }
  14524. }
  14525. #endif