intel_display.c 488 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517
  1. /*
  2. * Copyright © 2006-2007 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. */
  26. #include <linux/dmi.h>
  27. #include <linux/module.h>
  28. #include <linux/input.h>
  29. #include <linux/i2c.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/vgaarb.h>
  33. #include <drm/drm_edid.h>
  34. #include <drm/drmP.h>
  35. #include "intel_drv.h"
  36. #include "intel_frontbuffer.h"
  37. #include <drm/i915_drm.h>
  38. #include "i915_drv.h"
  39. #include "intel_dsi.h"
  40. #include "i915_trace.h"
  41. #include <drm/drm_atomic.h>
  42. #include <drm/drm_atomic_helper.h>
  43. #include <drm/drm_dp_helper.h>
  44. #include <drm/drm_crtc_helper.h>
  45. #include <drm/drm_plane_helper.h>
  46. #include <drm/drm_rect.h>
  47. #include <linux/dma_remapping.h>
  48. #include <linux/reservation.h>
  49. static bool is_mmio_work(struct intel_flip_work *work)
  50. {
  51. return work->mmio_work.func;
  52. }
  53. /* Primary plane formats for gen <= 3 */
  54. static const uint32_t i8xx_primary_formats[] = {
  55. DRM_FORMAT_C8,
  56. DRM_FORMAT_RGB565,
  57. DRM_FORMAT_XRGB1555,
  58. DRM_FORMAT_XRGB8888,
  59. };
  60. /* Primary plane formats for gen >= 4 */
  61. static const uint32_t i965_primary_formats[] = {
  62. DRM_FORMAT_C8,
  63. DRM_FORMAT_RGB565,
  64. DRM_FORMAT_XRGB8888,
  65. DRM_FORMAT_XBGR8888,
  66. DRM_FORMAT_XRGB2101010,
  67. DRM_FORMAT_XBGR2101010,
  68. };
  69. static const uint32_t skl_primary_formats[] = {
  70. DRM_FORMAT_C8,
  71. DRM_FORMAT_RGB565,
  72. DRM_FORMAT_XRGB8888,
  73. DRM_FORMAT_XBGR8888,
  74. DRM_FORMAT_ARGB8888,
  75. DRM_FORMAT_ABGR8888,
  76. DRM_FORMAT_XRGB2101010,
  77. DRM_FORMAT_XBGR2101010,
  78. DRM_FORMAT_YUYV,
  79. DRM_FORMAT_YVYU,
  80. DRM_FORMAT_UYVY,
  81. DRM_FORMAT_VYUY,
  82. };
  83. /* Cursor formats */
  84. static const uint32_t intel_cursor_formats[] = {
  85. DRM_FORMAT_ARGB8888,
  86. };
  87. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  88. struct intel_crtc_state *pipe_config);
  89. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  90. struct intel_crtc_state *pipe_config);
  91. static int intel_framebuffer_init(struct drm_device *dev,
  92. struct intel_framebuffer *ifb,
  93. struct drm_mode_fb_cmd2 *mode_cmd,
  94. struct drm_i915_gem_object *obj);
  95. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
  96. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
  97. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc);
  98. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  99. struct intel_link_m_n *m_n,
  100. struct intel_link_m_n *m2_n2);
  101. static void ironlake_set_pipeconf(struct drm_crtc *crtc);
  102. static void haswell_set_pipeconf(struct drm_crtc *crtc);
  103. static void haswell_set_pipemisc(struct drm_crtc *crtc);
  104. static void vlv_prepare_pll(struct intel_crtc *crtc,
  105. const struct intel_crtc_state *pipe_config);
  106. static void chv_prepare_pll(struct intel_crtc *crtc,
  107. const struct intel_crtc_state *pipe_config);
  108. static void intel_begin_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  109. static void intel_finish_crtc_commit(struct drm_crtc *, struct drm_crtc_state *);
  110. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  111. struct intel_crtc_state *crtc_state);
  112. static void skylake_pfit_enable(struct intel_crtc *crtc);
  113. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force);
  114. static void ironlake_pfit_enable(struct intel_crtc *crtc);
  115. static void intel_modeset_setup_hw_state(struct drm_device *dev);
  116. static void intel_pre_disable_primary_noatomic(struct drm_crtc *crtc);
  117. static int ilk_max_pixel_rate(struct drm_atomic_state *state);
  118. static int glk_calc_cdclk(int max_pixclk);
  119. static int bxt_calc_cdclk(int max_pixclk);
  120. struct intel_limit {
  121. struct {
  122. int min, max;
  123. } dot, vco, n, m, m1, m2, p, p1;
  124. struct {
  125. int dot_limit;
  126. int p2_slow, p2_fast;
  127. } p2;
  128. };
  129. /* returns HPLL frequency in kHz */
  130. static int valleyview_get_vco(struct drm_i915_private *dev_priv)
  131. {
  132. int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
  133. /* Obtain SKU information */
  134. mutex_lock(&dev_priv->sb_lock);
  135. hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
  136. CCK_FUSE_HPLL_FREQ_MASK;
  137. mutex_unlock(&dev_priv->sb_lock);
  138. return vco_freq[hpll_freq] * 1000;
  139. }
  140. int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
  141. const char *name, u32 reg, int ref_freq)
  142. {
  143. u32 val;
  144. int divider;
  145. mutex_lock(&dev_priv->sb_lock);
  146. val = vlv_cck_read(dev_priv, reg);
  147. mutex_unlock(&dev_priv->sb_lock);
  148. divider = val & CCK_FREQUENCY_VALUES;
  149. WARN((val & CCK_FREQUENCY_STATUS) !=
  150. (divider << CCK_FREQUENCY_STATUS_SHIFT),
  151. "%s change in progress\n", name);
  152. return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
  153. }
  154. static int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
  155. const char *name, u32 reg)
  156. {
  157. if (dev_priv->hpll_freq == 0)
  158. dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
  159. return vlv_get_cck_clock(dev_priv, name, reg,
  160. dev_priv->hpll_freq);
  161. }
  162. static int
  163. intel_pch_rawclk(struct drm_i915_private *dev_priv)
  164. {
  165. return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
  166. }
  167. static int
  168. intel_vlv_hrawclk(struct drm_i915_private *dev_priv)
  169. {
  170. /* RAWCLK_FREQ_VLV register updated from power well code */
  171. return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
  172. CCK_DISPLAY_REF_CLOCK_CONTROL);
  173. }
  174. static int
  175. intel_g4x_hrawclk(struct drm_i915_private *dev_priv)
  176. {
  177. uint32_t clkcfg;
  178. /* hrawclock is 1/4 the FSB frequency */
  179. clkcfg = I915_READ(CLKCFG);
  180. switch (clkcfg & CLKCFG_FSB_MASK) {
  181. case CLKCFG_FSB_400:
  182. return 100000;
  183. case CLKCFG_FSB_533:
  184. return 133333;
  185. case CLKCFG_FSB_667:
  186. return 166667;
  187. case CLKCFG_FSB_800:
  188. return 200000;
  189. case CLKCFG_FSB_1067:
  190. return 266667;
  191. case CLKCFG_FSB_1333:
  192. return 333333;
  193. /* these two are just a guess; one of them might be right */
  194. case CLKCFG_FSB_1600:
  195. case CLKCFG_FSB_1600_ALT:
  196. return 400000;
  197. default:
  198. return 133333;
  199. }
  200. }
  201. void intel_update_rawclk(struct drm_i915_private *dev_priv)
  202. {
  203. if (HAS_PCH_SPLIT(dev_priv))
  204. dev_priv->rawclk_freq = intel_pch_rawclk(dev_priv);
  205. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  206. dev_priv->rawclk_freq = intel_vlv_hrawclk(dev_priv);
  207. else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
  208. dev_priv->rawclk_freq = intel_g4x_hrawclk(dev_priv);
  209. else
  210. return; /* no rawclk on other platforms, or no need to know it */
  211. DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
  212. }
  213. static void intel_update_czclk(struct drm_i915_private *dev_priv)
  214. {
  215. if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
  216. return;
  217. dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
  218. CCK_CZ_CLOCK_CONTROL);
  219. DRM_DEBUG_DRIVER("CZ clock rate: %d kHz\n", dev_priv->czclk_freq);
  220. }
  221. static inline u32 /* units of 100MHz */
  222. intel_fdi_link_freq(struct drm_i915_private *dev_priv,
  223. const struct intel_crtc_state *pipe_config)
  224. {
  225. if (HAS_DDI(dev_priv))
  226. return pipe_config->port_clock; /* SPLL */
  227. else if (IS_GEN5(dev_priv))
  228. return ((I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2) * 10000;
  229. else
  230. return 270000;
  231. }
  232. static const struct intel_limit intel_limits_i8xx_dac = {
  233. .dot = { .min = 25000, .max = 350000 },
  234. .vco = { .min = 908000, .max = 1512000 },
  235. .n = { .min = 2, .max = 16 },
  236. .m = { .min = 96, .max = 140 },
  237. .m1 = { .min = 18, .max = 26 },
  238. .m2 = { .min = 6, .max = 16 },
  239. .p = { .min = 4, .max = 128 },
  240. .p1 = { .min = 2, .max = 33 },
  241. .p2 = { .dot_limit = 165000,
  242. .p2_slow = 4, .p2_fast = 2 },
  243. };
  244. static const struct intel_limit intel_limits_i8xx_dvo = {
  245. .dot = { .min = 25000, .max = 350000 },
  246. .vco = { .min = 908000, .max = 1512000 },
  247. .n = { .min = 2, .max = 16 },
  248. .m = { .min = 96, .max = 140 },
  249. .m1 = { .min = 18, .max = 26 },
  250. .m2 = { .min = 6, .max = 16 },
  251. .p = { .min = 4, .max = 128 },
  252. .p1 = { .min = 2, .max = 33 },
  253. .p2 = { .dot_limit = 165000,
  254. .p2_slow = 4, .p2_fast = 4 },
  255. };
  256. static const struct intel_limit intel_limits_i8xx_lvds = {
  257. .dot = { .min = 25000, .max = 350000 },
  258. .vco = { .min = 908000, .max = 1512000 },
  259. .n = { .min = 2, .max = 16 },
  260. .m = { .min = 96, .max = 140 },
  261. .m1 = { .min = 18, .max = 26 },
  262. .m2 = { .min = 6, .max = 16 },
  263. .p = { .min = 4, .max = 128 },
  264. .p1 = { .min = 1, .max = 6 },
  265. .p2 = { .dot_limit = 165000,
  266. .p2_slow = 14, .p2_fast = 7 },
  267. };
  268. static const struct intel_limit intel_limits_i9xx_sdvo = {
  269. .dot = { .min = 20000, .max = 400000 },
  270. .vco = { .min = 1400000, .max = 2800000 },
  271. .n = { .min = 1, .max = 6 },
  272. .m = { .min = 70, .max = 120 },
  273. .m1 = { .min = 8, .max = 18 },
  274. .m2 = { .min = 3, .max = 7 },
  275. .p = { .min = 5, .max = 80 },
  276. .p1 = { .min = 1, .max = 8 },
  277. .p2 = { .dot_limit = 200000,
  278. .p2_slow = 10, .p2_fast = 5 },
  279. };
  280. static const struct intel_limit intel_limits_i9xx_lvds = {
  281. .dot = { .min = 20000, .max = 400000 },
  282. .vco = { .min = 1400000, .max = 2800000 },
  283. .n = { .min = 1, .max = 6 },
  284. .m = { .min = 70, .max = 120 },
  285. .m1 = { .min = 8, .max = 18 },
  286. .m2 = { .min = 3, .max = 7 },
  287. .p = { .min = 7, .max = 98 },
  288. .p1 = { .min = 1, .max = 8 },
  289. .p2 = { .dot_limit = 112000,
  290. .p2_slow = 14, .p2_fast = 7 },
  291. };
  292. static const struct intel_limit intel_limits_g4x_sdvo = {
  293. .dot = { .min = 25000, .max = 270000 },
  294. .vco = { .min = 1750000, .max = 3500000},
  295. .n = { .min = 1, .max = 4 },
  296. .m = { .min = 104, .max = 138 },
  297. .m1 = { .min = 17, .max = 23 },
  298. .m2 = { .min = 5, .max = 11 },
  299. .p = { .min = 10, .max = 30 },
  300. .p1 = { .min = 1, .max = 3},
  301. .p2 = { .dot_limit = 270000,
  302. .p2_slow = 10,
  303. .p2_fast = 10
  304. },
  305. };
  306. static const struct intel_limit intel_limits_g4x_hdmi = {
  307. .dot = { .min = 22000, .max = 400000 },
  308. .vco = { .min = 1750000, .max = 3500000},
  309. .n = { .min = 1, .max = 4 },
  310. .m = { .min = 104, .max = 138 },
  311. .m1 = { .min = 16, .max = 23 },
  312. .m2 = { .min = 5, .max = 11 },
  313. .p = { .min = 5, .max = 80 },
  314. .p1 = { .min = 1, .max = 8},
  315. .p2 = { .dot_limit = 165000,
  316. .p2_slow = 10, .p2_fast = 5 },
  317. };
  318. static const struct intel_limit intel_limits_g4x_single_channel_lvds = {
  319. .dot = { .min = 20000, .max = 115000 },
  320. .vco = { .min = 1750000, .max = 3500000 },
  321. .n = { .min = 1, .max = 3 },
  322. .m = { .min = 104, .max = 138 },
  323. .m1 = { .min = 17, .max = 23 },
  324. .m2 = { .min = 5, .max = 11 },
  325. .p = { .min = 28, .max = 112 },
  326. .p1 = { .min = 2, .max = 8 },
  327. .p2 = { .dot_limit = 0,
  328. .p2_slow = 14, .p2_fast = 14
  329. },
  330. };
  331. static const struct intel_limit intel_limits_g4x_dual_channel_lvds = {
  332. .dot = { .min = 80000, .max = 224000 },
  333. .vco = { .min = 1750000, .max = 3500000 },
  334. .n = { .min = 1, .max = 3 },
  335. .m = { .min = 104, .max = 138 },
  336. .m1 = { .min = 17, .max = 23 },
  337. .m2 = { .min = 5, .max = 11 },
  338. .p = { .min = 14, .max = 42 },
  339. .p1 = { .min = 2, .max = 6 },
  340. .p2 = { .dot_limit = 0,
  341. .p2_slow = 7, .p2_fast = 7
  342. },
  343. };
  344. static const struct intel_limit intel_limits_pineview_sdvo = {
  345. .dot = { .min = 20000, .max = 400000},
  346. .vco = { .min = 1700000, .max = 3500000 },
  347. /* Pineview's Ncounter is a ring counter */
  348. .n = { .min = 3, .max = 6 },
  349. .m = { .min = 2, .max = 256 },
  350. /* Pineview only has one combined m divider, which we treat as m2. */
  351. .m1 = { .min = 0, .max = 0 },
  352. .m2 = { .min = 0, .max = 254 },
  353. .p = { .min = 5, .max = 80 },
  354. .p1 = { .min = 1, .max = 8 },
  355. .p2 = { .dot_limit = 200000,
  356. .p2_slow = 10, .p2_fast = 5 },
  357. };
  358. static const struct intel_limit intel_limits_pineview_lvds = {
  359. .dot = { .min = 20000, .max = 400000 },
  360. .vco = { .min = 1700000, .max = 3500000 },
  361. .n = { .min = 3, .max = 6 },
  362. .m = { .min = 2, .max = 256 },
  363. .m1 = { .min = 0, .max = 0 },
  364. .m2 = { .min = 0, .max = 254 },
  365. .p = { .min = 7, .max = 112 },
  366. .p1 = { .min = 1, .max = 8 },
  367. .p2 = { .dot_limit = 112000,
  368. .p2_slow = 14, .p2_fast = 14 },
  369. };
  370. /* Ironlake / Sandybridge
  371. *
  372. * We calculate clock using (register_value + 2) for N/M1/M2, so here
  373. * the range value for them is (actual_value - 2).
  374. */
  375. static const struct intel_limit intel_limits_ironlake_dac = {
  376. .dot = { .min = 25000, .max = 350000 },
  377. .vco = { .min = 1760000, .max = 3510000 },
  378. .n = { .min = 1, .max = 5 },
  379. .m = { .min = 79, .max = 127 },
  380. .m1 = { .min = 12, .max = 22 },
  381. .m2 = { .min = 5, .max = 9 },
  382. .p = { .min = 5, .max = 80 },
  383. .p1 = { .min = 1, .max = 8 },
  384. .p2 = { .dot_limit = 225000,
  385. .p2_slow = 10, .p2_fast = 5 },
  386. };
  387. static const struct intel_limit intel_limits_ironlake_single_lvds = {
  388. .dot = { .min = 25000, .max = 350000 },
  389. .vco = { .min = 1760000, .max = 3510000 },
  390. .n = { .min = 1, .max = 3 },
  391. .m = { .min = 79, .max = 118 },
  392. .m1 = { .min = 12, .max = 22 },
  393. .m2 = { .min = 5, .max = 9 },
  394. .p = { .min = 28, .max = 112 },
  395. .p1 = { .min = 2, .max = 8 },
  396. .p2 = { .dot_limit = 225000,
  397. .p2_slow = 14, .p2_fast = 14 },
  398. };
  399. static const struct intel_limit intel_limits_ironlake_dual_lvds = {
  400. .dot = { .min = 25000, .max = 350000 },
  401. .vco = { .min = 1760000, .max = 3510000 },
  402. .n = { .min = 1, .max = 3 },
  403. .m = { .min = 79, .max = 127 },
  404. .m1 = { .min = 12, .max = 22 },
  405. .m2 = { .min = 5, .max = 9 },
  406. .p = { .min = 14, .max = 56 },
  407. .p1 = { .min = 2, .max = 8 },
  408. .p2 = { .dot_limit = 225000,
  409. .p2_slow = 7, .p2_fast = 7 },
  410. };
  411. /* LVDS 100mhz refclk limits. */
  412. static const struct intel_limit intel_limits_ironlake_single_lvds_100m = {
  413. .dot = { .min = 25000, .max = 350000 },
  414. .vco = { .min = 1760000, .max = 3510000 },
  415. .n = { .min = 1, .max = 2 },
  416. .m = { .min = 79, .max = 126 },
  417. .m1 = { .min = 12, .max = 22 },
  418. .m2 = { .min = 5, .max = 9 },
  419. .p = { .min = 28, .max = 112 },
  420. .p1 = { .min = 2, .max = 8 },
  421. .p2 = { .dot_limit = 225000,
  422. .p2_slow = 14, .p2_fast = 14 },
  423. };
  424. static const struct intel_limit intel_limits_ironlake_dual_lvds_100m = {
  425. .dot = { .min = 25000, .max = 350000 },
  426. .vco = { .min = 1760000, .max = 3510000 },
  427. .n = { .min = 1, .max = 3 },
  428. .m = { .min = 79, .max = 126 },
  429. .m1 = { .min = 12, .max = 22 },
  430. .m2 = { .min = 5, .max = 9 },
  431. .p = { .min = 14, .max = 42 },
  432. .p1 = { .min = 2, .max = 6 },
  433. .p2 = { .dot_limit = 225000,
  434. .p2_slow = 7, .p2_fast = 7 },
  435. };
  436. static const struct intel_limit intel_limits_vlv = {
  437. /*
  438. * These are the data rate limits (measured in fast clocks)
  439. * since those are the strictest limits we have. The fast
  440. * clock and actual rate limits are more relaxed, so checking
  441. * them would make no difference.
  442. */
  443. .dot = { .min = 25000 * 5, .max = 270000 * 5 },
  444. .vco = { .min = 4000000, .max = 6000000 },
  445. .n = { .min = 1, .max = 7 },
  446. .m1 = { .min = 2, .max = 3 },
  447. .m2 = { .min = 11, .max = 156 },
  448. .p1 = { .min = 2, .max = 3 },
  449. .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
  450. };
  451. static const struct intel_limit intel_limits_chv = {
  452. /*
  453. * These are the data rate limits (measured in fast clocks)
  454. * since those are the strictest limits we have. The fast
  455. * clock and actual rate limits are more relaxed, so checking
  456. * them would make no difference.
  457. */
  458. .dot = { .min = 25000 * 5, .max = 540000 * 5},
  459. .vco = { .min = 4800000, .max = 6480000 },
  460. .n = { .min = 1, .max = 1 },
  461. .m1 = { .min = 2, .max = 2 },
  462. .m2 = { .min = 24 << 22, .max = 175 << 22 },
  463. .p1 = { .min = 2, .max = 4 },
  464. .p2 = { .p2_slow = 1, .p2_fast = 14 },
  465. };
  466. static const struct intel_limit intel_limits_bxt = {
  467. /* FIXME: find real dot limits */
  468. .dot = { .min = 0, .max = INT_MAX },
  469. .vco = { .min = 4800000, .max = 6700000 },
  470. .n = { .min = 1, .max = 1 },
  471. .m1 = { .min = 2, .max = 2 },
  472. /* FIXME: find real m2 limits */
  473. .m2 = { .min = 2 << 22, .max = 255 << 22 },
  474. .p1 = { .min = 2, .max = 4 },
  475. .p2 = { .p2_slow = 1, .p2_fast = 20 },
  476. };
  477. static bool
  478. needs_modeset(struct drm_crtc_state *state)
  479. {
  480. return drm_atomic_crtc_needs_modeset(state);
  481. }
  482. /*
  483. * Platform specific helpers to calculate the port PLL loopback- (clock.m),
  484. * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
  485. * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
  486. * The helpers' return value is the rate of the clock that is fed to the
  487. * display engine's pipe which can be the above fast dot clock rate or a
  488. * divided-down version of it.
  489. */
  490. /* m1 is reserved as 0 in Pineview, n is a ring counter */
  491. static int pnv_calc_dpll_params(int refclk, struct dpll *clock)
  492. {
  493. clock->m = clock->m2 + 2;
  494. clock->p = clock->p1 * clock->p2;
  495. if (WARN_ON(clock->n == 0 || clock->p == 0))
  496. return 0;
  497. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  498. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  499. return clock->dot;
  500. }
  501. static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
  502. {
  503. return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
  504. }
  505. static int i9xx_calc_dpll_params(int refclk, struct dpll *clock)
  506. {
  507. clock->m = i9xx_dpll_compute_m(clock);
  508. clock->p = clock->p1 * clock->p2;
  509. if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
  510. return 0;
  511. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
  512. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  513. return clock->dot;
  514. }
  515. static int vlv_calc_dpll_params(int refclk, struct dpll *clock)
  516. {
  517. clock->m = clock->m1 * clock->m2;
  518. clock->p = clock->p1 * clock->p2;
  519. if (WARN_ON(clock->n == 0 || clock->p == 0))
  520. return 0;
  521. clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
  522. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  523. return clock->dot / 5;
  524. }
  525. int chv_calc_dpll_params(int refclk, struct dpll *clock)
  526. {
  527. clock->m = clock->m1 * clock->m2;
  528. clock->p = clock->p1 * clock->p2;
  529. if (WARN_ON(clock->n == 0 || clock->p == 0))
  530. return 0;
  531. clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
  532. clock->n << 22);
  533. clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
  534. return clock->dot / 5;
  535. }
  536. #define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
  537. /**
  538. * Returns whether the given set of divisors are valid for a given refclk with
  539. * the given connectors.
  540. */
  541. static bool intel_PLL_is_valid(struct drm_i915_private *dev_priv,
  542. const struct intel_limit *limit,
  543. const struct dpll *clock)
  544. {
  545. if (clock->n < limit->n.min || limit->n.max < clock->n)
  546. INTELPllInvalid("n out of range\n");
  547. if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
  548. INTELPllInvalid("p1 out of range\n");
  549. if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
  550. INTELPllInvalid("m2 out of range\n");
  551. if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
  552. INTELPllInvalid("m1 out of range\n");
  553. if (!IS_PINEVIEW(dev_priv) && !IS_VALLEYVIEW(dev_priv) &&
  554. !IS_CHERRYVIEW(dev_priv) && !IS_GEN9_LP(dev_priv))
  555. if (clock->m1 <= clock->m2)
  556. INTELPllInvalid("m1 <= m2\n");
  557. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  558. !IS_GEN9_LP(dev_priv)) {
  559. if (clock->p < limit->p.min || limit->p.max < clock->p)
  560. INTELPllInvalid("p out of range\n");
  561. if (clock->m < limit->m.min || limit->m.max < clock->m)
  562. INTELPllInvalid("m out of range\n");
  563. }
  564. if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
  565. INTELPllInvalid("vco out of range\n");
  566. /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
  567. * connector, etc., rather than just a single range.
  568. */
  569. if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
  570. INTELPllInvalid("dot out of range\n");
  571. return true;
  572. }
  573. static int
  574. i9xx_select_p2_div(const struct intel_limit *limit,
  575. const struct intel_crtc_state *crtc_state,
  576. int target)
  577. {
  578. struct drm_device *dev = crtc_state->base.crtc->dev;
  579. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  580. /*
  581. * For LVDS just rely on its current settings for dual-channel.
  582. * We haven't figured out how to reliably set up different
  583. * single/dual channel state, if we even can.
  584. */
  585. if (intel_is_dual_link_lvds(dev))
  586. return limit->p2.p2_fast;
  587. else
  588. return limit->p2.p2_slow;
  589. } else {
  590. if (target < limit->p2.dot_limit)
  591. return limit->p2.p2_slow;
  592. else
  593. return limit->p2.p2_fast;
  594. }
  595. }
  596. /*
  597. * Returns a set of divisors for the desired target clock with the given
  598. * refclk, or FALSE. The returned values represent the clock equation:
  599. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  600. *
  601. * Target and reference clocks are specified in kHz.
  602. *
  603. * If match_clock is provided, then best_clock P divider must match the P
  604. * divider from @match_clock used for LVDS downclocking.
  605. */
  606. static bool
  607. i9xx_find_best_dpll(const struct intel_limit *limit,
  608. struct intel_crtc_state *crtc_state,
  609. int target, int refclk, struct dpll *match_clock,
  610. struct dpll *best_clock)
  611. {
  612. struct drm_device *dev = crtc_state->base.crtc->dev;
  613. struct dpll clock;
  614. int err = target;
  615. memset(best_clock, 0, sizeof(*best_clock));
  616. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  617. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  618. clock.m1++) {
  619. for (clock.m2 = limit->m2.min;
  620. clock.m2 <= limit->m2.max; clock.m2++) {
  621. if (clock.m2 >= clock.m1)
  622. break;
  623. for (clock.n = limit->n.min;
  624. clock.n <= limit->n.max; clock.n++) {
  625. for (clock.p1 = limit->p1.min;
  626. clock.p1 <= limit->p1.max; clock.p1++) {
  627. int this_err;
  628. i9xx_calc_dpll_params(refclk, &clock);
  629. if (!intel_PLL_is_valid(to_i915(dev),
  630. limit,
  631. &clock))
  632. continue;
  633. if (match_clock &&
  634. clock.p != match_clock->p)
  635. continue;
  636. this_err = abs(clock.dot - target);
  637. if (this_err < err) {
  638. *best_clock = clock;
  639. err = this_err;
  640. }
  641. }
  642. }
  643. }
  644. }
  645. return (err != target);
  646. }
  647. /*
  648. * Returns a set of divisors for the desired target clock with the given
  649. * refclk, or FALSE. The returned values represent the clock equation:
  650. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  651. *
  652. * Target and reference clocks are specified in kHz.
  653. *
  654. * If match_clock is provided, then best_clock P divider must match the P
  655. * divider from @match_clock used for LVDS downclocking.
  656. */
  657. static bool
  658. pnv_find_best_dpll(const struct intel_limit *limit,
  659. struct intel_crtc_state *crtc_state,
  660. int target, int refclk, struct dpll *match_clock,
  661. struct dpll *best_clock)
  662. {
  663. struct drm_device *dev = crtc_state->base.crtc->dev;
  664. struct dpll clock;
  665. int err = target;
  666. memset(best_clock, 0, sizeof(*best_clock));
  667. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  668. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
  669. clock.m1++) {
  670. for (clock.m2 = limit->m2.min;
  671. clock.m2 <= limit->m2.max; clock.m2++) {
  672. for (clock.n = limit->n.min;
  673. clock.n <= limit->n.max; clock.n++) {
  674. for (clock.p1 = limit->p1.min;
  675. clock.p1 <= limit->p1.max; clock.p1++) {
  676. int this_err;
  677. pnv_calc_dpll_params(refclk, &clock);
  678. if (!intel_PLL_is_valid(to_i915(dev),
  679. limit,
  680. &clock))
  681. continue;
  682. if (match_clock &&
  683. clock.p != match_clock->p)
  684. continue;
  685. this_err = abs(clock.dot - target);
  686. if (this_err < err) {
  687. *best_clock = clock;
  688. err = this_err;
  689. }
  690. }
  691. }
  692. }
  693. }
  694. return (err != target);
  695. }
  696. /*
  697. * Returns a set of divisors for the desired target clock with the given
  698. * refclk, or FALSE. The returned values represent the clock equation:
  699. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  700. *
  701. * Target and reference clocks are specified in kHz.
  702. *
  703. * If match_clock is provided, then best_clock P divider must match the P
  704. * divider from @match_clock used for LVDS downclocking.
  705. */
  706. static bool
  707. g4x_find_best_dpll(const struct intel_limit *limit,
  708. struct intel_crtc_state *crtc_state,
  709. int target, int refclk, struct dpll *match_clock,
  710. struct dpll *best_clock)
  711. {
  712. struct drm_device *dev = crtc_state->base.crtc->dev;
  713. struct dpll clock;
  714. int max_n;
  715. bool found = false;
  716. /* approximately equals target * 0.00585 */
  717. int err_most = (target >> 8) + (target >> 9);
  718. memset(best_clock, 0, sizeof(*best_clock));
  719. clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
  720. max_n = limit->n.max;
  721. /* based on hardware requirement, prefer smaller n to precision */
  722. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  723. /* based on hardware requirement, prefere larger m1,m2 */
  724. for (clock.m1 = limit->m1.max;
  725. clock.m1 >= limit->m1.min; clock.m1--) {
  726. for (clock.m2 = limit->m2.max;
  727. clock.m2 >= limit->m2.min; clock.m2--) {
  728. for (clock.p1 = limit->p1.max;
  729. clock.p1 >= limit->p1.min; clock.p1--) {
  730. int this_err;
  731. i9xx_calc_dpll_params(refclk, &clock);
  732. if (!intel_PLL_is_valid(to_i915(dev),
  733. limit,
  734. &clock))
  735. continue;
  736. this_err = abs(clock.dot - target);
  737. if (this_err < err_most) {
  738. *best_clock = clock;
  739. err_most = this_err;
  740. max_n = clock.n;
  741. found = true;
  742. }
  743. }
  744. }
  745. }
  746. }
  747. return found;
  748. }
  749. /*
  750. * Check if the calculated PLL configuration is more optimal compared to the
  751. * best configuration and error found so far. Return the calculated error.
  752. */
  753. static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
  754. const struct dpll *calculated_clock,
  755. const struct dpll *best_clock,
  756. unsigned int best_error_ppm,
  757. unsigned int *error_ppm)
  758. {
  759. /*
  760. * For CHV ignore the error and consider only the P value.
  761. * Prefer a bigger P value based on HW requirements.
  762. */
  763. if (IS_CHERRYVIEW(to_i915(dev))) {
  764. *error_ppm = 0;
  765. return calculated_clock->p > best_clock->p;
  766. }
  767. if (WARN_ON_ONCE(!target_freq))
  768. return false;
  769. *error_ppm = div_u64(1000000ULL *
  770. abs(target_freq - calculated_clock->dot),
  771. target_freq);
  772. /*
  773. * Prefer a better P value over a better (smaller) error if the error
  774. * is small. Ensure this preference for future configurations too by
  775. * setting the error to 0.
  776. */
  777. if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
  778. *error_ppm = 0;
  779. return true;
  780. }
  781. return *error_ppm + 10 < best_error_ppm;
  782. }
  783. /*
  784. * Returns a set of divisors for the desired target clock with the given
  785. * refclk, or FALSE. The returned values represent the clock equation:
  786. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  787. */
  788. static bool
  789. vlv_find_best_dpll(const struct intel_limit *limit,
  790. struct intel_crtc_state *crtc_state,
  791. int target, int refclk, struct dpll *match_clock,
  792. struct dpll *best_clock)
  793. {
  794. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  795. struct drm_device *dev = crtc->base.dev;
  796. struct dpll clock;
  797. unsigned int bestppm = 1000000;
  798. /* min update 19.2 MHz */
  799. int max_n = min(limit->n.max, refclk / 19200);
  800. bool found = false;
  801. target *= 5; /* fast clock */
  802. memset(best_clock, 0, sizeof(*best_clock));
  803. /* based on hardware requirement, prefer smaller n to precision */
  804. for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
  805. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  806. for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
  807. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  808. clock.p = clock.p1 * clock.p2;
  809. /* based on hardware requirement, prefer bigger m1,m2 values */
  810. for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
  811. unsigned int ppm;
  812. clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
  813. refclk * clock.m1);
  814. vlv_calc_dpll_params(refclk, &clock);
  815. if (!intel_PLL_is_valid(to_i915(dev),
  816. limit,
  817. &clock))
  818. continue;
  819. if (!vlv_PLL_is_optimal(dev, target,
  820. &clock,
  821. best_clock,
  822. bestppm, &ppm))
  823. continue;
  824. *best_clock = clock;
  825. bestppm = ppm;
  826. found = true;
  827. }
  828. }
  829. }
  830. }
  831. return found;
  832. }
  833. /*
  834. * Returns a set of divisors for the desired target clock with the given
  835. * refclk, or FALSE. The returned values represent the clock equation:
  836. * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
  837. */
  838. static bool
  839. chv_find_best_dpll(const struct intel_limit *limit,
  840. struct intel_crtc_state *crtc_state,
  841. int target, int refclk, struct dpll *match_clock,
  842. struct dpll *best_clock)
  843. {
  844. struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
  845. struct drm_device *dev = crtc->base.dev;
  846. unsigned int best_error_ppm;
  847. struct dpll clock;
  848. uint64_t m2;
  849. int found = false;
  850. memset(best_clock, 0, sizeof(*best_clock));
  851. best_error_ppm = 1000000;
  852. /*
  853. * Based on hardware doc, the n always set to 1, and m1 always
  854. * set to 2. If requires to support 200Mhz refclk, we need to
  855. * revisit this because n may not 1 anymore.
  856. */
  857. clock.n = 1, clock.m1 = 2;
  858. target *= 5; /* fast clock */
  859. for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
  860. for (clock.p2 = limit->p2.p2_fast;
  861. clock.p2 >= limit->p2.p2_slow;
  862. clock.p2 -= clock.p2 > 10 ? 2 : 1) {
  863. unsigned int error_ppm;
  864. clock.p = clock.p1 * clock.p2;
  865. m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
  866. clock.n) << 22, refclk * clock.m1);
  867. if (m2 > INT_MAX/clock.m1)
  868. continue;
  869. clock.m2 = m2;
  870. chv_calc_dpll_params(refclk, &clock);
  871. if (!intel_PLL_is_valid(to_i915(dev), limit, &clock))
  872. continue;
  873. if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
  874. best_error_ppm, &error_ppm))
  875. continue;
  876. *best_clock = clock;
  877. best_error_ppm = error_ppm;
  878. found = true;
  879. }
  880. }
  881. return found;
  882. }
  883. bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
  884. struct dpll *best_clock)
  885. {
  886. int refclk = 100000;
  887. const struct intel_limit *limit = &intel_limits_bxt;
  888. return chv_find_best_dpll(limit, crtc_state,
  889. target_clock, refclk, NULL, best_clock);
  890. }
  891. bool intel_crtc_active(struct intel_crtc *crtc)
  892. {
  893. /* Be paranoid as we can arrive here with only partial
  894. * state retrieved from the hardware during setup.
  895. *
  896. * We can ditch the adjusted_mode.crtc_clock check as soon
  897. * as Haswell has gained clock readout/fastboot support.
  898. *
  899. * We can ditch the crtc->primary->fb check as soon as we can
  900. * properly reconstruct framebuffers.
  901. *
  902. * FIXME: The intel_crtc->active here should be switched to
  903. * crtc->state->active once we have proper CRTC states wired up
  904. * for atomic.
  905. */
  906. return crtc->active && crtc->base.primary->state->fb &&
  907. crtc->config->base.adjusted_mode.crtc_clock;
  908. }
  909. enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
  910. enum pipe pipe)
  911. {
  912. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  913. return crtc->config->cpu_transcoder;
  914. }
  915. static bool pipe_dsl_stopped(struct drm_i915_private *dev_priv, enum pipe pipe)
  916. {
  917. i915_reg_t reg = PIPEDSL(pipe);
  918. u32 line1, line2;
  919. u32 line_mask;
  920. if (IS_GEN2(dev_priv))
  921. line_mask = DSL_LINEMASK_GEN2;
  922. else
  923. line_mask = DSL_LINEMASK_GEN3;
  924. line1 = I915_READ(reg) & line_mask;
  925. msleep(5);
  926. line2 = I915_READ(reg) & line_mask;
  927. return line1 == line2;
  928. }
  929. /*
  930. * intel_wait_for_pipe_off - wait for pipe to turn off
  931. * @crtc: crtc whose pipe to wait for
  932. *
  933. * After disabling a pipe, we can't wait for vblank in the usual way,
  934. * spinning on the vblank interrupt status bit, since we won't actually
  935. * see an interrupt when the pipe is disabled.
  936. *
  937. * On Gen4 and above:
  938. * wait for the pipe register state bit to turn off
  939. *
  940. * Otherwise:
  941. * wait for the display line value to settle (it usually
  942. * ends up stopping at the start of the next frame).
  943. *
  944. */
  945. static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
  946. {
  947. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  948. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  949. enum pipe pipe = crtc->pipe;
  950. if (INTEL_GEN(dev_priv) >= 4) {
  951. i915_reg_t reg = PIPECONF(cpu_transcoder);
  952. /* Wait for the Pipe State to go off */
  953. if (intel_wait_for_register(dev_priv,
  954. reg, I965_PIPECONF_ACTIVE, 0,
  955. 100))
  956. WARN(1, "pipe_off wait timed out\n");
  957. } else {
  958. /* Wait for the display line to settle */
  959. if (wait_for(pipe_dsl_stopped(dev_priv, pipe), 100))
  960. WARN(1, "pipe_off wait timed out\n");
  961. }
  962. }
  963. /* Only for pre-ILK configs */
  964. void assert_pll(struct drm_i915_private *dev_priv,
  965. enum pipe pipe, bool state)
  966. {
  967. u32 val;
  968. bool cur_state;
  969. val = I915_READ(DPLL(pipe));
  970. cur_state = !!(val & DPLL_VCO_ENABLE);
  971. I915_STATE_WARN(cur_state != state,
  972. "PLL state assertion failure (expected %s, current %s)\n",
  973. onoff(state), onoff(cur_state));
  974. }
  975. /* XXX: the dsi pll is shared between MIPI DSI ports */
  976. void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
  977. {
  978. u32 val;
  979. bool cur_state;
  980. mutex_lock(&dev_priv->sb_lock);
  981. val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
  982. mutex_unlock(&dev_priv->sb_lock);
  983. cur_state = val & DSI_PLL_VCO_EN;
  984. I915_STATE_WARN(cur_state != state,
  985. "DSI PLL state assertion failure (expected %s, current %s)\n",
  986. onoff(state), onoff(cur_state));
  987. }
  988. static void assert_fdi_tx(struct drm_i915_private *dev_priv,
  989. enum pipe pipe, bool state)
  990. {
  991. bool cur_state;
  992. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  993. pipe);
  994. if (HAS_DDI(dev_priv)) {
  995. /* DDI does not have a specific FDI_TX register */
  996. u32 val = I915_READ(TRANS_DDI_FUNC_CTL(cpu_transcoder));
  997. cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
  998. } else {
  999. u32 val = I915_READ(FDI_TX_CTL(pipe));
  1000. cur_state = !!(val & FDI_TX_ENABLE);
  1001. }
  1002. I915_STATE_WARN(cur_state != state,
  1003. "FDI TX state assertion failure (expected %s, current %s)\n",
  1004. onoff(state), onoff(cur_state));
  1005. }
  1006. #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
  1007. #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
  1008. static void assert_fdi_rx(struct drm_i915_private *dev_priv,
  1009. enum pipe pipe, bool state)
  1010. {
  1011. u32 val;
  1012. bool cur_state;
  1013. val = I915_READ(FDI_RX_CTL(pipe));
  1014. cur_state = !!(val & FDI_RX_ENABLE);
  1015. I915_STATE_WARN(cur_state != state,
  1016. "FDI RX state assertion failure (expected %s, current %s)\n",
  1017. onoff(state), onoff(cur_state));
  1018. }
  1019. #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
  1020. #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
  1021. static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
  1022. enum pipe pipe)
  1023. {
  1024. u32 val;
  1025. /* ILK FDI PLL is always enabled */
  1026. if (IS_GEN5(dev_priv))
  1027. return;
  1028. /* On Haswell, DDI ports are responsible for the FDI PLL setup */
  1029. if (HAS_DDI(dev_priv))
  1030. return;
  1031. val = I915_READ(FDI_TX_CTL(pipe));
  1032. I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
  1033. }
  1034. void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
  1035. enum pipe pipe, bool state)
  1036. {
  1037. u32 val;
  1038. bool cur_state;
  1039. val = I915_READ(FDI_RX_CTL(pipe));
  1040. cur_state = !!(val & FDI_RX_PLL_ENABLE);
  1041. I915_STATE_WARN(cur_state != state,
  1042. "FDI RX PLL assertion failure (expected %s, current %s)\n",
  1043. onoff(state), onoff(cur_state));
  1044. }
  1045. void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
  1046. {
  1047. i915_reg_t pp_reg;
  1048. u32 val;
  1049. enum pipe panel_pipe = PIPE_A;
  1050. bool locked = true;
  1051. if (WARN_ON(HAS_DDI(dev_priv)))
  1052. return;
  1053. if (HAS_PCH_SPLIT(dev_priv)) {
  1054. u32 port_sel;
  1055. pp_reg = PP_CONTROL(0);
  1056. port_sel = I915_READ(PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
  1057. if (port_sel == PANEL_PORT_SELECT_LVDS &&
  1058. I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
  1059. panel_pipe = PIPE_B;
  1060. /* XXX: else fix for eDP */
  1061. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1062. /* presumably write lock depends on pipe, not port select */
  1063. pp_reg = PP_CONTROL(pipe);
  1064. panel_pipe = pipe;
  1065. } else {
  1066. pp_reg = PP_CONTROL(0);
  1067. if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
  1068. panel_pipe = PIPE_B;
  1069. }
  1070. val = I915_READ(pp_reg);
  1071. if (!(val & PANEL_POWER_ON) ||
  1072. ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
  1073. locked = false;
  1074. I915_STATE_WARN(panel_pipe == pipe && locked,
  1075. "panel assertion failure, pipe %c regs locked\n",
  1076. pipe_name(pipe));
  1077. }
  1078. static void assert_cursor(struct drm_i915_private *dev_priv,
  1079. enum pipe pipe, bool state)
  1080. {
  1081. bool cur_state;
  1082. if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  1083. cur_state = I915_READ(CURCNTR(PIPE_A)) & CURSOR_ENABLE;
  1084. else
  1085. cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
  1086. I915_STATE_WARN(cur_state != state,
  1087. "cursor on pipe %c assertion failure (expected %s, current %s)\n",
  1088. pipe_name(pipe), onoff(state), onoff(cur_state));
  1089. }
  1090. #define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
  1091. #define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
  1092. void assert_pipe(struct drm_i915_private *dev_priv,
  1093. enum pipe pipe, bool state)
  1094. {
  1095. bool cur_state;
  1096. enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
  1097. pipe);
  1098. enum intel_display_power_domain power_domain;
  1099. /* if we need the pipe quirk it must be always on */
  1100. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1101. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1102. state = true;
  1103. power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
  1104. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  1105. u32 val = I915_READ(PIPECONF(cpu_transcoder));
  1106. cur_state = !!(val & PIPECONF_ENABLE);
  1107. intel_display_power_put(dev_priv, power_domain);
  1108. } else {
  1109. cur_state = false;
  1110. }
  1111. I915_STATE_WARN(cur_state != state,
  1112. "pipe %c assertion failure (expected %s, current %s)\n",
  1113. pipe_name(pipe), onoff(state), onoff(cur_state));
  1114. }
  1115. static void assert_plane(struct drm_i915_private *dev_priv,
  1116. enum plane plane, bool state)
  1117. {
  1118. u32 val;
  1119. bool cur_state;
  1120. val = I915_READ(DSPCNTR(plane));
  1121. cur_state = !!(val & DISPLAY_PLANE_ENABLE);
  1122. I915_STATE_WARN(cur_state != state,
  1123. "plane %c assertion failure (expected %s, current %s)\n",
  1124. plane_name(plane), onoff(state), onoff(cur_state));
  1125. }
  1126. #define assert_plane_enabled(d, p) assert_plane(d, p, true)
  1127. #define assert_plane_disabled(d, p) assert_plane(d, p, false)
  1128. static void assert_planes_disabled(struct drm_i915_private *dev_priv,
  1129. enum pipe pipe)
  1130. {
  1131. int i;
  1132. /* Primary planes are fixed to pipes on gen4+ */
  1133. if (INTEL_GEN(dev_priv) >= 4) {
  1134. u32 val = I915_READ(DSPCNTR(pipe));
  1135. I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
  1136. "plane %c assertion failure, should be disabled but not\n",
  1137. plane_name(pipe));
  1138. return;
  1139. }
  1140. /* Need to check both planes against the pipe */
  1141. for_each_pipe(dev_priv, i) {
  1142. u32 val = I915_READ(DSPCNTR(i));
  1143. enum pipe cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
  1144. DISPPLANE_SEL_PIPE_SHIFT;
  1145. I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
  1146. "plane %c assertion failure, should be off on pipe %c but is still active\n",
  1147. plane_name(i), pipe_name(pipe));
  1148. }
  1149. }
  1150. static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
  1151. enum pipe pipe)
  1152. {
  1153. int sprite;
  1154. if (INTEL_GEN(dev_priv) >= 9) {
  1155. for_each_sprite(dev_priv, pipe, sprite) {
  1156. u32 val = I915_READ(PLANE_CTL(pipe, sprite));
  1157. I915_STATE_WARN(val & PLANE_CTL_ENABLE,
  1158. "plane %d assertion failure, should be off on pipe %c but is still active\n",
  1159. sprite, pipe_name(pipe));
  1160. }
  1161. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  1162. for_each_sprite(dev_priv, pipe, sprite) {
  1163. u32 val = I915_READ(SPCNTR(pipe, PLANE_SPRITE0 + sprite));
  1164. I915_STATE_WARN(val & SP_ENABLE,
  1165. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1166. sprite_name(pipe, sprite), pipe_name(pipe));
  1167. }
  1168. } else if (INTEL_GEN(dev_priv) >= 7) {
  1169. u32 val = I915_READ(SPRCTL(pipe));
  1170. I915_STATE_WARN(val & SPRITE_ENABLE,
  1171. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1172. plane_name(pipe), pipe_name(pipe));
  1173. } else if (INTEL_GEN(dev_priv) >= 5) {
  1174. u32 val = I915_READ(DVSCNTR(pipe));
  1175. I915_STATE_WARN(val & DVS_ENABLE,
  1176. "sprite %c assertion failure, should be off on pipe %c but is still active\n",
  1177. plane_name(pipe), pipe_name(pipe));
  1178. }
  1179. }
  1180. static void assert_vblank_disabled(struct drm_crtc *crtc)
  1181. {
  1182. if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
  1183. drm_crtc_vblank_put(crtc);
  1184. }
  1185. void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
  1186. enum pipe pipe)
  1187. {
  1188. u32 val;
  1189. bool enabled;
  1190. val = I915_READ(PCH_TRANSCONF(pipe));
  1191. enabled = !!(val & TRANS_ENABLE);
  1192. I915_STATE_WARN(enabled,
  1193. "transcoder assertion failed, should be off on pipe %c but is still active\n",
  1194. pipe_name(pipe));
  1195. }
  1196. static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
  1197. enum pipe pipe, u32 port_sel, u32 val)
  1198. {
  1199. if ((val & DP_PORT_EN) == 0)
  1200. return false;
  1201. if (HAS_PCH_CPT(dev_priv)) {
  1202. u32 trans_dp_ctl = I915_READ(TRANS_DP_CTL(pipe));
  1203. if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
  1204. return false;
  1205. } else if (IS_CHERRYVIEW(dev_priv)) {
  1206. if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
  1207. return false;
  1208. } else {
  1209. if ((val & DP_PIPE_MASK) != (pipe << 30))
  1210. return false;
  1211. }
  1212. return true;
  1213. }
  1214. static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
  1215. enum pipe pipe, u32 val)
  1216. {
  1217. if ((val & SDVO_ENABLE) == 0)
  1218. return false;
  1219. if (HAS_PCH_CPT(dev_priv)) {
  1220. if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
  1221. return false;
  1222. } else if (IS_CHERRYVIEW(dev_priv)) {
  1223. if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
  1224. return false;
  1225. } else {
  1226. if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
  1227. return false;
  1228. }
  1229. return true;
  1230. }
  1231. static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
  1232. enum pipe pipe, u32 val)
  1233. {
  1234. if ((val & LVDS_PORT_EN) == 0)
  1235. return false;
  1236. if (HAS_PCH_CPT(dev_priv)) {
  1237. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1238. return false;
  1239. } else {
  1240. if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
  1241. return false;
  1242. }
  1243. return true;
  1244. }
  1245. static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
  1246. enum pipe pipe, u32 val)
  1247. {
  1248. if ((val & ADPA_DAC_ENABLE) == 0)
  1249. return false;
  1250. if (HAS_PCH_CPT(dev_priv)) {
  1251. if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
  1252. return false;
  1253. } else {
  1254. if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
  1255. return false;
  1256. }
  1257. return true;
  1258. }
  1259. static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
  1260. enum pipe pipe, i915_reg_t reg,
  1261. u32 port_sel)
  1262. {
  1263. u32 val = I915_READ(reg);
  1264. I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
  1265. "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
  1266. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1267. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & DP_PORT_EN) == 0
  1268. && (val & DP_PIPEB_SELECT),
  1269. "IBX PCH dp port still using transcoder B\n");
  1270. }
  1271. static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
  1272. enum pipe pipe, i915_reg_t reg)
  1273. {
  1274. u32 val = I915_READ(reg);
  1275. I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
  1276. "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
  1277. i915_mmio_reg_offset(reg), pipe_name(pipe));
  1278. I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && (val & SDVO_ENABLE) == 0
  1279. && (val & SDVO_PIPE_B_SELECT),
  1280. "IBX PCH hdmi port still using transcoder B\n");
  1281. }
  1282. static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
  1283. enum pipe pipe)
  1284. {
  1285. u32 val;
  1286. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
  1287. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
  1288. assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
  1289. val = I915_READ(PCH_ADPA);
  1290. I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
  1291. "PCH VGA enabled on transcoder %c, should be disabled\n",
  1292. pipe_name(pipe));
  1293. val = I915_READ(PCH_LVDS);
  1294. I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
  1295. "PCH LVDS enabled on transcoder %c, should be disabled\n",
  1296. pipe_name(pipe));
  1297. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
  1298. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
  1299. assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
  1300. }
  1301. static void _vlv_enable_pll(struct intel_crtc *crtc,
  1302. const struct intel_crtc_state *pipe_config)
  1303. {
  1304. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1305. enum pipe pipe = crtc->pipe;
  1306. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1307. POSTING_READ(DPLL(pipe));
  1308. udelay(150);
  1309. if (intel_wait_for_register(dev_priv,
  1310. DPLL(pipe),
  1311. DPLL_LOCK_VLV,
  1312. DPLL_LOCK_VLV,
  1313. 1))
  1314. DRM_ERROR("DPLL %d failed to lock\n", pipe);
  1315. }
  1316. static void vlv_enable_pll(struct intel_crtc *crtc,
  1317. const struct intel_crtc_state *pipe_config)
  1318. {
  1319. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1320. enum pipe pipe = crtc->pipe;
  1321. assert_pipe_disabled(dev_priv, pipe);
  1322. /* PLL is protected by panel, make sure we can write it */
  1323. assert_panel_unlocked(dev_priv, pipe);
  1324. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1325. _vlv_enable_pll(crtc, pipe_config);
  1326. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1327. POSTING_READ(DPLL_MD(pipe));
  1328. }
  1329. static void _chv_enable_pll(struct intel_crtc *crtc,
  1330. const struct intel_crtc_state *pipe_config)
  1331. {
  1332. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1333. enum pipe pipe = crtc->pipe;
  1334. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1335. u32 tmp;
  1336. mutex_lock(&dev_priv->sb_lock);
  1337. /* Enable back the 10bit clock to display controller */
  1338. tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1339. tmp |= DPIO_DCLKP_EN;
  1340. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
  1341. mutex_unlock(&dev_priv->sb_lock);
  1342. /*
  1343. * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
  1344. */
  1345. udelay(1);
  1346. /* Enable PLL */
  1347. I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
  1348. /* Check PLL is locked */
  1349. if (intel_wait_for_register(dev_priv,
  1350. DPLL(pipe), DPLL_LOCK_VLV, DPLL_LOCK_VLV,
  1351. 1))
  1352. DRM_ERROR("PLL %d failed to lock\n", pipe);
  1353. }
  1354. static void chv_enable_pll(struct intel_crtc *crtc,
  1355. const struct intel_crtc_state *pipe_config)
  1356. {
  1357. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1358. enum pipe pipe = crtc->pipe;
  1359. assert_pipe_disabled(dev_priv, pipe);
  1360. /* PLL is protected by panel, make sure we can write it */
  1361. assert_panel_unlocked(dev_priv, pipe);
  1362. if (pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE)
  1363. _chv_enable_pll(crtc, pipe_config);
  1364. if (pipe != PIPE_A) {
  1365. /*
  1366. * WaPixelRepeatModeFixForC0:chv
  1367. *
  1368. * DPLLCMD is AWOL. Use chicken bits to propagate
  1369. * the value from DPLLBMD to either pipe B or C.
  1370. */
  1371. I915_WRITE(CBR4_VLV, pipe == PIPE_B ? CBR_DPLLBMD_PIPE_B : CBR_DPLLBMD_PIPE_C);
  1372. I915_WRITE(DPLL_MD(PIPE_B), pipe_config->dpll_hw_state.dpll_md);
  1373. I915_WRITE(CBR4_VLV, 0);
  1374. dev_priv->chv_dpll_md[pipe] = pipe_config->dpll_hw_state.dpll_md;
  1375. /*
  1376. * DPLLB VGA mode also seems to cause problems.
  1377. * We should always have it disabled.
  1378. */
  1379. WARN_ON((I915_READ(DPLL(PIPE_B)) & DPLL_VGA_MODE_DIS) == 0);
  1380. } else {
  1381. I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
  1382. POSTING_READ(DPLL_MD(pipe));
  1383. }
  1384. }
  1385. static int intel_num_dvo_pipes(struct drm_i915_private *dev_priv)
  1386. {
  1387. struct intel_crtc *crtc;
  1388. int count = 0;
  1389. for_each_intel_crtc(&dev_priv->drm, crtc) {
  1390. count += crtc->base.state->active &&
  1391. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO);
  1392. }
  1393. return count;
  1394. }
  1395. static void i9xx_enable_pll(struct intel_crtc *crtc)
  1396. {
  1397. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1398. i915_reg_t reg = DPLL(crtc->pipe);
  1399. u32 dpll = crtc->config->dpll_hw_state.dpll;
  1400. assert_pipe_disabled(dev_priv, crtc->pipe);
  1401. /* PLL is protected by panel, make sure we can write it */
  1402. if (IS_MOBILE(dev_priv) && !IS_I830(dev_priv))
  1403. assert_panel_unlocked(dev_priv, crtc->pipe);
  1404. /* Enable DVO 2x clock on both PLLs if necessary */
  1405. if (IS_I830(dev_priv) && intel_num_dvo_pipes(dev_priv) > 0) {
  1406. /*
  1407. * It appears to be important that we don't enable this
  1408. * for the current pipe before otherwise configuring the
  1409. * PLL. No idea how this should be handled if multiple
  1410. * DVO outputs are enabled simultaneosly.
  1411. */
  1412. dpll |= DPLL_DVO_2X_MODE;
  1413. I915_WRITE(DPLL(!crtc->pipe),
  1414. I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
  1415. }
  1416. /*
  1417. * Apparently we need to have VGA mode enabled prior to changing
  1418. * the P1/P2 dividers. Otherwise the DPLL will keep using the old
  1419. * dividers, even though the register value does change.
  1420. */
  1421. I915_WRITE(reg, 0);
  1422. I915_WRITE(reg, dpll);
  1423. /* Wait for the clocks to stabilize. */
  1424. POSTING_READ(reg);
  1425. udelay(150);
  1426. if (INTEL_GEN(dev_priv) >= 4) {
  1427. I915_WRITE(DPLL_MD(crtc->pipe),
  1428. crtc->config->dpll_hw_state.dpll_md);
  1429. } else {
  1430. /* The pixel multiplier can only be updated once the
  1431. * DPLL is enabled and the clocks are stable.
  1432. *
  1433. * So write it again.
  1434. */
  1435. I915_WRITE(reg, dpll);
  1436. }
  1437. /* We do this three times for luck */
  1438. I915_WRITE(reg, dpll);
  1439. POSTING_READ(reg);
  1440. udelay(150); /* wait for warmup */
  1441. I915_WRITE(reg, dpll);
  1442. POSTING_READ(reg);
  1443. udelay(150); /* wait for warmup */
  1444. I915_WRITE(reg, dpll);
  1445. POSTING_READ(reg);
  1446. udelay(150); /* wait for warmup */
  1447. }
  1448. /**
  1449. * i9xx_disable_pll - disable a PLL
  1450. * @dev_priv: i915 private structure
  1451. * @pipe: pipe PLL to disable
  1452. *
  1453. * Disable the PLL for @pipe, making sure the pipe is off first.
  1454. *
  1455. * Note! This is for pre-ILK only.
  1456. */
  1457. static void i9xx_disable_pll(struct intel_crtc *crtc)
  1458. {
  1459. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1460. enum pipe pipe = crtc->pipe;
  1461. /* Disable DVO 2x clock on both PLLs if necessary */
  1462. if (IS_I830(dev_priv) &&
  1463. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DVO) &&
  1464. !intel_num_dvo_pipes(dev_priv)) {
  1465. I915_WRITE(DPLL(PIPE_B),
  1466. I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
  1467. I915_WRITE(DPLL(PIPE_A),
  1468. I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
  1469. }
  1470. /* Don't disable pipe or pipe PLLs if needed */
  1471. if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1472. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1473. return;
  1474. /* Make sure the pipe isn't still relying on us */
  1475. assert_pipe_disabled(dev_priv, pipe);
  1476. I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
  1477. POSTING_READ(DPLL(pipe));
  1478. }
  1479. static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1480. {
  1481. u32 val;
  1482. /* Make sure the pipe isn't still relying on us */
  1483. assert_pipe_disabled(dev_priv, pipe);
  1484. val = DPLL_INTEGRATED_REF_CLK_VLV |
  1485. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1486. if (pipe != PIPE_A)
  1487. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1488. I915_WRITE(DPLL(pipe), val);
  1489. POSTING_READ(DPLL(pipe));
  1490. }
  1491. static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
  1492. {
  1493. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  1494. u32 val;
  1495. /* Make sure the pipe isn't still relying on us */
  1496. assert_pipe_disabled(dev_priv, pipe);
  1497. val = DPLL_SSC_REF_CLK_CHV |
  1498. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  1499. if (pipe != PIPE_A)
  1500. val |= DPLL_INTEGRATED_CRI_CLK_VLV;
  1501. I915_WRITE(DPLL(pipe), val);
  1502. POSTING_READ(DPLL(pipe));
  1503. mutex_lock(&dev_priv->sb_lock);
  1504. /* Disable 10bit clock to display controller */
  1505. val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
  1506. val &= ~DPIO_DCLKP_EN;
  1507. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
  1508. mutex_unlock(&dev_priv->sb_lock);
  1509. }
  1510. void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
  1511. struct intel_digital_port *dport,
  1512. unsigned int expected_mask)
  1513. {
  1514. u32 port_mask;
  1515. i915_reg_t dpll_reg;
  1516. switch (dport->port) {
  1517. case PORT_B:
  1518. port_mask = DPLL_PORTB_READY_MASK;
  1519. dpll_reg = DPLL(0);
  1520. break;
  1521. case PORT_C:
  1522. port_mask = DPLL_PORTC_READY_MASK;
  1523. dpll_reg = DPLL(0);
  1524. expected_mask <<= 4;
  1525. break;
  1526. case PORT_D:
  1527. port_mask = DPLL_PORTD_READY_MASK;
  1528. dpll_reg = DPIO_PHY_STATUS;
  1529. break;
  1530. default:
  1531. BUG();
  1532. }
  1533. if (intel_wait_for_register(dev_priv,
  1534. dpll_reg, port_mask, expected_mask,
  1535. 1000))
  1536. WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
  1537. port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
  1538. }
  1539. static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1540. enum pipe pipe)
  1541. {
  1542. struct intel_crtc *intel_crtc = intel_get_crtc_for_pipe(dev_priv,
  1543. pipe);
  1544. i915_reg_t reg;
  1545. uint32_t val, pipeconf_val;
  1546. /* Make sure PCH DPLL is enabled */
  1547. assert_shared_dpll_enabled(dev_priv, intel_crtc->config->shared_dpll);
  1548. /* FDI must be feeding us bits for PCH ports */
  1549. assert_fdi_tx_enabled(dev_priv, pipe);
  1550. assert_fdi_rx_enabled(dev_priv, pipe);
  1551. if (HAS_PCH_CPT(dev_priv)) {
  1552. /* Workaround: Set the timing override bit before enabling the
  1553. * pch transcoder. */
  1554. reg = TRANS_CHICKEN2(pipe);
  1555. val = I915_READ(reg);
  1556. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1557. I915_WRITE(reg, val);
  1558. }
  1559. reg = PCH_TRANSCONF(pipe);
  1560. val = I915_READ(reg);
  1561. pipeconf_val = I915_READ(PIPECONF(pipe));
  1562. if (HAS_PCH_IBX(dev_priv)) {
  1563. /*
  1564. * Make the BPC in transcoder be consistent with
  1565. * that in pipeconf reg. For HDMI we must use 8bpc
  1566. * here for both 8bpc and 12bpc.
  1567. */
  1568. val &= ~PIPECONF_BPC_MASK;
  1569. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_HDMI))
  1570. val |= PIPECONF_8BPC;
  1571. else
  1572. val |= pipeconf_val & PIPECONF_BPC_MASK;
  1573. }
  1574. val &= ~TRANS_INTERLACE_MASK;
  1575. if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
  1576. if (HAS_PCH_IBX(dev_priv) &&
  1577. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  1578. val |= TRANS_LEGACY_INTERLACED_ILK;
  1579. else
  1580. val |= TRANS_INTERLACED;
  1581. else
  1582. val |= TRANS_PROGRESSIVE;
  1583. I915_WRITE(reg, val | TRANS_ENABLE);
  1584. if (intel_wait_for_register(dev_priv,
  1585. reg, TRANS_STATE_ENABLE, TRANS_STATE_ENABLE,
  1586. 100))
  1587. DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
  1588. }
  1589. static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
  1590. enum transcoder cpu_transcoder)
  1591. {
  1592. u32 val, pipeconf_val;
  1593. /* FDI must be feeding us bits for PCH ports */
  1594. assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
  1595. assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
  1596. /* Workaround: set timing override bit. */
  1597. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1598. val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
  1599. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1600. val = TRANS_ENABLE;
  1601. pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
  1602. if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
  1603. PIPECONF_INTERLACED_ILK)
  1604. val |= TRANS_INTERLACED;
  1605. else
  1606. val |= TRANS_PROGRESSIVE;
  1607. I915_WRITE(LPT_TRANSCONF, val);
  1608. if (intel_wait_for_register(dev_priv,
  1609. LPT_TRANSCONF,
  1610. TRANS_STATE_ENABLE,
  1611. TRANS_STATE_ENABLE,
  1612. 100))
  1613. DRM_ERROR("Failed to enable PCH transcoder\n");
  1614. }
  1615. static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
  1616. enum pipe pipe)
  1617. {
  1618. i915_reg_t reg;
  1619. uint32_t val;
  1620. /* FDI relies on the transcoder */
  1621. assert_fdi_tx_disabled(dev_priv, pipe);
  1622. assert_fdi_rx_disabled(dev_priv, pipe);
  1623. /* Ports must be off as well */
  1624. assert_pch_ports_disabled(dev_priv, pipe);
  1625. reg = PCH_TRANSCONF(pipe);
  1626. val = I915_READ(reg);
  1627. val &= ~TRANS_ENABLE;
  1628. I915_WRITE(reg, val);
  1629. /* wait for PCH transcoder off, transcoder state */
  1630. if (intel_wait_for_register(dev_priv,
  1631. reg, TRANS_STATE_ENABLE, 0,
  1632. 50))
  1633. DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
  1634. if (HAS_PCH_CPT(dev_priv)) {
  1635. /* Workaround: Clear the timing override chicken bit again. */
  1636. reg = TRANS_CHICKEN2(pipe);
  1637. val = I915_READ(reg);
  1638. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1639. I915_WRITE(reg, val);
  1640. }
  1641. }
  1642. void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
  1643. {
  1644. u32 val;
  1645. val = I915_READ(LPT_TRANSCONF);
  1646. val &= ~TRANS_ENABLE;
  1647. I915_WRITE(LPT_TRANSCONF, val);
  1648. /* wait for PCH transcoder off, transcoder state */
  1649. if (intel_wait_for_register(dev_priv,
  1650. LPT_TRANSCONF, TRANS_STATE_ENABLE, 0,
  1651. 50))
  1652. DRM_ERROR("Failed to disable PCH transcoder\n");
  1653. /* Workaround: clear timing override bit. */
  1654. val = I915_READ(TRANS_CHICKEN2(PIPE_A));
  1655. val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
  1656. I915_WRITE(TRANS_CHICKEN2(PIPE_A), val);
  1657. }
  1658. enum transcoder intel_crtc_pch_transcoder(struct intel_crtc *crtc)
  1659. {
  1660. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1661. WARN_ON(!crtc->config->has_pch_encoder);
  1662. if (HAS_PCH_LPT(dev_priv))
  1663. return TRANSCODER_A;
  1664. else
  1665. return (enum transcoder) crtc->pipe;
  1666. }
  1667. /**
  1668. * intel_enable_pipe - enable a pipe, asserting requirements
  1669. * @crtc: crtc responsible for the pipe
  1670. *
  1671. * Enable @crtc's pipe, making sure that various hardware specific requirements
  1672. * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
  1673. */
  1674. static void intel_enable_pipe(struct intel_crtc *crtc)
  1675. {
  1676. struct drm_device *dev = crtc->base.dev;
  1677. struct drm_i915_private *dev_priv = to_i915(dev);
  1678. enum pipe pipe = crtc->pipe;
  1679. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1680. i915_reg_t reg;
  1681. u32 val;
  1682. DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
  1683. assert_planes_disabled(dev_priv, pipe);
  1684. assert_cursor_disabled(dev_priv, pipe);
  1685. assert_sprites_disabled(dev_priv, pipe);
  1686. /*
  1687. * A pipe without a PLL won't actually be able to drive bits from
  1688. * a plane. On ILK+ the pipe PLLs are integrated, so we don't
  1689. * need the check.
  1690. */
  1691. if (HAS_GMCH_DISPLAY(dev_priv)) {
  1692. if (intel_crtc_has_type(crtc->config, INTEL_OUTPUT_DSI))
  1693. assert_dsi_pll_enabled(dev_priv);
  1694. else
  1695. assert_pll_enabled(dev_priv, pipe);
  1696. } else {
  1697. if (crtc->config->has_pch_encoder) {
  1698. /* if driving the PCH, we need FDI enabled */
  1699. assert_fdi_rx_pll_enabled(dev_priv,
  1700. (enum pipe) intel_crtc_pch_transcoder(crtc));
  1701. assert_fdi_tx_pll_enabled(dev_priv,
  1702. (enum pipe) cpu_transcoder);
  1703. }
  1704. /* FIXME: assert CPU port conditions for SNB+ */
  1705. }
  1706. reg = PIPECONF(cpu_transcoder);
  1707. val = I915_READ(reg);
  1708. if (val & PIPECONF_ENABLE) {
  1709. WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  1710. (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
  1711. return;
  1712. }
  1713. I915_WRITE(reg, val | PIPECONF_ENABLE);
  1714. POSTING_READ(reg);
  1715. /*
  1716. * Until the pipe starts DSL will read as 0, which would cause
  1717. * an apparent vblank timestamp jump, which messes up also the
  1718. * frame count when it's derived from the timestamps. So let's
  1719. * wait for the pipe to start properly before we call
  1720. * drm_crtc_vblank_on()
  1721. */
  1722. if (dev->max_vblank_count == 0 &&
  1723. wait_for(intel_get_crtc_scanline(crtc) != crtc->scanline_offset, 50))
  1724. DRM_ERROR("pipe %c didn't start\n", pipe_name(pipe));
  1725. }
  1726. /**
  1727. * intel_disable_pipe - disable a pipe, asserting requirements
  1728. * @crtc: crtc whose pipes is to be disabled
  1729. *
  1730. * Disable the pipe of @crtc, making sure that various hardware
  1731. * specific requirements are met, if applicable, e.g. plane
  1732. * disabled, panel fitter off, etc.
  1733. *
  1734. * Will wait until the pipe has shut down before returning.
  1735. */
  1736. static void intel_disable_pipe(struct intel_crtc *crtc)
  1737. {
  1738. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  1739. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  1740. enum pipe pipe = crtc->pipe;
  1741. i915_reg_t reg;
  1742. u32 val;
  1743. DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
  1744. /*
  1745. * Make sure planes won't keep trying to pump pixels to us,
  1746. * or we might hang the display.
  1747. */
  1748. assert_planes_disabled(dev_priv, pipe);
  1749. assert_cursor_disabled(dev_priv, pipe);
  1750. assert_sprites_disabled(dev_priv, pipe);
  1751. reg = PIPECONF(cpu_transcoder);
  1752. val = I915_READ(reg);
  1753. if ((val & PIPECONF_ENABLE) == 0)
  1754. return;
  1755. /*
  1756. * Double wide has implications for planes
  1757. * so best keep it disabled when not needed.
  1758. */
  1759. if (crtc->config->double_wide)
  1760. val &= ~PIPECONF_DOUBLE_WIDE;
  1761. /* Don't disable pipe or pipe PLLs if needed */
  1762. if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
  1763. !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  1764. val &= ~PIPECONF_ENABLE;
  1765. I915_WRITE(reg, val);
  1766. if ((val & PIPECONF_ENABLE) == 0)
  1767. intel_wait_for_pipe_off(crtc);
  1768. }
  1769. static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
  1770. {
  1771. return IS_GEN2(dev_priv) ? 2048 : 4096;
  1772. }
  1773. static unsigned int intel_tile_width_bytes(const struct drm_i915_private *dev_priv,
  1774. uint64_t fb_modifier, unsigned int cpp)
  1775. {
  1776. switch (fb_modifier) {
  1777. case DRM_FORMAT_MOD_NONE:
  1778. return cpp;
  1779. case I915_FORMAT_MOD_X_TILED:
  1780. if (IS_GEN2(dev_priv))
  1781. return 128;
  1782. else
  1783. return 512;
  1784. case I915_FORMAT_MOD_Y_TILED:
  1785. if (IS_GEN2(dev_priv) || HAS_128_BYTE_Y_TILING(dev_priv))
  1786. return 128;
  1787. else
  1788. return 512;
  1789. case I915_FORMAT_MOD_Yf_TILED:
  1790. switch (cpp) {
  1791. case 1:
  1792. return 64;
  1793. case 2:
  1794. case 4:
  1795. return 128;
  1796. case 8:
  1797. case 16:
  1798. return 256;
  1799. default:
  1800. MISSING_CASE(cpp);
  1801. return cpp;
  1802. }
  1803. break;
  1804. default:
  1805. MISSING_CASE(fb_modifier);
  1806. return cpp;
  1807. }
  1808. }
  1809. unsigned int intel_tile_height(const struct drm_i915_private *dev_priv,
  1810. uint64_t fb_modifier, unsigned int cpp)
  1811. {
  1812. if (fb_modifier == DRM_FORMAT_MOD_NONE)
  1813. return 1;
  1814. else
  1815. return intel_tile_size(dev_priv) /
  1816. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1817. }
  1818. /* Return the tile dimensions in pixel units */
  1819. static void intel_tile_dims(const struct drm_i915_private *dev_priv,
  1820. unsigned int *tile_width,
  1821. unsigned int *tile_height,
  1822. uint64_t fb_modifier,
  1823. unsigned int cpp)
  1824. {
  1825. unsigned int tile_width_bytes =
  1826. intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  1827. *tile_width = tile_width_bytes / cpp;
  1828. *tile_height = intel_tile_size(dev_priv) / tile_width_bytes;
  1829. }
  1830. unsigned int
  1831. intel_fb_align_height(struct drm_device *dev, unsigned int height,
  1832. uint32_t pixel_format, uint64_t fb_modifier)
  1833. {
  1834. unsigned int cpp = drm_format_plane_cpp(pixel_format, 0);
  1835. unsigned int tile_height = intel_tile_height(to_i915(dev), fb_modifier, cpp);
  1836. return ALIGN(height, tile_height);
  1837. }
  1838. unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
  1839. {
  1840. unsigned int size = 0;
  1841. int i;
  1842. for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
  1843. size += rot_info->plane[i].width * rot_info->plane[i].height;
  1844. return size;
  1845. }
  1846. static void
  1847. intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
  1848. const struct drm_framebuffer *fb,
  1849. unsigned int rotation)
  1850. {
  1851. view->type = I915_GGTT_VIEW_NORMAL;
  1852. if (drm_rotation_90_or_270(rotation)) {
  1853. view->type = I915_GGTT_VIEW_ROTATED;
  1854. view->rotated = to_intel_framebuffer(fb)->rot_info;
  1855. }
  1856. }
  1857. static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
  1858. {
  1859. if (INTEL_INFO(dev_priv)->gen >= 9)
  1860. return 256 * 1024;
  1861. else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
  1862. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  1863. return 128 * 1024;
  1864. else if (INTEL_INFO(dev_priv)->gen >= 4)
  1865. return 4 * 1024;
  1866. else
  1867. return 0;
  1868. }
  1869. static unsigned int intel_surf_alignment(const struct drm_i915_private *dev_priv,
  1870. uint64_t fb_modifier)
  1871. {
  1872. switch (fb_modifier) {
  1873. case DRM_FORMAT_MOD_NONE:
  1874. return intel_linear_alignment(dev_priv);
  1875. case I915_FORMAT_MOD_X_TILED:
  1876. if (INTEL_INFO(dev_priv)->gen >= 9)
  1877. return 256 * 1024;
  1878. return 0;
  1879. case I915_FORMAT_MOD_Y_TILED:
  1880. case I915_FORMAT_MOD_Yf_TILED:
  1881. return 1 * 1024 * 1024;
  1882. default:
  1883. MISSING_CASE(fb_modifier);
  1884. return 0;
  1885. }
  1886. }
  1887. struct i915_vma *
  1888. intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb, unsigned int rotation)
  1889. {
  1890. struct drm_device *dev = fb->dev;
  1891. struct drm_i915_private *dev_priv = to_i915(dev);
  1892. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  1893. struct i915_ggtt_view view;
  1894. struct i915_vma *vma;
  1895. u32 alignment;
  1896. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  1897. alignment = intel_surf_alignment(dev_priv, fb->modifier);
  1898. intel_fill_fb_ggtt_view(&view, fb, rotation);
  1899. /* Note that the w/a also requires 64 PTE of padding following the
  1900. * bo. We currently fill all unused PTE with the shadow page and so
  1901. * we should always have valid PTE following the scanout preventing
  1902. * the VT-d warning.
  1903. */
  1904. if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
  1905. alignment = 256 * 1024;
  1906. /*
  1907. * Global gtt pte registers are special registers which actually forward
  1908. * writes to a chunk of system memory. Which means that there is no risk
  1909. * that the register values disappear as soon as we call
  1910. * intel_runtime_pm_put(), so it is correct to wrap only the
  1911. * pin/unpin/fence and not more.
  1912. */
  1913. intel_runtime_pm_get(dev_priv);
  1914. vma = i915_gem_object_pin_to_display_plane(obj, alignment, &view);
  1915. if (IS_ERR(vma))
  1916. goto err;
  1917. if (i915_vma_is_map_and_fenceable(vma)) {
  1918. /* Install a fence for tiled scan-out. Pre-i965 always needs a
  1919. * fence, whereas 965+ only requires a fence if using
  1920. * framebuffer compression. For simplicity, we always, when
  1921. * possible, install a fence as the cost is not that onerous.
  1922. *
  1923. * If we fail to fence the tiled scanout, then either the
  1924. * modeset will reject the change (which is highly unlikely as
  1925. * the affected systems, all but one, do not have unmappable
  1926. * space) or we will not be able to enable full powersaving
  1927. * techniques (also likely not to apply due to various limits
  1928. * FBC and the like impose on the size of the buffer, which
  1929. * presumably we violated anyway with this unmappable buffer).
  1930. * Anyway, it is presumably better to stumble onwards with
  1931. * something and try to run the system in a "less than optimal"
  1932. * mode that matches the user configuration.
  1933. */
  1934. if (i915_vma_get_fence(vma) == 0)
  1935. i915_vma_pin_fence(vma);
  1936. }
  1937. i915_vma_get(vma);
  1938. err:
  1939. intel_runtime_pm_put(dev_priv);
  1940. return vma;
  1941. }
  1942. void intel_unpin_fb_vma(struct i915_vma *vma)
  1943. {
  1944. lockdep_assert_held(&vma->vm->i915->drm.struct_mutex);
  1945. i915_vma_unpin_fence(vma);
  1946. i915_gem_object_unpin_from_display_plane(vma);
  1947. i915_vma_put(vma);
  1948. }
  1949. static int intel_fb_pitch(const struct drm_framebuffer *fb, int plane,
  1950. unsigned int rotation)
  1951. {
  1952. if (drm_rotation_90_or_270(rotation))
  1953. return to_intel_framebuffer(fb)->rotated[plane].pitch;
  1954. else
  1955. return fb->pitches[plane];
  1956. }
  1957. /*
  1958. * Convert the x/y offsets into a linear offset.
  1959. * Only valid with 0/180 degree rotation, which is fine since linear
  1960. * offset is only used with linear buffers on pre-hsw and tiled buffers
  1961. * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
  1962. */
  1963. u32 intel_fb_xy_to_linear(int x, int y,
  1964. const struct intel_plane_state *state,
  1965. int plane)
  1966. {
  1967. const struct drm_framebuffer *fb = state->base.fb;
  1968. unsigned int cpp = fb->format->cpp[plane];
  1969. unsigned int pitch = fb->pitches[plane];
  1970. return y * pitch + x * cpp;
  1971. }
  1972. /*
  1973. * Add the x/y offsets derived from fb->offsets[] to the user
  1974. * specified plane src x/y offsets. The resulting x/y offsets
  1975. * specify the start of scanout from the beginning of the gtt mapping.
  1976. */
  1977. void intel_add_fb_offsets(int *x, int *y,
  1978. const struct intel_plane_state *state,
  1979. int plane)
  1980. {
  1981. const struct intel_framebuffer *intel_fb = to_intel_framebuffer(state->base.fb);
  1982. unsigned int rotation = state->base.rotation;
  1983. if (drm_rotation_90_or_270(rotation)) {
  1984. *x += intel_fb->rotated[plane].x;
  1985. *y += intel_fb->rotated[plane].y;
  1986. } else {
  1987. *x += intel_fb->normal[plane].x;
  1988. *y += intel_fb->normal[plane].y;
  1989. }
  1990. }
  1991. /*
  1992. * Input tile dimensions and pitch must already be
  1993. * rotated to match x and y, and in pixel units.
  1994. */
  1995. static u32 _intel_adjust_tile_offset(int *x, int *y,
  1996. unsigned int tile_width,
  1997. unsigned int tile_height,
  1998. unsigned int tile_size,
  1999. unsigned int pitch_tiles,
  2000. u32 old_offset,
  2001. u32 new_offset)
  2002. {
  2003. unsigned int pitch_pixels = pitch_tiles * tile_width;
  2004. unsigned int tiles;
  2005. WARN_ON(old_offset & (tile_size - 1));
  2006. WARN_ON(new_offset & (tile_size - 1));
  2007. WARN_ON(new_offset > old_offset);
  2008. tiles = (old_offset - new_offset) / tile_size;
  2009. *y += tiles / pitch_tiles * tile_height;
  2010. *x += tiles % pitch_tiles * tile_width;
  2011. /* minimize x in case it got needlessly big */
  2012. *y += *x / pitch_pixels * tile_height;
  2013. *x %= pitch_pixels;
  2014. return new_offset;
  2015. }
  2016. /*
  2017. * Adjust the tile offset by moving the difference into
  2018. * the x/y offsets.
  2019. */
  2020. static u32 intel_adjust_tile_offset(int *x, int *y,
  2021. const struct intel_plane_state *state, int plane,
  2022. u32 old_offset, u32 new_offset)
  2023. {
  2024. const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
  2025. const struct drm_framebuffer *fb = state->base.fb;
  2026. unsigned int cpp = fb->format->cpp[plane];
  2027. unsigned int rotation = state->base.rotation;
  2028. unsigned int pitch = intel_fb_pitch(fb, plane, rotation);
  2029. WARN_ON(new_offset > old_offset);
  2030. if (fb->modifier != DRM_FORMAT_MOD_NONE) {
  2031. unsigned int tile_size, tile_width, tile_height;
  2032. unsigned int pitch_tiles;
  2033. tile_size = intel_tile_size(dev_priv);
  2034. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2035. fb->modifier, cpp);
  2036. if (drm_rotation_90_or_270(rotation)) {
  2037. pitch_tiles = pitch / tile_height;
  2038. swap(tile_width, tile_height);
  2039. } else {
  2040. pitch_tiles = pitch / (tile_width * cpp);
  2041. }
  2042. _intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2043. tile_size, pitch_tiles,
  2044. old_offset, new_offset);
  2045. } else {
  2046. old_offset += *y * pitch + *x * cpp;
  2047. *y = (old_offset - new_offset) / pitch;
  2048. *x = ((old_offset - new_offset) - *y * pitch) / cpp;
  2049. }
  2050. return new_offset;
  2051. }
  2052. /*
  2053. * Computes the linear offset to the base tile and adjusts
  2054. * x, y. bytes per pixel is assumed to be a power-of-two.
  2055. *
  2056. * In the 90/270 rotated case, x and y are assumed
  2057. * to be already rotated to match the rotated GTT view, and
  2058. * pitch is the tile_height aligned framebuffer height.
  2059. *
  2060. * This function is used when computing the derived information
  2061. * under intel_framebuffer, so using any of that information
  2062. * here is not allowed. Anything under drm_framebuffer can be
  2063. * used. This is why the user has to pass in the pitch since it
  2064. * is specified in the rotated orientation.
  2065. */
  2066. static u32 _intel_compute_tile_offset(const struct drm_i915_private *dev_priv,
  2067. int *x, int *y,
  2068. const struct drm_framebuffer *fb, int plane,
  2069. unsigned int pitch,
  2070. unsigned int rotation,
  2071. u32 alignment)
  2072. {
  2073. uint64_t fb_modifier = fb->modifier;
  2074. unsigned int cpp = fb->format->cpp[plane];
  2075. u32 offset, offset_aligned;
  2076. if (alignment)
  2077. alignment--;
  2078. if (fb_modifier != DRM_FORMAT_MOD_NONE) {
  2079. unsigned int tile_size, tile_width, tile_height;
  2080. unsigned int tile_rows, tiles, pitch_tiles;
  2081. tile_size = intel_tile_size(dev_priv);
  2082. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2083. fb_modifier, cpp);
  2084. if (drm_rotation_90_or_270(rotation)) {
  2085. pitch_tiles = pitch / tile_height;
  2086. swap(tile_width, tile_height);
  2087. } else {
  2088. pitch_tiles = pitch / (tile_width * cpp);
  2089. }
  2090. tile_rows = *y / tile_height;
  2091. *y %= tile_height;
  2092. tiles = *x / tile_width;
  2093. *x %= tile_width;
  2094. offset = (tile_rows * pitch_tiles + tiles) * tile_size;
  2095. offset_aligned = offset & ~alignment;
  2096. _intel_adjust_tile_offset(x, y, tile_width, tile_height,
  2097. tile_size, pitch_tiles,
  2098. offset, offset_aligned);
  2099. } else {
  2100. offset = *y * pitch + *x * cpp;
  2101. offset_aligned = offset & ~alignment;
  2102. *y = (offset & alignment) / pitch;
  2103. *x = ((offset & alignment) - *y * pitch) / cpp;
  2104. }
  2105. return offset_aligned;
  2106. }
  2107. u32 intel_compute_tile_offset(int *x, int *y,
  2108. const struct intel_plane_state *state,
  2109. int plane)
  2110. {
  2111. const struct drm_i915_private *dev_priv = to_i915(state->base.plane->dev);
  2112. const struct drm_framebuffer *fb = state->base.fb;
  2113. unsigned int rotation = state->base.rotation;
  2114. int pitch = intel_fb_pitch(fb, plane, rotation);
  2115. u32 alignment;
  2116. /* AUX_DIST needs only 4K alignment */
  2117. if (fb->format->format == DRM_FORMAT_NV12 && plane == 1)
  2118. alignment = 4096;
  2119. else
  2120. alignment = intel_surf_alignment(dev_priv, fb->modifier);
  2121. return _intel_compute_tile_offset(dev_priv, x, y, fb, plane, pitch,
  2122. rotation, alignment);
  2123. }
  2124. /* Convert the fb->offset[] linear offset into x/y offsets */
  2125. static void intel_fb_offset_to_xy(int *x, int *y,
  2126. const struct drm_framebuffer *fb, int plane)
  2127. {
  2128. unsigned int cpp = fb->format->cpp[plane];
  2129. unsigned int pitch = fb->pitches[plane];
  2130. u32 linear_offset = fb->offsets[plane];
  2131. *y = linear_offset / pitch;
  2132. *x = linear_offset % pitch / cpp;
  2133. }
  2134. static unsigned int intel_fb_modifier_to_tiling(uint64_t fb_modifier)
  2135. {
  2136. switch (fb_modifier) {
  2137. case I915_FORMAT_MOD_X_TILED:
  2138. return I915_TILING_X;
  2139. case I915_FORMAT_MOD_Y_TILED:
  2140. return I915_TILING_Y;
  2141. default:
  2142. return I915_TILING_NONE;
  2143. }
  2144. }
  2145. static int
  2146. intel_fill_fb_info(struct drm_i915_private *dev_priv,
  2147. struct drm_framebuffer *fb)
  2148. {
  2149. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  2150. struct intel_rotation_info *rot_info = &intel_fb->rot_info;
  2151. u32 gtt_offset_rotated = 0;
  2152. unsigned int max_size = 0;
  2153. int i, num_planes = fb->format->num_planes;
  2154. unsigned int tile_size = intel_tile_size(dev_priv);
  2155. for (i = 0; i < num_planes; i++) {
  2156. unsigned int width, height;
  2157. unsigned int cpp, size;
  2158. u32 offset;
  2159. int x, y;
  2160. cpp = fb->format->cpp[i];
  2161. width = drm_framebuffer_plane_width(fb->width, fb, i);
  2162. height = drm_framebuffer_plane_height(fb->height, fb, i);
  2163. intel_fb_offset_to_xy(&x, &y, fb, i);
  2164. /*
  2165. * The fence (if used) is aligned to the start of the object
  2166. * so having the framebuffer wrap around across the edge of the
  2167. * fenced region doesn't really work. We have no API to configure
  2168. * the fence start offset within the object (nor could we probably
  2169. * on gen2/3). So it's just easier if we just require that the
  2170. * fb layout agrees with the fence layout. We already check that the
  2171. * fb stride matches the fence stride elsewhere.
  2172. */
  2173. if (i915_gem_object_is_tiled(intel_fb->obj) &&
  2174. (x + width) * cpp > fb->pitches[i]) {
  2175. DRM_DEBUG("bad fb plane %d offset: 0x%x\n",
  2176. i, fb->offsets[i]);
  2177. return -EINVAL;
  2178. }
  2179. /*
  2180. * First pixel of the framebuffer from
  2181. * the start of the normal gtt mapping.
  2182. */
  2183. intel_fb->normal[i].x = x;
  2184. intel_fb->normal[i].y = y;
  2185. offset = _intel_compute_tile_offset(dev_priv, &x, &y,
  2186. fb, 0, fb->pitches[i],
  2187. DRM_ROTATE_0, tile_size);
  2188. offset /= tile_size;
  2189. if (fb->modifier != DRM_FORMAT_MOD_NONE) {
  2190. unsigned int tile_width, tile_height;
  2191. unsigned int pitch_tiles;
  2192. struct drm_rect r;
  2193. intel_tile_dims(dev_priv, &tile_width, &tile_height,
  2194. fb->modifier, cpp);
  2195. rot_info->plane[i].offset = offset;
  2196. rot_info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i], tile_width * cpp);
  2197. rot_info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
  2198. rot_info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
  2199. intel_fb->rotated[i].pitch =
  2200. rot_info->plane[i].height * tile_height;
  2201. /* how many tiles does this plane need */
  2202. size = rot_info->plane[i].stride * rot_info->plane[i].height;
  2203. /*
  2204. * If the plane isn't horizontally tile aligned,
  2205. * we need one more tile.
  2206. */
  2207. if (x != 0)
  2208. size++;
  2209. /* rotate the x/y offsets to match the GTT view */
  2210. r.x1 = x;
  2211. r.y1 = y;
  2212. r.x2 = x + width;
  2213. r.y2 = y + height;
  2214. drm_rect_rotate(&r,
  2215. rot_info->plane[i].width * tile_width,
  2216. rot_info->plane[i].height * tile_height,
  2217. DRM_ROTATE_270);
  2218. x = r.x1;
  2219. y = r.y1;
  2220. /* rotate the tile dimensions to match the GTT view */
  2221. pitch_tiles = intel_fb->rotated[i].pitch / tile_height;
  2222. swap(tile_width, tile_height);
  2223. /*
  2224. * We only keep the x/y offsets, so push all of the
  2225. * gtt offset into the x/y offsets.
  2226. */
  2227. _intel_adjust_tile_offset(&x, &y, tile_size,
  2228. tile_width, tile_height, pitch_tiles,
  2229. gtt_offset_rotated * tile_size, 0);
  2230. gtt_offset_rotated += rot_info->plane[i].width * rot_info->plane[i].height;
  2231. /*
  2232. * First pixel of the framebuffer from
  2233. * the start of the rotated gtt mapping.
  2234. */
  2235. intel_fb->rotated[i].x = x;
  2236. intel_fb->rotated[i].y = y;
  2237. } else {
  2238. size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
  2239. x * cpp, tile_size);
  2240. }
  2241. /* how many tiles in total needed in the bo */
  2242. max_size = max(max_size, offset + size);
  2243. }
  2244. if (max_size * tile_size > to_intel_framebuffer(fb)->obj->base.size) {
  2245. DRM_DEBUG("fb too big for bo (need %u bytes, have %zu bytes)\n",
  2246. max_size * tile_size, to_intel_framebuffer(fb)->obj->base.size);
  2247. return -EINVAL;
  2248. }
  2249. return 0;
  2250. }
  2251. static int i9xx_format_to_fourcc(int format)
  2252. {
  2253. switch (format) {
  2254. case DISPPLANE_8BPP:
  2255. return DRM_FORMAT_C8;
  2256. case DISPPLANE_BGRX555:
  2257. return DRM_FORMAT_XRGB1555;
  2258. case DISPPLANE_BGRX565:
  2259. return DRM_FORMAT_RGB565;
  2260. default:
  2261. case DISPPLANE_BGRX888:
  2262. return DRM_FORMAT_XRGB8888;
  2263. case DISPPLANE_RGBX888:
  2264. return DRM_FORMAT_XBGR8888;
  2265. case DISPPLANE_BGRX101010:
  2266. return DRM_FORMAT_XRGB2101010;
  2267. case DISPPLANE_RGBX101010:
  2268. return DRM_FORMAT_XBGR2101010;
  2269. }
  2270. }
  2271. static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
  2272. {
  2273. switch (format) {
  2274. case PLANE_CTL_FORMAT_RGB_565:
  2275. return DRM_FORMAT_RGB565;
  2276. default:
  2277. case PLANE_CTL_FORMAT_XRGB_8888:
  2278. if (rgb_order) {
  2279. if (alpha)
  2280. return DRM_FORMAT_ABGR8888;
  2281. else
  2282. return DRM_FORMAT_XBGR8888;
  2283. } else {
  2284. if (alpha)
  2285. return DRM_FORMAT_ARGB8888;
  2286. else
  2287. return DRM_FORMAT_XRGB8888;
  2288. }
  2289. case PLANE_CTL_FORMAT_XRGB_2101010:
  2290. if (rgb_order)
  2291. return DRM_FORMAT_XBGR2101010;
  2292. else
  2293. return DRM_FORMAT_XRGB2101010;
  2294. }
  2295. }
  2296. static bool
  2297. intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
  2298. struct intel_initial_plane_config *plane_config)
  2299. {
  2300. struct drm_device *dev = crtc->base.dev;
  2301. struct drm_i915_private *dev_priv = to_i915(dev);
  2302. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  2303. struct drm_i915_gem_object *obj = NULL;
  2304. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  2305. struct drm_framebuffer *fb = &plane_config->fb->base;
  2306. u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
  2307. u32 size_aligned = round_up(plane_config->base + plane_config->size,
  2308. PAGE_SIZE);
  2309. size_aligned -= base_aligned;
  2310. if (plane_config->size == 0)
  2311. return false;
  2312. /* If the FB is too big, just don't use it since fbdev is not very
  2313. * important and we should probably use that space with FBC or other
  2314. * features. */
  2315. if (size_aligned * 2 > ggtt->stolen_usable_size)
  2316. return false;
  2317. mutex_lock(&dev->struct_mutex);
  2318. obj = i915_gem_object_create_stolen_for_preallocated(dev_priv,
  2319. base_aligned,
  2320. base_aligned,
  2321. size_aligned);
  2322. if (!obj) {
  2323. mutex_unlock(&dev->struct_mutex);
  2324. return false;
  2325. }
  2326. if (plane_config->tiling == I915_TILING_X)
  2327. obj->tiling_and_stride = fb->pitches[0] | I915_TILING_X;
  2328. mode_cmd.pixel_format = fb->format->format;
  2329. mode_cmd.width = fb->width;
  2330. mode_cmd.height = fb->height;
  2331. mode_cmd.pitches[0] = fb->pitches[0];
  2332. mode_cmd.modifier[0] = fb->modifier;
  2333. mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
  2334. if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
  2335. &mode_cmd, obj)) {
  2336. DRM_DEBUG_KMS("intel fb init failed\n");
  2337. goto out_unref_obj;
  2338. }
  2339. mutex_unlock(&dev->struct_mutex);
  2340. DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
  2341. return true;
  2342. out_unref_obj:
  2343. i915_gem_object_put(obj);
  2344. mutex_unlock(&dev->struct_mutex);
  2345. return false;
  2346. }
  2347. /* Update plane->state->fb to match plane->fb after driver-internal updates */
  2348. static void
  2349. update_state_fb(struct drm_plane *plane)
  2350. {
  2351. if (plane->fb == plane->state->fb)
  2352. return;
  2353. if (plane->state->fb)
  2354. drm_framebuffer_unreference(plane->state->fb);
  2355. plane->state->fb = plane->fb;
  2356. if (plane->state->fb)
  2357. drm_framebuffer_reference(plane->state->fb);
  2358. }
  2359. static void
  2360. intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
  2361. struct intel_initial_plane_config *plane_config)
  2362. {
  2363. struct drm_device *dev = intel_crtc->base.dev;
  2364. struct drm_i915_private *dev_priv = to_i915(dev);
  2365. struct drm_crtc *c;
  2366. struct drm_i915_gem_object *obj;
  2367. struct drm_plane *primary = intel_crtc->base.primary;
  2368. struct drm_plane_state *plane_state = primary->state;
  2369. struct drm_crtc_state *crtc_state = intel_crtc->base.state;
  2370. struct intel_plane *intel_plane = to_intel_plane(primary);
  2371. struct intel_plane_state *intel_state =
  2372. to_intel_plane_state(plane_state);
  2373. struct drm_framebuffer *fb;
  2374. if (!plane_config->fb)
  2375. return;
  2376. if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
  2377. fb = &plane_config->fb->base;
  2378. goto valid_fb;
  2379. }
  2380. kfree(plane_config->fb);
  2381. /*
  2382. * Failed to alloc the obj, check to see if we should share
  2383. * an fb with another CRTC instead
  2384. */
  2385. for_each_crtc(dev, c) {
  2386. struct intel_plane_state *state;
  2387. if (c == &intel_crtc->base)
  2388. continue;
  2389. if (!to_intel_crtc(c)->active)
  2390. continue;
  2391. state = to_intel_plane_state(c->primary->state);
  2392. if (!state->vma)
  2393. continue;
  2394. if (intel_plane_ggtt_offset(state) == plane_config->base) {
  2395. fb = c->primary->fb;
  2396. drm_framebuffer_reference(fb);
  2397. goto valid_fb;
  2398. }
  2399. }
  2400. /*
  2401. * We've failed to reconstruct the BIOS FB. Current display state
  2402. * indicates that the primary plane is visible, but has a NULL FB,
  2403. * which will lead to problems later if we don't fix it up. The
  2404. * simplest solution is to just disable the primary plane now and
  2405. * pretend the BIOS never had it enabled.
  2406. */
  2407. plane_state->visible = false;
  2408. crtc_state->plane_mask &= ~(1 << drm_plane_index(primary));
  2409. intel_pre_disable_primary_noatomic(&intel_crtc->base);
  2410. intel_plane->disable_plane(primary, &intel_crtc->base);
  2411. return;
  2412. valid_fb:
  2413. mutex_lock(&dev->struct_mutex);
  2414. intel_state->vma =
  2415. intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  2416. mutex_unlock(&dev->struct_mutex);
  2417. if (IS_ERR(intel_state->vma)) {
  2418. DRM_ERROR("failed to pin boot fb on pipe %d: %li\n",
  2419. intel_crtc->pipe, PTR_ERR(intel_state->vma));
  2420. intel_state->vma = NULL;
  2421. drm_framebuffer_unreference(fb);
  2422. return;
  2423. }
  2424. plane_state->src_x = 0;
  2425. plane_state->src_y = 0;
  2426. plane_state->src_w = fb->width << 16;
  2427. plane_state->src_h = fb->height << 16;
  2428. plane_state->crtc_x = 0;
  2429. plane_state->crtc_y = 0;
  2430. plane_state->crtc_w = fb->width;
  2431. plane_state->crtc_h = fb->height;
  2432. intel_state->base.src = drm_plane_state_src(plane_state);
  2433. intel_state->base.dst = drm_plane_state_dest(plane_state);
  2434. obj = intel_fb_obj(fb);
  2435. if (i915_gem_object_is_tiled(obj))
  2436. dev_priv->preserve_bios_swizzle = true;
  2437. drm_framebuffer_reference(fb);
  2438. primary->fb = primary->state->fb = fb;
  2439. primary->crtc = primary->state->crtc = &intel_crtc->base;
  2440. intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
  2441. atomic_or(to_intel_plane(primary)->frontbuffer_bit,
  2442. &obj->frontbuffer_bits);
  2443. }
  2444. static int skl_max_plane_width(const struct drm_framebuffer *fb, int plane,
  2445. unsigned int rotation)
  2446. {
  2447. int cpp = fb->format->cpp[plane];
  2448. switch (fb->modifier) {
  2449. case DRM_FORMAT_MOD_NONE:
  2450. case I915_FORMAT_MOD_X_TILED:
  2451. switch (cpp) {
  2452. case 8:
  2453. return 4096;
  2454. case 4:
  2455. case 2:
  2456. case 1:
  2457. return 8192;
  2458. default:
  2459. MISSING_CASE(cpp);
  2460. break;
  2461. }
  2462. break;
  2463. case I915_FORMAT_MOD_Y_TILED:
  2464. case I915_FORMAT_MOD_Yf_TILED:
  2465. switch (cpp) {
  2466. case 8:
  2467. return 2048;
  2468. case 4:
  2469. return 4096;
  2470. case 2:
  2471. case 1:
  2472. return 8192;
  2473. default:
  2474. MISSING_CASE(cpp);
  2475. break;
  2476. }
  2477. break;
  2478. default:
  2479. MISSING_CASE(fb->modifier);
  2480. }
  2481. return 2048;
  2482. }
  2483. static int skl_check_main_surface(struct intel_plane_state *plane_state)
  2484. {
  2485. const struct drm_i915_private *dev_priv = to_i915(plane_state->base.plane->dev);
  2486. const struct drm_framebuffer *fb = plane_state->base.fb;
  2487. unsigned int rotation = plane_state->base.rotation;
  2488. int x = plane_state->base.src.x1 >> 16;
  2489. int y = plane_state->base.src.y1 >> 16;
  2490. int w = drm_rect_width(&plane_state->base.src) >> 16;
  2491. int h = drm_rect_height(&plane_state->base.src) >> 16;
  2492. int max_width = skl_max_plane_width(fb, 0, rotation);
  2493. int max_height = 4096;
  2494. u32 alignment, offset, aux_offset = plane_state->aux.offset;
  2495. if (w > max_width || h > max_height) {
  2496. DRM_DEBUG_KMS("requested Y/RGB source size %dx%d too big (limit %dx%d)\n",
  2497. w, h, max_width, max_height);
  2498. return -EINVAL;
  2499. }
  2500. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2501. offset = intel_compute_tile_offset(&x, &y, plane_state, 0);
  2502. alignment = intel_surf_alignment(dev_priv, fb->modifier);
  2503. /*
  2504. * AUX surface offset is specified as the distance from the
  2505. * main surface offset, and it must be non-negative. Make
  2506. * sure that is what we will get.
  2507. */
  2508. if (offset > aux_offset)
  2509. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2510. offset, aux_offset & ~(alignment - 1));
  2511. /*
  2512. * When using an X-tiled surface, the plane blows up
  2513. * if the x offset + width exceed the stride.
  2514. *
  2515. * TODO: linear and Y-tiled seem fine, Yf untested,
  2516. */
  2517. if (fb->modifier == I915_FORMAT_MOD_X_TILED) {
  2518. int cpp = fb->format->cpp[0];
  2519. while ((x + w) * cpp > fb->pitches[0]) {
  2520. if (offset == 0) {
  2521. DRM_DEBUG_KMS("Unable to find suitable display surface offset\n");
  2522. return -EINVAL;
  2523. }
  2524. offset = intel_adjust_tile_offset(&x, &y, plane_state, 0,
  2525. offset, offset - alignment);
  2526. }
  2527. }
  2528. plane_state->main.offset = offset;
  2529. plane_state->main.x = x;
  2530. plane_state->main.y = y;
  2531. return 0;
  2532. }
  2533. static int skl_check_nv12_aux_surface(struct intel_plane_state *plane_state)
  2534. {
  2535. const struct drm_framebuffer *fb = plane_state->base.fb;
  2536. unsigned int rotation = plane_state->base.rotation;
  2537. int max_width = skl_max_plane_width(fb, 1, rotation);
  2538. int max_height = 4096;
  2539. int x = plane_state->base.src.x1 >> 17;
  2540. int y = plane_state->base.src.y1 >> 17;
  2541. int w = drm_rect_width(&plane_state->base.src) >> 17;
  2542. int h = drm_rect_height(&plane_state->base.src) >> 17;
  2543. u32 offset;
  2544. intel_add_fb_offsets(&x, &y, plane_state, 1);
  2545. offset = intel_compute_tile_offset(&x, &y, plane_state, 1);
  2546. /* FIXME not quite sure how/if these apply to the chroma plane */
  2547. if (w > max_width || h > max_height) {
  2548. DRM_DEBUG_KMS("CbCr source size %dx%d too big (limit %dx%d)\n",
  2549. w, h, max_width, max_height);
  2550. return -EINVAL;
  2551. }
  2552. plane_state->aux.offset = offset;
  2553. plane_state->aux.x = x;
  2554. plane_state->aux.y = y;
  2555. return 0;
  2556. }
  2557. int skl_check_plane_surface(struct intel_plane_state *plane_state)
  2558. {
  2559. const struct drm_framebuffer *fb = plane_state->base.fb;
  2560. unsigned int rotation = plane_state->base.rotation;
  2561. int ret;
  2562. if (!plane_state->base.visible)
  2563. return 0;
  2564. /* Rotate src coordinates to match rotated GTT view */
  2565. if (drm_rotation_90_or_270(rotation))
  2566. drm_rect_rotate(&plane_state->base.src,
  2567. fb->width << 16, fb->height << 16,
  2568. DRM_ROTATE_270);
  2569. /*
  2570. * Handle the AUX surface first since
  2571. * the main surface setup depends on it.
  2572. */
  2573. if (fb->format->format == DRM_FORMAT_NV12) {
  2574. ret = skl_check_nv12_aux_surface(plane_state);
  2575. if (ret)
  2576. return ret;
  2577. } else {
  2578. plane_state->aux.offset = ~0xfff;
  2579. plane_state->aux.x = 0;
  2580. plane_state->aux.y = 0;
  2581. }
  2582. ret = skl_check_main_surface(plane_state);
  2583. if (ret)
  2584. return ret;
  2585. return 0;
  2586. }
  2587. static void i9xx_update_primary_plane(struct drm_plane *primary,
  2588. const struct intel_crtc_state *crtc_state,
  2589. const struct intel_plane_state *plane_state)
  2590. {
  2591. struct drm_i915_private *dev_priv = to_i915(primary->dev);
  2592. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2593. struct drm_framebuffer *fb = plane_state->base.fb;
  2594. int plane = intel_crtc->plane;
  2595. u32 linear_offset;
  2596. u32 dspcntr;
  2597. i915_reg_t reg = DSPCNTR(plane);
  2598. unsigned int rotation = plane_state->base.rotation;
  2599. int x = plane_state->base.src.x1 >> 16;
  2600. int y = plane_state->base.src.y1 >> 16;
  2601. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2602. dspcntr |= DISPLAY_PLANE_ENABLE;
  2603. if (INTEL_GEN(dev_priv) < 4) {
  2604. if (intel_crtc->pipe == PIPE_B)
  2605. dspcntr |= DISPPLANE_SEL_PIPE_B;
  2606. /* pipesrc and dspsize control the size that is scaled from,
  2607. * which should always be the user's requested size.
  2608. */
  2609. I915_WRITE(DSPSIZE(plane),
  2610. ((crtc_state->pipe_src_h - 1) << 16) |
  2611. (crtc_state->pipe_src_w - 1));
  2612. I915_WRITE(DSPPOS(plane), 0);
  2613. } else if (IS_CHERRYVIEW(dev_priv) && plane == PLANE_B) {
  2614. I915_WRITE(PRIMSIZE(plane),
  2615. ((crtc_state->pipe_src_h - 1) << 16) |
  2616. (crtc_state->pipe_src_w - 1));
  2617. I915_WRITE(PRIMPOS(plane), 0);
  2618. I915_WRITE(PRIMCNSTALPHA(plane), 0);
  2619. }
  2620. switch (fb->format->format) {
  2621. case DRM_FORMAT_C8:
  2622. dspcntr |= DISPPLANE_8BPP;
  2623. break;
  2624. case DRM_FORMAT_XRGB1555:
  2625. dspcntr |= DISPPLANE_BGRX555;
  2626. break;
  2627. case DRM_FORMAT_RGB565:
  2628. dspcntr |= DISPPLANE_BGRX565;
  2629. break;
  2630. case DRM_FORMAT_XRGB8888:
  2631. dspcntr |= DISPPLANE_BGRX888;
  2632. break;
  2633. case DRM_FORMAT_XBGR8888:
  2634. dspcntr |= DISPPLANE_RGBX888;
  2635. break;
  2636. case DRM_FORMAT_XRGB2101010:
  2637. dspcntr |= DISPPLANE_BGRX101010;
  2638. break;
  2639. case DRM_FORMAT_XBGR2101010:
  2640. dspcntr |= DISPPLANE_RGBX101010;
  2641. break;
  2642. default:
  2643. BUG();
  2644. }
  2645. if (INTEL_GEN(dev_priv) >= 4 &&
  2646. fb->modifier == I915_FORMAT_MOD_X_TILED)
  2647. dspcntr |= DISPPLANE_TILED;
  2648. if (rotation & DRM_ROTATE_180)
  2649. dspcntr |= DISPPLANE_ROTATE_180;
  2650. if (rotation & DRM_REFLECT_X)
  2651. dspcntr |= DISPPLANE_MIRROR;
  2652. if (IS_G4X(dev_priv))
  2653. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2654. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2655. if (INTEL_GEN(dev_priv) >= 4)
  2656. intel_crtc->dspaddr_offset =
  2657. intel_compute_tile_offset(&x, &y, plane_state, 0);
  2658. if (rotation & DRM_ROTATE_180) {
  2659. x += crtc_state->pipe_src_w - 1;
  2660. y += crtc_state->pipe_src_h - 1;
  2661. } else if (rotation & DRM_REFLECT_X) {
  2662. x += crtc_state->pipe_src_w - 1;
  2663. }
  2664. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2665. if (INTEL_GEN(dev_priv) < 4)
  2666. intel_crtc->dspaddr_offset = linear_offset;
  2667. intel_crtc->adjusted_x = x;
  2668. intel_crtc->adjusted_y = y;
  2669. I915_WRITE(reg, dspcntr);
  2670. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2671. if (INTEL_GEN(dev_priv) >= 4) {
  2672. I915_WRITE(DSPSURF(plane),
  2673. intel_plane_ggtt_offset(plane_state) +
  2674. intel_crtc->dspaddr_offset);
  2675. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2676. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2677. } else {
  2678. I915_WRITE(DSPADDR(plane),
  2679. intel_plane_ggtt_offset(plane_state) +
  2680. intel_crtc->dspaddr_offset);
  2681. }
  2682. POSTING_READ(reg);
  2683. }
  2684. static void i9xx_disable_primary_plane(struct drm_plane *primary,
  2685. struct drm_crtc *crtc)
  2686. {
  2687. struct drm_device *dev = crtc->dev;
  2688. struct drm_i915_private *dev_priv = to_i915(dev);
  2689. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  2690. int plane = intel_crtc->plane;
  2691. I915_WRITE(DSPCNTR(plane), 0);
  2692. if (INTEL_INFO(dev_priv)->gen >= 4)
  2693. I915_WRITE(DSPSURF(plane), 0);
  2694. else
  2695. I915_WRITE(DSPADDR(plane), 0);
  2696. POSTING_READ(DSPCNTR(plane));
  2697. }
  2698. static void ironlake_update_primary_plane(struct drm_plane *primary,
  2699. const struct intel_crtc_state *crtc_state,
  2700. const struct intel_plane_state *plane_state)
  2701. {
  2702. struct drm_device *dev = primary->dev;
  2703. struct drm_i915_private *dev_priv = to_i915(dev);
  2704. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2705. struct drm_framebuffer *fb = plane_state->base.fb;
  2706. int plane = intel_crtc->plane;
  2707. u32 linear_offset;
  2708. u32 dspcntr;
  2709. i915_reg_t reg = DSPCNTR(plane);
  2710. unsigned int rotation = plane_state->base.rotation;
  2711. int x = plane_state->base.src.x1 >> 16;
  2712. int y = plane_state->base.src.y1 >> 16;
  2713. dspcntr = DISPPLANE_GAMMA_ENABLE;
  2714. dspcntr |= DISPLAY_PLANE_ENABLE;
  2715. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  2716. dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
  2717. switch (fb->format->format) {
  2718. case DRM_FORMAT_C8:
  2719. dspcntr |= DISPPLANE_8BPP;
  2720. break;
  2721. case DRM_FORMAT_RGB565:
  2722. dspcntr |= DISPPLANE_BGRX565;
  2723. break;
  2724. case DRM_FORMAT_XRGB8888:
  2725. dspcntr |= DISPPLANE_BGRX888;
  2726. break;
  2727. case DRM_FORMAT_XBGR8888:
  2728. dspcntr |= DISPPLANE_RGBX888;
  2729. break;
  2730. case DRM_FORMAT_XRGB2101010:
  2731. dspcntr |= DISPPLANE_BGRX101010;
  2732. break;
  2733. case DRM_FORMAT_XBGR2101010:
  2734. dspcntr |= DISPPLANE_RGBX101010;
  2735. break;
  2736. default:
  2737. BUG();
  2738. }
  2739. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  2740. dspcntr |= DISPPLANE_TILED;
  2741. if (rotation & DRM_ROTATE_180)
  2742. dspcntr |= DISPPLANE_ROTATE_180;
  2743. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv))
  2744. dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
  2745. intel_add_fb_offsets(&x, &y, plane_state, 0);
  2746. intel_crtc->dspaddr_offset =
  2747. intel_compute_tile_offset(&x, &y, plane_state, 0);
  2748. /* HSW+ does this automagically in hardware */
  2749. if (!IS_HASWELL(dev_priv) && !IS_BROADWELL(dev_priv) &&
  2750. rotation & DRM_ROTATE_180) {
  2751. x += crtc_state->pipe_src_w - 1;
  2752. y += crtc_state->pipe_src_h - 1;
  2753. }
  2754. linear_offset = intel_fb_xy_to_linear(x, y, plane_state, 0);
  2755. intel_crtc->adjusted_x = x;
  2756. intel_crtc->adjusted_y = y;
  2757. I915_WRITE(reg, dspcntr);
  2758. I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
  2759. I915_WRITE(DSPSURF(plane),
  2760. intel_plane_ggtt_offset(plane_state) +
  2761. intel_crtc->dspaddr_offset);
  2762. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  2763. I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
  2764. } else {
  2765. I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
  2766. I915_WRITE(DSPLINOFF(plane), linear_offset);
  2767. }
  2768. POSTING_READ(reg);
  2769. }
  2770. u32 intel_fb_stride_alignment(const struct drm_i915_private *dev_priv,
  2771. uint64_t fb_modifier, uint32_t pixel_format)
  2772. {
  2773. if (fb_modifier == DRM_FORMAT_MOD_NONE) {
  2774. return 64;
  2775. } else {
  2776. int cpp = drm_format_plane_cpp(pixel_format, 0);
  2777. return intel_tile_width_bytes(dev_priv, fb_modifier, cpp);
  2778. }
  2779. }
  2780. static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
  2781. {
  2782. struct drm_device *dev = intel_crtc->base.dev;
  2783. struct drm_i915_private *dev_priv = to_i915(dev);
  2784. I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
  2785. I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
  2786. I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
  2787. }
  2788. /*
  2789. * This function detaches (aka. unbinds) unused scalers in hardware
  2790. */
  2791. static void skl_detach_scalers(struct intel_crtc *intel_crtc)
  2792. {
  2793. struct intel_crtc_scaler_state *scaler_state;
  2794. int i;
  2795. scaler_state = &intel_crtc->config->scaler_state;
  2796. /* loop through and disable scalers that aren't in use */
  2797. for (i = 0; i < intel_crtc->num_scalers; i++) {
  2798. if (!scaler_state->scalers[i].in_use)
  2799. skl_detach_scaler(intel_crtc, i);
  2800. }
  2801. }
  2802. u32 skl_plane_stride(const struct drm_framebuffer *fb, int plane,
  2803. unsigned int rotation)
  2804. {
  2805. const struct drm_i915_private *dev_priv = to_i915(fb->dev);
  2806. u32 stride = intel_fb_pitch(fb, plane, rotation);
  2807. /*
  2808. * The stride is either expressed as a multiple of 64 bytes chunks for
  2809. * linear buffers or in number of tiles for tiled buffers.
  2810. */
  2811. if (drm_rotation_90_or_270(rotation)) {
  2812. int cpp = fb->format->cpp[plane];
  2813. stride /= intel_tile_height(dev_priv, fb->modifier, cpp);
  2814. } else {
  2815. stride /= intel_fb_stride_alignment(dev_priv, fb->modifier,
  2816. fb->format->format);
  2817. }
  2818. return stride;
  2819. }
  2820. u32 skl_plane_ctl_format(uint32_t pixel_format)
  2821. {
  2822. switch (pixel_format) {
  2823. case DRM_FORMAT_C8:
  2824. return PLANE_CTL_FORMAT_INDEXED;
  2825. case DRM_FORMAT_RGB565:
  2826. return PLANE_CTL_FORMAT_RGB_565;
  2827. case DRM_FORMAT_XBGR8888:
  2828. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
  2829. case DRM_FORMAT_XRGB8888:
  2830. return PLANE_CTL_FORMAT_XRGB_8888;
  2831. /*
  2832. * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
  2833. * to be already pre-multiplied. We need to add a knob (or a different
  2834. * DRM_FORMAT) for user-space to configure that.
  2835. */
  2836. case DRM_FORMAT_ABGR8888:
  2837. return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
  2838. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2839. case DRM_FORMAT_ARGB8888:
  2840. return PLANE_CTL_FORMAT_XRGB_8888 |
  2841. PLANE_CTL_ALPHA_SW_PREMULTIPLY;
  2842. case DRM_FORMAT_XRGB2101010:
  2843. return PLANE_CTL_FORMAT_XRGB_2101010;
  2844. case DRM_FORMAT_XBGR2101010:
  2845. return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
  2846. case DRM_FORMAT_YUYV:
  2847. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
  2848. case DRM_FORMAT_YVYU:
  2849. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
  2850. case DRM_FORMAT_UYVY:
  2851. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
  2852. case DRM_FORMAT_VYUY:
  2853. return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
  2854. default:
  2855. MISSING_CASE(pixel_format);
  2856. }
  2857. return 0;
  2858. }
  2859. u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
  2860. {
  2861. switch (fb_modifier) {
  2862. case DRM_FORMAT_MOD_NONE:
  2863. break;
  2864. case I915_FORMAT_MOD_X_TILED:
  2865. return PLANE_CTL_TILED_X;
  2866. case I915_FORMAT_MOD_Y_TILED:
  2867. return PLANE_CTL_TILED_Y;
  2868. case I915_FORMAT_MOD_Yf_TILED:
  2869. return PLANE_CTL_TILED_YF;
  2870. default:
  2871. MISSING_CASE(fb_modifier);
  2872. }
  2873. return 0;
  2874. }
  2875. u32 skl_plane_ctl_rotation(unsigned int rotation)
  2876. {
  2877. switch (rotation) {
  2878. case DRM_ROTATE_0:
  2879. break;
  2880. /*
  2881. * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
  2882. * while i915 HW rotation is clockwise, thats why this swapping.
  2883. */
  2884. case DRM_ROTATE_90:
  2885. return PLANE_CTL_ROTATE_270;
  2886. case DRM_ROTATE_180:
  2887. return PLANE_CTL_ROTATE_180;
  2888. case DRM_ROTATE_270:
  2889. return PLANE_CTL_ROTATE_90;
  2890. default:
  2891. MISSING_CASE(rotation);
  2892. }
  2893. return 0;
  2894. }
  2895. static void skylake_update_primary_plane(struct drm_plane *plane,
  2896. const struct intel_crtc_state *crtc_state,
  2897. const struct intel_plane_state *plane_state)
  2898. {
  2899. struct drm_device *dev = plane->dev;
  2900. struct drm_i915_private *dev_priv = to_i915(dev);
  2901. struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
  2902. struct drm_framebuffer *fb = plane_state->base.fb;
  2903. enum plane_id plane_id = to_intel_plane(plane)->id;
  2904. enum pipe pipe = to_intel_plane(plane)->pipe;
  2905. u32 plane_ctl;
  2906. unsigned int rotation = plane_state->base.rotation;
  2907. u32 stride = skl_plane_stride(fb, 0, rotation);
  2908. u32 surf_addr = plane_state->main.offset;
  2909. int scaler_id = plane_state->scaler_id;
  2910. int src_x = plane_state->main.x;
  2911. int src_y = plane_state->main.y;
  2912. int src_w = drm_rect_width(&plane_state->base.src) >> 16;
  2913. int src_h = drm_rect_height(&plane_state->base.src) >> 16;
  2914. int dst_x = plane_state->base.dst.x1;
  2915. int dst_y = plane_state->base.dst.y1;
  2916. int dst_w = drm_rect_width(&plane_state->base.dst);
  2917. int dst_h = drm_rect_height(&plane_state->base.dst);
  2918. plane_ctl = PLANE_CTL_ENABLE |
  2919. PLANE_CTL_PIPE_GAMMA_ENABLE |
  2920. PLANE_CTL_PIPE_CSC_ENABLE;
  2921. plane_ctl |= skl_plane_ctl_format(fb->format->format);
  2922. plane_ctl |= skl_plane_ctl_tiling(fb->modifier);
  2923. plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
  2924. plane_ctl |= skl_plane_ctl_rotation(rotation);
  2925. /* Sizes are 0 based */
  2926. src_w--;
  2927. src_h--;
  2928. dst_w--;
  2929. dst_h--;
  2930. intel_crtc->dspaddr_offset = surf_addr;
  2931. intel_crtc->adjusted_x = src_x;
  2932. intel_crtc->adjusted_y = src_y;
  2933. I915_WRITE(PLANE_CTL(pipe, plane_id), plane_ctl);
  2934. I915_WRITE(PLANE_OFFSET(pipe, plane_id), (src_y << 16) | src_x);
  2935. I915_WRITE(PLANE_STRIDE(pipe, plane_id), stride);
  2936. I915_WRITE(PLANE_SIZE(pipe, plane_id), (src_h << 16) | src_w);
  2937. if (scaler_id >= 0) {
  2938. uint32_t ps_ctrl = 0;
  2939. WARN_ON(!dst_w || !dst_h);
  2940. ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(plane_id) |
  2941. crtc_state->scaler_state.scalers[scaler_id].mode;
  2942. I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
  2943. I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
  2944. I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
  2945. I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
  2946. I915_WRITE(PLANE_POS(pipe, plane_id), 0);
  2947. } else {
  2948. I915_WRITE(PLANE_POS(pipe, plane_id), (dst_y << 16) | dst_x);
  2949. }
  2950. I915_WRITE(PLANE_SURF(pipe, plane_id),
  2951. intel_plane_ggtt_offset(plane_state) + surf_addr);
  2952. POSTING_READ(PLANE_SURF(pipe, plane_id));
  2953. }
  2954. static void skylake_disable_primary_plane(struct drm_plane *primary,
  2955. struct drm_crtc *crtc)
  2956. {
  2957. struct drm_device *dev = crtc->dev;
  2958. struct drm_i915_private *dev_priv = to_i915(dev);
  2959. enum plane_id plane_id = to_intel_plane(primary)->id;
  2960. enum pipe pipe = to_intel_plane(primary)->pipe;
  2961. I915_WRITE(PLANE_CTL(pipe, plane_id), 0);
  2962. I915_WRITE(PLANE_SURF(pipe, plane_id), 0);
  2963. POSTING_READ(PLANE_SURF(pipe, plane_id));
  2964. }
  2965. /* Assume fb object is pinned & idle & fenced and just update base pointers */
  2966. static int
  2967. intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  2968. int x, int y, enum mode_set_atomic state)
  2969. {
  2970. /* Support for kgdboc is disabled, this needs a major rework. */
  2971. DRM_ERROR("legacy panic handler not supported any more.\n");
  2972. return -ENODEV;
  2973. }
  2974. static void intel_complete_page_flips(struct drm_i915_private *dev_priv)
  2975. {
  2976. struct intel_crtc *crtc;
  2977. for_each_intel_crtc(&dev_priv->drm, crtc)
  2978. intel_finish_page_flip_cs(dev_priv, crtc->pipe);
  2979. }
  2980. static void intel_update_primary_planes(struct drm_device *dev)
  2981. {
  2982. struct drm_crtc *crtc;
  2983. for_each_crtc(dev, crtc) {
  2984. struct intel_plane *plane = to_intel_plane(crtc->primary);
  2985. struct intel_plane_state *plane_state =
  2986. to_intel_plane_state(plane->base.state);
  2987. if (plane_state->base.visible)
  2988. plane->update_plane(&plane->base,
  2989. to_intel_crtc_state(crtc->state),
  2990. plane_state);
  2991. }
  2992. }
  2993. static int
  2994. __intel_display_resume(struct drm_device *dev,
  2995. struct drm_atomic_state *state)
  2996. {
  2997. struct drm_crtc_state *crtc_state;
  2998. struct drm_crtc *crtc;
  2999. int i, ret;
  3000. intel_modeset_setup_hw_state(dev);
  3001. i915_redisable_vga(to_i915(dev));
  3002. if (!state)
  3003. return 0;
  3004. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  3005. /*
  3006. * Force recalculation even if we restore
  3007. * current state. With fast modeset this may not result
  3008. * in a modeset when the state is compatible.
  3009. */
  3010. crtc_state->mode_changed = true;
  3011. }
  3012. /* ignore any reset values/BIOS leftovers in the WM registers */
  3013. to_intel_atomic_state(state)->skip_intermediate_wm = true;
  3014. ret = drm_atomic_commit(state);
  3015. WARN_ON(ret == -EDEADLK);
  3016. return ret;
  3017. }
  3018. static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
  3019. {
  3020. return intel_has_gpu_reset(dev_priv) &&
  3021. INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv);
  3022. }
  3023. void intel_prepare_reset(struct drm_i915_private *dev_priv)
  3024. {
  3025. struct drm_device *dev = &dev_priv->drm;
  3026. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3027. struct drm_atomic_state *state;
  3028. int ret;
  3029. /*
  3030. * Need mode_config.mutex so that we don't
  3031. * trample ongoing ->detect() and whatnot.
  3032. */
  3033. mutex_lock(&dev->mode_config.mutex);
  3034. drm_modeset_acquire_init(ctx, 0);
  3035. while (1) {
  3036. ret = drm_modeset_lock_all_ctx(dev, ctx);
  3037. if (ret != -EDEADLK)
  3038. break;
  3039. drm_modeset_backoff(ctx);
  3040. }
  3041. /* reset doesn't touch the display, but flips might get nuked anyway, */
  3042. if (!i915.force_reset_modeset_test &&
  3043. !gpu_reset_clobbers_display(dev_priv))
  3044. return;
  3045. /*
  3046. * Disabling the crtcs gracefully seems nicer. Also the
  3047. * g33 docs say we should at least disable all the planes.
  3048. */
  3049. state = drm_atomic_helper_duplicate_state(dev, ctx);
  3050. if (IS_ERR(state)) {
  3051. ret = PTR_ERR(state);
  3052. DRM_ERROR("Duplicating state failed with %i\n", ret);
  3053. return;
  3054. }
  3055. ret = drm_atomic_helper_disable_all(dev, ctx);
  3056. if (ret) {
  3057. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  3058. drm_atomic_state_put(state);
  3059. return;
  3060. }
  3061. dev_priv->modeset_restore_state = state;
  3062. state->acquire_ctx = ctx;
  3063. }
  3064. void intel_finish_reset(struct drm_i915_private *dev_priv)
  3065. {
  3066. struct drm_device *dev = &dev_priv->drm;
  3067. struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
  3068. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  3069. int ret;
  3070. /*
  3071. * Flips in the rings will be nuked by the reset,
  3072. * so complete all pending flips so that user space
  3073. * will get its events and not get stuck.
  3074. */
  3075. intel_complete_page_flips(dev_priv);
  3076. dev_priv->modeset_restore_state = NULL;
  3077. /* reset doesn't touch the display */
  3078. if (!gpu_reset_clobbers_display(dev_priv)) {
  3079. if (!state) {
  3080. /*
  3081. * Flips in the rings have been nuked by the reset,
  3082. * so update the base address of all primary
  3083. * planes to the the last fb to make sure we're
  3084. * showing the correct fb after a reset.
  3085. *
  3086. * FIXME: Atomic will make this obsolete since we won't schedule
  3087. * CS-based flips (which might get lost in gpu resets) any more.
  3088. */
  3089. intel_update_primary_planes(dev);
  3090. } else {
  3091. ret = __intel_display_resume(dev, state);
  3092. if (ret)
  3093. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3094. }
  3095. } else {
  3096. /*
  3097. * The display has been reset as well,
  3098. * so need a full re-initialization.
  3099. */
  3100. intel_runtime_pm_disable_interrupts(dev_priv);
  3101. intel_runtime_pm_enable_interrupts(dev_priv);
  3102. intel_pps_unlock_regs_wa(dev_priv);
  3103. intel_modeset_init_hw(dev);
  3104. spin_lock_irq(&dev_priv->irq_lock);
  3105. if (dev_priv->display.hpd_irq_setup)
  3106. dev_priv->display.hpd_irq_setup(dev_priv);
  3107. spin_unlock_irq(&dev_priv->irq_lock);
  3108. ret = __intel_display_resume(dev, state);
  3109. if (ret)
  3110. DRM_ERROR("Restoring old state failed with %i\n", ret);
  3111. intel_hpd_init(dev_priv);
  3112. }
  3113. if (state)
  3114. drm_atomic_state_put(state);
  3115. drm_modeset_drop_locks(ctx);
  3116. drm_modeset_acquire_fini(ctx);
  3117. mutex_unlock(&dev->mode_config.mutex);
  3118. }
  3119. static bool abort_flip_on_reset(struct intel_crtc *crtc)
  3120. {
  3121. struct i915_gpu_error *error = &to_i915(crtc->base.dev)->gpu_error;
  3122. if (i915_reset_in_progress(error))
  3123. return true;
  3124. if (crtc->reset_count != i915_reset_count(error))
  3125. return true;
  3126. return false;
  3127. }
  3128. static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
  3129. {
  3130. struct drm_device *dev = crtc->dev;
  3131. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3132. bool pending;
  3133. if (abort_flip_on_reset(intel_crtc))
  3134. return false;
  3135. spin_lock_irq(&dev->event_lock);
  3136. pending = to_intel_crtc(crtc)->flip_work != NULL;
  3137. spin_unlock_irq(&dev->event_lock);
  3138. return pending;
  3139. }
  3140. static void intel_update_pipe_config(struct intel_crtc *crtc,
  3141. struct intel_crtc_state *old_crtc_state)
  3142. {
  3143. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  3144. struct intel_crtc_state *pipe_config =
  3145. to_intel_crtc_state(crtc->base.state);
  3146. /* drm_atomic_helper_update_legacy_modeset_state might not be called. */
  3147. crtc->base.mode = crtc->base.state->mode;
  3148. DRM_DEBUG_KMS("Updating pipe size %ix%i -> %ix%i\n",
  3149. old_crtc_state->pipe_src_w, old_crtc_state->pipe_src_h,
  3150. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  3151. /*
  3152. * Update pipe size and adjust fitter if needed: the reason for this is
  3153. * that in compute_mode_changes we check the native mode (not the pfit
  3154. * mode) to see if we can flip rather than do a full mode set. In the
  3155. * fastboot case, we'll flip, but if we don't update the pipesrc and
  3156. * pfit state, we'll end up with a big fb scanned out into the wrong
  3157. * sized surface.
  3158. */
  3159. I915_WRITE(PIPESRC(crtc->pipe),
  3160. ((pipe_config->pipe_src_w - 1) << 16) |
  3161. (pipe_config->pipe_src_h - 1));
  3162. /* on skylake this is done by detaching scalers */
  3163. if (INTEL_GEN(dev_priv) >= 9) {
  3164. skl_detach_scalers(crtc);
  3165. if (pipe_config->pch_pfit.enabled)
  3166. skylake_pfit_enable(crtc);
  3167. } else if (HAS_PCH_SPLIT(dev_priv)) {
  3168. if (pipe_config->pch_pfit.enabled)
  3169. ironlake_pfit_enable(crtc);
  3170. else if (old_crtc_state->pch_pfit.enabled)
  3171. ironlake_pfit_disable(crtc, true);
  3172. }
  3173. }
  3174. static void intel_fdi_normal_train(struct drm_crtc *crtc)
  3175. {
  3176. struct drm_device *dev = crtc->dev;
  3177. struct drm_i915_private *dev_priv = to_i915(dev);
  3178. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3179. int pipe = intel_crtc->pipe;
  3180. i915_reg_t reg;
  3181. u32 temp;
  3182. /* enable normal train */
  3183. reg = FDI_TX_CTL(pipe);
  3184. temp = I915_READ(reg);
  3185. if (IS_IVYBRIDGE(dev_priv)) {
  3186. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3187. temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
  3188. } else {
  3189. temp &= ~FDI_LINK_TRAIN_NONE;
  3190. temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
  3191. }
  3192. I915_WRITE(reg, temp);
  3193. reg = FDI_RX_CTL(pipe);
  3194. temp = I915_READ(reg);
  3195. if (HAS_PCH_CPT(dev_priv)) {
  3196. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3197. temp |= FDI_LINK_TRAIN_NORMAL_CPT;
  3198. } else {
  3199. temp &= ~FDI_LINK_TRAIN_NONE;
  3200. temp |= FDI_LINK_TRAIN_NONE;
  3201. }
  3202. I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
  3203. /* wait one idle pattern time */
  3204. POSTING_READ(reg);
  3205. udelay(1000);
  3206. /* IVB wants error correction enabled */
  3207. if (IS_IVYBRIDGE(dev_priv))
  3208. I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
  3209. FDI_FE_ERRC_ENABLE);
  3210. }
  3211. /* The FDI link training functions for ILK/Ibexpeak. */
  3212. static void ironlake_fdi_link_train(struct drm_crtc *crtc)
  3213. {
  3214. struct drm_device *dev = crtc->dev;
  3215. struct drm_i915_private *dev_priv = to_i915(dev);
  3216. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3217. int pipe = intel_crtc->pipe;
  3218. i915_reg_t reg;
  3219. u32 temp, tries;
  3220. /* FDI needs bits from pipe first */
  3221. assert_pipe_enabled(dev_priv, pipe);
  3222. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3223. for train result */
  3224. reg = FDI_RX_IMR(pipe);
  3225. temp = I915_READ(reg);
  3226. temp &= ~FDI_RX_SYMBOL_LOCK;
  3227. temp &= ~FDI_RX_BIT_LOCK;
  3228. I915_WRITE(reg, temp);
  3229. I915_READ(reg);
  3230. udelay(150);
  3231. /* enable CPU FDI TX and PCH FDI RX */
  3232. reg = FDI_TX_CTL(pipe);
  3233. temp = I915_READ(reg);
  3234. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3235. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3236. temp &= ~FDI_LINK_TRAIN_NONE;
  3237. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3238. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3239. reg = FDI_RX_CTL(pipe);
  3240. temp = I915_READ(reg);
  3241. temp &= ~FDI_LINK_TRAIN_NONE;
  3242. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3243. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3244. POSTING_READ(reg);
  3245. udelay(150);
  3246. /* Ironlake workaround, enable clock pointer after FDI enable*/
  3247. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3248. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
  3249. FDI_RX_PHASE_SYNC_POINTER_EN);
  3250. reg = FDI_RX_IIR(pipe);
  3251. for (tries = 0; tries < 5; tries++) {
  3252. temp = I915_READ(reg);
  3253. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3254. if ((temp & FDI_RX_BIT_LOCK)) {
  3255. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3256. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3257. break;
  3258. }
  3259. }
  3260. if (tries == 5)
  3261. DRM_ERROR("FDI train 1 fail!\n");
  3262. /* Train 2 */
  3263. reg = FDI_TX_CTL(pipe);
  3264. temp = I915_READ(reg);
  3265. temp &= ~FDI_LINK_TRAIN_NONE;
  3266. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3267. I915_WRITE(reg, temp);
  3268. reg = FDI_RX_CTL(pipe);
  3269. temp = I915_READ(reg);
  3270. temp &= ~FDI_LINK_TRAIN_NONE;
  3271. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3272. I915_WRITE(reg, temp);
  3273. POSTING_READ(reg);
  3274. udelay(150);
  3275. reg = FDI_RX_IIR(pipe);
  3276. for (tries = 0; tries < 5; tries++) {
  3277. temp = I915_READ(reg);
  3278. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3279. if (temp & FDI_RX_SYMBOL_LOCK) {
  3280. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3281. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3282. break;
  3283. }
  3284. }
  3285. if (tries == 5)
  3286. DRM_ERROR("FDI train 2 fail!\n");
  3287. DRM_DEBUG_KMS("FDI train done\n");
  3288. }
  3289. static const int snb_b_fdi_train_param[] = {
  3290. FDI_LINK_TRAIN_400MV_0DB_SNB_B,
  3291. FDI_LINK_TRAIN_400MV_6DB_SNB_B,
  3292. FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
  3293. FDI_LINK_TRAIN_800MV_0DB_SNB_B,
  3294. };
  3295. /* The FDI link training functions for SNB/Cougarpoint. */
  3296. static void gen6_fdi_link_train(struct drm_crtc *crtc)
  3297. {
  3298. struct drm_device *dev = crtc->dev;
  3299. struct drm_i915_private *dev_priv = to_i915(dev);
  3300. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3301. int pipe = intel_crtc->pipe;
  3302. i915_reg_t reg;
  3303. u32 temp, i, retry;
  3304. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3305. for train result */
  3306. reg = FDI_RX_IMR(pipe);
  3307. temp = I915_READ(reg);
  3308. temp &= ~FDI_RX_SYMBOL_LOCK;
  3309. temp &= ~FDI_RX_BIT_LOCK;
  3310. I915_WRITE(reg, temp);
  3311. POSTING_READ(reg);
  3312. udelay(150);
  3313. /* enable CPU FDI TX and PCH FDI RX */
  3314. reg = FDI_TX_CTL(pipe);
  3315. temp = I915_READ(reg);
  3316. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3317. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3318. temp &= ~FDI_LINK_TRAIN_NONE;
  3319. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3320. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3321. /* SNB-B */
  3322. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3323. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3324. I915_WRITE(FDI_RX_MISC(pipe),
  3325. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3326. reg = FDI_RX_CTL(pipe);
  3327. temp = I915_READ(reg);
  3328. if (HAS_PCH_CPT(dev_priv)) {
  3329. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3330. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3331. } else {
  3332. temp &= ~FDI_LINK_TRAIN_NONE;
  3333. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3334. }
  3335. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3336. POSTING_READ(reg);
  3337. udelay(150);
  3338. for (i = 0; i < 4; i++) {
  3339. reg = FDI_TX_CTL(pipe);
  3340. temp = I915_READ(reg);
  3341. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3342. temp |= snb_b_fdi_train_param[i];
  3343. I915_WRITE(reg, temp);
  3344. POSTING_READ(reg);
  3345. udelay(500);
  3346. for (retry = 0; retry < 5; retry++) {
  3347. reg = FDI_RX_IIR(pipe);
  3348. temp = I915_READ(reg);
  3349. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3350. if (temp & FDI_RX_BIT_LOCK) {
  3351. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3352. DRM_DEBUG_KMS("FDI train 1 done.\n");
  3353. break;
  3354. }
  3355. udelay(50);
  3356. }
  3357. if (retry < 5)
  3358. break;
  3359. }
  3360. if (i == 4)
  3361. DRM_ERROR("FDI train 1 fail!\n");
  3362. /* Train 2 */
  3363. reg = FDI_TX_CTL(pipe);
  3364. temp = I915_READ(reg);
  3365. temp &= ~FDI_LINK_TRAIN_NONE;
  3366. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3367. if (IS_GEN6(dev_priv)) {
  3368. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3369. /* SNB-B */
  3370. temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
  3371. }
  3372. I915_WRITE(reg, temp);
  3373. reg = FDI_RX_CTL(pipe);
  3374. temp = I915_READ(reg);
  3375. if (HAS_PCH_CPT(dev_priv)) {
  3376. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3377. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3378. } else {
  3379. temp &= ~FDI_LINK_TRAIN_NONE;
  3380. temp |= FDI_LINK_TRAIN_PATTERN_2;
  3381. }
  3382. I915_WRITE(reg, temp);
  3383. POSTING_READ(reg);
  3384. udelay(150);
  3385. for (i = 0; i < 4; i++) {
  3386. reg = FDI_TX_CTL(pipe);
  3387. temp = I915_READ(reg);
  3388. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3389. temp |= snb_b_fdi_train_param[i];
  3390. I915_WRITE(reg, temp);
  3391. POSTING_READ(reg);
  3392. udelay(500);
  3393. for (retry = 0; retry < 5; retry++) {
  3394. reg = FDI_RX_IIR(pipe);
  3395. temp = I915_READ(reg);
  3396. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3397. if (temp & FDI_RX_SYMBOL_LOCK) {
  3398. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3399. DRM_DEBUG_KMS("FDI train 2 done.\n");
  3400. break;
  3401. }
  3402. udelay(50);
  3403. }
  3404. if (retry < 5)
  3405. break;
  3406. }
  3407. if (i == 4)
  3408. DRM_ERROR("FDI train 2 fail!\n");
  3409. DRM_DEBUG_KMS("FDI train done.\n");
  3410. }
  3411. /* Manual link training for Ivy Bridge A0 parts */
  3412. static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
  3413. {
  3414. struct drm_device *dev = crtc->dev;
  3415. struct drm_i915_private *dev_priv = to_i915(dev);
  3416. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3417. int pipe = intel_crtc->pipe;
  3418. i915_reg_t reg;
  3419. u32 temp, i, j;
  3420. /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
  3421. for train result */
  3422. reg = FDI_RX_IMR(pipe);
  3423. temp = I915_READ(reg);
  3424. temp &= ~FDI_RX_SYMBOL_LOCK;
  3425. temp &= ~FDI_RX_BIT_LOCK;
  3426. I915_WRITE(reg, temp);
  3427. POSTING_READ(reg);
  3428. udelay(150);
  3429. DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
  3430. I915_READ(FDI_RX_IIR(pipe)));
  3431. /* Try each vswing and preemphasis setting twice before moving on */
  3432. for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
  3433. /* disable first in case we need to retry */
  3434. reg = FDI_TX_CTL(pipe);
  3435. temp = I915_READ(reg);
  3436. temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
  3437. temp &= ~FDI_TX_ENABLE;
  3438. I915_WRITE(reg, temp);
  3439. reg = FDI_RX_CTL(pipe);
  3440. temp = I915_READ(reg);
  3441. temp &= ~FDI_LINK_TRAIN_AUTO;
  3442. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3443. temp &= ~FDI_RX_ENABLE;
  3444. I915_WRITE(reg, temp);
  3445. /* enable CPU FDI TX and PCH FDI RX */
  3446. reg = FDI_TX_CTL(pipe);
  3447. temp = I915_READ(reg);
  3448. temp &= ~FDI_DP_PORT_WIDTH_MASK;
  3449. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3450. temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
  3451. temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
  3452. temp |= snb_b_fdi_train_param[j/2];
  3453. temp |= FDI_COMPOSITE_SYNC;
  3454. I915_WRITE(reg, temp | FDI_TX_ENABLE);
  3455. I915_WRITE(FDI_RX_MISC(pipe),
  3456. FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
  3457. reg = FDI_RX_CTL(pipe);
  3458. temp = I915_READ(reg);
  3459. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3460. temp |= FDI_COMPOSITE_SYNC;
  3461. I915_WRITE(reg, temp | FDI_RX_ENABLE);
  3462. POSTING_READ(reg);
  3463. udelay(1); /* should be 0.5us */
  3464. for (i = 0; i < 4; i++) {
  3465. reg = FDI_RX_IIR(pipe);
  3466. temp = I915_READ(reg);
  3467. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3468. if (temp & FDI_RX_BIT_LOCK ||
  3469. (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
  3470. I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
  3471. DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
  3472. i);
  3473. break;
  3474. }
  3475. udelay(1); /* should be 0.5us */
  3476. }
  3477. if (i == 4) {
  3478. DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
  3479. continue;
  3480. }
  3481. /* Train 2 */
  3482. reg = FDI_TX_CTL(pipe);
  3483. temp = I915_READ(reg);
  3484. temp &= ~FDI_LINK_TRAIN_NONE_IVB;
  3485. temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
  3486. I915_WRITE(reg, temp);
  3487. reg = FDI_RX_CTL(pipe);
  3488. temp = I915_READ(reg);
  3489. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3490. temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
  3491. I915_WRITE(reg, temp);
  3492. POSTING_READ(reg);
  3493. udelay(2); /* should be 1.5us */
  3494. for (i = 0; i < 4; i++) {
  3495. reg = FDI_RX_IIR(pipe);
  3496. temp = I915_READ(reg);
  3497. DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
  3498. if (temp & FDI_RX_SYMBOL_LOCK ||
  3499. (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
  3500. I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
  3501. DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
  3502. i);
  3503. goto train_done;
  3504. }
  3505. udelay(2); /* should be 1.5us */
  3506. }
  3507. if (i == 4)
  3508. DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
  3509. }
  3510. train_done:
  3511. DRM_DEBUG_KMS("FDI train done.\n");
  3512. }
  3513. static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
  3514. {
  3515. struct drm_device *dev = intel_crtc->base.dev;
  3516. struct drm_i915_private *dev_priv = to_i915(dev);
  3517. int pipe = intel_crtc->pipe;
  3518. i915_reg_t reg;
  3519. u32 temp;
  3520. /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
  3521. reg = FDI_RX_CTL(pipe);
  3522. temp = I915_READ(reg);
  3523. temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
  3524. temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
  3525. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3526. I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
  3527. POSTING_READ(reg);
  3528. udelay(200);
  3529. /* Switch from Rawclk to PCDclk */
  3530. temp = I915_READ(reg);
  3531. I915_WRITE(reg, temp | FDI_PCDCLK);
  3532. POSTING_READ(reg);
  3533. udelay(200);
  3534. /* Enable CPU FDI TX PLL, always on for Ironlake */
  3535. reg = FDI_TX_CTL(pipe);
  3536. temp = I915_READ(reg);
  3537. if ((temp & FDI_TX_PLL_ENABLE) == 0) {
  3538. I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
  3539. POSTING_READ(reg);
  3540. udelay(100);
  3541. }
  3542. }
  3543. static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
  3544. {
  3545. struct drm_device *dev = intel_crtc->base.dev;
  3546. struct drm_i915_private *dev_priv = to_i915(dev);
  3547. int pipe = intel_crtc->pipe;
  3548. i915_reg_t reg;
  3549. u32 temp;
  3550. /* Switch from PCDclk to Rawclk */
  3551. reg = FDI_RX_CTL(pipe);
  3552. temp = I915_READ(reg);
  3553. I915_WRITE(reg, temp & ~FDI_PCDCLK);
  3554. /* Disable CPU FDI TX PLL */
  3555. reg = FDI_TX_CTL(pipe);
  3556. temp = I915_READ(reg);
  3557. I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
  3558. POSTING_READ(reg);
  3559. udelay(100);
  3560. reg = FDI_RX_CTL(pipe);
  3561. temp = I915_READ(reg);
  3562. I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
  3563. /* Wait for the clocks to turn off. */
  3564. POSTING_READ(reg);
  3565. udelay(100);
  3566. }
  3567. static void ironlake_fdi_disable(struct drm_crtc *crtc)
  3568. {
  3569. struct drm_device *dev = crtc->dev;
  3570. struct drm_i915_private *dev_priv = to_i915(dev);
  3571. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3572. int pipe = intel_crtc->pipe;
  3573. i915_reg_t reg;
  3574. u32 temp;
  3575. /* disable CPU FDI tx and PCH FDI rx */
  3576. reg = FDI_TX_CTL(pipe);
  3577. temp = I915_READ(reg);
  3578. I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
  3579. POSTING_READ(reg);
  3580. reg = FDI_RX_CTL(pipe);
  3581. temp = I915_READ(reg);
  3582. temp &= ~(0x7 << 16);
  3583. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3584. I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
  3585. POSTING_READ(reg);
  3586. udelay(100);
  3587. /* Ironlake workaround, disable clock pointer after downing FDI */
  3588. if (HAS_PCH_IBX(dev_priv))
  3589. I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
  3590. /* still set train pattern 1 */
  3591. reg = FDI_TX_CTL(pipe);
  3592. temp = I915_READ(reg);
  3593. temp &= ~FDI_LINK_TRAIN_NONE;
  3594. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3595. I915_WRITE(reg, temp);
  3596. reg = FDI_RX_CTL(pipe);
  3597. temp = I915_READ(reg);
  3598. if (HAS_PCH_CPT(dev_priv)) {
  3599. temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
  3600. temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
  3601. } else {
  3602. temp &= ~FDI_LINK_TRAIN_NONE;
  3603. temp |= FDI_LINK_TRAIN_PATTERN_1;
  3604. }
  3605. /* BPC in FDI rx is consistent with that in PIPECONF */
  3606. temp &= ~(0x07 << 16);
  3607. temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
  3608. I915_WRITE(reg, temp);
  3609. POSTING_READ(reg);
  3610. udelay(100);
  3611. }
  3612. bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
  3613. {
  3614. struct intel_crtc *crtc;
  3615. /* Note that we don't need to be called with mode_config.lock here
  3616. * as our list of CRTC objects is static for the lifetime of the
  3617. * device and so cannot disappear as we iterate. Similarly, we can
  3618. * happily treat the predicates as racy, atomic checks as userspace
  3619. * cannot claim and pin a new fb without at least acquring the
  3620. * struct_mutex and so serialising with us.
  3621. */
  3622. for_each_intel_crtc(&dev_priv->drm, crtc) {
  3623. if (atomic_read(&crtc->unpin_work_count) == 0)
  3624. continue;
  3625. if (crtc->flip_work)
  3626. intel_wait_for_vblank(dev_priv, crtc->pipe);
  3627. return true;
  3628. }
  3629. return false;
  3630. }
  3631. static void page_flip_completed(struct intel_crtc *intel_crtc)
  3632. {
  3633. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  3634. struct intel_flip_work *work = intel_crtc->flip_work;
  3635. intel_crtc->flip_work = NULL;
  3636. if (work->event)
  3637. drm_crtc_send_vblank_event(&intel_crtc->base, work->event);
  3638. drm_crtc_vblank_put(&intel_crtc->base);
  3639. wake_up_all(&dev_priv->pending_flip_queue);
  3640. queue_work(dev_priv->wq, &work->unpin_work);
  3641. trace_i915_flip_complete(intel_crtc->plane,
  3642. work->pending_flip_obj);
  3643. }
  3644. static int intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
  3645. {
  3646. struct drm_device *dev = crtc->dev;
  3647. struct drm_i915_private *dev_priv = to_i915(dev);
  3648. long ret;
  3649. WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
  3650. ret = wait_event_interruptible_timeout(
  3651. dev_priv->pending_flip_queue,
  3652. !intel_crtc_has_pending_flip(crtc),
  3653. 60*HZ);
  3654. if (ret < 0)
  3655. return ret;
  3656. if (ret == 0) {
  3657. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3658. struct intel_flip_work *work;
  3659. spin_lock_irq(&dev->event_lock);
  3660. work = intel_crtc->flip_work;
  3661. if (work && !is_mmio_work(work)) {
  3662. WARN_ONCE(1, "Removing stuck page flip\n");
  3663. page_flip_completed(intel_crtc);
  3664. }
  3665. spin_unlock_irq(&dev->event_lock);
  3666. }
  3667. return 0;
  3668. }
  3669. void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
  3670. {
  3671. u32 temp;
  3672. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
  3673. mutex_lock(&dev_priv->sb_lock);
  3674. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3675. temp |= SBI_SSCCTL_DISABLE;
  3676. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3677. mutex_unlock(&dev_priv->sb_lock);
  3678. }
  3679. /* Program iCLKIP clock to the desired frequency */
  3680. static void lpt_program_iclkip(struct drm_crtc *crtc)
  3681. {
  3682. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  3683. int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
  3684. u32 divsel, phaseinc, auxdiv, phasedir = 0;
  3685. u32 temp;
  3686. lpt_disable_iclkip(dev_priv);
  3687. /* The iCLK virtual clock root frequency is in MHz,
  3688. * but the adjusted_mode->crtc_clock in in KHz. To get the
  3689. * divisors, it is necessary to divide one by another, so we
  3690. * convert the virtual clock precision to KHz here for higher
  3691. * precision.
  3692. */
  3693. for (auxdiv = 0; auxdiv < 2; auxdiv++) {
  3694. u32 iclk_virtual_root_freq = 172800 * 1000;
  3695. u32 iclk_pi_range = 64;
  3696. u32 desired_divisor;
  3697. desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3698. clock << auxdiv);
  3699. divsel = (desired_divisor / iclk_pi_range) - 2;
  3700. phaseinc = desired_divisor % iclk_pi_range;
  3701. /*
  3702. * Near 20MHz is a corner case which is
  3703. * out of range for the 7-bit divisor
  3704. */
  3705. if (divsel <= 0x7f)
  3706. break;
  3707. }
  3708. /* This should not happen with any sane values */
  3709. WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
  3710. ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
  3711. WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
  3712. ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
  3713. DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
  3714. clock,
  3715. auxdiv,
  3716. divsel,
  3717. phasedir,
  3718. phaseinc);
  3719. mutex_lock(&dev_priv->sb_lock);
  3720. /* Program SSCDIVINTPHASE6 */
  3721. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3722. temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
  3723. temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
  3724. temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
  3725. temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
  3726. temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
  3727. temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
  3728. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
  3729. /* Program SSCAUXDIV */
  3730. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3731. temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
  3732. temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
  3733. intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
  3734. /* Enable modulator and associated divider */
  3735. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3736. temp &= ~SBI_SSCCTL_DISABLE;
  3737. intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
  3738. mutex_unlock(&dev_priv->sb_lock);
  3739. /* Wait for initialization time */
  3740. udelay(24);
  3741. I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
  3742. }
  3743. int lpt_get_iclkip(struct drm_i915_private *dev_priv)
  3744. {
  3745. u32 divsel, phaseinc, auxdiv;
  3746. u32 iclk_virtual_root_freq = 172800 * 1000;
  3747. u32 iclk_pi_range = 64;
  3748. u32 desired_divisor;
  3749. u32 temp;
  3750. if ((I915_READ(PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
  3751. return 0;
  3752. mutex_lock(&dev_priv->sb_lock);
  3753. temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
  3754. if (temp & SBI_SSCCTL_DISABLE) {
  3755. mutex_unlock(&dev_priv->sb_lock);
  3756. return 0;
  3757. }
  3758. temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
  3759. divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
  3760. SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
  3761. phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
  3762. SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
  3763. temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
  3764. auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
  3765. SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
  3766. mutex_unlock(&dev_priv->sb_lock);
  3767. desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
  3768. return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
  3769. desired_divisor << auxdiv);
  3770. }
  3771. static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
  3772. enum pipe pch_transcoder)
  3773. {
  3774. struct drm_device *dev = crtc->base.dev;
  3775. struct drm_i915_private *dev_priv = to_i915(dev);
  3776. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  3777. I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
  3778. I915_READ(HTOTAL(cpu_transcoder)));
  3779. I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
  3780. I915_READ(HBLANK(cpu_transcoder)));
  3781. I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
  3782. I915_READ(HSYNC(cpu_transcoder)));
  3783. I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
  3784. I915_READ(VTOTAL(cpu_transcoder)));
  3785. I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
  3786. I915_READ(VBLANK(cpu_transcoder)));
  3787. I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
  3788. I915_READ(VSYNC(cpu_transcoder)));
  3789. I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
  3790. I915_READ(VSYNCSHIFT(cpu_transcoder)));
  3791. }
  3792. static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
  3793. {
  3794. struct drm_i915_private *dev_priv = to_i915(dev);
  3795. uint32_t temp;
  3796. temp = I915_READ(SOUTH_CHICKEN1);
  3797. if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
  3798. return;
  3799. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
  3800. WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
  3801. temp &= ~FDI_BC_BIFURCATION_SELECT;
  3802. if (enable)
  3803. temp |= FDI_BC_BIFURCATION_SELECT;
  3804. DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
  3805. I915_WRITE(SOUTH_CHICKEN1, temp);
  3806. POSTING_READ(SOUTH_CHICKEN1);
  3807. }
  3808. static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
  3809. {
  3810. struct drm_device *dev = intel_crtc->base.dev;
  3811. switch (intel_crtc->pipe) {
  3812. case PIPE_A:
  3813. break;
  3814. case PIPE_B:
  3815. if (intel_crtc->config->fdi_lanes > 2)
  3816. cpt_set_fdi_bc_bifurcation(dev, false);
  3817. else
  3818. cpt_set_fdi_bc_bifurcation(dev, true);
  3819. break;
  3820. case PIPE_C:
  3821. cpt_set_fdi_bc_bifurcation(dev, true);
  3822. break;
  3823. default:
  3824. BUG();
  3825. }
  3826. }
  3827. /* Return which DP Port should be selected for Transcoder DP control */
  3828. static enum port
  3829. intel_trans_dp_port_sel(struct drm_crtc *crtc)
  3830. {
  3831. struct drm_device *dev = crtc->dev;
  3832. struct intel_encoder *encoder;
  3833. for_each_encoder_on_crtc(dev, crtc, encoder) {
  3834. if (encoder->type == INTEL_OUTPUT_DP ||
  3835. encoder->type == INTEL_OUTPUT_EDP)
  3836. return enc_to_dig_port(&encoder->base)->port;
  3837. }
  3838. return -1;
  3839. }
  3840. /*
  3841. * Enable PCH resources required for PCH ports:
  3842. * - PCH PLLs
  3843. * - FDI training & RX/TX
  3844. * - update transcoder timings
  3845. * - DP transcoding bits
  3846. * - transcoder
  3847. */
  3848. static void ironlake_pch_enable(struct drm_crtc *crtc)
  3849. {
  3850. struct drm_device *dev = crtc->dev;
  3851. struct drm_i915_private *dev_priv = to_i915(dev);
  3852. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3853. int pipe = intel_crtc->pipe;
  3854. u32 temp;
  3855. assert_pch_transcoder_disabled(dev_priv, pipe);
  3856. if (IS_IVYBRIDGE(dev_priv))
  3857. ivybridge_update_fdi_bc_bifurcation(intel_crtc);
  3858. /* Write the TU size bits before fdi link training, so that error
  3859. * detection works. */
  3860. I915_WRITE(FDI_RX_TUSIZE1(pipe),
  3861. I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
  3862. /* For PCH output, training FDI link */
  3863. dev_priv->display.fdi_link_train(crtc);
  3864. /* We need to program the right clock selection before writing the pixel
  3865. * mutliplier into the DPLL. */
  3866. if (HAS_PCH_CPT(dev_priv)) {
  3867. u32 sel;
  3868. temp = I915_READ(PCH_DPLL_SEL);
  3869. temp |= TRANS_DPLL_ENABLE(pipe);
  3870. sel = TRANS_DPLLB_SEL(pipe);
  3871. if (intel_crtc->config->shared_dpll ==
  3872. intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
  3873. temp |= sel;
  3874. else
  3875. temp &= ~sel;
  3876. I915_WRITE(PCH_DPLL_SEL, temp);
  3877. }
  3878. /* XXX: pch pll's can be enabled any time before we enable the PCH
  3879. * transcoder, and we actually should do this to not upset any PCH
  3880. * transcoder that already use the clock when we share it.
  3881. *
  3882. * Note that enable_shared_dpll tries to do the right thing, but
  3883. * get_shared_dpll unconditionally resets the pll - we need that to have
  3884. * the right LVDS enable sequence. */
  3885. intel_enable_shared_dpll(intel_crtc);
  3886. /* set transcoder timing, panel must allow it */
  3887. assert_panel_unlocked(dev_priv, pipe);
  3888. ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
  3889. intel_fdi_normal_train(crtc);
  3890. /* For PCH DP, enable TRANS_DP_CTL */
  3891. if (HAS_PCH_CPT(dev_priv) &&
  3892. intel_crtc_has_dp_encoder(intel_crtc->config)) {
  3893. const struct drm_display_mode *adjusted_mode =
  3894. &intel_crtc->config->base.adjusted_mode;
  3895. u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
  3896. i915_reg_t reg = TRANS_DP_CTL(pipe);
  3897. temp = I915_READ(reg);
  3898. temp &= ~(TRANS_DP_PORT_SEL_MASK |
  3899. TRANS_DP_SYNC_MASK |
  3900. TRANS_DP_BPC_MASK);
  3901. temp |= TRANS_DP_OUTPUT_ENABLE;
  3902. temp |= bpc << 9; /* same format but at 11:9 */
  3903. if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
  3904. temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
  3905. if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
  3906. temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
  3907. switch (intel_trans_dp_port_sel(crtc)) {
  3908. case PORT_B:
  3909. temp |= TRANS_DP_PORT_SEL_B;
  3910. break;
  3911. case PORT_C:
  3912. temp |= TRANS_DP_PORT_SEL_C;
  3913. break;
  3914. case PORT_D:
  3915. temp |= TRANS_DP_PORT_SEL_D;
  3916. break;
  3917. default:
  3918. BUG();
  3919. }
  3920. I915_WRITE(reg, temp);
  3921. }
  3922. ironlake_enable_pch_transcoder(dev_priv, pipe);
  3923. }
  3924. static void lpt_pch_enable(struct drm_crtc *crtc)
  3925. {
  3926. struct drm_device *dev = crtc->dev;
  3927. struct drm_i915_private *dev_priv = to_i915(dev);
  3928. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  3929. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  3930. assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
  3931. lpt_program_iclkip(crtc);
  3932. /* Set transcoder timing. */
  3933. ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
  3934. lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
  3935. }
  3936. static void cpt_verify_modeset(struct drm_device *dev, int pipe)
  3937. {
  3938. struct drm_i915_private *dev_priv = to_i915(dev);
  3939. i915_reg_t dslreg = PIPEDSL(pipe);
  3940. u32 temp;
  3941. temp = I915_READ(dslreg);
  3942. udelay(500);
  3943. if (wait_for(I915_READ(dslreg) != temp, 5)) {
  3944. if (wait_for(I915_READ(dslreg) != temp, 5))
  3945. DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
  3946. }
  3947. }
  3948. static int
  3949. skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
  3950. unsigned scaler_user, int *scaler_id, unsigned int rotation,
  3951. int src_w, int src_h, int dst_w, int dst_h)
  3952. {
  3953. struct intel_crtc_scaler_state *scaler_state =
  3954. &crtc_state->scaler_state;
  3955. struct intel_crtc *intel_crtc =
  3956. to_intel_crtc(crtc_state->base.crtc);
  3957. int need_scaling;
  3958. need_scaling = drm_rotation_90_or_270(rotation) ?
  3959. (src_h != dst_w || src_w != dst_h):
  3960. (src_w != dst_w || src_h != dst_h);
  3961. /*
  3962. * if plane is being disabled or scaler is no more required or force detach
  3963. * - free scaler binded to this plane/crtc
  3964. * - in order to do this, update crtc->scaler_usage
  3965. *
  3966. * Here scaler state in crtc_state is set free so that
  3967. * scaler can be assigned to other user. Actual register
  3968. * update to free the scaler is done in plane/panel-fit programming.
  3969. * For this purpose crtc/plane_state->scaler_id isn't reset here.
  3970. */
  3971. if (force_detach || !need_scaling) {
  3972. if (*scaler_id >= 0) {
  3973. scaler_state->scaler_users &= ~(1 << scaler_user);
  3974. scaler_state->scalers[*scaler_id].in_use = 0;
  3975. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3976. "Staged freeing scaler id %d scaler_users = 0x%x\n",
  3977. intel_crtc->pipe, scaler_user, *scaler_id,
  3978. scaler_state->scaler_users);
  3979. *scaler_id = -1;
  3980. }
  3981. return 0;
  3982. }
  3983. /* range checks */
  3984. if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
  3985. dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
  3986. src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
  3987. dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
  3988. DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
  3989. "size is out of scaler range\n",
  3990. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
  3991. return -EINVAL;
  3992. }
  3993. /* mark this plane as a scaler user in crtc_state */
  3994. scaler_state->scaler_users |= (1 << scaler_user);
  3995. DRM_DEBUG_KMS("scaler_user index %u.%u: "
  3996. "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
  3997. intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
  3998. scaler_state->scaler_users);
  3999. return 0;
  4000. }
  4001. /**
  4002. * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
  4003. *
  4004. * @state: crtc's scaler state
  4005. *
  4006. * Return
  4007. * 0 - scaler_usage updated successfully
  4008. * error - requested scaling cannot be supported or other error condition
  4009. */
  4010. int skl_update_scaler_crtc(struct intel_crtc_state *state)
  4011. {
  4012. const struct drm_display_mode *adjusted_mode = &state->base.adjusted_mode;
  4013. return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
  4014. &state->scaler_state.scaler_id, DRM_ROTATE_0,
  4015. state->pipe_src_w, state->pipe_src_h,
  4016. adjusted_mode->crtc_hdisplay, adjusted_mode->crtc_vdisplay);
  4017. }
  4018. /**
  4019. * skl_update_scaler_plane - Stages update to scaler state for a given plane.
  4020. *
  4021. * @state: crtc's scaler state
  4022. * @plane_state: atomic plane state to update
  4023. *
  4024. * Return
  4025. * 0 - scaler_usage updated successfully
  4026. * error - requested scaling cannot be supported or other error condition
  4027. */
  4028. static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
  4029. struct intel_plane_state *plane_state)
  4030. {
  4031. struct intel_plane *intel_plane =
  4032. to_intel_plane(plane_state->base.plane);
  4033. struct drm_framebuffer *fb = plane_state->base.fb;
  4034. int ret;
  4035. bool force_detach = !fb || !plane_state->base.visible;
  4036. ret = skl_update_scaler(crtc_state, force_detach,
  4037. drm_plane_index(&intel_plane->base),
  4038. &plane_state->scaler_id,
  4039. plane_state->base.rotation,
  4040. drm_rect_width(&plane_state->base.src) >> 16,
  4041. drm_rect_height(&plane_state->base.src) >> 16,
  4042. drm_rect_width(&plane_state->base.dst),
  4043. drm_rect_height(&plane_state->base.dst));
  4044. if (ret || plane_state->scaler_id < 0)
  4045. return ret;
  4046. /* check colorkey */
  4047. if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
  4048. DRM_DEBUG_KMS("[PLANE:%d:%s] scaling with color key not allowed",
  4049. intel_plane->base.base.id,
  4050. intel_plane->base.name);
  4051. return -EINVAL;
  4052. }
  4053. /* Check src format */
  4054. switch (fb->format->format) {
  4055. case DRM_FORMAT_RGB565:
  4056. case DRM_FORMAT_XBGR8888:
  4057. case DRM_FORMAT_XRGB8888:
  4058. case DRM_FORMAT_ABGR8888:
  4059. case DRM_FORMAT_ARGB8888:
  4060. case DRM_FORMAT_XRGB2101010:
  4061. case DRM_FORMAT_XBGR2101010:
  4062. case DRM_FORMAT_YUYV:
  4063. case DRM_FORMAT_YVYU:
  4064. case DRM_FORMAT_UYVY:
  4065. case DRM_FORMAT_VYUY:
  4066. break;
  4067. default:
  4068. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d unsupported scaling format 0x%x\n",
  4069. intel_plane->base.base.id, intel_plane->base.name,
  4070. fb->base.id, fb->format->format);
  4071. return -EINVAL;
  4072. }
  4073. return 0;
  4074. }
  4075. static void skylake_scaler_disable(struct intel_crtc *crtc)
  4076. {
  4077. int i;
  4078. for (i = 0; i < crtc->num_scalers; i++)
  4079. skl_detach_scaler(crtc, i);
  4080. }
  4081. static void skylake_pfit_enable(struct intel_crtc *crtc)
  4082. {
  4083. struct drm_device *dev = crtc->base.dev;
  4084. struct drm_i915_private *dev_priv = to_i915(dev);
  4085. int pipe = crtc->pipe;
  4086. struct intel_crtc_scaler_state *scaler_state =
  4087. &crtc->config->scaler_state;
  4088. DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
  4089. if (crtc->config->pch_pfit.enabled) {
  4090. int id;
  4091. if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
  4092. DRM_ERROR("Requesting pfit without getting a scaler first\n");
  4093. return;
  4094. }
  4095. id = scaler_state->scaler_id;
  4096. I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
  4097. PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
  4098. I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
  4099. I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
  4100. DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
  4101. }
  4102. }
  4103. static void ironlake_pfit_enable(struct intel_crtc *crtc)
  4104. {
  4105. struct drm_device *dev = crtc->base.dev;
  4106. struct drm_i915_private *dev_priv = to_i915(dev);
  4107. int pipe = crtc->pipe;
  4108. if (crtc->config->pch_pfit.enabled) {
  4109. /* Force use of hard-coded filter coefficients
  4110. * as some pre-programmed values are broken,
  4111. * e.g. x201.
  4112. */
  4113. if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
  4114. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
  4115. PF_PIPE_SEL_IVB(pipe));
  4116. else
  4117. I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
  4118. I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
  4119. I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
  4120. }
  4121. }
  4122. void hsw_enable_ips(struct intel_crtc *crtc)
  4123. {
  4124. struct drm_device *dev = crtc->base.dev;
  4125. struct drm_i915_private *dev_priv = to_i915(dev);
  4126. if (!crtc->config->ips_enabled)
  4127. return;
  4128. /*
  4129. * We can only enable IPS after we enable a plane and wait for a vblank
  4130. * This function is called from post_plane_update, which is run after
  4131. * a vblank wait.
  4132. */
  4133. assert_plane_enabled(dev_priv, crtc->plane);
  4134. if (IS_BROADWELL(dev_priv)) {
  4135. mutex_lock(&dev_priv->rps.hw_lock);
  4136. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
  4137. mutex_unlock(&dev_priv->rps.hw_lock);
  4138. /* Quoting Art Runyan: "its not safe to expect any particular
  4139. * value in IPS_CTL bit 31 after enabling IPS through the
  4140. * mailbox." Moreover, the mailbox may return a bogus state,
  4141. * so we need to just enable it and continue on.
  4142. */
  4143. } else {
  4144. I915_WRITE(IPS_CTL, IPS_ENABLE);
  4145. /* The bit only becomes 1 in the next vblank, so this wait here
  4146. * is essentially intel_wait_for_vblank. If we don't have this
  4147. * and don't wait for vblanks until the end of crtc_enable, then
  4148. * the HW state readout code will complain that the expected
  4149. * IPS_CTL value is not the one we read. */
  4150. if (intel_wait_for_register(dev_priv,
  4151. IPS_CTL, IPS_ENABLE, IPS_ENABLE,
  4152. 50))
  4153. DRM_ERROR("Timed out waiting for IPS enable\n");
  4154. }
  4155. }
  4156. void hsw_disable_ips(struct intel_crtc *crtc)
  4157. {
  4158. struct drm_device *dev = crtc->base.dev;
  4159. struct drm_i915_private *dev_priv = to_i915(dev);
  4160. if (!crtc->config->ips_enabled)
  4161. return;
  4162. assert_plane_enabled(dev_priv, crtc->plane);
  4163. if (IS_BROADWELL(dev_priv)) {
  4164. mutex_lock(&dev_priv->rps.hw_lock);
  4165. WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
  4166. mutex_unlock(&dev_priv->rps.hw_lock);
  4167. /* wait for pcode to finish disabling IPS, which may take up to 42ms */
  4168. if (intel_wait_for_register(dev_priv,
  4169. IPS_CTL, IPS_ENABLE, 0,
  4170. 42))
  4171. DRM_ERROR("Timed out waiting for IPS disable\n");
  4172. } else {
  4173. I915_WRITE(IPS_CTL, 0);
  4174. POSTING_READ(IPS_CTL);
  4175. }
  4176. /* We need to wait for a vblank before we can disable the plane. */
  4177. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4178. }
  4179. static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
  4180. {
  4181. if (intel_crtc->overlay) {
  4182. struct drm_device *dev = intel_crtc->base.dev;
  4183. struct drm_i915_private *dev_priv = to_i915(dev);
  4184. mutex_lock(&dev->struct_mutex);
  4185. dev_priv->mm.interruptible = false;
  4186. (void) intel_overlay_switch_off(intel_crtc->overlay);
  4187. dev_priv->mm.interruptible = true;
  4188. mutex_unlock(&dev->struct_mutex);
  4189. }
  4190. /* Let userspace switch the overlay on again. In most cases userspace
  4191. * has to recompute where to put it anyway.
  4192. */
  4193. }
  4194. /**
  4195. * intel_post_enable_primary - Perform operations after enabling primary plane
  4196. * @crtc: the CRTC whose primary plane was just enabled
  4197. *
  4198. * Performs potentially sleeping operations that must be done after the primary
  4199. * plane is enabled, such as updating FBC and IPS. Note that this may be
  4200. * called due to an explicit primary plane update, or due to an implicit
  4201. * re-enable that is caused when a sprite plane is updated to no longer
  4202. * completely hide the primary plane.
  4203. */
  4204. static void
  4205. intel_post_enable_primary(struct drm_crtc *crtc)
  4206. {
  4207. struct drm_device *dev = crtc->dev;
  4208. struct drm_i915_private *dev_priv = to_i915(dev);
  4209. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4210. int pipe = intel_crtc->pipe;
  4211. /*
  4212. * FIXME IPS should be fine as long as one plane is
  4213. * enabled, but in practice it seems to have problems
  4214. * when going from primary only to sprite only and vice
  4215. * versa.
  4216. */
  4217. hsw_enable_ips(intel_crtc);
  4218. /*
  4219. * Gen2 reports pipe underruns whenever all planes are disabled.
  4220. * So don't enable underrun reporting before at least some planes
  4221. * are enabled.
  4222. * FIXME: Need to fix the logic to work when we turn off all planes
  4223. * but leave the pipe running.
  4224. */
  4225. if (IS_GEN2(dev_priv))
  4226. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4227. /* Underruns don't always raise interrupts, so check manually. */
  4228. intel_check_cpu_fifo_underruns(dev_priv);
  4229. intel_check_pch_fifo_underruns(dev_priv);
  4230. }
  4231. /* FIXME move all this to pre_plane_update() with proper state tracking */
  4232. static void
  4233. intel_pre_disable_primary(struct drm_crtc *crtc)
  4234. {
  4235. struct drm_device *dev = crtc->dev;
  4236. struct drm_i915_private *dev_priv = to_i915(dev);
  4237. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4238. int pipe = intel_crtc->pipe;
  4239. /*
  4240. * Gen2 reports pipe underruns whenever all planes are disabled.
  4241. * So diasble underrun reporting before all the planes get disabled.
  4242. * FIXME: Need to fix the logic to work when we turn off all planes
  4243. * but leave the pipe running.
  4244. */
  4245. if (IS_GEN2(dev_priv))
  4246. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4247. /*
  4248. * FIXME IPS should be fine as long as one plane is
  4249. * enabled, but in practice it seems to have problems
  4250. * when going from primary only to sprite only and vice
  4251. * versa.
  4252. */
  4253. hsw_disable_ips(intel_crtc);
  4254. }
  4255. /* FIXME get rid of this and use pre_plane_update */
  4256. static void
  4257. intel_pre_disable_primary_noatomic(struct drm_crtc *crtc)
  4258. {
  4259. struct drm_device *dev = crtc->dev;
  4260. struct drm_i915_private *dev_priv = to_i915(dev);
  4261. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4262. int pipe = intel_crtc->pipe;
  4263. intel_pre_disable_primary(crtc);
  4264. /*
  4265. * Vblank time updates from the shadow to live plane control register
  4266. * are blocked if the memory self-refresh mode is active at that
  4267. * moment. So to make sure the plane gets truly disabled, disable
  4268. * first the self-refresh mode. The self-refresh enable bit in turn
  4269. * will be checked/applied by the HW only at the next frame start
  4270. * event which is after the vblank start event, so we need to have a
  4271. * wait-for-vblank between disabling the plane and the pipe.
  4272. */
  4273. if (HAS_GMCH_DISPLAY(dev_priv) &&
  4274. intel_set_memory_cxsr(dev_priv, false))
  4275. intel_wait_for_vblank(dev_priv, pipe);
  4276. }
  4277. static void intel_post_plane_update(struct intel_crtc_state *old_crtc_state)
  4278. {
  4279. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4280. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4281. struct intel_crtc_state *pipe_config =
  4282. to_intel_crtc_state(crtc->base.state);
  4283. struct drm_plane *primary = crtc->base.primary;
  4284. struct drm_plane_state *old_pri_state =
  4285. drm_atomic_get_existing_plane_state(old_state, primary);
  4286. intel_frontbuffer_flip(to_i915(crtc->base.dev), pipe_config->fb_bits);
  4287. crtc->wm.cxsr_allowed = true;
  4288. if (pipe_config->update_wm_post && pipe_config->base.active)
  4289. intel_update_watermarks(crtc);
  4290. if (old_pri_state) {
  4291. struct intel_plane_state *primary_state =
  4292. to_intel_plane_state(primary->state);
  4293. struct intel_plane_state *old_primary_state =
  4294. to_intel_plane_state(old_pri_state);
  4295. intel_fbc_post_update(crtc);
  4296. if (primary_state->base.visible &&
  4297. (needs_modeset(&pipe_config->base) ||
  4298. !old_primary_state->base.visible))
  4299. intel_post_enable_primary(&crtc->base);
  4300. }
  4301. }
  4302. static void intel_pre_plane_update(struct intel_crtc_state *old_crtc_state)
  4303. {
  4304. struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->base.crtc);
  4305. struct drm_device *dev = crtc->base.dev;
  4306. struct drm_i915_private *dev_priv = to_i915(dev);
  4307. struct intel_crtc_state *pipe_config =
  4308. to_intel_crtc_state(crtc->base.state);
  4309. struct drm_atomic_state *old_state = old_crtc_state->base.state;
  4310. struct drm_plane *primary = crtc->base.primary;
  4311. struct drm_plane_state *old_pri_state =
  4312. drm_atomic_get_existing_plane_state(old_state, primary);
  4313. bool modeset = needs_modeset(&pipe_config->base);
  4314. struct intel_atomic_state *old_intel_state =
  4315. to_intel_atomic_state(old_state);
  4316. if (old_pri_state) {
  4317. struct intel_plane_state *primary_state =
  4318. to_intel_plane_state(primary->state);
  4319. struct intel_plane_state *old_primary_state =
  4320. to_intel_plane_state(old_pri_state);
  4321. intel_fbc_pre_update(crtc, pipe_config, primary_state);
  4322. if (old_primary_state->base.visible &&
  4323. (modeset || !primary_state->base.visible))
  4324. intel_pre_disable_primary(&crtc->base);
  4325. }
  4326. if (pipe_config->disable_cxsr && HAS_GMCH_DISPLAY(dev_priv)) {
  4327. crtc->wm.cxsr_allowed = false;
  4328. /*
  4329. * Vblank time updates from the shadow to live plane control register
  4330. * are blocked if the memory self-refresh mode is active at that
  4331. * moment. So to make sure the plane gets truly disabled, disable
  4332. * first the self-refresh mode. The self-refresh enable bit in turn
  4333. * will be checked/applied by the HW only at the next frame start
  4334. * event which is after the vblank start event, so we need to have a
  4335. * wait-for-vblank between disabling the plane and the pipe.
  4336. */
  4337. if (old_crtc_state->base.active &&
  4338. intel_set_memory_cxsr(dev_priv, false))
  4339. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4340. }
  4341. /*
  4342. * IVB workaround: must disable low power watermarks for at least
  4343. * one frame before enabling scaling. LP watermarks can be re-enabled
  4344. * when scaling is disabled.
  4345. *
  4346. * WaCxSRDisabledForSpriteScaling:ivb
  4347. */
  4348. if (pipe_config->disable_lp_wm && ilk_disable_lp_wm(dev))
  4349. intel_wait_for_vblank(dev_priv, crtc->pipe);
  4350. /*
  4351. * If we're doing a modeset, we're done. No need to do any pre-vblank
  4352. * watermark programming here.
  4353. */
  4354. if (needs_modeset(&pipe_config->base))
  4355. return;
  4356. /*
  4357. * For platforms that support atomic watermarks, program the
  4358. * 'intermediate' watermarks immediately. On pre-gen9 platforms, these
  4359. * will be the intermediate values that are safe for both pre- and
  4360. * post- vblank; when vblank happens, the 'active' values will be set
  4361. * to the final 'target' values and we'll do this again to get the
  4362. * optimal watermarks. For gen9+ platforms, the values we program here
  4363. * will be the final target values which will get automatically latched
  4364. * at vblank time; no further programming will be necessary.
  4365. *
  4366. * If a platform hasn't been transitioned to atomic watermarks yet,
  4367. * we'll continue to update watermarks the old way, if flags tell
  4368. * us to.
  4369. */
  4370. if (dev_priv->display.initial_watermarks != NULL)
  4371. dev_priv->display.initial_watermarks(old_intel_state,
  4372. pipe_config);
  4373. else if (pipe_config->update_wm_pre)
  4374. intel_update_watermarks(crtc);
  4375. }
  4376. static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
  4377. {
  4378. struct drm_device *dev = crtc->dev;
  4379. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4380. struct drm_plane *p;
  4381. int pipe = intel_crtc->pipe;
  4382. intel_crtc_dpms_overlay_disable(intel_crtc);
  4383. drm_for_each_plane_mask(p, dev, plane_mask)
  4384. to_intel_plane(p)->disable_plane(p, crtc);
  4385. /*
  4386. * FIXME: Once we grow proper nuclear flip support out of this we need
  4387. * to compute the mask of flip planes precisely. For the time being
  4388. * consider this a flip to a NULL plane.
  4389. */
  4390. intel_frontbuffer_flip(to_i915(dev), INTEL_FRONTBUFFER_ALL_MASK(pipe));
  4391. }
  4392. static void intel_encoders_pre_pll_enable(struct drm_crtc *crtc,
  4393. struct intel_crtc_state *crtc_state,
  4394. struct drm_atomic_state *old_state)
  4395. {
  4396. struct drm_connector_state *old_conn_state;
  4397. struct drm_connector *conn;
  4398. int i;
  4399. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4400. struct drm_connector_state *conn_state = conn->state;
  4401. struct intel_encoder *encoder =
  4402. to_intel_encoder(conn_state->best_encoder);
  4403. if (conn_state->crtc != crtc)
  4404. continue;
  4405. if (encoder->pre_pll_enable)
  4406. encoder->pre_pll_enable(encoder, crtc_state, conn_state);
  4407. }
  4408. }
  4409. static void intel_encoders_pre_enable(struct drm_crtc *crtc,
  4410. struct intel_crtc_state *crtc_state,
  4411. struct drm_atomic_state *old_state)
  4412. {
  4413. struct drm_connector_state *old_conn_state;
  4414. struct drm_connector *conn;
  4415. int i;
  4416. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4417. struct drm_connector_state *conn_state = conn->state;
  4418. struct intel_encoder *encoder =
  4419. to_intel_encoder(conn_state->best_encoder);
  4420. if (conn_state->crtc != crtc)
  4421. continue;
  4422. if (encoder->pre_enable)
  4423. encoder->pre_enable(encoder, crtc_state, conn_state);
  4424. }
  4425. }
  4426. static void intel_encoders_enable(struct drm_crtc *crtc,
  4427. struct intel_crtc_state *crtc_state,
  4428. struct drm_atomic_state *old_state)
  4429. {
  4430. struct drm_connector_state *old_conn_state;
  4431. struct drm_connector *conn;
  4432. int i;
  4433. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4434. struct drm_connector_state *conn_state = conn->state;
  4435. struct intel_encoder *encoder =
  4436. to_intel_encoder(conn_state->best_encoder);
  4437. if (conn_state->crtc != crtc)
  4438. continue;
  4439. encoder->enable(encoder, crtc_state, conn_state);
  4440. intel_opregion_notify_encoder(encoder, true);
  4441. }
  4442. }
  4443. static void intel_encoders_disable(struct drm_crtc *crtc,
  4444. struct intel_crtc_state *old_crtc_state,
  4445. struct drm_atomic_state *old_state)
  4446. {
  4447. struct drm_connector_state *old_conn_state;
  4448. struct drm_connector *conn;
  4449. int i;
  4450. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4451. struct intel_encoder *encoder =
  4452. to_intel_encoder(old_conn_state->best_encoder);
  4453. if (old_conn_state->crtc != crtc)
  4454. continue;
  4455. intel_opregion_notify_encoder(encoder, false);
  4456. encoder->disable(encoder, old_crtc_state, old_conn_state);
  4457. }
  4458. }
  4459. static void intel_encoders_post_disable(struct drm_crtc *crtc,
  4460. struct intel_crtc_state *old_crtc_state,
  4461. struct drm_atomic_state *old_state)
  4462. {
  4463. struct drm_connector_state *old_conn_state;
  4464. struct drm_connector *conn;
  4465. int i;
  4466. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4467. struct intel_encoder *encoder =
  4468. to_intel_encoder(old_conn_state->best_encoder);
  4469. if (old_conn_state->crtc != crtc)
  4470. continue;
  4471. if (encoder->post_disable)
  4472. encoder->post_disable(encoder, old_crtc_state, old_conn_state);
  4473. }
  4474. }
  4475. static void intel_encoders_post_pll_disable(struct drm_crtc *crtc,
  4476. struct intel_crtc_state *old_crtc_state,
  4477. struct drm_atomic_state *old_state)
  4478. {
  4479. struct drm_connector_state *old_conn_state;
  4480. struct drm_connector *conn;
  4481. int i;
  4482. for_each_connector_in_state(old_state, conn, old_conn_state, i) {
  4483. struct intel_encoder *encoder =
  4484. to_intel_encoder(old_conn_state->best_encoder);
  4485. if (old_conn_state->crtc != crtc)
  4486. continue;
  4487. if (encoder->post_pll_disable)
  4488. encoder->post_pll_disable(encoder, old_crtc_state, old_conn_state);
  4489. }
  4490. }
  4491. static void ironlake_crtc_enable(struct intel_crtc_state *pipe_config,
  4492. struct drm_atomic_state *old_state)
  4493. {
  4494. struct drm_crtc *crtc = pipe_config->base.crtc;
  4495. struct drm_device *dev = crtc->dev;
  4496. struct drm_i915_private *dev_priv = to_i915(dev);
  4497. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4498. int pipe = intel_crtc->pipe;
  4499. struct intel_atomic_state *old_intel_state =
  4500. to_intel_atomic_state(old_state);
  4501. if (WARN_ON(intel_crtc->active))
  4502. return;
  4503. /*
  4504. * Sometimes spurious CPU pipe underruns happen during FDI
  4505. * training, at least with VGA+HDMI cloning. Suppress them.
  4506. *
  4507. * On ILK we get an occasional spurious CPU pipe underruns
  4508. * between eDP port A enable and vdd enable. Also PCH port
  4509. * enable seems to result in the occasional CPU pipe underrun.
  4510. *
  4511. * Spurious PCH underruns also occur during PCH enabling.
  4512. */
  4513. if (intel_crtc->config->has_pch_encoder || IS_GEN5(dev_priv))
  4514. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4515. if (intel_crtc->config->has_pch_encoder)
  4516. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4517. if (intel_crtc->config->has_pch_encoder)
  4518. intel_prepare_shared_dpll(intel_crtc);
  4519. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4520. intel_dp_set_m_n(intel_crtc, M1_N1);
  4521. intel_set_pipe_timings(intel_crtc);
  4522. intel_set_pipe_src_size(intel_crtc);
  4523. if (intel_crtc->config->has_pch_encoder) {
  4524. intel_cpu_transcoder_set_m_n(intel_crtc,
  4525. &intel_crtc->config->fdi_m_n, NULL);
  4526. }
  4527. ironlake_set_pipeconf(crtc);
  4528. intel_crtc->active = true;
  4529. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4530. if (intel_crtc->config->has_pch_encoder) {
  4531. /* Note: FDI PLL enabling _must_ be done before we enable the
  4532. * cpu pipes, hence this is separate from all the other fdi/pch
  4533. * enabling. */
  4534. ironlake_fdi_pll_enable(intel_crtc);
  4535. } else {
  4536. assert_fdi_tx_disabled(dev_priv, pipe);
  4537. assert_fdi_rx_disabled(dev_priv, pipe);
  4538. }
  4539. ironlake_pfit_enable(intel_crtc);
  4540. /*
  4541. * On ILK+ LUT must be loaded before the pipe is running but with
  4542. * clocks enabled
  4543. */
  4544. intel_color_load_luts(&pipe_config->base);
  4545. if (dev_priv->display.initial_watermarks != NULL)
  4546. dev_priv->display.initial_watermarks(old_intel_state, intel_crtc->config);
  4547. intel_enable_pipe(intel_crtc);
  4548. if (intel_crtc->config->has_pch_encoder)
  4549. ironlake_pch_enable(crtc);
  4550. assert_vblank_disabled(crtc);
  4551. drm_crtc_vblank_on(crtc);
  4552. intel_encoders_enable(crtc, pipe_config, old_state);
  4553. if (HAS_PCH_CPT(dev_priv))
  4554. cpt_verify_modeset(dev, intel_crtc->pipe);
  4555. /* Must wait for vblank to avoid spurious PCH FIFO underruns */
  4556. if (intel_crtc->config->has_pch_encoder)
  4557. intel_wait_for_vblank(dev_priv, pipe);
  4558. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4559. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4560. }
  4561. /* IPS only exists on ULT machines and is tied to pipe A. */
  4562. static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
  4563. {
  4564. return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
  4565. }
  4566. static void haswell_crtc_enable(struct intel_crtc_state *pipe_config,
  4567. struct drm_atomic_state *old_state)
  4568. {
  4569. struct drm_crtc *crtc = pipe_config->base.crtc;
  4570. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4571. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4572. int pipe = intel_crtc->pipe, hsw_workaround_pipe;
  4573. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4574. struct intel_atomic_state *old_intel_state =
  4575. to_intel_atomic_state(old_state);
  4576. if (WARN_ON(intel_crtc->active))
  4577. return;
  4578. if (intel_crtc->config->has_pch_encoder)
  4579. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4580. false);
  4581. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  4582. if (intel_crtc->config->shared_dpll)
  4583. intel_enable_shared_dpll(intel_crtc);
  4584. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  4585. intel_dp_set_m_n(intel_crtc, M1_N1);
  4586. if (!transcoder_is_dsi(cpu_transcoder))
  4587. intel_set_pipe_timings(intel_crtc);
  4588. intel_set_pipe_src_size(intel_crtc);
  4589. if (cpu_transcoder != TRANSCODER_EDP &&
  4590. !transcoder_is_dsi(cpu_transcoder)) {
  4591. I915_WRITE(PIPE_MULT(cpu_transcoder),
  4592. intel_crtc->config->pixel_multiplier - 1);
  4593. }
  4594. if (intel_crtc->config->has_pch_encoder) {
  4595. intel_cpu_transcoder_set_m_n(intel_crtc,
  4596. &intel_crtc->config->fdi_m_n, NULL);
  4597. }
  4598. if (!transcoder_is_dsi(cpu_transcoder))
  4599. haswell_set_pipeconf(crtc);
  4600. haswell_set_pipemisc(crtc);
  4601. intel_color_set_csc(&pipe_config->base);
  4602. intel_crtc->active = true;
  4603. if (intel_crtc->config->has_pch_encoder)
  4604. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4605. else
  4606. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4607. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  4608. if (intel_crtc->config->has_pch_encoder)
  4609. dev_priv->display.fdi_link_train(crtc);
  4610. if (!transcoder_is_dsi(cpu_transcoder))
  4611. intel_ddi_enable_pipe_clock(intel_crtc);
  4612. if (INTEL_GEN(dev_priv) >= 9)
  4613. skylake_pfit_enable(intel_crtc);
  4614. else
  4615. ironlake_pfit_enable(intel_crtc);
  4616. /*
  4617. * On ILK+ LUT must be loaded before the pipe is running but with
  4618. * clocks enabled
  4619. */
  4620. intel_color_load_luts(&pipe_config->base);
  4621. intel_ddi_set_pipe_settings(crtc);
  4622. if (!transcoder_is_dsi(cpu_transcoder))
  4623. intel_ddi_enable_transcoder_func(crtc);
  4624. if (dev_priv->display.initial_watermarks != NULL)
  4625. dev_priv->display.initial_watermarks(old_intel_state, pipe_config);
  4626. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4627. if (!transcoder_is_dsi(cpu_transcoder))
  4628. intel_enable_pipe(intel_crtc);
  4629. if (intel_crtc->config->has_pch_encoder)
  4630. lpt_pch_enable(crtc);
  4631. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4632. intel_ddi_set_vc_payload_alloc(crtc, true);
  4633. assert_vblank_disabled(crtc);
  4634. drm_crtc_vblank_on(crtc);
  4635. intel_encoders_enable(crtc, pipe_config, old_state);
  4636. if (intel_crtc->config->has_pch_encoder) {
  4637. intel_wait_for_vblank(dev_priv, pipe);
  4638. intel_wait_for_vblank(dev_priv, pipe);
  4639. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4640. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4641. true);
  4642. }
  4643. /* If we change the relative order between pipe/planes enabling, we need
  4644. * to change the workaround. */
  4645. hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
  4646. if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
  4647. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4648. intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
  4649. }
  4650. }
  4651. static void ironlake_pfit_disable(struct intel_crtc *crtc, bool force)
  4652. {
  4653. struct drm_device *dev = crtc->base.dev;
  4654. struct drm_i915_private *dev_priv = to_i915(dev);
  4655. int pipe = crtc->pipe;
  4656. /* To avoid upsetting the power well on haswell only disable the pfit if
  4657. * it's in use. The hw state code will make sure we get this right. */
  4658. if (force || crtc->config->pch_pfit.enabled) {
  4659. I915_WRITE(PF_CTL(pipe), 0);
  4660. I915_WRITE(PF_WIN_POS(pipe), 0);
  4661. I915_WRITE(PF_WIN_SZ(pipe), 0);
  4662. }
  4663. }
  4664. static void ironlake_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4665. struct drm_atomic_state *old_state)
  4666. {
  4667. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4668. struct drm_device *dev = crtc->dev;
  4669. struct drm_i915_private *dev_priv = to_i915(dev);
  4670. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4671. int pipe = intel_crtc->pipe;
  4672. /*
  4673. * Sometimes spurious CPU pipe underruns happen when the
  4674. * pipe is already disabled, but FDI RX/TX is still enabled.
  4675. * Happens at least with VGA+HDMI cloning. Suppress them.
  4676. */
  4677. if (intel_crtc->config->has_pch_encoder) {
  4678. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  4679. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
  4680. }
  4681. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4682. drm_crtc_vblank_off(crtc);
  4683. assert_vblank_disabled(crtc);
  4684. intel_disable_pipe(intel_crtc);
  4685. ironlake_pfit_disable(intel_crtc, false);
  4686. if (intel_crtc->config->has_pch_encoder)
  4687. ironlake_fdi_disable(crtc);
  4688. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4689. if (intel_crtc->config->has_pch_encoder) {
  4690. ironlake_disable_pch_transcoder(dev_priv, pipe);
  4691. if (HAS_PCH_CPT(dev_priv)) {
  4692. i915_reg_t reg;
  4693. u32 temp;
  4694. /* disable TRANS_DP_CTL */
  4695. reg = TRANS_DP_CTL(pipe);
  4696. temp = I915_READ(reg);
  4697. temp &= ~(TRANS_DP_OUTPUT_ENABLE |
  4698. TRANS_DP_PORT_SEL_MASK);
  4699. temp |= TRANS_DP_PORT_SEL_NONE;
  4700. I915_WRITE(reg, temp);
  4701. /* disable DPLL_SEL */
  4702. temp = I915_READ(PCH_DPLL_SEL);
  4703. temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
  4704. I915_WRITE(PCH_DPLL_SEL, temp);
  4705. }
  4706. ironlake_fdi_pll_disable(intel_crtc);
  4707. }
  4708. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  4709. intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
  4710. }
  4711. static void haswell_crtc_disable(struct intel_crtc_state *old_crtc_state,
  4712. struct drm_atomic_state *old_state)
  4713. {
  4714. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  4715. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4716. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4717. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  4718. if (intel_crtc->config->has_pch_encoder)
  4719. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4720. false);
  4721. intel_encoders_disable(crtc, old_crtc_state, old_state);
  4722. drm_crtc_vblank_off(crtc);
  4723. assert_vblank_disabled(crtc);
  4724. /* XXX: Do the pipe assertions at the right place for BXT DSI. */
  4725. if (!transcoder_is_dsi(cpu_transcoder))
  4726. intel_disable_pipe(intel_crtc);
  4727. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DP_MST))
  4728. intel_ddi_set_vc_payload_alloc(crtc, false);
  4729. if (!transcoder_is_dsi(cpu_transcoder))
  4730. intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
  4731. if (INTEL_GEN(dev_priv) >= 9)
  4732. skylake_scaler_disable(intel_crtc);
  4733. else
  4734. ironlake_pfit_disable(intel_crtc, false);
  4735. if (!transcoder_is_dsi(cpu_transcoder))
  4736. intel_ddi_disable_pipe_clock(intel_crtc);
  4737. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  4738. if (old_crtc_state->has_pch_encoder)
  4739. intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
  4740. true);
  4741. }
  4742. static void i9xx_pfit_enable(struct intel_crtc *crtc)
  4743. {
  4744. struct drm_device *dev = crtc->base.dev;
  4745. struct drm_i915_private *dev_priv = to_i915(dev);
  4746. struct intel_crtc_state *pipe_config = crtc->config;
  4747. if (!pipe_config->gmch_pfit.control)
  4748. return;
  4749. /*
  4750. * The panel fitter should only be adjusted whilst the pipe is disabled,
  4751. * according to register description and PRM.
  4752. */
  4753. WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
  4754. assert_pipe_disabled(dev_priv, crtc->pipe);
  4755. I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
  4756. I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
  4757. /* Border color in case we don't scale up to the full screen. Black by
  4758. * default, change to something else for debugging. */
  4759. I915_WRITE(BCLRPAT(crtc->pipe), 0);
  4760. }
  4761. static enum intel_display_power_domain port_to_power_domain(enum port port)
  4762. {
  4763. switch (port) {
  4764. case PORT_A:
  4765. return POWER_DOMAIN_PORT_DDI_A_LANES;
  4766. case PORT_B:
  4767. return POWER_DOMAIN_PORT_DDI_B_LANES;
  4768. case PORT_C:
  4769. return POWER_DOMAIN_PORT_DDI_C_LANES;
  4770. case PORT_D:
  4771. return POWER_DOMAIN_PORT_DDI_D_LANES;
  4772. case PORT_E:
  4773. return POWER_DOMAIN_PORT_DDI_E_LANES;
  4774. default:
  4775. MISSING_CASE(port);
  4776. return POWER_DOMAIN_PORT_OTHER;
  4777. }
  4778. }
  4779. static enum intel_display_power_domain port_to_aux_power_domain(enum port port)
  4780. {
  4781. switch (port) {
  4782. case PORT_A:
  4783. return POWER_DOMAIN_AUX_A;
  4784. case PORT_B:
  4785. return POWER_DOMAIN_AUX_B;
  4786. case PORT_C:
  4787. return POWER_DOMAIN_AUX_C;
  4788. case PORT_D:
  4789. return POWER_DOMAIN_AUX_D;
  4790. case PORT_E:
  4791. /* FIXME: Check VBT for actual wiring of PORT E */
  4792. return POWER_DOMAIN_AUX_D;
  4793. default:
  4794. MISSING_CASE(port);
  4795. return POWER_DOMAIN_AUX_A;
  4796. }
  4797. }
  4798. enum intel_display_power_domain
  4799. intel_display_port_power_domain(struct intel_encoder *intel_encoder)
  4800. {
  4801. struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
  4802. struct intel_digital_port *intel_dig_port;
  4803. switch (intel_encoder->type) {
  4804. case INTEL_OUTPUT_UNKNOWN:
  4805. /* Only DDI platforms should ever use this output type */
  4806. WARN_ON_ONCE(!HAS_DDI(dev_priv));
  4807. case INTEL_OUTPUT_DP:
  4808. case INTEL_OUTPUT_HDMI:
  4809. case INTEL_OUTPUT_EDP:
  4810. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4811. return port_to_power_domain(intel_dig_port->port);
  4812. case INTEL_OUTPUT_DP_MST:
  4813. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4814. return port_to_power_domain(intel_dig_port->port);
  4815. case INTEL_OUTPUT_ANALOG:
  4816. return POWER_DOMAIN_PORT_CRT;
  4817. case INTEL_OUTPUT_DSI:
  4818. return POWER_DOMAIN_PORT_DSI;
  4819. default:
  4820. return POWER_DOMAIN_PORT_OTHER;
  4821. }
  4822. }
  4823. enum intel_display_power_domain
  4824. intel_display_port_aux_power_domain(struct intel_encoder *intel_encoder)
  4825. {
  4826. struct drm_i915_private *dev_priv = to_i915(intel_encoder->base.dev);
  4827. struct intel_digital_port *intel_dig_port;
  4828. switch (intel_encoder->type) {
  4829. case INTEL_OUTPUT_UNKNOWN:
  4830. case INTEL_OUTPUT_HDMI:
  4831. /*
  4832. * Only DDI platforms should ever use these output types.
  4833. * We can get here after the HDMI detect code has already set
  4834. * the type of the shared encoder. Since we can't be sure
  4835. * what's the status of the given connectors, play safe and
  4836. * run the DP detection too.
  4837. */
  4838. WARN_ON_ONCE(!HAS_DDI(dev_priv));
  4839. case INTEL_OUTPUT_DP:
  4840. case INTEL_OUTPUT_EDP:
  4841. intel_dig_port = enc_to_dig_port(&intel_encoder->base);
  4842. return port_to_aux_power_domain(intel_dig_port->port);
  4843. case INTEL_OUTPUT_DP_MST:
  4844. intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
  4845. return port_to_aux_power_domain(intel_dig_port->port);
  4846. default:
  4847. MISSING_CASE(intel_encoder->type);
  4848. return POWER_DOMAIN_AUX_A;
  4849. }
  4850. }
  4851. static unsigned long get_crtc_power_domains(struct drm_crtc *crtc,
  4852. struct intel_crtc_state *crtc_state)
  4853. {
  4854. struct drm_device *dev = crtc->dev;
  4855. struct drm_encoder *encoder;
  4856. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4857. enum pipe pipe = intel_crtc->pipe;
  4858. unsigned long mask;
  4859. enum transcoder transcoder = crtc_state->cpu_transcoder;
  4860. if (!crtc_state->base.active)
  4861. return 0;
  4862. mask = BIT(POWER_DOMAIN_PIPE(pipe));
  4863. mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
  4864. if (crtc_state->pch_pfit.enabled ||
  4865. crtc_state->pch_pfit.force_thru)
  4866. mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
  4867. drm_for_each_encoder_mask(encoder, dev, crtc_state->base.encoder_mask) {
  4868. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  4869. mask |= BIT(intel_display_port_power_domain(intel_encoder));
  4870. }
  4871. if (crtc_state->shared_dpll)
  4872. mask |= BIT(POWER_DOMAIN_PLLS);
  4873. return mask;
  4874. }
  4875. static unsigned long
  4876. modeset_get_crtc_power_domains(struct drm_crtc *crtc,
  4877. struct intel_crtc_state *crtc_state)
  4878. {
  4879. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  4880. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  4881. enum intel_display_power_domain domain;
  4882. unsigned long domains, new_domains, old_domains;
  4883. old_domains = intel_crtc->enabled_power_domains;
  4884. intel_crtc->enabled_power_domains = new_domains =
  4885. get_crtc_power_domains(crtc, crtc_state);
  4886. domains = new_domains & ~old_domains;
  4887. for_each_power_domain(domain, domains)
  4888. intel_display_power_get(dev_priv, domain);
  4889. return old_domains & ~new_domains;
  4890. }
  4891. static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
  4892. unsigned long domains)
  4893. {
  4894. enum intel_display_power_domain domain;
  4895. for_each_power_domain(domain, domains)
  4896. intel_display_power_put(dev_priv, domain);
  4897. }
  4898. static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
  4899. {
  4900. int max_cdclk_freq = dev_priv->max_cdclk_freq;
  4901. if (IS_GEMINILAKE(dev_priv))
  4902. return 2 * max_cdclk_freq;
  4903. else if (INTEL_INFO(dev_priv)->gen >= 9 ||
  4904. IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  4905. return max_cdclk_freq;
  4906. else if (IS_CHERRYVIEW(dev_priv))
  4907. return max_cdclk_freq*95/100;
  4908. else if (INTEL_INFO(dev_priv)->gen < 4)
  4909. return 2*max_cdclk_freq*90/100;
  4910. else
  4911. return max_cdclk_freq*90/100;
  4912. }
  4913. static int skl_calc_cdclk(int max_pixclk, int vco);
  4914. static void intel_update_max_cdclk(struct drm_i915_private *dev_priv)
  4915. {
  4916. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
  4917. u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
  4918. int max_cdclk, vco;
  4919. vco = dev_priv->skl_preferred_vco_freq;
  4920. WARN_ON(vco != 8100000 && vco != 8640000);
  4921. /*
  4922. * Use the lower (vco 8640) cdclk values as a
  4923. * first guess. skl_calc_cdclk() will correct it
  4924. * if the preferred vco is 8100 instead.
  4925. */
  4926. if (limit == SKL_DFSM_CDCLK_LIMIT_675)
  4927. max_cdclk = 617143;
  4928. else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
  4929. max_cdclk = 540000;
  4930. else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
  4931. max_cdclk = 432000;
  4932. else
  4933. max_cdclk = 308571;
  4934. dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
  4935. } else if (IS_GEMINILAKE(dev_priv)) {
  4936. dev_priv->max_cdclk_freq = 316800;
  4937. } else if (IS_BROXTON(dev_priv)) {
  4938. dev_priv->max_cdclk_freq = 624000;
  4939. } else if (IS_BROADWELL(dev_priv)) {
  4940. /*
  4941. * FIXME with extra cooling we can allow
  4942. * 540 MHz for ULX and 675 Mhz for ULT.
  4943. * How can we know if extra cooling is
  4944. * available? PCI ID, VTB, something else?
  4945. */
  4946. if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  4947. dev_priv->max_cdclk_freq = 450000;
  4948. else if (IS_BDW_ULX(dev_priv))
  4949. dev_priv->max_cdclk_freq = 450000;
  4950. else if (IS_BDW_ULT(dev_priv))
  4951. dev_priv->max_cdclk_freq = 540000;
  4952. else
  4953. dev_priv->max_cdclk_freq = 675000;
  4954. } else if (IS_CHERRYVIEW(dev_priv)) {
  4955. dev_priv->max_cdclk_freq = 320000;
  4956. } else if (IS_VALLEYVIEW(dev_priv)) {
  4957. dev_priv->max_cdclk_freq = 400000;
  4958. } else {
  4959. /* otherwise assume cdclk is fixed */
  4960. dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
  4961. }
  4962. dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
  4963. DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
  4964. dev_priv->max_cdclk_freq);
  4965. DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
  4966. dev_priv->max_dotclk_freq);
  4967. }
  4968. static void intel_update_cdclk(struct drm_i915_private *dev_priv)
  4969. {
  4970. dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev_priv);
  4971. if (INTEL_GEN(dev_priv) >= 9)
  4972. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz, VCO: %d kHz, ref: %d kHz\n",
  4973. dev_priv->cdclk_freq, dev_priv->cdclk_pll.vco,
  4974. dev_priv->cdclk_pll.ref);
  4975. else
  4976. DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
  4977. dev_priv->cdclk_freq);
  4978. /*
  4979. * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
  4980. * Programmng [sic] note: bit[9:2] should be programmed to the number
  4981. * of cdclk that generates 4MHz reference clock freq which is used to
  4982. * generate GMBus clock. This will vary with the cdclk freq.
  4983. */
  4984. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  4985. I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
  4986. }
  4987. /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
  4988. static int skl_cdclk_decimal(int cdclk)
  4989. {
  4990. return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
  4991. }
  4992. static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
  4993. {
  4994. int ratio;
  4995. if (cdclk == dev_priv->cdclk_pll.ref)
  4996. return 0;
  4997. switch (cdclk) {
  4998. default:
  4999. MISSING_CASE(cdclk);
  5000. case 144000:
  5001. case 288000:
  5002. case 384000:
  5003. case 576000:
  5004. ratio = 60;
  5005. break;
  5006. case 624000:
  5007. ratio = 65;
  5008. break;
  5009. }
  5010. return dev_priv->cdclk_pll.ref * ratio;
  5011. }
  5012. static int glk_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
  5013. {
  5014. int ratio;
  5015. if (cdclk == dev_priv->cdclk_pll.ref)
  5016. return 0;
  5017. switch (cdclk) {
  5018. default:
  5019. MISSING_CASE(cdclk);
  5020. case 79200:
  5021. case 158400:
  5022. case 316800:
  5023. ratio = 33;
  5024. break;
  5025. }
  5026. return dev_priv->cdclk_pll.ref * ratio;
  5027. }
  5028. static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
  5029. {
  5030. I915_WRITE(BXT_DE_PLL_ENABLE, 0);
  5031. /* Timeout 200us */
  5032. if (intel_wait_for_register(dev_priv,
  5033. BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
  5034. 1))
  5035. DRM_ERROR("timeout waiting for DE PLL unlock\n");
  5036. dev_priv->cdclk_pll.vco = 0;
  5037. }
  5038. static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
  5039. {
  5040. int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk_pll.ref);
  5041. u32 val;
  5042. val = I915_READ(BXT_DE_PLL_CTL);
  5043. val &= ~BXT_DE_PLL_RATIO_MASK;
  5044. val |= BXT_DE_PLL_RATIO(ratio);
  5045. I915_WRITE(BXT_DE_PLL_CTL, val);
  5046. I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
  5047. /* Timeout 200us */
  5048. if (intel_wait_for_register(dev_priv,
  5049. BXT_DE_PLL_ENABLE,
  5050. BXT_DE_PLL_LOCK,
  5051. BXT_DE_PLL_LOCK,
  5052. 1))
  5053. DRM_ERROR("timeout waiting for DE PLL lock\n");
  5054. dev_priv->cdclk_pll.vco = vco;
  5055. }
  5056. static void bxt_set_cdclk(struct drm_i915_private *dev_priv, int cdclk)
  5057. {
  5058. u32 val, divider;
  5059. int vco, ret;
  5060. if (IS_GEMINILAKE(dev_priv))
  5061. vco = glk_de_pll_vco(dev_priv, cdclk);
  5062. else
  5063. vco = bxt_de_pll_vco(dev_priv, cdclk);
  5064. DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
  5065. /* cdclk = vco / 2 / div{1,1.5,2,4} */
  5066. switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
  5067. case 8:
  5068. divider = BXT_CDCLK_CD2X_DIV_SEL_4;
  5069. break;
  5070. case 4:
  5071. divider = BXT_CDCLK_CD2X_DIV_SEL_2;
  5072. break;
  5073. case 3:
  5074. WARN(IS_GEMINILAKE(dev_priv), "Unsupported divider\n");
  5075. divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
  5076. break;
  5077. case 2:
  5078. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  5079. break;
  5080. default:
  5081. WARN_ON(cdclk != dev_priv->cdclk_pll.ref);
  5082. WARN_ON(vco != 0);
  5083. divider = BXT_CDCLK_CD2X_DIV_SEL_1;
  5084. break;
  5085. }
  5086. /* Inform power controller of upcoming frequency change */
  5087. mutex_lock(&dev_priv->rps.hw_lock);
  5088. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  5089. 0x80000000);
  5090. mutex_unlock(&dev_priv->rps.hw_lock);
  5091. if (ret) {
  5092. DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
  5093. ret, cdclk);
  5094. return;
  5095. }
  5096. if (dev_priv->cdclk_pll.vco != 0 &&
  5097. dev_priv->cdclk_pll.vco != vco)
  5098. bxt_de_pll_disable(dev_priv);
  5099. if (dev_priv->cdclk_pll.vco != vco)
  5100. bxt_de_pll_enable(dev_priv, vco);
  5101. val = divider | skl_cdclk_decimal(cdclk);
  5102. /*
  5103. * FIXME if only the cd2x divider needs changing, it could be done
  5104. * without shutting off the pipe (if only one pipe is active).
  5105. */
  5106. val |= BXT_CDCLK_CD2X_PIPE_NONE;
  5107. /*
  5108. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  5109. * enable otherwise.
  5110. */
  5111. if (cdclk >= 500000)
  5112. val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  5113. I915_WRITE(CDCLK_CTL, val);
  5114. mutex_lock(&dev_priv->rps.hw_lock);
  5115. ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
  5116. DIV_ROUND_UP(cdclk, 25000));
  5117. mutex_unlock(&dev_priv->rps.hw_lock);
  5118. if (ret) {
  5119. DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
  5120. ret, cdclk);
  5121. return;
  5122. }
  5123. intel_update_cdclk(dev_priv);
  5124. }
  5125. static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
  5126. {
  5127. u32 cdctl, expected;
  5128. intel_update_cdclk(dev_priv);
  5129. if (dev_priv->cdclk_pll.vco == 0 ||
  5130. dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
  5131. goto sanitize;
  5132. /* DPLL okay; verify the cdclock
  5133. *
  5134. * Some BIOS versions leave an incorrect decimal frequency value and
  5135. * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
  5136. * so sanitize this register.
  5137. */
  5138. cdctl = I915_READ(CDCLK_CTL);
  5139. /*
  5140. * Let's ignore the pipe field, since BIOS could have configured the
  5141. * dividers both synching to an active pipe, or asynchronously
  5142. * (PIPE_NONE).
  5143. */
  5144. cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;
  5145. expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
  5146. skl_cdclk_decimal(dev_priv->cdclk_freq);
  5147. /*
  5148. * Disable SSA Precharge when CD clock frequency < 500 MHz,
  5149. * enable otherwise.
  5150. */
  5151. if (dev_priv->cdclk_freq >= 500000)
  5152. expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
  5153. if (cdctl == expected)
  5154. /* All well; nothing to sanitize */
  5155. return;
  5156. sanitize:
  5157. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  5158. /* force cdclk programming */
  5159. dev_priv->cdclk_freq = 0;
  5160. /* force full PLL disable + enable */
  5161. dev_priv->cdclk_pll.vco = -1;
  5162. }
  5163. void bxt_init_cdclk(struct drm_i915_private *dev_priv)
  5164. {
  5165. int cdclk;
  5166. bxt_sanitize_cdclk(dev_priv);
  5167. if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0)
  5168. return;
  5169. /*
  5170. * FIXME:
  5171. * - The initial CDCLK needs to be read from VBT.
  5172. * Need to make this change after VBT has changes for BXT.
  5173. */
  5174. if (IS_GEMINILAKE(dev_priv))
  5175. cdclk = glk_calc_cdclk(0);
  5176. else
  5177. cdclk = bxt_calc_cdclk(0);
  5178. bxt_set_cdclk(dev_priv, cdclk);
  5179. }
  5180. void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
  5181. {
  5182. bxt_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref);
  5183. }
  5184. static int skl_calc_cdclk(int max_pixclk, int vco)
  5185. {
  5186. if (vco == 8640000) {
  5187. if (max_pixclk > 540000)
  5188. return 617143;
  5189. else if (max_pixclk > 432000)
  5190. return 540000;
  5191. else if (max_pixclk > 308571)
  5192. return 432000;
  5193. else
  5194. return 308571;
  5195. } else {
  5196. if (max_pixclk > 540000)
  5197. return 675000;
  5198. else if (max_pixclk > 450000)
  5199. return 540000;
  5200. else if (max_pixclk > 337500)
  5201. return 450000;
  5202. else
  5203. return 337500;
  5204. }
  5205. }
  5206. static void
  5207. skl_dpll0_update(struct drm_i915_private *dev_priv)
  5208. {
  5209. u32 val;
  5210. dev_priv->cdclk_pll.ref = 24000;
  5211. dev_priv->cdclk_pll.vco = 0;
  5212. val = I915_READ(LCPLL1_CTL);
  5213. if ((val & LCPLL_PLL_ENABLE) == 0)
  5214. return;
  5215. if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
  5216. return;
  5217. val = I915_READ(DPLL_CTRL1);
  5218. if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
  5219. DPLL_CTRL1_SSC(SKL_DPLL0) |
  5220. DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
  5221. DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
  5222. return;
  5223. switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
  5224. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
  5225. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
  5226. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
  5227. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
  5228. dev_priv->cdclk_pll.vco = 8100000;
  5229. break;
  5230. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
  5231. case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
  5232. dev_priv->cdclk_pll.vco = 8640000;
  5233. break;
  5234. default:
  5235. MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  5236. break;
  5237. }
  5238. }
  5239. void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv, int vco)
  5240. {
  5241. bool changed = dev_priv->skl_preferred_vco_freq != vco;
  5242. dev_priv->skl_preferred_vco_freq = vco;
  5243. if (changed)
  5244. intel_update_max_cdclk(dev_priv);
  5245. }
  5246. static void
  5247. skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
  5248. {
  5249. int min_cdclk = skl_calc_cdclk(0, vco);
  5250. u32 val;
  5251. WARN_ON(vco != 8100000 && vco != 8640000);
  5252. /* select the minimum CDCLK before enabling DPLL 0 */
  5253. val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_cdclk);
  5254. I915_WRITE(CDCLK_CTL, val);
  5255. POSTING_READ(CDCLK_CTL);
  5256. /*
  5257. * We always enable DPLL0 with the lowest link rate possible, but still
  5258. * taking into account the VCO required to operate the eDP panel at the
  5259. * desired frequency. The usual DP link rates operate with a VCO of
  5260. * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
  5261. * The modeset code is responsible for the selection of the exact link
  5262. * rate later on, with the constraint of choosing a frequency that
  5263. * works with vco.
  5264. */
  5265. val = I915_READ(DPLL_CTRL1);
  5266. val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
  5267. DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
  5268. val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
  5269. if (vco == 8640000)
  5270. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
  5271. SKL_DPLL0);
  5272. else
  5273. val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
  5274. SKL_DPLL0);
  5275. I915_WRITE(DPLL_CTRL1, val);
  5276. POSTING_READ(DPLL_CTRL1);
  5277. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
  5278. if (intel_wait_for_register(dev_priv,
  5279. LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  5280. 5))
  5281. DRM_ERROR("DPLL0 not locked\n");
  5282. dev_priv->cdclk_pll.vco = vco;
  5283. /* We'll want to keep using the current vco from now on. */
  5284. skl_set_preferred_cdclk_vco(dev_priv, vco);
  5285. }
  5286. static void
  5287. skl_dpll0_disable(struct drm_i915_private *dev_priv)
  5288. {
  5289. I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
  5290. if (intel_wait_for_register(dev_priv,
  5291. LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
  5292. 1))
  5293. DRM_ERROR("Couldn't disable DPLL0\n");
  5294. dev_priv->cdclk_pll.vco = 0;
  5295. }
  5296. static void skl_set_cdclk(struct drm_i915_private *dev_priv, int cdclk, int vco)
  5297. {
  5298. u32 freq_select, pcu_ack;
  5299. int ret;
  5300. WARN_ON((cdclk == 24000) != (vco == 0));
  5301. DRM_DEBUG_DRIVER("Changing CDCLK to %d kHz (VCO %d kHz)\n", cdclk, vco);
  5302. mutex_lock(&dev_priv->rps.hw_lock);
  5303. ret = skl_pcode_request(dev_priv, SKL_PCODE_CDCLK_CONTROL,
  5304. SKL_CDCLK_PREPARE_FOR_CHANGE,
  5305. SKL_CDCLK_READY_FOR_CHANGE,
  5306. SKL_CDCLK_READY_FOR_CHANGE, 3);
  5307. mutex_unlock(&dev_priv->rps.hw_lock);
  5308. if (ret) {
  5309. DRM_ERROR("Failed to inform PCU about cdclk change (%d)\n",
  5310. ret);
  5311. return;
  5312. }
  5313. /* set CDCLK_CTL */
  5314. switch (cdclk) {
  5315. case 450000:
  5316. case 432000:
  5317. freq_select = CDCLK_FREQ_450_432;
  5318. pcu_ack = 1;
  5319. break;
  5320. case 540000:
  5321. freq_select = CDCLK_FREQ_540;
  5322. pcu_ack = 2;
  5323. break;
  5324. case 308571:
  5325. case 337500:
  5326. default:
  5327. freq_select = CDCLK_FREQ_337_308;
  5328. pcu_ack = 0;
  5329. break;
  5330. case 617143:
  5331. case 675000:
  5332. freq_select = CDCLK_FREQ_675_617;
  5333. pcu_ack = 3;
  5334. break;
  5335. }
  5336. if (dev_priv->cdclk_pll.vco != 0 &&
  5337. dev_priv->cdclk_pll.vco != vco)
  5338. skl_dpll0_disable(dev_priv);
  5339. if (dev_priv->cdclk_pll.vco != vco)
  5340. skl_dpll0_enable(dev_priv, vco);
  5341. I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(cdclk));
  5342. POSTING_READ(CDCLK_CTL);
  5343. /* inform PCU of the change */
  5344. mutex_lock(&dev_priv->rps.hw_lock);
  5345. sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
  5346. mutex_unlock(&dev_priv->rps.hw_lock);
  5347. intel_update_cdclk(dev_priv);
  5348. }
  5349. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv);
  5350. void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
  5351. {
  5352. skl_set_cdclk(dev_priv, dev_priv->cdclk_pll.ref, 0);
  5353. }
  5354. void skl_init_cdclk(struct drm_i915_private *dev_priv)
  5355. {
  5356. int cdclk, vco;
  5357. skl_sanitize_cdclk(dev_priv);
  5358. if (dev_priv->cdclk_freq != 0 && dev_priv->cdclk_pll.vco != 0) {
  5359. /*
  5360. * Use the current vco as our initial
  5361. * guess as to what the preferred vco is.
  5362. */
  5363. if (dev_priv->skl_preferred_vco_freq == 0)
  5364. skl_set_preferred_cdclk_vco(dev_priv,
  5365. dev_priv->cdclk_pll.vco);
  5366. return;
  5367. }
  5368. vco = dev_priv->skl_preferred_vco_freq;
  5369. if (vco == 0)
  5370. vco = 8100000;
  5371. cdclk = skl_calc_cdclk(0, vco);
  5372. skl_set_cdclk(dev_priv, cdclk, vco);
  5373. }
  5374. static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
  5375. {
  5376. uint32_t cdctl, expected;
  5377. /*
  5378. * check if the pre-os intialized the display
  5379. * There is SWF18 scratchpad register defined which is set by the
  5380. * pre-os which can be used by the OS drivers to check the status
  5381. */
  5382. if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
  5383. goto sanitize;
  5384. intel_update_cdclk(dev_priv);
  5385. /* Is PLL enabled and locked ? */
  5386. if (dev_priv->cdclk_pll.vco == 0 ||
  5387. dev_priv->cdclk_freq == dev_priv->cdclk_pll.ref)
  5388. goto sanitize;
  5389. /* DPLL okay; verify the cdclock
  5390. *
  5391. * Noticed in some instances that the freq selection is correct but
  5392. * decimal part is programmed wrong from BIOS where pre-os does not
  5393. * enable display. Verify the same as well.
  5394. */
  5395. cdctl = I915_READ(CDCLK_CTL);
  5396. expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
  5397. skl_cdclk_decimal(dev_priv->cdclk_freq);
  5398. if (cdctl == expected)
  5399. /* All well; nothing to sanitize */
  5400. return;
  5401. sanitize:
  5402. DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
  5403. /* force cdclk programming */
  5404. dev_priv->cdclk_freq = 0;
  5405. /* force full PLL disable + enable */
  5406. dev_priv->cdclk_pll.vco = -1;
  5407. }
  5408. /* Adjust CDclk dividers to allow high res or save power if possible */
  5409. static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
  5410. {
  5411. struct drm_i915_private *dev_priv = to_i915(dev);
  5412. u32 val, cmd;
  5413. WARN_ON(dev_priv->display.get_display_clock_speed(dev_priv)
  5414. != dev_priv->cdclk_freq);
  5415. if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
  5416. cmd = 2;
  5417. else if (cdclk == 266667)
  5418. cmd = 1;
  5419. else
  5420. cmd = 0;
  5421. mutex_lock(&dev_priv->rps.hw_lock);
  5422. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  5423. val &= ~DSPFREQGUAR_MASK;
  5424. val |= (cmd << DSPFREQGUAR_SHIFT);
  5425. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  5426. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  5427. DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
  5428. 50)) {
  5429. DRM_ERROR("timed out waiting for CDclk change\n");
  5430. }
  5431. mutex_unlock(&dev_priv->rps.hw_lock);
  5432. mutex_lock(&dev_priv->sb_lock);
  5433. if (cdclk == 400000) {
  5434. u32 divider;
  5435. divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  5436. /* adjust cdclk divider */
  5437. val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
  5438. val &= ~CCK_FREQUENCY_VALUES;
  5439. val |= divider;
  5440. vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
  5441. if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
  5442. CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
  5443. 50))
  5444. DRM_ERROR("timed out waiting for CDclk change\n");
  5445. }
  5446. /* adjust self-refresh exit latency value */
  5447. val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
  5448. val &= ~0x7f;
  5449. /*
  5450. * For high bandwidth configs, we set a higher latency in the bunit
  5451. * so that the core display fetch happens in time to avoid underruns.
  5452. */
  5453. if (cdclk == 400000)
  5454. val |= 4500 / 250; /* 4.5 usec */
  5455. else
  5456. val |= 3000 / 250; /* 3.0 usec */
  5457. vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
  5458. mutex_unlock(&dev_priv->sb_lock);
  5459. intel_update_cdclk(dev_priv);
  5460. }
  5461. static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
  5462. {
  5463. struct drm_i915_private *dev_priv = to_i915(dev);
  5464. u32 val, cmd;
  5465. WARN_ON(dev_priv->display.get_display_clock_speed(dev_priv)
  5466. != dev_priv->cdclk_freq);
  5467. switch (cdclk) {
  5468. case 333333:
  5469. case 320000:
  5470. case 266667:
  5471. case 200000:
  5472. break;
  5473. default:
  5474. MISSING_CASE(cdclk);
  5475. return;
  5476. }
  5477. /*
  5478. * Specs are full of misinformation, but testing on actual
  5479. * hardware has shown that we just need to write the desired
  5480. * CCK divider into the Punit register.
  5481. */
  5482. cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
  5483. mutex_lock(&dev_priv->rps.hw_lock);
  5484. val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
  5485. val &= ~DSPFREQGUAR_MASK_CHV;
  5486. val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
  5487. vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
  5488. if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
  5489. DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
  5490. 50)) {
  5491. DRM_ERROR("timed out waiting for CDclk change\n");
  5492. }
  5493. mutex_unlock(&dev_priv->rps.hw_lock);
  5494. intel_update_cdclk(dev_priv);
  5495. }
  5496. static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
  5497. int max_pixclk)
  5498. {
  5499. int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
  5500. int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
  5501. /*
  5502. * Really only a few cases to deal with, as only 4 CDclks are supported:
  5503. * 200MHz
  5504. * 267MHz
  5505. * 320/333MHz (depends on HPLL freq)
  5506. * 400MHz (VLV only)
  5507. * So we check to see whether we're above 90% (VLV) or 95% (CHV)
  5508. * of the lower bin and adjust if needed.
  5509. *
  5510. * We seem to get an unstable or solid color picture at 200MHz.
  5511. * Not sure what's wrong. For now use 200MHz only when all pipes
  5512. * are off.
  5513. */
  5514. if (!IS_CHERRYVIEW(dev_priv) &&
  5515. max_pixclk > freq_320*limit/100)
  5516. return 400000;
  5517. else if (max_pixclk > 266667*limit/100)
  5518. return freq_320;
  5519. else if (max_pixclk > 0)
  5520. return 266667;
  5521. else
  5522. return 200000;
  5523. }
  5524. static int glk_calc_cdclk(int max_pixclk)
  5525. {
  5526. if (max_pixclk > 2 * 158400)
  5527. return 316800;
  5528. else if (max_pixclk > 2 * 79200)
  5529. return 158400;
  5530. else
  5531. return 79200;
  5532. }
  5533. static int bxt_calc_cdclk(int max_pixclk)
  5534. {
  5535. if (max_pixclk > 576000)
  5536. return 624000;
  5537. else if (max_pixclk > 384000)
  5538. return 576000;
  5539. else if (max_pixclk > 288000)
  5540. return 384000;
  5541. else if (max_pixclk > 144000)
  5542. return 288000;
  5543. else
  5544. return 144000;
  5545. }
  5546. /* Compute the max pixel clock for new configuration. */
  5547. static int intel_mode_max_pixclk(struct drm_device *dev,
  5548. struct drm_atomic_state *state)
  5549. {
  5550. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  5551. struct drm_i915_private *dev_priv = to_i915(dev);
  5552. struct drm_crtc *crtc;
  5553. struct drm_crtc_state *crtc_state;
  5554. unsigned max_pixclk = 0, i;
  5555. enum pipe pipe;
  5556. memcpy(intel_state->min_pixclk, dev_priv->min_pixclk,
  5557. sizeof(intel_state->min_pixclk));
  5558. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  5559. int pixclk = 0;
  5560. if (crtc_state->enable)
  5561. pixclk = crtc_state->adjusted_mode.crtc_clock;
  5562. intel_state->min_pixclk[i] = pixclk;
  5563. }
  5564. for_each_pipe(dev_priv, pipe)
  5565. max_pixclk = max(intel_state->min_pixclk[pipe], max_pixclk);
  5566. return max_pixclk;
  5567. }
  5568. static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
  5569. {
  5570. struct drm_device *dev = state->dev;
  5571. struct drm_i915_private *dev_priv = to_i915(dev);
  5572. int max_pixclk = intel_mode_max_pixclk(dev, state);
  5573. struct intel_atomic_state *intel_state =
  5574. to_intel_atomic_state(state);
  5575. intel_state->cdclk = intel_state->dev_cdclk =
  5576. valleyview_calc_cdclk(dev_priv, max_pixclk);
  5577. if (!intel_state->active_crtcs)
  5578. intel_state->dev_cdclk = valleyview_calc_cdclk(dev_priv, 0);
  5579. return 0;
  5580. }
  5581. static int bxt_modeset_calc_cdclk(struct drm_atomic_state *state)
  5582. {
  5583. struct drm_i915_private *dev_priv = to_i915(state->dev);
  5584. int max_pixclk = ilk_max_pixel_rate(state);
  5585. struct intel_atomic_state *intel_state =
  5586. to_intel_atomic_state(state);
  5587. int cdclk;
  5588. if (IS_GEMINILAKE(dev_priv))
  5589. cdclk = glk_calc_cdclk(max_pixclk);
  5590. else
  5591. cdclk = bxt_calc_cdclk(max_pixclk);
  5592. intel_state->cdclk = intel_state->dev_cdclk = cdclk;
  5593. if (!intel_state->active_crtcs) {
  5594. if (IS_GEMINILAKE(dev_priv))
  5595. cdclk = glk_calc_cdclk(0);
  5596. else
  5597. cdclk = bxt_calc_cdclk(0);
  5598. intel_state->dev_cdclk = cdclk;
  5599. }
  5600. return 0;
  5601. }
  5602. static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
  5603. {
  5604. unsigned int credits, default_credits;
  5605. if (IS_CHERRYVIEW(dev_priv))
  5606. default_credits = PFI_CREDIT(12);
  5607. else
  5608. default_credits = PFI_CREDIT(8);
  5609. if (dev_priv->cdclk_freq >= dev_priv->czclk_freq) {
  5610. /* CHV suggested value is 31 or 63 */
  5611. if (IS_CHERRYVIEW(dev_priv))
  5612. credits = PFI_CREDIT_63;
  5613. else
  5614. credits = PFI_CREDIT(15);
  5615. } else {
  5616. credits = default_credits;
  5617. }
  5618. /*
  5619. * WA - write default credits before re-programming
  5620. * FIXME: should we also set the resend bit here?
  5621. */
  5622. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5623. default_credits);
  5624. I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
  5625. credits | PFI_CREDIT_RESEND);
  5626. /*
  5627. * FIXME is this guaranteed to clear
  5628. * immediately or should we poll for it?
  5629. */
  5630. WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
  5631. }
  5632. static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  5633. {
  5634. struct drm_device *dev = old_state->dev;
  5635. struct drm_i915_private *dev_priv = to_i915(dev);
  5636. struct intel_atomic_state *old_intel_state =
  5637. to_intel_atomic_state(old_state);
  5638. unsigned req_cdclk = old_intel_state->dev_cdclk;
  5639. /*
  5640. * FIXME: We can end up here with all power domains off, yet
  5641. * with a CDCLK frequency other than the minimum. To account
  5642. * for this take the PIPE-A power domain, which covers the HW
  5643. * blocks needed for the following programming. This can be
  5644. * removed once it's guaranteed that we get here either with
  5645. * the minimum CDCLK set, or the required power domains
  5646. * enabled.
  5647. */
  5648. intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
  5649. if (IS_CHERRYVIEW(dev_priv))
  5650. cherryview_set_cdclk(dev, req_cdclk);
  5651. else
  5652. valleyview_set_cdclk(dev, req_cdclk);
  5653. vlv_program_pfi_credits(dev_priv);
  5654. intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
  5655. }
  5656. static void valleyview_crtc_enable(struct intel_crtc_state *pipe_config,
  5657. struct drm_atomic_state *old_state)
  5658. {
  5659. struct drm_crtc *crtc = pipe_config->base.crtc;
  5660. struct drm_device *dev = crtc->dev;
  5661. struct drm_i915_private *dev_priv = to_i915(dev);
  5662. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5663. int pipe = intel_crtc->pipe;
  5664. if (WARN_ON(intel_crtc->active))
  5665. return;
  5666. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5667. intel_dp_set_m_n(intel_crtc, M1_N1);
  5668. intel_set_pipe_timings(intel_crtc);
  5669. intel_set_pipe_src_size(intel_crtc);
  5670. if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  5671. struct drm_i915_private *dev_priv = to_i915(dev);
  5672. I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
  5673. I915_WRITE(CHV_CANVAS(pipe), 0);
  5674. }
  5675. i9xx_set_pipeconf(intel_crtc);
  5676. intel_crtc->active = true;
  5677. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5678. intel_encoders_pre_pll_enable(crtc, pipe_config, old_state);
  5679. if (IS_CHERRYVIEW(dev_priv)) {
  5680. chv_prepare_pll(intel_crtc, intel_crtc->config);
  5681. chv_enable_pll(intel_crtc, intel_crtc->config);
  5682. } else {
  5683. vlv_prepare_pll(intel_crtc, intel_crtc->config);
  5684. vlv_enable_pll(intel_crtc, intel_crtc->config);
  5685. }
  5686. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  5687. i9xx_pfit_enable(intel_crtc);
  5688. intel_color_load_luts(&pipe_config->base);
  5689. intel_update_watermarks(intel_crtc);
  5690. intel_enable_pipe(intel_crtc);
  5691. assert_vblank_disabled(crtc);
  5692. drm_crtc_vblank_on(crtc);
  5693. intel_encoders_enable(crtc, pipe_config, old_state);
  5694. }
  5695. static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
  5696. {
  5697. struct drm_device *dev = crtc->base.dev;
  5698. struct drm_i915_private *dev_priv = to_i915(dev);
  5699. I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
  5700. I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
  5701. }
  5702. static void i9xx_crtc_enable(struct intel_crtc_state *pipe_config,
  5703. struct drm_atomic_state *old_state)
  5704. {
  5705. struct drm_crtc *crtc = pipe_config->base.crtc;
  5706. struct drm_device *dev = crtc->dev;
  5707. struct drm_i915_private *dev_priv = to_i915(dev);
  5708. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5709. enum pipe pipe = intel_crtc->pipe;
  5710. if (WARN_ON(intel_crtc->active))
  5711. return;
  5712. i9xx_set_pll_dividers(intel_crtc);
  5713. if (intel_crtc_has_dp_encoder(intel_crtc->config))
  5714. intel_dp_set_m_n(intel_crtc, M1_N1);
  5715. intel_set_pipe_timings(intel_crtc);
  5716. intel_set_pipe_src_size(intel_crtc);
  5717. i9xx_set_pipeconf(intel_crtc);
  5718. intel_crtc->active = true;
  5719. if (!IS_GEN2(dev_priv))
  5720. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
  5721. intel_encoders_pre_enable(crtc, pipe_config, old_state);
  5722. i9xx_enable_pll(intel_crtc);
  5723. i9xx_pfit_enable(intel_crtc);
  5724. intel_color_load_luts(&pipe_config->base);
  5725. intel_update_watermarks(intel_crtc);
  5726. intel_enable_pipe(intel_crtc);
  5727. assert_vblank_disabled(crtc);
  5728. drm_crtc_vblank_on(crtc);
  5729. intel_encoders_enable(crtc, pipe_config, old_state);
  5730. }
  5731. static void i9xx_pfit_disable(struct intel_crtc *crtc)
  5732. {
  5733. struct drm_device *dev = crtc->base.dev;
  5734. struct drm_i915_private *dev_priv = to_i915(dev);
  5735. if (!crtc->config->gmch_pfit.control)
  5736. return;
  5737. assert_pipe_disabled(dev_priv, crtc->pipe);
  5738. DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
  5739. I915_READ(PFIT_CONTROL));
  5740. I915_WRITE(PFIT_CONTROL, 0);
  5741. }
  5742. static void i9xx_crtc_disable(struct intel_crtc_state *old_crtc_state,
  5743. struct drm_atomic_state *old_state)
  5744. {
  5745. struct drm_crtc *crtc = old_crtc_state->base.crtc;
  5746. struct drm_device *dev = crtc->dev;
  5747. struct drm_i915_private *dev_priv = to_i915(dev);
  5748. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5749. int pipe = intel_crtc->pipe;
  5750. /*
  5751. * On gen2 planes are double buffered but the pipe isn't, so we must
  5752. * wait for planes to fully turn off before disabling the pipe.
  5753. */
  5754. if (IS_GEN2(dev_priv))
  5755. intel_wait_for_vblank(dev_priv, pipe);
  5756. intel_encoders_disable(crtc, old_crtc_state, old_state);
  5757. drm_crtc_vblank_off(crtc);
  5758. assert_vblank_disabled(crtc);
  5759. intel_disable_pipe(intel_crtc);
  5760. i9xx_pfit_disable(intel_crtc);
  5761. intel_encoders_post_disable(crtc, old_crtc_state, old_state);
  5762. if (!intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_DSI)) {
  5763. if (IS_CHERRYVIEW(dev_priv))
  5764. chv_disable_pll(dev_priv, pipe);
  5765. else if (IS_VALLEYVIEW(dev_priv))
  5766. vlv_disable_pll(dev_priv, pipe);
  5767. else
  5768. i9xx_disable_pll(intel_crtc);
  5769. }
  5770. intel_encoders_post_pll_disable(crtc, old_crtc_state, old_state);
  5771. if (!IS_GEN2(dev_priv))
  5772. intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
  5773. }
  5774. static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
  5775. {
  5776. struct intel_encoder *encoder;
  5777. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  5778. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  5779. enum intel_display_power_domain domain;
  5780. unsigned long domains;
  5781. struct drm_atomic_state *state;
  5782. struct intel_crtc_state *crtc_state;
  5783. int ret;
  5784. if (!intel_crtc->active)
  5785. return;
  5786. if (crtc->primary->state->visible) {
  5787. WARN_ON(intel_crtc->flip_work);
  5788. intel_pre_disable_primary_noatomic(crtc);
  5789. intel_crtc_disable_planes(crtc, 1 << drm_plane_index(crtc->primary));
  5790. crtc->primary->state->visible = false;
  5791. }
  5792. state = drm_atomic_state_alloc(crtc->dev);
  5793. state->acquire_ctx = crtc->dev->mode_config.acquire_ctx;
  5794. /* Everything's already locked, -EDEADLK can't happen. */
  5795. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  5796. ret = drm_atomic_add_affected_connectors(state, crtc);
  5797. WARN_ON(IS_ERR(crtc_state) || ret);
  5798. dev_priv->display.crtc_disable(crtc_state, state);
  5799. drm_atomic_state_put(state);
  5800. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
  5801. crtc->base.id, crtc->name);
  5802. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->state, NULL) < 0);
  5803. crtc->state->active = false;
  5804. intel_crtc->active = false;
  5805. crtc->enabled = false;
  5806. crtc->state->connector_mask = 0;
  5807. crtc->state->encoder_mask = 0;
  5808. for_each_encoder_on_crtc(crtc->dev, crtc, encoder)
  5809. encoder->base.crtc = NULL;
  5810. intel_fbc_disable(intel_crtc);
  5811. intel_update_watermarks(intel_crtc);
  5812. intel_disable_shared_dpll(intel_crtc);
  5813. domains = intel_crtc->enabled_power_domains;
  5814. for_each_power_domain(domain, domains)
  5815. intel_display_power_put(dev_priv, domain);
  5816. intel_crtc->enabled_power_domains = 0;
  5817. dev_priv->active_crtcs &= ~(1 << intel_crtc->pipe);
  5818. dev_priv->min_pixclk[intel_crtc->pipe] = 0;
  5819. }
  5820. /*
  5821. * turn all crtc's off, but do not adjust state
  5822. * This has to be paired with a call to intel_modeset_setup_hw_state.
  5823. */
  5824. int intel_display_suspend(struct drm_device *dev)
  5825. {
  5826. struct drm_i915_private *dev_priv = to_i915(dev);
  5827. struct drm_atomic_state *state;
  5828. int ret;
  5829. state = drm_atomic_helper_suspend(dev);
  5830. ret = PTR_ERR_OR_ZERO(state);
  5831. if (ret)
  5832. DRM_ERROR("Suspending crtc's failed with %i\n", ret);
  5833. else
  5834. dev_priv->modeset_restore_state = state;
  5835. return ret;
  5836. }
  5837. void intel_encoder_destroy(struct drm_encoder *encoder)
  5838. {
  5839. struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
  5840. drm_encoder_cleanup(encoder);
  5841. kfree(intel_encoder);
  5842. }
  5843. /* Cross check the actual hw state with our own modeset state tracking (and it's
  5844. * internal consistency). */
  5845. static void intel_connector_verify_state(struct intel_connector *connector)
  5846. {
  5847. struct drm_crtc *crtc = connector->base.state->crtc;
  5848. DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  5849. connector->base.base.id,
  5850. connector->base.name);
  5851. if (connector->get_hw_state(connector)) {
  5852. struct intel_encoder *encoder = connector->encoder;
  5853. struct drm_connector_state *conn_state = connector->base.state;
  5854. I915_STATE_WARN(!crtc,
  5855. "connector enabled without attached crtc\n");
  5856. if (!crtc)
  5857. return;
  5858. I915_STATE_WARN(!crtc->state->active,
  5859. "connector is active, but attached crtc isn't\n");
  5860. if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
  5861. return;
  5862. I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
  5863. "atomic encoder doesn't match attached encoder\n");
  5864. I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
  5865. "attached encoder crtc differs from connector crtc\n");
  5866. } else {
  5867. I915_STATE_WARN(crtc && crtc->state->active,
  5868. "attached crtc is active, but connector isn't\n");
  5869. I915_STATE_WARN(!crtc && connector->base.state->best_encoder,
  5870. "best encoder set without crtc!\n");
  5871. }
  5872. }
  5873. int intel_connector_init(struct intel_connector *connector)
  5874. {
  5875. drm_atomic_helper_connector_reset(&connector->base);
  5876. if (!connector->base.state)
  5877. return -ENOMEM;
  5878. return 0;
  5879. }
  5880. struct intel_connector *intel_connector_alloc(void)
  5881. {
  5882. struct intel_connector *connector;
  5883. connector = kzalloc(sizeof *connector, GFP_KERNEL);
  5884. if (!connector)
  5885. return NULL;
  5886. if (intel_connector_init(connector) < 0) {
  5887. kfree(connector);
  5888. return NULL;
  5889. }
  5890. return connector;
  5891. }
  5892. /* Simple connector->get_hw_state implementation for encoders that support only
  5893. * one connector and no cloning and hence the encoder state determines the state
  5894. * of the connector. */
  5895. bool intel_connector_get_hw_state(struct intel_connector *connector)
  5896. {
  5897. enum pipe pipe = 0;
  5898. struct intel_encoder *encoder = connector->encoder;
  5899. return encoder->get_hw_state(encoder, &pipe);
  5900. }
  5901. static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
  5902. {
  5903. if (crtc_state->base.enable && crtc_state->has_pch_encoder)
  5904. return crtc_state->fdi_lanes;
  5905. return 0;
  5906. }
  5907. static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
  5908. struct intel_crtc_state *pipe_config)
  5909. {
  5910. struct drm_i915_private *dev_priv = to_i915(dev);
  5911. struct drm_atomic_state *state = pipe_config->base.state;
  5912. struct intel_crtc *other_crtc;
  5913. struct intel_crtc_state *other_crtc_state;
  5914. DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
  5915. pipe_name(pipe), pipe_config->fdi_lanes);
  5916. if (pipe_config->fdi_lanes > 4) {
  5917. DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
  5918. pipe_name(pipe), pipe_config->fdi_lanes);
  5919. return -EINVAL;
  5920. }
  5921. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  5922. if (pipe_config->fdi_lanes > 2) {
  5923. DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
  5924. pipe_config->fdi_lanes);
  5925. return -EINVAL;
  5926. } else {
  5927. return 0;
  5928. }
  5929. }
  5930. if (INTEL_INFO(dev_priv)->num_pipes == 2)
  5931. return 0;
  5932. /* Ivybridge 3 pipe is really complicated */
  5933. switch (pipe) {
  5934. case PIPE_A:
  5935. return 0;
  5936. case PIPE_B:
  5937. if (pipe_config->fdi_lanes <= 2)
  5938. return 0;
  5939. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_C);
  5940. other_crtc_state =
  5941. intel_atomic_get_crtc_state(state, other_crtc);
  5942. if (IS_ERR(other_crtc_state))
  5943. return PTR_ERR(other_crtc_state);
  5944. if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
  5945. DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
  5946. pipe_name(pipe), pipe_config->fdi_lanes);
  5947. return -EINVAL;
  5948. }
  5949. return 0;
  5950. case PIPE_C:
  5951. if (pipe_config->fdi_lanes > 2) {
  5952. DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
  5953. pipe_name(pipe), pipe_config->fdi_lanes);
  5954. return -EINVAL;
  5955. }
  5956. other_crtc = intel_get_crtc_for_pipe(dev_priv, PIPE_B);
  5957. other_crtc_state =
  5958. intel_atomic_get_crtc_state(state, other_crtc);
  5959. if (IS_ERR(other_crtc_state))
  5960. return PTR_ERR(other_crtc_state);
  5961. if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
  5962. DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
  5963. return -EINVAL;
  5964. }
  5965. return 0;
  5966. default:
  5967. BUG();
  5968. }
  5969. }
  5970. #define RETRY 1
  5971. static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
  5972. struct intel_crtc_state *pipe_config)
  5973. {
  5974. struct drm_device *dev = intel_crtc->base.dev;
  5975. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  5976. int lane, link_bw, fdi_dotclock, ret;
  5977. bool needs_recompute = false;
  5978. retry:
  5979. /* FDI is a binary signal running at ~2.7GHz, encoding
  5980. * each output octet as 10 bits. The actual frequency
  5981. * is stored as a divider into a 100MHz clock, and the
  5982. * mode pixel clock is stored in units of 1KHz.
  5983. * Hence the bw of each lane in terms of the mode signal
  5984. * is:
  5985. */
  5986. link_bw = intel_fdi_link_freq(to_i915(dev), pipe_config);
  5987. fdi_dotclock = adjusted_mode->crtc_clock;
  5988. lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
  5989. pipe_config->pipe_bpp);
  5990. pipe_config->fdi_lanes = lane;
  5991. intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
  5992. link_bw, &pipe_config->fdi_m_n);
  5993. ret = ironlake_check_fdi_lanes(dev, intel_crtc->pipe, pipe_config);
  5994. if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
  5995. pipe_config->pipe_bpp -= 2*3;
  5996. DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
  5997. pipe_config->pipe_bpp);
  5998. needs_recompute = true;
  5999. pipe_config->bw_constrained = true;
  6000. goto retry;
  6001. }
  6002. if (needs_recompute)
  6003. return RETRY;
  6004. return ret;
  6005. }
  6006. static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
  6007. struct intel_crtc_state *pipe_config)
  6008. {
  6009. if (pipe_config->pipe_bpp > 24)
  6010. return false;
  6011. /* HSW can handle pixel rate up to cdclk? */
  6012. if (IS_HASWELL(dev_priv))
  6013. return true;
  6014. /*
  6015. * We compare against max which means we must take
  6016. * the increased cdclk requirement into account when
  6017. * calculating the new cdclk.
  6018. *
  6019. * Should measure whether using a lower cdclk w/o IPS
  6020. */
  6021. return ilk_pipe_pixel_rate(pipe_config) <=
  6022. dev_priv->max_cdclk_freq * 95 / 100;
  6023. }
  6024. static void hsw_compute_ips_config(struct intel_crtc *crtc,
  6025. struct intel_crtc_state *pipe_config)
  6026. {
  6027. struct drm_device *dev = crtc->base.dev;
  6028. struct drm_i915_private *dev_priv = to_i915(dev);
  6029. pipe_config->ips_enabled = i915.enable_ips &&
  6030. hsw_crtc_supports_ips(crtc) &&
  6031. pipe_config_supports_ips(dev_priv, pipe_config);
  6032. }
  6033. static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
  6034. {
  6035. const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6036. /* GDG double wide on either pipe, otherwise pipe A only */
  6037. return INTEL_INFO(dev_priv)->gen < 4 &&
  6038. (crtc->pipe == PIPE_A || IS_I915G(dev_priv));
  6039. }
  6040. static int intel_crtc_compute_config(struct intel_crtc *crtc,
  6041. struct intel_crtc_state *pipe_config)
  6042. {
  6043. struct drm_device *dev = crtc->base.dev;
  6044. struct drm_i915_private *dev_priv = to_i915(dev);
  6045. const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
  6046. int clock_limit = dev_priv->max_dotclk_freq;
  6047. if (INTEL_GEN(dev_priv) < 4) {
  6048. clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
  6049. /*
  6050. * Enable double wide mode when the dot clock
  6051. * is > 90% of the (display) core speed.
  6052. */
  6053. if (intel_crtc_supports_double_wide(crtc) &&
  6054. adjusted_mode->crtc_clock > clock_limit) {
  6055. clock_limit = dev_priv->max_dotclk_freq;
  6056. pipe_config->double_wide = true;
  6057. }
  6058. }
  6059. if (adjusted_mode->crtc_clock > clock_limit) {
  6060. DRM_DEBUG_KMS("requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
  6061. adjusted_mode->crtc_clock, clock_limit,
  6062. yesno(pipe_config->double_wide));
  6063. return -EINVAL;
  6064. }
  6065. /*
  6066. * Pipe horizontal size must be even in:
  6067. * - DVO ganged mode
  6068. * - LVDS dual channel mode
  6069. * - Double wide pipe
  6070. */
  6071. if ((intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
  6072. intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
  6073. pipe_config->pipe_src_w &= ~1;
  6074. /* Cantiga+ cannot handle modes with a hsync front porch of 0.
  6075. * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
  6076. */
  6077. if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
  6078. adjusted_mode->crtc_hsync_start == adjusted_mode->crtc_hdisplay)
  6079. return -EINVAL;
  6080. if (HAS_IPS(dev_priv))
  6081. hsw_compute_ips_config(crtc, pipe_config);
  6082. if (pipe_config->has_pch_encoder)
  6083. return ironlake_fdi_compute_config(crtc, pipe_config);
  6084. return 0;
  6085. }
  6086. static int skylake_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6087. {
  6088. u32 cdctl;
  6089. skl_dpll0_update(dev_priv);
  6090. if (dev_priv->cdclk_pll.vco == 0)
  6091. return dev_priv->cdclk_pll.ref;
  6092. cdctl = I915_READ(CDCLK_CTL);
  6093. if (dev_priv->cdclk_pll.vco == 8640000) {
  6094. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  6095. case CDCLK_FREQ_450_432:
  6096. return 432000;
  6097. case CDCLK_FREQ_337_308:
  6098. return 308571;
  6099. case CDCLK_FREQ_540:
  6100. return 540000;
  6101. case CDCLK_FREQ_675_617:
  6102. return 617143;
  6103. default:
  6104. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  6105. }
  6106. } else {
  6107. switch (cdctl & CDCLK_FREQ_SEL_MASK) {
  6108. case CDCLK_FREQ_450_432:
  6109. return 450000;
  6110. case CDCLK_FREQ_337_308:
  6111. return 337500;
  6112. case CDCLK_FREQ_540:
  6113. return 540000;
  6114. case CDCLK_FREQ_675_617:
  6115. return 675000;
  6116. default:
  6117. MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
  6118. }
  6119. }
  6120. return dev_priv->cdclk_pll.ref;
  6121. }
  6122. static void bxt_de_pll_update(struct drm_i915_private *dev_priv)
  6123. {
  6124. u32 val;
  6125. dev_priv->cdclk_pll.ref = 19200;
  6126. dev_priv->cdclk_pll.vco = 0;
  6127. val = I915_READ(BXT_DE_PLL_ENABLE);
  6128. if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
  6129. return;
  6130. if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
  6131. return;
  6132. val = I915_READ(BXT_DE_PLL_CTL);
  6133. dev_priv->cdclk_pll.vco = (val & BXT_DE_PLL_RATIO_MASK) *
  6134. dev_priv->cdclk_pll.ref;
  6135. }
  6136. static int broxton_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6137. {
  6138. u32 divider;
  6139. int div, vco;
  6140. bxt_de_pll_update(dev_priv);
  6141. vco = dev_priv->cdclk_pll.vco;
  6142. if (vco == 0)
  6143. return dev_priv->cdclk_pll.ref;
  6144. divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
  6145. switch (divider) {
  6146. case BXT_CDCLK_CD2X_DIV_SEL_1:
  6147. div = 2;
  6148. break;
  6149. case BXT_CDCLK_CD2X_DIV_SEL_1_5:
  6150. WARN(IS_GEMINILAKE(dev_priv), "Unsupported divider\n");
  6151. div = 3;
  6152. break;
  6153. case BXT_CDCLK_CD2X_DIV_SEL_2:
  6154. div = 4;
  6155. break;
  6156. case BXT_CDCLK_CD2X_DIV_SEL_4:
  6157. div = 8;
  6158. break;
  6159. default:
  6160. MISSING_CASE(divider);
  6161. return dev_priv->cdclk_pll.ref;
  6162. }
  6163. return DIV_ROUND_CLOSEST(vco, div);
  6164. }
  6165. static int broadwell_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6166. {
  6167. uint32_t lcpll = I915_READ(LCPLL_CTL);
  6168. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  6169. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  6170. return 800000;
  6171. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  6172. return 450000;
  6173. else if (freq == LCPLL_CLK_FREQ_450)
  6174. return 450000;
  6175. else if (freq == LCPLL_CLK_FREQ_54O_BDW)
  6176. return 540000;
  6177. else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
  6178. return 337500;
  6179. else
  6180. return 675000;
  6181. }
  6182. static int haswell_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6183. {
  6184. uint32_t lcpll = I915_READ(LCPLL_CTL);
  6185. uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
  6186. if (lcpll & LCPLL_CD_SOURCE_FCLK)
  6187. return 800000;
  6188. else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
  6189. return 450000;
  6190. else if (freq == LCPLL_CLK_FREQ_450)
  6191. return 450000;
  6192. else if (IS_HSW_ULT(dev_priv))
  6193. return 337500;
  6194. else
  6195. return 540000;
  6196. }
  6197. static int valleyview_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6198. {
  6199. return vlv_get_cck_clock_hpll(dev_priv, "cdclk",
  6200. CCK_DISPLAY_CLOCK_CONTROL);
  6201. }
  6202. static int ilk_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6203. {
  6204. return 450000;
  6205. }
  6206. static int i945_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6207. {
  6208. return 400000;
  6209. }
  6210. static int i915_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6211. {
  6212. return 333333;
  6213. }
  6214. static int i9xx_misc_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6215. {
  6216. return 200000;
  6217. }
  6218. static int pnv_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6219. {
  6220. struct pci_dev *pdev = dev_priv->drm.pdev;
  6221. u16 gcfgc = 0;
  6222. pci_read_config_word(pdev, GCFGC, &gcfgc);
  6223. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  6224. case GC_DISPLAY_CLOCK_267_MHZ_PNV:
  6225. return 266667;
  6226. case GC_DISPLAY_CLOCK_333_MHZ_PNV:
  6227. return 333333;
  6228. case GC_DISPLAY_CLOCK_444_MHZ_PNV:
  6229. return 444444;
  6230. case GC_DISPLAY_CLOCK_200_MHZ_PNV:
  6231. return 200000;
  6232. default:
  6233. DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
  6234. case GC_DISPLAY_CLOCK_133_MHZ_PNV:
  6235. return 133333;
  6236. case GC_DISPLAY_CLOCK_167_MHZ_PNV:
  6237. return 166667;
  6238. }
  6239. }
  6240. static int i915gm_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6241. {
  6242. struct pci_dev *pdev = dev_priv->drm.pdev;
  6243. u16 gcfgc = 0;
  6244. pci_read_config_word(pdev, GCFGC, &gcfgc);
  6245. if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
  6246. return 133333;
  6247. else {
  6248. switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
  6249. case GC_DISPLAY_CLOCK_333_MHZ:
  6250. return 333333;
  6251. default:
  6252. case GC_DISPLAY_CLOCK_190_200_MHZ:
  6253. return 190000;
  6254. }
  6255. }
  6256. }
  6257. static int i865_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6258. {
  6259. return 266667;
  6260. }
  6261. static int i85x_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6262. {
  6263. struct pci_dev *pdev = dev_priv->drm.pdev;
  6264. u16 hpllcc = 0;
  6265. /*
  6266. * 852GM/852GMV only supports 133 MHz and the HPLLCC
  6267. * encoding is different :(
  6268. * FIXME is this the right way to detect 852GM/852GMV?
  6269. */
  6270. if (pdev->revision == 0x1)
  6271. return 133333;
  6272. pci_bus_read_config_word(pdev->bus,
  6273. PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
  6274. /* Assume that the hardware is in the high speed state. This
  6275. * should be the default.
  6276. */
  6277. switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
  6278. case GC_CLOCK_133_200:
  6279. case GC_CLOCK_133_200_2:
  6280. case GC_CLOCK_100_200:
  6281. return 200000;
  6282. case GC_CLOCK_166_250:
  6283. return 250000;
  6284. case GC_CLOCK_100_133:
  6285. return 133333;
  6286. case GC_CLOCK_133_266:
  6287. case GC_CLOCK_133_266_2:
  6288. case GC_CLOCK_166_266:
  6289. return 266667;
  6290. }
  6291. /* Shouldn't happen */
  6292. return 0;
  6293. }
  6294. static int i830_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6295. {
  6296. return 133333;
  6297. }
  6298. static unsigned int intel_hpll_vco(struct drm_i915_private *dev_priv)
  6299. {
  6300. static const unsigned int blb_vco[8] = {
  6301. [0] = 3200000,
  6302. [1] = 4000000,
  6303. [2] = 5333333,
  6304. [3] = 4800000,
  6305. [4] = 6400000,
  6306. };
  6307. static const unsigned int pnv_vco[8] = {
  6308. [0] = 3200000,
  6309. [1] = 4000000,
  6310. [2] = 5333333,
  6311. [3] = 4800000,
  6312. [4] = 2666667,
  6313. };
  6314. static const unsigned int cl_vco[8] = {
  6315. [0] = 3200000,
  6316. [1] = 4000000,
  6317. [2] = 5333333,
  6318. [3] = 6400000,
  6319. [4] = 3333333,
  6320. [5] = 3566667,
  6321. [6] = 4266667,
  6322. };
  6323. static const unsigned int elk_vco[8] = {
  6324. [0] = 3200000,
  6325. [1] = 4000000,
  6326. [2] = 5333333,
  6327. [3] = 4800000,
  6328. };
  6329. static const unsigned int ctg_vco[8] = {
  6330. [0] = 3200000,
  6331. [1] = 4000000,
  6332. [2] = 5333333,
  6333. [3] = 6400000,
  6334. [4] = 2666667,
  6335. [5] = 4266667,
  6336. };
  6337. const unsigned int *vco_table;
  6338. unsigned int vco;
  6339. uint8_t tmp = 0;
  6340. /* FIXME other chipsets? */
  6341. if (IS_GM45(dev_priv))
  6342. vco_table = ctg_vco;
  6343. else if (IS_G4X(dev_priv))
  6344. vco_table = elk_vco;
  6345. else if (IS_I965GM(dev_priv))
  6346. vco_table = cl_vco;
  6347. else if (IS_PINEVIEW(dev_priv))
  6348. vco_table = pnv_vco;
  6349. else if (IS_G33(dev_priv))
  6350. vco_table = blb_vco;
  6351. else
  6352. return 0;
  6353. tmp = I915_READ(IS_MOBILE(dev_priv) ? HPLLVCO_MOBILE : HPLLVCO);
  6354. vco = vco_table[tmp & 0x7];
  6355. if (vco == 0)
  6356. DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
  6357. else
  6358. DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
  6359. return vco;
  6360. }
  6361. static int gm45_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6362. {
  6363. struct pci_dev *pdev = dev_priv->drm.pdev;
  6364. unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
  6365. uint16_t tmp = 0;
  6366. pci_read_config_word(pdev, GCFGC, &tmp);
  6367. cdclk_sel = (tmp >> 12) & 0x1;
  6368. switch (vco) {
  6369. case 2666667:
  6370. case 4000000:
  6371. case 5333333:
  6372. return cdclk_sel ? 333333 : 222222;
  6373. case 3200000:
  6374. return cdclk_sel ? 320000 : 228571;
  6375. default:
  6376. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
  6377. return 222222;
  6378. }
  6379. }
  6380. static int i965gm_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6381. {
  6382. struct pci_dev *pdev = dev_priv->drm.pdev;
  6383. static const uint8_t div_3200[] = { 16, 10, 8 };
  6384. static const uint8_t div_4000[] = { 20, 12, 10 };
  6385. static const uint8_t div_5333[] = { 24, 16, 14 };
  6386. const uint8_t *div_table;
  6387. unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
  6388. uint16_t tmp = 0;
  6389. pci_read_config_word(pdev, GCFGC, &tmp);
  6390. cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
  6391. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  6392. goto fail;
  6393. switch (vco) {
  6394. case 3200000:
  6395. div_table = div_3200;
  6396. break;
  6397. case 4000000:
  6398. div_table = div_4000;
  6399. break;
  6400. case 5333333:
  6401. div_table = div_5333;
  6402. break;
  6403. default:
  6404. goto fail;
  6405. }
  6406. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  6407. fail:
  6408. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
  6409. return 200000;
  6410. }
  6411. static int g33_get_display_clock_speed(struct drm_i915_private *dev_priv)
  6412. {
  6413. struct pci_dev *pdev = dev_priv->drm.pdev;
  6414. static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
  6415. static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
  6416. static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
  6417. static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
  6418. const uint8_t *div_table;
  6419. unsigned int cdclk_sel, vco = intel_hpll_vco(dev_priv);
  6420. uint16_t tmp = 0;
  6421. pci_read_config_word(pdev, GCFGC, &tmp);
  6422. cdclk_sel = (tmp >> 4) & 0x7;
  6423. if (cdclk_sel >= ARRAY_SIZE(div_3200))
  6424. goto fail;
  6425. switch (vco) {
  6426. case 3200000:
  6427. div_table = div_3200;
  6428. break;
  6429. case 4000000:
  6430. div_table = div_4000;
  6431. break;
  6432. case 4800000:
  6433. div_table = div_4800;
  6434. break;
  6435. case 5333333:
  6436. div_table = div_5333;
  6437. break;
  6438. default:
  6439. goto fail;
  6440. }
  6441. return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
  6442. fail:
  6443. DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
  6444. return 190476;
  6445. }
  6446. static void
  6447. intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
  6448. {
  6449. while (*num > DATA_LINK_M_N_MASK ||
  6450. *den > DATA_LINK_M_N_MASK) {
  6451. *num >>= 1;
  6452. *den >>= 1;
  6453. }
  6454. }
  6455. static void compute_m_n(unsigned int m, unsigned int n,
  6456. uint32_t *ret_m, uint32_t *ret_n)
  6457. {
  6458. *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
  6459. *ret_m = div_u64((uint64_t) m * *ret_n, n);
  6460. intel_reduce_m_n_ratio(ret_m, ret_n);
  6461. }
  6462. void
  6463. intel_link_compute_m_n(int bits_per_pixel, int nlanes,
  6464. int pixel_clock, int link_clock,
  6465. struct intel_link_m_n *m_n)
  6466. {
  6467. m_n->tu = 64;
  6468. compute_m_n(bits_per_pixel * pixel_clock,
  6469. link_clock * nlanes * 8,
  6470. &m_n->gmch_m, &m_n->gmch_n);
  6471. compute_m_n(pixel_clock, link_clock,
  6472. &m_n->link_m, &m_n->link_n);
  6473. }
  6474. static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
  6475. {
  6476. if (i915.panel_use_ssc >= 0)
  6477. return i915.panel_use_ssc != 0;
  6478. return dev_priv->vbt.lvds_use_ssc
  6479. && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
  6480. }
  6481. static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
  6482. {
  6483. return (1 << dpll->n) << 16 | dpll->m2;
  6484. }
  6485. static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
  6486. {
  6487. return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
  6488. }
  6489. static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
  6490. struct intel_crtc_state *crtc_state,
  6491. struct dpll *reduced_clock)
  6492. {
  6493. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6494. u32 fp, fp2 = 0;
  6495. if (IS_PINEVIEW(dev_priv)) {
  6496. fp = pnv_dpll_compute_fp(&crtc_state->dpll);
  6497. if (reduced_clock)
  6498. fp2 = pnv_dpll_compute_fp(reduced_clock);
  6499. } else {
  6500. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  6501. if (reduced_clock)
  6502. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  6503. }
  6504. crtc_state->dpll_hw_state.fp0 = fp;
  6505. crtc->lowfreq_avail = false;
  6506. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6507. reduced_clock) {
  6508. crtc_state->dpll_hw_state.fp1 = fp2;
  6509. crtc->lowfreq_avail = true;
  6510. } else {
  6511. crtc_state->dpll_hw_state.fp1 = fp;
  6512. }
  6513. }
  6514. static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
  6515. pipe)
  6516. {
  6517. u32 reg_val;
  6518. /*
  6519. * PLLB opamp always calibrates to max value of 0x3f, force enable it
  6520. * and set it to a reasonable value instead.
  6521. */
  6522. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6523. reg_val &= 0xffffff00;
  6524. reg_val |= 0x00000030;
  6525. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6526. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6527. reg_val &= 0x8cffffff;
  6528. reg_val = 0x8c000000;
  6529. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6530. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
  6531. reg_val &= 0xffffff00;
  6532. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
  6533. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
  6534. reg_val &= 0x00ffffff;
  6535. reg_val |= 0xb0000000;
  6536. vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
  6537. }
  6538. static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
  6539. struct intel_link_m_n *m_n)
  6540. {
  6541. struct drm_device *dev = crtc->base.dev;
  6542. struct drm_i915_private *dev_priv = to_i915(dev);
  6543. int pipe = crtc->pipe;
  6544. I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6545. I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
  6546. I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
  6547. I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
  6548. }
  6549. static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
  6550. struct intel_link_m_n *m_n,
  6551. struct intel_link_m_n *m2_n2)
  6552. {
  6553. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6554. int pipe = crtc->pipe;
  6555. enum transcoder transcoder = crtc->config->cpu_transcoder;
  6556. if (INTEL_GEN(dev_priv) >= 5) {
  6557. I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6558. I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
  6559. I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
  6560. I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
  6561. /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
  6562. * for gen < 8) and if DRRS is supported (to make sure the
  6563. * registers are not unnecessarily accessed).
  6564. */
  6565. if (m2_n2 && (IS_CHERRYVIEW(dev_priv) ||
  6566. INTEL_GEN(dev_priv) < 8) && crtc->config->has_drrs) {
  6567. I915_WRITE(PIPE_DATA_M2(transcoder),
  6568. TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
  6569. I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
  6570. I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
  6571. I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
  6572. }
  6573. } else {
  6574. I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
  6575. I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
  6576. I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
  6577. I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
  6578. }
  6579. }
  6580. void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
  6581. {
  6582. struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
  6583. if (m_n == M1_N1) {
  6584. dp_m_n = &crtc->config->dp_m_n;
  6585. dp_m2_n2 = &crtc->config->dp_m2_n2;
  6586. } else if (m_n == M2_N2) {
  6587. /*
  6588. * M2_N2 registers are not supported. Hence m2_n2 divider value
  6589. * needs to be programmed into M1_N1.
  6590. */
  6591. dp_m_n = &crtc->config->dp_m2_n2;
  6592. } else {
  6593. DRM_ERROR("Unsupported divider value\n");
  6594. return;
  6595. }
  6596. if (crtc->config->has_pch_encoder)
  6597. intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
  6598. else
  6599. intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
  6600. }
  6601. static void vlv_compute_dpll(struct intel_crtc *crtc,
  6602. struct intel_crtc_state *pipe_config)
  6603. {
  6604. pipe_config->dpll_hw_state.dpll = DPLL_INTEGRATED_REF_CLK_VLV |
  6605. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6606. if (crtc->pipe != PIPE_A)
  6607. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6608. /* DPLL not used with DSI, but still need the rest set up */
  6609. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  6610. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE |
  6611. DPLL_EXT_BUFFER_ENABLE_VLV;
  6612. pipe_config->dpll_hw_state.dpll_md =
  6613. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6614. }
  6615. static void chv_compute_dpll(struct intel_crtc *crtc,
  6616. struct intel_crtc_state *pipe_config)
  6617. {
  6618. pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
  6619. DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
  6620. if (crtc->pipe != PIPE_A)
  6621. pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
  6622. /* DPLL not used with DSI, but still need the rest set up */
  6623. if (!intel_crtc_has_type(pipe_config, INTEL_OUTPUT_DSI))
  6624. pipe_config->dpll_hw_state.dpll |= DPLL_VCO_ENABLE;
  6625. pipe_config->dpll_hw_state.dpll_md =
  6626. (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6627. }
  6628. static void vlv_prepare_pll(struct intel_crtc *crtc,
  6629. const struct intel_crtc_state *pipe_config)
  6630. {
  6631. struct drm_device *dev = crtc->base.dev;
  6632. struct drm_i915_private *dev_priv = to_i915(dev);
  6633. enum pipe pipe = crtc->pipe;
  6634. u32 mdiv;
  6635. u32 bestn, bestm1, bestm2, bestp1, bestp2;
  6636. u32 coreclk, reg_val;
  6637. /* Enable Refclk */
  6638. I915_WRITE(DPLL(pipe),
  6639. pipe_config->dpll_hw_state.dpll &
  6640. ~(DPLL_VCO_ENABLE | DPLL_EXT_BUFFER_ENABLE_VLV));
  6641. /* No need to actually set up the DPLL with DSI */
  6642. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6643. return;
  6644. mutex_lock(&dev_priv->sb_lock);
  6645. bestn = pipe_config->dpll.n;
  6646. bestm1 = pipe_config->dpll.m1;
  6647. bestm2 = pipe_config->dpll.m2;
  6648. bestp1 = pipe_config->dpll.p1;
  6649. bestp2 = pipe_config->dpll.p2;
  6650. /* See eDP HDMI DPIO driver vbios notes doc */
  6651. /* PLL B needs special handling */
  6652. if (pipe == PIPE_B)
  6653. vlv_pllb_recal_opamp(dev_priv, pipe);
  6654. /* Set up Tx target for periodic Rcomp update */
  6655. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
  6656. /* Disable target IRef on PLL */
  6657. reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
  6658. reg_val &= 0x00ffffff;
  6659. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
  6660. /* Disable fast lock */
  6661. vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
  6662. /* Set idtafcrecal before PLL is enabled */
  6663. mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
  6664. mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
  6665. mdiv |= ((bestn << DPIO_N_SHIFT));
  6666. mdiv |= (1 << DPIO_K_SHIFT);
  6667. /*
  6668. * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
  6669. * but we don't support that).
  6670. * Note: don't use the DAC post divider as it seems unstable.
  6671. */
  6672. mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
  6673. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6674. mdiv |= DPIO_ENABLE_CALIBRATION;
  6675. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
  6676. /* Set HBR and RBR LPF coefficients */
  6677. if (pipe_config->port_clock == 162000 ||
  6678. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_ANALOG) ||
  6679. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI))
  6680. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6681. 0x009f0003);
  6682. else
  6683. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
  6684. 0x00d0000f);
  6685. if (intel_crtc_has_dp_encoder(pipe_config)) {
  6686. /* Use SSC source */
  6687. if (pipe == PIPE_A)
  6688. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6689. 0x0df40000);
  6690. else
  6691. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6692. 0x0df70000);
  6693. } else { /* HDMI or VGA */
  6694. /* Use bend source */
  6695. if (pipe == PIPE_A)
  6696. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6697. 0x0df70000);
  6698. else
  6699. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
  6700. 0x0df40000);
  6701. }
  6702. coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
  6703. coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
  6704. if (intel_crtc_has_dp_encoder(crtc->config))
  6705. coreclk |= 0x01000000;
  6706. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
  6707. vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
  6708. mutex_unlock(&dev_priv->sb_lock);
  6709. }
  6710. static void chv_prepare_pll(struct intel_crtc *crtc,
  6711. const struct intel_crtc_state *pipe_config)
  6712. {
  6713. struct drm_device *dev = crtc->base.dev;
  6714. struct drm_i915_private *dev_priv = to_i915(dev);
  6715. enum pipe pipe = crtc->pipe;
  6716. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  6717. u32 loopfilter, tribuf_calcntr;
  6718. u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
  6719. u32 dpio_val;
  6720. int vco;
  6721. /* Enable Refclk and SSC */
  6722. I915_WRITE(DPLL(pipe),
  6723. pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
  6724. /* No need to actually set up the DPLL with DSI */
  6725. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  6726. return;
  6727. bestn = pipe_config->dpll.n;
  6728. bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
  6729. bestm1 = pipe_config->dpll.m1;
  6730. bestm2 = pipe_config->dpll.m2 >> 22;
  6731. bestp1 = pipe_config->dpll.p1;
  6732. bestp2 = pipe_config->dpll.p2;
  6733. vco = pipe_config->dpll.vco;
  6734. dpio_val = 0;
  6735. loopfilter = 0;
  6736. mutex_lock(&dev_priv->sb_lock);
  6737. /* p1 and p2 divider */
  6738. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
  6739. 5 << DPIO_CHV_S1_DIV_SHIFT |
  6740. bestp1 << DPIO_CHV_P1_DIV_SHIFT |
  6741. bestp2 << DPIO_CHV_P2_DIV_SHIFT |
  6742. 1 << DPIO_CHV_K_DIV_SHIFT);
  6743. /* Feedback post-divider - m2 */
  6744. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
  6745. /* Feedback refclk divider - n and m1 */
  6746. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
  6747. DPIO_CHV_M1_DIV_BY_2 |
  6748. 1 << DPIO_CHV_N_DIV_SHIFT);
  6749. /* M2 fraction division */
  6750. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
  6751. /* M2 fraction division enable */
  6752. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  6753. dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
  6754. dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
  6755. if (bestm2_frac)
  6756. dpio_val |= DPIO_CHV_FRAC_DIV_EN;
  6757. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
  6758. /* Program digital lock detect threshold */
  6759. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
  6760. dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
  6761. DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
  6762. dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
  6763. if (!bestm2_frac)
  6764. dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
  6765. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
  6766. /* Loop filter */
  6767. if (vco == 5400000) {
  6768. loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
  6769. loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
  6770. loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6771. tribuf_calcntr = 0x9;
  6772. } else if (vco <= 6200000) {
  6773. loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
  6774. loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
  6775. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6776. tribuf_calcntr = 0x9;
  6777. } else if (vco <= 6480000) {
  6778. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6779. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6780. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6781. tribuf_calcntr = 0x8;
  6782. } else {
  6783. /* Not supported. Apply the same limits as in the max case */
  6784. loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
  6785. loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
  6786. loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
  6787. tribuf_calcntr = 0;
  6788. }
  6789. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
  6790. dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
  6791. dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
  6792. dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
  6793. vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
  6794. /* AFC Recal */
  6795. vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
  6796. vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
  6797. DPIO_AFC_RECAL);
  6798. mutex_unlock(&dev_priv->sb_lock);
  6799. }
  6800. /**
  6801. * vlv_force_pll_on - forcibly enable just the PLL
  6802. * @dev_priv: i915 private structure
  6803. * @pipe: pipe PLL to enable
  6804. * @dpll: PLL configuration
  6805. *
  6806. * Enable the PLL for @pipe using the supplied @dpll config. To be used
  6807. * in cases where we need the PLL enabled even when @pipe is not going to
  6808. * be enabled.
  6809. */
  6810. int vlv_force_pll_on(struct drm_i915_private *dev_priv, enum pipe pipe,
  6811. const struct dpll *dpll)
  6812. {
  6813. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  6814. struct intel_crtc_state *pipe_config;
  6815. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  6816. if (!pipe_config)
  6817. return -ENOMEM;
  6818. pipe_config->base.crtc = &crtc->base;
  6819. pipe_config->pixel_multiplier = 1;
  6820. pipe_config->dpll = *dpll;
  6821. if (IS_CHERRYVIEW(dev_priv)) {
  6822. chv_compute_dpll(crtc, pipe_config);
  6823. chv_prepare_pll(crtc, pipe_config);
  6824. chv_enable_pll(crtc, pipe_config);
  6825. } else {
  6826. vlv_compute_dpll(crtc, pipe_config);
  6827. vlv_prepare_pll(crtc, pipe_config);
  6828. vlv_enable_pll(crtc, pipe_config);
  6829. }
  6830. kfree(pipe_config);
  6831. return 0;
  6832. }
  6833. /**
  6834. * vlv_force_pll_off - forcibly disable just the PLL
  6835. * @dev_priv: i915 private structure
  6836. * @pipe: pipe PLL to disable
  6837. *
  6838. * Disable the PLL for @pipe. To be used in cases where we need
  6839. * the PLL enabled even when @pipe is not going to be enabled.
  6840. */
  6841. void vlv_force_pll_off(struct drm_i915_private *dev_priv, enum pipe pipe)
  6842. {
  6843. if (IS_CHERRYVIEW(dev_priv))
  6844. chv_disable_pll(dev_priv, pipe);
  6845. else
  6846. vlv_disable_pll(dev_priv, pipe);
  6847. }
  6848. static void i9xx_compute_dpll(struct intel_crtc *crtc,
  6849. struct intel_crtc_state *crtc_state,
  6850. struct dpll *reduced_clock)
  6851. {
  6852. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  6853. u32 dpll;
  6854. struct dpll *clock = &crtc_state->dpll;
  6855. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6856. dpll = DPLL_VGA_MODE_DIS;
  6857. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  6858. dpll |= DPLLB_MODE_LVDS;
  6859. else
  6860. dpll |= DPLLB_MODE_DAC_SERIAL;
  6861. if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  6862. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  6863. dpll |= (crtc_state->pixel_multiplier - 1)
  6864. << SDVO_MULTIPLIER_SHIFT_HIRES;
  6865. }
  6866. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  6867. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  6868. dpll |= DPLL_SDVO_HIGH_SPEED;
  6869. if (intel_crtc_has_dp_encoder(crtc_state))
  6870. dpll |= DPLL_SDVO_HIGH_SPEED;
  6871. /* compute bitmask from p1 value */
  6872. if (IS_PINEVIEW(dev_priv))
  6873. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
  6874. else {
  6875. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6876. if (IS_G4X(dev_priv) && reduced_clock)
  6877. dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  6878. }
  6879. switch (clock->p2) {
  6880. case 5:
  6881. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  6882. break;
  6883. case 7:
  6884. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  6885. break;
  6886. case 10:
  6887. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  6888. break;
  6889. case 14:
  6890. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  6891. break;
  6892. }
  6893. if (INTEL_GEN(dev_priv) >= 4)
  6894. dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
  6895. if (crtc_state->sdvo_tv_clock)
  6896. dpll |= PLL_REF_INPUT_TVCLKINBC;
  6897. else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6898. intel_panel_use_ssc(dev_priv))
  6899. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6900. else
  6901. dpll |= PLL_REF_INPUT_DREFCLK;
  6902. dpll |= DPLL_VCO_ENABLE;
  6903. crtc_state->dpll_hw_state.dpll = dpll;
  6904. if (INTEL_GEN(dev_priv) >= 4) {
  6905. u32 dpll_md = (crtc_state->pixel_multiplier - 1)
  6906. << DPLL_MD_UDI_MULTIPLIER_SHIFT;
  6907. crtc_state->dpll_hw_state.dpll_md = dpll_md;
  6908. }
  6909. }
  6910. static void i8xx_compute_dpll(struct intel_crtc *crtc,
  6911. struct intel_crtc_state *crtc_state,
  6912. struct dpll *reduced_clock)
  6913. {
  6914. struct drm_device *dev = crtc->base.dev;
  6915. struct drm_i915_private *dev_priv = to_i915(dev);
  6916. u32 dpll;
  6917. struct dpll *clock = &crtc_state->dpll;
  6918. i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
  6919. dpll = DPLL_VGA_MODE_DIS;
  6920. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  6921. dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6922. } else {
  6923. if (clock->p1 == 2)
  6924. dpll |= PLL_P1_DIVIDE_BY_TWO;
  6925. else
  6926. dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  6927. if (clock->p2 == 4)
  6928. dpll |= PLL_P2_DIVIDE_BY_4;
  6929. }
  6930. if (!IS_I830(dev_priv) &&
  6931. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO))
  6932. dpll |= DPLL_DVO_2X_MODE;
  6933. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  6934. intel_panel_use_ssc(dev_priv))
  6935. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  6936. else
  6937. dpll |= PLL_REF_INPUT_DREFCLK;
  6938. dpll |= DPLL_VCO_ENABLE;
  6939. crtc_state->dpll_hw_state.dpll = dpll;
  6940. }
  6941. static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
  6942. {
  6943. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  6944. enum pipe pipe = intel_crtc->pipe;
  6945. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  6946. const struct drm_display_mode *adjusted_mode = &intel_crtc->config->base.adjusted_mode;
  6947. uint32_t crtc_vtotal, crtc_vblank_end;
  6948. int vsyncshift = 0;
  6949. /* We need to be careful not to changed the adjusted mode, for otherwise
  6950. * the hw state checker will get angry at the mismatch. */
  6951. crtc_vtotal = adjusted_mode->crtc_vtotal;
  6952. crtc_vblank_end = adjusted_mode->crtc_vblank_end;
  6953. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
  6954. /* the chip adds 2 halflines automatically */
  6955. crtc_vtotal -= 1;
  6956. crtc_vblank_end -= 1;
  6957. if (intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  6958. vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
  6959. else
  6960. vsyncshift = adjusted_mode->crtc_hsync_start -
  6961. adjusted_mode->crtc_htotal / 2;
  6962. if (vsyncshift < 0)
  6963. vsyncshift += adjusted_mode->crtc_htotal;
  6964. }
  6965. if (INTEL_GEN(dev_priv) > 3)
  6966. I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
  6967. I915_WRITE(HTOTAL(cpu_transcoder),
  6968. (adjusted_mode->crtc_hdisplay - 1) |
  6969. ((adjusted_mode->crtc_htotal - 1) << 16));
  6970. I915_WRITE(HBLANK(cpu_transcoder),
  6971. (adjusted_mode->crtc_hblank_start - 1) |
  6972. ((adjusted_mode->crtc_hblank_end - 1) << 16));
  6973. I915_WRITE(HSYNC(cpu_transcoder),
  6974. (adjusted_mode->crtc_hsync_start - 1) |
  6975. ((adjusted_mode->crtc_hsync_end - 1) << 16));
  6976. I915_WRITE(VTOTAL(cpu_transcoder),
  6977. (adjusted_mode->crtc_vdisplay - 1) |
  6978. ((crtc_vtotal - 1) << 16));
  6979. I915_WRITE(VBLANK(cpu_transcoder),
  6980. (adjusted_mode->crtc_vblank_start - 1) |
  6981. ((crtc_vblank_end - 1) << 16));
  6982. I915_WRITE(VSYNC(cpu_transcoder),
  6983. (adjusted_mode->crtc_vsync_start - 1) |
  6984. ((adjusted_mode->crtc_vsync_end - 1) << 16));
  6985. /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
  6986. * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
  6987. * documented on the DDI_FUNC_CTL register description, EDP Input Select
  6988. * bits. */
  6989. if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
  6990. (pipe == PIPE_B || pipe == PIPE_C))
  6991. I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
  6992. }
  6993. static void intel_set_pipe_src_size(struct intel_crtc *intel_crtc)
  6994. {
  6995. struct drm_device *dev = intel_crtc->base.dev;
  6996. struct drm_i915_private *dev_priv = to_i915(dev);
  6997. enum pipe pipe = intel_crtc->pipe;
  6998. /* pipesrc controls the size that is scaled from, which should
  6999. * always be the user's requested size.
  7000. */
  7001. I915_WRITE(PIPESRC(pipe),
  7002. ((intel_crtc->config->pipe_src_w - 1) << 16) |
  7003. (intel_crtc->config->pipe_src_h - 1));
  7004. }
  7005. static void intel_get_pipe_timings(struct intel_crtc *crtc,
  7006. struct intel_crtc_state *pipe_config)
  7007. {
  7008. struct drm_device *dev = crtc->base.dev;
  7009. struct drm_i915_private *dev_priv = to_i915(dev);
  7010. enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
  7011. uint32_t tmp;
  7012. tmp = I915_READ(HTOTAL(cpu_transcoder));
  7013. pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
  7014. pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
  7015. tmp = I915_READ(HBLANK(cpu_transcoder));
  7016. pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
  7017. pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
  7018. tmp = I915_READ(HSYNC(cpu_transcoder));
  7019. pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
  7020. pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
  7021. tmp = I915_READ(VTOTAL(cpu_transcoder));
  7022. pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
  7023. pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
  7024. tmp = I915_READ(VBLANK(cpu_transcoder));
  7025. pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
  7026. pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
  7027. tmp = I915_READ(VSYNC(cpu_transcoder));
  7028. pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
  7029. pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
  7030. if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
  7031. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
  7032. pipe_config->base.adjusted_mode.crtc_vtotal += 1;
  7033. pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
  7034. }
  7035. }
  7036. static void intel_get_pipe_src_size(struct intel_crtc *crtc,
  7037. struct intel_crtc_state *pipe_config)
  7038. {
  7039. struct drm_device *dev = crtc->base.dev;
  7040. struct drm_i915_private *dev_priv = to_i915(dev);
  7041. u32 tmp;
  7042. tmp = I915_READ(PIPESRC(crtc->pipe));
  7043. pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
  7044. pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
  7045. pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
  7046. pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
  7047. }
  7048. void intel_mode_from_pipe_config(struct drm_display_mode *mode,
  7049. struct intel_crtc_state *pipe_config)
  7050. {
  7051. mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
  7052. mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
  7053. mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
  7054. mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
  7055. mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
  7056. mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
  7057. mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
  7058. mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
  7059. mode->flags = pipe_config->base.adjusted_mode.flags;
  7060. mode->type = DRM_MODE_TYPE_DRIVER;
  7061. mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
  7062. mode->hsync = drm_mode_hsync(mode);
  7063. mode->vrefresh = drm_mode_vrefresh(mode);
  7064. drm_mode_set_name(mode);
  7065. }
  7066. static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
  7067. {
  7068. struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
  7069. uint32_t pipeconf;
  7070. pipeconf = 0;
  7071. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  7072. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  7073. pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
  7074. if (intel_crtc->config->double_wide)
  7075. pipeconf |= PIPECONF_DOUBLE_WIDE;
  7076. /* only g4x and later have fancy bpc/dither controls */
  7077. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  7078. IS_CHERRYVIEW(dev_priv)) {
  7079. /* Bspec claims that we can't use dithering for 30bpp pipes. */
  7080. if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
  7081. pipeconf |= PIPECONF_DITHER_EN |
  7082. PIPECONF_DITHER_TYPE_SP;
  7083. switch (intel_crtc->config->pipe_bpp) {
  7084. case 18:
  7085. pipeconf |= PIPECONF_6BPC;
  7086. break;
  7087. case 24:
  7088. pipeconf |= PIPECONF_8BPC;
  7089. break;
  7090. case 30:
  7091. pipeconf |= PIPECONF_10BPC;
  7092. break;
  7093. default:
  7094. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7095. BUG();
  7096. }
  7097. }
  7098. if (HAS_PIPE_CXSR(dev_priv)) {
  7099. if (intel_crtc->lowfreq_avail) {
  7100. DRM_DEBUG_KMS("enabling CxSR downclocking\n");
  7101. pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
  7102. } else {
  7103. DRM_DEBUG_KMS("disabling CxSR downclocking\n");
  7104. }
  7105. }
  7106. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
  7107. if (INTEL_GEN(dev_priv) < 4 ||
  7108. intel_crtc_has_type(intel_crtc->config, INTEL_OUTPUT_SDVO))
  7109. pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
  7110. else
  7111. pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
  7112. } else
  7113. pipeconf |= PIPECONF_PROGRESSIVE;
  7114. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  7115. intel_crtc->config->limited_color_range)
  7116. pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
  7117. I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
  7118. POSTING_READ(PIPECONF(intel_crtc->pipe));
  7119. }
  7120. static int i8xx_crtc_compute_clock(struct intel_crtc *crtc,
  7121. struct intel_crtc_state *crtc_state)
  7122. {
  7123. struct drm_device *dev = crtc->base.dev;
  7124. struct drm_i915_private *dev_priv = to_i915(dev);
  7125. const struct intel_limit *limit;
  7126. int refclk = 48000;
  7127. memset(&crtc_state->dpll_hw_state, 0,
  7128. sizeof(crtc_state->dpll_hw_state));
  7129. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7130. if (intel_panel_use_ssc(dev_priv)) {
  7131. refclk = dev_priv->vbt.lvds_ssc_freq;
  7132. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7133. }
  7134. limit = &intel_limits_i8xx_lvds;
  7135. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DVO)) {
  7136. limit = &intel_limits_i8xx_dvo;
  7137. } else {
  7138. limit = &intel_limits_i8xx_dac;
  7139. }
  7140. if (!crtc_state->clock_set &&
  7141. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7142. refclk, NULL, &crtc_state->dpll)) {
  7143. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7144. return -EINVAL;
  7145. }
  7146. i8xx_compute_dpll(crtc, crtc_state, NULL);
  7147. return 0;
  7148. }
  7149. static int g4x_crtc_compute_clock(struct intel_crtc *crtc,
  7150. struct intel_crtc_state *crtc_state)
  7151. {
  7152. struct drm_device *dev = crtc->base.dev;
  7153. struct drm_i915_private *dev_priv = to_i915(dev);
  7154. const struct intel_limit *limit;
  7155. int refclk = 96000;
  7156. memset(&crtc_state->dpll_hw_state, 0,
  7157. sizeof(crtc_state->dpll_hw_state));
  7158. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7159. if (intel_panel_use_ssc(dev_priv)) {
  7160. refclk = dev_priv->vbt.lvds_ssc_freq;
  7161. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7162. }
  7163. if (intel_is_dual_link_lvds(dev))
  7164. limit = &intel_limits_g4x_dual_channel_lvds;
  7165. else
  7166. limit = &intel_limits_g4x_single_channel_lvds;
  7167. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ||
  7168. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
  7169. limit = &intel_limits_g4x_hdmi;
  7170. } else if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO)) {
  7171. limit = &intel_limits_g4x_sdvo;
  7172. } else {
  7173. /* The option is for other outputs */
  7174. limit = &intel_limits_i9xx_sdvo;
  7175. }
  7176. if (!crtc_state->clock_set &&
  7177. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7178. refclk, NULL, &crtc_state->dpll)) {
  7179. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7180. return -EINVAL;
  7181. }
  7182. i9xx_compute_dpll(crtc, crtc_state, NULL);
  7183. return 0;
  7184. }
  7185. static int pnv_crtc_compute_clock(struct intel_crtc *crtc,
  7186. struct intel_crtc_state *crtc_state)
  7187. {
  7188. struct drm_device *dev = crtc->base.dev;
  7189. struct drm_i915_private *dev_priv = to_i915(dev);
  7190. const struct intel_limit *limit;
  7191. int refclk = 96000;
  7192. memset(&crtc_state->dpll_hw_state, 0,
  7193. sizeof(crtc_state->dpll_hw_state));
  7194. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7195. if (intel_panel_use_ssc(dev_priv)) {
  7196. refclk = dev_priv->vbt.lvds_ssc_freq;
  7197. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7198. }
  7199. limit = &intel_limits_pineview_lvds;
  7200. } else {
  7201. limit = &intel_limits_pineview_sdvo;
  7202. }
  7203. if (!crtc_state->clock_set &&
  7204. !pnv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7205. refclk, NULL, &crtc_state->dpll)) {
  7206. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7207. return -EINVAL;
  7208. }
  7209. i9xx_compute_dpll(crtc, crtc_state, NULL);
  7210. return 0;
  7211. }
  7212. static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
  7213. struct intel_crtc_state *crtc_state)
  7214. {
  7215. struct drm_device *dev = crtc->base.dev;
  7216. struct drm_i915_private *dev_priv = to_i915(dev);
  7217. const struct intel_limit *limit;
  7218. int refclk = 96000;
  7219. memset(&crtc_state->dpll_hw_state, 0,
  7220. sizeof(crtc_state->dpll_hw_state));
  7221. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7222. if (intel_panel_use_ssc(dev_priv)) {
  7223. refclk = dev_priv->vbt.lvds_ssc_freq;
  7224. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
  7225. }
  7226. limit = &intel_limits_i9xx_lvds;
  7227. } else {
  7228. limit = &intel_limits_i9xx_sdvo;
  7229. }
  7230. if (!crtc_state->clock_set &&
  7231. !i9xx_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7232. refclk, NULL, &crtc_state->dpll)) {
  7233. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7234. return -EINVAL;
  7235. }
  7236. i9xx_compute_dpll(crtc, crtc_state, NULL);
  7237. return 0;
  7238. }
  7239. static int chv_crtc_compute_clock(struct intel_crtc *crtc,
  7240. struct intel_crtc_state *crtc_state)
  7241. {
  7242. int refclk = 100000;
  7243. const struct intel_limit *limit = &intel_limits_chv;
  7244. memset(&crtc_state->dpll_hw_state, 0,
  7245. sizeof(crtc_state->dpll_hw_state));
  7246. if (!crtc_state->clock_set &&
  7247. !chv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7248. refclk, NULL, &crtc_state->dpll)) {
  7249. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7250. return -EINVAL;
  7251. }
  7252. chv_compute_dpll(crtc, crtc_state);
  7253. return 0;
  7254. }
  7255. static int vlv_crtc_compute_clock(struct intel_crtc *crtc,
  7256. struct intel_crtc_state *crtc_state)
  7257. {
  7258. int refclk = 100000;
  7259. const struct intel_limit *limit = &intel_limits_vlv;
  7260. memset(&crtc_state->dpll_hw_state, 0,
  7261. sizeof(crtc_state->dpll_hw_state));
  7262. if (!crtc_state->clock_set &&
  7263. !vlv_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  7264. refclk, NULL, &crtc_state->dpll)) {
  7265. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  7266. return -EINVAL;
  7267. }
  7268. vlv_compute_dpll(crtc, crtc_state);
  7269. return 0;
  7270. }
  7271. static void i9xx_get_pfit_config(struct intel_crtc *crtc,
  7272. struct intel_crtc_state *pipe_config)
  7273. {
  7274. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7275. uint32_t tmp;
  7276. if (INTEL_GEN(dev_priv) <= 3 &&
  7277. (IS_I830(dev_priv) || !IS_MOBILE(dev_priv)))
  7278. return;
  7279. tmp = I915_READ(PFIT_CONTROL);
  7280. if (!(tmp & PFIT_ENABLE))
  7281. return;
  7282. /* Check whether the pfit is attached to our pipe. */
  7283. if (INTEL_GEN(dev_priv) < 4) {
  7284. if (crtc->pipe != PIPE_B)
  7285. return;
  7286. } else {
  7287. if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
  7288. return;
  7289. }
  7290. pipe_config->gmch_pfit.control = tmp;
  7291. pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
  7292. }
  7293. static void vlv_crtc_clock_get(struct intel_crtc *crtc,
  7294. struct intel_crtc_state *pipe_config)
  7295. {
  7296. struct drm_device *dev = crtc->base.dev;
  7297. struct drm_i915_private *dev_priv = to_i915(dev);
  7298. int pipe = pipe_config->cpu_transcoder;
  7299. struct dpll clock;
  7300. u32 mdiv;
  7301. int refclk = 100000;
  7302. /* In case of DSI, DPLL will not be used */
  7303. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  7304. return;
  7305. mutex_lock(&dev_priv->sb_lock);
  7306. mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
  7307. mutex_unlock(&dev_priv->sb_lock);
  7308. clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
  7309. clock.m2 = mdiv & DPIO_M2DIV_MASK;
  7310. clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
  7311. clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
  7312. clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
  7313. pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
  7314. }
  7315. static void
  7316. i9xx_get_initial_plane_config(struct intel_crtc *crtc,
  7317. struct intel_initial_plane_config *plane_config)
  7318. {
  7319. struct drm_device *dev = crtc->base.dev;
  7320. struct drm_i915_private *dev_priv = to_i915(dev);
  7321. u32 val, base, offset;
  7322. int pipe = crtc->pipe, plane = crtc->plane;
  7323. int fourcc, pixel_format;
  7324. unsigned int aligned_height;
  7325. struct drm_framebuffer *fb;
  7326. struct intel_framebuffer *intel_fb;
  7327. val = I915_READ(DSPCNTR(plane));
  7328. if (!(val & DISPLAY_PLANE_ENABLE))
  7329. return;
  7330. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  7331. if (!intel_fb) {
  7332. DRM_DEBUG_KMS("failed to alloc fb\n");
  7333. return;
  7334. }
  7335. fb = &intel_fb->base;
  7336. fb->dev = dev;
  7337. if (INTEL_GEN(dev_priv) >= 4) {
  7338. if (val & DISPPLANE_TILED) {
  7339. plane_config->tiling = I915_TILING_X;
  7340. fb->modifier = I915_FORMAT_MOD_X_TILED;
  7341. }
  7342. }
  7343. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  7344. fourcc = i9xx_format_to_fourcc(pixel_format);
  7345. fb->format = drm_format_info(fourcc);
  7346. if (INTEL_GEN(dev_priv) >= 4) {
  7347. if (plane_config->tiling)
  7348. offset = I915_READ(DSPTILEOFF(plane));
  7349. else
  7350. offset = I915_READ(DSPLINOFF(plane));
  7351. base = I915_READ(DSPSURF(plane)) & 0xfffff000;
  7352. } else {
  7353. base = I915_READ(DSPADDR(plane));
  7354. }
  7355. plane_config->base = base;
  7356. val = I915_READ(PIPESRC(pipe));
  7357. fb->width = ((val >> 16) & 0xfff) + 1;
  7358. fb->height = ((val >> 0) & 0xfff) + 1;
  7359. val = I915_READ(DSPSTRIDE(pipe));
  7360. fb->pitches[0] = val & 0xffffffc0;
  7361. aligned_height = intel_fb_align_height(dev, fb->height,
  7362. fb->format->format,
  7363. fb->modifier);
  7364. plane_config->size = fb->pitches[0] * aligned_height;
  7365. DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  7366. pipe_name(pipe), plane, fb->width, fb->height,
  7367. fb->format->cpp[0] * 8, base, fb->pitches[0],
  7368. plane_config->size);
  7369. plane_config->fb = intel_fb;
  7370. }
  7371. static void chv_crtc_clock_get(struct intel_crtc *crtc,
  7372. struct intel_crtc_state *pipe_config)
  7373. {
  7374. struct drm_device *dev = crtc->base.dev;
  7375. struct drm_i915_private *dev_priv = to_i915(dev);
  7376. int pipe = pipe_config->cpu_transcoder;
  7377. enum dpio_channel port = vlv_pipe_to_channel(pipe);
  7378. struct dpll clock;
  7379. u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
  7380. int refclk = 100000;
  7381. /* In case of DSI, DPLL will not be used */
  7382. if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
  7383. return;
  7384. mutex_lock(&dev_priv->sb_lock);
  7385. cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
  7386. pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
  7387. pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
  7388. pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
  7389. pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
  7390. mutex_unlock(&dev_priv->sb_lock);
  7391. clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
  7392. clock.m2 = (pll_dw0 & 0xff) << 22;
  7393. if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
  7394. clock.m2 |= pll_dw2 & 0x3fffff;
  7395. clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
  7396. clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
  7397. clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
  7398. pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
  7399. }
  7400. static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
  7401. struct intel_crtc_state *pipe_config)
  7402. {
  7403. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  7404. enum intel_display_power_domain power_domain;
  7405. uint32_t tmp;
  7406. bool ret;
  7407. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  7408. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  7409. return false;
  7410. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  7411. pipe_config->shared_dpll = NULL;
  7412. ret = false;
  7413. tmp = I915_READ(PIPECONF(crtc->pipe));
  7414. if (!(tmp & PIPECONF_ENABLE))
  7415. goto out;
  7416. if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  7417. IS_CHERRYVIEW(dev_priv)) {
  7418. switch (tmp & PIPECONF_BPC_MASK) {
  7419. case PIPECONF_6BPC:
  7420. pipe_config->pipe_bpp = 18;
  7421. break;
  7422. case PIPECONF_8BPC:
  7423. pipe_config->pipe_bpp = 24;
  7424. break;
  7425. case PIPECONF_10BPC:
  7426. pipe_config->pipe_bpp = 30;
  7427. break;
  7428. default:
  7429. break;
  7430. }
  7431. }
  7432. if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
  7433. (tmp & PIPECONF_COLOR_RANGE_SELECT))
  7434. pipe_config->limited_color_range = true;
  7435. if (INTEL_GEN(dev_priv) < 4)
  7436. pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
  7437. intel_get_pipe_timings(crtc, pipe_config);
  7438. intel_get_pipe_src_size(crtc, pipe_config);
  7439. i9xx_get_pfit_config(crtc, pipe_config);
  7440. if (INTEL_GEN(dev_priv) >= 4) {
  7441. /* No way to read it out on pipes B and C */
  7442. if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
  7443. tmp = dev_priv->chv_dpll_md[crtc->pipe];
  7444. else
  7445. tmp = I915_READ(DPLL_MD(crtc->pipe));
  7446. pipe_config->pixel_multiplier =
  7447. ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
  7448. >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
  7449. pipe_config->dpll_hw_state.dpll_md = tmp;
  7450. } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
  7451. IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
  7452. tmp = I915_READ(DPLL(crtc->pipe));
  7453. pipe_config->pixel_multiplier =
  7454. ((tmp & SDVO_MULTIPLIER_MASK)
  7455. >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
  7456. } else {
  7457. /* Note that on i915G/GM the pixel multiplier is in the sdvo
  7458. * port and will be fixed up in the encoder->get_config
  7459. * function. */
  7460. pipe_config->pixel_multiplier = 1;
  7461. }
  7462. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
  7463. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  7464. /*
  7465. * DPLL_DVO_2X_MODE must be enabled for both DPLLs
  7466. * on 830. Filter it out here so that we don't
  7467. * report errors due to that.
  7468. */
  7469. if (IS_I830(dev_priv))
  7470. pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
  7471. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
  7472. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
  7473. } else {
  7474. /* Mask out read-only status bits. */
  7475. pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
  7476. DPLL_PORTC_READY_MASK |
  7477. DPLL_PORTB_READY_MASK);
  7478. }
  7479. if (IS_CHERRYVIEW(dev_priv))
  7480. chv_crtc_clock_get(crtc, pipe_config);
  7481. else if (IS_VALLEYVIEW(dev_priv))
  7482. vlv_crtc_clock_get(crtc, pipe_config);
  7483. else
  7484. i9xx_crtc_clock_get(crtc, pipe_config);
  7485. /*
  7486. * Normally the dotclock is filled in by the encoder .get_config()
  7487. * but in case the pipe is enabled w/o any ports we need a sane
  7488. * default.
  7489. */
  7490. pipe_config->base.adjusted_mode.crtc_clock =
  7491. pipe_config->port_clock / pipe_config->pixel_multiplier;
  7492. ret = true;
  7493. out:
  7494. intel_display_power_put(dev_priv, power_domain);
  7495. return ret;
  7496. }
  7497. static void ironlake_init_pch_refclk(struct drm_i915_private *dev_priv)
  7498. {
  7499. struct intel_encoder *encoder;
  7500. int i;
  7501. u32 val, final;
  7502. bool has_lvds = false;
  7503. bool has_cpu_edp = false;
  7504. bool has_panel = false;
  7505. bool has_ck505 = false;
  7506. bool can_ssc = false;
  7507. bool using_ssc_source = false;
  7508. /* We need to take the global config into account */
  7509. for_each_intel_encoder(&dev_priv->drm, encoder) {
  7510. switch (encoder->type) {
  7511. case INTEL_OUTPUT_LVDS:
  7512. has_panel = true;
  7513. has_lvds = true;
  7514. break;
  7515. case INTEL_OUTPUT_EDP:
  7516. has_panel = true;
  7517. if (enc_to_dig_port(&encoder->base)->port == PORT_A)
  7518. has_cpu_edp = true;
  7519. break;
  7520. default:
  7521. break;
  7522. }
  7523. }
  7524. if (HAS_PCH_IBX(dev_priv)) {
  7525. has_ck505 = dev_priv->vbt.display_clock_mode;
  7526. can_ssc = has_ck505;
  7527. } else {
  7528. has_ck505 = false;
  7529. can_ssc = true;
  7530. }
  7531. /* Check if any DPLLs are using the SSC source */
  7532. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  7533. u32 temp = I915_READ(PCH_DPLL(i));
  7534. if (!(temp & DPLL_VCO_ENABLE))
  7535. continue;
  7536. if ((temp & PLL_REF_INPUT_MASK) ==
  7537. PLLB_REF_INPUT_SPREADSPECTRUMIN) {
  7538. using_ssc_source = true;
  7539. break;
  7540. }
  7541. }
  7542. DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
  7543. has_panel, has_lvds, has_ck505, using_ssc_source);
  7544. /* Ironlake: try to setup display ref clock before DPLL
  7545. * enabling. This is only under driver's control after
  7546. * PCH B stepping, previous chipset stepping should be
  7547. * ignoring this setting.
  7548. */
  7549. val = I915_READ(PCH_DREF_CONTROL);
  7550. /* As we must carefully and slowly disable/enable each source in turn,
  7551. * compute the final state we want first and check if we need to
  7552. * make any changes at all.
  7553. */
  7554. final = val;
  7555. final &= ~DREF_NONSPREAD_SOURCE_MASK;
  7556. if (has_ck505)
  7557. final |= DREF_NONSPREAD_CK505_ENABLE;
  7558. else
  7559. final |= DREF_NONSPREAD_SOURCE_ENABLE;
  7560. final &= ~DREF_SSC_SOURCE_MASK;
  7561. final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7562. final &= ~DREF_SSC1_ENABLE;
  7563. if (has_panel) {
  7564. final |= DREF_SSC_SOURCE_ENABLE;
  7565. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7566. final |= DREF_SSC1_ENABLE;
  7567. if (has_cpu_edp) {
  7568. if (intel_panel_use_ssc(dev_priv) && can_ssc)
  7569. final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7570. else
  7571. final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7572. } else
  7573. final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7574. } else if (using_ssc_source) {
  7575. final |= DREF_SSC_SOURCE_ENABLE;
  7576. final |= DREF_SSC1_ENABLE;
  7577. }
  7578. if (final == val)
  7579. return;
  7580. /* Always enable nonspread source */
  7581. val &= ~DREF_NONSPREAD_SOURCE_MASK;
  7582. if (has_ck505)
  7583. val |= DREF_NONSPREAD_CK505_ENABLE;
  7584. else
  7585. val |= DREF_NONSPREAD_SOURCE_ENABLE;
  7586. if (has_panel) {
  7587. val &= ~DREF_SSC_SOURCE_MASK;
  7588. val |= DREF_SSC_SOURCE_ENABLE;
  7589. /* SSC must be turned on before enabling the CPU output */
  7590. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7591. DRM_DEBUG_KMS("Using SSC on panel\n");
  7592. val |= DREF_SSC1_ENABLE;
  7593. } else
  7594. val &= ~DREF_SSC1_ENABLE;
  7595. /* Get SSC going before enabling the outputs */
  7596. I915_WRITE(PCH_DREF_CONTROL, val);
  7597. POSTING_READ(PCH_DREF_CONTROL);
  7598. udelay(200);
  7599. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7600. /* Enable CPU source on CPU attached eDP */
  7601. if (has_cpu_edp) {
  7602. if (intel_panel_use_ssc(dev_priv) && can_ssc) {
  7603. DRM_DEBUG_KMS("Using SSC on eDP\n");
  7604. val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
  7605. } else
  7606. val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
  7607. } else
  7608. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7609. I915_WRITE(PCH_DREF_CONTROL, val);
  7610. POSTING_READ(PCH_DREF_CONTROL);
  7611. udelay(200);
  7612. } else {
  7613. DRM_DEBUG_KMS("Disabling CPU source output\n");
  7614. val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
  7615. /* Turn off CPU output */
  7616. val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
  7617. I915_WRITE(PCH_DREF_CONTROL, val);
  7618. POSTING_READ(PCH_DREF_CONTROL);
  7619. udelay(200);
  7620. if (!using_ssc_source) {
  7621. DRM_DEBUG_KMS("Disabling SSC source\n");
  7622. /* Turn off the SSC source */
  7623. val &= ~DREF_SSC_SOURCE_MASK;
  7624. val |= DREF_SSC_SOURCE_DISABLE;
  7625. /* Turn off SSC1 */
  7626. val &= ~DREF_SSC1_ENABLE;
  7627. I915_WRITE(PCH_DREF_CONTROL, val);
  7628. POSTING_READ(PCH_DREF_CONTROL);
  7629. udelay(200);
  7630. }
  7631. }
  7632. BUG_ON(val != final);
  7633. }
  7634. static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
  7635. {
  7636. uint32_t tmp;
  7637. tmp = I915_READ(SOUTH_CHICKEN2);
  7638. tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
  7639. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7640. if (wait_for_us(I915_READ(SOUTH_CHICKEN2) &
  7641. FDI_MPHY_IOSFSB_RESET_STATUS, 100))
  7642. DRM_ERROR("FDI mPHY reset assert timeout\n");
  7643. tmp = I915_READ(SOUTH_CHICKEN2);
  7644. tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
  7645. I915_WRITE(SOUTH_CHICKEN2, tmp);
  7646. if (wait_for_us((I915_READ(SOUTH_CHICKEN2) &
  7647. FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
  7648. DRM_ERROR("FDI mPHY reset de-assert timeout\n");
  7649. }
  7650. /* WaMPhyProgramming:hsw */
  7651. static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
  7652. {
  7653. uint32_t tmp;
  7654. tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
  7655. tmp &= ~(0xFF << 24);
  7656. tmp |= (0x12 << 24);
  7657. intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
  7658. tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
  7659. tmp |= (1 << 11);
  7660. intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
  7661. tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
  7662. tmp |= (1 << 11);
  7663. intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
  7664. tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
  7665. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7666. intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
  7667. tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
  7668. tmp |= (1 << 24) | (1 << 21) | (1 << 18);
  7669. intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
  7670. tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
  7671. tmp &= ~(7 << 13);
  7672. tmp |= (5 << 13);
  7673. intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
  7674. tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
  7675. tmp &= ~(7 << 13);
  7676. tmp |= (5 << 13);
  7677. intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
  7678. tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
  7679. tmp &= ~0xFF;
  7680. tmp |= 0x1C;
  7681. intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
  7682. tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
  7683. tmp &= ~0xFF;
  7684. tmp |= 0x1C;
  7685. intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
  7686. tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
  7687. tmp &= ~(0xFF << 16);
  7688. tmp |= (0x1C << 16);
  7689. intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
  7690. tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
  7691. tmp &= ~(0xFF << 16);
  7692. tmp |= (0x1C << 16);
  7693. intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
  7694. tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
  7695. tmp |= (1 << 27);
  7696. intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
  7697. tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
  7698. tmp |= (1 << 27);
  7699. intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
  7700. tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
  7701. tmp &= ~(0xF << 28);
  7702. tmp |= (4 << 28);
  7703. intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
  7704. tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
  7705. tmp &= ~(0xF << 28);
  7706. tmp |= (4 << 28);
  7707. intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
  7708. }
  7709. /* Implements 3 different sequences from BSpec chapter "Display iCLK
  7710. * Programming" based on the parameters passed:
  7711. * - Sequence to enable CLKOUT_DP
  7712. * - Sequence to enable CLKOUT_DP without spread
  7713. * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
  7714. */
  7715. static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
  7716. bool with_spread, bool with_fdi)
  7717. {
  7718. uint32_t reg, tmp;
  7719. if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
  7720. with_spread = true;
  7721. if (WARN(HAS_PCH_LPT_LP(dev_priv) &&
  7722. with_fdi, "LP PCH doesn't have FDI\n"))
  7723. with_fdi = false;
  7724. mutex_lock(&dev_priv->sb_lock);
  7725. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7726. tmp &= ~SBI_SSCCTL_DISABLE;
  7727. tmp |= SBI_SSCCTL_PATHALT;
  7728. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7729. udelay(24);
  7730. if (with_spread) {
  7731. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7732. tmp &= ~SBI_SSCCTL_PATHALT;
  7733. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7734. if (with_fdi) {
  7735. lpt_reset_fdi_mphy(dev_priv);
  7736. lpt_program_fdi_mphy(dev_priv);
  7737. }
  7738. }
  7739. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  7740. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7741. tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7742. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7743. mutex_unlock(&dev_priv->sb_lock);
  7744. }
  7745. /* Sequence to disable CLKOUT_DP */
  7746. static void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
  7747. {
  7748. uint32_t reg, tmp;
  7749. mutex_lock(&dev_priv->sb_lock);
  7750. reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
  7751. tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
  7752. tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
  7753. intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
  7754. tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
  7755. if (!(tmp & SBI_SSCCTL_DISABLE)) {
  7756. if (!(tmp & SBI_SSCCTL_PATHALT)) {
  7757. tmp |= SBI_SSCCTL_PATHALT;
  7758. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7759. udelay(32);
  7760. }
  7761. tmp |= SBI_SSCCTL_DISABLE;
  7762. intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
  7763. }
  7764. mutex_unlock(&dev_priv->sb_lock);
  7765. }
  7766. #define BEND_IDX(steps) ((50 + (steps)) / 5)
  7767. static const uint16_t sscdivintphase[] = {
  7768. [BEND_IDX( 50)] = 0x3B23,
  7769. [BEND_IDX( 45)] = 0x3B23,
  7770. [BEND_IDX( 40)] = 0x3C23,
  7771. [BEND_IDX( 35)] = 0x3C23,
  7772. [BEND_IDX( 30)] = 0x3D23,
  7773. [BEND_IDX( 25)] = 0x3D23,
  7774. [BEND_IDX( 20)] = 0x3E23,
  7775. [BEND_IDX( 15)] = 0x3E23,
  7776. [BEND_IDX( 10)] = 0x3F23,
  7777. [BEND_IDX( 5)] = 0x3F23,
  7778. [BEND_IDX( 0)] = 0x0025,
  7779. [BEND_IDX( -5)] = 0x0025,
  7780. [BEND_IDX(-10)] = 0x0125,
  7781. [BEND_IDX(-15)] = 0x0125,
  7782. [BEND_IDX(-20)] = 0x0225,
  7783. [BEND_IDX(-25)] = 0x0225,
  7784. [BEND_IDX(-30)] = 0x0325,
  7785. [BEND_IDX(-35)] = 0x0325,
  7786. [BEND_IDX(-40)] = 0x0425,
  7787. [BEND_IDX(-45)] = 0x0425,
  7788. [BEND_IDX(-50)] = 0x0525,
  7789. };
  7790. /*
  7791. * Bend CLKOUT_DP
  7792. * steps -50 to 50 inclusive, in steps of 5
  7793. * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
  7794. * change in clock period = -(steps / 10) * 5.787 ps
  7795. */
  7796. static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
  7797. {
  7798. uint32_t tmp;
  7799. int idx = BEND_IDX(steps);
  7800. if (WARN_ON(steps % 5 != 0))
  7801. return;
  7802. if (WARN_ON(idx >= ARRAY_SIZE(sscdivintphase)))
  7803. return;
  7804. mutex_lock(&dev_priv->sb_lock);
  7805. if (steps % 10 != 0)
  7806. tmp = 0xAAAAAAAB;
  7807. else
  7808. tmp = 0x00000000;
  7809. intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
  7810. tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
  7811. tmp &= 0xffff0000;
  7812. tmp |= sscdivintphase[idx];
  7813. intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
  7814. mutex_unlock(&dev_priv->sb_lock);
  7815. }
  7816. #undef BEND_IDX
  7817. static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
  7818. {
  7819. struct intel_encoder *encoder;
  7820. bool has_vga = false;
  7821. for_each_intel_encoder(&dev_priv->drm, encoder) {
  7822. switch (encoder->type) {
  7823. case INTEL_OUTPUT_ANALOG:
  7824. has_vga = true;
  7825. break;
  7826. default:
  7827. break;
  7828. }
  7829. }
  7830. if (has_vga) {
  7831. lpt_bend_clkout_dp(dev_priv, 0);
  7832. lpt_enable_clkout_dp(dev_priv, true, true);
  7833. } else {
  7834. lpt_disable_clkout_dp(dev_priv);
  7835. }
  7836. }
  7837. /*
  7838. * Initialize reference clocks when the driver loads
  7839. */
  7840. void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
  7841. {
  7842. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
  7843. ironlake_init_pch_refclk(dev_priv);
  7844. else if (HAS_PCH_LPT(dev_priv))
  7845. lpt_init_pch_refclk(dev_priv);
  7846. }
  7847. static void ironlake_set_pipeconf(struct drm_crtc *crtc)
  7848. {
  7849. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7850. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7851. int pipe = intel_crtc->pipe;
  7852. uint32_t val;
  7853. val = 0;
  7854. switch (intel_crtc->config->pipe_bpp) {
  7855. case 18:
  7856. val |= PIPECONF_6BPC;
  7857. break;
  7858. case 24:
  7859. val |= PIPECONF_8BPC;
  7860. break;
  7861. case 30:
  7862. val |= PIPECONF_10BPC;
  7863. break;
  7864. case 36:
  7865. val |= PIPECONF_12BPC;
  7866. break;
  7867. default:
  7868. /* Case prevented by intel_choose_pipe_bpp_dither. */
  7869. BUG();
  7870. }
  7871. if (intel_crtc->config->dither)
  7872. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7873. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7874. val |= PIPECONF_INTERLACED_ILK;
  7875. else
  7876. val |= PIPECONF_PROGRESSIVE;
  7877. if (intel_crtc->config->limited_color_range)
  7878. val |= PIPECONF_COLOR_RANGE_SELECT;
  7879. I915_WRITE(PIPECONF(pipe), val);
  7880. POSTING_READ(PIPECONF(pipe));
  7881. }
  7882. static void haswell_set_pipeconf(struct drm_crtc *crtc)
  7883. {
  7884. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7885. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7886. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  7887. u32 val = 0;
  7888. if (IS_HASWELL(dev_priv) && intel_crtc->config->dither)
  7889. val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
  7890. if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
  7891. val |= PIPECONF_INTERLACED_ILK;
  7892. else
  7893. val |= PIPECONF_PROGRESSIVE;
  7894. I915_WRITE(PIPECONF(cpu_transcoder), val);
  7895. POSTING_READ(PIPECONF(cpu_transcoder));
  7896. }
  7897. static void haswell_set_pipemisc(struct drm_crtc *crtc)
  7898. {
  7899. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  7900. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  7901. if (IS_BROADWELL(dev_priv) || INTEL_INFO(dev_priv)->gen >= 9) {
  7902. u32 val = 0;
  7903. switch (intel_crtc->config->pipe_bpp) {
  7904. case 18:
  7905. val |= PIPEMISC_DITHER_6_BPC;
  7906. break;
  7907. case 24:
  7908. val |= PIPEMISC_DITHER_8_BPC;
  7909. break;
  7910. case 30:
  7911. val |= PIPEMISC_DITHER_10_BPC;
  7912. break;
  7913. case 36:
  7914. val |= PIPEMISC_DITHER_12_BPC;
  7915. break;
  7916. default:
  7917. /* Case prevented by pipe_config_set_bpp. */
  7918. BUG();
  7919. }
  7920. if (intel_crtc->config->dither)
  7921. val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
  7922. I915_WRITE(PIPEMISC(intel_crtc->pipe), val);
  7923. }
  7924. }
  7925. int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
  7926. {
  7927. /*
  7928. * Account for spread spectrum to avoid
  7929. * oversubscribing the link. Max center spread
  7930. * is 2.5%; use 5% for safety's sake.
  7931. */
  7932. u32 bps = target_clock * bpp * 21 / 20;
  7933. return DIV_ROUND_UP(bps, link_bw * 8);
  7934. }
  7935. static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
  7936. {
  7937. return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
  7938. }
  7939. static void ironlake_compute_dpll(struct intel_crtc *intel_crtc,
  7940. struct intel_crtc_state *crtc_state,
  7941. struct dpll *reduced_clock)
  7942. {
  7943. struct drm_crtc *crtc = &intel_crtc->base;
  7944. struct drm_device *dev = crtc->dev;
  7945. struct drm_i915_private *dev_priv = to_i915(dev);
  7946. u32 dpll, fp, fp2;
  7947. int factor;
  7948. /* Enable autotuning of the PLL clock (if permissible) */
  7949. factor = 21;
  7950. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  7951. if ((intel_panel_use_ssc(dev_priv) &&
  7952. dev_priv->vbt.lvds_ssc_freq == 100000) ||
  7953. (HAS_PCH_IBX(dev_priv) && intel_is_dual_link_lvds(dev)))
  7954. factor = 25;
  7955. } else if (crtc_state->sdvo_tv_clock)
  7956. factor = 20;
  7957. fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
  7958. if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
  7959. fp |= FP_CB_TUNE;
  7960. if (reduced_clock) {
  7961. fp2 = i9xx_dpll_compute_fp(reduced_clock);
  7962. if (reduced_clock->m < factor * reduced_clock->n)
  7963. fp2 |= FP_CB_TUNE;
  7964. } else {
  7965. fp2 = fp;
  7966. }
  7967. dpll = 0;
  7968. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS))
  7969. dpll |= DPLLB_MODE_LVDS;
  7970. else
  7971. dpll |= DPLLB_MODE_DAC_SERIAL;
  7972. dpll |= (crtc_state->pixel_multiplier - 1)
  7973. << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
  7974. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO) ||
  7975. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
  7976. dpll |= DPLL_SDVO_HIGH_SPEED;
  7977. if (intel_crtc_has_dp_encoder(crtc_state))
  7978. dpll |= DPLL_SDVO_HIGH_SPEED;
  7979. /*
  7980. * The high speed IO clock is only really required for
  7981. * SDVO/HDMI/DP, but we also enable it for CRT to make it
  7982. * possible to share the DPLL between CRT and HDMI. Enabling
  7983. * the clock needlessly does no real harm, except use up a
  7984. * bit of power potentially.
  7985. *
  7986. * We'll limit this to IVB with 3 pipes, since it has only two
  7987. * DPLLs and so DPLL sharing is the only way to get three pipes
  7988. * driving PCH ports at the same time. On SNB we could do this,
  7989. * and potentially avoid enabling the second DPLL, but it's not
  7990. * clear if it''s a win or loss power wise. No point in doing
  7991. * this on ILK at all since it has a fixed DPLL<->pipe mapping.
  7992. */
  7993. if (INTEL_INFO(dev_priv)->num_pipes == 3 &&
  7994. intel_crtc_has_type(crtc_state, INTEL_OUTPUT_ANALOG))
  7995. dpll |= DPLL_SDVO_HIGH_SPEED;
  7996. /* compute bitmask from p1 value */
  7997. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
  7998. /* also FPA1 */
  7999. dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
  8000. switch (crtc_state->dpll.p2) {
  8001. case 5:
  8002. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
  8003. break;
  8004. case 7:
  8005. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
  8006. break;
  8007. case 10:
  8008. dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
  8009. break;
  8010. case 14:
  8011. dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
  8012. break;
  8013. }
  8014. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  8015. intel_panel_use_ssc(dev_priv))
  8016. dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
  8017. else
  8018. dpll |= PLL_REF_INPUT_DREFCLK;
  8019. dpll |= DPLL_VCO_ENABLE;
  8020. crtc_state->dpll_hw_state.dpll = dpll;
  8021. crtc_state->dpll_hw_state.fp0 = fp;
  8022. crtc_state->dpll_hw_state.fp1 = fp2;
  8023. }
  8024. static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
  8025. struct intel_crtc_state *crtc_state)
  8026. {
  8027. struct drm_device *dev = crtc->base.dev;
  8028. struct drm_i915_private *dev_priv = to_i915(dev);
  8029. struct dpll reduced_clock;
  8030. bool has_reduced_clock = false;
  8031. struct intel_shared_dpll *pll;
  8032. const struct intel_limit *limit;
  8033. int refclk = 120000;
  8034. memset(&crtc_state->dpll_hw_state, 0,
  8035. sizeof(crtc_state->dpll_hw_state));
  8036. crtc->lowfreq_avail = false;
  8037. /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
  8038. if (!crtc_state->has_pch_encoder)
  8039. return 0;
  8040. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS)) {
  8041. if (intel_panel_use_ssc(dev_priv)) {
  8042. DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
  8043. dev_priv->vbt.lvds_ssc_freq);
  8044. refclk = dev_priv->vbt.lvds_ssc_freq;
  8045. }
  8046. if (intel_is_dual_link_lvds(dev)) {
  8047. if (refclk == 100000)
  8048. limit = &intel_limits_ironlake_dual_lvds_100m;
  8049. else
  8050. limit = &intel_limits_ironlake_dual_lvds;
  8051. } else {
  8052. if (refclk == 100000)
  8053. limit = &intel_limits_ironlake_single_lvds_100m;
  8054. else
  8055. limit = &intel_limits_ironlake_single_lvds;
  8056. }
  8057. } else {
  8058. limit = &intel_limits_ironlake_dac;
  8059. }
  8060. if (!crtc_state->clock_set &&
  8061. !g4x_find_best_dpll(limit, crtc_state, crtc_state->port_clock,
  8062. refclk, NULL, &crtc_state->dpll)) {
  8063. DRM_ERROR("Couldn't find PLL settings for mode!\n");
  8064. return -EINVAL;
  8065. }
  8066. ironlake_compute_dpll(crtc, crtc_state,
  8067. has_reduced_clock ? &reduced_clock : NULL);
  8068. pll = intel_get_shared_dpll(crtc, crtc_state, NULL);
  8069. if (pll == NULL) {
  8070. DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
  8071. pipe_name(crtc->pipe));
  8072. return -EINVAL;
  8073. }
  8074. if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
  8075. has_reduced_clock)
  8076. crtc->lowfreq_avail = true;
  8077. return 0;
  8078. }
  8079. static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
  8080. struct intel_link_m_n *m_n)
  8081. {
  8082. struct drm_device *dev = crtc->base.dev;
  8083. struct drm_i915_private *dev_priv = to_i915(dev);
  8084. enum pipe pipe = crtc->pipe;
  8085. m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
  8086. m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
  8087. m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
  8088. & ~TU_SIZE_MASK;
  8089. m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
  8090. m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
  8091. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8092. }
  8093. static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
  8094. enum transcoder transcoder,
  8095. struct intel_link_m_n *m_n,
  8096. struct intel_link_m_n *m2_n2)
  8097. {
  8098. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8099. enum pipe pipe = crtc->pipe;
  8100. if (INTEL_GEN(dev_priv) >= 5) {
  8101. m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
  8102. m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
  8103. m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
  8104. & ~TU_SIZE_MASK;
  8105. m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
  8106. m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
  8107. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8108. /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
  8109. * gen < 8) and if DRRS is supported (to make sure the
  8110. * registers are not unnecessarily read).
  8111. */
  8112. if (m2_n2 && INTEL_GEN(dev_priv) < 8 &&
  8113. crtc->config->has_drrs) {
  8114. m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
  8115. m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
  8116. m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
  8117. & ~TU_SIZE_MASK;
  8118. m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
  8119. m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
  8120. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8121. }
  8122. } else {
  8123. m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
  8124. m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
  8125. m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
  8126. & ~TU_SIZE_MASK;
  8127. m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
  8128. m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
  8129. & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
  8130. }
  8131. }
  8132. void intel_dp_get_m_n(struct intel_crtc *crtc,
  8133. struct intel_crtc_state *pipe_config)
  8134. {
  8135. if (pipe_config->has_pch_encoder)
  8136. intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
  8137. else
  8138. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  8139. &pipe_config->dp_m_n,
  8140. &pipe_config->dp_m2_n2);
  8141. }
  8142. static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
  8143. struct intel_crtc_state *pipe_config)
  8144. {
  8145. intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
  8146. &pipe_config->fdi_m_n, NULL);
  8147. }
  8148. static void skylake_get_pfit_config(struct intel_crtc *crtc,
  8149. struct intel_crtc_state *pipe_config)
  8150. {
  8151. struct drm_device *dev = crtc->base.dev;
  8152. struct drm_i915_private *dev_priv = to_i915(dev);
  8153. struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
  8154. uint32_t ps_ctrl = 0;
  8155. int id = -1;
  8156. int i;
  8157. /* find scaler attached to this pipe */
  8158. for (i = 0; i < crtc->num_scalers; i++) {
  8159. ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
  8160. if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
  8161. id = i;
  8162. pipe_config->pch_pfit.enabled = true;
  8163. pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
  8164. pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
  8165. break;
  8166. }
  8167. }
  8168. scaler_state->scaler_id = id;
  8169. if (id >= 0) {
  8170. scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
  8171. } else {
  8172. scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8173. }
  8174. }
  8175. static void
  8176. skylake_get_initial_plane_config(struct intel_crtc *crtc,
  8177. struct intel_initial_plane_config *plane_config)
  8178. {
  8179. struct drm_device *dev = crtc->base.dev;
  8180. struct drm_i915_private *dev_priv = to_i915(dev);
  8181. u32 val, base, offset, stride_mult, tiling;
  8182. int pipe = crtc->pipe;
  8183. int fourcc, pixel_format;
  8184. unsigned int aligned_height;
  8185. struct drm_framebuffer *fb;
  8186. struct intel_framebuffer *intel_fb;
  8187. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8188. if (!intel_fb) {
  8189. DRM_DEBUG_KMS("failed to alloc fb\n");
  8190. return;
  8191. }
  8192. fb = &intel_fb->base;
  8193. fb->dev = dev;
  8194. val = I915_READ(PLANE_CTL(pipe, 0));
  8195. if (!(val & PLANE_CTL_ENABLE))
  8196. goto error;
  8197. pixel_format = val & PLANE_CTL_FORMAT_MASK;
  8198. fourcc = skl_format_to_fourcc(pixel_format,
  8199. val & PLANE_CTL_ORDER_RGBX,
  8200. val & PLANE_CTL_ALPHA_MASK);
  8201. fb->format = drm_format_info(fourcc);
  8202. tiling = val & PLANE_CTL_TILED_MASK;
  8203. switch (tiling) {
  8204. case PLANE_CTL_TILED_LINEAR:
  8205. fb->modifier = DRM_FORMAT_MOD_NONE;
  8206. break;
  8207. case PLANE_CTL_TILED_X:
  8208. plane_config->tiling = I915_TILING_X;
  8209. fb->modifier = I915_FORMAT_MOD_X_TILED;
  8210. break;
  8211. case PLANE_CTL_TILED_Y:
  8212. fb->modifier = I915_FORMAT_MOD_Y_TILED;
  8213. break;
  8214. case PLANE_CTL_TILED_YF:
  8215. fb->modifier = I915_FORMAT_MOD_Yf_TILED;
  8216. break;
  8217. default:
  8218. MISSING_CASE(tiling);
  8219. goto error;
  8220. }
  8221. base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
  8222. plane_config->base = base;
  8223. offset = I915_READ(PLANE_OFFSET(pipe, 0));
  8224. val = I915_READ(PLANE_SIZE(pipe, 0));
  8225. fb->height = ((val >> 16) & 0xfff) + 1;
  8226. fb->width = ((val >> 0) & 0x1fff) + 1;
  8227. val = I915_READ(PLANE_STRIDE(pipe, 0));
  8228. stride_mult = intel_fb_stride_alignment(dev_priv, fb->modifier,
  8229. fb->format->format);
  8230. fb->pitches[0] = (val & 0x3ff) * stride_mult;
  8231. aligned_height = intel_fb_align_height(dev, fb->height,
  8232. fb->format->format,
  8233. fb->modifier);
  8234. plane_config->size = fb->pitches[0] * aligned_height;
  8235. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  8236. pipe_name(pipe), fb->width, fb->height,
  8237. fb->format->cpp[0] * 8, base, fb->pitches[0],
  8238. plane_config->size);
  8239. plane_config->fb = intel_fb;
  8240. return;
  8241. error:
  8242. kfree(intel_fb);
  8243. }
  8244. static void ironlake_get_pfit_config(struct intel_crtc *crtc,
  8245. struct intel_crtc_state *pipe_config)
  8246. {
  8247. struct drm_device *dev = crtc->base.dev;
  8248. struct drm_i915_private *dev_priv = to_i915(dev);
  8249. uint32_t tmp;
  8250. tmp = I915_READ(PF_CTL(crtc->pipe));
  8251. if (tmp & PF_ENABLE) {
  8252. pipe_config->pch_pfit.enabled = true;
  8253. pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
  8254. pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
  8255. /* We currently do not free assignements of panel fitters on
  8256. * ivb/hsw (since we don't use the higher upscaling modes which
  8257. * differentiates them) so just WARN about this case for now. */
  8258. if (IS_GEN7(dev_priv)) {
  8259. WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
  8260. PF_PIPE_SEL_IVB(crtc->pipe));
  8261. }
  8262. }
  8263. }
  8264. static void
  8265. ironlake_get_initial_plane_config(struct intel_crtc *crtc,
  8266. struct intel_initial_plane_config *plane_config)
  8267. {
  8268. struct drm_device *dev = crtc->base.dev;
  8269. struct drm_i915_private *dev_priv = to_i915(dev);
  8270. u32 val, base, offset;
  8271. int pipe = crtc->pipe;
  8272. int fourcc, pixel_format;
  8273. unsigned int aligned_height;
  8274. struct drm_framebuffer *fb;
  8275. struct intel_framebuffer *intel_fb;
  8276. val = I915_READ(DSPCNTR(pipe));
  8277. if (!(val & DISPLAY_PLANE_ENABLE))
  8278. return;
  8279. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  8280. if (!intel_fb) {
  8281. DRM_DEBUG_KMS("failed to alloc fb\n");
  8282. return;
  8283. }
  8284. fb = &intel_fb->base;
  8285. fb->dev = dev;
  8286. if (INTEL_GEN(dev_priv) >= 4) {
  8287. if (val & DISPPLANE_TILED) {
  8288. plane_config->tiling = I915_TILING_X;
  8289. fb->modifier = I915_FORMAT_MOD_X_TILED;
  8290. }
  8291. }
  8292. pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
  8293. fourcc = i9xx_format_to_fourcc(pixel_format);
  8294. fb->format = drm_format_info(fourcc);
  8295. base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
  8296. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  8297. offset = I915_READ(DSPOFFSET(pipe));
  8298. } else {
  8299. if (plane_config->tiling)
  8300. offset = I915_READ(DSPTILEOFF(pipe));
  8301. else
  8302. offset = I915_READ(DSPLINOFF(pipe));
  8303. }
  8304. plane_config->base = base;
  8305. val = I915_READ(PIPESRC(pipe));
  8306. fb->width = ((val >> 16) & 0xfff) + 1;
  8307. fb->height = ((val >> 0) & 0xfff) + 1;
  8308. val = I915_READ(DSPSTRIDE(pipe));
  8309. fb->pitches[0] = val & 0xffffffc0;
  8310. aligned_height = intel_fb_align_height(dev, fb->height,
  8311. fb->format->format,
  8312. fb->modifier);
  8313. plane_config->size = fb->pitches[0] * aligned_height;
  8314. DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
  8315. pipe_name(pipe), fb->width, fb->height,
  8316. fb->format->cpp[0] * 8, base, fb->pitches[0],
  8317. plane_config->size);
  8318. plane_config->fb = intel_fb;
  8319. }
  8320. static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
  8321. struct intel_crtc_state *pipe_config)
  8322. {
  8323. struct drm_device *dev = crtc->base.dev;
  8324. struct drm_i915_private *dev_priv = to_i915(dev);
  8325. enum intel_display_power_domain power_domain;
  8326. uint32_t tmp;
  8327. bool ret;
  8328. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  8329. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8330. return false;
  8331. pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
  8332. pipe_config->shared_dpll = NULL;
  8333. ret = false;
  8334. tmp = I915_READ(PIPECONF(crtc->pipe));
  8335. if (!(tmp & PIPECONF_ENABLE))
  8336. goto out;
  8337. switch (tmp & PIPECONF_BPC_MASK) {
  8338. case PIPECONF_6BPC:
  8339. pipe_config->pipe_bpp = 18;
  8340. break;
  8341. case PIPECONF_8BPC:
  8342. pipe_config->pipe_bpp = 24;
  8343. break;
  8344. case PIPECONF_10BPC:
  8345. pipe_config->pipe_bpp = 30;
  8346. break;
  8347. case PIPECONF_12BPC:
  8348. pipe_config->pipe_bpp = 36;
  8349. break;
  8350. default:
  8351. break;
  8352. }
  8353. if (tmp & PIPECONF_COLOR_RANGE_SELECT)
  8354. pipe_config->limited_color_range = true;
  8355. if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
  8356. struct intel_shared_dpll *pll;
  8357. enum intel_dpll_id pll_id;
  8358. pipe_config->has_pch_encoder = true;
  8359. tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
  8360. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8361. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8362. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8363. if (HAS_PCH_IBX(dev_priv)) {
  8364. /*
  8365. * The pipe->pch transcoder and pch transcoder->pll
  8366. * mapping is fixed.
  8367. */
  8368. pll_id = (enum intel_dpll_id) crtc->pipe;
  8369. } else {
  8370. tmp = I915_READ(PCH_DPLL_SEL);
  8371. if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
  8372. pll_id = DPLL_ID_PCH_PLL_B;
  8373. else
  8374. pll_id= DPLL_ID_PCH_PLL_A;
  8375. }
  8376. pipe_config->shared_dpll =
  8377. intel_get_shared_dpll_by_id(dev_priv, pll_id);
  8378. pll = pipe_config->shared_dpll;
  8379. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  8380. &pipe_config->dpll_hw_state));
  8381. tmp = pipe_config->dpll_hw_state.dpll;
  8382. pipe_config->pixel_multiplier =
  8383. ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
  8384. >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
  8385. ironlake_pch_clock_get(crtc, pipe_config);
  8386. } else {
  8387. pipe_config->pixel_multiplier = 1;
  8388. }
  8389. intel_get_pipe_timings(crtc, pipe_config);
  8390. intel_get_pipe_src_size(crtc, pipe_config);
  8391. ironlake_get_pfit_config(crtc, pipe_config);
  8392. ret = true;
  8393. out:
  8394. intel_display_power_put(dev_priv, power_domain);
  8395. return ret;
  8396. }
  8397. static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
  8398. {
  8399. struct drm_device *dev = &dev_priv->drm;
  8400. struct intel_crtc *crtc;
  8401. for_each_intel_crtc(dev, crtc)
  8402. I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
  8403. pipe_name(crtc->pipe));
  8404. I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
  8405. I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
  8406. I915_STATE_WARN(I915_READ(WRPLL_CTL(0)) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
  8407. I915_STATE_WARN(I915_READ(WRPLL_CTL(1)) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
  8408. I915_STATE_WARN(I915_READ(PP_STATUS(0)) & PP_ON, "Panel power on\n");
  8409. I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
  8410. "CPU PWM1 enabled\n");
  8411. if (IS_HASWELL(dev_priv))
  8412. I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
  8413. "CPU PWM2 enabled\n");
  8414. I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
  8415. "PCH PWM1 enabled\n");
  8416. I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
  8417. "Utility pin enabled\n");
  8418. I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
  8419. /*
  8420. * In theory we can still leave IRQs enabled, as long as only the HPD
  8421. * interrupts remain enabled. We used to check for that, but since it's
  8422. * gen-specific and since we only disable LCPLL after we fully disable
  8423. * the interrupts, the check below should be enough.
  8424. */
  8425. I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
  8426. }
  8427. static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
  8428. {
  8429. if (IS_HASWELL(dev_priv))
  8430. return I915_READ(D_COMP_HSW);
  8431. else
  8432. return I915_READ(D_COMP_BDW);
  8433. }
  8434. static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
  8435. {
  8436. if (IS_HASWELL(dev_priv)) {
  8437. mutex_lock(&dev_priv->rps.hw_lock);
  8438. if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
  8439. val))
  8440. DRM_DEBUG_KMS("Failed to write to D_COMP\n");
  8441. mutex_unlock(&dev_priv->rps.hw_lock);
  8442. } else {
  8443. I915_WRITE(D_COMP_BDW, val);
  8444. POSTING_READ(D_COMP_BDW);
  8445. }
  8446. }
  8447. /*
  8448. * This function implements pieces of two sequences from BSpec:
  8449. * - Sequence for display software to disable LCPLL
  8450. * - Sequence for display software to allow package C8+
  8451. * The steps implemented here are just the steps that actually touch the LCPLL
  8452. * register. Callers should take care of disabling all the display engine
  8453. * functions, doing the mode unset, fixing interrupts, etc.
  8454. */
  8455. static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
  8456. bool switch_to_fclk, bool allow_power_down)
  8457. {
  8458. uint32_t val;
  8459. assert_can_disable_lcpll(dev_priv);
  8460. val = I915_READ(LCPLL_CTL);
  8461. if (switch_to_fclk) {
  8462. val |= LCPLL_CD_SOURCE_FCLK;
  8463. I915_WRITE(LCPLL_CTL, val);
  8464. if (wait_for_us(I915_READ(LCPLL_CTL) &
  8465. LCPLL_CD_SOURCE_FCLK_DONE, 1))
  8466. DRM_ERROR("Switching to FCLK failed\n");
  8467. val = I915_READ(LCPLL_CTL);
  8468. }
  8469. val |= LCPLL_PLL_DISABLE;
  8470. I915_WRITE(LCPLL_CTL, val);
  8471. POSTING_READ(LCPLL_CTL);
  8472. if (intel_wait_for_register(dev_priv, LCPLL_CTL, LCPLL_PLL_LOCK, 0, 1))
  8473. DRM_ERROR("LCPLL still locked\n");
  8474. val = hsw_read_dcomp(dev_priv);
  8475. val |= D_COMP_COMP_DISABLE;
  8476. hsw_write_dcomp(dev_priv, val);
  8477. ndelay(100);
  8478. if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
  8479. 1))
  8480. DRM_ERROR("D_COMP RCOMP still in progress\n");
  8481. if (allow_power_down) {
  8482. val = I915_READ(LCPLL_CTL);
  8483. val |= LCPLL_POWER_DOWN_ALLOW;
  8484. I915_WRITE(LCPLL_CTL, val);
  8485. POSTING_READ(LCPLL_CTL);
  8486. }
  8487. }
  8488. /*
  8489. * Fully restores LCPLL, disallowing power down and switching back to LCPLL
  8490. * source.
  8491. */
  8492. static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
  8493. {
  8494. uint32_t val;
  8495. val = I915_READ(LCPLL_CTL);
  8496. if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
  8497. LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
  8498. return;
  8499. /*
  8500. * Make sure we're not on PC8 state before disabling PC8, otherwise
  8501. * we'll hang the machine. To prevent PC8 state, just enable force_wake.
  8502. */
  8503. intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
  8504. if (val & LCPLL_POWER_DOWN_ALLOW) {
  8505. val &= ~LCPLL_POWER_DOWN_ALLOW;
  8506. I915_WRITE(LCPLL_CTL, val);
  8507. POSTING_READ(LCPLL_CTL);
  8508. }
  8509. val = hsw_read_dcomp(dev_priv);
  8510. val |= D_COMP_COMP_FORCE;
  8511. val &= ~D_COMP_COMP_DISABLE;
  8512. hsw_write_dcomp(dev_priv, val);
  8513. val = I915_READ(LCPLL_CTL);
  8514. val &= ~LCPLL_PLL_DISABLE;
  8515. I915_WRITE(LCPLL_CTL, val);
  8516. if (intel_wait_for_register(dev_priv,
  8517. LCPLL_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
  8518. 5))
  8519. DRM_ERROR("LCPLL not locked yet\n");
  8520. if (val & LCPLL_CD_SOURCE_FCLK) {
  8521. val = I915_READ(LCPLL_CTL);
  8522. val &= ~LCPLL_CD_SOURCE_FCLK;
  8523. I915_WRITE(LCPLL_CTL, val);
  8524. if (wait_for_us((I915_READ(LCPLL_CTL) &
  8525. LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
  8526. DRM_ERROR("Switching back to LCPLL failed\n");
  8527. }
  8528. intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
  8529. intel_update_cdclk(dev_priv);
  8530. }
  8531. /*
  8532. * Package states C8 and deeper are really deep PC states that can only be
  8533. * reached when all the devices on the system allow it, so even if the graphics
  8534. * device allows PC8+, it doesn't mean the system will actually get to these
  8535. * states. Our driver only allows PC8+ when going into runtime PM.
  8536. *
  8537. * The requirements for PC8+ are that all the outputs are disabled, the power
  8538. * well is disabled and most interrupts are disabled, and these are also
  8539. * requirements for runtime PM. When these conditions are met, we manually do
  8540. * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
  8541. * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
  8542. * hang the machine.
  8543. *
  8544. * When we really reach PC8 or deeper states (not just when we allow it) we lose
  8545. * the state of some registers, so when we come back from PC8+ we need to
  8546. * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
  8547. * need to take care of the registers kept by RC6. Notice that this happens even
  8548. * if we don't put the device in PCI D3 state (which is what currently happens
  8549. * because of the runtime PM support).
  8550. *
  8551. * For more, read "Display Sequences for Package C8" on the hardware
  8552. * documentation.
  8553. */
  8554. void hsw_enable_pc8(struct drm_i915_private *dev_priv)
  8555. {
  8556. uint32_t val;
  8557. DRM_DEBUG_KMS("Enabling package C8+\n");
  8558. if (HAS_PCH_LPT_LP(dev_priv)) {
  8559. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8560. val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
  8561. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8562. }
  8563. lpt_disable_clkout_dp(dev_priv);
  8564. hsw_disable_lcpll(dev_priv, true, true);
  8565. }
  8566. void hsw_disable_pc8(struct drm_i915_private *dev_priv)
  8567. {
  8568. uint32_t val;
  8569. DRM_DEBUG_KMS("Disabling package C8+\n");
  8570. hsw_restore_lcpll(dev_priv);
  8571. lpt_init_pch_refclk(dev_priv);
  8572. if (HAS_PCH_LPT_LP(dev_priv)) {
  8573. val = I915_READ(SOUTH_DSPCLK_GATE_D);
  8574. val |= PCH_LP_PARTITION_LEVEL_DISABLE;
  8575. I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
  8576. }
  8577. }
  8578. static void bxt_modeset_commit_cdclk(struct drm_atomic_state *old_state)
  8579. {
  8580. struct drm_device *dev = old_state->dev;
  8581. struct intel_atomic_state *old_intel_state =
  8582. to_intel_atomic_state(old_state);
  8583. unsigned int req_cdclk = old_intel_state->dev_cdclk;
  8584. bxt_set_cdclk(to_i915(dev), req_cdclk);
  8585. }
  8586. static int bdw_adjust_min_pipe_pixel_rate(struct intel_crtc_state *crtc_state,
  8587. int pixel_rate)
  8588. {
  8589. struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
  8590. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  8591. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  8592. pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
  8593. /* BSpec says "Do not use DisplayPort with CDCLK less than
  8594. * 432 MHz, audio enabled, port width x4, and link rate
  8595. * HBR2 (5.4 GHz), or else there may be audio corruption or
  8596. * screen corruption."
  8597. */
  8598. if (intel_crtc_has_dp_encoder(crtc_state) &&
  8599. crtc_state->has_audio &&
  8600. crtc_state->port_clock >= 540000 &&
  8601. crtc_state->lane_count == 4)
  8602. pixel_rate = max(432000, pixel_rate);
  8603. return pixel_rate;
  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. if (IS_BROADWELL(dev_priv) || IS_GEN9(dev_priv))
  8626. pixel_rate = bdw_adjust_min_pipe_pixel_rate(crtc_state,
  8627. pixel_rate);
  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_priv);
  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_i915_private *dev_priv = to_i915(crtc->base.dev);
  8937. struct intel_shared_dpll *pll;
  8938. enum port port;
  8939. uint32_t tmp;
  8940. tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
  8941. port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
  8942. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  8943. skylake_get_ddi_pll(dev_priv, port, pipe_config);
  8944. else if (IS_GEN9_LP(dev_priv))
  8945. bxt_get_ddi_pll(dev_priv, port, pipe_config);
  8946. else
  8947. haswell_get_ddi_pll(dev_priv, port, pipe_config);
  8948. pll = pipe_config->shared_dpll;
  8949. if (pll) {
  8950. WARN_ON(!pll->funcs.get_hw_state(dev_priv, pll,
  8951. &pipe_config->dpll_hw_state));
  8952. }
  8953. /*
  8954. * Haswell has only FDI/PCH transcoder A. It is which is connected to
  8955. * DDI E. So just check whether this pipe is wired to DDI E and whether
  8956. * the PCH transcoder is on.
  8957. */
  8958. if (INTEL_GEN(dev_priv) < 9 &&
  8959. (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
  8960. pipe_config->has_pch_encoder = true;
  8961. tmp = I915_READ(FDI_RX_CTL(PIPE_A));
  8962. pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
  8963. FDI_DP_PORT_WIDTH_SHIFT) + 1;
  8964. ironlake_get_fdi_m_n_config(crtc, pipe_config);
  8965. }
  8966. }
  8967. static bool haswell_get_pipe_config(struct intel_crtc *crtc,
  8968. struct intel_crtc_state *pipe_config)
  8969. {
  8970. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  8971. enum intel_display_power_domain power_domain;
  8972. unsigned long power_domain_mask;
  8973. bool active;
  8974. power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
  8975. if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
  8976. return false;
  8977. power_domain_mask = BIT(power_domain);
  8978. pipe_config->shared_dpll = NULL;
  8979. active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_mask);
  8980. if (IS_GEN9_LP(dev_priv) &&
  8981. bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_mask)) {
  8982. WARN_ON(active);
  8983. active = true;
  8984. }
  8985. if (!active)
  8986. goto out;
  8987. if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  8988. haswell_get_ddi_port_state(crtc, pipe_config);
  8989. intel_get_pipe_timings(crtc, pipe_config);
  8990. }
  8991. intel_get_pipe_src_size(crtc, pipe_config);
  8992. pipe_config->gamma_mode =
  8993. I915_READ(GAMMA_MODE(crtc->pipe)) & GAMMA_MODE_MODE_MASK;
  8994. if (INTEL_GEN(dev_priv) >= 9) {
  8995. intel_crtc_init_scalers(crtc, pipe_config);
  8996. pipe_config->scaler_state.scaler_id = -1;
  8997. pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
  8998. }
  8999. power_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
  9000. if (intel_display_power_get_if_enabled(dev_priv, power_domain)) {
  9001. power_domain_mask |= BIT(power_domain);
  9002. if (INTEL_GEN(dev_priv) >= 9)
  9003. skylake_get_pfit_config(crtc, pipe_config);
  9004. else
  9005. ironlake_get_pfit_config(crtc, pipe_config);
  9006. }
  9007. if (IS_HASWELL(dev_priv))
  9008. pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
  9009. (I915_READ(IPS_CTL) & IPS_ENABLE);
  9010. if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
  9011. !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
  9012. pipe_config->pixel_multiplier =
  9013. I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
  9014. } else {
  9015. pipe_config->pixel_multiplier = 1;
  9016. }
  9017. out:
  9018. for_each_power_domain(power_domain, power_domain_mask)
  9019. intel_display_power_put(dev_priv, power_domain);
  9020. return active;
  9021. }
  9022. static void i845_update_cursor(struct drm_crtc *crtc, u32 base,
  9023. const struct intel_plane_state *plane_state)
  9024. {
  9025. struct drm_device *dev = crtc->dev;
  9026. struct drm_i915_private *dev_priv = to_i915(dev);
  9027. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9028. uint32_t cntl = 0, size = 0;
  9029. if (plane_state && plane_state->base.visible) {
  9030. unsigned int width = plane_state->base.crtc_w;
  9031. unsigned int height = plane_state->base.crtc_h;
  9032. unsigned int stride = roundup_pow_of_two(width) * 4;
  9033. switch (stride) {
  9034. default:
  9035. WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
  9036. width, stride);
  9037. stride = 256;
  9038. /* fallthrough */
  9039. case 256:
  9040. case 512:
  9041. case 1024:
  9042. case 2048:
  9043. break;
  9044. }
  9045. cntl |= CURSOR_ENABLE |
  9046. CURSOR_GAMMA_ENABLE |
  9047. CURSOR_FORMAT_ARGB |
  9048. CURSOR_STRIDE(stride);
  9049. size = (height << 12) | width;
  9050. }
  9051. if (intel_crtc->cursor_cntl != 0 &&
  9052. (intel_crtc->cursor_base != base ||
  9053. intel_crtc->cursor_size != size ||
  9054. intel_crtc->cursor_cntl != cntl)) {
  9055. /* On these chipsets we can only modify the base/size/stride
  9056. * whilst the cursor is disabled.
  9057. */
  9058. I915_WRITE(CURCNTR(PIPE_A), 0);
  9059. POSTING_READ(CURCNTR(PIPE_A));
  9060. intel_crtc->cursor_cntl = 0;
  9061. }
  9062. if (intel_crtc->cursor_base != base) {
  9063. I915_WRITE(CURBASE(PIPE_A), base);
  9064. intel_crtc->cursor_base = base;
  9065. }
  9066. if (intel_crtc->cursor_size != size) {
  9067. I915_WRITE(CURSIZE, size);
  9068. intel_crtc->cursor_size = size;
  9069. }
  9070. if (intel_crtc->cursor_cntl != cntl) {
  9071. I915_WRITE(CURCNTR(PIPE_A), cntl);
  9072. POSTING_READ(CURCNTR(PIPE_A));
  9073. intel_crtc->cursor_cntl = cntl;
  9074. }
  9075. }
  9076. static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base,
  9077. const struct intel_plane_state *plane_state)
  9078. {
  9079. struct drm_device *dev = crtc->dev;
  9080. struct drm_i915_private *dev_priv = to_i915(dev);
  9081. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9082. int pipe = intel_crtc->pipe;
  9083. uint32_t cntl = 0;
  9084. if (plane_state && plane_state->base.visible) {
  9085. cntl = MCURSOR_GAMMA_ENABLE;
  9086. switch (plane_state->base.crtc_w) {
  9087. case 64:
  9088. cntl |= CURSOR_MODE_64_ARGB_AX;
  9089. break;
  9090. case 128:
  9091. cntl |= CURSOR_MODE_128_ARGB_AX;
  9092. break;
  9093. case 256:
  9094. cntl |= CURSOR_MODE_256_ARGB_AX;
  9095. break;
  9096. default:
  9097. MISSING_CASE(plane_state->base.crtc_w);
  9098. return;
  9099. }
  9100. cntl |= pipe << 28; /* Connect to correct pipe */
  9101. if (HAS_DDI(dev_priv))
  9102. cntl |= CURSOR_PIPE_CSC_ENABLE;
  9103. if (plane_state->base.rotation & DRM_ROTATE_180)
  9104. cntl |= CURSOR_ROTATE_180;
  9105. }
  9106. if (intel_crtc->cursor_cntl != cntl) {
  9107. I915_WRITE(CURCNTR(pipe), cntl);
  9108. POSTING_READ(CURCNTR(pipe));
  9109. intel_crtc->cursor_cntl = cntl;
  9110. }
  9111. /* and commit changes on next vblank */
  9112. I915_WRITE(CURBASE(pipe), base);
  9113. POSTING_READ(CURBASE(pipe));
  9114. intel_crtc->cursor_base = base;
  9115. }
  9116. /* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
  9117. static void intel_crtc_update_cursor(struct drm_crtc *crtc,
  9118. const struct intel_plane_state *plane_state)
  9119. {
  9120. struct drm_device *dev = crtc->dev;
  9121. struct drm_i915_private *dev_priv = to_i915(dev);
  9122. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9123. int pipe = intel_crtc->pipe;
  9124. u32 base = intel_crtc->cursor_addr;
  9125. u32 pos = 0;
  9126. if (plane_state) {
  9127. int x = plane_state->base.crtc_x;
  9128. int y = plane_state->base.crtc_y;
  9129. if (x < 0) {
  9130. pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
  9131. x = -x;
  9132. }
  9133. pos |= x << CURSOR_X_SHIFT;
  9134. if (y < 0) {
  9135. pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
  9136. y = -y;
  9137. }
  9138. pos |= y << CURSOR_Y_SHIFT;
  9139. /* ILK+ do this automagically */
  9140. if (HAS_GMCH_DISPLAY(dev_priv) &&
  9141. plane_state->base.rotation & DRM_ROTATE_180) {
  9142. base += (plane_state->base.crtc_h *
  9143. plane_state->base.crtc_w - 1) * 4;
  9144. }
  9145. }
  9146. I915_WRITE(CURPOS(pipe), pos);
  9147. if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
  9148. i845_update_cursor(crtc, base, plane_state);
  9149. else
  9150. i9xx_update_cursor(crtc, base, plane_state);
  9151. }
  9152. static bool cursor_size_ok(struct drm_i915_private *dev_priv,
  9153. uint32_t width, uint32_t height)
  9154. {
  9155. if (width == 0 || height == 0)
  9156. return false;
  9157. /*
  9158. * 845g/865g are special in that they are only limited by
  9159. * the width of their cursors, the height is arbitrary up to
  9160. * the precision of the register. Everything else requires
  9161. * square cursors, limited to a few power-of-two sizes.
  9162. */
  9163. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  9164. if ((width & 63) != 0)
  9165. return false;
  9166. if (width > (IS_I845G(dev_priv) ? 64 : 512))
  9167. return false;
  9168. if (height > 1023)
  9169. return false;
  9170. } else {
  9171. switch (width | height) {
  9172. case 256:
  9173. case 128:
  9174. if (IS_GEN2(dev_priv))
  9175. return false;
  9176. case 64:
  9177. break;
  9178. default:
  9179. return false;
  9180. }
  9181. }
  9182. return true;
  9183. }
  9184. /* VESA 640x480x72Hz mode to set on the pipe */
  9185. static struct drm_display_mode load_detect_mode = {
  9186. DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
  9187. 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
  9188. };
  9189. struct drm_framebuffer *
  9190. __intel_framebuffer_create(struct drm_device *dev,
  9191. struct drm_mode_fb_cmd2 *mode_cmd,
  9192. struct drm_i915_gem_object *obj)
  9193. {
  9194. struct intel_framebuffer *intel_fb;
  9195. int ret;
  9196. intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
  9197. if (!intel_fb)
  9198. return ERR_PTR(-ENOMEM);
  9199. ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
  9200. if (ret)
  9201. goto err;
  9202. return &intel_fb->base;
  9203. err:
  9204. kfree(intel_fb);
  9205. return ERR_PTR(ret);
  9206. }
  9207. static struct drm_framebuffer *
  9208. intel_framebuffer_create(struct drm_device *dev,
  9209. struct drm_mode_fb_cmd2 *mode_cmd,
  9210. struct drm_i915_gem_object *obj)
  9211. {
  9212. struct drm_framebuffer *fb;
  9213. int ret;
  9214. ret = i915_mutex_lock_interruptible(dev);
  9215. if (ret)
  9216. return ERR_PTR(ret);
  9217. fb = __intel_framebuffer_create(dev, mode_cmd, obj);
  9218. mutex_unlock(&dev->struct_mutex);
  9219. return fb;
  9220. }
  9221. static u32
  9222. intel_framebuffer_pitch_for_width(int width, int bpp)
  9223. {
  9224. u32 pitch = DIV_ROUND_UP(width * bpp, 8);
  9225. return ALIGN(pitch, 64);
  9226. }
  9227. static u32
  9228. intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
  9229. {
  9230. u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
  9231. return PAGE_ALIGN(pitch * mode->vdisplay);
  9232. }
  9233. static struct drm_framebuffer *
  9234. intel_framebuffer_create_for_mode(struct drm_device *dev,
  9235. struct drm_display_mode *mode,
  9236. int depth, int bpp)
  9237. {
  9238. struct drm_framebuffer *fb;
  9239. struct drm_i915_gem_object *obj;
  9240. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  9241. obj = i915_gem_object_create(to_i915(dev),
  9242. intel_framebuffer_size_for_mode(mode, bpp));
  9243. if (IS_ERR(obj))
  9244. return ERR_CAST(obj);
  9245. mode_cmd.width = mode->hdisplay;
  9246. mode_cmd.height = mode->vdisplay;
  9247. mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
  9248. bpp);
  9249. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  9250. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  9251. if (IS_ERR(fb))
  9252. i915_gem_object_put(obj);
  9253. return fb;
  9254. }
  9255. static struct drm_framebuffer *
  9256. mode_fits_in_fbdev(struct drm_device *dev,
  9257. struct drm_display_mode *mode)
  9258. {
  9259. #ifdef CONFIG_DRM_FBDEV_EMULATION
  9260. struct drm_i915_private *dev_priv = to_i915(dev);
  9261. struct drm_i915_gem_object *obj;
  9262. struct drm_framebuffer *fb;
  9263. if (!dev_priv->fbdev)
  9264. return NULL;
  9265. if (!dev_priv->fbdev->fb)
  9266. return NULL;
  9267. obj = dev_priv->fbdev->fb->obj;
  9268. BUG_ON(!obj);
  9269. fb = &dev_priv->fbdev->fb->base;
  9270. if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
  9271. fb->format->cpp[0] * 8))
  9272. return NULL;
  9273. if (obj->base.size < mode->vdisplay * fb->pitches[0])
  9274. return NULL;
  9275. drm_framebuffer_reference(fb);
  9276. return fb;
  9277. #else
  9278. return NULL;
  9279. #endif
  9280. }
  9281. static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
  9282. struct drm_crtc *crtc,
  9283. struct drm_display_mode *mode,
  9284. struct drm_framebuffer *fb,
  9285. int x, int y)
  9286. {
  9287. struct drm_plane_state *plane_state;
  9288. int hdisplay, vdisplay;
  9289. int ret;
  9290. plane_state = drm_atomic_get_plane_state(state, crtc->primary);
  9291. if (IS_ERR(plane_state))
  9292. return PTR_ERR(plane_state);
  9293. if (mode)
  9294. drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
  9295. else
  9296. hdisplay = vdisplay = 0;
  9297. ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
  9298. if (ret)
  9299. return ret;
  9300. drm_atomic_set_fb_for_plane(plane_state, fb);
  9301. plane_state->crtc_x = 0;
  9302. plane_state->crtc_y = 0;
  9303. plane_state->crtc_w = hdisplay;
  9304. plane_state->crtc_h = vdisplay;
  9305. plane_state->src_x = x << 16;
  9306. plane_state->src_y = y << 16;
  9307. plane_state->src_w = hdisplay << 16;
  9308. plane_state->src_h = vdisplay << 16;
  9309. return 0;
  9310. }
  9311. bool intel_get_load_detect_pipe(struct drm_connector *connector,
  9312. struct drm_display_mode *mode,
  9313. struct intel_load_detect_pipe *old,
  9314. struct drm_modeset_acquire_ctx *ctx)
  9315. {
  9316. struct intel_crtc *intel_crtc;
  9317. struct intel_encoder *intel_encoder =
  9318. intel_attached_encoder(connector);
  9319. struct drm_crtc *possible_crtc;
  9320. struct drm_encoder *encoder = &intel_encoder->base;
  9321. struct drm_crtc *crtc = NULL;
  9322. struct drm_device *dev = encoder->dev;
  9323. struct drm_i915_private *dev_priv = to_i915(dev);
  9324. struct drm_framebuffer *fb;
  9325. struct drm_mode_config *config = &dev->mode_config;
  9326. struct drm_atomic_state *state = NULL, *restore_state = NULL;
  9327. struct drm_connector_state *connector_state;
  9328. struct intel_crtc_state *crtc_state;
  9329. int ret, i = -1;
  9330. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  9331. connector->base.id, connector->name,
  9332. encoder->base.id, encoder->name);
  9333. old->restore_state = NULL;
  9334. retry:
  9335. ret = drm_modeset_lock(&config->connection_mutex, ctx);
  9336. if (ret)
  9337. goto fail;
  9338. /*
  9339. * Algorithm gets a little messy:
  9340. *
  9341. * - if the connector already has an assigned crtc, use it (but make
  9342. * sure it's on first)
  9343. *
  9344. * - try to find the first unused crtc that can drive this connector,
  9345. * and use that if we find one
  9346. */
  9347. /* See if we already have a CRTC for this connector */
  9348. if (connector->state->crtc) {
  9349. crtc = connector->state->crtc;
  9350. ret = drm_modeset_lock(&crtc->mutex, ctx);
  9351. if (ret)
  9352. goto fail;
  9353. /* Make sure the crtc and connector are running */
  9354. goto found;
  9355. }
  9356. /* Find an unused one (if possible) */
  9357. for_each_crtc(dev, possible_crtc) {
  9358. i++;
  9359. if (!(encoder->possible_crtcs & (1 << i)))
  9360. continue;
  9361. ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
  9362. if (ret)
  9363. goto fail;
  9364. if (possible_crtc->state->enable) {
  9365. drm_modeset_unlock(&possible_crtc->mutex);
  9366. continue;
  9367. }
  9368. crtc = possible_crtc;
  9369. break;
  9370. }
  9371. /*
  9372. * If we didn't find an unused CRTC, don't use any.
  9373. */
  9374. if (!crtc) {
  9375. DRM_DEBUG_KMS("no pipe available for load-detect\n");
  9376. goto fail;
  9377. }
  9378. found:
  9379. intel_crtc = to_intel_crtc(crtc);
  9380. ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
  9381. if (ret)
  9382. goto fail;
  9383. state = drm_atomic_state_alloc(dev);
  9384. restore_state = drm_atomic_state_alloc(dev);
  9385. if (!state || !restore_state) {
  9386. ret = -ENOMEM;
  9387. goto fail;
  9388. }
  9389. state->acquire_ctx = ctx;
  9390. restore_state->acquire_ctx = ctx;
  9391. connector_state = drm_atomic_get_connector_state(state, connector);
  9392. if (IS_ERR(connector_state)) {
  9393. ret = PTR_ERR(connector_state);
  9394. goto fail;
  9395. }
  9396. ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
  9397. if (ret)
  9398. goto fail;
  9399. crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
  9400. if (IS_ERR(crtc_state)) {
  9401. ret = PTR_ERR(crtc_state);
  9402. goto fail;
  9403. }
  9404. crtc_state->base.active = crtc_state->base.enable = true;
  9405. if (!mode)
  9406. mode = &load_detect_mode;
  9407. /* We need a framebuffer large enough to accommodate all accesses
  9408. * that the plane may generate whilst we perform load detection.
  9409. * We can not rely on the fbcon either being present (we get called
  9410. * during its initialisation to detect all boot displays, or it may
  9411. * not even exist) or that it is large enough to satisfy the
  9412. * requested mode.
  9413. */
  9414. fb = mode_fits_in_fbdev(dev, mode);
  9415. if (fb == NULL) {
  9416. DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
  9417. fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
  9418. } else
  9419. DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
  9420. if (IS_ERR(fb)) {
  9421. DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
  9422. goto fail;
  9423. }
  9424. ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
  9425. if (ret)
  9426. goto fail;
  9427. drm_framebuffer_unreference(fb);
  9428. ret = drm_atomic_set_mode_for_crtc(&crtc_state->base, mode);
  9429. if (ret)
  9430. goto fail;
  9431. ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
  9432. if (!ret)
  9433. ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
  9434. if (!ret)
  9435. ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(restore_state, crtc->primary));
  9436. if (ret) {
  9437. DRM_DEBUG_KMS("Failed to create a copy of old state to restore: %i\n", ret);
  9438. goto fail;
  9439. }
  9440. ret = drm_atomic_commit(state);
  9441. if (ret) {
  9442. DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
  9443. goto fail;
  9444. }
  9445. old->restore_state = restore_state;
  9446. drm_atomic_state_put(state);
  9447. /* let the connector get through one full cycle before testing */
  9448. intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
  9449. return true;
  9450. fail:
  9451. if (state) {
  9452. drm_atomic_state_put(state);
  9453. state = NULL;
  9454. }
  9455. if (restore_state) {
  9456. drm_atomic_state_put(restore_state);
  9457. restore_state = NULL;
  9458. }
  9459. if (ret == -EDEADLK) {
  9460. drm_modeset_backoff(ctx);
  9461. goto retry;
  9462. }
  9463. return false;
  9464. }
  9465. void intel_release_load_detect_pipe(struct drm_connector *connector,
  9466. struct intel_load_detect_pipe *old,
  9467. struct drm_modeset_acquire_ctx *ctx)
  9468. {
  9469. struct intel_encoder *intel_encoder =
  9470. intel_attached_encoder(connector);
  9471. struct drm_encoder *encoder = &intel_encoder->base;
  9472. struct drm_atomic_state *state = old->restore_state;
  9473. int ret;
  9474. DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
  9475. connector->base.id, connector->name,
  9476. encoder->base.id, encoder->name);
  9477. if (!state)
  9478. return;
  9479. ret = drm_atomic_commit(state);
  9480. if (ret)
  9481. DRM_DEBUG_KMS("Couldn't release load detect pipe: %i\n", ret);
  9482. drm_atomic_state_put(state);
  9483. }
  9484. static int i9xx_pll_refclk(struct drm_device *dev,
  9485. const struct intel_crtc_state *pipe_config)
  9486. {
  9487. struct drm_i915_private *dev_priv = to_i915(dev);
  9488. u32 dpll = pipe_config->dpll_hw_state.dpll;
  9489. if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
  9490. return dev_priv->vbt.lvds_ssc_freq;
  9491. else if (HAS_PCH_SPLIT(dev_priv))
  9492. return 120000;
  9493. else if (!IS_GEN2(dev_priv))
  9494. return 96000;
  9495. else
  9496. return 48000;
  9497. }
  9498. /* Returns the clock of the currently programmed mode of the given pipe. */
  9499. static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
  9500. struct intel_crtc_state *pipe_config)
  9501. {
  9502. struct drm_device *dev = crtc->base.dev;
  9503. struct drm_i915_private *dev_priv = to_i915(dev);
  9504. int pipe = pipe_config->cpu_transcoder;
  9505. u32 dpll = pipe_config->dpll_hw_state.dpll;
  9506. u32 fp;
  9507. struct dpll clock;
  9508. int port_clock;
  9509. int refclk = i9xx_pll_refclk(dev, pipe_config);
  9510. if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
  9511. fp = pipe_config->dpll_hw_state.fp0;
  9512. else
  9513. fp = pipe_config->dpll_hw_state.fp1;
  9514. clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
  9515. if (IS_PINEVIEW(dev_priv)) {
  9516. clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
  9517. clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
  9518. } else {
  9519. clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
  9520. clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
  9521. }
  9522. if (!IS_GEN2(dev_priv)) {
  9523. if (IS_PINEVIEW(dev_priv))
  9524. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
  9525. DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
  9526. else
  9527. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
  9528. DPLL_FPA01_P1_POST_DIV_SHIFT);
  9529. switch (dpll & DPLL_MODE_MASK) {
  9530. case DPLLB_MODE_DAC_SERIAL:
  9531. clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
  9532. 5 : 10;
  9533. break;
  9534. case DPLLB_MODE_LVDS:
  9535. clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
  9536. 7 : 14;
  9537. break;
  9538. default:
  9539. DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
  9540. "mode\n", (int)(dpll & DPLL_MODE_MASK));
  9541. return;
  9542. }
  9543. if (IS_PINEVIEW(dev_priv))
  9544. port_clock = pnv_calc_dpll_params(refclk, &clock);
  9545. else
  9546. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  9547. } else {
  9548. u32 lvds = IS_I830(dev_priv) ? 0 : I915_READ(LVDS);
  9549. bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
  9550. if (is_lvds) {
  9551. clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
  9552. DPLL_FPA01_P1_POST_DIV_SHIFT);
  9553. if (lvds & LVDS_CLKB_POWER_UP)
  9554. clock.p2 = 7;
  9555. else
  9556. clock.p2 = 14;
  9557. } else {
  9558. if (dpll & PLL_P1_DIVIDE_BY_TWO)
  9559. clock.p1 = 2;
  9560. else {
  9561. clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
  9562. DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
  9563. }
  9564. if (dpll & PLL_P2_DIVIDE_BY_4)
  9565. clock.p2 = 4;
  9566. else
  9567. clock.p2 = 2;
  9568. }
  9569. port_clock = i9xx_calc_dpll_params(refclk, &clock);
  9570. }
  9571. /*
  9572. * This value includes pixel_multiplier. We will use
  9573. * port_clock to compute adjusted_mode.crtc_clock in the
  9574. * encoder's get_config() function.
  9575. */
  9576. pipe_config->port_clock = port_clock;
  9577. }
  9578. int intel_dotclock_calculate(int link_freq,
  9579. const struct intel_link_m_n *m_n)
  9580. {
  9581. /*
  9582. * The calculation for the data clock is:
  9583. * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
  9584. * But we want to avoid losing precison if possible, so:
  9585. * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
  9586. *
  9587. * and the link clock is simpler:
  9588. * link_clock = (m * link_clock) / n
  9589. */
  9590. if (!m_n->link_n)
  9591. return 0;
  9592. return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
  9593. }
  9594. static void ironlake_pch_clock_get(struct intel_crtc *crtc,
  9595. struct intel_crtc_state *pipe_config)
  9596. {
  9597. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  9598. /* read out port_clock from the DPLL */
  9599. i9xx_crtc_clock_get(crtc, pipe_config);
  9600. /*
  9601. * In case there is an active pipe without active ports,
  9602. * we may need some idea for the dotclock anyway.
  9603. * Calculate one based on the FDI configuration.
  9604. */
  9605. pipe_config->base.adjusted_mode.crtc_clock =
  9606. intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  9607. &pipe_config->fdi_m_n);
  9608. }
  9609. /** Returns the currently programmed mode of the given pipe. */
  9610. struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
  9611. struct drm_crtc *crtc)
  9612. {
  9613. struct drm_i915_private *dev_priv = to_i915(dev);
  9614. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9615. enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
  9616. struct drm_display_mode *mode;
  9617. struct intel_crtc_state *pipe_config;
  9618. int htot = I915_READ(HTOTAL(cpu_transcoder));
  9619. int hsync = I915_READ(HSYNC(cpu_transcoder));
  9620. int vtot = I915_READ(VTOTAL(cpu_transcoder));
  9621. int vsync = I915_READ(VSYNC(cpu_transcoder));
  9622. enum pipe pipe = intel_crtc->pipe;
  9623. mode = kzalloc(sizeof(*mode), GFP_KERNEL);
  9624. if (!mode)
  9625. return NULL;
  9626. pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
  9627. if (!pipe_config) {
  9628. kfree(mode);
  9629. return NULL;
  9630. }
  9631. /*
  9632. * Construct a pipe_config sufficient for getting the clock info
  9633. * back out of crtc_clock_get.
  9634. *
  9635. * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
  9636. * to use a real value here instead.
  9637. */
  9638. pipe_config->cpu_transcoder = (enum transcoder) pipe;
  9639. pipe_config->pixel_multiplier = 1;
  9640. pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(pipe));
  9641. pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(pipe));
  9642. pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(pipe));
  9643. i9xx_crtc_clock_get(intel_crtc, pipe_config);
  9644. mode->clock = pipe_config->port_clock / pipe_config->pixel_multiplier;
  9645. mode->hdisplay = (htot & 0xffff) + 1;
  9646. mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
  9647. mode->hsync_start = (hsync & 0xffff) + 1;
  9648. mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
  9649. mode->vdisplay = (vtot & 0xffff) + 1;
  9650. mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
  9651. mode->vsync_start = (vsync & 0xffff) + 1;
  9652. mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
  9653. drm_mode_set_name(mode);
  9654. kfree(pipe_config);
  9655. return mode;
  9656. }
  9657. static void intel_crtc_destroy(struct drm_crtc *crtc)
  9658. {
  9659. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9660. struct drm_device *dev = crtc->dev;
  9661. struct intel_flip_work *work;
  9662. spin_lock_irq(&dev->event_lock);
  9663. work = intel_crtc->flip_work;
  9664. intel_crtc->flip_work = NULL;
  9665. spin_unlock_irq(&dev->event_lock);
  9666. if (work) {
  9667. cancel_work_sync(&work->mmio_work);
  9668. cancel_work_sync(&work->unpin_work);
  9669. kfree(work);
  9670. }
  9671. drm_crtc_cleanup(crtc);
  9672. kfree(intel_crtc);
  9673. }
  9674. static void intel_unpin_work_fn(struct work_struct *__work)
  9675. {
  9676. struct intel_flip_work *work =
  9677. container_of(__work, struct intel_flip_work, unpin_work);
  9678. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  9679. struct drm_device *dev = crtc->base.dev;
  9680. struct drm_plane *primary = crtc->base.primary;
  9681. if (is_mmio_work(work))
  9682. flush_work(&work->mmio_work);
  9683. mutex_lock(&dev->struct_mutex);
  9684. intel_unpin_fb_vma(work->old_vma);
  9685. i915_gem_object_put(work->pending_flip_obj);
  9686. mutex_unlock(&dev->struct_mutex);
  9687. i915_gem_request_put(work->flip_queued_req);
  9688. intel_frontbuffer_flip_complete(to_i915(dev),
  9689. to_intel_plane(primary)->frontbuffer_bit);
  9690. intel_fbc_post_update(crtc);
  9691. drm_framebuffer_unreference(work->old_fb);
  9692. BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
  9693. atomic_dec(&crtc->unpin_work_count);
  9694. kfree(work);
  9695. }
  9696. /* Is 'a' after or equal to 'b'? */
  9697. static bool g4x_flip_count_after_eq(u32 a, u32 b)
  9698. {
  9699. return !((a - b) & 0x80000000);
  9700. }
  9701. static bool __pageflip_finished_cs(struct intel_crtc *crtc,
  9702. struct intel_flip_work *work)
  9703. {
  9704. struct drm_device *dev = crtc->base.dev;
  9705. struct drm_i915_private *dev_priv = to_i915(dev);
  9706. if (abort_flip_on_reset(crtc))
  9707. return true;
  9708. /*
  9709. * The relevant registers doen't exist on pre-ctg.
  9710. * As the flip done interrupt doesn't trigger for mmio
  9711. * flips on gmch platforms, a flip count check isn't
  9712. * really needed there. But since ctg has the registers,
  9713. * include it in the check anyway.
  9714. */
  9715. if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
  9716. return true;
  9717. /*
  9718. * BDW signals flip done immediately if the plane
  9719. * is disabled, even if the plane enable is already
  9720. * armed to occur at the next vblank :(
  9721. */
  9722. /*
  9723. * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
  9724. * used the same base address. In that case the mmio flip might
  9725. * have completed, but the CS hasn't even executed the flip yet.
  9726. *
  9727. * A flip count check isn't enough as the CS might have updated
  9728. * the base address just after start of vblank, but before we
  9729. * managed to process the interrupt. This means we'd complete the
  9730. * CS flip too soon.
  9731. *
  9732. * Combining both checks should get us a good enough result. It may
  9733. * still happen that the CS flip has been executed, but has not
  9734. * yet actually completed. But in case the base address is the same
  9735. * anyway, we don't really care.
  9736. */
  9737. return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
  9738. crtc->flip_work->gtt_offset &&
  9739. g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_G4X(crtc->pipe)),
  9740. crtc->flip_work->flip_count);
  9741. }
  9742. static bool
  9743. __pageflip_finished_mmio(struct intel_crtc *crtc,
  9744. struct intel_flip_work *work)
  9745. {
  9746. /*
  9747. * MMIO work completes when vblank is different from
  9748. * flip_queued_vblank.
  9749. *
  9750. * Reset counter value doesn't matter, this is handled by
  9751. * i915_wait_request finishing early, so no need to handle
  9752. * reset here.
  9753. */
  9754. return intel_crtc_get_vblank_counter(crtc) != work->flip_queued_vblank;
  9755. }
  9756. static bool pageflip_finished(struct intel_crtc *crtc,
  9757. struct intel_flip_work *work)
  9758. {
  9759. if (!atomic_read(&work->pending))
  9760. return false;
  9761. smp_rmb();
  9762. if (is_mmio_work(work))
  9763. return __pageflip_finished_mmio(crtc, work);
  9764. else
  9765. return __pageflip_finished_cs(crtc, work);
  9766. }
  9767. void intel_finish_page_flip_cs(struct drm_i915_private *dev_priv, int pipe)
  9768. {
  9769. struct drm_device *dev = &dev_priv->drm;
  9770. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  9771. struct intel_flip_work *work;
  9772. unsigned long flags;
  9773. /* Ignore early vblank irqs */
  9774. if (!crtc)
  9775. return;
  9776. /*
  9777. * This is called both by irq handlers and the reset code (to complete
  9778. * lost pageflips) so needs the full irqsave spinlocks.
  9779. */
  9780. spin_lock_irqsave(&dev->event_lock, flags);
  9781. work = crtc->flip_work;
  9782. if (work != NULL &&
  9783. !is_mmio_work(work) &&
  9784. pageflip_finished(crtc, work))
  9785. page_flip_completed(crtc);
  9786. spin_unlock_irqrestore(&dev->event_lock, flags);
  9787. }
  9788. void intel_finish_page_flip_mmio(struct drm_i915_private *dev_priv, int pipe)
  9789. {
  9790. struct drm_device *dev = &dev_priv->drm;
  9791. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  9792. struct intel_flip_work *work;
  9793. unsigned long flags;
  9794. /* Ignore early vblank irqs */
  9795. if (!crtc)
  9796. return;
  9797. /*
  9798. * This is called both by irq handlers and the reset code (to complete
  9799. * lost pageflips) so needs the full irqsave spinlocks.
  9800. */
  9801. spin_lock_irqsave(&dev->event_lock, flags);
  9802. work = crtc->flip_work;
  9803. if (work != NULL &&
  9804. is_mmio_work(work) &&
  9805. pageflip_finished(crtc, work))
  9806. page_flip_completed(crtc);
  9807. spin_unlock_irqrestore(&dev->event_lock, flags);
  9808. }
  9809. static inline void intel_mark_page_flip_active(struct intel_crtc *crtc,
  9810. struct intel_flip_work *work)
  9811. {
  9812. work->flip_queued_vblank = intel_crtc_get_vblank_counter(crtc);
  9813. /* Ensure that the work item is consistent when activating it ... */
  9814. smp_mb__before_atomic();
  9815. atomic_set(&work->pending, 1);
  9816. }
  9817. static int intel_gen2_queue_flip(struct drm_device *dev,
  9818. struct drm_crtc *crtc,
  9819. struct drm_framebuffer *fb,
  9820. struct drm_i915_gem_object *obj,
  9821. struct drm_i915_gem_request *req,
  9822. uint32_t flags)
  9823. {
  9824. struct intel_ring *ring = req->ring;
  9825. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9826. u32 flip_mask;
  9827. int ret;
  9828. ret = intel_ring_begin(req, 6);
  9829. if (ret)
  9830. return ret;
  9831. /* Can't queue multiple flips, so wait for the previous
  9832. * one to finish before executing the next.
  9833. */
  9834. if (intel_crtc->plane)
  9835. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9836. else
  9837. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9838. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9839. intel_ring_emit(ring, MI_NOOP);
  9840. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9841. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9842. intel_ring_emit(ring, fb->pitches[0]);
  9843. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  9844. intel_ring_emit(ring, 0); /* aux display base address, unused */
  9845. return 0;
  9846. }
  9847. static int intel_gen3_queue_flip(struct drm_device *dev,
  9848. struct drm_crtc *crtc,
  9849. struct drm_framebuffer *fb,
  9850. struct drm_i915_gem_object *obj,
  9851. struct drm_i915_gem_request *req,
  9852. uint32_t flags)
  9853. {
  9854. struct intel_ring *ring = req->ring;
  9855. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9856. u32 flip_mask;
  9857. int ret;
  9858. ret = intel_ring_begin(req, 6);
  9859. if (ret)
  9860. return ret;
  9861. if (intel_crtc->plane)
  9862. flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  9863. else
  9864. flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  9865. intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  9866. intel_ring_emit(ring, MI_NOOP);
  9867. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
  9868. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9869. intel_ring_emit(ring, fb->pitches[0]);
  9870. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  9871. intel_ring_emit(ring, MI_NOOP);
  9872. return 0;
  9873. }
  9874. static int intel_gen4_queue_flip(struct drm_device *dev,
  9875. struct drm_crtc *crtc,
  9876. struct drm_framebuffer *fb,
  9877. struct drm_i915_gem_object *obj,
  9878. struct drm_i915_gem_request *req,
  9879. uint32_t flags)
  9880. {
  9881. struct intel_ring *ring = req->ring;
  9882. struct drm_i915_private *dev_priv = to_i915(dev);
  9883. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9884. uint32_t pf, pipesrc;
  9885. int ret;
  9886. ret = intel_ring_begin(req, 4);
  9887. if (ret)
  9888. return ret;
  9889. /* i965+ uses the linear or tiled offsets from the
  9890. * Display Registers (which do not change across a page-flip)
  9891. * so we need only reprogram the base address.
  9892. */
  9893. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9894. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9895. intel_ring_emit(ring, fb->pitches[0]);
  9896. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset |
  9897. intel_fb_modifier_to_tiling(fb->modifier));
  9898. /* XXX Enabling the panel-fitter across page-flip is so far
  9899. * untested on non-native modes, so ignore it for now.
  9900. * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
  9901. */
  9902. pf = 0;
  9903. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9904. intel_ring_emit(ring, pf | pipesrc);
  9905. return 0;
  9906. }
  9907. static int intel_gen6_queue_flip(struct drm_device *dev,
  9908. struct drm_crtc *crtc,
  9909. struct drm_framebuffer *fb,
  9910. struct drm_i915_gem_object *obj,
  9911. struct drm_i915_gem_request *req,
  9912. uint32_t flags)
  9913. {
  9914. struct intel_ring *ring = req->ring;
  9915. struct drm_i915_private *dev_priv = to_i915(dev);
  9916. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9917. uint32_t pf, pipesrc;
  9918. int ret;
  9919. ret = intel_ring_begin(req, 4);
  9920. if (ret)
  9921. return ret;
  9922. intel_ring_emit(ring, MI_DISPLAY_FLIP |
  9923. MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
  9924. intel_ring_emit(ring, fb->pitches[0] |
  9925. intel_fb_modifier_to_tiling(fb->modifier));
  9926. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  9927. /* Contrary to the suggestions in the documentation,
  9928. * "Enable Panel Fitter" does not seem to be required when page
  9929. * flipping with a non-native mode, and worse causes a normal
  9930. * modeset to fail.
  9931. * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
  9932. */
  9933. pf = 0;
  9934. pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
  9935. intel_ring_emit(ring, pf | pipesrc);
  9936. return 0;
  9937. }
  9938. static int intel_gen7_queue_flip(struct drm_device *dev,
  9939. struct drm_crtc *crtc,
  9940. struct drm_framebuffer *fb,
  9941. struct drm_i915_gem_object *obj,
  9942. struct drm_i915_gem_request *req,
  9943. uint32_t flags)
  9944. {
  9945. struct drm_i915_private *dev_priv = to_i915(dev);
  9946. struct intel_ring *ring = req->ring;
  9947. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  9948. uint32_t plane_bit = 0;
  9949. int len, ret;
  9950. switch (intel_crtc->plane) {
  9951. case PLANE_A:
  9952. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
  9953. break;
  9954. case PLANE_B:
  9955. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
  9956. break;
  9957. case PLANE_C:
  9958. plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
  9959. break;
  9960. default:
  9961. WARN_ONCE(1, "unknown plane in flip command\n");
  9962. return -ENODEV;
  9963. }
  9964. len = 4;
  9965. if (req->engine->id == RCS) {
  9966. len += 6;
  9967. /*
  9968. * On Gen 8, SRM is now taking an extra dword to accommodate
  9969. * 48bits addresses, and we need a NOOP for the batch size to
  9970. * stay even.
  9971. */
  9972. if (IS_GEN8(dev_priv))
  9973. len += 2;
  9974. }
  9975. /*
  9976. * BSpec MI_DISPLAY_FLIP for IVB:
  9977. * "The full packet must be contained within the same cache line."
  9978. *
  9979. * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
  9980. * cacheline, if we ever start emitting more commands before
  9981. * the MI_DISPLAY_FLIP we may need to first emit everything else,
  9982. * then do the cacheline alignment, and finally emit the
  9983. * MI_DISPLAY_FLIP.
  9984. */
  9985. ret = intel_ring_cacheline_align(req);
  9986. if (ret)
  9987. return ret;
  9988. ret = intel_ring_begin(req, len);
  9989. if (ret)
  9990. return ret;
  9991. /* Unmask the flip-done completion message. Note that the bspec says that
  9992. * we should do this for both the BCS and RCS, and that we must not unmask
  9993. * more than one flip event at any time (or ensure that one flip message
  9994. * can be sent by waiting for flip-done prior to queueing new flips).
  9995. * Experimentation says that BCS works despite DERRMR masking all
  9996. * flip-done completion events and that unmasking all planes at once
  9997. * for the RCS also doesn't appear to drop events. Setting the DERRMR
  9998. * to zero does lead to lockups within MI_DISPLAY_FLIP.
  9999. */
  10000. if (req->engine->id == RCS) {
  10001. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  10002. intel_ring_emit_reg(ring, DERRMR);
  10003. intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
  10004. DERRMR_PIPEB_PRI_FLIP_DONE |
  10005. DERRMR_PIPEC_PRI_FLIP_DONE));
  10006. if (IS_GEN8(dev_priv))
  10007. intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8 |
  10008. MI_SRM_LRM_GLOBAL_GTT);
  10009. else
  10010. intel_ring_emit(ring, MI_STORE_REGISTER_MEM |
  10011. MI_SRM_LRM_GLOBAL_GTT);
  10012. intel_ring_emit_reg(ring, DERRMR);
  10013. intel_ring_emit(ring,
  10014. i915_ggtt_offset(req->engine->scratch) + 256);
  10015. if (IS_GEN8(dev_priv)) {
  10016. intel_ring_emit(ring, 0);
  10017. intel_ring_emit(ring, MI_NOOP);
  10018. }
  10019. }
  10020. intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
  10021. intel_ring_emit(ring, fb->pitches[0] |
  10022. intel_fb_modifier_to_tiling(fb->modifier));
  10023. intel_ring_emit(ring, intel_crtc->flip_work->gtt_offset);
  10024. intel_ring_emit(ring, (MI_NOOP));
  10025. return 0;
  10026. }
  10027. static bool use_mmio_flip(struct intel_engine_cs *engine,
  10028. struct drm_i915_gem_object *obj)
  10029. {
  10030. /*
  10031. * This is not being used for older platforms, because
  10032. * non-availability of flip done interrupt forces us to use
  10033. * CS flips. Older platforms derive flip done using some clever
  10034. * tricks involving the flip_pending status bits and vblank irqs.
  10035. * So using MMIO flips there would disrupt this mechanism.
  10036. */
  10037. if (engine == NULL)
  10038. return true;
  10039. if (INTEL_GEN(engine->i915) < 5)
  10040. return false;
  10041. if (i915.use_mmio_flip < 0)
  10042. return false;
  10043. else if (i915.use_mmio_flip > 0)
  10044. return true;
  10045. else if (i915.enable_execlists)
  10046. return true;
  10047. return engine != i915_gem_object_last_write_engine(obj);
  10048. }
  10049. static void skl_do_mmio_flip(struct intel_crtc *intel_crtc,
  10050. unsigned int rotation,
  10051. struct intel_flip_work *work)
  10052. {
  10053. struct drm_device *dev = intel_crtc->base.dev;
  10054. struct drm_i915_private *dev_priv = to_i915(dev);
  10055. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  10056. const enum pipe pipe = intel_crtc->pipe;
  10057. u32 ctl, stride = skl_plane_stride(fb, 0, rotation);
  10058. ctl = I915_READ(PLANE_CTL(pipe, 0));
  10059. ctl &= ~PLANE_CTL_TILED_MASK;
  10060. switch (fb->modifier) {
  10061. case DRM_FORMAT_MOD_NONE:
  10062. break;
  10063. case I915_FORMAT_MOD_X_TILED:
  10064. ctl |= PLANE_CTL_TILED_X;
  10065. break;
  10066. case I915_FORMAT_MOD_Y_TILED:
  10067. ctl |= PLANE_CTL_TILED_Y;
  10068. break;
  10069. case I915_FORMAT_MOD_Yf_TILED:
  10070. ctl |= PLANE_CTL_TILED_YF;
  10071. break;
  10072. default:
  10073. MISSING_CASE(fb->modifier);
  10074. }
  10075. /*
  10076. * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
  10077. * PLANE_SURF updates, the update is then guaranteed to be atomic.
  10078. */
  10079. I915_WRITE(PLANE_CTL(pipe, 0), ctl);
  10080. I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
  10081. I915_WRITE(PLANE_SURF(pipe, 0), work->gtt_offset);
  10082. POSTING_READ(PLANE_SURF(pipe, 0));
  10083. }
  10084. static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc,
  10085. struct intel_flip_work *work)
  10086. {
  10087. struct drm_device *dev = intel_crtc->base.dev;
  10088. struct drm_i915_private *dev_priv = to_i915(dev);
  10089. struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
  10090. i915_reg_t reg = DSPCNTR(intel_crtc->plane);
  10091. u32 dspcntr;
  10092. dspcntr = I915_READ(reg);
  10093. if (fb->modifier == I915_FORMAT_MOD_X_TILED)
  10094. dspcntr |= DISPPLANE_TILED;
  10095. else
  10096. dspcntr &= ~DISPPLANE_TILED;
  10097. I915_WRITE(reg, dspcntr);
  10098. I915_WRITE(DSPSURF(intel_crtc->plane), work->gtt_offset);
  10099. POSTING_READ(DSPSURF(intel_crtc->plane));
  10100. }
  10101. static void intel_mmio_flip_work_func(struct work_struct *w)
  10102. {
  10103. struct intel_flip_work *work =
  10104. container_of(w, struct intel_flip_work, mmio_work);
  10105. struct intel_crtc *crtc = to_intel_crtc(work->crtc);
  10106. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  10107. struct intel_framebuffer *intel_fb =
  10108. to_intel_framebuffer(crtc->base.primary->fb);
  10109. struct drm_i915_gem_object *obj = intel_fb->obj;
  10110. WARN_ON(i915_gem_object_wait(obj, 0, MAX_SCHEDULE_TIMEOUT, NULL) < 0);
  10111. intel_pipe_update_start(crtc);
  10112. if (INTEL_GEN(dev_priv) >= 9)
  10113. skl_do_mmio_flip(crtc, work->rotation, work);
  10114. else
  10115. /* use_mmio_flip() retricts MMIO flips to ilk+ */
  10116. ilk_do_mmio_flip(crtc, work);
  10117. intel_pipe_update_end(crtc, work);
  10118. }
  10119. static int intel_default_queue_flip(struct drm_device *dev,
  10120. struct drm_crtc *crtc,
  10121. struct drm_framebuffer *fb,
  10122. struct drm_i915_gem_object *obj,
  10123. struct drm_i915_gem_request *req,
  10124. uint32_t flags)
  10125. {
  10126. return -ENODEV;
  10127. }
  10128. static bool __pageflip_stall_check_cs(struct drm_i915_private *dev_priv,
  10129. struct intel_crtc *intel_crtc,
  10130. struct intel_flip_work *work)
  10131. {
  10132. u32 addr, vblank;
  10133. if (!atomic_read(&work->pending))
  10134. return false;
  10135. smp_rmb();
  10136. vblank = intel_crtc_get_vblank_counter(intel_crtc);
  10137. if (work->flip_ready_vblank == 0) {
  10138. if (work->flip_queued_req &&
  10139. !i915_gem_request_completed(work->flip_queued_req))
  10140. return false;
  10141. work->flip_ready_vblank = vblank;
  10142. }
  10143. if (vblank - work->flip_ready_vblank < 3)
  10144. return false;
  10145. /* Potential stall - if we see that the flip has happened,
  10146. * assume a missed interrupt. */
  10147. if (INTEL_GEN(dev_priv) >= 4)
  10148. addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
  10149. else
  10150. addr = I915_READ(DSPADDR(intel_crtc->plane));
  10151. /* There is a potential issue here with a false positive after a flip
  10152. * to the same address. We could address this by checking for a
  10153. * non-incrementing frame counter.
  10154. */
  10155. return addr == work->gtt_offset;
  10156. }
  10157. void intel_check_page_flip(struct drm_i915_private *dev_priv, int pipe)
  10158. {
  10159. struct drm_device *dev = &dev_priv->drm;
  10160. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  10161. struct intel_flip_work *work;
  10162. WARN_ON(!in_interrupt());
  10163. if (crtc == NULL)
  10164. return;
  10165. spin_lock(&dev->event_lock);
  10166. work = crtc->flip_work;
  10167. if (work != NULL && !is_mmio_work(work) &&
  10168. __pageflip_stall_check_cs(dev_priv, crtc, work)) {
  10169. WARN_ONCE(1,
  10170. "Kicking stuck page flip: queued at %d, now %d\n",
  10171. work->flip_queued_vblank, intel_crtc_get_vblank_counter(crtc));
  10172. page_flip_completed(crtc);
  10173. work = NULL;
  10174. }
  10175. if (work != NULL && !is_mmio_work(work) &&
  10176. intel_crtc_get_vblank_counter(crtc) - work->flip_queued_vblank > 1)
  10177. intel_queue_rps_boost_for_request(work->flip_queued_req);
  10178. spin_unlock(&dev->event_lock);
  10179. }
  10180. static int intel_crtc_page_flip(struct drm_crtc *crtc,
  10181. struct drm_framebuffer *fb,
  10182. struct drm_pending_vblank_event *event,
  10183. uint32_t page_flip_flags)
  10184. {
  10185. struct drm_device *dev = crtc->dev;
  10186. struct drm_i915_private *dev_priv = to_i915(dev);
  10187. struct drm_framebuffer *old_fb = crtc->primary->fb;
  10188. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  10189. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10190. struct drm_plane *primary = crtc->primary;
  10191. enum pipe pipe = intel_crtc->pipe;
  10192. struct intel_flip_work *work;
  10193. struct intel_engine_cs *engine;
  10194. bool mmio_flip;
  10195. struct drm_i915_gem_request *request;
  10196. struct i915_vma *vma;
  10197. int ret;
  10198. /*
  10199. * drm_mode_page_flip_ioctl() should already catch this, but double
  10200. * check to be safe. In the future we may enable pageflipping from
  10201. * a disabled primary plane.
  10202. */
  10203. if (WARN_ON(intel_fb_obj(old_fb) == NULL))
  10204. return -EBUSY;
  10205. /* Can't change pixel format via MI display flips. */
  10206. if (fb->format != crtc->primary->fb->format)
  10207. return -EINVAL;
  10208. /*
  10209. * TILEOFF/LINOFF registers can't be changed via MI display flips.
  10210. * Note that pitch changes could also affect these register.
  10211. */
  10212. if (INTEL_GEN(dev_priv) > 3 &&
  10213. (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
  10214. fb->pitches[0] != crtc->primary->fb->pitches[0]))
  10215. return -EINVAL;
  10216. if (i915_terminally_wedged(&dev_priv->gpu_error))
  10217. goto out_hang;
  10218. work = kzalloc(sizeof(*work), GFP_KERNEL);
  10219. if (work == NULL)
  10220. return -ENOMEM;
  10221. work->event = event;
  10222. work->crtc = crtc;
  10223. work->old_fb = old_fb;
  10224. INIT_WORK(&work->unpin_work, intel_unpin_work_fn);
  10225. ret = drm_crtc_vblank_get(crtc);
  10226. if (ret)
  10227. goto free_work;
  10228. /* We borrow the event spin lock for protecting flip_work */
  10229. spin_lock_irq(&dev->event_lock);
  10230. if (intel_crtc->flip_work) {
  10231. /* Before declaring the flip queue wedged, check if
  10232. * the hardware completed the operation behind our backs.
  10233. */
  10234. if (pageflip_finished(intel_crtc, intel_crtc->flip_work)) {
  10235. DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
  10236. page_flip_completed(intel_crtc);
  10237. } else {
  10238. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  10239. spin_unlock_irq(&dev->event_lock);
  10240. drm_crtc_vblank_put(crtc);
  10241. kfree(work);
  10242. return -EBUSY;
  10243. }
  10244. }
  10245. intel_crtc->flip_work = work;
  10246. spin_unlock_irq(&dev->event_lock);
  10247. if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
  10248. flush_workqueue(dev_priv->wq);
  10249. /* Reference the objects for the scheduled work. */
  10250. drm_framebuffer_reference(work->old_fb);
  10251. crtc->primary->fb = fb;
  10252. update_state_fb(crtc->primary);
  10253. work->pending_flip_obj = i915_gem_object_get(obj);
  10254. ret = i915_mutex_lock_interruptible(dev);
  10255. if (ret)
  10256. goto cleanup;
  10257. intel_crtc->reset_count = i915_reset_count(&dev_priv->gpu_error);
  10258. if (i915_reset_in_progress_or_wedged(&dev_priv->gpu_error)) {
  10259. ret = -EIO;
  10260. goto unlock;
  10261. }
  10262. atomic_inc(&intel_crtc->unpin_work_count);
  10263. if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  10264. work->flip_count = I915_READ(PIPE_FLIPCOUNT_G4X(pipe)) + 1;
  10265. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  10266. engine = dev_priv->engine[BCS];
  10267. if (fb->modifier != old_fb->modifier)
  10268. /* vlv: DISPLAY_FLIP fails to change tiling */
  10269. engine = NULL;
  10270. } else if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv)) {
  10271. engine = dev_priv->engine[BCS];
  10272. } else if (INTEL_GEN(dev_priv) >= 7) {
  10273. engine = i915_gem_object_last_write_engine(obj);
  10274. if (engine == NULL || engine->id != RCS)
  10275. engine = dev_priv->engine[BCS];
  10276. } else {
  10277. engine = dev_priv->engine[RCS];
  10278. }
  10279. mmio_flip = use_mmio_flip(engine, obj);
  10280. vma = intel_pin_and_fence_fb_obj(fb, primary->state->rotation);
  10281. if (IS_ERR(vma)) {
  10282. ret = PTR_ERR(vma);
  10283. goto cleanup_pending;
  10284. }
  10285. work->old_vma = to_intel_plane_state(primary->state)->vma;
  10286. to_intel_plane_state(primary->state)->vma = vma;
  10287. work->gtt_offset = i915_ggtt_offset(vma) + intel_crtc->dspaddr_offset;
  10288. work->rotation = crtc->primary->state->rotation;
  10289. /*
  10290. * There's the potential that the next frame will not be compatible with
  10291. * FBC, so we want to call pre_update() before the actual page flip.
  10292. * The problem is that pre_update() caches some information about the fb
  10293. * object, so we want to do this only after the object is pinned. Let's
  10294. * be on the safe side and do this immediately before scheduling the
  10295. * flip.
  10296. */
  10297. intel_fbc_pre_update(intel_crtc, intel_crtc->config,
  10298. to_intel_plane_state(primary->state));
  10299. if (mmio_flip) {
  10300. INIT_WORK(&work->mmio_work, intel_mmio_flip_work_func);
  10301. queue_work(system_unbound_wq, &work->mmio_work);
  10302. } else {
  10303. request = i915_gem_request_alloc(engine,
  10304. dev_priv->kernel_context);
  10305. if (IS_ERR(request)) {
  10306. ret = PTR_ERR(request);
  10307. goto cleanup_unpin;
  10308. }
  10309. ret = i915_gem_request_await_object(request, obj, false);
  10310. if (ret)
  10311. goto cleanup_request;
  10312. ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
  10313. page_flip_flags);
  10314. if (ret)
  10315. goto cleanup_request;
  10316. intel_mark_page_flip_active(intel_crtc, work);
  10317. work->flip_queued_req = i915_gem_request_get(request);
  10318. i915_add_request_no_flush(request);
  10319. }
  10320. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  10321. i915_gem_track_fb(intel_fb_obj(old_fb), obj,
  10322. to_intel_plane(primary)->frontbuffer_bit);
  10323. mutex_unlock(&dev->struct_mutex);
  10324. intel_frontbuffer_flip_prepare(to_i915(dev),
  10325. to_intel_plane(primary)->frontbuffer_bit);
  10326. trace_i915_flip_request(intel_crtc->plane, obj);
  10327. return 0;
  10328. cleanup_request:
  10329. i915_add_request_no_flush(request);
  10330. cleanup_unpin:
  10331. to_intel_plane_state(primary->state)->vma = work->old_vma;
  10332. intel_unpin_fb_vma(vma);
  10333. cleanup_pending:
  10334. atomic_dec(&intel_crtc->unpin_work_count);
  10335. unlock:
  10336. mutex_unlock(&dev->struct_mutex);
  10337. cleanup:
  10338. crtc->primary->fb = old_fb;
  10339. update_state_fb(crtc->primary);
  10340. i915_gem_object_put(obj);
  10341. drm_framebuffer_unreference(work->old_fb);
  10342. spin_lock_irq(&dev->event_lock);
  10343. intel_crtc->flip_work = NULL;
  10344. spin_unlock_irq(&dev->event_lock);
  10345. drm_crtc_vblank_put(crtc);
  10346. free_work:
  10347. kfree(work);
  10348. if (ret == -EIO) {
  10349. struct drm_atomic_state *state;
  10350. struct drm_plane_state *plane_state;
  10351. out_hang:
  10352. state = drm_atomic_state_alloc(dev);
  10353. if (!state)
  10354. return -ENOMEM;
  10355. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  10356. retry:
  10357. plane_state = drm_atomic_get_plane_state(state, primary);
  10358. ret = PTR_ERR_OR_ZERO(plane_state);
  10359. if (!ret) {
  10360. drm_atomic_set_fb_for_plane(plane_state, fb);
  10361. ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
  10362. if (!ret)
  10363. ret = drm_atomic_commit(state);
  10364. }
  10365. if (ret == -EDEADLK) {
  10366. drm_modeset_backoff(state->acquire_ctx);
  10367. drm_atomic_state_clear(state);
  10368. goto retry;
  10369. }
  10370. drm_atomic_state_put(state);
  10371. if (ret == 0 && event) {
  10372. spin_lock_irq(&dev->event_lock);
  10373. drm_crtc_send_vblank_event(crtc, event);
  10374. spin_unlock_irq(&dev->event_lock);
  10375. }
  10376. }
  10377. return ret;
  10378. }
  10379. /**
  10380. * intel_wm_need_update - Check whether watermarks need updating
  10381. * @plane: drm plane
  10382. * @state: new plane state
  10383. *
  10384. * Check current plane state versus the new one to determine whether
  10385. * watermarks need to be recalculated.
  10386. *
  10387. * Returns true or false.
  10388. */
  10389. static bool intel_wm_need_update(struct drm_plane *plane,
  10390. struct drm_plane_state *state)
  10391. {
  10392. struct intel_plane_state *new = to_intel_plane_state(state);
  10393. struct intel_plane_state *cur = to_intel_plane_state(plane->state);
  10394. /* Update watermarks on tiling or size changes. */
  10395. if (new->base.visible != cur->base.visible)
  10396. return true;
  10397. if (!cur->base.fb || !new->base.fb)
  10398. return false;
  10399. if (cur->base.fb->modifier != new->base.fb->modifier ||
  10400. cur->base.rotation != new->base.rotation ||
  10401. drm_rect_width(&new->base.src) != drm_rect_width(&cur->base.src) ||
  10402. drm_rect_height(&new->base.src) != drm_rect_height(&cur->base.src) ||
  10403. drm_rect_width(&new->base.dst) != drm_rect_width(&cur->base.dst) ||
  10404. drm_rect_height(&new->base.dst) != drm_rect_height(&cur->base.dst))
  10405. return true;
  10406. return false;
  10407. }
  10408. static bool needs_scaling(struct intel_plane_state *state)
  10409. {
  10410. int src_w = drm_rect_width(&state->base.src) >> 16;
  10411. int src_h = drm_rect_height(&state->base.src) >> 16;
  10412. int dst_w = drm_rect_width(&state->base.dst);
  10413. int dst_h = drm_rect_height(&state->base.dst);
  10414. return (src_w != dst_w || src_h != dst_h);
  10415. }
  10416. int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
  10417. struct drm_plane_state *plane_state)
  10418. {
  10419. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc_state);
  10420. struct drm_crtc *crtc = crtc_state->crtc;
  10421. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10422. struct drm_plane *plane = plane_state->plane;
  10423. struct drm_device *dev = crtc->dev;
  10424. struct drm_i915_private *dev_priv = to_i915(dev);
  10425. struct intel_plane_state *old_plane_state =
  10426. to_intel_plane_state(plane->state);
  10427. bool mode_changed = needs_modeset(crtc_state);
  10428. bool was_crtc_enabled = crtc->state->active;
  10429. bool is_crtc_enabled = crtc_state->active;
  10430. bool turn_off, turn_on, visible, was_visible;
  10431. struct drm_framebuffer *fb = plane_state->fb;
  10432. int ret;
  10433. if (INTEL_GEN(dev_priv) >= 9 && plane->type != DRM_PLANE_TYPE_CURSOR) {
  10434. ret = skl_update_scaler_plane(
  10435. to_intel_crtc_state(crtc_state),
  10436. to_intel_plane_state(plane_state));
  10437. if (ret)
  10438. return ret;
  10439. }
  10440. was_visible = old_plane_state->base.visible;
  10441. visible = plane_state->visible;
  10442. if (!was_crtc_enabled && WARN_ON(was_visible))
  10443. was_visible = false;
  10444. /*
  10445. * Visibility is calculated as if the crtc was on, but
  10446. * after scaler setup everything depends on it being off
  10447. * when the crtc isn't active.
  10448. *
  10449. * FIXME this is wrong for watermarks. Watermarks should also
  10450. * be computed as if the pipe would be active. Perhaps move
  10451. * per-plane wm computation to the .check_plane() hook, and
  10452. * only combine the results from all planes in the current place?
  10453. */
  10454. if (!is_crtc_enabled)
  10455. plane_state->visible = visible = false;
  10456. if (!was_visible && !visible)
  10457. return 0;
  10458. if (fb != old_plane_state->base.fb)
  10459. pipe_config->fb_changed = true;
  10460. turn_off = was_visible && (!visible || mode_changed);
  10461. turn_on = visible && (!was_visible || mode_changed);
  10462. DRM_DEBUG_ATOMIC("[CRTC:%d:%s] has [PLANE:%d:%s] with fb %i\n",
  10463. intel_crtc->base.base.id,
  10464. intel_crtc->base.name,
  10465. plane->base.id, plane->name,
  10466. fb ? fb->base.id : -1);
  10467. DRM_DEBUG_ATOMIC("[PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
  10468. plane->base.id, plane->name,
  10469. was_visible, visible,
  10470. turn_off, turn_on, mode_changed);
  10471. if (turn_on) {
  10472. pipe_config->update_wm_pre = true;
  10473. /* must disable cxsr around plane enable/disable */
  10474. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  10475. pipe_config->disable_cxsr = true;
  10476. } else if (turn_off) {
  10477. pipe_config->update_wm_post = true;
  10478. /* must disable cxsr around plane enable/disable */
  10479. if (plane->type != DRM_PLANE_TYPE_CURSOR)
  10480. pipe_config->disable_cxsr = true;
  10481. } else if (intel_wm_need_update(plane, plane_state)) {
  10482. /* FIXME bollocks */
  10483. pipe_config->update_wm_pre = true;
  10484. pipe_config->update_wm_post = true;
  10485. }
  10486. /* Pre-gen9 platforms need two-step watermark updates */
  10487. if ((pipe_config->update_wm_pre || pipe_config->update_wm_post) &&
  10488. INTEL_GEN(dev_priv) < 9 && dev_priv->display.optimize_watermarks)
  10489. to_intel_crtc_state(crtc_state)->wm.need_postvbl_update = true;
  10490. if (visible || was_visible)
  10491. pipe_config->fb_bits |= to_intel_plane(plane)->frontbuffer_bit;
  10492. /*
  10493. * WaCxSRDisabledForSpriteScaling:ivb
  10494. *
  10495. * cstate->update_wm was already set above, so this flag will
  10496. * take effect when we commit and program watermarks.
  10497. */
  10498. if (plane->type == DRM_PLANE_TYPE_OVERLAY && IS_IVYBRIDGE(dev_priv) &&
  10499. needs_scaling(to_intel_plane_state(plane_state)) &&
  10500. !needs_scaling(old_plane_state))
  10501. pipe_config->disable_lp_wm = true;
  10502. return 0;
  10503. }
  10504. static bool encoders_cloneable(const struct intel_encoder *a,
  10505. const struct intel_encoder *b)
  10506. {
  10507. /* masks could be asymmetric, so check both ways */
  10508. return a == b || (a->cloneable & (1 << b->type) &&
  10509. b->cloneable & (1 << a->type));
  10510. }
  10511. static bool check_single_encoder_cloning(struct drm_atomic_state *state,
  10512. struct intel_crtc *crtc,
  10513. struct intel_encoder *encoder)
  10514. {
  10515. struct intel_encoder *source_encoder;
  10516. struct drm_connector *connector;
  10517. struct drm_connector_state *connector_state;
  10518. int i;
  10519. for_each_connector_in_state(state, connector, connector_state, i) {
  10520. if (connector_state->crtc != &crtc->base)
  10521. continue;
  10522. source_encoder =
  10523. to_intel_encoder(connector_state->best_encoder);
  10524. if (!encoders_cloneable(encoder, source_encoder))
  10525. return false;
  10526. }
  10527. return true;
  10528. }
  10529. static int intel_crtc_atomic_check(struct drm_crtc *crtc,
  10530. struct drm_crtc_state *crtc_state)
  10531. {
  10532. struct drm_device *dev = crtc->dev;
  10533. struct drm_i915_private *dev_priv = to_i915(dev);
  10534. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  10535. struct intel_crtc_state *pipe_config =
  10536. to_intel_crtc_state(crtc_state);
  10537. struct drm_atomic_state *state = crtc_state->state;
  10538. int ret;
  10539. bool mode_changed = needs_modeset(crtc_state);
  10540. if (mode_changed && !crtc_state->active)
  10541. pipe_config->update_wm_post = true;
  10542. if (mode_changed && crtc_state->enable &&
  10543. dev_priv->display.crtc_compute_clock &&
  10544. !WARN_ON(pipe_config->shared_dpll)) {
  10545. ret = dev_priv->display.crtc_compute_clock(intel_crtc,
  10546. pipe_config);
  10547. if (ret)
  10548. return ret;
  10549. }
  10550. if (crtc_state->color_mgmt_changed) {
  10551. ret = intel_color_check(crtc, crtc_state);
  10552. if (ret)
  10553. return ret;
  10554. /*
  10555. * Changing color management on Intel hardware is
  10556. * handled as part of planes update.
  10557. */
  10558. crtc_state->planes_changed = true;
  10559. }
  10560. ret = 0;
  10561. if (dev_priv->display.compute_pipe_wm) {
  10562. ret = dev_priv->display.compute_pipe_wm(pipe_config);
  10563. if (ret) {
  10564. DRM_DEBUG_KMS("Target pipe watermarks are invalid\n");
  10565. return ret;
  10566. }
  10567. }
  10568. if (dev_priv->display.compute_intermediate_wm &&
  10569. !to_intel_atomic_state(state)->skip_intermediate_wm) {
  10570. if (WARN_ON(!dev_priv->display.compute_pipe_wm))
  10571. return 0;
  10572. /*
  10573. * Calculate 'intermediate' watermarks that satisfy both the
  10574. * old state and the new state. We can program these
  10575. * immediately.
  10576. */
  10577. ret = dev_priv->display.compute_intermediate_wm(dev,
  10578. intel_crtc,
  10579. pipe_config);
  10580. if (ret) {
  10581. DRM_DEBUG_KMS("No valid intermediate pipe watermarks are possible\n");
  10582. return ret;
  10583. }
  10584. } else if (dev_priv->display.compute_intermediate_wm) {
  10585. if (HAS_PCH_SPLIT(dev_priv) && INTEL_GEN(dev_priv) < 9)
  10586. pipe_config->wm.ilk.intermediate = pipe_config->wm.ilk.optimal;
  10587. }
  10588. if (INTEL_GEN(dev_priv) >= 9) {
  10589. if (mode_changed)
  10590. ret = skl_update_scaler_crtc(pipe_config);
  10591. if (!ret)
  10592. ret = intel_atomic_setup_scalers(dev, intel_crtc,
  10593. pipe_config);
  10594. }
  10595. return ret;
  10596. }
  10597. static const struct drm_crtc_helper_funcs intel_helper_funcs = {
  10598. .mode_set_base_atomic = intel_pipe_set_base_atomic,
  10599. .atomic_begin = intel_begin_crtc_commit,
  10600. .atomic_flush = intel_finish_crtc_commit,
  10601. .atomic_check = intel_crtc_atomic_check,
  10602. };
  10603. static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
  10604. {
  10605. struct intel_connector *connector;
  10606. for_each_intel_connector(dev, connector) {
  10607. if (connector->base.state->crtc)
  10608. drm_connector_unreference(&connector->base);
  10609. if (connector->base.encoder) {
  10610. connector->base.state->best_encoder =
  10611. connector->base.encoder;
  10612. connector->base.state->crtc =
  10613. connector->base.encoder->crtc;
  10614. drm_connector_reference(&connector->base);
  10615. } else {
  10616. connector->base.state->best_encoder = NULL;
  10617. connector->base.state->crtc = NULL;
  10618. }
  10619. }
  10620. }
  10621. static void
  10622. connected_sink_compute_bpp(struct intel_connector *connector,
  10623. struct intel_crtc_state *pipe_config)
  10624. {
  10625. const struct drm_display_info *info = &connector->base.display_info;
  10626. int bpp = pipe_config->pipe_bpp;
  10627. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
  10628. connector->base.base.id,
  10629. connector->base.name);
  10630. /* Don't use an invalid EDID bpc value */
  10631. if (info->bpc != 0 && info->bpc * 3 < bpp) {
  10632. DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
  10633. bpp, info->bpc * 3);
  10634. pipe_config->pipe_bpp = info->bpc * 3;
  10635. }
  10636. /* Clamp bpp to 8 on screens without EDID 1.4 */
  10637. if (info->bpc == 0 && bpp > 24) {
  10638. DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
  10639. bpp);
  10640. pipe_config->pipe_bpp = 24;
  10641. }
  10642. }
  10643. static int
  10644. compute_baseline_pipe_bpp(struct intel_crtc *crtc,
  10645. struct intel_crtc_state *pipe_config)
  10646. {
  10647. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  10648. struct drm_atomic_state *state;
  10649. struct drm_connector *connector;
  10650. struct drm_connector_state *connector_state;
  10651. int bpp, i;
  10652. if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
  10653. IS_CHERRYVIEW(dev_priv)))
  10654. bpp = 10*3;
  10655. else if (INTEL_GEN(dev_priv) >= 5)
  10656. bpp = 12*3;
  10657. else
  10658. bpp = 8*3;
  10659. pipe_config->pipe_bpp = bpp;
  10660. state = pipe_config->base.state;
  10661. /* Clamp display bpp to EDID value */
  10662. for_each_connector_in_state(state, connector, connector_state, i) {
  10663. if (connector_state->crtc != &crtc->base)
  10664. continue;
  10665. connected_sink_compute_bpp(to_intel_connector(connector),
  10666. pipe_config);
  10667. }
  10668. return bpp;
  10669. }
  10670. static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
  10671. {
  10672. DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
  10673. "type: 0x%x flags: 0x%x\n",
  10674. mode->crtc_clock,
  10675. mode->crtc_hdisplay, mode->crtc_hsync_start,
  10676. mode->crtc_hsync_end, mode->crtc_htotal,
  10677. mode->crtc_vdisplay, mode->crtc_vsync_start,
  10678. mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
  10679. }
  10680. static inline void
  10681. intel_dump_m_n_config(struct intel_crtc_state *pipe_config, char *id,
  10682. unsigned int lane_count, struct intel_link_m_n *m_n)
  10683. {
  10684. DRM_DEBUG_KMS("%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
  10685. id, lane_count,
  10686. m_n->gmch_m, m_n->gmch_n,
  10687. m_n->link_m, m_n->link_n, m_n->tu);
  10688. }
  10689. static void intel_dump_pipe_config(struct intel_crtc *crtc,
  10690. struct intel_crtc_state *pipe_config,
  10691. const char *context)
  10692. {
  10693. struct drm_device *dev = crtc->base.dev;
  10694. struct drm_i915_private *dev_priv = to_i915(dev);
  10695. struct drm_plane *plane;
  10696. struct intel_plane *intel_plane;
  10697. struct intel_plane_state *state;
  10698. struct drm_framebuffer *fb;
  10699. DRM_DEBUG_KMS("[CRTC:%d:%s]%s\n",
  10700. crtc->base.base.id, crtc->base.name, context);
  10701. DRM_DEBUG_KMS("cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
  10702. transcoder_name(pipe_config->cpu_transcoder),
  10703. pipe_config->pipe_bpp, pipe_config->dither);
  10704. if (pipe_config->has_pch_encoder)
  10705. intel_dump_m_n_config(pipe_config, "fdi",
  10706. pipe_config->fdi_lanes,
  10707. &pipe_config->fdi_m_n);
  10708. if (intel_crtc_has_dp_encoder(pipe_config)) {
  10709. intel_dump_m_n_config(pipe_config, "dp m_n",
  10710. pipe_config->lane_count, &pipe_config->dp_m_n);
  10711. if (pipe_config->has_drrs)
  10712. intel_dump_m_n_config(pipe_config, "dp m2_n2",
  10713. pipe_config->lane_count,
  10714. &pipe_config->dp_m2_n2);
  10715. }
  10716. DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
  10717. pipe_config->has_audio, pipe_config->has_infoframe);
  10718. DRM_DEBUG_KMS("requested mode:\n");
  10719. drm_mode_debug_printmodeline(&pipe_config->base.mode);
  10720. DRM_DEBUG_KMS("adjusted mode:\n");
  10721. drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
  10722. intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
  10723. DRM_DEBUG_KMS("port clock: %d, pipe src size: %dx%d\n",
  10724. pipe_config->port_clock,
  10725. pipe_config->pipe_src_w, pipe_config->pipe_src_h);
  10726. if (INTEL_GEN(dev_priv) >= 9)
  10727. DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
  10728. crtc->num_scalers,
  10729. pipe_config->scaler_state.scaler_users,
  10730. pipe_config->scaler_state.scaler_id);
  10731. if (HAS_GMCH_DISPLAY(dev_priv))
  10732. DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
  10733. pipe_config->gmch_pfit.control,
  10734. pipe_config->gmch_pfit.pgm_ratios,
  10735. pipe_config->gmch_pfit.lvds_border_bits);
  10736. else
  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. enableddisabled(pipe_config->pch_pfit.enabled));
  10741. DRM_DEBUG_KMS("ips: %i, double wide: %i\n",
  10742. pipe_config->ips_enabled, pipe_config->double_wide);
  10743. intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
  10744. DRM_DEBUG_KMS("planes on this crtc\n");
  10745. list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  10746. struct drm_format_name_buf format_name;
  10747. intel_plane = to_intel_plane(plane);
  10748. if (intel_plane->pipe != crtc->pipe)
  10749. continue;
  10750. state = to_intel_plane_state(plane->state);
  10751. fb = state->base.fb;
  10752. if (!fb) {
  10753. DRM_DEBUG_KMS("[PLANE:%d:%s] disabled, scaler_id = %d\n",
  10754. plane->base.id, plane->name, state->scaler_id);
  10755. continue;
  10756. }
  10757. DRM_DEBUG_KMS("[PLANE:%d:%s] FB:%d, fb = %ux%u format = %s\n",
  10758. plane->base.id, plane->name,
  10759. fb->base.id, fb->width, fb->height,
  10760. drm_get_format_name(fb->format->format, &format_name));
  10761. if (INTEL_GEN(dev_priv) >= 9)
  10762. DRM_DEBUG_KMS("\tscaler:%d src %dx%d+%d+%d dst %dx%d+%d+%d\n",
  10763. state->scaler_id,
  10764. state->base.src.x1 >> 16,
  10765. state->base.src.y1 >> 16,
  10766. drm_rect_width(&state->base.src) >> 16,
  10767. drm_rect_height(&state->base.src) >> 16,
  10768. state->base.dst.x1, state->base.dst.y1,
  10769. drm_rect_width(&state->base.dst),
  10770. drm_rect_height(&state->base.dst));
  10771. }
  10772. }
  10773. static bool check_digital_port_conflicts(struct drm_atomic_state *state)
  10774. {
  10775. struct drm_device *dev = state->dev;
  10776. struct drm_connector *connector;
  10777. unsigned int used_ports = 0;
  10778. unsigned int used_mst_ports = 0;
  10779. /*
  10780. * Walk the connector list instead of the encoder
  10781. * list to detect the problem on ddi platforms
  10782. * where there's just one encoder per digital port.
  10783. */
  10784. drm_for_each_connector(connector, dev) {
  10785. struct drm_connector_state *connector_state;
  10786. struct intel_encoder *encoder;
  10787. connector_state = drm_atomic_get_existing_connector_state(state, connector);
  10788. if (!connector_state)
  10789. connector_state = connector->state;
  10790. if (!connector_state->best_encoder)
  10791. continue;
  10792. encoder = to_intel_encoder(connector_state->best_encoder);
  10793. WARN_ON(!connector_state->crtc);
  10794. switch (encoder->type) {
  10795. unsigned int port_mask;
  10796. case INTEL_OUTPUT_UNKNOWN:
  10797. if (WARN_ON(!HAS_DDI(to_i915(dev))))
  10798. break;
  10799. case INTEL_OUTPUT_DP:
  10800. case INTEL_OUTPUT_HDMI:
  10801. case INTEL_OUTPUT_EDP:
  10802. port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
  10803. /* the same port mustn't appear more than once */
  10804. if (used_ports & port_mask)
  10805. return false;
  10806. used_ports |= port_mask;
  10807. break;
  10808. case INTEL_OUTPUT_DP_MST:
  10809. used_mst_ports |=
  10810. 1 << enc_to_mst(&encoder->base)->primary->port;
  10811. break;
  10812. default:
  10813. break;
  10814. }
  10815. }
  10816. /* can't mix MST and SST/HDMI on the same port */
  10817. if (used_ports & used_mst_ports)
  10818. return false;
  10819. return true;
  10820. }
  10821. static void
  10822. clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
  10823. {
  10824. struct drm_crtc_state tmp_state;
  10825. struct intel_crtc_scaler_state scaler_state;
  10826. struct intel_dpll_hw_state dpll_hw_state;
  10827. struct intel_shared_dpll *shared_dpll;
  10828. bool force_thru;
  10829. /* FIXME: before the switch to atomic started, a new pipe_config was
  10830. * kzalloc'd. Code that depends on any field being zero should be
  10831. * fixed, so that the crtc_state can be safely duplicated. For now,
  10832. * only fields that are know to not cause problems are preserved. */
  10833. tmp_state = crtc_state->base;
  10834. scaler_state = crtc_state->scaler_state;
  10835. shared_dpll = crtc_state->shared_dpll;
  10836. dpll_hw_state = crtc_state->dpll_hw_state;
  10837. force_thru = crtc_state->pch_pfit.force_thru;
  10838. memset(crtc_state, 0, sizeof *crtc_state);
  10839. crtc_state->base = tmp_state;
  10840. crtc_state->scaler_state = scaler_state;
  10841. crtc_state->shared_dpll = shared_dpll;
  10842. crtc_state->dpll_hw_state = dpll_hw_state;
  10843. crtc_state->pch_pfit.force_thru = force_thru;
  10844. }
  10845. static int
  10846. intel_modeset_pipe_config(struct drm_crtc *crtc,
  10847. struct intel_crtc_state *pipe_config)
  10848. {
  10849. struct drm_atomic_state *state = pipe_config->base.state;
  10850. struct intel_encoder *encoder;
  10851. struct drm_connector *connector;
  10852. struct drm_connector_state *connector_state;
  10853. int base_bpp, ret = -EINVAL;
  10854. int i;
  10855. bool retry = true;
  10856. clear_intel_crtc_state(pipe_config);
  10857. pipe_config->cpu_transcoder =
  10858. (enum transcoder) to_intel_crtc(crtc)->pipe;
  10859. /*
  10860. * Sanitize sync polarity flags based on requested ones. If neither
  10861. * positive or negative polarity is requested, treat this as meaning
  10862. * negative polarity.
  10863. */
  10864. if (!(pipe_config->base.adjusted_mode.flags &
  10865. (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
  10866. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
  10867. if (!(pipe_config->base.adjusted_mode.flags &
  10868. (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
  10869. pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
  10870. base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
  10871. pipe_config);
  10872. if (base_bpp < 0)
  10873. goto fail;
  10874. /*
  10875. * Determine the real pipe dimensions. Note that stereo modes can
  10876. * increase the actual pipe size due to the frame doubling and
  10877. * insertion of additional space for blanks between the frame. This
  10878. * is stored in the crtc timings. We use the requested mode to do this
  10879. * computation to clearly distinguish it from the adjusted mode, which
  10880. * can be changed by the connectors in the below retry loop.
  10881. */
  10882. drm_crtc_get_hv_timing(&pipe_config->base.mode,
  10883. &pipe_config->pipe_src_w,
  10884. &pipe_config->pipe_src_h);
  10885. for_each_connector_in_state(state, connector, connector_state, i) {
  10886. if (connector_state->crtc != crtc)
  10887. continue;
  10888. encoder = to_intel_encoder(connector_state->best_encoder);
  10889. if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
  10890. DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
  10891. goto fail;
  10892. }
  10893. /*
  10894. * Determine output_types before calling the .compute_config()
  10895. * hooks so that the hooks can use this information safely.
  10896. */
  10897. pipe_config->output_types |= 1 << encoder->type;
  10898. }
  10899. encoder_retry:
  10900. /* Ensure the port clock defaults are reset when retrying. */
  10901. pipe_config->port_clock = 0;
  10902. pipe_config->pixel_multiplier = 1;
  10903. /* Fill in default crtc timings, allow encoders to overwrite them. */
  10904. drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
  10905. CRTC_STEREO_DOUBLE);
  10906. /* Pass our mode to the connectors and the CRTC to give them a chance to
  10907. * adjust it according to limitations or connector properties, and also
  10908. * a chance to reject the mode entirely.
  10909. */
  10910. for_each_connector_in_state(state, connector, connector_state, i) {
  10911. if (connector_state->crtc != crtc)
  10912. continue;
  10913. encoder = to_intel_encoder(connector_state->best_encoder);
  10914. if (!(encoder->compute_config(encoder, pipe_config, connector_state))) {
  10915. DRM_DEBUG_KMS("Encoder config failure\n");
  10916. goto fail;
  10917. }
  10918. }
  10919. /* Set default port clock if not overwritten by the encoder. Needs to be
  10920. * done afterwards in case the encoder adjusts the mode. */
  10921. if (!pipe_config->port_clock)
  10922. pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
  10923. * pipe_config->pixel_multiplier;
  10924. ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
  10925. if (ret < 0) {
  10926. DRM_DEBUG_KMS("CRTC fixup failed\n");
  10927. goto fail;
  10928. }
  10929. if (ret == RETRY) {
  10930. if (WARN(!retry, "loop in pipe configuration computation\n")) {
  10931. ret = -EINVAL;
  10932. goto fail;
  10933. }
  10934. DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
  10935. retry = false;
  10936. goto encoder_retry;
  10937. }
  10938. /* Dithering seems to not pass-through bits correctly when it should, so
  10939. * only enable it on 6bpc panels. */
  10940. pipe_config->dither = pipe_config->pipe_bpp == 6*3;
  10941. DRM_DEBUG_KMS("hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
  10942. base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
  10943. fail:
  10944. return ret;
  10945. }
  10946. static void
  10947. intel_modeset_update_crtc_state(struct drm_atomic_state *state)
  10948. {
  10949. struct drm_crtc *crtc;
  10950. struct drm_crtc_state *crtc_state;
  10951. int i;
  10952. /* Double check state. */
  10953. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  10954. to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
  10955. /* Update hwmode for vblank functions */
  10956. if (crtc->state->active)
  10957. crtc->hwmode = crtc->state->adjusted_mode;
  10958. else
  10959. crtc->hwmode.crtc_clock = 0;
  10960. /*
  10961. * Update legacy state to satisfy fbc code. This can
  10962. * be removed when fbc uses the atomic state.
  10963. */
  10964. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  10965. struct drm_plane_state *plane_state = crtc->primary->state;
  10966. crtc->primary->fb = plane_state->fb;
  10967. crtc->x = plane_state->src_x >> 16;
  10968. crtc->y = plane_state->src_y >> 16;
  10969. }
  10970. }
  10971. }
  10972. static bool intel_fuzzy_clock_check(int clock1, int clock2)
  10973. {
  10974. int diff;
  10975. if (clock1 == clock2)
  10976. return true;
  10977. if (!clock1 || !clock2)
  10978. return false;
  10979. diff = abs(clock1 - clock2);
  10980. if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
  10981. return true;
  10982. return false;
  10983. }
  10984. static bool
  10985. intel_compare_m_n(unsigned int m, unsigned int n,
  10986. unsigned int m2, unsigned int n2,
  10987. bool exact)
  10988. {
  10989. if (m == m2 && n == n2)
  10990. return true;
  10991. if (exact || !m || !n || !m2 || !n2)
  10992. return false;
  10993. BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
  10994. if (n > n2) {
  10995. while (n > n2) {
  10996. m2 <<= 1;
  10997. n2 <<= 1;
  10998. }
  10999. } else if (n < n2) {
  11000. while (n < n2) {
  11001. m <<= 1;
  11002. n <<= 1;
  11003. }
  11004. }
  11005. if (n != n2)
  11006. return false;
  11007. return intel_fuzzy_clock_check(m, m2);
  11008. }
  11009. static bool
  11010. intel_compare_link_m_n(const struct intel_link_m_n *m_n,
  11011. struct intel_link_m_n *m2_n2,
  11012. bool adjust)
  11013. {
  11014. if (m_n->tu == m2_n2->tu &&
  11015. intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
  11016. m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
  11017. intel_compare_m_n(m_n->link_m, m_n->link_n,
  11018. m2_n2->link_m, m2_n2->link_n, !adjust)) {
  11019. if (adjust)
  11020. *m2_n2 = *m_n;
  11021. return true;
  11022. }
  11023. return false;
  11024. }
  11025. static void __printf(3, 4)
  11026. pipe_config_err(bool adjust, const char *name, const char *format, ...)
  11027. {
  11028. char *level;
  11029. unsigned int category;
  11030. struct va_format vaf;
  11031. va_list args;
  11032. if (adjust) {
  11033. level = KERN_DEBUG;
  11034. category = DRM_UT_KMS;
  11035. } else {
  11036. level = KERN_ERR;
  11037. category = DRM_UT_NONE;
  11038. }
  11039. va_start(args, format);
  11040. vaf.fmt = format;
  11041. vaf.va = &args;
  11042. drm_printk(level, category, "mismatch in %s %pV", name, &vaf);
  11043. va_end(args);
  11044. }
  11045. static bool
  11046. intel_pipe_config_compare(struct drm_i915_private *dev_priv,
  11047. struct intel_crtc_state *current_config,
  11048. struct intel_crtc_state *pipe_config,
  11049. bool adjust)
  11050. {
  11051. bool ret = true;
  11052. #define PIPE_CONF_CHECK_X(name) \
  11053. if (current_config->name != pipe_config->name) { \
  11054. pipe_config_err(adjust, __stringify(name), \
  11055. "(expected 0x%08x, found 0x%08x)\n", \
  11056. current_config->name, \
  11057. pipe_config->name); \
  11058. ret = false; \
  11059. }
  11060. #define PIPE_CONF_CHECK_I(name) \
  11061. if (current_config->name != pipe_config->name) { \
  11062. pipe_config_err(adjust, __stringify(name), \
  11063. "(expected %i, found %i)\n", \
  11064. current_config->name, \
  11065. pipe_config->name); \
  11066. ret = false; \
  11067. }
  11068. #define PIPE_CONF_CHECK_P(name) \
  11069. if (current_config->name != pipe_config->name) { \
  11070. pipe_config_err(adjust, __stringify(name), \
  11071. "(expected %p, found %p)\n", \
  11072. current_config->name, \
  11073. pipe_config->name); \
  11074. ret = false; \
  11075. }
  11076. #define PIPE_CONF_CHECK_M_N(name) \
  11077. if (!intel_compare_link_m_n(&current_config->name, \
  11078. &pipe_config->name,\
  11079. adjust)) { \
  11080. pipe_config_err(adjust, __stringify(name), \
  11081. "(expected tu %i gmch %i/%i link %i/%i, " \
  11082. "found tu %i, gmch %i/%i link %i/%i)\n", \
  11083. current_config->name.tu, \
  11084. current_config->name.gmch_m, \
  11085. current_config->name.gmch_n, \
  11086. current_config->name.link_m, \
  11087. current_config->name.link_n, \
  11088. pipe_config->name.tu, \
  11089. pipe_config->name.gmch_m, \
  11090. pipe_config->name.gmch_n, \
  11091. pipe_config->name.link_m, \
  11092. pipe_config->name.link_n); \
  11093. ret = false; \
  11094. }
  11095. /* This is required for BDW+ where there is only one set of registers for
  11096. * switching between high and low RR.
  11097. * This macro can be used whenever a comparison has to be made between one
  11098. * hw state and multiple sw state variables.
  11099. */
  11100. #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
  11101. if (!intel_compare_link_m_n(&current_config->name, \
  11102. &pipe_config->name, adjust) && \
  11103. !intel_compare_link_m_n(&current_config->alt_name, \
  11104. &pipe_config->name, adjust)) { \
  11105. pipe_config_err(adjust, __stringify(name), \
  11106. "(expected tu %i gmch %i/%i link %i/%i, " \
  11107. "or tu %i gmch %i/%i link %i/%i, " \
  11108. "found tu %i, gmch %i/%i link %i/%i)\n", \
  11109. current_config->name.tu, \
  11110. current_config->name.gmch_m, \
  11111. current_config->name.gmch_n, \
  11112. current_config->name.link_m, \
  11113. current_config->name.link_n, \
  11114. current_config->alt_name.tu, \
  11115. current_config->alt_name.gmch_m, \
  11116. current_config->alt_name.gmch_n, \
  11117. current_config->alt_name.link_m, \
  11118. current_config->alt_name.link_n, \
  11119. pipe_config->name.tu, \
  11120. pipe_config->name.gmch_m, \
  11121. pipe_config->name.gmch_n, \
  11122. pipe_config->name.link_m, \
  11123. pipe_config->name.link_n); \
  11124. ret = false; \
  11125. }
  11126. #define PIPE_CONF_CHECK_FLAGS(name, mask) \
  11127. if ((current_config->name ^ pipe_config->name) & (mask)) { \
  11128. pipe_config_err(adjust, __stringify(name), \
  11129. "(%x) (expected %i, found %i)\n", \
  11130. (mask), \
  11131. current_config->name & (mask), \
  11132. pipe_config->name & (mask)); \
  11133. ret = false; \
  11134. }
  11135. #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
  11136. if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
  11137. pipe_config_err(adjust, __stringify(name), \
  11138. "(expected %i, found %i)\n", \
  11139. current_config->name, \
  11140. pipe_config->name); \
  11141. ret = false; \
  11142. }
  11143. #define PIPE_CONF_QUIRK(quirk) \
  11144. ((current_config->quirks | pipe_config->quirks) & (quirk))
  11145. PIPE_CONF_CHECK_I(cpu_transcoder);
  11146. PIPE_CONF_CHECK_I(has_pch_encoder);
  11147. PIPE_CONF_CHECK_I(fdi_lanes);
  11148. PIPE_CONF_CHECK_M_N(fdi_m_n);
  11149. PIPE_CONF_CHECK_I(lane_count);
  11150. PIPE_CONF_CHECK_X(lane_lat_optim_mask);
  11151. if (INTEL_GEN(dev_priv) < 8) {
  11152. PIPE_CONF_CHECK_M_N(dp_m_n);
  11153. if (current_config->has_drrs)
  11154. PIPE_CONF_CHECK_M_N(dp_m2_n2);
  11155. } else
  11156. PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
  11157. PIPE_CONF_CHECK_X(output_types);
  11158. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
  11159. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
  11160. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
  11161. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
  11162. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
  11163. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
  11164. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
  11165. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
  11166. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
  11167. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
  11168. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
  11169. PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
  11170. PIPE_CONF_CHECK_I(pixel_multiplier);
  11171. PIPE_CONF_CHECK_I(has_hdmi_sink);
  11172. if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
  11173. IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  11174. PIPE_CONF_CHECK_I(limited_color_range);
  11175. PIPE_CONF_CHECK_I(has_infoframe);
  11176. PIPE_CONF_CHECK_I(has_audio);
  11177. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11178. DRM_MODE_FLAG_INTERLACE);
  11179. if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
  11180. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11181. DRM_MODE_FLAG_PHSYNC);
  11182. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11183. DRM_MODE_FLAG_NHSYNC);
  11184. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11185. DRM_MODE_FLAG_PVSYNC);
  11186. PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
  11187. DRM_MODE_FLAG_NVSYNC);
  11188. }
  11189. PIPE_CONF_CHECK_X(gmch_pfit.control);
  11190. /* pfit ratios are autocomputed by the hw on gen4+ */
  11191. if (INTEL_GEN(dev_priv) < 4)
  11192. PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
  11193. PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
  11194. if (!adjust) {
  11195. PIPE_CONF_CHECK_I(pipe_src_w);
  11196. PIPE_CONF_CHECK_I(pipe_src_h);
  11197. PIPE_CONF_CHECK_I(pch_pfit.enabled);
  11198. if (current_config->pch_pfit.enabled) {
  11199. PIPE_CONF_CHECK_X(pch_pfit.pos);
  11200. PIPE_CONF_CHECK_X(pch_pfit.size);
  11201. }
  11202. PIPE_CONF_CHECK_I(scaler_state.scaler_id);
  11203. }
  11204. /* BDW+ don't expose a synchronous way to read the state */
  11205. if (IS_HASWELL(dev_priv))
  11206. PIPE_CONF_CHECK_I(ips_enabled);
  11207. PIPE_CONF_CHECK_I(double_wide);
  11208. PIPE_CONF_CHECK_P(shared_dpll);
  11209. PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
  11210. PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
  11211. PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
  11212. PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
  11213. PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
  11214. PIPE_CONF_CHECK_X(dpll_hw_state.spll);
  11215. PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
  11216. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
  11217. PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
  11218. PIPE_CONF_CHECK_X(dsi_pll.ctrl);
  11219. PIPE_CONF_CHECK_X(dsi_pll.div);
  11220. if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
  11221. PIPE_CONF_CHECK_I(pipe_bpp);
  11222. PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
  11223. PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
  11224. #undef PIPE_CONF_CHECK_X
  11225. #undef PIPE_CONF_CHECK_I
  11226. #undef PIPE_CONF_CHECK_P
  11227. #undef PIPE_CONF_CHECK_FLAGS
  11228. #undef PIPE_CONF_CHECK_CLOCK_FUZZY
  11229. #undef PIPE_CONF_QUIRK
  11230. return ret;
  11231. }
  11232. static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
  11233. const struct intel_crtc_state *pipe_config)
  11234. {
  11235. if (pipe_config->has_pch_encoder) {
  11236. int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
  11237. &pipe_config->fdi_m_n);
  11238. int dotclock = pipe_config->base.adjusted_mode.crtc_clock;
  11239. /*
  11240. * FDI already provided one idea for the dotclock.
  11241. * Yell if the encoder disagrees.
  11242. */
  11243. WARN(!intel_fuzzy_clock_check(fdi_dotclock, dotclock),
  11244. "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
  11245. fdi_dotclock, dotclock);
  11246. }
  11247. }
  11248. static void verify_wm_state(struct drm_crtc *crtc,
  11249. struct drm_crtc_state *new_state)
  11250. {
  11251. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  11252. struct skl_ddb_allocation hw_ddb, *sw_ddb;
  11253. struct skl_pipe_wm hw_wm, *sw_wm;
  11254. struct skl_plane_wm *hw_plane_wm, *sw_plane_wm;
  11255. struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
  11256. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11257. const enum pipe pipe = intel_crtc->pipe;
  11258. int plane, level, max_level = ilk_wm_max_level(dev_priv);
  11259. if (INTEL_GEN(dev_priv) < 9 || !new_state->active)
  11260. return;
  11261. skl_pipe_wm_get_hw_state(crtc, &hw_wm);
  11262. sw_wm = &to_intel_crtc_state(new_state)->wm.skl.optimal;
  11263. skl_ddb_get_hw_state(dev_priv, &hw_ddb);
  11264. sw_ddb = &dev_priv->wm.skl_hw.ddb;
  11265. /* planes */
  11266. for_each_universal_plane(dev_priv, pipe, plane) {
  11267. hw_plane_wm = &hw_wm.planes[plane];
  11268. sw_plane_wm = &sw_wm->planes[plane];
  11269. /* Watermarks */
  11270. for (level = 0; level <= max_level; level++) {
  11271. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  11272. &sw_plane_wm->wm[level]))
  11273. continue;
  11274. 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",
  11275. pipe_name(pipe), plane + 1, level,
  11276. sw_plane_wm->wm[level].plane_en,
  11277. sw_plane_wm->wm[level].plane_res_b,
  11278. sw_plane_wm->wm[level].plane_res_l,
  11279. hw_plane_wm->wm[level].plane_en,
  11280. hw_plane_wm->wm[level].plane_res_b,
  11281. hw_plane_wm->wm[level].plane_res_l);
  11282. }
  11283. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  11284. &sw_plane_wm->trans_wm)) {
  11285. 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",
  11286. pipe_name(pipe), plane + 1,
  11287. sw_plane_wm->trans_wm.plane_en,
  11288. sw_plane_wm->trans_wm.plane_res_b,
  11289. sw_plane_wm->trans_wm.plane_res_l,
  11290. hw_plane_wm->trans_wm.plane_en,
  11291. hw_plane_wm->trans_wm.plane_res_b,
  11292. hw_plane_wm->trans_wm.plane_res_l);
  11293. }
  11294. /* DDB */
  11295. hw_ddb_entry = &hw_ddb.plane[pipe][plane];
  11296. sw_ddb_entry = &sw_ddb->plane[pipe][plane];
  11297. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  11298. DRM_ERROR("mismatch in DDB state pipe %c plane %d (expected (%u,%u), found (%u,%u))\n",
  11299. pipe_name(pipe), plane + 1,
  11300. sw_ddb_entry->start, sw_ddb_entry->end,
  11301. hw_ddb_entry->start, hw_ddb_entry->end);
  11302. }
  11303. }
  11304. /*
  11305. * cursor
  11306. * If the cursor plane isn't active, we may not have updated it's ddb
  11307. * allocation. In that case since the ddb allocation will be updated
  11308. * once the plane becomes visible, we can skip this check
  11309. */
  11310. if (intel_crtc->cursor_addr) {
  11311. hw_plane_wm = &hw_wm.planes[PLANE_CURSOR];
  11312. sw_plane_wm = &sw_wm->planes[PLANE_CURSOR];
  11313. /* Watermarks */
  11314. for (level = 0; level <= max_level; level++) {
  11315. if (skl_wm_level_equals(&hw_plane_wm->wm[level],
  11316. &sw_plane_wm->wm[level]))
  11317. continue;
  11318. 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",
  11319. pipe_name(pipe), level,
  11320. sw_plane_wm->wm[level].plane_en,
  11321. sw_plane_wm->wm[level].plane_res_b,
  11322. sw_plane_wm->wm[level].plane_res_l,
  11323. hw_plane_wm->wm[level].plane_en,
  11324. hw_plane_wm->wm[level].plane_res_b,
  11325. hw_plane_wm->wm[level].plane_res_l);
  11326. }
  11327. if (!skl_wm_level_equals(&hw_plane_wm->trans_wm,
  11328. &sw_plane_wm->trans_wm)) {
  11329. DRM_ERROR("mismatch in trans WM pipe %c cursor (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
  11330. pipe_name(pipe),
  11331. sw_plane_wm->trans_wm.plane_en,
  11332. sw_plane_wm->trans_wm.plane_res_b,
  11333. sw_plane_wm->trans_wm.plane_res_l,
  11334. hw_plane_wm->trans_wm.plane_en,
  11335. hw_plane_wm->trans_wm.plane_res_b,
  11336. hw_plane_wm->trans_wm.plane_res_l);
  11337. }
  11338. /* DDB */
  11339. hw_ddb_entry = &hw_ddb.plane[pipe][PLANE_CURSOR];
  11340. sw_ddb_entry = &sw_ddb->plane[pipe][PLANE_CURSOR];
  11341. if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
  11342. DRM_ERROR("mismatch in DDB state pipe %c cursor (expected (%u,%u), found (%u,%u))\n",
  11343. pipe_name(pipe),
  11344. sw_ddb_entry->start, sw_ddb_entry->end,
  11345. hw_ddb_entry->start, hw_ddb_entry->end);
  11346. }
  11347. }
  11348. }
  11349. static void
  11350. verify_connector_state(struct drm_device *dev,
  11351. struct drm_atomic_state *state,
  11352. struct drm_crtc *crtc)
  11353. {
  11354. struct drm_connector *connector;
  11355. struct drm_connector_state *old_conn_state;
  11356. int i;
  11357. for_each_connector_in_state(state, connector, old_conn_state, i) {
  11358. struct drm_encoder *encoder = connector->encoder;
  11359. struct drm_connector_state *state = connector->state;
  11360. if (state->crtc != crtc)
  11361. continue;
  11362. intel_connector_verify_state(to_intel_connector(connector));
  11363. I915_STATE_WARN(state->best_encoder != encoder,
  11364. "connector's atomic encoder doesn't match legacy encoder\n");
  11365. }
  11366. }
  11367. static void
  11368. verify_encoder_state(struct drm_device *dev)
  11369. {
  11370. struct intel_encoder *encoder;
  11371. struct intel_connector *connector;
  11372. for_each_intel_encoder(dev, encoder) {
  11373. bool enabled = false;
  11374. enum pipe pipe;
  11375. DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
  11376. encoder->base.base.id,
  11377. encoder->base.name);
  11378. for_each_intel_connector(dev, connector) {
  11379. if (connector->base.state->best_encoder != &encoder->base)
  11380. continue;
  11381. enabled = true;
  11382. I915_STATE_WARN(connector->base.state->crtc !=
  11383. encoder->base.crtc,
  11384. "connector's crtc doesn't match encoder crtc\n");
  11385. }
  11386. I915_STATE_WARN(!!encoder->base.crtc != enabled,
  11387. "encoder's enabled state mismatch "
  11388. "(expected %i, found %i)\n",
  11389. !!encoder->base.crtc, enabled);
  11390. if (!encoder->base.crtc) {
  11391. bool active;
  11392. active = encoder->get_hw_state(encoder, &pipe);
  11393. I915_STATE_WARN(active,
  11394. "encoder detached but still enabled on pipe %c.\n",
  11395. pipe_name(pipe));
  11396. }
  11397. }
  11398. }
  11399. static void
  11400. verify_crtc_state(struct drm_crtc *crtc,
  11401. struct drm_crtc_state *old_crtc_state,
  11402. struct drm_crtc_state *new_crtc_state)
  11403. {
  11404. struct drm_device *dev = crtc->dev;
  11405. struct drm_i915_private *dev_priv = to_i915(dev);
  11406. struct intel_encoder *encoder;
  11407. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11408. struct intel_crtc_state *pipe_config, *sw_config;
  11409. struct drm_atomic_state *old_state;
  11410. bool active;
  11411. old_state = old_crtc_state->state;
  11412. __drm_atomic_helper_crtc_destroy_state(old_crtc_state);
  11413. pipe_config = to_intel_crtc_state(old_crtc_state);
  11414. memset(pipe_config, 0, sizeof(*pipe_config));
  11415. pipe_config->base.crtc = crtc;
  11416. pipe_config->base.state = old_state;
  11417. DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
  11418. active = dev_priv->display.get_pipe_config(intel_crtc, pipe_config);
  11419. /* hw state is inconsistent with the pipe quirk */
  11420. if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
  11421. (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
  11422. active = new_crtc_state->active;
  11423. I915_STATE_WARN(new_crtc_state->active != active,
  11424. "crtc active state doesn't match with hw state "
  11425. "(expected %i, found %i)\n", new_crtc_state->active, active);
  11426. I915_STATE_WARN(intel_crtc->active != new_crtc_state->active,
  11427. "transitional active state does not match atomic hw state "
  11428. "(expected %i, found %i)\n", new_crtc_state->active, intel_crtc->active);
  11429. for_each_encoder_on_crtc(dev, crtc, encoder) {
  11430. enum pipe pipe;
  11431. active = encoder->get_hw_state(encoder, &pipe);
  11432. I915_STATE_WARN(active != new_crtc_state->active,
  11433. "[ENCODER:%i] active %i with crtc active %i\n",
  11434. encoder->base.base.id, active, new_crtc_state->active);
  11435. I915_STATE_WARN(active && intel_crtc->pipe != pipe,
  11436. "Encoder connected to wrong pipe %c\n",
  11437. pipe_name(pipe));
  11438. if (active) {
  11439. pipe_config->output_types |= 1 << encoder->type;
  11440. encoder->get_config(encoder, pipe_config);
  11441. }
  11442. }
  11443. if (!new_crtc_state->active)
  11444. return;
  11445. intel_pipe_config_sanity_check(dev_priv, pipe_config);
  11446. sw_config = to_intel_crtc_state(crtc->state);
  11447. if (!intel_pipe_config_compare(dev_priv, sw_config,
  11448. pipe_config, false)) {
  11449. I915_STATE_WARN(1, "pipe state doesn't match!\n");
  11450. intel_dump_pipe_config(intel_crtc, pipe_config,
  11451. "[hw state]");
  11452. intel_dump_pipe_config(intel_crtc, sw_config,
  11453. "[sw state]");
  11454. }
  11455. }
  11456. static void
  11457. verify_single_dpll_state(struct drm_i915_private *dev_priv,
  11458. struct intel_shared_dpll *pll,
  11459. struct drm_crtc *crtc,
  11460. struct drm_crtc_state *new_state)
  11461. {
  11462. struct intel_dpll_hw_state dpll_hw_state;
  11463. unsigned crtc_mask;
  11464. bool active;
  11465. memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
  11466. DRM_DEBUG_KMS("%s\n", pll->name);
  11467. active = pll->funcs.get_hw_state(dev_priv, pll, &dpll_hw_state);
  11468. if (!(pll->flags & INTEL_DPLL_ALWAYS_ON)) {
  11469. I915_STATE_WARN(!pll->on && pll->active_mask,
  11470. "pll in active use but not on in sw tracking\n");
  11471. I915_STATE_WARN(pll->on && !pll->active_mask,
  11472. "pll is on but not used by any active crtc\n");
  11473. I915_STATE_WARN(pll->on != active,
  11474. "pll on state mismatch (expected %i, found %i)\n",
  11475. pll->on, active);
  11476. }
  11477. if (!crtc) {
  11478. I915_STATE_WARN(pll->active_mask & ~pll->state.crtc_mask,
  11479. "more active pll users than references: %x vs %x\n",
  11480. pll->active_mask, pll->state.crtc_mask);
  11481. return;
  11482. }
  11483. crtc_mask = 1 << drm_crtc_index(crtc);
  11484. if (new_state->active)
  11485. I915_STATE_WARN(!(pll->active_mask & crtc_mask),
  11486. "pll active mismatch (expected pipe %c in active mask 0x%02x)\n",
  11487. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  11488. else
  11489. I915_STATE_WARN(pll->active_mask & crtc_mask,
  11490. "pll active mismatch (didn't expect pipe %c in active mask 0x%02x)\n",
  11491. pipe_name(drm_crtc_index(crtc)), pll->active_mask);
  11492. I915_STATE_WARN(!(pll->state.crtc_mask & crtc_mask),
  11493. "pll enabled crtcs mismatch (expected 0x%x in 0x%02x)\n",
  11494. crtc_mask, pll->state.crtc_mask);
  11495. I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
  11496. &dpll_hw_state,
  11497. sizeof(dpll_hw_state)),
  11498. "pll hw state mismatch\n");
  11499. }
  11500. static void
  11501. verify_shared_dpll_state(struct drm_device *dev, struct drm_crtc *crtc,
  11502. struct drm_crtc_state *old_crtc_state,
  11503. struct drm_crtc_state *new_crtc_state)
  11504. {
  11505. struct drm_i915_private *dev_priv = to_i915(dev);
  11506. struct intel_crtc_state *old_state = to_intel_crtc_state(old_crtc_state);
  11507. struct intel_crtc_state *new_state = to_intel_crtc_state(new_crtc_state);
  11508. if (new_state->shared_dpll)
  11509. verify_single_dpll_state(dev_priv, new_state->shared_dpll, crtc, new_crtc_state);
  11510. if (old_state->shared_dpll &&
  11511. old_state->shared_dpll != new_state->shared_dpll) {
  11512. unsigned crtc_mask = 1 << drm_crtc_index(crtc);
  11513. struct intel_shared_dpll *pll = old_state->shared_dpll;
  11514. I915_STATE_WARN(pll->active_mask & crtc_mask,
  11515. "pll active mismatch (didn't expect pipe %c in active mask)\n",
  11516. pipe_name(drm_crtc_index(crtc)));
  11517. I915_STATE_WARN(pll->state.crtc_mask & crtc_mask,
  11518. "pll enabled crtcs mismatch (found %x in enabled mask)\n",
  11519. pipe_name(drm_crtc_index(crtc)));
  11520. }
  11521. }
  11522. static void
  11523. intel_modeset_verify_crtc(struct drm_crtc *crtc,
  11524. struct drm_atomic_state *state,
  11525. struct drm_crtc_state *old_state,
  11526. struct drm_crtc_state *new_state)
  11527. {
  11528. if (!needs_modeset(new_state) &&
  11529. !to_intel_crtc_state(new_state)->update_pipe)
  11530. return;
  11531. verify_wm_state(crtc, new_state);
  11532. verify_connector_state(crtc->dev, state, crtc);
  11533. verify_crtc_state(crtc, old_state, new_state);
  11534. verify_shared_dpll_state(crtc->dev, crtc, old_state, new_state);
  11535. }
  11536. static void
  11537. verify_disabled_dpll_state(struct drm_device *dev)
  11538. {
  11539. struct drm_i915_private *dev_priv = to_i915(dev);
  11540. int i;
  11541. for (i = 0; i < dev_priv->num_shared_dpll; i++)
  11542. verify_single_dpll_state(dev_priv, &dev_priv->shared_dplls[i], NULL, NULL);
  11543. }
  11544. static void
  11545. intel_modeset_verify_disabled(struct drm_device *dev,
  11546. struct drm_atomic_state *state)
  11547. {
  11548. verify_encoder_state(dev);
  11549. verify_connector_state(dev, state, NULL);
  11550. verify_disabled_dpll_state(dev);
  11551. }
  11552. static void update_scanline_offset(struct intel_crtc *crtc)
  11553. {
  11554. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  11555. /*
  11556. * The scanline counter increments at the leading edge of hsync.
  11557. *
  11558. * On most platforms it starts counting from vtotal-1 on the
  11559. * first active line. That means the scanline counter value is
  11560. * always one less than what we would expect. Ie. just after
  11561. * start of vblank, which also occurs at start of hsync (on the
  11562. * last active line), the scanline counter will read vblank_start-1.
  11563. *
  11564. * On gen2 the scanline counter starts counting from 1 instead
  11565. * of vtotal-1, so we have to subtract one (or rather add vtotal-1
  11566. * to keep the value positive), instead of adding one.
  11567. *
  11568. * On HSW+ the behaviour of the scanline counter depends on the output
  11569. * type. For DP ports it behaves like most other platforms, but on HDMI
  11570. * there's an extra 1 line difference. So we need to add two instead of
  11571. * one to the value.
  11572. */
  11573. if (IS_GEN2(dev_priv)) {
  11574. const struct drm_display_mode *adjusted_mode = &crtc->config->base.adjusted_mode;
  11575. int vtotal;
  11576. vtotal = adjusted_mode->crtc_vtotal;
  11577. if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE)
  11578. vtotal /= 2;
  11579. crtc->scanline_offset = vtotal - 1;
  11580. } else if (HAS_DDI(dev_priv) &&
  11581. intel_crtc_has_type(crtc->config, INTEL_OUTPUT_HDMI)) {
  11582. crtc->scanline_offset = 2;
  11583. } else
  11584. crtc->scanline_offset = 1;
  11585. }
  11586. static void intel_modeset_clear_plls(struct drm_atomic_state *state)
  11587. {
  11588. struct drm_device *dev = state->dev;
  11589. struct drm_i915_private *dev_priv = to_i915(dev);
  11590. struct drm_crtc *crtc;
  11591. struct drm_crtc_state *crtc_state;
  11592. int i;
  11593. if (!dev_priv->display.crtc_compute_clock)
  11594. return;
  11595. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11596. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11597. struct intel_shared_dpll *old_dpll =
  11598. to_intel_crtc_state(crtc->state)->shared_dpll;
  11599. if (!needs_modeset(crtc_state))
  11600. continue;
  11601. to_intel_crtc_state(crtc_state)->shared_dpll = NULL;
  11602. if (!old_dpll)
  11603. continue;
  11604. intel_release_shared_dpll(old_dpll, intel_crtc, state);
  11605. }
  11606. }
  11607. /*
  11608. * This implements the workaround described in the "notes" section of the mode
  11609. * set sequence documentation. When going from no pipes or single pipe to
  11610. * multiple pipes, and planes are enabled after the pipe, we need to wait at
  11611. * least 2 vblanks on the first pipe before enabling planes on the second pipe.
  11612. */
  11613. static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
  11614. {
  11615. struct drm_crtc_state *crtc_state;
  11616. struct intel_crtc *intel_crtc;
  11617. struct drm_crtc *crtc;
  11618. struct intel_crtc_state *first_crtc_state = NULL;
  11619. struct intel_crtc_state *other_crtc_state = NULL;
  11620. enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
  11621. int i;
  11622. /* look at all crtc's that are going to be enabled in during modeset */
  11623. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11624. intel_crtc = to_intel_crtc(crtc);
  11625. if (!crtc_state->active || !needs_modeset(crtc_state))
  11626. continue;
  11627. if (first_crtc_state) {
  11628. other_crtc_state = to_intel_crtc_state(crtc_state);
  11629. break;
  11630. } else {
  11631. first_crtc_state = to_intel_crtc_state(crtc_state);
  11632. first_pipe = intel_crtc->pipe;
  11633. }
  11634. }
  11635. /* No workaround needed? */
  11636. if (!first_crtc_state)
  11637. return 0;
  11638. /* w/a possibly needed, check how many crtc's are already enabled. */
  11639. for_each_intel_crtc(state->dev, intel_crtc) {
  11640. struct intel_crtc_state *pipe_config;
  11641. pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
  11642. if (IS_ERR(pipe_config))
  11643. return PTR_ERR(pipe_config);
  11644. pipe_config->hsw_workaround_pipe = INVALID_PIPE;
  11645. if (!pipe_config->base.active ||
  11646. needs_modeset(&pipe_config->base))
  11647. continue;
  11648. /* 2 or more enabled crtcs means no need for w/a */
  11649. if (enabled_pipe != INVALID_PIPE)
  11650. return 0;
  11651. enabled_pipe = intel_crtc->pipe;
  11652. }
  11653. if (enabled_pipe != INVALID_PIPE)
  11654. first_crtc_state->hsw_workaround_pipe = enabled_pipe;
  11655. else if (other_crtc_state)
  11656. other_crtc_state->hsw_workaround_pipe = first_pipe;
  11657. return 0;
  11658. }
  11659. static int intel_lock_all_pipes(struct drm_atomic_state *state)
  11660. {
  11661. struct drm_crtc *crtc;
  11662. /* Add all pipes to the state */
  11663. for_each_crtc(state->dev, crtc) {
  11664. struct drm_crtc_state *crtc_state;
  11665. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11666. if (IS_ERR(crtc_state))
  11667. return PTR_ERR(crtc_state);
  11668. }
  11669. return 0;
  11670. }
  11671. static int intel_modeset_all_pipes(struct drm_atomic_state *state)
  11672. {
  11673. struct drm_crtc *crtc;
  11674. /*
  11675. * Add all pipes to the state, and force
  11676. * a modeset on all the active ones.
  11677. */
  11678. for_each_crtc(state->dev, crtc) {
  11679. struct drm_crtc_state *crtc_state;
  11680. int ret;
  11681. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  11682. if (IS_ERR(crtc_state))
  11683. return PTR_ERR(crtc_state);
  11684. if (!crtc_state->active || needs_modeset(crtc_state))
  11685. continue;
  11686. crtc_state->mode_changed = true;
  11687. ret = drm_atomic_add_affected_connectors(state, crtc);
  11688. if (ret)
  11689. return ret;
  11690. ret = drm_atomic_add_affected_planes(state, crtc);
  11691. if (ret)
  11692. return ret;
  11693. }
  11694. return 0;
  11695. }
  11696. static int intel_modeset_checks(struct drm_atomic_state *state)
  11697. {
  11698. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11699. struct drm_i915_private *dev_priv = to_i915(state->dev);
  11700. struct drm_crtc *crtc;
  11701. struct drm_crtc_state *crtc_state;
  11702. int ret = 0, i;
  11703. if (!check_digital_port_conflicts(state)) {
  11704. DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
  11705. return -EINVAL;
  11706. }
  11707. intel_state->modeset = true;
  11708. intel_state->active_crtcs = dev_priv->active_crtcs;
  11709. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11710. if (crtc_state->active)
  11711. intel_state->active_crtcs |= 1 << i;
  11712. else
  11713. intel_state->active_crtcs &= ~(1 << i);
  11714. if (crtc_state->active != crtc->state->active)
  11715. intel_state->active_pipe_changes |= drm_crtc_mask(crtc);
  11716. }
  11717. /*
  11718. * See if the config requires any additional preparation, e.g.
  11719. * to adjust global state with pipes off. We need to do this
  11720. * here so we can get the modeset_pipe updated config for the new
  11721. * mode set on this crtc. For other crtcs we need to use the
  11722. * adjusted_mode bits in the crtc directly.
  11723. */
  11724. if (dev_priv->display.modeset_calc_cdclk) {
  11725. if (!intel_state->cdclk_pll_vco)
  11726. intel_state->cdclk_pll_vco = dev_priv->cdclk_pll.vco;
  11727. if (!intel_state->cdclk_pll_vco)
  11728. intel_state->cdclk_pll_vco = dev_priv->skl_preferred_vco_freq;
  11729. ret = dev_priv->display.modeset_calc_cdclk(state);
  11730. if (ret < 0)
  11731. return ret;
  11732. /*
  11733. * Writes to dev_priv->atomic_cdclk_freq must protected by
  11734. * holding all the crtc locks, even if we don't end up
  11735. * touching the hardware
  11736. */
  11737. if (intel_state->cdclk != dev_priv->atomic_cdclk_freq) {
  11738. ret = intel_lock_all_pipes(state);
  11739. if (ret < 0)
  11740. return ret;
  11741. }
  11742. /* All pipes must be switched off while we change the cdclk. */
  11743. if (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
  11744. intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco) {
  11745. ret = intel_modeset_all_pipes(state);
  11746. if (ret < 0)
  11747. return ret;
  11748. }
  11749. DRM_DEBUG_KMS("New cdclk calculated to be atomic %u, actual %u\n",
  11750. intel_state->cdclk, intel_state->dev_cdclk);
  11751. } else {
  11752. to_intel_atomic_state(state)->cdclk = dev_priv->atomic_cdclk_freq;
  11753. }
  11754. intel_modeset_clear_plls(state);
  11755. if (IS_HASWELL(dev_priv))
  11756. return haswell_mode_set_planes_workaround(state);
  11757. return 0;
  11758. }
  11759. /*
  11760. * Handle calculation of various watermark data at the end of the atomic check
  11761. * phase. The code here should be run after the per-crtc and per-plane 'check'
  11762. * handlers to ensure that all derived state has been updated.
  11763. */
  11764. static int calc_watermark_data(struct drm_atomic_state *state)
  11765. {
  11766. struct drm_device *dev = state->dev;
  11767. struct drm_i915_private *dev_priv = to_i915(dev);
  11768. /* Is there platform-specific watermark information to calculate? */
  11769. if (dev_priv->display.compute_global_watermarks)
  11770. return dev_priv->display.compute_global_watermarks(state);
  11771. return 0;
  11772. }
  11773. /**
  11774. * intel_atomic_check - validate state object
  11775. * @dev: drm device
  11776. * @state: state to validate
  11777. */
  11778. static int intel_atomic_check(struct drm_device *dev,
  11779. struct drm_atomic_state *state)
  11780. {
  11781. struct drm_i915_private *dev_priv = to_i915(dev);
  11782. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11783. struct drm_crtc *crtc;
  11784. struct drm_crtc_state *crtc_state;
  11785. int ret, i;
  11786. bool any_ms = false;
  11787. ret = drm_atomic_helper_check_modeset(dev, state);
  11788. if (ret)
  11789. return ret;
  11790. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11791. struct intel_crtc_state *pipe_config =
  11792. to_intel_crtc_state(crtc_state);
  11793. /* Catch I915_MODE_FLAG_INHERITED */
  11794. if (crtc_state->mode.private_flags != crtc->state->mode.private_flags)
  11795. crtc_state->mode_changed = true;
  11796. if (!needs_modeset(crtc_state))
  11797. continue;
  11798. if (!crtc_state->enable) {
  11799. any_ms = true;
  11800. continue;
  11801. }
  11802. /* FIXME: For only active_changed we shouldn't need to do any
  11803. * state recomputation at all. */
  11804. ret = drm_atomic_add_affected_connectors(state, crtc);
  11805. if (ret)
  11806. return ret;
  11807. ret = intel_modeset_pipe_config(crtc, pipe_config);
  11808. if (ret) {
  11809. intel_dump_pipe_config(to_intel_crtc(crtc),
  11810. pipe_config, "[failed]");
  11811. return ret;
  11812. }
  11813. if (i915.fastboot &&
  11814. intel_pipe_config_compare(dev_priv,
  11815. to_intel_crtc_state(crtc->state),
  11816. pipe_config, true)) {
  11817. crtc_state->mode_changed = false;
  11818. to_intel_crtc_state(crtc_state)->update_pipe = true;
  11819. }
  11820. if (needs_modeset(crtc_state))
  11821. any_ms = true;
  11822. ret = drm_atomic_add_affected_planes(state, crtc);
  11823. if (ret)
  11824. return ret;
  11825. intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
  11826. needs_modeset(crtc_state) ?
  11827. "[modeset]" : "[fastset]");
  11828. }
  11829. if (any_ms) {
  11830. ret = intel_modeset_checks(state);
  11831. if (ret)
  11832. return ret;
  11833. } else {
  11834. intel_state->cdclk = dev_priv->atomic_cdclk_freq;
  11835. }
  11836. ret = drm_atomic_helper_check_planes(dev, state);
  11837. if (ret)
  11838. return ret;
  11839. intel_fbc_choose_crtc(dev_priv, state);
  11840. return calc_watermark_data(state);
  11841. }
  11842. static int intel_atomic_prepare_commit(struct drm_device *dev,
  11843. struct drm_atomic_state *state)
  11844. {
  11845. struct drm_i915_private *dev_priv = to_i915(dev);
  11846. struct drm_crtc_state *crtc_state;
  11847. struct drm_crtc *crtc;
  11848. int i, ret;
  11849. for_each_crtc_in_state(state, crtc, crtc_state, i) {
  11850. if (state->legacy_cursor_update)
  11851. continue;
  11852. ret = intel_crtc_wait_for_pending_flips(crtc);
  11853. if (ret)
  11854. return ret;
  11855. if (atomic_read(&to_intel_crtc(crtc)->unpin_work_count) >= 2)
  11856. flush_workqueue(dev_priv->wq);
  11857. }
  11858. ret = mutex_lock_interruptible(&dev->struct_mutex);
  11859. if (ret)
  11860. return ret;
  11861. ret = drm_atomic_helper_prepare_planes(dev, state);
  11862. mutex_unlock(&dev->struct_mutex);
  11863. return ret;
  11864. }
  11865. u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
  11866. {
  11867. struct drm_device *dev = crtc->base.dev;
  11868. if (!dev->max_vblank_count)
  11869. return drm_accurate_vblank_count(&crtc->base);
  11870. return dev->driver->get_vblank_counter(dev, crtc->pipe);
  11871. }
  11872. static void intel_atomic_wait_for_vblanks(struct drm_device *dev,
  11873. struct drm_i915_private *dev_priv,
  11874. unsigned crtc_mask)
  11875. {
  11876. unsigned last_vblank_count[I915_MAX_PIPES];
  11877. enum pipe pipe;
  11878. int ret;
  11879. if (!crtc_mask)
  11880. return;
  11881. for_each_pipe(dev_priv, pipe) {
  11882. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  11883. pipe);
  11884. if (!((1 << pipe) & crtc_mask))
  11885. continue;
  11886. ret = drm_crtc_vblank_get(&crtc->base);
  11887. if (WARN_ON(ret != 0)) {
  11888. crtc_mask &= ~(1 << pipe);
  11889. continue;
  11890. }
  11891. last_vblank_count[pipe] = drm_crtc_vblank_count(&crtc->base);
  11892. }
  11893. for_each_pipe(dev_priv, pipe) {
  11894. struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
  11895. pipe);
  11896. long lret;
  11897. if (!((1 << pipe) & crtc_mask))
  11898. continue;
  11899. lret = wait_event_timeout(dev->vblank[pipe].queue,
  11900. last_vblank_count[pipe] !=
  11901. drm_crtc_vblank_count(&crtc->base),
  11902. msecs_to_jiffies(50));
  11903. WARN(!lret, "pipe %c vblank wait timed out\n", pipe_name(pipe));
  11904. drm_crtc_vblank_put(&crtc->base);
  11905. }
  11906. }
  11907. static bool needs_vblank_wait(struct intel_crtc_state *crtc_state)
  11908. {
  11909. /* fb updated, need to unpin old fb */
  11910. if (crtc_state->fb_changed)
  11911. return true;
  11912. /* wm changes, need vblank before final wm's */
  11913. if (crtc_state->update_wm_post)
  11914. return true;
  11915. /*
  11916. * cxsr is re-enabled after vblank.
  11917. * This is already handled by crtc_state->update_wm_post,
  11918. * but added for clarity.
  11919. */
  11920. if (crtc_state->disable_cxsr)
  11921. return true;
  11922. return false;
  11923. }
  11924. static void intel_update_crtc(struct drm_crtc *crtc,
  11925. struct drm_atomic_state *state,
  11926. struct drm_crtc_state *old_crtc_state,
  11927. unsigned int *crtc_vblank_mask)
  11928. {
  11929. struct drm_device *dev = crtc->dev;
  11930. struct drm_i915_private *dev_priv = to_i915(dev);
  11931. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  11932. struct intel_crtc_state *pipe_config = to_intel_crtc_state(crtc->state);
  11933. bool modeset = needs_modeset(crtc->state);
  11934. if (modeset) {
  11935. update_scanline_offset(intel_crtc);
  11936. dev_priv->display.crtc_enable(pipe_config, state);
  11937. } else {
  11938. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  11939. }
  11940. if (drm_atomic_get_existing_plane_state(state, crtc->primary)) {
  11941. intel_fbc_enable(
  11942. intel_crtc, pipe_config,
  11943. to_intel_plane_state(crtc->primary->state));
  11944. }
  11945. drm_atomic_helper_commit_planes_on_crtc(old_crtc_state);
  11946. if (needs_vblank_wait(pipe_config))
  11947. *crtc_vblank_mask |= drm_crtc_mask(crtc);
  11948. }
  11949. static void intel_update_crtcs(struct drm_atomic_state *state,
  11950. unsigned int *crtc_vblank_mask)
  11951. {
  11952. struct drm_crtc *crtc;
  11953. struct drm_crtc_state *old_crtc_state;
  11954. int i;
  11955. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11956. if (!crtc->state->active)
  11957. continue;
  11958. intel_update_crtc(crtc, state, old_crtc_state,
  11959. crtc_vblank_mask);
  11960. }
  11961. }
  11962. static void skl_update_crtcs(struct drm_atomic_state *state,
  11963. unsigned int *crtc_vblank_mask)
  11964. {
  11965. struct drm_i915_private *dev_priv = to_i915(state->dev);
  11966. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  11967. struct drm_crtc *crtc;
  11968. struct intel_crtc *intel_crtc;
  11969. struct drm_crtc_state *old_crtc_state;
  11970. struct intel_crtc_state *cstate;
  11971. unsigned int updated = 0;
  11972. bool progress;
  11973. enum pipe pipe;
  11974. int i;
  11975. const struct skl_ddb_entry *entries[I915_MAX_PIPES] = {};
  11976. for_each_crtc_in_state(state, crtc, old_crtc_state, i)
  11977. /* ignore allocations for crtc's that have been turned off. */
  11978. if (crtc->state->active)
  11979. entries[i] = &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb;
  11980. /*
  11981. * Whenever the number of active pipes changes, we need to make sure we
  11982. * update the pipes in the right order so that their ddb allocations
  11983. * never overlap with eachother inbetween CRTC updates. Otherwise we'll
  11984. * cause pipe underruns and other bad stuff.
  11985. */
  11986. do {
  11987. progress = false;
  11988. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  11989. bool vbl_wait = false;
  11990. unsigned int cmask = drm_crtc_mask(crtc);
  11991. intel_crtc = to_intel_crtc(crtc);
  11992. cstate = to_intel_crtc_state(crtc->state);
  11993. pipe = intel_crtc->pipe;
  11994. if (updated & cmask || !cstate->base.active)
  11995. continue;
  11996. if (skl_ddb_allocation_overlaps(entries, &cstate->wm.skl.ddb, i))
  11997. continue;
  11998. updated |= cmask;
  11999. entries[i] = &cstate->wm.skl.ddb;
  12000. /*
  12001. * If this is an already active pipe, it's DDB changed,
  12002. * and this isn't the last pipe that needs updating
  12003. * then we need to wait for a vblank to pass for the
  12004. * new ddb allocation to take effect.
  12005. */
  12006. if (!skl_ddb_entry_equal(&cstate->wm.skl.ddb,
  12007. &to_intel_crtc_state(old_crtc_state)->wm.skl.ddb) &&
  12008. !crtc->state->active_changed &&
  12009. intel_state->wm_results.dirty_pipes != updated)
  12010. vbl_wait = true;
  12011. intel_update_crtc(crtc, state, old_crtc_state,
  12012. crtc_vblank_mask);
  12013. if (vbl_wait)
  12014. intel_wait_for_vblank(dev_priv, pipe);
  12015. progress = true;
  12016. }
  12017. } while (progress);
  12018. }
  12019. static void intel_atomic_commit_tail(struct drm_atomic_state *state)
  12020. {
  12021. struct drm_device *dev = state->dev;
  12022. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  12023. struct drm_i915_private *dev_priv = to_i915(dev);
  12024. struct drm_crtc_state *old_crtc_state;
  12025. struct drm_crtc *crtc;
  12026. struct intel_crtc_state *intel_cstate;
  12027. bool hw_check = intel_state->modeset;
  12028. unsigned long put_domains[I915_MAX_PIPES] = {};
  12029. unsigned crtc_vblank_mask = 0;
  12030. int i;
  12031. drm_atomic_helper_wait_for_dependencies(state);
  12032. if (intel_state->modeset)
  12033. intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
  12034. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12035. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12036. if (needs_modeset(crtc->state) ||
  12037. to_intel_crtc_state(crtc->state)->update_pipe) {
  12038. hw_check = true;
  12039. put_domains[to_intel_crtc(crtc)->pipe] =
  12040. modeset_get_crtc_power_domains(crtc,
  12041. to_intel_crtc_state(crtc->state));
  12042. }
  12043. if (!needs_modeset(crtc->state))
  12044. continue;
  12045. intel_pre_plane_update(to_intel_crtc_state(old_crtc_state));
  12046. if (old_crtc_state->active) {
  12047. intel_crtc_disable_planes(crtc, old_crtc_state->plane_mask);
  12048. dev_priv->display.crtc_disable(to_intel_crtc_state(old_crtc_state), state);
  12049. intel_crtc->active = false;
  12050. intel_fbc_disable(intel_crtc);
  12051. intel_disable_shared_dpll(intel_crtc);
  12052. /*
  12053. * Underruns don't always raise
  12054. * interrupts, so check manually.
  12055. */
  12056. intel_check_cpu_fifo_underruns(dev_priv);
  12057. intel_check_pch_fifo_underruns(dev_priv);
  12058. if (!crtc->state->active) {
  12059. /*
  12060. * Make sure we don't call initial_watermarks
  12061. * for ILK-style watermark updates.
  12062. */
  12063. if (dev_priv->display.atomic_update_watermarks)
  12064. dev_priv->display.initial_watermarks(intel_state,
  12065. to_intel_crtc_state(crtc->state));
  12066. else
  12067. intel_update_watermarks(intel_crtc);
  12068. }
  12069. }
  12070. }
  12071. /* Only after disabling all output pipelines that will be changed can we
  12072. * update the the output configuration. */
  12073. intel_modeset_update_crtc_state(state);
  12074. if (intel_state->modeset) {
  12075. drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
  12076. if (dev_priv->display.modeset_commit_cdclk &&
  12077. (intel_state->dev_cdclk != dev_priv->cdclk_freq ||
  12078. intel_state->cdclk_pll_vco != dev_priv->cdclk_pll.vco))
  12079. dev_priv->display.modeset_commit_cdclk(state);
  12080. /*
  12081. * SKL workaround: bspec recommends we disable the SAGV when we
  12082. * have more then one pipe enabled
  12083. */
  12084. if (!intel_can_enable_sagv(state))
  12085. intel_disable_sagv(dev_priv);
  12086. intel_modeset_verify_disabled(dev, state);
  12087. }
  12088. /* Complete the events for pipes that have now been disabled */
  12089. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12090. bool modeset = needs_modeset(crtc->state);
  12091. /* Complete events for now disable pipes here. */
  12092. if (modeset && !crtc->state->active && crtc->state->event) {
  12093. spin_lock_irq(&dev->event_lock);
  12094. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  12095. spin_unlock_irq(&dev->event_lock);
  12096. crtc->state->event = NULL;
  12097. }
  12098. }
  12099. /* Now enable the clocks, plane, pipe, and connectors that we set up. */
  12100. dev_priv->display.update_crtcs(state, &crtc_vblank_mask);
  12101. /* FIXME: We should call drm_atomic_helper_commit_hw_done() here
  12102. * already, but still need the state for the delayed optimization. To
  12103. * fix this:
  12104. * - wrap the optimization/post_plane_update stuff into a per-crtc work.
  12105. * - schedule that vblank worker _before_ calling hw_done
  12106. * - at the start of commit_tail, cancel it _synchrously
  12107. * - switch over to the vblank wait helper in the core after that since
  12108. * we don't need out special handling any more.
  12109. */
  12110. if (!state->legacy_cursor_update)
  12111. intel_atomic_wait_for_vblanks(dev, dev_priv, crtc_vblank_mask);
  12112. /*
  12113. * Now that the vblank has passed, we can go ahead and program the
  12114. * optimal watermarks on platforms that need two-step watermark
  12115. * programming.
  12116. *
  12117. * TODO: Move this (and other cleanup) to an async worker eventually.
  12118. */
  12119. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12120. intel_cstate = to_intel_crtc_state(crtc->state);
  12121. if (dev_priv->display.optimize_watermarks)
  12122. dev_priv->display.optimize_watermarks(intel_state,
  12123. intel_cstate);
  12124. }
  12125. for_each_crtc_in_state(state, crtc, old_crtc_state, i) {
  12126. intel_post_plane_update(to_intel_crtc_state(old_crtc_state));
  12127. if (put_domains[i])
  12128. modeset_put_power_domains(dev_priv, put_domains[i]);
  12129. intel_modeset_verify_crtc(crtc, state, old_crtc_state, crtc->state);
  12130. }
  12131. if (intel_state->modeset && intel_can_enable_sagv(state))
  12132. intel_enable_sagv(dev_priv);
  12133. drm_atomic_helper_commit_hw_done(state);
  12134. if (intel_state->modeset)
  12135. intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET);
  12136. mutex_lock(&dev->struct_mutex);
  12137. drm_atomic_helper_cleanup_planes(dev, state);
  12138. mutex_unlock(&dev->struct_mutex);
  12139. drm_atomic_helper_commit_cleanup_done(state);
  12140. drm_atomic_state_put(state);
  12141. /* As one of the primary mmio accessors, KMS has a high likelihood
  12142. * of triggering bugs in unclaimed access. After we finish
  12143. * modesetting, see if an error has been flagged, and if so
  12144. * enable debugging for the next modeset - and hope we catch
  12145. * the culprit.
  12146. *
  12147. * XXX note that we assume display power is on at this point.
  12148. * This might hold true now but we need to add pm helper to check
  12149. * unclaimed only when the hardware is on, as atomic commits
  12150. * can happen also when the device is completely off.
  12151. */
  12152. intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
  12153. }
  12154. static void intel_atomic_commit_work(struct work_struct *work)
  12155. {
  12156. struct drm_atomic_state *state =
  12157. container_of(work, struct drm_atomic_state, commit_work);
  12158. intel_atomic_commit_tail(state);
  12159. }
  12160. static int __i915_sw_fence_call
  12161. intel_atomic_commit_ready(struct i915_sw_fence *fence,
  12162. enum i915_sw_fence_notify notify)
  12163. {
  12164. struct intel_atomic_state *state =
  12165. container_of(fence, struct intel_atomic_state, commit_ready);
  12166. switch (notify) {
  12167. case FENCE_COMPLETE:
  12168. if (state->base.commit_work.func)
  12169. queue_work(system_unbound_wq, &state->base.commit_work);
  12170. break;
  12171. case FENCE_FREE:
  12172. drm_atomic_state_put(&state->base);
  12173. break;
  12174. }
  12175. return NOTIFY_DONE;
  12176. }
  12177. static void intel_atomic_track_fbs(struct drm_atomic_state *state)
  12178. {
  12179. struct drm_plane_state *old_plane_state;
  12180. struct drm_plane *plane;
  12181. int i;
  12182. for_each_plane_in_state(state, plane, old_plane_state, i)
  12183. i915_gem_track_fb(intel_fb_obj(old_plane_state->fb),
  12184. intel_fb_obj(plane->state->fb),
  12185. to_intel_plane(plane)->frontbuffer_bit);
  12186. }
  12187. /**
  12188. * intel_atomic_commit - commit validated state object
  12189. * @dev: DRM device
  12190. * @state: the top-level driver state object
  12191. * @nonblock: nonblocking commit
  12192. *
  12193. * This function commits a top-level state object that has been validated
  12194. * with drm_atomic_helper_check().
  12195. *
  12196. * RETURNS
  12197. * Zero for success or -errno.
  12198. */
  12199. static int intel_atomic_commit(struct drm_device *dev,
  12200. struct drm_atomic_state *state,
  12201. bool nonblock)
  12202. {
  12203. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  12204. struct drm_i915_private *dev_priv = to_i915(dev);
  12205. int ret = 0;
  12206. ret = drm_atomic_helper_setup_commit(state, nonblock);
  12207. if (ret)
  12208. return ret;
  12209. drm_atomic_state_get(state);
  12210. i915_sw_fence_init(&intel_state->commit_ready,
  12211. intel_atomic_commit_ready);
  12212. ret = intel_atomic_prepare_commit(dev, state);
  12213. if (ret) {
  12214. DRM_DEBUG_ATOMIC("Preparing state failed with %i\n", ret);
  12215. i915_sw_fence_commit(&intel_state->commit_ready);
  12216. return ret;
  12217. }
  12218. drm_atomic_helper_swap_state(state, true);
  12219. dev_priv->wm.distrust_bios_wm = false;
  12220. intel_shared_dpll_swap_state(state);
  12221. intel_atomic_track_fbs(state);
  12222. if (intel_state->modeset) {
  12223. memcpy(dev_priv->min_pixclk, intel_state->min_pixclk,
  12224. sizeof(intel_state->min_pixclk));
  12225. dev_priv->active_crtcs = intel_state->active_crtcs;
  12226. dev_priv->atomic_cdclk_freq = intel_state->cdclk;
  12227. }
  12228. drm_atomic_state_get(state);
  12229. INIT_WORK(&state->commit_work,
  12230. nonblock ? intel_atomic_commit_work : NULL);
  12231. i915_sw_fence_commit(&intel_state->commit_ready);
  12232. if (!nonblock) {
  12233. i915_sw_fence_wait(&intel_state->commit_ready);
  12234. intel_atomic_commit_tail(state);
  12235. }
  12236. return 0;
  12237. }
  12238. void intel_crtc_restore_mode(struct drm_crtc *crtc)
  12239. {
  12240. struct drm_device *dev = crtc->dev;
  12241. struct drm_atomic_state *state;
  12242. struct drm_crtc_state *crtc_state;
  12243. int ret;
  12244. state = drm_atomic_state_alloc(dev);
  12245. if (!state) {
  12246. DRM_DEBUG_KMS("[CRTC:%d:%s] crtc restore failed, out of memory",
  12247. crtc->base.id, crtc->name);
  12248. return;
  12249. }
  12250. state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
  12251. retry:
  12252. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  12253. ret = PTR_ERR_OR_ZERO(crtc_state);
  12254. if (!ret) {
  12255. if (!crtc_state->active)
  12256. goto out;
  12257. crtc_state->mode_changed = true;
  12258. ret = drm_atomic_commit(state);
  12259. }
  12260. if (ret == -EDEADLK) {
  12261. drm_atomic_state_clear(state);
  12262. drm_modeset_backoff(state->acquire_ctx);
  12263. goto retry;
  12264. }
  12265. out:
  12266. drm_atomic_state_put(state);
  12267. }
  12268. /*
  12269. * FIXME: Remove this once i915 is fully DRIVER_ATOMIC by calling
  12270. * drm_atomic_helper_legacy_gamma_set() directly.
  12271. */
  12272. static int intel_atomic_legacy_gamma_set(struct drm_crtc *crtc,
  12273. u16 *red, u16 *green, u16 *blue,
  12274. uint32_t size)
  12275. {
  12276. struct drm_device *dev = crtc->dev;
  12277. struct drm_mode_config *config = &dev->mode_config;
  12278. struct drm_crtc_state *state;
  12279. int ret;
  12280. ret = drm_atomic_helper_legacy_gamma_set(crtc, red, green, blue, size);
  12281. if (ret)
  12282. return ret;
  12283. /*
  12284. * Make sure we update the legacy properties so this works when
  12285. * atomic is not enabled.
  12286. */
  12287. state = crtc->state;
  12288. drm_object_property_set_value(&crtc->base,
  12289. config->degamma_lut_property,
  12290. (state->degamma_lut) ?
  12291. state->degamma_lut->base.id : 0);
  12292. drm_object_property_set_value(&crtc->base,
  12293. config->ctm_property,
  12294. (state->ctm) ?
  12295. state->ctm->base.id : 0);
  12296. drm_object_property_set_value(&crtc->base,
  12297. config->gamma_lut_property,
  12298. (state->gamma_lut) ?
  12299. state->gamma_lut->base.id : 0);
  12300. return 0;
  12301. }
  12302. static const struct drm_crtc_funcs intel_crtc_funcs = {
  12303. .gamma_set = intel_atomic_legacy_gamma_set,
  12304. .set_config = drm_atomic_helper_set_config,
  12305. .set_property = drm_atomic_helper_crtc_set_property,
  12306. .destroy = intel_crtc_destroy,
  12307. .page_flip = intel_crtc_page_flip,
  12308. .atomic_duplicate_state = intel_crtc_duplicate_state,
  12309. .atomic_destroy_state = intel_crtc_destroy_state,
  12310. .set_crc_source = intel_crtc_set_crc_source,
  12311. };
  12312. /**
  12313. * intel_prepare_plane_fb - Prepare fb for usage on plane
  12314. * @plane: drm plane to prepare for
  12315. * @fb: framebuffer to prepare for presentation
  12316. *
  12317. * Prepares a framebuffer for usage on a display plane. Generally this
  12318. * involves pinning the underlying object and updating the frontbuffer tracking
  12319. * bits. Some older platforms need special physical address handling for
  12320. * cursor planes.
  12321. *
  12322. * Must be called with struct_mutex held.
  12323. *
  12324. * Returns 0 on success, negative error code on failure.
  12325. */
  12326. int
  12327. intel_prepare_plane_fb(struct drm_plane *plane,
  12328. struct drm_plane_state *new_state)
  12329. {
  12330. struct intel_atomic_state *intel_state =
  12331. to_intel_atomic_state(new_state->state);
  12332. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  12333. struct drm_framebuffer *fb = new_state->fb;
  12334. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  12335. struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->state->fb);
  12336. int ret;
  12337. if (!obj && !old_obj)
  12338. return 0;
  12339. if (old_obj) {
  12340. struct drm_crtc_state *crtc_state =
  12341. drm_atomic_get_existing_crtc_state(new_state->state,
  12342. plane->state->crtc);
  12343. /* Big Hammer, we also need to ensure that any pending
  12344. * MI_WAIT_FOR_EVENT inside a user batch buffer on the
  12345. * current scanout is retired before unpinning the old
  12346. * framebuffer. Note that we rely on userspace rendering
  12347. * into the buffer attached to the pipe they are waiting
  12348. * on. If not, userspace generates a GPU hang with IPEHR
  12349. * point to the MI_WAIT_FOR_EVENT.
  12350. *
  12351. * This should only fail upon a hung GPU, in which case we
  12352. * can safely continue.
  12353. */
  12354. if (needs_modeset(crtc_state)) {
  12355. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  12356. old_obj->resv, NULL,
  12357. false, 0,
  12358. GFP_KERNEL);
  12359. if (ret < 0)
  12360. return ret;
  12361. }
  12362. }
  12363. if (new_state->fence) { /* explicit fencing */
  12364. ret = i915_sw_fence_await_dma_fence(&intel_state->commit_ready,
  12365. new_state->fence,
  12366. I915_FENCE_TIMEOUT,
  12367. GFP_KERNEL);
  12368. if (ret < 0)
  12369. return ret;
  12370. }
  12371. if (!obj)
  12372. return 0;
  12373. if (!new_state->fence) { /* implicit fencing */
  12374. ret = i915_sw_fence_await_reservation(&intel_state->commit_ready,
  12375. obj->resv, NULL,
  12376. false, I915_FENCE_TIMEOUT,
  12377. GFP_KERNEL);
  12378. if (ret < 0)
  12379. return ret;
  12380. i915_gem_object_wait_priority(obj, 0, I915_PRIORITY_DISPLAY);
  12381. }
  12382. if (plane->type == DRM_PLANE_TYPE_CURSOR &&
  12383. INTEL_INFO(dev_priv)->cursor_needs_physical) {
  12384. int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
  12385. ret = i915_gem_object_attach_phys(obj, align);
  12386. if (ret) {
  12387. DRM_DEBUG_KMS("failed to attach phys object\n");
  12388. return ret;
  12389. }
  12390. } else {
  12391. struct i915_vma *vma;
  12392. vma = intel_pin_and_fence_fb_obj(fb, new_state->rotation);
  12393. if (IS_ERR(vma)) {
  12394. DRM_DEBUG_KMS("failed to pin object\n");
  12395. return PTR_ERR(vma);
  12396. }
  12397. to_intel_plane_state(new_state)->vma = vma;
  12398. }
  12399. return 0;
  12400. }
  12401. /**
  12402. * intel_cleanup_plane_fb - Cleans up an fb after plane use
  12403. * @plane: drm plane to clean up for
  12404. * @fb: old framebuffer that was on plane
  12405. *
  12406. * Cleans up a framebuffer that has just been removed from a plane.
  12407. *
  12408. * Must be called with struct_mutex held.
  12409. */
  12410. void
  12411. intel_cleanup_plane_fb(struct drm_plane *plane,
  12412. struct drm_plane_state *old_state)
  12413. {
  12414. struct i915_vma *vma;
  12415. /* Should only be called after a successful intel_prepare_plane_fb()! */
  12416. vma = fetch_and_zero(&to_intel_plane_state(old_state)->vma);
  12417. if (vma)
  12418. intel_unpin_fb_vma(vma);
  12419. }
  12420. int
  12421. skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
  12422. {
  12423. int max_scale;
  12424. int crtc_clock, cdclk;
  12425. if (!intel_crtc || !crtc_state->base.enable)
  12426. return DRM_PLANE_HELPER_NO_SCALING;
  12427. crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
  12428. cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
  12429. if (WARN_ON_ONCE(!crtc_clock || cdclk < crtc_clock))
  12430. return DRM_PLANE_HELPER_NO_SCALING;
  12431. /*
  12432. * skl max scale is lower of:
  12433. * close to 3 but not 3, -1 is for that purpose
  12434. * or
  12435. * cdclk/crtc_clock
  12436. */
  12437. max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
  12438. return max_scale;
  12439. }
  12440. static int
  12441. intel_check_primary_plane(struct drm_plane *plane,
  12442. struct intel_crtc_state *crtc_state,
  12443. struct intel_plane_state *state)
  12444. {
  12445. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  12446. struct drm_crtc *crtc = state->base.crtc;
  12447. int min_scale = DRM_PLANE_HELPER_NO_SCALING;
  12448. int max_scale = DRM_PLANE_HELPER_NO_SCALING;
  12449. bool can_position = false;
  12450. int ret;
  12451. if (INTEL_GEN(dev_priv) >= 9) {
  12452. /* use scaler when colorkey is not required */
  12453. if (state->ckey.flags == I915_SET_COLORKEY_NONE) {
  12454. min_scale = 1;
  12455. max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
  12456. }
  12457. can_position = true;
  12458. }
  12459. ret = drm_plane_helper_check_state(&state->base,
  12460. &state->clip,
  12461. min_scale, max_scale,
  12462. can_position, true);
  12463. if (ret)
  12464. return ret;
  12465. if (!state->base.fb)
  12466. return 0;
  12467. if (INTEL_GEN(dev_priv) >= 9) {
  12468. ret = skl_check_plane_surface(state);
  12469. if (ret)
  12470. return ret;
  12471. }
  12472. return 0;
  12473. }
  12474. static void intel_begin_crtc_commit(struct drm_crtc *crtc,
  12475. struct drm_crtc_state *old_crtc_state)
  12476. {
  12477. struct drm_device *dev = crtc->dev;
  12478. struct drm_i915_private *dev_priv = to_i915(dev);
  12479. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12480. struct intel_crtc_state *intel_cstate =
  12481. to_intel_crtc_state(crtc->state);
  12482. struct intel_crtc_state *old_intel_cstate =
  12483. to_intel_crtc_state(old_crtc_state);
  12484. struct intel_atomic_state *old_intel_state =
  12485. to_intel_atomic_state(old_crtc_state->state);
  12486. bool modeset = needs_modeset(crtc->state);
  12487. /* Perform vblank evasion around commit operation */
  12488. intel_pipe_update_start(intel_crtc);
  12489. if (modeset)
  12490. goto out;
  12491. if (crtc->state->color_mgmt_changed || to_intel_crtc_state(crtc->state)->update_pipe) {
  12492. intel_color_set_csc(crtc->state);
  12493. intel_color_load_luts(crtc->state);
  12494. }
  12495. if (intel_cstate->update_pipe)
  12496. intel_update_pipe_config(intel_crtc, old_intel_cstate);
  12497. else if (INTEL_GEN(dev_priv) >= 9)
  12498. skl_detach_scalers(intel_crtc);
  12499. out:
  12500. if (dev_priv->display.atomic_update_watermarks)
  12501. dev_priv->display.atomic_update_watermarks(old_intel_state,
  12502. intel_cstate);
  12503. }
  12504. static void intel_finish_crtc_commit(struct drm_crtc *crtc,
  12505. struct drm_crtc_state *old_crtc_state)
  12506. {
  12507. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12508. intel_pipe_update_end(intel_crtc, NULL);
  12509. }
  12510. /**
  12511. * intel_plane_destroy - destroy a plane
  12512. * @plane: plane to destroy
  12513. *
  12514. * Common destruction function for all types of planes (primary, cursor,
  12515. * sprite).
  12516. */
  12517. void intel_plane_destroy(struct drm_plane *plane)
  12518. {
  12519. drm_plane_cleanup(plane);
  12520. kfree(to_intel_plane(plane));
  12521. }
  12522. const struct drm_plane_funcs intel_plane_funcs = {
  12523. .update_plane = drm_atomic_helper_update_plane,
  12524. .disable_plane = drm_atomic_helper_disable_plane,
  12525. .destroy = intel_plane_destroy,
  12526. .set_property = drm_atomic_helper_plane_set_property,
  12527. .atomic_get_property = intel_plane_atomic_get_property,
  12528. .atomic_set_property = intel_plane_atomic_set_property,
  12529. .atomic_duplicate_state = intel_plane_duplicate_state,
  12530. .atomic_destroy_state = intel_plane_destroy_state,
  12531. };
  12532. static int
  12533. intel_legacy_cursor_update(struct drm_plane *plane,
  12534. struct drm_crtc *crtc,
  12535. struct drm_framebuffer *fb,
  12536. int crtc_x, int crtc_y,
  12537. unsigned int crtc_w, unsigned int crtc_h,
  12538. uint32_t src_x, uint32_t src_y,
  12539. uint32_t src_w, uint32_t src_h)
  12540. {
  12541. struct drm_i915_private *dev_priv = to_i915(crtc->dev);
  12542. int ret;
  12543. struct drm_plane_state *old_plane_state, *new_plane_state;
  12544. struct intel_plane *intel_plane = to_intel_plane(plane);
  12545. struct drm_framebuffer *old_fb;
  12546. struct drm_crtc_state *crtc_state = crtc->state;
  12547. struct i915_vma *old_vma;
  12548. /*
  12549. * When crtc is inactive or there is a modeset pending,
  12550. * wait for it to complete in the slowpath
  12551. */
  12552. if (!crtc_state->active || needs_modeset(crtc_state) ||
  12553. to_intel_crtc_state(crtc_state)->update_pipe)
  12554. goto slow;
  12555. old_plane_state = plane->state;
  12556. /*
  12557. * If any parameters change that may affect watermarks,
  12558. * take the slowpath. Only changing fb or position should be
  12559. * in the fastpath.
  12560. */
  12561. if (old_plane_state->crtc != crtc ||
  12562. old_plane_state->src_w != src_w ||
  12563. old_plane_state->src_h != src_h ||
  12564. old_plane_state->crtc_w != crtc_w ||
  12565. old_plane_state->crtc_h != crtc_h ||
  12566. !old_plane_state->visible ||
  12567. old_plane_state->fb->modifier != fb->modifier)
  12568. goto slow;
  12569. new_plane_state = intel_plane_duplicate_state(plane);
  12570. if (!new_plane_state)
  12571. return -ENOMEM;
  12572. drm_atomic_set_fb_for_plane(new_plane_state, fb);
  12573. new_plane_state->src_x = src_x;
  12574. new_plane_state->src_y = src_y;
  12575. new_plane_state->src_w = src_w;
  12576. new_plane_state->src_h = src_h;
  12577. new_plane_state->crtc_x = crtc_x;
  12578. new_plane_state->crtc_y = crtc_y;
  12579. new_plane_state->crtc_w = crtc_w;
  12580. new_plane_state->crtc_h = crtc_h;
  12581. ret = intel_plane_atomic_check_with_state(to_intel_crtc_state(crtc->state),
  12582. to_intel_plane_state(new_plane_state));
  12583. if (ret)
  12584. goto out_free;
  12585. /* Visibility changed, must take slowpath. */
  12586. if (!new_plane_state->visible)
  12587. goto slow_free;
  12588. ret = mutex_lock_interruptible(&dev_priv->drm.struct_mutex);
  12589. if (ret)
  12590. goto out_free;
  12591. if (INTEL_INFO(dev_priv)->cursor_needs_physical) {
  12592. int align = IS_I830(dev_priv) ? 16 * 1024 : 256;
  12593. ret = i915_gem_object_attach_phys(intel_fb_obj(fb), align);
  12594. if (ret) {
  12595. DRM_DEBUG_KMS("failed to attach phys object\n");
  12596. goto out_unlock;
  12597. }
  12598. } else {
  12599. struct i915_vma *vma;
  12600. vma = intel_pin_and_fence_fb_obj(fb, new_plane_state->rotation);
  12601. if (IS_ERR(vma)) {
  12602. DRM_DEBUG_KMS("failed to pin object\n");
  12603. ret = PTR_ERR(vma);
  12604. goto out_unlock;
  12605. }
  12606. to_intel_plane_state(new_plane_state)->vma = vma;
  12607. }
  12608. old_fb = old_plane_state->fb;
  12609. old_vma = to_intel_plane_state(old_plane_state)->vma;
  12610. i915_gem_track_fb(intel_fb_obj(old_fb), intel_fb_obj(fb),
  12611. intel_plane->frontbuffer_bit);
  12612. /* Swap plane state */
  12613. new_plane_state->fence = old_plane_state->fence;
  12614. *to_intel_plane_state(old_plane_state) = *to_intel_plane_state(new_plane_state);
  12615. new_plane_state->fence = NULL;
  12616. new_plane_state->fb = old_fb;
  12617. to_intel_plane_state(new_plane_state)->vma = old_vma;
  12618. intel_plane->update_plane(plane,
  12619. to_intel_crtc_state(crtc->state),
  12620. to_intel_plane_state(plane->state));
  12621. intel_cleanup_plane_fb(plane, new_plane_state);
  12622. out_unlock:
  12623. mutex_unlock(&dev_priv->drm.struct_mutex);
  12624. out_free:
  12625. intel_plane_destroy_state(plane, new_plane_state);
  12626. return ret;
  12627. slow_free:
  12628. intel_plane_destroy_state(plane, new_plane_state);
  12629. slow:
  12630. return drm_atomic_helper_update_plane(plane, crtc, fb,
  12631. crtc_x, crtc_y, crtc_w, crtc_h,
  12632. src_x, src_y, src_w, src_h);
  12633. }
  12634. static const struct drm_plane_funcs intel_cursor_plane_funcs = {
  12635. .update_plane = intel_legacy_cursor_update,
  12636. .disable_plane = drm_atomic_helper_disable_plane,
  12637. .destroy = intel_plane_destroy,
  12638. .set_property = drm_atomic_helper_plane_set_property,
  12639. .atomic_get_property = intel_plane_atomic_get_property,
  12640. .atomic_set_property = intel_plane_atomic_set_property,
  12641. .atomic_duplicate_state = intel_plane_duplicate_state,
  12642. .atomic_destroy_state = intel_plane_destroy_state,
  12643. };
  12644. static struct intel_plane *
  12645. intel_primary_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  12646. {
  12647. struct intel_plane *primary = NULL;
  12648. struct intel_plane_state *state = NULL;
  12649. const uint32_t *intel_primary_formats;
  12650. unsigned int supported_rotations;
  12651. unsigned int num_formats;
  12652. int ret;
  12653. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  12654. if (!primary) {
  12655. ret = -ENOMEM;
  12656. goto fail;
  12657. }
  12658. state = intel_create_plane_state(&primary->base);
  12659. if (!state) {
  12660. ret = -ENOMEM;
  12661. goto fail;
  12662. }
  12663. primary->base.state = &state->base;
  12664. primary->can_scale = false;
  12665. primary->max_downscale = 1;
  12666. if (INTEL_GEN(dev_priv) >= 9) {
  12667. primary->can_scale = true;
  12668. state->scaler_id = -1;
  12669. }
  12670. primary->pipe = pipe;
  12671. /*
  12672. * On gen2/3 only plane A can do FBC, but the panel fitter and LVDS
  12673. * port is hooked to pipe B. Hence we want plane A feeding pipe B.
  12674. */
  12675. if (HAS_FBC(dev_priv) && INTEL_GEN(dev_priv) < 4)
  12676. primary->plane = (enum plane) !pipe;
  12677. else
  12678. primary->plane = (enum plane) pipe;
  12679. primary->id = PLANE_PRIMARY;
  12680. primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
  12681. primary->check_plane = intel_check_primary_plane;
  12682. if (INTEL_GEN(dev_priv) >= 9) {
  12683. intel_primary_formats = skl_primary_formats;
  12684. num_formats = ARRAY_SIZE(skl_primary_formats);
  12685. primary->update_plane = skylake_update_primary_plane;
  12686. primary->disable_plane = skylake_disable_primary_plane;
  12687. } else if (HAS_PCH_SPLIT(dev_priv)) {
  12688. intel_primary_formats = i965_primary_formats;
  12689. num_formats = ARRAY_SIZE(i965_primary_formats);
  12690. primary->update_plane = ironlake_update_primary_plane;
  12691. primary->disable_plane = i9xx_disable_primary_plane;
  12692. } else if (INTEL_GEN(dev_priv) >= 4) {
  12693. intel_primary_formats = i965_primary_formats;
  12694. num_formats = ARRAY_SIZE(i965_primary_formats);
  12695. primary->update_plane = i9xx_update_primary_plane;
  12696. primary->disable_plane = i9xx_disable_primary_plane;
  12697. } else {
  12698. intel_primary_formats = i8xx_primary_formats;
  12699. num_formats = ARRAY_SIZE(i8xx_primary_formats);
  12700. primary->update_plane = i9xx_update_primary_plane;
  12701. primary->disable_plane = i9xx_disable_primary_plane;
  12702. }
  12703. if (INTEL_GEN(dev_priv) >= 9)
  12704. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  12705. 0, &intel_plane_funcs,
  12706. intel_primary_formats, num_formats,
  12707. DRM_PLANE_TYPE_PRIMARY,
  12708. "plane 1%c", pipe_name(pipe));
  12709. else if (INTEL_GEN(dev_priv) >= 5 || IS_G4X(dev_priv))
  12710. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  12711. 0, &intel_plane_funcs,
  12712. intel_primary_formats, num_formats,
  12713. DRM_PLANE_TYPE_PRIMARY,
  12714. "primary %c", pipe_name(pipe));
  12715. else
  12716. ret = drm_universal_plane_init(&dev_priv->drm, &primary->base,
  12717. 0, &intel_plane_funcs,
  12718. intel_primary_formats, num_formats,
  12719. DRM_PLANE_TYPE_PRIMARY,
  12720. "plane %c", plane_name(primary->plane));
  12721. if (ret)
  12722. goto fail;
  12723. if (INTEL_GEN(dev_priv) >= 9) {
  12724. supported_rotations =
  12725. DRM_ROTATE_0 | DRM_ROTATE_90 |
  12726. DRM_ROTATE_180 | DRM_ROTATE_270;
  12727. } else if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
  12728. supported_rotations =
  12729. DRM_ROTATE_0 | DRM_ROTATE_180 |
  12730. DRM_REFLECT_X;
  12731. } else if (INTEL_GEN(dev_priv) >= 4) {
  12732. supported_rotations =
  12733. DRM_ROTATE_0 | DRM_ROTATE_180;
  12734. } else {
  12735. supported_rotations = DRM_ROTATE_0;
  12736. }
  12737. if (INTEL_GEN(dev_priv) >= 4)
  12738. drm_plane_create_rotation_property(&primary->base,
  12739. DRM_ROTATE_0,
  12740. supported_rotations);
  12741. drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
  12742. return primary;
  12743. fail:
  12744. kfree(state);
  12745. kfree(primary);
  12746. return ERR_PTR(ret);
  12747. }
  12748. static int
  12749. intel_check_cursor_plane(struct drm_plane *plane,
  12750. struct intel_crtc_state *crtc_state,
  12751. struct intel_plane_state *state)
  12752. {
  12753. struct drm_framebuffer *fb = state->base.fb;
  12754. struct drm_i915_gem_object *obj = intel_fb_obj(fb);
  12755. enum pipe pipe = to_intel_plane(plane)->pipe;
  12756. unsigned stride;
  12757. int ret;
  12758. ret = drm_plane_helper_check_state(&state->base,
  12759. &state->clip,
  12760. DRM_PLANE_HELPER_NO_SCALING,
  12761. DRM_PLANE_HELPER_NO_SCALING,
  12762. true, true);
  12763. if (ret)
  12764. return ret;
  12765. /* if we want to turn off the cursor ignore width and height */
  12766. if (!obj)
  12767. return 0;
  12768. /* Check for which cursor types we support */
  12769. if (!cursor_size_ok(to_i915(plane->dev), state->base.crtc_w,
  12770. state->base.crtc_h)) {
  12771. DRM_DEBUG("Cursor dimension %dx%d not supported\n",
  12772. state->base.crtc_w, state->base.crtc_h);
  12773. return -EINVAL;
  12774. }
  12775. stride = roundup_pow_of_two(state->base.crtc_w) * 4;
  12776. if (obj->base.size < stride * state->base.crtc_h) {
  12777. DRM_DEBUG_KMS("buffer is too small\n");
  12778. return -ENOMEM;
  12779. }
  12780. if (fb->modifier != DRM_FORMAT_MOD_NONE) {
  12781. DRM_DEBUG_KMS("cursor cannot be tiled\n");
  12782. return -EINVAL;
  12783. }
  12784. /*
  12785. * There's something wrong with the cursor on CHV pipe C.
  12786. * If it straddles the left edge of the screen then
  12787. * moving it away from the edge or disabling it often
  12788. * results in a pipe underrun, and often that can lead to
  12789. * dead pipe (constant underrun reported, and it scans
  12790. * out just a solid color). To recover from that, the
  12791. * display power well must be turned off and on again.
  12792. * Refuse the put the cursor into that compromised position.
  12793. */
  12794. if (IS_CHERRYVIEW(to_i915(plane->dev)) && pipe == PIPE_C &&
  12795. state->base.visible && state->base.crtc_x < 0) {
  12796. DRM_DEBUG_KMS("CHV cursor C not allowed to straddle the left screen edge\n");
  12797. return -EINVAL;
  12798. }
  12799. return 0;
  12800. }
  12801. static void
  12802. intel_disable_cursor_plane(struct drm_plane *plane,
  12803. struct drm_crtc *crtc)
  12804. {
  12805. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12806. intel_crtc->cursor_addr = 0;
  12807. intel_crtc_update_cursor(crtc, NULL);
  12808. }
  12809. static void
  12810. intel_update_cursor_plane(struct drm_plane *plane,
  12811. const struct intel_crtc_state *crtc_state,
  12812. const struct intel_plane_state *state)
  12813. {
  12814. struct drm_crtc *crtc = crtc_state->base.crtc;
  12815. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  12816. struct drm_i915_private *dev_priv = to_i915(plane->dev);
  12817. struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
  12818. uint32_t addr;
  12819. if (!obj)
  12820. addr = 0;
  12821. else if (!INTEL_INFO(dev_priv)->cursor_needs_physical)
  12822. addr = intel_plane_ggtt_offset(state);
  12823. else
  12824. addr = obj->phys_handle->busaddr;
  12825. intel_crtc->cursor_addr = addr;
  12826. intel_crtc_update_cursor(crtc, state);
  12827. }
  12828. static struct intel_plane *
  12829. intel_cursor_plane_create(struct drm_i915_private *dev_priv, enum pipe pipe)
  12830. {
  12831. struct intel_plane *cursor = NULL;
  12832. struct intel_plane_state *state = NULL;
  12833. int ret;
  12834. cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
  12835. if (!cursor) {
  12836. ret = -ENOMEM;
  12837. goto fail;
  12838. }
  12839. state = intel_create_plane_state(&cursor->base);
  12840. if (!state) {
  12841. ret = -ENOMEM;
  12842. goto fail;
  12843. }
  12844. cursor->base.state = &state->base;
  12845. cursor->can_scale = false;
  12846. cursor->max_downscale = 1;
  12847. cursor->pipe = pipe;
  12848. cursor->plane = pipe;
  12849. cursor->id = PLANE_CURSOR;
  12850. cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
  12851. cursor->check_plane = intel_check_cursor_plane;
  12852. cursor->update_plane = intel_update_cursor_plane;
  12853. cursor->disable_plane = intel_disable_cursor_plane;
  12854. ret = drm_universal_plane_init(&dev_priv->drm, &cursor->base,
  12855. 0, &intel_cursor_plane_funcs,
  12856. intel_cursor_formats,
  12857. ARRAY_SIZE(intel_cursor_formats),
  12858. DRM_PLANE_TYPE_CURSOR,
  12859. "cursor %c", pipe_name(pipe));
  12860. if (ret)
  12861. goto fail;
  12862. if (INTEL_GEN(dev_priv) >= 4)
  12863. drm_plane_create_rotation_property(&cursor->base,
  12864. DRM_ROTATE_0,
  12865. DRM_ROTATE_0 |
  12866. DRM_ROTATE_180);
  12867. if (INTEL_GEN(dev_priv) >= 9)
  12868. state->scaler_id = -1;
  12869. drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
  12870. return cursor;
  12871. fail:
  12872. kfree(state);
  12873. kfree(cursor);
  12874. return ERR_PTR(ret);
  12875. }
  12876. static void intel_crtc_init_scalers(struct intel_crtc *crtc,
  12877. struct intel_crtc_state *crtc_state)
  12878. {
  12879. struct intel_crtc_scaler_state *scaler_state =
  12880. &crtc_state->scaler_state;
  12881. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  12882. int i;
  12883. crtc->num_scalers = dev_priv->info.num_scalers[crtc->pipe];
  12884. if (!crtc->num_scalers)
  12885. return;
  12886. for (i = 0; i < crtc->num_scalers; i++) {
  12887. struct intel_scaler *scaler = &scaler_state->scalers[i];
  12888. scaler->in_use = 0;
  12889. scaler->mode = PS_SCALER_MODE_DYN;
  12890. }
  12891. scaler_state->scaler_id = -1;
  12892. }
  12893. static int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
  12894. {
  12895. struct intel_crtc *intel_crtc;
  12896. struct intel_crtc_state *crtc_state = NULL;
  12897. struct intel_plane *primary = NULL;
  12898. struct intel_plane *cursor = NULL;
  12899. int sprite, ret;
  12900. intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
  12901. if (!intel_crtc)
  12902. return -ENOMEM;
  12903. crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
  12904. if (!crtc_state) {
  12905. ret = -ENOMEM;
  12906. goto fail;
  12907. }
  12908. intel_crtc->config = crtc_state;
  12909. intel_crtc->base.state = &crtc_state->base;
  12910. crtc_state->base.crtc = &intel_crtc->base;
  12911. primary = intel_primary_plane_create(dev_priv, pipe);
  12912. if (IS_ERR(primary)) {
  12913. ret = PTR_ERR(primary);
  12914. goto fail;
  12915. }
  12916. intel_crtc->plane_ids_mask |= BIT(primary->id);
  12917. for_each_sprite(dev_priv, pipe, sprite) {
  12918. struct intel_plane *plane;
  12919. plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
  12920. if (IS_ERR(plane)) {
  12921. ret = PTR_ERR(plane);
  12922. goto fail;
  12923. }
  12924. intel_crtc->plane_ids_mask |= BIT(plane->id);
  12925. }
  12926. cursor = intel_cursor_plane_create(dev_priv, pipe);
  12927. if (IS_ERR(cursor)) {
  12928. ret = PTR_ERR(cursor);
  12929. goto fail;
  12930. }
  12931. intel_crtc->plane_ids_mask |= BIT(cursor->id);
  12932. ret = drm_crtc_init_with_planes(&dev_priv->drm, &intel_crtc->base,
  12933. &primary->base, &cursor->base,
  12934. &intel_crtc_funcs,
  12935. "pipe %c", pipe_name(pipe));
  12936. if (ret)
  12937. goto fail;
  12938. intel_crtc->pipe = pipe;
  12939. intel_crtc->plane = primary->plane;
  12940. intel_crtc->cursor_base = ~0;
  12941. intel_crtc->cursor_cntl = ~0;
  12942. intel_crtc->cursor_size = ~0;
  12943. intel_crtc->wm.cxsr_allowed = true;
  12944. /* initialize shared scalers */
  12945. intel_crtc_init_scalers(intel_crtc, crtc_state);
  12946. BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
  12947. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
  12948. dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = intel_crtc;
  12949. dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = intel_crtc;
  12950. drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
  12951. intel_color_init(&intel_crtc->base);
  12952. WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
  12953. return 0;
  12954. fail:
  12955. /*
  12956. * drm_mode_config_cleanup() will free up any
  12957. * crtcs/planes already initialized.
  12958. */
  12959. kfree(crtc_state);
  12960. kfree(intel_crtc);
  12961. return ret;
  12962. }
  12963. enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
  12964. {
  12965. struct drm_encoder *encoder = connector->base.encoder;
  12966. struct drm_device *dev = connector->base.dev;
  12967. WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  12968. if (!encoder || WARN_ON(!encoder->crtc))
  12969. return INVALID_PIPE;
  12970. return to_intel_crtc(encoder->crtc)->pipe;
  12971. }
  12972. int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
  12973. struct drm_file *file)
  12974. {
  12975. struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
  12976. struct drm_crtc *drmmode_crtc;
  12977. struct intel_crtc *crtc;
  12978. drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
  12979. if (!drmmode_crtc)
  12980. return -ENOENT;
  12981. crtc = to_intel_crtc(drmmode_crtc);
  12982. pipe_from_crtc_id->pipe = crtc->pipe;
  12983. return 0;
  12984. }
  12985. static int intel_encoder_clones(struct intel_encoder *encoder)
  12986. {
  12987. struct drm_device *dev = encoder->base.dev;
  12988. struct intel_encoder *source_encoder;
  12989. int index_mask = 0;
  12990. int entry = 0;
  12991. for_each_intel_encoder(dev, source_encoder) {
  12992. if (encoders_cloneable(encoder, source_encoder))
  12993. index_mask |= (1 << entry);
  12994. entry++;
  12995. }
  12996. return index_mask;
  12997. }
  12998. static bool has_edp_a(struct drm_i915_private *dev_priv)
  12999. {
  13000. if (!IS_MOBILE(dev_priv))
  13001. return false;
  13002. if ((I915_READ(DP_A) & DP_DETECTED) == 0)
  13003. return false;
  13004. if (IS_GEN5(dev_priv) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
  13005. return false;
  13006. return true;
  13007. }
  13008. static bool intel_crt_present(struct drm_i915_private *dev_priv)
  13009. {
  13010. if (INTEL_GEN(dev_priv) >= 9)
  13011. return false;
  13012. if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
  13013. return false;
  13014. if (IS_CHERRYVIEW(dev_priv))
  13015. return false;
  13016. if (HAS_PCH_LPT_H(dev_priv) &&
  13017. I915_READ(SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
  13018. return false;
  13019. /* DDI E can't be used if DDI A requires 4 lanes */
  13020. if (HAS_DDI(dev_priv) && I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
  13021. return false;
  13022. if (!dev_priv->vbt.int_crt_support)
  13023. return false;
  13024. return true;
  13025. }
  13026. void intel_pps_unlock_regs_wa(struct drm_i915_private *dev_priv)
  13027. {
  13028. int pps_num;
  13029. int pps_idx;
  13030. if (HAS_DDI(dev_priv))
  13031. return;
  13032. /*
  13033. * This w/a is needed at least on CPT/PPT, but to be sure apply it
  13034. * everywhere where registers can be write protected.
  13035. */
  13036. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  13037. pps_num = 2;
  13038. else
  13039. pps_num = 1;
  13040. for (pps_idx = 0; pps_idx < pps_num; pps_idx++) {
  13041. u32 val = I915_READ(PP_CONTROL(pps_idx));
  13042. val = (val & ~PANEL_UNLOCK_MASK) | PANEL_UNLOCK_REGS;
  13043. I915_WRITE(PP_CONTROL(pps_idx), val);
  13044. }
  13045. }
  13046. static void intel_pps_init(struct drm_i915_private *dev_priv)
  13047. {
  13048. if (HAS_PCH_SPLIT(dev_priv) || IS_GEN9_LP(dev_priv))
  13049. dev_priv->pps_mmio_base = PCH_PPS_BASE;
  13050. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  13051. dev_priv->pps_mmio_base = VLV_PPS_BASE;
  13052. else
  13053. dev_priv->pps_mmio_base = PPS_BASE;
  13054. intel_pps_unlock_regs_wa(dev_priv);
  13055. }
  13056. static void intel_setup_outputs(struct drm_i915_private *dev_priv)
  13057. {
  13058. struct intel_encoder *encoder;
  13059. bool dpd_is_edp = false;
  13060. intel_pps_init(dev_priv);
  13061. /*
  13062. * intel_edp_init_connector() depends on this completing first, to
  13063. * prevent the registeration of both eDP and LVDS and the incorrect
  13064. * sharing of the PPS.
  13065. */
  13066. intel_lvds_init(dev_priv);
  13067. if (intel_crt_present(dev_priv))
  13068. intel_crt_init(dev_priv);
  13069. if (IS_GEN9_LP(dev_priv)) {
  13070. /*
  13071. * FIXME: Broxton doesn't support port detection via the
  13072. * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
  13073. * detect the ports.
  13074. */
  13075. intel_ddi_init(dev_priv, PORT_A);
  13076. intel_ddi_init(dev_priv, PORT_B);
  13077. intel_ddi_init(dev_priv, PORT_C);
  13078. intel_dsi_init(dev_priv);
  13079. } else if (HAS_DDI(dev_priv)) {
  13080. int found;
  13081. /*
  13082. * Haswell uses DDI functions to detect digital outputs.
  13083. * On SKL pre-D0 the strap isn't connected, so we assume
  13084. * it's there.
  13085. */
  13086. found = I915_READ(DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
  13087. /* WaIgnoreDDIAStrap: skl */
  13088. if (found || IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  13089. intel_ddi_init(dev_priv, PORT_A);
  13090. /* DDI B, C and D detection is indicated by the SFUSE_STRAP
  13091. * register */
  13092. found = I915_READ(SFUSE_STRAP);
  13093. if (found & SFUSE_STRAP_DDIB_DETECTED)
  13094. intel_ddi_init(dev_priv, PORT_B);
  13095. if (found & SFUSE_STRAP_DDIC_DETECTED)
  13096. intel_ddi_init(dev_priv, PORT_C);
  13097. if (found & SFUSE_STRAP_DDID_DETECTED)
  13098. intel_ddi_init(dev_priv, PORT_D);
  13099. /*
  13100. * On SKL we don't have a way to detect DDI-E so we rely on VBT.
  13101. */
  13102. if ((IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) &&
  13103. (dev_priv->vbt.ddi_port_info[PORT_E].supports_dp ||
  13104. dev_priv->vbt.ddi_port_info[PORT_E].supports_dvi ||
  13105. dev_priv->vbt.ddi_port_info[PORT_E].supports_hdmi))
  13106. intel_ddi_init(dev_priv, PORT_E);
  13107. } else if (HAS_PCH_SPLIT(dev_priv)) {
  13108. int found;
  13109. dpd_is_edp = intel_dp_is_edp(dev_priv, PORT_D);
  13110. if (has_edp_a(dev_priv))
  13111. intel_dp_init(dev_priv, DP_A, PORT_A);
  13112. if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
  13113. /* PCH SDVOB multiplex with HDMIB */
  13114. found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
  13115. if (!found)
  13116. intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
  13117. if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
  13118. intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
  13119. }
  13120. if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
  13121. intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
  13122. if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
  13123. intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
  13124. if (I915_READ(PCH_DP_C) & DP_DETECTED)
  13125. intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
  13126. if (I915_READ(PCH_DP_D) & DP_DETECTED)
  13127. intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
  13128. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  13129. bool has_edp, has_port;
  13130. /*
  13131. * The DP_DETECTED bit is the latched state of the DDC
  13132. * SDA pin at boot. However since eDP doesn't require DDC
  13133. * (no way to plug in a DP->HDMI dongle) the DDC pins for
  13134. * eDP ports may have been muxed to an alternate function.
  13135. * Thus we can't rely on the DP_DETECTED bit alone to detect
  13136. * eDP ports. Consult the VBT as well as DP_DETECTED to
  13137. * detect eDP ports.
  13138. *
  13139. * Sadly the straps seem to be missing sometimes even for HDMI
  13140. * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
  13141. * and VBT for the presence of the port. Additionally we can't
  13142. * trust the port type the VBT declares as we've seen at least
  13143. * HDMI ports that the VBT claim are DP or eDP.
  13144. */
  13145. has_edp = intel_dp_is_edp(dev_priv, PORT_B);
  13146. has_port = intel_bios_is_port_present(dev_priv, PORT_B);
  13147. if (I915_READ(VLV_DP_B) & DP_DETECTED || has_port)
  13148. has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
  13149. if ((I915_READ(VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
  13150. intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
  13151. has_edp = intel_dp_is_edp(dev_priv, PORT_C);
  13152. has_port = intel_bios_is_port_present(dev_priv, PORT_C);
  13153. if (I915_READ(VLV_DP_C) & DP_DETECTED || has_port)
  13154. has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
  13155. if ((I915_READ(VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
  13156. intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
  13157. if (IS_CHERRYVIEW(dev_priv)) {
  13158. /*
  13159. * eDP not supported on port D,
  13160. * so no need to worry about it
  13161. */
  13162. has_port = intel_bios_is_port_present(dev_priv, PORT_D);
  13163. if (I915_READ(CHV_DP_D) & DP_DETECTED || has_port)
  13164. intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
  13165. if (I915_READ(CHV_HDMID) & SDVO_DETECTED || has_port)
  13166. intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
  13167. }
  13168. intel_dsi_init(dev_priv);
  13169. } else if (!IS_GEN2(dev_priv) && !IS_PINEVIEW(dev_priv)) {
  13170. bool found = false;
  13171. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  13172. DRM_DEBUG_KMS("probing SDVOB\n");
  13173. found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
  13174. if (!found && IS_G4X(dev_priv)) {
  13175. DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
  13176. intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
  13177. }
  13178. if (!found && IS_G4X(dev_priv))
  13179. intel_dp_init(dev_priv, DP_B, PORT_B);
  13180. }
  13181. /* Before G4X SDVOC doesn't have its own detect register */
  13182. if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
  13183. DRM_DEBUG_KMS("probing SDVOC\n");
  13184. found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
  13185. }
  13186. if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
  13187. if (IS_G4X(dev_priv)) {
  13188. DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
  13189. intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
  13190. }
  13191. if (IS_G4X(dev_priv))
  13192. intel_dp_init(dev_priv, DP_C, PORT_C);
  13193. }
  13194. if (IS_G4X(dev_priv) && (I915_READ(DP_D) & DP_DETECTED))
  13195. intel_dp_init(dev_priv, DP_D, PORT_D);
  13196. } else if (IS_GEN2(dev_priv))
  13197. intel_dvo_init(dev_priv);
  13198. if (SUPPORTS_TV(dev_priv))
  13199. intel_tv_init(dev_priv);
  13200. intel_psr_init(dev_priv);
  13201. for_each_intel_encoder(&dev_priv->drm, encoder) {
  13202. encoder->base.possible_crtcs = encoder->crtc_mask;
  13203. encoder->base.possible_clones =
  13204. intel_encoder_clones(encoder);
  13205. }
  13206. intel_init_pch_refclk(dev_priv);
  13207. drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
  13208. }
  13209. static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
  13210. {
  13211. struct drm_device *dev = fb->dev;
  13212. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  13213. drm_framebuffer_cleanup(fb);
  13214. mutex_lock(&dev->struct_mutex);
  13215. WARN_ON(!intel_fb->obj->framebuffer_references--);
  13216. i915_gem_object_put(intel_fb->obj);
  13217. mutex_unlock(&dev->struct_mutex);
  13218. kfree(intel_fb);
  13219. }
  13220. static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  13221. struct drm_file *file,
  13222. unsigned int *handle)
  13223. {
  13224. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  13225. struct drm_i915_gem_object *obj = intel_fb->obj;
  13226. if (obj->userptr.mm) {
  13227. DRM_DEBUG("attempting to use a userptr for a framebuffer, denied\n");
  13228. return -EINVAL;
  13229. }
  13230. return drm_gem_handle_create(file, &obj->base, handle);
  13231. }
  13232. static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
  13233. struct drm_file *file,
  13234. unsigned flags, unsigned color,
  13235. struct drm_clip_rect *clips,
  13236. unsigned num_clips)
  13237. {
  13238. struct drm_device *dev = fb->dev;
  13239. struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
  13240. struct drm_i915_gem_object *obj = intel_fb->obj;
  13241. mutex_lock(&dev->struct_mutex);
  13242. if (obj->pin_display && obj->cache_dirty)
  13243. i915_gem_clflush_object(obj, true);
  13244. intel_fb_obj_flush(obj, false, ORIGIN_DIRTYFB);
  13245. mutex_unlock(&dev->struct_mutex);
  13246. return 0;
  13247. }
  13248. static const struct drm_framebuffer_funcs intel_fb_funcs = {
  13249. .destroy = intel_user_framebuffer_destroy,
  13250. .create_handle = intel_user_framebuffer_create_handle,
  13251. .dirty = intel_user_framebuffer_dirty,
  13252. };
  13253. static
  13254. u32 intel_fb_pitch_limit(struct drm_i915_private *dev_priv,
  13255. uint64_t fb_modifier, uint32_t pixel_format)
  13256. {
  13257. u32 gen = INTEL_INFO(dev_priv)->gen;
  13258. if (gen >= 9) {
  13259. int cpp = drm_format_plane_cpp(pixel_format, 0);
  13260. /* "The stride in bytes must not exceed the of the size of 8K
  13261. * pixels and 32K bytes."
  13262. */
  13263. return min(8192 * cpp, 32768);
  13264. } else if (gen >= 5 && !IS_VALLEYVIEW(dev_priv) &&
  13265. !IS_CHERRYVIEW(dev_priv)) {
  13266. return 32*1024;
  13267. } else if (gen >= 4) {
  13268. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  13269. return 16*1024;
  13270. else
  13271. return 32*1024;
  13272. } else if (gen >= 3) {
  13273. if (fb_modifier == I915_FORMAT_MOD_X_TILED)
  13274. return 8*1024;
  13275. else
  13276. return 16*1024;
  13277. } else {
  13278. /* XXX DSPC is limited to 4k tiled */
  13279. return 8*1024;
  13280. }
  13281. }
  13282. static int intel_framebuffer_init(struct drm_device *dev,
  13283. struct intel_framebuffer *intel_fb,
  13284. struct drm_mode_fb_cmd2 *mode_cmd,
  13285. struct drm_i915_gem_object *obj)
  13286. {
  13287. struct drm_i915_private *dev_priv = to_i915(dev);
  13288. unsigned int tiling = i915_gem_object_get_tiling(obj);
  13289. int ret;
  13290. u32 pitch_limit, stride_alignment;
  13291. struct drm_format_name_buf format_name;
  13292. WARN_ON(!mutex_is_locked(&dev->struct_mutex));
  13293. if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
  13294. /*
  13295. * If there's a fence, enforce that
  13296. * the fb modifier and tiling mode match.
  13297. */
  13298. if (tiling != I915_TILING_NONE &&
  13299. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  13300. DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
  13301. return -EINVAL;
  13302. }
  13303. } else {
  13304. if (tiling == I915_TILING_X) {
  13305. mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
  13306. } else if (tiling == I915_TILING_Y) {
  13307. DRM_DEBUG("No Y tiling for legacy addfb\n");
  13308. return -EINVAL;
  13309. }
  13310. }
  13311. /* Passed in modifier sanity checking. */
  13312. switch (mode_cmd->modifier[0]) {
  13313. case I915_FORMAT_MOD_Y_TILED:
  13314. case I915_FORMAT_MOD_Yf_TILED:
  13315. if (INTEL_GEN(dev_priv) < 9) {
  13316. DRM_DEBUG("Unsupported tiling 0x%llx!\n",
  13317. mode_cmd->modifier[0]);
  13318. return -EINVAL;
  13319. }
  13320. case DRM_FORMAT_MOD_NONE:
  13321. case I915_FORMAT_MOD_X_TILED:
  13322. break;
  13323. default:
  13324. DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
  13325. mode_cmd->modifier[0]);
  13326. return -EINVAL;
  13327. }
  13328. /*
  13329. * gen2/3 display engine uses the fence if present,
  13330. * so the tiling mode must match the fb modifier exactly.
  13331. */
  13332. if (INTEL_INFO(dev_priv)->gen < 4 &&
  13333. tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
  13334. DRM_DEBUG("tiling_mode must match fb modifier exactly on gen2/3\n");
  13335. return -EINVAL;
  13336. }
  13337. stride_alignment = intel_fb_stride_alignment(dev_priv,
  13338. mode_cmd->modifier[0],
  13339. mode_cmd->pixel_format);
  13340. if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
  13341. DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
  13342. mode_cmd->pitches[0], stride_alignment);
  13343. return -EINVAL;
  13344. }
  13345. pitch_limit = intel_fb_pitch_limit(dev_priv, mode_cmd->modifier[0],
  13346. mode_cmd->pixel_format);
  13347. if (mode_cmd->pitches[0] > pitch_limit) {
  13348. DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
  13349. mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
  13350. "tiled" : "linear",
  13351. mode_cmd->pitches[0], pitch_limit);
  13352. return -EINVAL;
  13353. }
  13354. /*
  13355. * If there's a fence, enforce that
  13356. * the fb pitch and fence stride match.
  13357. */
  13358. if (tiling != I915_TILING_NONE &&
  13359. mode_cmd->pitches[0] != i915_gem_object_get_stride(obj)) {
  13360. DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
  13361. mode_cmd->pitches[0],
  13362. i915_gem_object_get_stride(obj));
  13363. return -EINVAL;
  13364. }
  13365. /* Reject formats not supported by any plane early. */
  13366. switch (mode_cmd->pixel_format) {
  13367. case DRM_FORMAT_C8:
  13368. case DRM_FORMAT_RGB565:
  13369. case DRM_FORMAT_XRGB8888:
  13370. case DRM_FORMAT_ARGB8888:
  13371. break;
  13372. case DRM_FORMAT_XRGB1555:
  13373. if (INTEL_GEN(dev_priv) > 3) {
  13374. DRM_DEBUG("unsupported pixel format: %s\n",
  13375. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  13376. return -EINVAL;
  13377. }
  13378. break;
  13379. case DRM_FORMAT_ABGR8888:
  13380. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv) &&
  13381. INTEL_GEN(dev_priv) < 9) {
  13382. DRM_DEBUG("unsupported pixel format: %s\n",
  13383. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  13384. return -EINVAL;
  13385. }
  13386. break;
  13387. case DRM_FORMAT_XBGR8888:
  13388. case DRM_FORMAT_XRGB2101010:
  13389. case DRM_FORMAT_XBGR2101010:
  13390. if (INTEL_GEN(dev_priv) < 4) {
  13391. DRM_DEBUG("unsupported pixel format: %s\n",
  13392. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  13393. return -EINVAL;
  13394. }
  13395. break;
  13396. case DRM_FORMAT_ABGR2101010:
  13397. if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
  13398. DRM_DEBUG("unsupported pixel format: %s\n",
  13399. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  13400. return -EINVAL;
  13401. }
  13402. break;
  13403. case DRM_FORMAT_YUYV:
  13404. case DRM_FORMAT_UYVY:
  13405. case DRM_FORMAT_YVYU:
  13406. case DRM_FORMAT_VYUY:
  13407. if (INTEL_GEN(dev_priv) < 5) {
  13408. DRM_DEBUG("unsupported pixel format: %s\n",
  13409. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  13410. return -EINVAL;
  13411. }
  13412. break;
  13413. default:
  13414. DRM_DEBUG("unsupported pixel format: %s\n",
  13415. drm_get_format_name(mode_cmd->pixel_format, &format_name));
  13416. return -EINVAL;
  13417. }
  13418. /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
  13419. if (mode_cmd->offsets[0] != 0)
  13420. return -EINVAL;
  13421. drm_helper_mode_fill_fb_struct(dev, &intel_fb->base, mode_cmd);
  13422. intel_fb->obj = obj;
  13423. ret = intel_fill_fb_info(dev_priv, &intel_fb->base);
  13424. if (ret)
  13425. return ret;
  13426. ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
  13427. if (ret) {
  13428. DRM_ERROR("framebuffer init failed %d\n", ret);
  13429. return ret;
  13430. }
  13431. intel_fb->obj->framebuffer_references++;
  13432. return 0;
  13433. }
  13434. static struct drm_framebuffer *
  13435. intel_user_framebuffer_create(struct drm_device *dev,
  13436. struct drm_file *filp,
  13437. const struct drm_mode_fb_cmd2 *user_mode_cmd)
  13438. {
  13439. struct drm_framebuffer *fb;
  13440. struct drm_i915_gem_object *obj;
  13441. struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
  13442. obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
  13443. if (!obj)
  13444. return ERR_PTR(-ENOENT);
  13445. fb = intel_framebuffer_create(dev, &mode_cmd, obj);
  13446. if (IS_ERR(fb))
  13447. i915_gem_object_put(obj);
  13448. return fb;
  13449. }
  13450. static void intel_atomic_state_free(struct drm_atomic_state *state)
  13451. {
  13452. struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
  13453. drm_atomic_state_default_release(state);
  13454. i915_sw_fence_fini(&intel_state->commit_ready);
  13455. kfree(state);
  13456. }
  13457. static const struct drm_mode_config_funcs intel_mode_funcs = {
  13458. .fb_create = intel_user_framebuffer_create,
  13459. .output_poll_changed = intel_fbdev_output_poll_changed,
  13460. .atomic_check = intel_atomic_check,
  13461. .atomic_commit = intel_atomic_commit,
  13462. .atomic_state_alloc = intel_atomic_state_alloc,
  13463. .atomic_state_clear = intel_atomic_state_clear,
  13464. .atomic_state_free = intel_atomic_state_free,
  13465. };
  13466. /**
  13467. * intel_init_display_hooks - initialize the display modesetting hooks
  13468. * @dev_priv: device private
  13469. */
  13470. void intel_init_display_hooks(struct drm_i915_private *dev_priv)
  13471. {
  13472. if (INTEL_INFO(dev_priv)->gen >= 9) {
  13473. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  13474. dev_priv->display.get_initial_plane_config =
  13475. skylake_get_initial_plane_config;
  13476. dev_priv->display.crtc_compute_clock =
  13477. haswell_crtc_compute_clock;
  13478. dev_priv->display.crtc_enable = haswell_crtc_enable;
  13479. dev_priv->display.crtc_disable = haswell_crtc_disable;
  13480. } else if (HAS_DDI(dev_priv)) {
  13481. dev_priv->display.get_pipe_config = haswell_get_pipe_config;
  13482. dev_priv->display.get_initial_plane_config =
  13483. ironlake_get_initial_plane_config;
  13484. dev_priv->display.crtc_compute_clock =
  13485. haswell_crtc_compute_clock;
  13486. dev_priv->display.crtc_enable = haswell_crtc_enable;
  13487. dev_priv->display.crtc_disable = haswell_crtc_disable;
  13488. } else if (HAS_PCH_SPLIT(dev_priv)) {
  13489. dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
  13490. dev_priv->display.get_initial_plane_config =
  13491. ironlake_get_initial_plane_config;
  13492. dev_priv->display.crtc_compute_clock =
  13493. ironlake_crtc_compute_clock;
  13494. dev_priv->display.crtc_enable = ironlake_crtc_enable;
  13495. dev_priv->display.crtc_disable = ironlake_crtc_disable;
  13496. } else if (IS_CHERRYVIEW(dev_priv)) {
  13497. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13498. dev_priv->display.get_initial_plane_config =
  13499. i9xx_get_initial_plane_config;
  13500. dev_priv->display.crtc_compute_clock = chv_crtc_compute_clock;
  13501. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  13502. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13503. } else if (IS_VALLEYVIEW(dev_priv)) {
  13504. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13505. dev_priv->display.get_initial_plane_config =
  13506. i9xx_get_initial_plane_config;
  13507. dev_priv->display.crtc_compute_clock = vlv_crtc_compute_clock;
  13508. dev_priv->display.crtc_enable = valleyview_crtc_enable;
  13509. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13510. } else if (IS_G4X(dev_priv)) {
  13511. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13512. dev_priv->display.get_initial_plane_config =
  13513. i9xx_get_initial_plane_config;
  13514. dev_priv->display.crtc_compute_clock = g4x_crtc_compute_clock;
  13515. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13516. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13517. } else if (IS_PINEVIEW(dev_priv)) {
  13518. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13519. dev_priv->display.get_initial_plane_config =
  13520. i9xx_get_initial_plane_config;
  13521. dev_priv->display.crtc_compute_clock = pnv_crtc_compute_clock;
  13522. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13523. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13524. } else if (!IS_GEN2(dev_priv)) {
  13525. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13526. dev_priv->display.get_initial_plane_config =
  13527. i9xx_get_initial_plane_config;
  13528. dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
  13529. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13530. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13531. } else {
  13532. dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
  13533. dev_priv->display.get_initial_plane_config =
  13534. i9xx_get_initial_plane_config;
  13535. dev_priv->display.crtc_compute_clock = i8xx_crtc_compute_clock;
  13536. dev_priv->display.crtc_enable = i9xx_crtc_enable;
  13537. dev_priv->display.crtc_disable = i9xx_crtc_disable;
  13538. }
  13539. /* Returns the core display clock speed */
  13540. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  13541. dev_priv->display.get_display_clock_speed =
  13542. skylake_get_display_clock_speed;
  13543. else if (IS_GEN9_LP(dev_priv))
  13544. dev_priv->display.get_display_clock_speed =
  13545. broxton_get_display_clock_speed;
  13546. else if (IS_BROADWELL(dev_priv))
  13547. dev_priv->display.get_display_clock_speed =
  13548. broadwell_get_display_clock_speed;
  13549. else if (IS_HASWELL(dev_priv))
  13550. dev_priv->display.get_display_clock_speed =
  13551. haswell_get_display_clock_speed;
  13552. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  13553. dev_priv->display.get_display_clock_speed =
  13554. valleyview_get_display_clock_speed;
  13555. else if (IS_GEN5(dev_priv))
  13556. dev_priv->display.get_display_clock_speed =
  13557. ilk_get_display_clock_speed;
  13558. else if (IS_I945G(dev_priv) || IS_I965G(dev_priv) ||
  13559. IS_GEN6(dev_priv) || IS_IVYBRIDGE(dev_priv))
  13560. dev_priv->display.get_display_clock_speed =
  13561. i945_get_display_clock_speed;
  13562. else if (IS_GM45(dev_priv))
  13563. dev_priv->display.get_display_clock_speed =
  13564. gm45_get_display_clock_speed;
  13565. else if (IS_I965GM(dev_priv))
  13566. dev_priv->display.get_display_clock_speed =
  13567. i965gm_get_display_clock_speed;
  13568. else if (IS_PINEVIEW(dev_priv))
  13569. dev_priv->display.get_display_clock_speed =
  13570. pnv_get_display_clock_speed;
  13571. else if (IS_G33(dev_priv) || IS_G4X(dev_priv))
  13572. dev_priv->display.get_display_clock_speed =
  13573. g33_get_display_clock_speed;
  13574. else if (IS_I915G(dev_priv))
  13575. dev_priv->display.get_display_clock_speed =
  13576. i915_get_display_clock_speed;
  13577. else if (IS_I945GM(dev_priv) || IS_I845G(dev_priv))
  13578. dev_priv->display.get_display_clock_speed =
  13579. i9xx_misc_get_display_clock_speed;
  13580. else if (IS_I915GM(dev_priv))
  13581. dev_priv->display.get_display_clock_speed =
  13582. i915gm_get_display_clock_speed;
  13583. else if (IS_I865G(dev_priv))
  13584. dev_priv->display.get_display_clock_speed =
  13585. i865_get_display_clock_speed;
  13586. else if (IS_I85X(dev_priv))
  13587. dev_priv->display.get_display_clock_speed =
  13588. i85x_get_display_clock_speed;
  13589. else { /* 830 */
  13590. WARN(!IS_I830(dev_priv), "Unknown platform. Assuming 133 MHz CDCLK\n");
  13591. dev_priv->display.get_display_clock_speed =
  13592. i830_get_display_clock_speed;
  13593. }
  13594. if (IS_GEN5(dev_priv)) {
  13595. dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
  13596. } else if (IS_GEN6(dev_priv)) {
  13597. dev_priv->display.fdi_link_train = gen6_fdi_link_train;
  13598. } else if (IS_IVYBRIDGE(dev_priv)) {
  13599. /* FIXME: detect B0+ stepping and use auto training */
  13600. dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
  13601. } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
  13602. dev_priv->display.fdi_link_train = hsw_fdi_link_train;
  13603. }
  13604. if (IS_BROADWELL(dev_priv)) {
  13605. dev_priv->display.modeset_commit_cdclk =
  13606. broadwell_modeset_commit_cdclk;
  13607. dev_priv->display.modeset_calc_cdclk =
  13608. broadwell_modeset_calc_cdclk;
  13609. } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
  13610. dev_priv->display.modeset_commit_cdclk =
  13611. valleyview_modeset_commit_cdclk;
  13612. dev_priv->display.modeset_calc_cdclk =
  13613. valleyview_modeset_calc_cdclk;
  13614. } else if (IS_GEN9_LP(dev_priv)) {
  13615. dev_priv->display.modeset_commit_cdclk =
  13616. bxt_modeset_commit_cdclk;
  13617. dev_priv->display.modeset_calc_cdclk =
  13618. bxt_modeset_calc_cdclk;
  13619. } else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
  13620. dev_priv->display.modeset_commit_cdclk =
  13621. skl_modeset_commit_cdclk;
  13622. dev_priv->display.modeset_calc_cdclk =
  13623. skl_modeset_calc_cdclk;
  13624. }
  13625. if (dev_priv->info.gen >= 9)
  13626. dev_priv->display.update_crtcs = skl_update_crtcs;
  13627. else
  13628. dev_priv->display.update_crtcs = intel_update_crtcs;
  13629. switch (INTEL_INFO(dev_priv)->gen) {
  13630. case 2:
  13631. dev_priv->display.queue_flip = intel_gen2_queue_flip;
  13632. break;
  13633. case 3:
  13634. dev_priv->display.queue_flip = intel_gen3_queue_flip;
  13635. break;
  13636. case 4:
  13637. case 5:
  13638. dev_priv->display.queue_flip = intel_gen4_queue_flip;
  13639. break;
  13640. case 6:
  13641. dev_priv->display.queue_flip = intel_gen6_queue_flip;
  13642. break;
  13643. case 7:
  13644. case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
  13645. dev_priv->display.queue_flip = intel_gen7_queue_flip;
  13646. break;
  13647. case 9:
  13648. /* Drop through - unsupported since execlist only. */
  13649. default:
  13650. /* Default just returns -ENODEV to indicate unsupported */
  13651. dev_priv->display.queue_flip = intel_default_queue_flip;
  13652. }
  13653. }
  13654. /*
  13655. * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
  13656. * resume, or other times. This quirk makes sure that's the case for
  13657. * affected systems.
  13658. */
  13659. static void quirk_pipea_force(struct drm_device *dev)
  13660. {
  13661. struct drm_i915_private *dev_priv = to_i915(dev);
  13662. dev_priv->quirks |= QUIRK_PIPEA_FORCE;
  13663. DRM_INFO("applying pipe a force quirk\n");
  13664. }
  13665. static void quirk_pipeb_force(struct drm_device *dev)
  13666. {
  13667. struct drm_i915_private *dev_priv = to_i915(dev);
  13668. dev_priv->quirks |= QUIRK_PIPEB_FORCE;
  13669. DRM_INFO("applying pipe b force quirk\n");
  13670. }
  13671. /*
  13672. * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
  13673. */
  13674. static void quirk_ssc_force_disable(struct drm_device *dev)
  13675. {
  13676. struct drm_i915_private *dev_priv = to_i915(dev);
  13677. dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
  13678. DRM_INFO("applying lvds SSC disable quirk\n");
  13679. }
  13680. /*
  13681. * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
  13682. * brightness value
  13683. */
  13684. static void quirk_invert_brightness(struct drm_device *dev)
  13685. {
  13686. struct drm_i915_private *dev_priv = to_i915(dev);
  13687. dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
  13688. DRM_INFO("applying inverted panel brightness quirk\n");
  13689. }
  13690. /* Some VBT's incorrectly indicate no backlight is present */
  13691. static void quirk_backlight_present(struct drm_device *dev)
  13692. {
  13693. struct drm_i915_private *dev_priv = to_i915(dev);
  13694. dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
  13695. DRM_INFO("applying backlight present quirk\n");
  13696. }
  13697. struct intel_quirk {
  13698. int device;
  13699. int subsystem_vendor;
  13700. int subsystem_device;
  13701. void (*hook)(struct drm_device *dev);
  13702. };
  13703. /* For systems that don't have a meaningful PCI subdevice/subvendor ID */
  13704. struct intel_dmi_quirk {
  13705. void (*hook)(struct drm_device *dev);
  13706. const struct dmi_system_id (*dmi_id_list)[];
  13707. };
  13708. static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
  13709. {
  13710. DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
  13711. return 1;
  13712. }
  13713. static const struct intel_dmi_quirk intel_dmi_quirks[] = {
  13714. {
  13715. .dmi_id_list = &(const struct dmi_system_id[]) {
  13716. {
  13717. .callback = intel_dmi_reverse_brightness,
  13718. .ident = "NCR Corporation",
  13719. .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
  13720. DMI_MATCH(DMI_PRODUCT_NAME, ""),
  13721. },
  13722. },
  13723. { } /* terminating entry */
  13724. },
  13725. .hook = quirk_invert_brightness,
  13726. },
  13727. };
  13728. static struct intel_quirk intel_quirks[] = {
  13729. /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
  13730. { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
  13731. /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
  13732. { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
  13733. /* 830 needs to leave pipe A & dpll A up */
  13734. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
  13735. /* 830 needs to leave pipe B & dpll B up */
  13736. { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
  13737. /* Lenovo U160 cannot use SSC on LVDS */
  13738. { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
  13739. /* Sony Vaio Y cannot use SSC on LVDS */
  13740. { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
  13741. /* Acer Aspire 5734Z must invert backlight brightness */
  13742. { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
  13743. /* Acer/eMachines G725 */
  13744. { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
  13745. /* Acer/eMachines e725 */
  13746. { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
  13747. /* Acer/Packard Bell NCL20 */
  13748. { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
  13749. /* Acer Aspire 4736Z */
  13750. { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
  13751. /* Acer Aspire 5336 */
  13752. { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
  13753. /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
  13754. { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
  13755. /* Acer C720 Chromebook (Core i3 4005U) */
  13756. { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
  13757. /* Apple Macbook 2,1 (Core 2 T7400) */
  13758. { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
  13759. /* Apple Macbook 4,1 */
  13760. { 0x2a02, 0x106b, 0x00a1, quirk_backlight_present },
  13761. /* Toshiba CB35 Chromebook (Celeron 2955U) */
  13762. { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
  13763. /* HP Chromebook 14 (Celeron 2955U) */
  13764. { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
  13765. /* Dell Chromebook 11 */
  13766. { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
  13767. /* Dell Chromebook 11 (2015 version) */
  13768. { 0x0a16, 0x1028, 0x0a35, quirk_backlight_present },
  13769. };
  13770. static void intel_init_quirks(struct drm_device *dev)
  13771. {
  13772. struct pci_dev *d = dev->pdev;
  13773. int i;
  13774. for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
  13775. struct intel_quirk *q = &intel_quirks[i];
  13776. if (d->device == q->device &&
  13777. (d->subsystem_vendor == q->subsystem_vendor ||
  13778. q->subsystem_vendor == PCI_ANY_ID) &&
  13779. (d->subsystem_device == q->subsystem_device ||
  13780. q->subsystem_device == PCI_ANY_ID))
  13781. q->hook(dev);
  13782. }
  13783. for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
  13784. if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
  13785. intel_dmi_quirks[i].hook(dev);
  13786. }
  13787. }
  13788. /* Disable the VGA plane that we never use */
  13789. static void i915_disable_vga(struct drm_i915_private *dev_priv)
  13790. {
  13791. struct pci_dev *pdev = dev_priv->drm.pdev;
  13792. u8 sr1;
  13793. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  13794. /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
  13795. vga_get_uninterruptible(pdev, VGA_RSRC_LEGACY_IO);
  13796. outb(SR01, VGA_SR_INDEX);
  13797. sr1 = inb(VGA_SR_DATA);
  13798. outb(sr1 | 1<<5, VGA_SR_DATA);
  13799. vga_put(pdev, VGA_RSRC_LEGACY_IO);
  13800. udelay(300);
  13801. I915_WRITE(vga_reg, VGA_DISP_DISABLE);
  13802. POSTING_READ(vga_reg);
  13803. }
  13804. void intel_modeset_init_hw(struct drm_device *dev)
  13805. {
  13806. struct drm_i915_private *dev_priv = to_i915(dev);
  13807. intel_update_cdclk(dev_priv);
  13808. dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
  13809. intel_init_clock_gating(dev_priv);
  13810. }
  13811. /*
  13812. * Calculate what we think the watermarks should be for the state we've read
  13813. * out of the hardware and then immediately program those watermarks so that
  13814. * we ensure the hardware settings match our internal state.
  13815. *
  13816. * We can calculate what we think WM's should be by creating a duplicate of the
  13817. * current state (which was constructed during hardware readout) and running it
  13818. * through the atomic check code to calculate new watermark values in the
  13819. * state object.
  13820. */
  13821. static void sanitize_watermarks(struct drm_device *dev)
  13822. {
  13823. struct drm_i915_private *dev_priv = to_i915(dev);
  13824. struct drm_atomic_state *state;
  13825. struct intel_atomic_state *intel_state;
  13826. struct drm_crtc *crtc;
  13827. struct drm_crtc_state *cstate;
  13828. struct drm_modeset_acquire_ctx ctx;
  13829. int ret;
  13830. int i;
  13831. /* Only supported on platforms that use atomic watermark design */
  13832. if (!dev_priv->display.optimize_watermarks)
  13833. return;
  13834. /*
  13835. * We need to hold connection_mutex before calling duplicate_state so
  13836. * that the connector loop is protected.
  13837. */
  13838. drm_modeset_acquire_init(&ctx, 0);
  13839. retry:
  13840. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  13841. if (ret == -EDEADLK) {
  13842. drm_modeset_backoff(&ctx);
  13843. goto retry;
  13844. } else if (WARN_ON(ret)) {
  13845. goto fail;
  13846. }
  13847. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  13848. if (WARN_ON(IS_ERR(state)))
  13849. goto fail;
  13850. intel_state = to_intel_atomic_state(state);
  13851. /*
  13852. * Hardware readout is the only time we don't want to calculate
  13853. * intermediate watermarks (since we don't trust the current
  13854. * watermarks).
  13855. */
  13856. intel_state->skip_intermediate_wm = true;
  13857. ret = intel_atomic_check(dev, state);
  13858. if (ret) {
  13859. /*
  13860. * If we fail here, it means that the hardware appears to be
  13861. * programmed in a way that shouldn't be possible, given our
  13862. * understanding of watermark requirements. This might mean a
  13863. * mistake in the hardware readout code or a mistake in the
  13864. * watermark calculations for a given platform. Raise a WARN
  13865. * so that this is noticeable.
  13866. *
  13867. * If this actually happens, we'll have to just leave the
  13868. * BIOS-programmed watermarks untouched and hope for the best.
  13869. */
  13870. WARN(true, "Could not determine valid watermarks for inherited state\n");
  13871. goto put_state;
  13872. }
  13873. /* Write calculated watermark values back */
  13874. for_each_crtc_in_state(state, crtc, cstate, i) {
  13875. struct intel_crtc_state *cs = to_intel_crtc_state(cstate);
  13876. cs->wm.need_postvbl_update = true;
  13877. dev_priv->display.optimize_watermarks(intel_state, cs);
  13878. }
  13879. put_state:
  13880. drm_atomic_state_put(state);
  13881. fail:
  13882. drm_modeset_drop_locks(&ctx);
  13883. drm_modeset_acquire_fini(&ctx);
  13884. }
  13885. int intel_modeset_init(struct drm_device *dev)
  13886. {
  13887. struct drm_i915_private *dev_priv = to_i915(dev);
  13888. struct i915_ggtt *ggtt = &dev_priv->ggtt;
  13889. enum pipe pipe;
  13890. struct intel_crtc *crtc;
  13891. drm_mode_config_init(dev);
  13892. dev->mode_config.min_width = 0;
  13893. dev->mode_config.min_height = 0;
  13894. dev->mode_config.preferred_depth = 24;
  13895. dev->mode_config.prefer_shadow = 1;
  13896. dev->mode_config.allow_fb_modifiers = true;
  13897. dev->mode_config.funcs = &intel_mode_funcs;
  13898. intel_init_quirks(dev);
  13899. intel_init_pm(dev_priv);
  13900. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  13901. return 0;
  13902. /*
  13903. * There may be no VBT; and if the BIOS enabled SSC we can
  13904. * just keep using it to avoid unnecessary flicker. Whereas if the
  13905. * BIOS isn't using it, don't assume it will work even if the VBT
  13906. * indicates as much.
  13907. */
  13908. if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
  13909. bool bios_lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
  13910. DREF_SSC1_ENABLE);
  13911. if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
  13912. DRM_DEBUG_KMS("SSC %sabled by BIOS, overriding VBT which says %sabled\n",
  13913. bios_lvds_use_ssc ? "en" : "dis",
  13914. dev_priv->vbt.lvds_use_ssc ? "en" : "dis");
  13915. dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
  13916. }
  13917. }
  13918. if (IS_GEN2(dev_priv)) {
  13919. dev->mode_config.max_width = 2048;
  13920. dev->mode_config.max_height = 2048;
  13921. } else if (IS_GEN3(dev_priv)) {
  13922. dev->mode_config.max_width = 4096;
  13923. dev->mode_config.max_height = 4096;
  13924. } else {
  13925. dev->mode_config.max_width = 8192;
  13926. dev->mode_config.max_height = 8192;
  13927. }
  13928. if (IS_I845G(dev_priv) || IS_I865G(dev_priv)) {
  13929. dev->mode_config.cursor_width = IS_I845G(dev_priv) ? 64 : 512;
  13930. dev->mode_config.cursor_height = 1023;
  13931. } else if (IS_GEN2(dev_priv)) {
  13932. dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
  13933. dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
  13934. } else {
  13935. dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
  13936. dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
  13937. }
  13938. dev->mode_config.fb_base = ggtt->mappable_base;
  13939. DRM_DEBUG_KMS("%d display pipe%s available.\n",
  13940. INTEL_INFO(dev_priv)->num_pipes,
  13941. INTEL_INFO(dev_priv)->num_pipes > 1 ? "s" : "");
  13942. for_each_pipe(dev_priv, pipe) {
  13943. int ret;
  13944. ret = intel_crtc_init(dev_priv, pipe);
  13945. if (ret) {
  13946. drm_mode_config_cleanup(dev);
  13947. return ret;
  13948. }
  13949. }
  13950. intel_update_czclk(dev_priv);
  13951. intel_update_cdclk(dev_priv);
  13952. dev_priv->atomic_cdclk_freq = dev_priv->cdclk_freq;
  13953. intel_shared_dpll_init(dev);
  13954. if (dev_priv->max_cdclk_freq == 0)
  13955. intel_update_max_cdclk(dev_priv);
  13956. /* Just disable it once at startup */
  13957. i915_disable_vga(dev_priv);
  13958. intel_setup_outputs(dev_priv);
  13959. drm_modeset_lock_all(dev);
  13960. intel_modeset_setup_hw_state(dev);
  13961. drm_modeset_unlock_all(dev);
  13962. for_each_intel_crtc(dev, crtc) {
  13963. struct intel_initial_plane_config plane_config = {};
  13964. if (!crtc->active)
  13965. continue;
  13966. /*
  13967. * Note that reserving the BIOS fb up front prevents us
  13968. * from stuffing other stolen allocations like the ring
  13969. * on top. This prevents some ugliness at boot time, and
  13970. * can even allow for smooth boot transitions if the BIOS
  13971. * fb is large enough for the active pipe configuration.
  13972. */
  13973. dev_priv->display.get_initial_plane_config(crtc,
  13974. &plane_config);
  13975. /*
  13976. * If the fb is shared between multiple heads, we'll
  13977. * just get the first one.
  13978. */
  13979. intel_find_initial_plane_obj(crtc, &plane_config);
  13980. }
  13981. /*
  13982. * Make sure hardware watermarks really match the state we read out.
  13983. * Note that we need to do this after reconstructing the BIOS fb's
  13984. * since the watermark calculation done here will use pstate->fb.
  13985. */
  13986. sanitize_watermarks(dev);
  13987. return 0;
  13988. }
  13989. static void intel_enable_pipe_a(struct drm_device *dev)
  13990. {
  13991. struct intel_connector *connector;
  13992. struct drm_connector *crt = NULL;
  13993. struct intel_load_detect_pipe load_detect_temp;
  13994. struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
  13995. /* We can't just switch on the pipe A, we need to set things up with a
  13996. * proper mode and output configuration. As a gross hack, enable pipe A
  13997. * by enabling the load detect pipe once. */
  13998. for_each_intel_connector(dev, connector) {
  13999. if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
  14000. crt = &connector->base;
  14001. break;
  14002. }
  14003. }
  14004. if (!crt)
  14005. return;
  14006. if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
  14007. intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
  14008. }
  14009. static bool
  14010. intel_check_plane_mapping(struct intel_crtc *crtc)
  14011. {
  14012. struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
  14013. u32 val;
  14014. if (INTEL_INFO(dev_priv)->num_pipes == 1)
  14015. return true;
  14016. val = I915_READ(DSPCNTR(!crtc->plane));
  14017. if ((val & DISPLAY_PLANE_ENABLE) &&
  14018. (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
  14019. return false;
  14020. return true;
  14021. }
  14022. static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
  14023. {
  14024. struct drm_device *dev = crtc->base.dev;
  14025. struct intel_encoder *encoder;
  14026. for_each_encoder_on_crtc(dev, &crtc->base, encoder)
  14027. return true;
  14028. return false;
  14029. }
  14030. static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
  14031. {
  14032. struct drm_device *dev = encoder->base.dev;
  14033. struct intel_connector *connector;
  14034. for_each_connector_on_encoder(dev, &encoder->base, connector)
  14035. return connector;
  14036. return NULL;
  14037. }
  14038. static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
  14039. enum transcoder pch_transcoder)
  14040. {
  14041. return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
  14042. (HAS_PCH_LPT_H(dev_priv) && pch_transcoder == TRANSCODER_A);
  14043. }
  14044. static void intel_sanitize_crtc(struct intel_crtc *crtc)
  14045. {
  14046. struct drm_device *dev = crtc->base.dev;
  14047. struct drm_i915_private *dev_priv = to_i915(dev);
  14048. enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
  14049. /* Clear any frame start delays used for debugging left by the BIOS */
  14050. if (!transcoder_is_dsi(cpu_transcoder)) {
  14051. i915_reg_t reg = PIPECONF(cpu_transcoder);
  14052. I915_WRITE(reg,
  14053. I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
  14054. }
  14055. /* restore vblank interrupts to correct state */
  14056. drm_crtc_vblank_reset(&crtc->base);
  14057. if (crtc->active) {
  14058. struct intel_plane *plane;
  14059. drm_crtc_vblank_on(&crtc->base);
  14060. /* Disable everything but the primary plane */
  14061. for_each_intel_plane_on_crtc(dev, crtc, plane) {
  14062. if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
  14063. continue;
  14064. plane->disable_plane(&plane->base, &crtc->base);
  14065. }
  14066. }
  14067. /* We need to sanitize the plane -> pipe mapping first because this will
  14068. * disable the crtc (and hence change the state) if it is wrong. Note
  14069. * that gen4+ has a fixed plane -> pipe mapping. */
  14070. if (INTEL_GEN(dev_priv) < 4 && !intel_check_plane_mapping(crtc)) {
  14071. bool plane;
  14072. DRM_DEBUG_KMS("[CRTC:%d:%s] wrong plane connection detected!\n",
  14073. crtc->base.base.id, crtc->base.name);
  14074. /* Pipe has the wrong plane attached and the plane is active.
  14075. * Temporarily change the plane mapping and disable everything
  14076. * ... */
  14077. plane = crtc->plane;
  14078. crtc->base.primary->state->visible = true;
  14079. crtc->plane = !plane;
  14080. intel_crtc_disable_noatomic(&crtc->base);
  14081. crtc->plane = plane;
  14082. }
  14083. if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
  14084. crtc->pipe == PIPE_A && !crtc->active) {
  14085. /* BIOS forgot to enable pipe A, this mostly happens after
  14086. * resume. Force-enable the pipe to fix this, the update_dpms
  14087. * call below we restore the pipe to the right state, but leave
  14088. * the required bits on. */
  14089. intel_enable_pipe_a(dev);
  14090. }
  14091. /* Adjust the state of the output pipe according to whether we
  14092. * have active connectors/encoders. */
  14093. if (crtc->active && !intel_crtc_has_encoders(crtc))
  14094. intel_crtc_disable_noatomic(&crtc->base);
  14095. if (crtc->active || HAS_GMCH_DISPLAY(dev_priv)) {
  14096. /*
  14097. * We start out with underrun reporting disabled to avoid races.
  14098. * For correct bookkeeping mark this on active crtcs.
  14099. *
  14100. * Also on gmch platforms we dont have any hardware bits to
  14101. * disable the underrun reporting. Which means we need to start
  14102. * out with underrun reporting disabled also on inactive pipes,
  14103. * since otherwise we'll complain about the garbage we read when
  14104. * e.g. coming up after runtime pm.
  14105. *
  14106. * No protection against concurrent access is required - at
  14107. * worst a fifo underrun happens which also sets this to false.
  14108. */
  14109. crtc->cpu_fifo_underrun_disabled = true;
  14110. /*
  14111. * We track the PCH trancoder underrun reporting state
  14112. * within the crtc. With crtc for pipe A housing the underrun
  14113. * reporting state for PCH transcoder A, crtc for pipe B housing
  14114. * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
  14115. * and marking underrun reporting as disabled for the non-existing
  14116. * PCH transcoders B and C would prevent enabling the south
  14117. * error interrupt (see cpt_can_enable_serr_int()).
  14118. */
  14119. if (has_pch_trancoder(dev_priv, (enum transcoder)crtc->pipe))
  14120. crtc->pch_fifo_underrun_disabled = true;
  14121. }
  14122. }
  14123. static void intel_sanitize_encoder(struct intel_encoder *encoder)
  14124. {
  14125. struct intel_connector *connector;
  14126. /* We need to check both for a crtc link (meaning that the
  14127. * encoder is active and trying to read from a pipe) and the
  14128. * pipe itself being active. */
  14129. bool has_active_crtc = encoder->base.crtc &&
  14130. to_intel_crtc(encoder->base.crtc)->active;
  14131. connector = intel_encoder_find_connector(encoder);
  14132. if (connector && !has_active_crtc) {
  14133. DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
  14134. encoder->base.base.id,
  14135. encoder->base.name);
  14136. /* Connector is active, but has no active pipe. This is
  14137. * fallout from our resume register restoring. Disable
  14138. * the encoder manually again. */
  14139. if (encoder->base.crtc) {
  14140. struct drm_crtc_state *crtc_state = encoder->base.crtc->state;
  14141. DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
  14142. encoder->base.base.id,
  14143. encoder->base.name);
  14144. encoder->disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  14145. if (encoder->post_disable)
  14146. encoder->post_disable(encoder, to_intel_crtc_state(crtc_state), connector->base.state);
  14147. }
  14148. encoder->base.crtc = NULL;
  14149. /* Inconsistent output/port/pipe state happens presumably due to
  14150. * a bug in one of the get_hw_state functions. Or someplace else
  14151. * in our code, like the register restore mess on resume. Clamp
  14152. * things to off as a safer default. */
  14153. connector->base.dpms = DRM_MODE_DPMS_OFF;
  14154. connector->base.encoder = NULL;
  14155. }
  14156. /* Enabled encoders without active connectors will be fixed in
  14157. * the crtc fixup. */
  14158. }
  14159. void i915_redisable_vga_power_on(struct drm_i915_private *dev_priv)
  14160. {
  14161. i915_reg_t vga_reg = i915_vgacntrl_reg(dev_priv);
  14162. if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
  14163. DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
  14164. i915_disable_vga(dev_priv);
  14165. }
  14166. }
  14167. void i915_redisable_vga(struct drm_i915_private *dev_priv)
  14168. {
  14169. /* This function can be called both from intel_modeset_setup_hw_state or
  14170. * at a very early point in our resume sequence, where the power well
  14171. * structures are not yet restored. Since this function is at a very
  14172. * paranoid "someone might have enabled VGA while we were not looking"
  14173. * level, just check if the power well is enabled instead of trying to
  14174. * follow the "don't touch the power well if we don't need it" policy
  14175. * the rest of the driver uses. */
  14176. if (!intel_display_power_get_if_enabled(dev_priv, POWER_DOMAIN_VGA))
  14177. return;
  14178. i915_redisable_vga_power_on(dev_priv);
  14179. intel_display_power_put(dev_priv, POWER_DOMAIN_VGA);
  14180. }
  14181. static bool primary_get_hw_state(struct intel_plane *plane)
  14182. {
  14183. struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
  14184. return I915_READ(DSPCNTR(plane->plane)) & DISPLAY_PLANE_ENABLE;
  14185. }
  14186. /* FIXME read out full plane state for all planes */
  14187. static void readout_plane_state(struct intel_crtc *crtc)
  14188. {
  14189. struct drm_plane *primary = crtc->base.primary;
  14190. struct intel_plane_state *plane_state =
  14191. to_intel_plane_state(primary->state);
  14192. plane_state->base.visible = crtc->active &&
  14193. primary_get_hw_state(to_intel_plane(primary));
  14194. if (plane_state->base.visible)
  14195. crtc->base.state->plane_mask |= 1 << drm_plane_index(primary);
  14196. }
  14197. static void intel_modeset_readout_hw_state(struct drm_device *dev)
  14198. {
  14199. struct drm_i915_private *dev_priv = to_i915(dev);
  14200. enum pipe pipe;
  14201. struct intel_crtc *crtc;
  14202. struct intel_encoder *encoder;
  14203. struct intel_connector *connector;
  14204. int i;
  14205. dev_priv->active_crtcs = 0;
  14206. for_each_intel_crtc(dev, crtc) {
  14207. struct intel_crtc_state *crtc_state =
  14208. to_intel_crtc_state(crtc->base.state);
  14209. __drm_atomic_helper_crtc_destroy_state(&crtc_state->base);
  14210. memset(crtc_state, 0, sizeof(*crtc_state));
  14211. crtc_state->base.crtc = &crtc->base;
  14212. crtc_state->base.active = crtc_state->base.enable =
  14213. dev_priv->display.get_pipe_config(crtc, crtc_state);
  14214. crtc->base.enabled = crtc_state->base.enable;
  14215. crtc->active = crtc_state->base.active;
  14216. if (crtc_state->base.active)
  14217. dev_priv->active_crtcs |= 1 << crtc->pipe;
  14218. readout_plane_state(crtc);
  14219. DRM_DEBUG_KMS("[CRTC:%d:%s] hw state readout: %s\n",
  14220. crtc->base.base.id, crtc->base.name,
  14221. enableddisabled(crtc_state->base.active));
  14222. }
  14223. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  14224. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  14225. pll->on = pll->funcs.get_hw_state(dev_priv, pll,
  14226. &pll->state.hw_state);
  14227. pll->state.crtc_mask = 0;
  14228. for_each_intel_crtc(dev, crtc) {
  14229. struct intel_crtc_state *crtc_state =
  14230. to_intel_crtc_state(crtc->base.state);
  14231. if (crtc_state->base.active &&
  14232. crtc_state->shared_dpll == pll)
  14233. pll->state.crtc_mask |= 1 << crtc->pipe;
  14234. }
  14235. pll->active_mask = pll->state.crtc_mask;
  14236. DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
  14237. pll->name, pll->state.crtc_mask, pll->on);
  14238. }
  14239. for_each_intel_encoder(dev, encoder) {
  14240. pipe = 0;
  14241. if (encoder->get_hw_state(encoder, &pipe)) {
  14242. struct intel_crtc_state *crtc_state;
  14243. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  14244. crtc_state = to_intel_crtc_state(crtc->base.state);
  14245. encoder->base.crtc = &crtc->base;
  14246. crtc_state->output_types |= 1 << encoder->type;
  14247. encoder->get_config(encoder, crtc_state);
  14248. } else {
  14249. encoder->base.crtc = NULL;
  14250. }
  14251. DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
  14252. encoder->base.base.id, encoder->base.name,
  14253. enableddisabled(encoder->base.crtc),
  14254. pipe_name(pipe));
  14255. }
  14256. for_each_intel_connector(dev, connector) {
  14257. if (connector->get_hw_state(connector)) {
  14258. connector->base.dpms = DRM_MODE_DPMS_ON;
  14259. encoder = connector->encoder;
  14260. connector->base.encoder = &encoder->base;
  14261. if (encoder->base.crtc &&
  14262. encoder->base.crtc->state->active) {
  14263. /*
  14264. * This has to be done during hardware readout
  14265. * because anything calling .crtc_disable may
  14266. * rely on the connector_mask being accurate.
  14267. */
  14268. encoder->base.crtc->state->connector_mask |=
  14269. 1 << drm_connector_index(&connector->base);
  14270. encoder->base.crtc->state->encoder_mask |=
  14271. 1 << drm_encoder_index(&encoder->base);
  14272. }
  14273. } else {
  14274. connector->base.dpms = DRM_MODE_DPMS_OFF;
  14275. connector->base.encoder = NULL;
  14276. }
  14277. DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
  14278. connector->base.base.id, connector->base.name,
  14279. enableddisabled(connector->base.encoder));
  14280. }
  14281. for_each_intel_crtc(dev, crtc) {
  14282. struct intel_crtc_state *crtc_state =
  14283. to_intel_crtc_state(crtc->base.state);
  14284. int pixclk = 0;
  14285. crtc->base.hwmode = crtc_state->base.adjusted_mode;
  14286. memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
  14287. if (crtc_state->base.active) {
  14288. intel_mode_from_pipe_config(&crtc->base.mode, crtc_state);
  14289. intel_mode_from_pipe_config(&crtc_state->base.adjusted_mode, crtc_state);
  14290. WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
  14291. /*
  14292. * The initial mode needs to be set in order to keep
  14293. * the atomic core happy. It wants a valid mode if the
  14294. * crtc's enabled, so we do the above call.
  14295. *
  14296. * But we don't set all the derived state fully, hence
  14297. * set a flag to indicate that a full recalculation is
  14298. * needed on the next commit.
  14299. */
  14300. crtc_state->base.mode.private_flags = I915_MODE_FLAG_INHERITED;
  14301. if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
  14302. pixclk = ilk_pipe_pixel_rate(crtc_state);
  14303. else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  14304. pixclk = crtc_state->base.adjusted_mode.crtc_clock;
  14305. else
  14306. WARN_ON(dev_priv->display.modeset_calc_cdclk);
  14307. /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
  14308. if (IS_BROADWELL(dev_priv) && crtc_state->ips_enabled)
  14309. pixclk = DIV_ROUND_UP(pixclk * 100, 95);
  14310. drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
  14311. update_scanline_offset(crtc);
  14312. }
  14313. dev_priv->min_pixclk[crtc->pipe] = pixclk;
  14314. intel_pipe_config_sanity_check(dev_priv, crtc_state);
  14315. }
  14316. }
  14317. /* Scan out the current hw modeset state,
  14318. * and sanitizes it to the current state
  14319. */
  14320. static void
  14321. intel_modeset_setup_hw_state(struct drm_device *dev)
  14322. {
  14323. struct drm_i915_private *dev_priv = to_i915(dev);
  14324. enum pipe pipe;
  14325. struct intel_crtc *crtc;
  14326. struct intel_encoder *encoder;
  14327. int i;
  14328. intel_modeset_readout_hw_state(dev);
  14329. /* HW state is read out, now we need to sanitize this mess. */
  14330. for_each_intel_encoder(dev, encoder) {
  14331. intel_sanitize_encoder(encoder);
  14332. }
  14333. for_each_pipe(dev_priv, pipe) {
  14334. crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
  14335. intel_sanitize_crtc(crtc);
  14336. intel_dump_pipe_config(crtc, crtc->config,
  14337. "[setup_hw_state]");
  14338. }
  14339. intel_modeset_update_connector_atomic_state(dev);
  14340. for (i = 0; i < dev_priv->num_shared_dpll; i++) {
  14341. struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
  14342. if (!pll->on || pll->active_mask)
  14343. continue;
  14344. DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
  14345. pll->funcs.disable(dev_priv, pll);
  14346. pll->on = false;
  14347. }
  14348. if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
  14349. vlv_wm_get_hw_state(dev);
  14350. else if (IS_GEN9(dev_priv))
  14351. skl_wm_get_hw_state(dev);
  14352. else if (HAS_PCH_SPLIT(dev_priv))
  14353. ilk_wm_get_hw_state(dev);
  14354. for_each_intel_crtc(dev, crtc) {
  14355. unsigned long put_domains;
  14356. put_domains = modeset_get_crtc_power_domains(&crtc->base, crtc->config);
  14357. if (WARN_ON(put_domains))
  14358. modeset_put_power_domains(dev_priv, put_domains);
  14359. }
  14360. intel_display_set_init_power(dev_priv, false);
  14361. intel_fbc_init_pipe_state(dev_priv);
  14362. }
  14363. void intel_display_resume(struct drm_device *dev)
  14364. {
  14365. struct drm_i915_private *dev_priv = to_i915(dev);
  14366. struct drm_atomic_state *state = dev_priv->modeset_restore_state;
  14367. struct drm_modeset_acquire_ctx ctx;
  14368. int ret;
  14369. dev_priv->modeset_restore_state = NULL;
  14370. if (state)
  14371. state->acquire_ctx = &ctx;
  14372. /*
  14373. * This is a cludge because with real atomic modeset mode_config.mutex
  14374. * won't be taken. Unfortunately some probed state like
  14375. * audio_codec_enable is still protected by mode_config.mutex, so lock
  14376. * it here for now.
  14377. */
  14378. mutex_lock(&dev->mode_config.mutex);
  14379. drm_modeset_acquire_init(&ctx, 0);
  14380. while (1) {
  14381. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  14382. if (ret != -EDEADLK)
  14383. break;
  14384. drm_modeset_backoff(&ctx);
  14385. }
  14386. if (!ret)
  14387. ret = __intel_display_resume(dev, state);
  14388. drm_modeset_drop_locks(&ctx);
  14389. drm_modeset_acquire_fini(&ctx);
  14390. mutex_unlock(&dev->mode_config.mutex);
  14391. if (ret)
  14392. DRM_ERROR("Restoring old state failed with %i\n", ret);
  14393. if (state)
  14394. drm_atomic_state_put(state);
  14395. }
  14396. void intel_modeset_gem_init(struct drm_device *dev)
  14397. {
  14398. struct drm_i915_private *dev_priv = to_i915(dev);
  14399. intel_init_gt_powersave(dev_priv);
  14400. intel_modeset_init_hw(dev);
  14401. intel_setup_overlay(dev_priv);
  14402. }
  14403. int intel_connector_register(struct drm_connector *connector)
  14404. {
  14405. struct intel_connector *intel_connector = to_intel_connector(connector);
  14406. int ret;
  14407. ret = intel_backlight_device_register(intel_connector);
  14408. if (ret)
  14409. goto err;
  14410. return 0;
  14411. err:
  14412. return ret;
  14413. }
  14414. void intel_connector_unregister(struct drm_connector *connector)
  14415. {
  14416. struct intel_connector *intel_connector = to_intel_connector(connector);
  14417. intel_backlight_device_unregister(intel_connector);
  14418. intel_panel_destroy_backlight(connector);
  14419. }
  14420. void intel_modeset_cleanup(struct drm_device *dev)
  14421. {
  14422. struct drm_i915_private *dev_priv = to_i915(dev);
  14423. intel_disable_gt_powersave(dev_priv);
  14424. /*
  14425. * Interrupts and polling as the first thing to avoid creating havoc.
  14426. * Too much stuff here (turning of connectors, ...) would
  14427. * experience fancy races otherwise.
  14428. */
  14429. intel_irq_uninstall(dev_priv);
  14430. /*
  14431. * Due to the hpd irq storm handling the hotplug work can re-arm the
  14432. * poll handlers. Hence disable polling after hpd handling is shut down.
  14433. */
  14434. drm_kms_helper_poll_fini(dev);
  14435. intel_unregister_dsm_handler();
  14436. intel_fbc_global_disable(dev_priv);
  14437. /* flush any delayed tasks or pending work */
  14438. flush_scheduled_work();
  14439. drm_mode_config_cleanup(dev);
  14440. intel_cleanup_overlay(dev_priv);
  14441. intel_cleanup_gt_powersave(dev_priv);
  14442. intel_teardown_gmbus(dev_priv);
  14443. }
  14444. void intel_connector_attach_encoder(struct intel_connector *connector,
  14445. struct intel_encoder *encoder)
  14446. {
  14447. connector->encoder = encoder;
  14448. drm_mode_connector_attach_encoder(&connector->base,
  14449. &encoder->base);
  14450. }
  14451. /*
  14452. * set vga decode state - true == enable VGA decode
  14453. */
  14454. int intel_modeset_vga_set_state(struct drm_i915_private *dev_priv, bool state)
  14455. {
  14456. unsigned reg = INTEL_GEN(dev_priv) >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
  14457. u16 gmch_ctrl;
  14458. if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
  14459. DRM_ERROR("failed to read control word\n");
  14460. return -EIO;
  14461. }
  14462. if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
  14463. return 0;
  14464. if (state)
  14465. gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
  14466. else
  14467. gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
  14468. if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
  14469. DRM_ERROR("failed to write control word\n");
  14470. return -EIO;
  14471. }
  14472. return 0;
  14473. }
  14474. #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
  14475. struct intel_display_error_state {
  14476. u32 power_well_driver;
  14477. int num_transcoders;
  14478. struct intel_cursor_error_state {
  14479. u32 control;
  14480. u32 position;
  14481. u32 base;
  14482. u32 size;
  14483. } cursor[I915_MAX_PIPES];
  14484. struct intel_pipe_error_state {
  14485. bool power_domain_on;
  14486. u32 source;
  14487. u32 stat;
  14488. } pipe[I915_MAX_PIPES];
  14489. struct intel_plane_error_state {
  14490. u32 control;
  14491. u32 stride;
  14492. u32 size;
  14493. u32 pos;
  14494. u32 addr;
  14495. u32 surface;
  14496. u32 tile_offset;
  14497. } plane[I915_MAX_PIPES];
  14498. struct intel_transcoder_error_state {
  14499. bool power_domain_on;
  14500. enum transcoder cpu_transcoder;
  14501. u32 conf;
  14502. u32 htotal;
  14503. u32 hblank;
  14504. u32 hsync;
  14505. u32 vtotal;
  14506. u32 vblank;
  14507. u32 vsync;
  14508. } transcoder[4];
  14509. };
  14510. struct intel_display_error_state *
  14511. intel_display_capture_error_state(struct drm_i915_private *dev_priv)
  14512. {
  14513. struct intel_display_error_state *error;
  14514. int transcoders[] = {
  14515. TRANSCODER_A,
  14516. TRANSCODER_B,
  14517. TRANSCODER_C,
  14518. TRANSCODER_EDP,
  14519. };
  14520. int i;
  14521. if (INTEL_INFO(dev_priv)->num_pipes == 0)
  14522. return NULL;
  14523. error = kzalloc(sizeof(*error), GFP_ATOMIC);
  14524. if (error == NULL)
  14525. return NULL;
  14526. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  14527. error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
  14528. for_each_pipe(dev_priv, i) {
  14529. error->pipe[i].power_domain_on =
  14530. __intel_display_power_is_enabled(dev_priv,
  14531. POWER_DOMAIN_PIPE(i));
  14532. if (!error->pipe[i].power_domain_on)
  14533. continue;
  14534. error->cursor[i].control = I915_READ(CURCNTR(i));
  14535. error->cursor[i].position = I915_READ(CURPOS(i));
  14536. error->cursor[i].base = I915_READ(CURBASE(i));
  14537. error->plane[i].control = I915_READ(DSPCNTR(i));
  14538. error->plane[i].stride = I915_READ(DSPSTRIDE(i));
  14539. if (INTEL_GEN(dev_priv) <= 3) {
  14540. error->plane[i].size = I915_READ(DSPSIZE(i));
  14541. error->plane[i].pos = I915_READ(DSPPOS(i));
  14542. }
  14543. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  14544. error->plane[i].addr = I915_READ(DSPADDR(i));
  14545. if (INTEL_GEN(dev_priv) >= 4) {
  14546. error->plane[i].surface = I915_READ(DSPSURF(i));
  14547. error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
  14548. }
  14549. error->pipe[i].source = I915_READ(PIPESRC(i));
  14550. if (HAS_GMCH_DISPLAY(dev_priv))
  14551. error->pipe[i].stat = I915_READ(PIPESTAT(i));
  14552. }
  14553. /* Note: this does not include DSI transcoders. */
  14554. error->num_transcoders = INTEL_INFO(dev_priv)->num_pipes;
  14555. if (HAS_DDI(dev_priv))
  14556. error->num_transcoders++; /* Account for eDP. */
  14557. for (i = 0; i < error->num_transcoders; i++) {
  14558. enum transcoder cpu_transcoder = transcoders[i];
  14559. error->transcoder[i].power_domain_on =
  14560. __intel_display_power_is_enabled(dev_priv,
  14561. POWER_DOMAIN_TRANSCODER(cpu_transcoder));
  14562. if (!error->transcoder[i].power_domain_on)
  14563. continue;
  14564. error->transcoder[i].cpu_transcoder = cpu_transcoder;
  14565. error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
  14566. error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
  14567. error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
  14568. error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
  14569. error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
  14570. error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
  14571. error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
  14572. }
  14573. return error;
  14574. }
  14575. #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
  14576. void
  14577. intel_display_print_error_state(struct drm_i915_error_state_buf *m,
  14578. struct drm_i915_private *dev_priv,
  14579. struct intel_display_error_state *error)
  14580. {
  14581. int i;
  14582. if (!error)
  14583. return;
  14584. err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev_priv)->num_pipes);
  14585. if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
  14586. err_printf(m, "PWR_WELL_CTL2: %08x\n",
  14587. error->power_well_driver);
  14588. for_each_pipe(dev_priv, i) {
  14589. err_printf(m, "Pipe [%d]:\n", i);
  14590. err_printf(m, " Power: %s\n",
  14591. onoff(error->pipe[i].power_domain_on));
  14592. err_printf(m, " SRC: %08x\n", error->pipe[i].source);
  14593. err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
  14594. err_printf(m, "Plane [%d]:\n", i);
  14595. err_printf(m, " CNTR: %08x\n", error->plane[i].control);
  14596. err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
  14597. if (INTEL_GEN(dev_priv) <= 3) {
  14598. err_printf(m, " SIZE: %08x\n", error->plane[i].size);
  14599. err_printf(m, " POS: %08x\n", error->plane[i].pos);
  14600. }
  14601. if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
  14602. err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
  14603. if (INTEL_GEN(dev_priv) >= 4) {
  14604. err_printf(m, " SURF: %08x\n", error->plane[i].surface);
  14605. err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
  14606. }
  14607. err_printf(m, "Cursor [%d]:\n", i);
  14608. err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
  14609. err_printf(m, " POS: %08x\n", error->cursor[i].position);
  14610. err_printf(m, " BASE: %08x\n", error->cursor[i].base);
  14611. }
  14612. for (i = 0; i < error->num_transcoders; i++) {
  14613. err_printf(m, "CPU transcoder: %s\n",
  14614. transcoder_name(error->transcoder[i].cpu_transcoder));
  14615. err_printf(m, " Power: %s\n",
  14616. onoff(error->transcoder[i].power_domain_on));
  14617. err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
  14618. err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
  14619. err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
  14620. err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
  14621. err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
  14622. err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
  14623. err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
  14624. }
  14625. }
  14626. #endif